blob: 6a378442c875b26f5f40266e897adbad8c9bbd9f [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)
532 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100533 int off;
534
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100535 // Avoid starting halfway a multi-byte character.
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200536 if (b_insert)
537 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100538 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200539 }
540 else
541 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100542 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200543 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200544 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100545 spaces -= off;
546 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200547 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200548 if (spaces < 0) // can happen when the cursor was moved
549 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200550
Bram Moolenaar964b3742019-05-24 18:54:09 +0200551 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (newp == NULL)
553 continue;
554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100555 // copy up to shifted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 mch_memmove(newp, oldp, (size_t)(offset));
557 oldp += offset;
558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200560 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200561 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100563 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200564 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 offset += s_len;
566
567 if (spaces && !bdp->is_short)
568 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 // insert post-padding
Bram Moolenaardac13472019-09-16 21:06:21 +0200570 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100571 // We're splitting a TAB, don't copy it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 oldp++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100573 // We allowed for that TAB, remember this now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 count++;
575 }
576
577 if (spaces > 0)
578 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000579 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
581 ml_replace(lnum, newp, FALSE);
582
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200583 if (b_insert)
584 // correct any text properties
585 inserted_bytes(lnum, startcol, s_len);
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (lnum == oap->end.lnum)
588 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100589 // Set "']" mark to the end of the block instead of the end of
590 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 curbuf->b_op_end.lnum = oap->end.lnum;
592 curbuf->b_op_end.col = offset;
593 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
597
598 State = oldstate;
599}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200602 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200604 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 */
606 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100607op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 int n;
610 linenr_T lnum;
611 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 char_u *newp, *oldp;
613 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
615 int did_yank = FALSE;
616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 return OK;
619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100620 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 if (oap->empty)
622 return u_save_cursor();
623
624 if (!curbuf->b_p_ma)
625 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200626 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 return FAIL;
628 }
629
630#ifdef FEAT_CLIPBOARD
631 adjust_clip_reg(&oap->regname);
632#endif
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (has_mbyte)
635 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
Bram Moolenaard04b7502010-07-08 22:27:55 +0200637 /*
638 * Imitate the strange Vi behaviour: If the delete spans more than one
639 * line and motion_type == MCHAR and the result is a blank line, make the
640 * delete linewise. Don't do this for the change command or Visual mode.
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000644 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100646 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 && oap->op_type == OP_DELETE)
648 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200649 ptr = ml_get(oap->end.lnum) + oap->end.col;
650 if (*ptr != NUL)
651 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 ptr = skipwhite(ptr);
653 if (*ptr == NUL && inindent(0))
654 oap->motion_type = MLINE;
655 }
656
Bram Moolenaard04b7502010-07-08 22:27:55 +0200657 /*
658 * Check for trying to delete (e.g. "D") in an empty line.
659 * Note: For the change operator it is ok.
660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if ( oap->motion_type == MCHAR
662 && oap->line_count == 1
663 && oap->op_type == OP_DELETE
664 && *ml_get(oap->start.lnum) == NUL)
665 {
666 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 * 'cpoptions' (Vi compatible).
669 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000670 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100671 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
672 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000673 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
675 beep_flush();
676 return OK;
677 }
678
Bram Moolenaard04b7502010-07-08 22:27:55 +0200679 /*
680 * Do a yank of whatever we're about to delete.
681 * If a yank register was specified, put the deleted text into that
682 * register. For the black hole register '_' don't yank anything.
683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (oap->regname != '_')
685 {
686 if (oap->regname != 0)
687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100688 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (!valid_yank_reg(oap->regname, TRUE))
690 {
691 beep_flush();
692 return OK;
693 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100694 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
695 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 did_yank = TRUE;
697 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200698 else
699 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
701 /*
702 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100703 * delete contains a line break, or when using a specific operator (Vi
704 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200705 * Use the register name from before adjust_clip_reg() may have
706 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100708 if (oap->motion_type == MLINE || oap->line_count > 1
709 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100711 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (op_yank(oap, TRUE, FALSE) == OK)
713 did_yank = TRUE;
714 }
715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100716 // Yank into small delete register when no named register specified
717 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200718 if ((
719#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200720 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
721 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200722#endif
723 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 && oap->line_count == 1)
725 {
726 oap->regname = '-';
727 get_yank_register(oap->regname, TRUE);
728 if (op_yank(oap, TRUE, FALSE) == OK)
729 did_yank = TRUE;
730 oap->regname = 0;
731 }
732
733 /*
734 * If there's too much stuff to fit in the yank register, then get a
735 * confirmation before doing the delete. This is crude, but simple.
736 * And it avoids doing a delete of something we can't put back if we
737 * want.
738 */
739 if (!did_yank)
740 {
741 int msg_silent_save = msg_silent;
742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100743 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
745 msg_silent = msg_silent_save;
746 if (n != 'y')
747 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000748 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 return FAIL;
750 }
751 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100752
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100753#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100754 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200755 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 }
758
Bram Moolenaard04b7502010-07-08 22:27:55 +0200759 /*
760 * block mode delete
761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (oap->block_mode)
763 {
764 if (u_save((linenr_T)(oap->start.lnum - 1),
765 (linenr_T)(oap->end.lnum + 1)) == FAIL)
766 return FAIL;
767
768 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
769 {
770 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 continue;
773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100774 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (lnum == curwin->w_cursor.lnum)
776 {
777 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780
Bram Moolenaar8055d172019-05-17 22:57:26 +0200781 // "n" == number of chars deleted
782 // If we delete a TAB, it may be replaced by several characters.
783 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 n = bd.textlen - bd.startspaces - bd.endspaces;
785 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200786 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (newp == NULL)
788 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200792 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100794 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000796 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200799
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100800#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200801 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200802 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805
806 check_cursor_col();
807 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
808 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100811 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {
813 if (oap->op_type == OP_CHANGE)
814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100815 // Delete the lines except the first one. Temporarily move the
816 // cursor to the next line. Save the current line number, if the
817 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (oap->line_count > 1)
819 {
820 lnum = curwin->w_cursor.lnum;
821 ++curwin->w_cursor.lnum;
822 del_lines((long)(oap->line_count - 1), TRUE);
823 curwin->w_cursor.lnum = lnum;
824 }
825 if (u_save_cursor() == FAIL)
826 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 beginline(BL_WHITE); // cursor on first non-white
830 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ai_col = curwin->w_cursor.col;
832 }
833 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100834 beginline(0); // cursor in column 0
835 truncate_line(FALSE); // delete the rest of the line
836 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 else
841 {
842 del_lines(oap->line_count, TRUE);
843 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 }
847 else
848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (virtual_op)
850 {
851 int endcol = 0;
852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (gchar_pos(&oap->start) == '\t')
855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return FAIL;
858 if (oap->line_count == 1)
859 endcol = getviscol2(oap->end.col, oap->end.coladd);
860 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
861 oap->start = curwin->w_cursor;
862 if (oap->line_count == 1)
863 {
864 coladvance(endcol);
865 oap->end.col = curwin->w_cursor.col;
866 oap->end.coladd = curwin->w_cursor.coladd;
867 curwin->w_cursor = oap->start;
868 }
869 }
870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (gchar_pos(&oap->end) == '\t'
873 && (int)oap->end.coladd < oap->inclusive)
874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (u_save((linenr_T)(oap->end.lnum - 1),
877 (linenr_T)(oap->end.lnum + 1)) == FAIL)
878 return FAIL;
879 curwin->w_cursor = oap->end;
880 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
881 oap->end = curwin->w_cursor;
882 curwin->w_cursor = oap->start;
883 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100884 if (has_mbyte)
885 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100888 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100890 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return FAIL;
892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100894 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 && oap->op_type == OP_CHANGE
896 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100897 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 display_dollar(oap->end.col - !oap->inclusive);
899
900 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (virtual_op)
903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100904 // fix up things for virtualedit-delete:
905 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 char_u *curline = ml_get_curline();
907 int len = (int)STRLEN(curline);
908
909 if (oap->end.coladd != 0
910 && (int)oap->end.col >= len - 1
911 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
912 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100913 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (n == 0 && oap->start.coladd != oap->end.coladd)
915 n = 1;
916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (gchar_cursor() != NUL)
919 curwin->w_cursor.coladd = 0;
920 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200921 (void)del_bytes((long)n, !virtual_op,
922 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
926 pos_T curpos;
927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100928 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
930 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
931 return FAIL;
932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100933 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 ++curwin->w_cursor.lnum;
937 del_lines((long)(oap->line_count - 2), FALSE);
938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100939 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200940 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200941 curwin->w_cursor.col = 0;
942 (void)del_bytes((long)n, !virtual_op,
943 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100944 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200945 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200947 if (oap->op_type == OP_DELETE)
948 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950
951 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
952
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000953setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200954 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100956 if (oap->block_mode)
957 {
958 curbuf->b_op_end.lnum = oap->end.lnum;
959 curbuf->b_op_end.col = oap->start.col;
960 }
961 else
962 curbuf->b_op_end = oap->start;
963 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 return OK;
967}
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969/*
970 * Adjust end of operating area for ending on a multi-byte character.
971 * Used for deletion.
972 */
973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100974mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 char_u *p;
977
978 if (oap->inclusive)
979 {
980 p = ml_get(oap->end.lnum);
981 oap->end.col += mb_tail_off(p, p + oap->end.col);
982 }
983}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar630afe82018-06-28 19:26:28 +0200985/*
986 * Replace the character under the cursor with "c".
987 * This takes care of multi-byte characters.
988 */
989 static void
990replace_character(int c)
991{
992 int n = State;
993
994 State = REPLACE;
995 ins_char(c);
996 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100997 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200998 dec_cursor();
999}
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/*
1002 * Replace a whole area with one character.
1003 */
1004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001005op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 char_u *newp, *oldp;
1010 size_t oldlen;
1011 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001012 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001013 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001016 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001018 if (c == REPLACE_CR_NCHAR)
1019 {
1020 had_ctrl_v_cr = TRUE;
1021 c = CAR;
1022 }
1023 else if (c == REPLACE_NL_NCHAR)
1024 {
1025 had_ctrl_v_cr = TRUE;
1026 c = NL;
1027 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (has_mbyte)
1030 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 if (u_save((linenr_T)(oap->start.lnum - 1),
1033 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1034 return FAIL;
1035
1036 /*
1037 * block mode replace
1038 */
1039 if (oap->block_mode)
1040 {
1041 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1042 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1043 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1046 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // n == number of extra chars required
1050 // If we split a TAB, it may be replaced by several characters.
1051 // Thus the number of characters may increase!
1052 // If the range starts in virtual space, count the initial
1053 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1055 {
1056 pos_T vpos;
1057
Bram Moolenaara1381de2009-11-03 15:44:21 +00001058 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 getvpos(&vpos, oap->start_vcol);
1060 bd.startspaces += vpos.coladd;
1061 n = bd.startspaces;
1062 }
1063 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 numc = oap->end_vcol - oap->start_vcol + 1;
1073 if (bd.is_short && (!virtual_op || bd.is_MAX))
1074 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001076 // A double-wide character can be replaced only up to half the
1077 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if ((*mb_char2cells)(c) > 1)
1079 {
1080 if ((numc & 1) && !bd.is_short)
1081 {
1082 ++bd.endspaces;
1083 ++n;
1084 }
1085 numc = numc / 2;
1086 }
1087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001088 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 num_chars = numc;
1090 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001091 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 n += numc - bd.textlen;
1093
1094 oldp = ml_get_curline();
1095 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001096 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (newp == NULL)
1098 continue;
1099 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 mch_memmove(newp, oldp, (size_t)bd.textcol);
1102 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001103 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001104 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001105 // insert replacement chars CHECK FOR ALLOCATED SPACE
1106 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1107 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001108 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001110 if (has_mbyte)
1111 {
1112 n = (int)STRLEN(newp);
1113 while (--num_chars >= 0)
1114 n += (*mb_char2bytes)(c, newp + n);
1115 }
1116 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001117 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001118 if (!bd.is_short)
1119 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001120 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001121 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001122 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001123 STRMOVE(newp + STRLEN(newp), oldp);
1124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001128 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001129 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001130 if (after_p != NULL)
1131 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001133 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001135 if (after_p != NULL)
1136 {
1137 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1138 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1139 oap->end.lnum++;
1140 vim_free(after_p);
1141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 }
1144 else
1145 {
1146 /*
1147 * MCHAR and MLINE motion replace.
1148 */
1149 if (oap->motion_type == MLINE)
1150 {
1151 oap->start.col = 0;
1152 curwin->w_cursor.col = 0;
1153 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1154 if (oap->end.col)
1155 --oap->end.col;
1156 }
1157 else if (!oap->inclusive)
1158 dec(&(oap->end));
1159
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001160 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
1162 n = gchar_cursor();
1163 if (n != NUL)
1164 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001165 int new_byte_len = (*mb_char2len)(c);
1166 int old_byte_len = mb_ptr2len(ml_get_cursor());
1167
1168 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 // This is slow, but it handles replacing a single-byte
1171 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001172 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001173 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001174 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (n == TAB)
1179 {
1180 int end_vcol = 0;
1181
1182 if (curwin->w_cursor.lnum == oap->end.lnum)
1183 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001184 // oap->end has to be recalculated when
1185 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 end_vcol = getviscol2(oap->end.col,
1187 oap->end.coladd);
1188 }
1189 coladvance_force(getviscol());
1190 if (curwin->w_cursor.lnum == oap->end.lnum)
1191 getvpos(&oap->end, end_vcol);
1192 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001193 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1197 {
1198 int virtcols = oap->end.coladd;
1199
1200 if (curwin->w_cursor.lnum == oap->start.lnum
1201 && oap->start.col == oap->end.col && oap->start.coladd)
1202 virtcols -= oap->start.coladd;
1203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001204 // oap->end has been trimmed so it's effectively inclusive;
1205 // as a result an extra +1 must be counted so we don't
1206 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1208 curwin->w_cursor.col -= (virtcols + 1);
1209 for (; virtcols >= 0; virtcols--)
1210 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001211 if ((*mb_char2len)(c) > 1)
1212 replace_character(c);
1213 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001214 PBYTE(curwin->w_cursor, c);
1215 if (inc(&curwin->w_cursor) == -1)
1216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (inc_cursor() == -1)
1222 break;
1223 }
1224 }
1225
1226 curwin->w_cursor = oap->start;
1227 check_cursor();
1228 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1229
Bram Moolenaare1004402020-10-24 20:49:43 +02001230 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001232 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001233 curbuf->b_op_start = oap->start;
1234 curbuf->b_op_end = oap->end;
1235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
1237 return OK;
1238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001240static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242/*
1243 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1244 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001246op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001250 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 if (u_save((linenr_T)(oap->start.lnum - 1),
1253 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1254 return;
1255
1256 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1260 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001261 int one_change;
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 block_prep(oap, &bd, pos.lnum, FALSE);
1264 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001265 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1266 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001267
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001268#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001269 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
1271 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1272
Bram Moolenaar009b2592004-10-24 19:18:58 +00001273 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1274 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001276 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280 if (did_change)
1281 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1282 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {
1285 if (oap->motion_type == MLINE)
1286 {
1287 oap->start.col = 0;
1288 pos.col = 0;
1289 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1290 if (oap->end.col)
1291 --oap->end.col;
1292 }
1293 else if (!oap->inclusive)
1294 dec(&(oap->end));
1295
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001296 if (pos.lnum == oap->end.lnum)
1297 did_change = swapchars(oap->op_type, &pos,
1298 oap->end.col - pos.col + 1);
1299 else
1300 for (;;)
1301 {
1302 did_change |= swapchars(oap->op_type, &pos,
1303 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1304 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001305 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001306 break;
1307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (did_change)
1309 {
1310 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1311 0L);
1312#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001313 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
1315 char_u *ptr;
1316 int count;
1317
1318 pos = oap->start;
1319 while (pos.lnum < oap->end.lnum)
1320 {
1321 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001322 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001323 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001325 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 pos.col = 0;
1327 pos.lnum++;
1328 }
1329 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1330 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001331 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001333 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
1335#endif
1336 }
1337 }
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001340 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
Bram Moolenaare1004402020-10-24 20:49:43 +02001343 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001344 {
1345 // Set '[ and '] marks.
1346 curbuf->b_op_start = oap->start;
1347 curbuf->b_op_end = oap->end;
1348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
1350 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001351 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001352 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353}
1354
1355/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001356 * Invoke swapchar() on "length" bytes at position "pos".
1357 * "pos" is advanced to just after the changed characters.
1358 * "length" is rounded up to include the whole last multi-byte character.
1359 * Also works correctly when the number of bytes changes.
1360 * Returns TRUE if some character was changed.
1361 */
1362 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001363swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001364{
1365 int todo;
1366 int did_change = 0;
1367
1368 for (todo = length; todo > 0; --todo)
1369 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001370 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001371 {
1372 int len = (*mb_ptr2len)(ml_get_pos(pos));
1373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001374 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001375 if (len > 0)
1376 todo -= len - 1;
1377 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001378 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001379 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001380 break;
1381 }
1382 return did_change;
1383}
1384
1385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 * If op_type == OP_UPPER: make uppercase,
1387 * if op_type == OP_LOWER: make lowercase,
1388 * if op_type == OP_ROT13: do rot13 encoding,
1389 * else swap case of character at 'pos'
1390 * returns TRUE when something actually changed.
1391 */
1392 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001393swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 int c;
1396 int nc;
1397
1398 c = gchar_pos(pos);
1399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001400 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (c >= 0x80 && op_type == OP_ROT13)
1402 return FALSE;
1403
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001404 if (op_type == OP_UPPER && c == 0xdf
1405 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001406 {
1407 pos_T sp = curwin->w_cursor;
1408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001409 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001410 curwin->w_cursor = *pos;
1411 del_char(FALSE);
1412 ins_char('S');
1413 ins_char('S');
1414 curwin->w_cursor = sp;
1415 inc(pos);
1416 }
1417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001418 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 nc = c;
1421 if (MB_ISLOWER(c))
1422 {
1423 if (op_type == OP_ROT13)
1424 nc = ROT13(c, 'a');
1425 else if (op_type != OP_LOWER)
1426 nc = MB_TOUPPER(c);
1427 }
1428 else if (MB_ISUPPER(c))
1429 {
1430 if (op_type == OP_ROT13)
1431 nc = ROT13(c, 'A');
1432 else if (op_type != OP_UPPER)
1433 nc = MB_TOLOWER(c);
1434 }
1435 if (nc != c)
1436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1438 {
1439 pos_T sp = curwin->w_cursor;
1440
1441 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001442 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001443 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 ins_char(nc);
1445 curwin->w_cursor = sp;
1446 }
1447 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001448 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 return TRUE;
1450 }
1451 return FALSE;
1452}
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454/*
1455 * op_insert - Insert and append operators for Visual mode.
1456 */
1457 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001458op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 long ins_len, pre_textlen = 0;
1461 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001462 colnr_T ind_pre_col = 0, ind_post_col;
1463 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 struct block_def bd;
1465 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001466 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001467 pos_T start_insert;
1468 // offset when cursor was moved in insert mode
1469 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001474 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 curwin->w_cursor.lnum = oap->start.lnum;
1476 update_screen(INVERTED);
1477
1478 if (oap->block_mode)
1479 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001480 // When 'virtualedit' is used, need to insert the extra spaces before
1481 // doing block_prep(). When only "block" is used, virtual edit is
1482 // already disabled, but still need it when calling
1483 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001484 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001485 // state for the current window. To override that state, we need to
1486 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (curwin->w_cursor.coladd > 0)
1488 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001489 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (u_save_cursor() == FAIL)
1492 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001493
Gary Johnson51ad8502021-08-03 18:33:08 +02001494 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 coladvance_force(oap->op_type == OP_APPEND
1496 ? oap->end_vcol + 1 : getviscol());
1497 if (oap->op_type == OP_APPEND)
1498 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001499 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001501 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001503 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001504 ind_pre_col = (colnr_T)getwhitecols_curline();
1505 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if (oap->op_type == OP_APPEND)
1509 firstline += bd.textlen;
1510 pre_textlen = (long)STRLEN(firstline);
1511 }
1512
1513 if (oap->op_type == OP_APPEND)
1514 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001515 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001517 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 curwin->w_set_curswant = TRUE;
1519 while (*ml_get_cursor() != NUL
1520 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1521 ++curwin->w_cursor.col;
1522 if (bd.is_short && !bd.is_MAX)
1523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001524 // First line was too short, make it longer and adjust the
1525 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (u_save_cursor() == FAIL)
1527 return;
1528 for (i = 0; i < bd.endspaces; ++i)
1529 ins_char(' ');
1530 bd.textlen += bd.endspaces;
1531 }
1532 }
1533 else
1534 {
1535 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001536 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001538 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001539 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 && oap->start_vcol != oap->end_vcol)
1541 inc_cursor();
1542 }
1543 }
1544
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001545 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001546 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001547 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001549 // When a tab was inserted, and the characters in front of the tab
1550 // have been converted to a tab as well, the column of the cursor
1551 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001552 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001553 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001554 oap->start = curbuf->b_op_start_orig;
1555
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001556 // If user has moved off this line, we don't know what to do, so do
1557 // nothing.
1558 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001559 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 return;
1561
1562 if (oap->block_mode)
1563 {
1564 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001565 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001566 size_t len;
1567 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001569 // If indent kicked in, the firstline might have changed
1570 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001571 ind_post_col = (colnr_T)getwhitecols_curline();
1572 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001573 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001574 bd.textcol += ind_post_col - ind_pre_col;
1575 ind_post_vcol = get_indent();
1576 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001577 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001578 }
1579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001580 // The user may have moved the cursor before inserting something, try
1581 // to adjust the block for that. But only do it, if the difference
1582 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001583 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1584 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001585 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001586 int t = getviscol2(curbuf->b_op_start_orig.col,
1587 curbuf->b_op_start_orig.coladd);
1588
1589 if (!bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001590 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001591 if (oap->op_type == OP_INSERT
1592 && oap->start.col + oap->start.coladd
1593 != curbuf->b_op_start_orig.col
1594 + curbuf->b_op_start_orig.coladd)
1595 {
1596 oap->start.col = curbuf->b_op_start_orig.col;
1597 pre_textlen -= t - oap->start_vcol;
1598 oap->start_vcol = t;
1599 }
1600 else if (oap->op_type == OP_APPEND
1601 && oap->end.col + oap->end.coladd
1602 >= curbuf->b_op_start_orig.col
1603 + curbuf->b_op_start_orig.coladd)
1604 {
1605 oap->start.col = curbuf->b_op_start_orig.col;
1606 // reset pre_textlen to the value of OP_INSERT
1607 pre_textlen += bd.textlen;
1608 pre_textlen -= t - oap->start_vcol;
1609 oap->start_vcol = t;
1610 oap->op_type = OP_INSERT;
1611 }
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001612 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001613 else if (bd.is_MAX && oap->op_type == OP_APPEND)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001615 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001616 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001617 pre_textlen -= t - oap->start_vcol;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001618 }
1619 }
1620
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001621 // Spaces and tabs in the indent may have changed to other spaces and
1622 // tabs. Get the starting column again and correct the length.
1623 // Don't do this when "$" used, end-of-line will have changed.
1624 //
1625 // if indent was added and the inserted text was after the indent,
1626 // correct the selection for the new indent.
1627 if (did_indent && bd.textcol - ind_post_col > 0)
1628 {
1629 oap->start.col += ind_post_col - ind_pre_col;
1630 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1631 oap->end.col += ind_post_col - ind_pre_col;
1632 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001635 if (did_indent && bd.textcol - ind_post_col > 0)
1636 {
1637 // undo for where "oap" is used below
1638 oap->start.col -= ind_post_col - ind_pre_col;
1639 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1640 oap->end.col -= ind_post_col - ind_pre_col;
1641 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1644 {
1645 if (oap->op_type == OP_APPEND)
1646 {
1647 pre_textlen += bd2.textlen - bd.textlen;
1648 if (bd2.endspaces)
1649 --bd2.textlen;
1650 }
1651 bd.textcol = bd2.textcol;
1652 bd.textlen = bd2.textlen;
1653 }
1654
1655 /*
1656 * Subsequent calls to ml_get() flush the firstline data - take a
1657 * copy of the required string.
1658 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001659 firstline = ml_get(oap->start.lnum);
1660 len = STRLEN(firstline);
1661 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001663 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001664 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001665 // account for pressing cursor in insert mode when '$' was used
1666 if (bd.is_MAX
1667 && (start_insert.lnum == Insstart.lnum
1668 && start_insert.col > Insstart.col))
1669 {
1670 offset = (start_insert.col - Insstart.col);
1671 add -= offset;
1672 if (oap->end_vcol > offset)
1673 oap->end_vcol -= (offset + 1);
1674 else
1675 // moved outside of the visual block, what to do?
1676 return;
1677 }
1678 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001679 if ((size_t)add > len)
1680 firstline += len; // short line, point to the NUL
1681 else
1682 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001683 if (pre_textlen >= 0 && (ins_len =
1684 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001686 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 if (ins_text != NULL)
1688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001689 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (u_save(oap->start.lnum,
1691 (linenr_T)(oap->end.lnum + 1)) == OK)
1692 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1693 &bd);
1694
1695 curwin->w_cursor.col = oap->start.col;
1696 check_cursor();
1697 vim_free(ins_text);
1698 }
1699 }
1700 }
1701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703/*
1704 * op_change - handle a change operation
1705 *
1706 * return TRUE if edit() returns because of a CTRL-O command
1707 */
1708 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001709op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 colnr_T l;
1712 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 long offset;
1714 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001715 long ins_len;
1716 long pre_textlen = 0;
1717 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 char_u *firstline;
1719 char_u *ins_text, *newp, *oldp;
1720 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 l = oap->start.col;
1723 if (oap->motion_type == MLINE)
1724 {
1725 l = 0;
1726#ifdef FEAT_SMARTINDENT
1727 if (!p_paste && curbuf->b_p_si
1728# ifdef FEAT_CINDENT
1729 && !curbuf->b_p_cin
1730# endif
1731 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001732 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#endif
1734 }
1735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001736 // First delete the text in the region. In an empty buffer only need to
1737 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1739 {
1740 if (u_save_cursor() == FAIL)
1741 return FALSE;
1742 }
1743 else if (op_delete(oap) == FAIL)
1744 return FALSE;
1745
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001746 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 && !virtual_op)
1748 inc_cursor();
1749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001750 // check for still on same line (<CR> in inserted text meaningless)
1751 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (oap->block_mode)
1753 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001754 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (virtual_op && (curwin->w_cursor.coladd > 0
1756 || gchar_cursor() == NUL))
1757 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001758 firstline = ml_get(oap->start.lnum);
1759 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001760 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 bd.textcol = curwin->w_cursor.col;
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1765 if (oap->motion_type == MLINE)
1766 fix_indent();
1767#endif
1768
1769 retval = edit(NUL, FALSE, (linenr_T)1);
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001772 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001774 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001776 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001778 // Auto-indenting may have changed the indent. If the cursor was past
1779 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001781 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001783 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001784
1785 pre_textlen += new_indent - pre_indent;
1786 bd.textcol += new_indent - pre_indent;
1787 }
1788
1789 ins_len = (long)STRLEN(firstline) - pre_textlen;
1790 if (ins_len > 0)
1791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001792 // Subsequent calls to ml_get() flush the firstline data - take a
1793 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001794 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001796 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1798 linenr++)
1799 {
1800 block_prep(oap, &bd, linenr, TRUE);
1801 if (!bd.is_short || virtual_op)
1802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 pos_T vpos;
1804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001805 // If the block starts in virtual space, count the
1806 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (bd.is_short)
1808 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001809 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 else
1813 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001815 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (newp == NULL)
1817 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001818 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 mch_memmove(newp, oldp, (size_t)bd.textcol);
1820 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001821 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1824 offset += ins_len;
1825 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001826 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 ml_replace(linenr, newp, FALSE);
1828 }
1829 }
1830 check_cursor();
1831
1832 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1833 }
1834 vim_free(ins_text);
1835 }
1836 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001837 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841
1842/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001843 * When the cursor is on the NUL past the end of the line and it should not be
1844 * there move it left.
1845 */
1846 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001847adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001848{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001849 unsigned int cur_ve_flags = get_ve_flags();
1850
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001851 if (curwin->w_cursor.col > 0
1852 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001853 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 && !(restart_edit || (State & INSERT)))
1855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001856 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001857 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001858
Gary Johnson53ba05b2021-07-26 22:19:10 +02001859 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001860 {
1861 colnr_T scol, ecol;
1862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001863 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001864 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1865 curwin->w_cursor.coladd = ecol - scol + 1;
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868}
1869
Bram Moolenaar81340392012-06-06 16:12:59 +02001870/*
1871 * If "process" is TRUE and the line begins with a comment leader (possibly
1872 * after some white space), return a pointer to the text after it. Put a boolean
1873 * value indicating whether the line ends with an unclosed comment in
1874 * "is_comment".
1875 * line - line to be processed,
1876 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001877 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001878 * include_space - whether to also skip space following the comment leader,
1879 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001880 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001881 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001882 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001883skip_comment(
1884 char_u *line,
1885 int process,
1886 int include_space,
1887 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001888{
1889 char_u *comment_flags = NULL;
1890 int lead_len;
1891 int leader_offset = get_last_leader_offset(line, &comment_flags);
1892
1893 *is_comment = FALSE;
1894 if (leader_offset != -1)
1895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001896 // Let's check whether the line ends with an unclosed comment.
1897 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001898 while (*comment_flags)
1899 {
1900 if (*comment_flags == COM_END
1901 || *comment_flags == ':')
1902 break;
1903 ++comment_flags;
1904 }
1905 if (*comment_flags != COM_END)
1906 *is_comment = TRUE;
1907 }
1908
1909 if (process == FALSE)
1910 return line;
1911
1912 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1913
1914 if (lead_len == 0)
1915 return line;
1916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001917 // Find:
1918 // - COM_END,
1919 // - colon,
1920 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001921 while (*comment_flags)
1922 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001923 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001924 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001926 ++comment_flags;
1927 }
1928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001929 // If we found a colon, it means that we are not processing a line
1930 // starting with a closing part of a three-part comment. That's good,
1931 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001932 if (*comment_flags == ':' || *comment_flags == NUL)
1933 line += lead_len;
1934
1935 return line;
1936}
Bram Moolenaar81340392012-06-06 16:12:59 +02001937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001939 * Join 'count' lines (minimal 2) at cursor position.
1940 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001941 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1942 * leaders should not be removed.
1943 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1944 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001946 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 */
1948 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001949do_join(
1950 long count,
1951 int insert_space,
1952 int save_undo,
1953 int use_formatoptions UNUSED,
1954 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001956 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001957 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001958 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001960 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001962 int endcurr1 = NUL;
1963 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 int currsize = 0; // size of the current line
1965 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001967 colnr_T col = 0;
1968 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001969 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001970 int remove_comments = (use_formatoptions == TRUE)
1971 && has_format_option(FO_REMOVE_COMS);
1972 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001973#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001974 int propcount = 0; // number of props over all joined lines
1975 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001978 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1979 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1980 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001982 // Allocate an array to store the number of spaces inserted before each
1983 // line. We will use it to pre-compute the length of the new line and the
1984 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001985 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001986 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001988 if (remove_comments)
1989 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001990 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001991 if (comments == NULL)
1992 {
1993 vim_free(spaces);
1994 return FAIL;
1995 }
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001999 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002000 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002001 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002003 for (t = 0; t < count; ++t)
2004 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002005 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002006#ifdef FEAT_PROP_POPUP
2007 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
2008#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002009 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002012 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2013 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2014 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002015 if (remove_comments)
2016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002017 // We don't want to remove the comment leader if the
2018 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002019 if (t > 0 && prev_was_comment)
2020 {
2021
2022 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2023 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002024 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002025 curr = new_curr;
2026 }
2027 else
2028 curr = skip_comment(curr, FALSE, insert_space,
2029 &prev_was_comment);
2030 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002031
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002032 if (insert_space && t > 0)
2033 {
2034 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002035 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002036 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002037 && (!has_format_option(FO_MBYTE_JOIN)
2038 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2039 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002040 || (mb_ptr2char(curr) < 0x100
2041 && !(enc_utf8 && utf_eat_space(endcurr1)))
2042 || (endcurr1 < 0x100
2043 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 )
2045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002046 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002047 if (endcurr1 == ' ')
2048 endcurr1 = endcurr2;
2049 else
2050 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002051 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002052 if ( p_js
2053 && (endcurr1 == '.'
2054 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2055 && (endcurr1 == '?' || endcurr1 == '!'))))
2056 ++spaces[t];
2057 }
2058 }
2059 currsize = (int)STRLEN(curr);
2060 sumsize += currsize + spaces[t];
2061 endcurr1 = endcurr2 = NUL;
2062 if (insert_space && currsize > 0)
2063 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002064 if (has_mbyte)
2065 {
2066 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002067 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002068 endcurr1 = (*mb_ptr2char)(cend);
2069 if (cend > curr)
2070 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002071 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002072 endcurr2 = (*mb_ptr2char)(cend);
2073 }
2074 }
2075 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002076 {
2077 endcurr1 = *(curr + currsize - 1);
2078 if (currsize > 1)
2079 endcurr2 = *(curr + currsize - 2);
2080 }
2081 }
2082 line_breakcheck();
2083 if (got_int)
2084 {
2085 ret = FAIL;
2086 goto theend;
2087 }
2088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002090 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002091 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002093 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002094 newp_len = sumsize + 1;
2095#ifdef FEAT_PROP_POPUP
2096 newp_len += propcount * sizeof(textprop_T);
2097#endif
2098 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002099 if (newp == NULL)
2100 {
2101 ret = FAIL;
2102 goto theend;
2103 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 cend = newp + sumsize;
2105 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002107 /*
2108 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002109 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002110 *
2111 * Move marks from each deleted line to the joined line, adjusting the
2112 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2113 * should not really be a problem.
2114 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002115#ifdef FEAT_PROP_POPUP
2116 props_remaining = propcount;
2117#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002118 for (t = count - 1; ; --t)
2119 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002120 int spaces_removed;
2121
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002122 cend -= currsize;
2123 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002124
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002125 if (spaces[t] > 0)
2126 {
2127 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002128 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002129 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002130
2131 // If deleting more spaces than adding, the cursor moves no more than
2132 // what is added if it is inside these spaces.
2133 spaces_removed = (curr - curr_start) - spaces[t];
2134
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002135 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002136 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002137#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002138 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2139 curwin->w_cursor.lnum + t, t == count - 1,
2140 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002141#endif
2142
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002143 if (t == 0)
2144 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002145 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002146 if (remove_comments)
2147 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002148 if (insert_space && t > 1)
2149 curr = skipwhite(curr);
2150 currsize = (int)STRLEN(curr);
2151 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002152
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002153 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaare1004402020-10-24 20:49:43 +02002155 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002156 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002157 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002158 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002159 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002160 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002161
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002162 // Only report the change in the first line here, del_lines() will report
2163 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 changed_lines(curwin->w_cursor.lnum, currsize,
2165 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002167 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 * briefly, and then move it back. After del_lines() the cursor may
2169 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 */
2171 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002173 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 curwin->w_cursor.lnum = t;
2175
2176 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002177 * Set the cursor column:
2178 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002179 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002181 curwin->w_cursor.col =
2182 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 curwin->w_set_curswant = TRUE;
2187
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002188theend:
2189 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002190 if (remove_comments)
2191 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002192 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193}
2194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * prepare a few things for block mode yank/delete/tilde
2197 *
2198 * for delete:
2199 * - textlen includes the first/last char to be (partly) deleted
2200 * - start/endspaces is the number of columns that are taken by the
2201 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002202 * deleted.
2203 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 * - textlen includes the first/last char to be wholly yanked
2205 * - start/endspaces is the number of columns of the first/last yanked char
2206 * that are to be yanked.
2207 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002208 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002209block_prep(
2210 oparg_T *oap,
2211 struct block_def *bdp,
2212 linenr_T lnum,
2213 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 int incr = 0;
2216 char_u *pend;
2217 char_u *pstart;
2218 char_u *line;
2219 char_u *prev_pstart;
2220 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002221#ifdef FEAT_LINEBREAK
2222 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002224 // Avoid a problem with unwanted linebreaks in block mode.
2225 curwin->w_p_lbr = FALSE;
2226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 bdp->startspaces = 0;
2228 bdp->endspaces = 0;
2229 bdp->textlen = 0;
2230 bdp->start_vcol = 0;
2231 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 bdp->is_short = FALSE;
2233 bdp->is_oneChar = FALSE;
2234 bdp->pre_whitesp = 0;
2235 bdp->pre_whitesp_c = 0;
2236 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 bdp->start_char_vcols = 0;
2238
2239 line = ml_get(lnum);
2240 pstart = line;
2241 prev_pstart = line;
2242 while (bdp->start_vcol < oap->start_vcol && *pstart)
2243 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002245 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002247 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 bdp->pre_whitesp += incr;
2250 bdp->pre_whitesp_c++;
2251 }
2252 else
2253 {
2254 bdp->pre_whitesp = 0;
2255 bdp->pre_whitesp_c = 0;
2256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002258 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 }
2260 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002261 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
2263 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (!is_del || oap->op_type == OP_APPEND)
2266 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2267 }
2268 else
2269 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002270 // notice: this converts partly selected Multibyte characters to
2271 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2273 if (is_del && bdp->startspaces)
2274 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2275 pend = pstart;
2276 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002277 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (oap->op_type == OP_INSERT)
2281 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2282 else if (oap->op_type == OP_APPEND)
2283 {
2284 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2285 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2286 }
2287 else
2288 {
2289 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2290 if (is_del && oap->op_type != OP_LSHIFT)
2291 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002292 // just putting the sum of those two into
2293 // bdp->startspaces doesn't work for Visual replace,
2294 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 bdp->startspaces = bdp->start_char_vcols
2296 - (bdp->start_vcol - oap->start_vcol);
2297 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2298 }
2299 }
2300 }
2301 else
2302 {
2303 prev_pend = pend;
2304 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2305 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002308 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 bdp->end_vcol += incr;
2310 }
2311 if (bdp->end_vcol <= oap->end_vcol
2312 && (!is_del
2313 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002314 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002317 // Alternative: include spaces to fill up the block.
2318 // Disadvantage: can lead to trailing spaces when the line is
2319 // short where the text is put
2320 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (oap->op_type == OP_APPEND || virtual_op)
2322 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002323 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002325 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 else if (bdp->end_vcol > oap->end_vcol)
2328 {
2329 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2330 if (!is_del && bdp->endspaces)
2331 {
2332 bdp->endspaces = incr - bdp->endspaces;
2333 if (pend != pstart)
2334 pend = prev_pend;
2335 }
2336 }
2337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (is_del && bdp->startspaces)
2340 pstart = prev_pstart;
2341 bdp->textlen = (int)(pend - pstart);
2342 }
2343 bdp->textcol = (colnr_T) (pstart - line);
2344 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002345#ifdef FEAT_LINEBREAK
2346 curwin->w_p_lbr = lbr_saved;
2347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002351 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002354op_addsub(
2355 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002356 linenr_T Prenum1, // Amount of add/subtract
2357 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002359 pos_T pos;
2360 struct block_def bd;
2361 int change_cnt = 0;
2362 linenr_T amount = Prenum1;
2363
Bram Moolenaar6b731882018-11-16 20:54:47 +01002364 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002365 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002366 // the call to changed_lines().
2367#ifdef FEAT_FOLDING
2368 disable_fold_update++;
2369#endif
2370
Bram Moolenaard79e5502016-01-10 22:13:02 +01002371 if (!VIsual_active)
2372 {
2373 pos = curwin->w_cursor;
2374 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002375 {
2376#ifdef FEAT_FOLDING
2377 disable_fold_update--;
2378#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002379 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002380 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002381 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002382#ifdef FEAT_FOLDING
2383 disable_fold_update--;
2384#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002385 if (change_cnt)
2386 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2387 }
2388 else
2389 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002390 int one_change;
2391 int length;
2392 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002393
2394 if (u_save((linenr_T)(oap->start.lnum - 1),
2395 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002396 {
2397#ifdef FEAT_FOLDING
2398 disable_fold_update--;
2399#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002400 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002401 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002402
2403 pos = oap->start;
2404 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2405 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002406 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002407 {
2408 block_prep(oap, &bd, pos.lnum, FALSE);
2409 pos.col = bd.textcol;
2410 length = bd.textlen;
2411 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002412 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002413 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002414 curwin->w_cursor.col = 0;
2415 pos.col = 0;
2416 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2417 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002418 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002419 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002420 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002421 dec(&(oap->end));
2422 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2423 pos.col = 0;
2424 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002425 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002426 pos.col += oap->start.col;
2427 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002428 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002429 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002430 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002431 length = (int)STRLEN(ml_get(oap->end.lnum));
2432 if (oap->end.col >= length)
2433 oap->end.col = length - 1;
2434 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002435 }
2436 }
2437 one_change = do_addsub(oap->op_type, &pos, length, amount);
2438 if (one_change)
2439 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002440 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002441 if (change_cnt == 0)
2442 startpos = curbuf->b_op_start;
2443 ++change_cnt;
2444 }
2445
2446#ifdef FEAT_NETBEANS_INTG
2447 if (netbeans_active() && one_change)
2448 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002449 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002450
2451 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002452 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002453 netbeans_inserted(curbuf, pos.lnum, pos.col,
2454 &ptr[pos.col], length);
2455 }
2456#endif
2457 if (g_cmd && one_change)
2458 amount += Prenum1;
2459 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002460
2461#ifdef FEAT_FOLDING
2462 disable_fold_update--;
2463#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002464 if (change_cnt)
2465 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2466
2467 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002468 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002469 redraw_curbuf_later(INVERTED);
2470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002471 // Set '[ mark if something changed. Keep the last end
2472 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002473 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002474 curbuf->b_op_start = startpos;
2475
2476 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002477 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002478 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002479 }
2480}
2481
2482/*
2483 * Add or subtract 'Prenum1' from a number in a line
2484 * op_type is OP_NR_ADD or OP_NR_SUB
2485 *
2486 * Returns TRUE if some character was changed.
2487 */
2488 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002489do_addsub(
2490 int op_type,
2491 pos_T *pos,
2492 int length,
2493 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002494{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 int col;
2496 char_u *buf1;
2497 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002498 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2499 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002500 uvarnumber_T n;
2501 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 char_u *ptr;
2503 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002505 int do_hex;
2506 int do_oct;
2507 int do_bin;
2508 int do_alpha;
2509 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002512 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002513 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002514 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002515 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002516 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002517 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002518 pos_T startpos;
2519 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002520 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002522 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2523 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2524 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2525 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2526 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002528 if (virtual_active())
2529 {
2530 save_coladd = pos->coladd;
2531 pos->coladd = 0;
2532 }
2533
Bram Moolenaard79e5502016-01-10 22:13:02 +01002534 curwin->w_cursor = *pos;
2535 ptr = ml_get(pos->lnum);
2536 col = pos->col;
2537
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002538 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002539 goto theend;
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 /*
2542 * First check if we are on a hexadecimal number, after the "0x".
2543 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002544 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002546 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002547 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002548 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002549 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002550 if (has_mbyte)
2551 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002552 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002553
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002554 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002555 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002556 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002557 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002558 if (has_mbyte)
2559 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002560 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002561
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002562 if ( do_bin
2563 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002564 && ! ((col > 0
2565 && (ptr[col] == 'X'
2566 || ptr[col] == 'x')
2567 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002568 && (!has_mbyte ||
2569 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002570 && vim_isxdigit(ptr[col + 1]))))
2571 {
2572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002573 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002574
Bram Moolenaard79e5502016-01-10 22:13:02 +01002575 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002576
2577 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002578 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002579 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002580 if (has_mbyte)
2581 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002582 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002583 }
2584
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002585 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002586 && col > 0
2587 && (ptr[col] == 'X'
2588 || ptr[col] == 'x')
2589 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002590 && (!has_mbyte ||
2591 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002592 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002593 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002594 && col > 0
2595 && (ptr[col] == 'B'
2596 || ptr[col] == 'b')
2597 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002598 && (!has_mbyte ||
2599 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002600 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002602 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002603 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002604 if (has_mbyte)
2605 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002606 }
2607 else
2608 {
2609 /*
2610 * Search forward and then backward to find the start of number.
2611 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002612 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002613
2614 while (ptr[col] != NUL
2615 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002616 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002617 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002618
2619 while (col > 0
2620 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002621 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002622 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002623 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002624 if (has_mbyte)
2625 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002626 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002627 }
2628 }
2629
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002630 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002631 {
2632 while (ptr[col] != NUL && length > 0
2633 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002634 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002635 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002636 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002637
2638 col += mb_len;
2639 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002640 }
2641
2642 if (length == 0)
2643 goto theend;
2644
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002645 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002646 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2647 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002648 {
2649 negative = TRUE;
2650 was_positive = FALSE;
2651 }
2652 }
2653
2654 /*
2655 * If a number was found, and saving for undo works, replace the number.
2656 */
2657 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002658 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002659 {
2660 beep_flush();
2661 goto theend;
2662 }
2663
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002664 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002665 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002667 if (op_type == OP_NR_SUB)
2668 {
2669 if (CharOrd(firstdigit) < Prenum1)
2670 {
2671 if (isupper(firstdigit))
2672 firstdigit = 'A';
2673 else
2674 firstdigit = 'a';
2675 }
2676 else
2677#ifdef EBCDIC
2678 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2679#else
2680 firstdigit -= Prenum1;
2681#endif
2682 }
2683 else
2684 {
2685 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2686 {
2687 if (isupper(firstdigit))
2688 firstdigit = 'Z';
2689 else
2690 firstdigit = 'z';
2691 }
2692 else
2693#ifdef EBCDIC
2694 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2695#else
2696 firstdigit += Prenum1;
2697#endif
2698 }
2699 curwin->w_cursor.col = col;
2700 if (!did_change)
2701 startpos = curwin->w_cursor;
2702 did_change = TRUE;
2703 (void)del_char(FALSE);
2704 ins_char(firstdigit);
2705 endpos = curwin->w_cursor;
2706 curwin->w_cursor.col = col;
2707 }
2708 else
2709 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002710 pos_T save_pos;
2711 int i;
2712
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002713 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002714 && (!has_mbyte ||
2715 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002716 && !visual
2717 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002718 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002720 --col;
2721 negative = TRUE;
2722 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002724 if (visual && VIsual_mode != 'V')
2725 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2726 ? (int)STRLEN(ptr) - col
2727 : length);
2728
2729 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002730 0 + (do_bin ? STR2NR_BIN : 0)
2731 + (do_oct ? STR2NR_OCT : 0)
2732 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002733 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002736 if (pre && negative)
2737 {
2738 ++col;
2739 --length;
2740 negative = FALSE;
2741 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002742 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002743 subtract = FALSE;
2744 if (op_type == OP_NR_SUB)
2745 subtract ^= TRUE;
2746 if (negative)
2747 subtract ^= TRUE;
2748
2749 oldn = n;
2750 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002751 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002752 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002753 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002755 if (!pre)
2756 {
2757 if (subtract)
2758 {
2759 if (n > oldn)
2760 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002761 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002762 negative ^= TRUE;
2763 }
2764 }
2765 else
2766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002768 if (n < oldn)
2769 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002770 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002771 negative ^= TRUE;
2772 }
2773 }
2774 if (n == 0)
2775 negative = FALSE;
2776 }
2777
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002778 if (do_unsigned && negative)
2779 {
2780 if (subtract)
2781 // sticking at zero.
2782 n = (uvarnumber_T)0;
2783 else
2784 // sticking at 2^64 - 1.
2785 n = (uvarnumber_T)(-1);
2786 negative = FALSE;
2787 }
2788
Bram Moolenaard79e5502016-01-10 22:13:02 +01002789 if (visual && !was_positive && !negative && col > 0)
2790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002792 col--;
2793 length++;
2794 }
2795
2796 /*
2797 * Delete the old number.
2798 */
2799 curwin->w_cursor.col = col;
2800 if (!did_change)
2801 startpos = curwin->w_cursor;
2802 did_change = TRUE;
2803 todel = length;
2804 c = gchar_cursor();
2805 /*
2806 * Don't include the '-' in the length, only the length of the
2807 * part after it is kept the same.
2808 */
2809 if (c == '-')
2810 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002811
2812 save_pos = curwin->w_cursor;
2813 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002814 {
2815 if (c < 0x100 && isalpha(c))
2816 {
2817 if (isupper(c))
2818 hexupper = TRUE;
2819 else
2820 hexupper = FALSE;
2821 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002822 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002823 c = gchar_cursor();
2824 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002825 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002826
2827 /*
2828 * Prepare the leading characters in buf1[].
2829 * When there are many leading zeros it could be very long.
2830 * Allocate a bit too much.
2831 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002832 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002833 if (buf1 == NULL)
2834 goto theend;
2835 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002836 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002837 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002838 if (pre)
2839 {
2840 *ptr++ = '0';
2841 --length;
2842 }
2843 if (pre == 'b' || pre == 'B' ||
2844 pre == 'x' || pre == 'X')
2845 {
2846 *ptr++ = pre;
2847 --length;
2848 }
2849
2850 /*
2851 * Put the number characters in buf2[].
2852 */
2853 if (pre == 'b' || pre == 'B')
2854 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002855 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002856 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002859 for (bit = bits; bit > 0; bit--)
2860 if ((n >> (bit - 1)) & 0x1) break;
2861
2862 for (i = 0; bit > 0; bit--)
2863 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2864
2865 buf2[i] = '\0';
2866 }
2867 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002868 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002869 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002870 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002871 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002872 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002873 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002874 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002875 length -= (int)STRLEN(buf2);
2876
2877 /*
2878 * Adjust number of zeros to the new number of digits, so the
2879 * total length of the number remains the same.
2880 * Don't do this when
2881 * the result may look like an octal number.
2882 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002883 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002884 while (length-- > 0)
2885 *ptr++ = '0';
2886 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002887
Bram Moolenaard79e5502016-01-10 22:13:02 +01002888 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002889
2890 // Insert just after the first character to be removed, so that any
2891 // text properties will be adjusted. Then delete the old number
2892 // afterwards.
2893 save_pos = curwin->w_cursor;
2894 if (todel > 0)
2895 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002897 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002898
2899 // del_char() will also mark line needing displaying
2900 if (todel > 0)
2901 {
2902 int bytes_after = (int)STRLEN(ml_get_curline())
2903 - curwin->w_cursor.col;
2904
2905 // Delete the one character before the insert.
2906 curwin->w_cursor = save_pos;
2907 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002908 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2909 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002910 --todel;
2911 }
2912 while (todel-- > 0)
2913 (void)del_char(FALSE);
2914
Bram Moolenaard79e5502016-01-10 22:13:02 +01002915 endpos = curwin->w_cursor;
2916 if (did_change && curwin->w_cursor.col)
2917 --curwin->w_cursor.col;
2918 }
2919
Bram Moolenaare1004402020-10-24 20:49:43 +02002920 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002923 curbuf->b_op_start = startpos;
2924 curbuf->b_op_end = endpos;
2925 if (curbuf->b_op_end.col > 0)
2926 --curbuf->b_op_end.col;
2927 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002928
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002929theend:
2930 if (visual)
2931 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002932 else if (did_change)
2933 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002934 else if (virtual_active())
2935 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002936
Bram Moolenaard79e5502016-01-10 22:13:02 +01002937 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938}
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002941clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002943 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002947 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 *
2949 * "Words" are counted by looking for boundaries between non-space and
2950 * space characters. (it seems to produce results that match 'wc'.)
2951 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002952 * Return value is byte count; word count for the line is added to "*wc".
2953 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 *
2955 * The function will only examine the first "limit" characters in the
2956 * line, stopping if it encounters an end-of-line (NUL byte). In that
2957 * case, eol_size will be added to the character count to account for
2958 * the size of the EOL character.
2959 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002960 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002961line_count_info(
2962 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002963 varnumber_T *wc,
2964 varnumber_T *cc,
2965 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002966 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002968 varnumber_T i;
2969 varnumber_T words = 0;
2970 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 int is_word = 0;
2972
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002973 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
2975 if (is_word)
2976 {
2977 if (vim_isspace(line[i]))
2978 {
2979 words++;
2980 is_word = 0;
2981 }
2982 }
2983 else if (!vim_isspace(line[i]))
2984 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002985 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002986 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
2988
2989 if (is_word)
2990 words++;
2991 *wc += words;
2992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002994 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002997 chars += eol_size;
2998 }
2999 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 return i;
3001}
3002
3003/*
3004 * Give some info about the position of the cursor (for "g CTRL-G").
3005 * In Visual mode, give some info about the selected region. (In this case,
3006 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003007 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003010cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003013 char_u buf1[50];
3014 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003016 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003017 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003018 varnumber_T byte_count_cursor = 0;
3019 varnumber_T char_count = 0;
3020 varnumber_T char_count_cursor = 0;
3021 varnumber_T word_count = 0;
3022 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003023 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003024 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 long line_count_selected = 0;
3026 pos_T min_pos, max_pos;
3027 oparg_T oparg;
3028 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 /*
3031 * Compute the length of the file in characters.
3032 */
3033 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3034 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003035 if (dict == NULL)
3036 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003037 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003038 return;
3039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
3041 else
3042 {
3043 if (get_fileformat(curbuf) == EOL_DOS)
3044 eol_size = 2;
3045 else
3046 eol_size = 1;
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (VIsual_active)
3049 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003050 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 {
3052 min_pos = VIsual;
3053 max_pos = curwin->w_cursor;
3054 }
3055 else
3056 {
3057 min_pos = curwin->w_cursor;
3058 max_pos = VIsual;
3059 }
3060 if (*p_sel == 'e' && max_pos.col > 0)
3061 --max_pos.col;
3062
3063 if (VIsual_mode == Ctrl_V)
3064 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003065#ifdef FEAT_LINEBREAK
3066 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003067 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003070 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003071 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 oparg.is_VIsual = 1;
3074 oparg.block_mode = TRUE;
3075 oparg.op_type = OP_NOP;
3076 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003077 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003078#ifdef FEAT_LINEBREAK
3079 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003080 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003081#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003082 if (curwin->w_curswant == MAXCOL)
3083 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (oparg.end_vcol < oparg.start_vcol)
3086 {
3087 oparg.end_vcol += oparg.start_vcol;
3088 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3089 oparg.end_vcol -= oparg.start_vcol;
3090 }
3091 }
3092 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3096 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003098 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 {
3100 ui_breakcheck();
3101 if (got_int)
3102 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003103 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003106 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 if (VIsual_active
3108 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3109 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003110 char_u *s = NULL;
3111 long len = 0L;
3112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 switch (VIsual_mode)
3114 {
3115 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003119 s = bd.textstart;
3120 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 break;
3122 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003123 s = ml_get(lnum);
3124 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 break;
3126 case 'v':
3127 {
3128 colnr_T start_col = (lnum == min_pos.lnum)
3129 ? min_pos.col : 0;
3130 colnr_T end_col = (lnum == max_pos.lnum)
3131 ? max_pos.col - start_col + 1 : MAXCOL;
3132
Bram Moolenaardef9e822004-12-31 20:58:58 +00003133 s = ml_get(lnum) + start_col;
3134 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136 break;
3137 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003138 if (s != NULL)
3139 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003140 byte_count_cursor += line_count_info(s, &word_count_cursor,
3141 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003142 if (lnum == curbuf->b_ml.ml_line_count
3143 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003144 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003145 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003146 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003151 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (lnum == curwin->w_cursor.lnum)
3153 {
3154 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003155 char_count_cursor += char_count;
3156 byte_count_cursor = byte_count +
3157 line_count_info(ml_get(lnum),
3158 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003159 (varnumber_T)(curwin->w_cursor.col + 1),
3160 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
3162 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003163 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003164 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003165 &char_count, (varnumber_T)MAXCOL,
3166 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003169 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003170 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003171 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaared767a22016-01-03 22:49:16 +01003173 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003175 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003177 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3178 {
3179 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3180 &max_pos.col);
3181 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3182 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3183 }
3184 else
3185 buf1[0] = NUL;
3186
3187 if (char_count_cursor == byte_count_cursor
3188 && char_count == byte_count)
3189 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003190 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003191 buf1, line_count_selected,
3192 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003193 (varnumber_T)word_count_cursor,
3194 (varnumber_T)word_count,
3195 (varnumber_T)byte_count_cursor,
3196 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003197 else
3198 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003199 _("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 +01003200 buf1, line_count_selected,
3201 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003202 (varnumber_T)word_count_cursor,
3203 (varnumber_T)word_count,
3204 (varnumber_T)char_count_cursor,
3205 (varnumber_T)char_count,
3206 (varnumber_T)byte_count_cursor,
3207 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
3209 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003210 {
3211 p = ml_get_curline();
3212 validate_virtcol();
3213 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3214 (int)curwin->w_virtcol + 1);
3215 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3216 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaared767a22016-01-03 22:49:16 +01003218 if (char_count_cursor == byte_count_cursor
3219 && char_count == byte_count)
3220 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003221 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003222 (char *)buf1, (char *)buf2,
3223 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003225 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3226 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003227 else
3228 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003229 _("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 +01003230 (char *)buf1, (char *)buf2,
3231 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003232 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003233 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3234 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3235 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003236 }
3237 }
3238
Bram Moolenaared767a22016-01-03 22:49:16 +01003239 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003240 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003241 {
3242 size_t len = STRLEN(IObuff);
3243
3244 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003245 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003246 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003247 if (dict == NULL)
3248 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003249 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003250 p = p_shm;
3251 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003252 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003253 p_shm = p;
3254 }
3255 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003256#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003257 if (dict != NULL)
3258 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003259 dict_add_number(dict, "words", word_count);
3260 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003261 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003262 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3263 byte_count_cursor);
3264 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3265 char_count_cursor);
3266 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3267 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003271
3272/*
3273 * Handle indent and format operators and visual mode ":".
3274 */
3275 static void
3276op_colon(oparg_T *oap)
3277{
3278 stuffcharReadbuff(':');
3279 if (oap->is_VIsual)
3280 stuffReadbuff((char_u *)"'<,'>");
3281 else
3282 {
3283 // Make the range look nice, so it can be repeated.
3284 if (oap->start.lnum == curwin->w_cursor.lnum)
3285 stuffcharReadbuff('.');
3286 else
3287 stuffnumReadbuff((long)oap->start.lnum);
3288 if (oap->end.lnum != oap->start.lnum)
3289 {
3290 stuffcharReadbuff(',');
3291 if (oap->end.lnum == curwin->w_cursor.lnum)
3292 stuffcharReadbuff('.');
3293 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3294 stuffcharReadbuff('$');
3295 else if (oap->start.lnum == curwin->w_cursor.lnum)
3296 {
3297 stuffReadbuff((char_u *)".+");
3298 stuffnumReadbuff((long)oap->line_count - 1);
3299 }
3300 else
3301 stuffnumReadbuff((long)oap->end.lnum);
3302 }
3303 }
3304 if (oap->op_type != OP_COLON)
3305 stuffReadbuff((char_u *)"!");
3306 if (oap->op_type == OP_INDENT)
3307 {
3308#ifndef FEAT_CINDENT
3309 if (*get_equalprg() == NUL)
3310 stuffReadbuff((char_u *)"indent");
3311 else
3312#endif
3313 stuffReadbuff(get_equalprg());
3314 stuffReadbuff((char_u *)"\n");
3315 }
3316 else if (oap->op_type == OP_FORMAT)
3317 {
3318 if (*curbuf->b_p_fp != NUL)
3319 stuffReadbuff(curbuf->b_p_fp);
3320 else if (*p_fp != NUL)
3321 stuffReadbuff(p_fp);
3322 else
3323 stuffReadbuff((char_u *)"fmt");
3324 stuffReadbuff((char_u *)"\n']");
3325 }
3326
3327 // do_cmdline() does the rest
3328}
3329
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003330// callback function for 'operatorfunc'
3331static callback_T opfunc_cb;
3332
3333/*
3334 * Process the 'operatorfunc' option value.
3335 * Returns OK or FAIL.
3336 */
3337 int
3338set_operatorfunc_option(void)
3339{
3340 return option_set_callback_func(p_opfunc, &opfunc_cb);
3341}
3342
3343#if defined(EXITFREE) || defined(PROTO)
3344 void
3345free_operatorfunc_option(void)
3346{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003347# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003348 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003349# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003350}
3351#endif
3352
Dominique Pelle748b3082022-01-08 12:41:16 +00003353#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003354/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003355 * Mark the global 'operatorfunc' callback with 'copyID' so that it is not
3356 * garbage collected.
3357 */
3358 int
3359set_ref_in_opfunc(int copyID UNUSED)
3360{
3361 int abort = FALSE;
3362
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003363 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003364
3365 return abort;
3366}
Dominique Pelle748b3082022-01-08 12:41:16 +00003367#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003368
3369/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003370 * Handle the "g@" operator: call 'operatorfunc'.
3371 */
3372 static void
3373op_function(oparg_T *oap UNUSED)
3374{
3375#ifdef FEAT_EVAL
3376 typval_T argv[2];
3377 int save_virtual_op = virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003378 int save_finish_op = finish_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003379 pos_T orig_start = curbuf->b_op_start;
3380 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003381 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003382
3383 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003384 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003385 else
3386 {
3387 // Set '[ and '] marks to text to be operated on.
3388 curbuf->b_op_start = oap->start;
3389 curbuf->b_op_end = oap->end;
3390 if (oap->motion_type != MLINE && !oap->inclusive)
3391 // Exclude the end position.
3392 decl(&curbuf->b_op_end);
3393
3394 argv[0].v_type = VAR_STRING;
3395 if (oap->block_mode)
3396 argv[0].vval.v_string = (char_u *)"block";
3397 else if (oap->motion_type == MLINE)
3398 argv[0].vval.v_string = (char_u *)"line";
3399 else
3400 argv[0].vval.v_string = (char_u *)"char";
3401 argv[1].v_type = VAR_UNKNOWN;
3402
3403 // Reset virtual_op so that 'virtualedit' can be changed in the
3404 // function.
3405 virtual_op = MAYBE;
3406
naohiro ono75c30e92021-10-19 11:15:41 +01003407 // Reset finish_op so that mode() returns the right value.
3408 finish_op = FALSE;
3409
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003410 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3411 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003412
3413 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003414 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003415 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003416 {
3417 curbuf->b_op_start = orig_start;
3418 curbuf->b_op_end = orig_end;
3419 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003420 }
3421#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003422 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003423#endif
3424}
3425
3426/*
3427 * Calculate start/end virtual columns for operating in block mode.
3428 */
3429 static void
3430get_op_vcol(
3431 oparg_T *oap,
3432 colnr_T redo_VIsual_vcol,
3433 int initial) // when TRUE adjust position for 'selectmode'
3434{
3435 colnr_T start, end;
3436
3437 if (VIsual_mode != Ctrl_V
3438 || (!initial && oap->end.col < curwin->w_width))
3439 return;
3440
3441 oap->block_mode = TRUE;
3442
3443 // prevent from moving onto a trail byte
3444 if (has_mbyte)
3445 mb_adjustpos(curwin->w_buffer, &oap->end);
3446
3447 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3448
3449 if (!redo_VIsual_busy)
3450 {
3451 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3452
3453 if (start < oap->start_vcol)
3454 oap->start_vcol = start;
3455 if (end > oap->end_vcol)
3456 {
3457 if (initial && *p_sel == 'e' && start >= 1
3458 && start - 1 >= oap->end_vcol)
3459 oap->end_vcol = start - 1;
3460 else
3461 oap->end_vcol = end;
3462 }
3463 }
3464
3465 // if '$' was used, get oap->end_vcol from longest line
3466 if (curwin->w_curswant == MAXCOL)
3467 {
3468 curwin->w_cursor.col = MAXCOL;
3469 oap->end_vcol = 0;
3470 for (curwin->w_cursor.lnum = oap->start.lnum;
3471 curwin->w_cursor.lnum <= oap->end.lnum;
3472 ++curwin->w_cursor.lnum)
3473 {
3474 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3475 if (end > oap->end_vcol)
3476 oap->end_vcol = end;
3477 }
3478 }
3479 else if (redo_VIsual_busy)
3480 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3481 // Correct oap->end.col and oap->start.col to be the
3482 // upper-left and lower-right corner of the block area.
3483 //
3484 // (Actually, this does convert column positions into character
3485 // positions)
3486 curwin->w_cursor.lnum = oap->end.lnum;
3487 coladvance(oap->end_vcol);
3488 oap->end = curwin->w_cursor;
3489
3490 curwin->w_cursor = oap->start;
3491 coladvance(oap->start_vcol);
3492 oap->start = curwin->w_cursor;
3493}
3494
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003495// Information for redoing the previous Visual selection.
3496typedef struct {
3497 int rv_mode; // 'v', 'V', or Ctrl-V
3498 linenr_T rv_line_count; // number of lines
3499 colnr_T rv_vcol; // number of cols or end column
3500 long rv_count; // count for Visual operator
3501 int rv_arg; // extra argument
3502} redo_VIsual_T;
3503
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003504 static int
3505is_ex_cmdchar(cmdarg_T *cap)
3506{
3507 return cap->cmdchar == ':'
3508 || cap->cmdchar == K_COMMAND
3509 || cap->cmdchar == K_SCRIPT_COMMAND;
3510}
3511
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003512/*
3513 * Handle an operator after Visual mode or when the movement is finished.
3514 * "gui_yank" is true when yanking text for the clipboard.
3515 */
3516 void
3517do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3518{
3519 oparg_T *oap = cap->oap;
3520 pos_T old_cursor;
3521 int empty_region_error;
3522 int restart_edit_save;
3523#ifdef FEAT_LINEBREAK
3524 int lbr_saved = curwin->w_p_lbr;
3525#endif
3526
3527 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003528 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3529
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003530 int include_line_break = FALSE;
3531
3532#if defined(FEAT_CLIPBOARD)
3533 // Yank the visual area into the GUI selection register before we operate
3534 // on it and lose it forever.
3535 // Don't do it if a specific register was specified, so that ""x"*P works.
3536 // This could call do_pending_operator() recursively, but that's OK
3537 // because gui_yank will be TRUE for the nested call.
3538 if ((clip_star.available || clip_plus.available)
3539 && oap->op_type != OP_NOP
3540 && !gui_yank
3541 && VIsual_active
3542 && !redo_VIsual_busy
3543 && oap->regname == 0)
3544 clip_auto_select();
3545#endif
3546 old_cursor = curwin->w_cursor;
3547
3548 // If an operation is pending, handle it...
3549 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3550 {
3551 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3552 // for the clipboard.
3553 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3554
3555#ifdef FEAT_LINEBREAK
3556 // Avoid a problem with unwanted linebreaks in block mode.
3557 if (curwin->w_p_lbr)
3558 curwin->w_valid &= ~VALID_VIRTCOL;
3559 curwin->w_p_lbr = FALSE;
3560#endif
3561 oap->is_VIsual = VIsual_active;
3562 if (oap->motion_force == 'V')
3563 oap->motion_type = MLINE;
3564 else if (oap->motion_force == 'v')
3565 {
3566 // If the motion was linewise, "inclusive" will not have been set.
3567 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3568 if (oap->motion_type == MLINE)
3569 oap->inclusive = FALSE;
3570 // If the motion already was characterwise, toggle "inclusive"
3571 else if (oap->motion_type == MCHAR)
3572 oap->inclusive = !oap->inclusive;
3573 oap->motion_type = MCHAR;
3574 }
3575 else if (oap->motion_force == Ctrl_V)
3576 {
3577 // Change line- or characterwise motion into Visual block mode.
3578 if (!VIsual_active)
3579 {
3580 VIsual_active = TRUE;
3581 VIsual = oap->start;
3582 }
3583 VIsual_mode = Ctrl_V;
3584 VIsual_select = FALSE;
3585 VIsual_reselect = FALSE;
3586 }
3587
3588 // Only redo yank when 'y' flag is in 'cpoptions'.
3589 // Never redo "zf" (define fold).
3590 if ((redo_yank || oap->op_type != OP_YANK)
3591 && ((!VIsual_active || oap->motion_force)
3592 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003593 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003594 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003595 && cap->cmdchar != 'D'
3596#ifdef FEAT_FOLDING
3597 && oap->op_type != OP_FOLD
3598 && oap->op_type != OP_FOLDOPEN
3599 && oap->op_type != OP_FOLDOPENREC
3600 && oap->op_type != OP_FOLDCLOSE
3601 && oap->op_type != OP_FOLDCLOSEREC
3602 && oap->op_type != OP_FOLDDEL
3603 && oap->op_type != OP_FOLDDELREC
3604#endif
3605 )
3606 {
3607 prep_redo(oap->regname, cap->count0,
3608 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3609 oap->motion_force, cap->cmdchar, cap->nchar);
3610 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3611 {
3612 // If 'cpoptions' does not contain 'r', insert the search
3613 // pattern to really repeat the same command.
3614 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3615 AppendToRedobuffLit(cap->searchbuf, -1);
3616 AppendToRedobuff(NL_STR);
3617 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003618 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003619 {
3620 // do_cmdline() has stored the first typed line in
3621 // "repeat_cmdline". When several lines are typed repeating
3622 // won't be possible.
3623 if (repeat_cmdline == NULL)
3624 ResetRedobuff();
3625 else
3626 {
3627 AppendToRedobuffLit(repeat_cmdline, -1);
3628 AppendToRedobuff(NL_STR);
3629 VIM_CLEAR(repeat_cmdline);
3630 }
3631 }
3632 }
3633
3634 if (redo_VIsual_busy)
3635 {
3636 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003637 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003638 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003639 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003640 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3641 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003642 VIsual_mode = redo_VIsual.rv_mode;
3643 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003644 {
3645 if (VIsual_mode == 'v')
3646 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003647 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003648 {
3649 validate_virtcol();
3650 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003651 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003652 }
3653 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003654 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003655 }
3656 else
3657 {
3658 curwin->w_curswant = MAXCOL;
3659 }
3660 coladvance(curwin->w_curswant);
3661 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003662 cap->count0 = redo_VIsual.rv_count;
3663 if (redo_VIsual.rv_count != 0)
3664 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003665 else
3666 cap->count1 = 1;
3667 }
3668 else if (VIsual_active)
3669 {
3670 if (!gui_yank)
3671 {
3672 // Save the current VIsual area for '< and '> marks, and "gv"
3673 curbuf->b_visual.vi_start = VIsual;
3674 curbuf->b_visual.vi_end = curwin->w_cursor;
3675 curbuf->b_visual.vi_mode = VIsual_mode;
3676 restore_visual_mode();
3677 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3678# ifdef FEAT_EVAL
3679 curbuf->b_visual_mode_eval = VIsual_mode;
3680# endif
3681 }
3682
3683 // In Select mode, a linewise selection is operated upon like a
3684 // characterwise selection.
3685 // Special case: gH<Del> deletes the last line.
3686 if (VIsual_select && VIsual_mode == 'V'
3687 && cap->oap->op_type != OP_DELETE)
3688 {
3689 if (LT_POS(VIsual, curwin->w_cursor))
3690 {
3691 VIsual.col = 0;
3692 curwin->w_cursor.col =
3693 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3694 }
3695 else
3696 {
3697 curwin->w_cursor.col = 0;
3698 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3699 }
3700 VIsual_mode = 'v';
3701 }
3702 // If 'selection' is "exclusive", backup one character for
3703 // charwise selections.
3704 else if (VIsual_mode == 'v')
3705 include_line_break = unadjust_for_sel();
3706
3707 oap->start = VIsual;
3708 if (VIsual_mode == 'V')
3709 {
3710 oap->start.col = 0;
3711 oap->start.coladd = 0;
3712 }
3713 }
3714
3715 // Set oap->start to the first position of the operated text, oap->end
3716 // to the end of the operated text. w_cursor is equal to oap->start.
3717 if (LT_POS(oap->start, curwin->w_cursor))
3718 {
3719#ifdef FEAT_FOLDING
3720 // Include folded lines completely.
3721 if (!VIsual_active)
3722 {
3723 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3724 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003725 if ((curwin->w_cursor.col > 0 || oap->inclusive
3726 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003727 && hasFolding(curwin->w_cursor.lnum, NULL,
3728 &curwin->w_cursor.lnum))
3729 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3730 }
3731#endif
3732 oap->end = curwin->w_cursor;
3733 curwin->w_cursor = oap->start;
3734
3735 // w_virtcol may have been updated; if the cursor goes back to its
3736 // previous position w_virtcol becomes invalid and isn't updated
3737 // automatically.
3738 curwin->w_valid &= ~VALID_VIRTCOL;
3739 }
3740 else
3741 {
3742#ifdef FEAT_FOLDING
3743 // Include folded lines completely.
3744 if (!VIsual_active && oap->motion_type == MLINE)
3745 {
3746 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3747 NULL))
3748 curwin->w_cursor.col = 0;
3749 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3750 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3751 }
3752#endif
3753 oap->end = oap->start;
3754 oap->start = curwin->w_cursor;
3755 }
3756
3757 // Just in case lines were deleted that make the position invalid.
3758 check_pos(curwin->w_buffer, &oap->end);
3759 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3760
3761 // Set "virtual_op" before resetting VIsual_active.
3762 virtual_op = virtual_active();
3763
3764 if (VIsual_active || redo_VIsual_busy)
3765 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003766 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003767
3768 if (!redo_VIsual_busy && !gui_yank)
3769 {
3770 // Prepare to reselect and redo Visual: this is based on the
3771 // size of the Visual text
3772 resel_VIsual_mode = VIsual_mode;
3773 if (curwin->w_curswant == MAXCOL)
3774 resel_VIsual_vcol = MAXCOL;
3775 else
3776 {
3777 if (VIsual_mode != Ctrl_V)
3778 getvvcol(curwin, &(oap->end),
3779 NULL, NULL, &oap->end_vcol);
3780 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3781 {
3782 if (VIsual_mode != Ctrl_V)
3783 getvvcol(curwin, &(oap->start),
3784 &oap->start_vcol, NULL, NULL);
3785 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3786 }
3787 else
3788 resel_VIsual_vcol = oap->end_vcol;
3789 }
3790 resel_VIsual_line_count = oap->line_count;
3791 }
3792
3793 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3794 if ((redo_yank || oap->op_type != OP_YANK)
3795 && oap->op_type != OP_COLON
3796#ifdef FEAT_FOLDING
3797 && oap->op_type != OP_FOLD
3798 && oap->op_type != OP_FOLDOPEN
3799 && oap->op_type != OP_FOLDOPENREC
3800 && oap->op_type != OP_FOLDCLOSE
3801 && oap->op_type != OP_FOLDCLOSEREC
3802 && oap->op_type != OP_FOLDDEL
3803 && oap->op_type != OP_FOLDDELREC
3804#endif
3805 && oap->motion_force == NUL
3806 )
3807 {
3808 // Prepare for redoing. Only use the nchar field for "r",
3809 // otherwise it might be the second char of the operator.
3810 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3811 || cap->nchar == 'N'))
3812 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 get_op_char(oap->op_type),
3814 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003815 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003816 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003817 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003818 int opchar = get_op_char(oap->op_type);
3819 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003820 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3821
3822 // reverse what nv_replace() did
3823 if (nchar == REPLACE_CR_NCHAR)
3824 nchar = CAR;
3825 else if (nchar == REPLACE_NL_NCHAR)
3826 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003827
3828 if (opchar == 'g' && extra_opchar == '@')
3829 // also repeat the count for 'operatorfunc'
3830 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3831 cap->count0, opchar, extra_opchar, nchar);
3832 else
3833 prep_redo(oap->regname, 0L, NUL, 'v',
3834 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003835 }
3836 if (!redo_VIsual_busy)
3837 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003838 redo_VIsual.rv_mode = resel_VIsual_mode;
3839 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3840 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3841 redo_VIsual.rv_count = cap->count0;
3842 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003843 }
3844 }
3845
3846 // oap->inclusive defaults to TRUE.
3847 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3848 // FALSE. This makes "d}P" and "v}dP" work the same.
3849 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3850 oap->inclusive = TRUE;
3851 if (VIsual_mode == 'V')
3852 oap->motion_type = MLINE;
3853 else
3854 {
3855 oap->motion_type = MCHAR;
3856 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3857 && (include_line_break || !virtual_op))
3858 {
3859 oap->inclusive = FALSE;
3860 // Try to include the newline, unless it's an operator
3861 // that works on lines only.
3862 if (*p_sel != 'o'
3863 && !op_on_lines(oap->op_type)
3864 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3865 {
3866 ++oap->end.lnum;
3867 oap->end.col = 0;
3868 oap->end.coladd = 0;
3869 ++oap->line_count;
3870 }
3871 }
3872 }
3873
3874 redo_VIsual_busy = FALSE;
3875
3876 // Switch Visual off now, so screen updating does
3877 // not show inverted text when the screen is redrawn.
3878 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3879 // no screen redraw, so it is done here to remove the inverted
3880 // part.
3881 if (!gui_yank)
3882 {
3883 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003884 setmouse();
3885 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003886 may_clear_cmdline();
3887 if ((oap->op_type == OP_YANK
3888 || oap->op_type == OP_COLON
3889 || oap->op_type == OP_FUNCTION
3890 || oap->op_type == OP_FILTER)
3891 && oap->motion_force == NUL)
3892 {
3893#ifdef FEAT_LINEBREAK
3894 // make sure redrawing is correct
3895 curwin->w_p_lbr = lbr_saved;
3896#endif
3897 redraw_curbuf_later(INVERTED);
3898 }
3899 }
3900 }
3901
3902 // Include the trailing byte of a multi-byte char.
3903 if (has_mbyte && oap->inclusive)
3904 {
3905 int l;
3906
3907 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3908 if (l > 1)
3909 oap->end.col += l - 1;
3910 }
3911 curwin->w_set_curswant = TRUE;
3912
3913 // oap->empty is set when start and end are the same. The inclusive
3914 // flag affects this too, unless yanking and the end is on a NUL.
3915 oap->empty = (oap->motion_type == MCHAR
3916 && (!oap->inclusive
3917 || (oap->op_type == OP_YANK
3918 && gchar_pos(&oap->end) == NUL))
3919 && EQUAL_POS(oap->start, oap->end)
3920 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3921 // For delete, change and yank, it's an error to operate on an
3922 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3923 empty_region_error = (oap->empty
3924 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3925
3926 // Force a redraw when operating on an empty Visual region, when
3927 // 'modifiable is off or creating a fold.
3928 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3929#ifdef FEAT_FOLDING
3930 || oap->op_type == OP_FOLD
3931#endif
3932 ))
3933 {
3934#ifdef FEAT_LINEBREAK
3935 curwin->w_p_lbr = lbr_saved;
3936#endif
3937 redraw_curbuf_later(INVERTED);
3938 }
3939
3940 // If the end of an operator is in column one while oap->motion_type
3941 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3942 // character in the previous line. If op_start is on or before the
3943 // first non-blank in the line, the operator becomes linewise
3944 // (strange, but that's the way vi does it).
3945 if ( oap->motion_type == MCHAR
3946 && oap->inclusive == FALSE
3947 && !(cap->retval & CA_NO_ADJ_OP_END)
3948 && oap->end.col == 0
3949 && (!oap->is_VIsual || *p_sel == 'o')
3950 && !oap->block_mode
3951 && oap->line_count > 1)
3952 {
3953 oap->end_adjusted = TRUE; // remember that we did this
3954 --oap->line_count;
3955 --oap->end.lnum;
3956 if (inindent(0))
3957 oap->motion_type = MLINE;
3958 else
3959 {
3960 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3961 if (oap->end.col)
3962 {
3963 --oap->end.col;
3964 oap->inclusive = TRUE;
3965 }
3966 }
3967 }
3968 else
3969 oap->end_adjusted = FALSE;
3970
3971 switch (oap->op_type)
3972 {
3973 case OP_LSHIFT:
3974 case OP_RSHIFT:
3975 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3976 auto_format(FALSE, TRUE);
3977 break;
3978
3979 case OP_JOIN_NS:
3980 case OP_JOIN:
3981 if (oap->line_count < 2)
3982 oap->line_count = 2;
3983 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3984 curbuf->b_ml.ml_line_count)
3985 beep_flush();
3986 else
3987 {
3988 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3989 TRUE, TRUE, TRUE);
3990 auto_format(FALSE, TRUE);
3991 }
3992 break;
3993
3994 case OP_DELETE:
3995 VIsual_reselect = FALSE; // don't reselect now
3996 if (empty_region_error)
3997 {
3998 vim_beep(BO_OPER);
3999 CancelRedo();
4000 }
4001 else
4002 {
4003 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004004 // save cursor line for undo if it wasn't saved yet
4005 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4006 && u_save_cursor() == OK)
4007 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004008 }
4009 break;
4010
4011 case OP_YANK:
4012 if (empty_region_error)
4013 {
4014 if (!gui_yank)
4015 {
4016 vim_beep(BO_OPER);
4017 CancelRedo();
4018 }
4019 }
4020 else
4021 {
4022#ifdef FEAT_LINEBREAK
4023 curwin->w_p_lbr = lbr_saved;
4024#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004025 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004026 (void)op_yank(oap, FALSE, !gui_yank);
4027 }
4028 check_cursor_col();
4029 break;
4030
4031 case OP_CHANGE:
4032 VIsual_reselect = FALSE; // don't reselect now
4033 if (empty_region_error)
4034 {
4035 vim_beep(BO_OPER);
4036 CancelRedo();
4037 }
4038 else
4039 {
4040 // This is a new edit command, not a restart. Need to
4041 // remember it to make 'insertmode' work with mappings for
4042 // Visual mode. But do this only once and not when typed and
4043 // 'insertmode' isn't set.
4044 if (p_im || !KeyTyped)
4045 restart_edit_save = restart_edit;
4046 else
4047 restart_edit_save = 0;
4048 restart_edit = 0;
4049#ifdef FEAT_LINEBREAK
4050 // Restore linebreak, so that when the user edits it looks as
4051 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004052 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004053#endif
4054 // Reset finish_op now, don't want it set inside edit().
4055 finish_op = FALSE;
4056 if (op_change(oap)) // will call edit()
4057 cap->retval |= CA_COMMAND_BUSY;
4058 if (restart_edit == 0)
4059 restart_edit = restart_edit_save;
4060 }
4061 break;
4062
4063 case OP_FILTER:
4064 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4065 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4066 else
4067 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4068 // FALLTHROUGH
4069
4070 case OP_INDENT:
4071 case OP_COLON:
4072
4073#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4074 // If 'equalprg' is empty, do the indenting internally.
4075 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4076 {
4077# ifdef FEAT_LISP
4078 if (curbuf->b_p_lisp)
4079 {
4080 op_reindent(oap, get_lisp_indent);
4081 break;
4082 }
4083# endif
4084# ifdef FEAT_CINDENT
4085 op_reindent(oap,
4086# ifdef FEAT_EVAL
4087 *curbuf->b_p_inde != NUL ? get_expr_indent :
4088# endif
4089 get_c_indent);
4090 break;
4091# endif
4092 }
4093#endif
4094
4095 op_colon(oap);
4096 break;
4097
4098 case OP_TILDE:
4099 case OP_UPPER:
4100 case OP_LOWER:
4101 case OP_ROT13:
4102 if (empty_region_error)
4103 {
4104 vim_beep(BO_OPER);
4105 CancelRedo();
4106 }
4107 else
4108 op_tilde(oap);
4109 check_cursor_col();
4110 break;
4111
4112 case OP_FORMAT:
4113#if defined(FEAT_EVAL)
4114 if (*curbuf->b_p_fex != NUL)
4115 op_formatexpr(oap); // use expression
4116 else
4117#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004118 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004119 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004120 op_colon(oap); // use external command
4121 else
4122 op_format(oap, FALSE); // use internal function
4123 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004124 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004125 case OP_FORMAT2:
4126 op_format(oap, TRUE); // use internal function
4127 break;
4128
4129 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004130 {
4131 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4132
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004133#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004134 // Restore linebreak, so that when the user edits it looks as
4135 // before.
4136 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004137#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004138 // call 'operatorfunc'
4139 op_function(oap);
4140
4141 // Restore the info for redoing Visual mode, the function may
4142 // invoke another operator and unintentionally change it.
4143 redo_VIsual = save_redo_VIsual;
4144 break;
4145 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004146
4147 case OP_INSERT:
4148 case OP_APPEND:
4149 VIsual_reselect = FALSE; // don't reselect now
4150 if (empty_region_error)
4151 {
4152 vim_beep(BO_OPER);
4153 CancelRedo();
4154 }
4155 else
4156 {
4157 // This is a new edit command, not a restart. Need to
4158 // remember it to make 'insertmode' work with mappings for
4159 // Visual mode. But do this only once.
4160 restart_edit_save = restart_edit;
4161 restart_edit = 0;
4162#ifdef FEAT_LINEBREAK
4163 // Restore linebreak, so that when the user edits it looks as
4164 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004165 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004166#endif
4167 op_insert(oap, cap->count1);
4168#ifdef FEAT_LINEBREAK
4169 // Reset linebreak, so that formatting works correctly.
4170 curwin->w_p_lbr = FALSE;
4171#endif
4172
4173 // TODO: when inserting in several lines, should format all
4174 // the lines.
4175 auto_format(FALSE, TRUE);
4176
4177 if (restart_edit == 0)
4178 restart_edit = restart_edit_save;
4179 else
4180 cap->retval |= CA_COMMAND_BUSY;
4181 }
4182 break;
4183
4184 case OP_REPLACE:
4185 VIsual_reselect = FALSE; // don't reselect now
4186 if (empty_region_error)
4187 {
4188 vim_beep(BO_OPER);
4189 CancelRedo();
4190 }
4191 else
4192 {
4193#ifdef FEAT_LINEBREAK
4194 // Restore linebreak, so that when the user edits it looks as
4195 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004196 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004197#endif
4198 op_replace(oap, cap->nchar);
4199 }
4200 break;
4201
4202#ifdef FEAT_FOLDING
4203 case OP_FOLD:
4204 VIsual_reselect = FALSE; // don't reselect now
4205 foldCreate(oap->start.lnum, oap->end.lnum);
4206 break;
4207
4208 case OP_FOLDOPEN:
4209 case OP_FOLDOPENREC:
4210 case OP_FOLDCLOSE:
4211 case OP_FOLDCLOSEREC:
4212 VIsual_reselect = FALSE; // don't reselect now
4213 opFoldRange(oap->start.lnum, oap->end.lnum,
4214 oap->op_type == OP_FOLDOPEN
4215 || oap->op_type == OP_FOLDOPENREC,
4216 oap->op_type == OP_FOLDOPENREC
4217 || oap->op_type == OP_FOLDCLOSEREC,
4218 oap->is_VIsual);
4219 break;
4220
4221 case OP_FOLDDEL:
4222 case OP_FOLDDELREC:
4223 VIsual_reselect = FALSE; // don't reselect now
4224 deleteFold(oap->start.lnum, oap->end.lnum,
4225 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4226 break;
4227#endif
4228 case OP_NR_ADD:
4229 case OP_NR_SUB:
4230 if (empty_region_error)
4231 {
4232 vim_beep(BO_OPER);
4233 CancelRedo();
4234 }
4235 else
4236 {
4237 VIsual_active = TRUE;
4238#ifdef FEAT_LINEBREAK
4239 curwin->w_p_lbr = lbr_saved;
4240#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004241 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004242 VIsual_active = FALSE;
4243 }
4244 check_cursor_col();
4245 break;
4246 default:
4247 clearopbeep(oap);
4248 }
4249 virtual_op = MAYBE;
4250 if (!gui_yank)
4251 {
4252 // if 'sol' not set, go back to old column for some commands
4253 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4254 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4255 || oap->op_type == OP_DELETE))
4256 {
4257#ifdef FEAT_LINEBREAK
4258 curwin->w_p_lbr = FALSE;
4259#endif
4260 coladvance(curwin->w_curswant = old_col);
4261 }
4262 }
4263 else
4264 {
4265 curwin->w_cursor = old_cursor;
4266 }
4267 oap->block_mode = FALSE;
4268 clearop(oap);
4269 motion_force = NUL;
4270 }
4271#ifdef FEAT_LINEBREAK
4272 curwin->w_p_lbr = lbr_saved;
4273#endif
4274}