blob: e0fa344d8ee622307e863f64aace7fd39e9eda4c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
Bram Moolenaar032a2d02020-12-22 17:59:35 +010012 * op_change, op_yank, do_join
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010017static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
Bram Moolenaarf2732452018-06-03 14:47:35 +020021// Flags for third item in "opchars".
22#define OPF_LINES 1 // operator always works on lines
23#define OPF_CHANGE 2 // operator changes text
24
Bram Moolenaar071d4272004-06-13 20:20:40 +000025/*
26 * The names of operators.
27 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020028 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 */
30static char opchars[][3] =
31{
Bram Moolenaarf2732452018-06-03 14:47:35 +020032 {NUL, NUL, 0}, // OP_NOP
33 {'d', NUL, OPF_CHANGE}, // OP_DELETE
34 {'y', NUL, 0}, // OP_YANK
35 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
36 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
37 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
38 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
39 {'g', '~', OPF_CHANGE}, // OP_TILDE
40 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
41 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
42 {':', NUL, OPF_LINES}, // OP_COLON
43 {'g', 'U', OPF_CHANGE}, // OP_UPPER
44 {'g', 'u', OPF_CHANGE}, // OP_LOWER
45 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
46 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
47 {'g', '?', OPF_CHANGE}, // OP_ROT13
48 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
49 {'I', NUL, OPF_CHANGE}, // OP_INSERT
50 {'A', NUL, OPF_CHANGE}, // OP_APPEND
51 {'z', 'f', OPF_LINES}, // OP_FOLD
52 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
53 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
54 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
55 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
56 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
57 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
58 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
59 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
60 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
61 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000062};
63
64/*
65 * Translate a command name into an operator type.
66 * Must only be called with a valid operator name!
67 */
68 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010069get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070{
71 int i;
72
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010073 if (char1 == 'r') // ignore second character
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 return OP_REPLACE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010075 if (char1 == '~') // when tilde is an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 return OP_TILDE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010077 if (char1 == 'g' && char2 == Ctrl_A) // add
Bram Moolenaard79e5502016-01-10 22:13:02 +010078 return OP_NR_ADD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010079 if (char1 == 'g' && char2 == Ctrl_X) // subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +010080 return OP_NR_SUB;
Christian Brabandt544a38e2021-06-10 19:39:11 +020081 if (char1 == 'z' && char2 == 'y') // OP_YANK
82 return OP_YANK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 if (opchars[i][0] == char1 && opchars[i][1] == char2)
86 break;
K.Takataeeec2542021-06-02 13:28:16 +020087 if (i == (int)ARRAY_LENGTH(opchars) - 1)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010088 {
89 internal_error("get_op_type()");
90 break;
91 }
92 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return i;
94}
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * Return TRUE if operator "op" always works on whole lines.
98 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020099 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100100op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200102 return opchars[op][2] & OPF_LINES;
103}
104
Bram Moolenaar113e1072019-01-20 15:30:40 +0100105#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200106/*
107 * Return TRUE if operator "op" changes text.
108 */
109 int
110op_is_change(int op)
111{
112 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
116/*
117 * Get first operator command character.
118 * Returns 'g' or 'z' if there is another command character.
119 */
120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100121get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return opchars[optype][0];
124}
125
126/*
127 * Get second operator command character.
128 */
129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100130get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 return opchars[optype][1];
133}
134
135/*
136 * op_shift - handle a shift operation
137 */
138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100139op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 long i;
142 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145 if (u_save((linenr_T)(oap->start.lnum - 1),
146 (linenr_T)(oap->end.lnum + 1)) == FAIL)
147 return;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 if (oap->block_mode)
150 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152 for (i = oap->line_count; --i >= 0; )
153 {
154 first_char = *ml_get_curline();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100155 if (first_char == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 else if (oap->block_mode)
158 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100160 // Move the line right if it doesn't start with '#', 'smartindent'
161 // isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
163 if (first_char != '#' || !preprocs_left())
164#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000165 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++curwin->w_cursor.lnum;
167 }
168
169 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (oap->block_mode)
171 {
172 curwin->w_cursor.lnum = oap->start.lnum;
173 curwin->w_cursor.col = block_col;
174 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100175 else if (curs_top) // put cursor on first line, for ">>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 {
177 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100178 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
180 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100181 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100184 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100185 foldOpenCursor();
186#endif
187
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (oap->line_count > p_report)
190 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200191 char *op;
192 char *msg_line_single;
193 char *msg_line_plural;
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200196 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200198 op = "<";
199 msg_line_single = NGETTEXT("%ld line %sed %d time",
200 "%ld line %sed %d times", amount);
201 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
202 "%ld lines %sed %d times", amount);
203 vim_snprintf((char *)IObuff, IOSIZE,
204 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
205 oap->line_count, op, amount);
Bram Moolenaare4fc7462020-03-15 19:17:50 +0100206 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
208
Bram Moolenaare1004402020-10-24 20:49:43 +0200209 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100210 {
211 // Set "'[" and "']" marks.
212 curbuf->b_op_start = oap->start;
213 curbuf->b_op_end.lnum = oap->end.lnum;
214 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
215 if (curbuf->b_op_end.col > 0)
216 --curbuf->b_op_end.col;
217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
220/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100221 * Shift the current line one shiftwidth left (if left != 0) or right
222 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 */
224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100225shift_line(
226 int left,
227 int round,
228 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200229 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 int count;
232 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200233 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200235 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200237 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200239 i = count / sw_val; // number of 'shiftwidth' rounded down
240 j = count % sw_val; // extra spaces
241 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 --amount;
243 if (left)
244 {
245 i -= amount;
246 if (i < 0)
247 i = 0;
248 }
249 else
250 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200251 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200253 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
255 if (left)
256 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200257 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 if (count < 0)
259 count = 0;
260 }
261 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200262 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200265 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000267 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000269 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270}
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272/*
273 * Shift one line of the current block one shiftwidth right or left.
274 * Leaves cursor on first character in block.
275 */
276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100277shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278{
279 int left = (oap->op_type == OP_LSHIFT);
280 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000281 int total;
282 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 int sw_val = (int)get_sw_value_indent(curbuf);
285 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000288 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int i = 0, j = 0;
290 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifdef FEAT_RIGHTLEFT
292 int old_p_ri = p_ri;
293
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100294 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100297 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
299 if (bd.is_short)
300 return;
301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100302 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 total = (int)((unsigned)amount * (unsigned)sw_val);
304 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100305 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 oldp = ml_get_curline();
308
309 if (!left)
310 {
311 /*
312 * 1. Get start vcol
313 * 2. Total ws vcols
314 * 3. Divvy into TABs & spp
315 * 4. Construct new string
316 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100317 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 ws_vcol = bd.start_vcol - bd.pre_whitesp;
319 if (bd.startspaces)
320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100322 {
323 if ((*mb_ptr2len)(bd.textstart) == 1)
324 ++bd.textstart;
325 else
326 {
327 ws_vcol = 0;
328 bd.startspaces = 0;
329 }
330 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000331 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000332 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100334 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100336 // TODO: is passing bd.textstart for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200337 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
338 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 total += incr;
340 bd.start_vcol += incr;
341 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100342 // OK, now total=all the VWS reqd, and textstart points at the 1st
343 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200344#ifdef FEAT_VARTABS
345 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200346 tabstop_fromto(ws_vcol, ws_vcol + total,
347 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348 else
349 j = total;
350#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 if (!curbuf->b_p_et)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100352 i = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (i)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100354 j = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 else
356 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100358 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
360 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200361 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (newp == NULL)
363 return;
364 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
365 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200366 vim_memset(newp + bd.textcol, TAB, (size_t)i);
367 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100368 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
370 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100371 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100373 colnr_T destination_col; // column to which text in block will
374 // be shifted
375 char_u *verbatim_copy_end; // end of the part of the line which is
376 // copied verbatim
377 colnr_T verbatim_copy_width;// the (displayed) width of this part
378 // of line
379 unsigned fill; // nr of spaces that replace a TAB
380 unsigned new_line_len; // the length of the line after the
381 // block shift
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000382 size_t block_space_width;
383 size_t shift_amount;
384 char_u *non_white = bd.textstart;
385 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000387 /*
388 * Firstly, let's find the first non-whitespace character that is
389 * displayed after the block's start column and the character's column
390 * number. Also, let's calculate the width of all the whitespace
391 * characters that are displayed in the block and precede the searched
392 * non-whitespace character.
393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100395 // If "bd.startspaces" is set, "bd.textstart" points to the character,
396 // the part of which is displayed at the block's beginning. Let's start
397 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000398 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100399 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100401 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000402 non_white_col = bd.start_vcol;
403
Bram Moolenaar1c465442017-03-12 20:10:05 +0100404 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000405 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200406 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000407 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000410 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100411 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000412 shift_amount = (block_space_width < (size_t)total
413 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100415 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000416 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Now let's find out how much of the beginning of the line we can
419 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000420 verbatim_copy_end = bd.textstart;
421 verbatim_copy_width = bd.start_vcol;
422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100423 // If "bd.startspaces" is set, "bd.textstart" points to the character
424 // preceding the block. We have to subtract its width to obtain its
425 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000426 if (bd.startspaces)
427 verbatim_copy_width -= bd.start_char_vcols;
428 while (verbatim_copy_width < destination_col)
429 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200430 char_u *line = verbatim_copy_end;
431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100432 // TODO: is passing verbatim_copy_end for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200433 incr = lbr_chartabsize(line, verbatim_copy_end,
434 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000435 if (verbatim_copy_width + incr > destination_col)
436 break;
437 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100438 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000439 }
440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100441 // If "destination_col" is different from the width of the initial
442 // part of the line that will be copied, it means we encountered a tab
443 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000444 fill = destination_col - verbatim_copy_width;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // The replacement line will consist of:
447 // - the beginning of the original line up to "verbatim_copy_end",
448 // - "fill" number of spaces,
449 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000450 new_line_len = (unsigned)(verbatim_copy_end - oldp)
451 + fill
452 + (unsigned)STRLEN(non_white) + 1;
453
Bram Moolenaar964b3742019-05-24 18:54:09 +0200454 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (newp == NULL)
456 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200458 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000459 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100461 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
463 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
464 State = oldstate;
465 curwin->w_cursor.col = oldcol;
466#ifdef FEAT_RIGHTLEFT
467 p_ri = old_p_ri;
468#endif
469}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * Insert string "s" (b_insert ? before : after) block :AKelly
473 * Caller must prepare for undo.
474 */
475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100476block_insert(
477 oparg_T *oap,
478 char_u *s,
479 int b_insert,
480 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaardac13472019-09-16 21:06:21 +0200482 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100483 int count = 0; // extra spaces to replace a cut TAB
484 int spaces = 0; // non-zero if cutting a TAB
485 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200486 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 unsigned s_len; // STRLEN(s)
488 char_u *newp, *oldp; // new, old lines
489 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int oldstate = State;
491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100492 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 s_len = (unsigned)STRLEN(s);
494
495 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
496 {
497 block_prep(oap, bdp, lnum, TRUE);
498 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100499 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
501 oldp = ml_get(lnum);
502
503 if (b_insert)
504 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200505 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 spaces = bdp->startspaces;
507 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 offset = bdp->textcol;
510 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100511 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200513 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100514 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200516 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100518 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 offset = bdp->textcol + bdp->textlen - (spaces != 0);
520 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100523 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 if (!bdp->is_MAX)
525 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
526 count = spaces;
527 offset = bdp->textcol + bdp->textlen;
528 }
529 }
530
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200531 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000532 // avoid copying part of a multi-byte character
533 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100534
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200535 if (spaces < 0) // can happen when the cursor was moved
536 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200537
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000538 // Make sure the allocated size matches what is actually copied below.
539 newp = alloc(STRLEN(oldp) + spaces + s_len
540 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
541 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (newp == NULL)
543 continue;
544
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100545 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000546 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 oldp += offset;
548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200550 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200551 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100553 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200554 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 offset += s_len;
556
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000557 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000559 if (*oldp == TAB)
560 {
561 // insert post-padding
562 vim_memset(newp + offset + spaces, ' ',
563 (size_t)(ts_val - spaces));
564 // we're splitting a TAB, don't copy it
565 oldp++;
566 // We allowed for that TAB, remember this now
567 count++;
568 }
569 else
570 // Not a TAB, no extra spaces
571 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
573
574 if (spaces > 0)
575 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000576 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 ml_replace(lnum, newp, FALSE);
579
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200580 if (b_insert)
581 // correct any text properties
582 inserted_bytes(lnum, startcol, s_len);
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (lnum == oap->end.lnum)
585 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100586 // Set "']" mark to the end of the block instead of the end of
587 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 curbuf->b_op_end.lnum = oap->end.lnum;
589 curbuf->b_op_end.col = offset;
590 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100591 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
594
595 State = oldstate;
596}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200599 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200601 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 */
603 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 int n;
607 linenr_T lnum;
608 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 char_u *newp, *oldp;
610 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
612 int did_yank = FALSE;
613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 return OK;
616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 if (oap->empty)
619 return u_save_cursor();
620
621 if (!curbuf->b_p_ma)
622 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200623 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 return FAIL;
625 }
626
627#ifdef FEAT_CLIPBOARD
628 adjust_clip_reg(&oap->regname);
629#endif
630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 if (has_mbyte)
632 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
Bram Moolenaard04b7502010-07-08 22:27:55 +0200634 /*
635 * Imitate the strange Vi behaviour: If the delete spans more than one
636 * line and motion_type == MCHAR and the result is a blank line, make the
637 * delete linewise. Don't do this for the change command or Visual mode.
638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000641 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100643 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 && oap->op_type == OP_DELETE)
645 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200646 ptr = ml_get(oap->end.lnum) + oap->end.col;
647 if (*ptr != NUL)
648 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 ptr = skipwhite(ptr);
650 if (*ptr == NUL && inindent(0))
651 oap->motion_type = MLINE;
652 }
653
Bram Moolenaard04b7502010-07-08 22:27:55 +0200654 /*
655 * Check for trying to delete (e.g. "D") in an empty line.
656 * Note: For the change operator it is ok.
657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if ( oap->motion_type == MCHAR
659 && oap->line_count == 1
660 && oap->op_type == OP_DELETE
661 && *ml_get(oap->start.lnum) == NUL)
662 {
663 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000664 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 * 'cpoptions' (Vi compatible).
666 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
669 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000670 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
672 beep_flush();
673 return OK;
674 }
675
Bram Moolenaard04b7502010-07-08 22:27:55 +0200676 /*
677 * Do a yank of whatever we're about to delete.
678 * If a yank register was specified, put the deleted text into that
679 * register. For the black hole register '_' don't yank anything.
680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (oap->regname != '_')
682 {
683 if (oap->regname != 0)
684 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (!valid_yank_reg(oap->regname, TRUE))
687 {
688 beep_flush();
689 return OK;
690 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100691 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
692 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 did_yank = TRUE;
694 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200695 else
696 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 /*
699 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100700 * delete contains a line break, or when using a specific operator (Vi
701 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200702 * Use the register name from before adjust_clip_reg() may have
703 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100705 if (oap->motion_type == MLINE || oap->line_count > 1
706 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100708 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (op_yank(oap, TRUE, FALSE) == OK)
710 did_yank = TRUE;
711 }
712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 // Yank into small delete register when no named register specified
714 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200715 if ((
716#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200717 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
718 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200719#endif
720 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 && oap->line_count == 1)
722 {
723 oap->regname = '-';
724 get_yank_register(oap->regname, TRUE);
725 if (op_yank(oap, TRUE, FALSE) == OK)
726 did_yank = TRUE;
727 oap->regname = 0;
728 }
729
730 /*
731 * If there's too much stuff to fit in the yank register, then get a
732 * confirmation before doing the delete. This is crude, but simple.
733 * And it avoids doing a delete of something we can't put back if we
734 * want.
735 */
736 if (!did_yank)
737 {
738 int msg_silent_save = msg_silent;
739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100740 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
742 msg_silent = msg_silent_save;
743 if (n != 'y')
744 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000745 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 return FAIL;
747 }
748 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100749
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100750#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100751 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200752 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 }
755
Bram Moolenaard04b7502010-07-08 22:27:55 +0200756 /*
757 * block mode delete
758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (oap->block_mode)
760 {
761 if (u_save((linenr_T)(oap->start.lnum - 1),
762 (linenr_T)(oap->end.lnum + 1)) == FAIL)
763 return FAIL;
764
765 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
766 {
767 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100768 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 continue;
770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (lnum == curwin->w_cursor.lnum)
773 {
774 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 }
777
Bram Moolenaar8055d172019-05-17 22:57:26 +0200778 // "n" == number of chars deleted
779 // If we delete a TAB, it may be replaced by several characters.
780 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 n = bd.textlen - bd.startspaces - bd.endspaces;
782 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200783 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (newp == NULL)
785 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100786 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100788 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200789 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000793 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100794 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200796
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100797#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200798 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200799 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802
803 check_cursor_col();
804 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
805 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100806 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100808 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
810 if (oap->op_type == OP_CHANGE)
811 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100812 // Delete the lines except the first one. Temporarily move the
813 // cursor to the next line. Save the current line number, if the
814 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (oap->line_count > 1)
816 {
817 lnum = curwin->w_cursor.lnum;
818 ++curwin->w_cursor.lnum;
819 del_lines((long)(oap->line_count - 1), TRUE);
820 curwin->w_cursor.lnum = lnum;
821 }
822 if (u_save_cursor() == FAIL)
823 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100824 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 beginline(BL_WHITE); // cursor on first non-white
827 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 ai_col = curwin->w_cursor.col;
829 }
830 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 beginline(0); // cursor in column 0
832 truncate_line(FALSE); // delete the rest of the line
833 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100835 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 else
838 {
839 del_lines(oap->line_count, TRUE);
840 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 }
844 else
845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (virtual_op)
847 {
848 int endcol = 0;
849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100850 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (gchar_pos(&oap->start) == '\t')
852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 return FAIL;
855 if (oap->line_count == 1)
856 endcol = getviscol2(oap->end.col, oap->end.coladd);
857 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
858 oap->start = curwin->w_cursor;
859 if (oap->line_count == 1)
860 {
861 coladvance(endcol);
862 oap->end.col = curwin->w_cursor.col;
863 oap->end.coladd = curwin->w_cursor.coladd;
864 curwin->w_cursor = oap->start;
865 }
866 }
867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100868 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (gchar_pos(&oap->end) == '\t'
870 && (int)oap->end.coladd < oap->inclusive)
871 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100872 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (u_save((linenr_T)(oap->end.lnum - 1),
874 (linenr_T)(oap->end.lnum + 1)) == FAIL)
875 return FAIL;
876 curwin->w_cursor = oap->end;
877 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
878 oap->end = curwin->w_cursor;
879 curwin->w_cursor = oap->start;
880 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100881 if (has_mbyte)
882 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100885 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100887 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 return FAIL;
889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100890 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100891 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 && oap->op_type == OP_CHANGE
893 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100894 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 display_dollar(oap->end.col - !oap->inclusive);
896
897 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (virtual_op)
900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 // fix up things for virtualedit-delete:
902 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 char_u *curline = ml_get_curline();
904 int len = (int)STRLEN(curline);
905
906 if (oap->end.coladd != 0
907 && (int)oap->end.col >= len - 1
908 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
909 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (n == 0 && oap->start.coladd != oap->end.coladd)
912 n = 1;
913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100914 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (gchar_cursor() != NUL)
916 curwin->w_cursor.coladd = 0;
917 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200918 (void)del_bytes((long)n, !virtual_op,
919 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100921 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
923 pos_T curpos;
924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100925 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
927 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
928 return FAIL;
929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100930 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100932 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 ++curwin->w_cursor.lnum;
934 del_lines((long)(oap->line_count - 2), FALSE);
935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100936 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200937 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200938 curwin->w_cursor.col = 0;
939 (void)del_bytes((long)n, !virtual_op,
940 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100941 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200942 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200944 if (oap->op_type == OP_DELETE)
945 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947
948 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
949
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000950setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200951 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100953 if (oap->block_mode)
954 {
955 curbuf->b_op_end.lnum = oap->end.lnum;
956 curbuf->b_op_end.col = oap->start.col;
957 }
958 else
959 curbuf->b_op_end = oap->start;
960 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
963 return OK;
964}
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966/*
967 * Adjust end of operating area for ending on a multi-byte character.
968 * Used for deletion.
969 */
970 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100971mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 char_u *p;
974
975 if (oap->inclusive)
976 {
977 p = ml_get(oap->end.lnum);
978 oap->end.col += mb_tail_off(p, p + oap->end.col);
979 }
980}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar630afe82018-06-28 19:26:28 +0200982/*
983 * Replace the character under the cursor with "c".
984 * This takes care of multi-byte characters.
985 */
986 static void
987replace_character(int c)
988{
989 int n = State;
990
991 State = REPLACE;
992 ins_char(c);
993 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100994 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200995 dec_cursor();
996}
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998/*
999 * Replace a whole area with one character.
1000 */
1001 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001002op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 char_u *newp, *oldp;
1007 size_t oldlen;
1008 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001009 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001010 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001013 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001015 if (c == REPLACE_CR_NCHAR)
1016 {
1017 had_ctrl_v_cr = TRUE;
1018 c = CAR;
1019 }
1020 else if (c == REPLACE_NL_NCHAR)
1021 {
1022 had_ctrl_v_cr = TRUE;
1023 c = NL;
1024 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (has_mbyte)
1027 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 if (u_save((linenr_T)(oap->start.lnum - 1),
1030 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1031 return FAIL;
1032
1033 /*
1034 * block mode replace
1035 */
1036 if (oap->block_mode)
1037 {
1038 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1039 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1040 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001041 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1043 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001046 // n == number of extra chars required
1047 // If we split a TAB, it may be replaced by several characters.
1048 // Thus the number of characters may increase!
1049 // If the range starts in virtual space, count the initial
1050 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1052 {
1053 pos_T vpos;
1054
Bram Moolenaara1381de2009-11-03 15:44:21 +00001055 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 getvpos(&vpos, oap->start_vcol);
1057 bd.startspaces += vpos.coladd;
1058 n = bd.startspaces;
1059 }
1060 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001061 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 numc = oap->end_vcol - oap->start_vcol + 1;
1070 if (bd.is_short && (!virtual_op || bd.is_MAX))
1071 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001073 // A double-wide character can be replaced only up to half the
1074 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if ((*mb_char2cells)(c) > 1)
1076 {
1077 if ((numc & 1) && !bd.is_short)
1078 {
1079 ++bd.endspaces;
1080 ++n;
1081 }
1082 numc = numc / 2;
1083 }
1084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001085 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 num_chars = numc;
1087 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001088 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 n += numc - bd.textlen;
1090
1091 oldp = ml_get_curline();
1092 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001093 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (newp == NULL)
1095 continue;
1096 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001097 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 mch_memmove(newp, oldp, (size_t)bd.textcol);
1099 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001101 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001102 // insert replacement chars CHECK FOR ALLOCATED SPACE
1103 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1104 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001105 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001107 if (has_mbyte)
1108 {
1109 n = (int)STRLEN(newp);
1110 while (--num_chars >= 0)
1111 n += (*mb_char2bytes)(c, newp + n);
1112 }
1113 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001114 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001115 if (!bd.is_short)
1116 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001117 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001118 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001119 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001120 STRMOVE(newp + STRLEN(newp), oldp);
1121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001125 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001126 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001127 if (after_p != NULL)
1128 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001130 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001132 if (after_p != NULL)
1133 {
1134 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1135 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1136 oap->end.lnum++;
1137 vim_free(after_p);
1138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 }
1141 else
1142 {
1143 /*
1144 * MCHAR and MLINE motion replace.
1145 */
1146 if (oap->motion_type == MLINE)
1147 {
1148 oap->start.col = 0;
1149 curwin->w_cursor.col = 0;
1150 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1151 if (oap->end.col)
1152 --oap->end.col;
1153 }
1154 else if (!oap->inclusive)
1155 dec(&(oap->end));
1156
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001157 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 n = gchar_cursor();
1160 if (n != NUL)
1161 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001162 int new_byte_len = (*mb_char2len)(c);
1163 int old_byte_len = mb_ptr2len(ml_get_cursor());
1164
1165 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001167 // This is slow, but it handles replacing a single-byte
1168 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001169 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001170 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001171 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (n == TAB)
1176 {
1177 int end_vcol = 0;
1178
1179 if (curwin->w_cursor.lnum == oap->end.lnum)
1180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001181 // oap->end has to be recalculated when
1182 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 end_vcol = getviscol2(oap->end.col,
1184 oap->end.coladd);
1185 }
1186 coladvance_force(getviscol());
1187 if (curwin->w_cursor.lnum == oap->end.lnum)
1188 getvpos(&oap->end, end_vcol);
1189 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001190 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1194 {
1195 int virtcols = oap->end.coladd;
1196
1197 if (curwin->w_cursor.lnum == oap->start.lnum
1198 && oap->start.col == oap->end.col && oap->start.coladd)
1199 virtcols -= oap->start.coladd;
1200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 // oap->end has been trimmed so it's effectively inclusive;
1202 // as a result an extra +1 must be counted so we don't
1203 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1205 curwin->w_cursor.col -= (virtcols + 1);
1206 for (; virtcols >= 0; virtcols--)
1207 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001208 if ((*mb_char2len)(c) > 1)
1209 replace_character(c);
1210 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001211 PBYTE(curwin->w_cursor, c);
1212 if (inc(&curwin->w_cursor) == -1)
1213 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (inc_cursor() == -1)
1219 break;
1220 }
1221 }
1222
1223 curwin->w_cursor = oap->start;
1224 check_cursor();
1225 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1226
Bram Moolenaare1004402020-10-24 20:49:43 +02001227 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001230 curbuf->b_op_start = oap->start;
1231 curbuf->b_op_end = oap->end;
1232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234 return OK;
1235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001237static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239/*
1240 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1241 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001242 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001243op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001247 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
1249 if (u_save((linenr_T)(oap->start.lnum - 1),
1250 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1251 return;
1252
1253 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
1256 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1257 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001258 int one_change;
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 block_prep(oap, &bd, pos.lnum, FALSE);
1261 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001262 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1263 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001264
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001265#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001266 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1269
Bram Moolenaar009b2592004-10-24 19:18:58 +00001270 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1271 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001273 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 if (did_change)
1278 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1279 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001280 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {
1282 if (oap->motion_type == MLINE)
1283 {
1284 oap->start.col = 0;
1285 pos.col = 0;
1286 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1287 if (oap->end.col)
1288 --oap->end.col;
1289 }
1290 else if (!oap->inclusive)
1291 dec(&(oap->end));
1292
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001293 if (pos.lnum == oap->end.lnum)
1294 did_change = swapchars(oap->op_type, &pos,
1295 oap->end.col - pos.col + 1);
1296 else
1297 for (;;)
1298 {
1299 did_change |= swapchars(oap->op_type, &pos,
1300 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1301 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001302 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001303 break;
1304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (did_change)
1306 {
1307 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1308 0L);
1309#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001310 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
1312 char_u *ptr;
1313 int count;
1314
1315 pos = oap->start;
1316 while (pos.lnum < oap->end.lnum)
1317 {
1318 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001319 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001320 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001322 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 pos.col = 0;
1324 pos.lnum++;
1325 }
1326 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1327 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001328 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001330 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332#endif
1333 }
1334 }
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001337 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
Bram Moolenaare1004402020-10-24 20:49:43 +02001340 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001341 {
1342 // Set '[ and '] marks.
1343 curbuf->b_op_start = oap->start;
1344 curbuf->b_op_end = oap->end;
1345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001348 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001349 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350}
1351
1352/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001353 * Invoke swapchar() on "length" bytes at position "pos".
1354 * "pos" is advanced to just after the changed characters.
1355 * "length" is rounded up to include the whole last multi-byte character.
1356 * Also works correctly when the number of bytes changes.
1357 * Returns TRUE if some character was changed.
1358 */
1359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001361{
1362 int todo;
1363 int did_change = 0;
1364
1365 for (todo = length; todo > 0; --todo)
1366 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001367 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001368 {
1369 int len = (*mb_ptr2len)(ml_get_pos(pos));
1370
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001371 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001372 if (len > 0)
1373 todo -= len - 1;
1374 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001375 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001376 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001377 break;
1378 }
1379 return did_change;
1380}
1381
1382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 * If op_type == OP_UPPER: make uppercase,
1384 * if op_type == OP_LOWER: make lowercase,
1385 * if op_type == OP_ROT13: do rot13 encoding,
1386 * else swap case of character at 'pos'
1387 * returns TRUE when something actually changed.
1388 */
1389 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001390swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 int c;
1393 int nc;
1394
1395 c = gchar_pos(pos);
1396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001397 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (c >= 0x80 && op_type == OP_ROT13)
1399 return FALSE;
1400
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001401 if (op_type == OP_UPPER && c == 0xdf
1402 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001403 {
1404 pos_T sp = curwin->w_cursor;
1405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001407 curwin->w_cursor = *pos;
1408 del_char(FALSE);
1409 ins_char('S');
1410 ins_char('S');
1411 curwin->w_cursor = sp;
1412 inc(pos);
1413 }
1414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 nc = c;
1418 if (MB_ISLOWER(c))
1419 {
1420 if (op_type == OP_ROT13)
1421 nc = ROT13(c, 'a');
1422 else if (op_type != OP_LOWER)
1423 nc = MB_TOUPPER(c);
1424 }
1425 else if (MB_ISUPPER(c))
1426 {
1427 if (op_type == OP_ROT13)
1428 nc = ROT13(c, 'A');
1429 else if (op_type != OP_UPPER)
1430 nc = MB_TOLOWER(c);
1431 }
1432 if (nc != c)
1433 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1435 {
1436 pos_T sp = curwin->w_cursor;
1437
1438 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001439 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001440 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 ins_char(nc);
1442 curwin->w_cursor = sp;
1443 }
1444 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001445 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return TRUE;
1447 }
1448 return FALSE;
1449}
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
1452 * op_insert - Insert and append operators for Visual mode.
1453 */
1454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001455op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 long ins_len, pre_textlen = 0;
1458 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001459 colnr_T ind_pre_col = 0, ind_post_col;
1460 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 struct block_def bd;
1462 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001463 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001464 pos_T start_insert;
1465 // offset when cursor was moved in insert mode
1466 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001468 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 curwin->w_cursor.lnum = oap->start.lnum;
1473 update_screen(INVERTED);
1474
1475 if (oap->block_mode)
1476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001477 // When 'virtualedit' is used, need to insert the extra spaces before
1478 // doing block_prep(). When only "block" is used, virtual edit is
1479 // already disabled, but still need it when calling
1480 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001481 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001482 // state for the current window. To override that state, we need to
1483 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (curwin->w_cursor.coladd > 0)
1485 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001486 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (u_save_cursor() == FAIL)
1489 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001490
Gary Johnson51ad8502021-08-03 18:33:08 +02001491 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 coladvance_force(oap->op_type == OP_APPEND
1493 ? oap->end_vcol + 1 : getviscol());
1494 if (oap->op_type == OP_APPEND)
1495 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001496 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001500 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001501 ind_pre_col = (colnr_T)getwhitecols_curline();
1502 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (oap->op_type == OP_APPEND)
1506 firstline += bd.textlen;
1507 pre_textlen = (long)STRLEN(firstline);
1508 }
1509
1510 if (oap->op_type == OP_APPEND)
1511 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001512 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001514 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 curwin->w_set_curswant = TRUE;
1516 while (*ml_get_cursor() != NUL
1517 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1518 ++curwin->w_cursor.col;
1519 if (bd.is_short && !bd.is_MAX)
1520 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001521 // First line was too short, make it longer and adjust the
1522 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 if (u_save_cursor() == FAIL)
1524 return;
1525 for (i = 0; i < bd.endspaces; ++i)
1526 ins_char(' ');
1527 bd.textlen += bd.endspaces;
1528 }
1529 }
1530 else
1531 {
1532 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001533 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001535 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001536 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 && oap->start_vcol != oap->end_vcol)
1538 inc_cursor();
1539 }
1540 }
1541
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001542 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001543 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001544 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001546 // When a tab was inserted, and the characters in front of the tab
1547 // have been converted to a tab as well, the column of the cursor
1548 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001549 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001550 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001551 oap->start = curbuf->b_op_start_orig;
1552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001553 // If user has moved off this line, we don't know what to do, so do
1554 // nothing.
1555 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001556 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 return;
1558
1559 if (oap->block_mode)
1560 {
1561 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001562 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001563 size_t len;
1564 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001566 // If indent kicked in, the firstline might have changed
1567 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001568 ind_post_col = (colnr_T)getwhitecols_curline();
1569 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001570 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001571 bd.textcol += ind_post_col - ind_pre_col;
1572 ind_post_vcol = get_indent();
1573 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001574 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001575 }
1576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001577 // The user may have moved the cursor before inserting something, try
1578 // to adjust the block for that. But only do it, if the difference
1579 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001580 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1581 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001582 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001583 int t = getviscol2(curbuf->b_op_start_orig.col,
1584 curbuf->b_op_start_orig.coladd);
1585
1586 if (!bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001587 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001588 if (oap->op_type == OP_INSERT
1589 && oap->start.col + oap->start.coladd
1590 != curbuf->b_op_start_orig.col
1591 + curbuf->b_op_start_orig.coladd)
1592 {
1593 oap->start.col = curbuf->b_op_start_orig.col;
1594 pre_textlen -= t - oap->start_vcol;
1595 oap->start_vcol = t;
1596 }
1597 else if (oap->op_type == OP_APPEND
Bram Moolenaar9f8c3042022-01-17 17:30:21 +00001598 && oap->start.col + oap->start.coladd
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001599 >= curbuf->b_op_start_orig.col
1600 + curbuf->b_op_start_orig.coladd)
1601 {
1602 oap->start.col = curbuf->b_op_start_orig.col;
1603 // reset pre_textlen to the value of OP_INSERT
1604 pre_textlen += bd.textlen;
1605 pre_textlen -= t - oap->start_vcol;
1606 oap->start_vcol = t;
1607 oap->op_type = OP_INSERT;
1608 }
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001609 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001610 else if (bd.is_MAX && oap->op_type == OP_APPEND)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001611 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001612 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001613 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001614 pre_textlen -= t - oap->start_vcol;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001615 }
1616 }
1617
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001618 // Spaces and tabs in the indent may have changed to other spaces and
1619 // tabs. Get the starting column again and correct the length.
1620 // Don't do this when "$" used, end-of-line will have changed.
1621 //
1622 // if indent was added and the inserted text was after the indent,
1623 // correct the selection for the new indent.
1624 if (did_indent && bd.textcol - ind_post_col > 0)
1625 {
1626 oap->start.col += ind_post_col - ind_pre_col;
1627 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1628 oap->end.col += ind_post_col - ind_pre_col;
1629 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001632 if (did_indent && bd.textcol - ind_post_col > 0)
1633 {
1634 // undo for where "oap" is used below
1635 oap->start.col -= ind_post_col - ind_pre_col;
1636 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1637 oap->end.col -= ind_post_col - ind_pre_col;
1638 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1641 {
1642 if (oap->op_type == OP_APPEND)
1643 {
1644 pre_textlen += bd2.textlen - bd.textlen;
1645 if (bd2.endspaces)
1646 --bd2.textlen;
1647 }
1648 bd.textcol = bd2.textcol;
1649 bd.textlen = bd2.textlen;
1650 }
1651
1652 /*
1653 * Subsequent calls to ml_get() flush the firstline data - take a
1654 * copy of the required string.
1655 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001656 firstline = ml_get(oap->start.lnum);
1657 len = STRLEN(firstline);
1658 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001660 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001661 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001662 // account for pressing cursor in insert mode when '$' was used
1663 if (bd.is_MAX
1664 && (start_insert.lnum == Insstart.lnum
1665 && start_insert.col > Insstart.col))
1666 {
1667 offset = (start_insert.col - Insstart.col);
1668 add -= offset;
1669 if (oap->end_vcol > offset)
1670 oap->end_vcol -= (offset + 1);
1671 else
1672 // moved outside of the visual block, what to do?
1673 return;
1674 }
1675 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001676 if ((size_t)add > len)
1677 firstline += len; // short line, point to the NUL
1678 else
1679 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001680 if (pre_textlen >= 0 && (ins_len =
1681 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001683 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (ins_text != NULL)
1685 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001686 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 if (u_save(oap->start.lnum,
1688 (linenr_T)(oap->end.lnum + 1)) == OK)
1689 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1690 &bd);
1691
1692 curwin->w_cursor.col = oap->start.col;
1693 check_cursor();
1694 vim_free(ins_text);
1695 }
1696 }
1697 }
1698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700/*
1701 * op_change - handle a change operation
1702 *
1703 * return TRUE if edit() returns because of a CTRL-O command
1704 */
1705 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001706op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 colnr_T l;
1709 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 long offset;
1711 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001712 long ins_len;
1713 long pre_textlen = 0;
1714 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 char_u *firstline;
1716 char_u *ins_text, *newp, *oldp;
1717 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719 l = oap->start.col;
1720 if (oap->motion_type == MLINE)
1721 {
1722 l = 0;
1723#ifdef FEAT_SMARTINDENT
1724 if (!p_paste && curbuf->b_p_si
1725# ifdef FEAT_CINDENT
1726 && !curbuf->b_p_cin
1727# endif
1728 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731 }
1732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001733 // First delete the text in the region. In an empty buffer only need to
1734 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1736 {
1737 if (u_save_cursor() == FAIL)
1738 return FALSE;
1739 }
1740 else if (op_delete(oap) == FAIL)
1741 return FALSE;
1742
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001743 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 && !virtual_op)
1745 inc_cursor();
1746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001747 // check for still on same line (<CR> in inserted text meaningless)
1748 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (oap->block_mode)
1750 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001751 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (virtual_op && (curwin->w_cursor.coladd > 0
1753 || gchar_cursor() == NUL))
1754 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001755 firstline = ml_get(oap->start.lnum);
1756 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001757 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 bd.textcol = curwin->w_cursor.col;
1759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1762 if (oap->motion_type == MLINE)
1763 fix_indent();
1764#endif
1765
1766 retval = edit(NUL, FALSE, (linenr_T)1);
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001769 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001771 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001773 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001775 // Auto-indenting may have changed the indent. If the cursor was past
1776 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001778 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001780 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001781
1782 pre_textlen += new_indent - pre_indent;
1783 bd.textcol += new_indent - pre_indent;
1784 }
1785
1786 ins_len = (long)STRLEN(firstline) - pre_textlen;
1787 if (ins_len > 0)
1788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001789 // Subsequent calls to ml_get() flush the firstline data - take a
1790 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001791 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001793 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1795 linenr++)
1796 {
1797 block_prep(oap, &bd, linenr, TRUE);
1798 if (!bd.is_short || virtual_op)
1799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 pos_T vpos;
1801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001802 // If the block starts in virtual space, count the
1803 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (bd.is_short)
1805 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001806 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 else
1810 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001812 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (newp == NULL)
1814 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001815 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 mch_memmove(newp, oldp, (size_t)bd.textcol);
1817 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001818 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1821 offset += ins_len;
1822 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001823 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 ml_replace(linenr, newp, FALSE);
1825 }
1826 }
1827 check_cursor();
1828
1829 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1830 }
1831 vim_free(ins_text);
1832 }
1833 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001834 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 return retval;
1837}
1838
1839/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001840 * When the cursor is on the NUL past the end of the line and it should not be
1841 * there move it left.
1842 */
1843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001844adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001845{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001846 unsigned int cur_ve_flags = get_ve_flags();
1847
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001848 if (curwin->w_cursor.col > 0
1849 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001850 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 && !(restart_edit || (State & INSERT)))
1852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001853 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001854 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001855
Gary Johnson53ba05b2021-07-26 22:19:10 +02001856 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001857 {
1858 colnr_T scol, ecol;
1859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001860 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001861 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1862 curwin->w_cursor.coladd = ecol - scol + 1;
1863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 }
1865}
1866
Bram Moolenaar81340392012-06-06 16:12:59 +02001867/*
1868 * If "process" is TRUE and the line begins with a comment leader (possibly
1869 * after some white space), return a pointer to the text after it. Put a boolean
1870 * value indicating whether the line ends with an unclosed comment in
1871 * "is_comment".
1872 * line - line to be processed,
1873 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001874 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001875 * include_space - whether to also skip space following the comment leader,
1876 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001877 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001878 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001879 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001880skip_comment(
1881 char_u *line,
1882 int process,
1883 int include_space,
1884 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001885{
1886 char_u *comment_flags = NULL;
1887 int lead_len;
1888 int leader_offset = get_last_leader_offset(line, &comment_flags);
1889
1890 *is_comment = FALSE;
1891 if (leader_offset != -1)
1892 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 // Let's check whether the line ends with an unclosed comment.
1894 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001895 while (*comment_flags)
1896 {
1897 if (*comment_flags == COM_END
1898 || *comment_flags == ':')
1899 break;
1900 ++comment_flags;
1901 }
1902 if (*comment_flags != COM_END)
1903 *is_comment = TRUE;
1904 }
1905
1906 if (process == FALSE)
1907 return line;
1908
1909 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1910
1911 if (lead_len == 0)
1912 return line;
1913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001914 // Find:
1915 // - COM_END,
1916 // - colon,
1917 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001918 while (*comment_flags)
1919 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001920 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001921 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001922 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001923 ++comment_flags;
1924 }
1925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001926 // If we found a colon, it means that we are not processing a line
1927 // starting with a closing part of a three-part comment. That's good,
1928 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001929 if (*comment_flags == ':' || *comment_flags == NUL)
1930 line += lead_len;
1931
1932 return line;
1933}
Bram Moolenaar81340392012-06-06 16:12:59 +02001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001936 * Join 'count' lines (minimal 2) at cursor position.
1937 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001938 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1939 * leaders should not be removed.
1940 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1941 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001943 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 */
1945 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001946do_join(
1947 long count,
1948 int insert_space,
1949 int save_undo,
1950 int use_formatoptions UNUSED,
1951 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001953 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001954 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001955 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001957 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001958 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001959 int endcurr1 = NUL;
1960 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 int currsize = 0; // size of the current line
1962 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001964 colnr_T col = 0;
1965 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001966 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001967 int remove_comments = (use_formatoptions == TRUE)
1968 && has_format_option(FO_REMOVE_COMS);
1969 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001970#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001971 int propcount = 0; // number of props over all joined lines
1972 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001975 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1976 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001979 // Allocate an array to store the number of spaces inserted before each
1980 // line. We will use it to pre-compute the length of the new line and the
1981 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001982 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001983 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001985 if (remove_comments)
1986 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001987 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001988 if (comments == NULL)
1989 {
1990 vim_free(spaces);
1991 return FAIL;
1992 }
1993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001996 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001997 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001998 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002000 for (t = 0; t < count; ++t)
2001 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002002 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002003#ifdef FEAT_PROP_POPUP
2004 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
2005#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002006 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002008 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002009 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2010 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2011 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002012 if (remove_comments)
2013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002014 // We don't want to remove the comment leader if the
2015 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002016 if (t > 0 && prev_was_comment)
2017 {
2018
2019 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2020 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002021 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002022 curr = new_curr;
2023 }
2024 else
2025 curr = skip_comment(curr, FALSE, insert_space,
2026 &prev_was_comment);
2027 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002028
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002029 if (insert_space && t > 0)
2030 {
2031 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002032 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002033 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002034 && (!has_format_option(FO_MBYTE_JOIN)
2035 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2036 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002037 || (mb_ptr2char(curr) < 0x100
2038 && !(enc_utf8 && utf_eat_space(endcurr1)))
2039 || (endcurr1 < 0x100
2040 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002041 )
2042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 if (endcurr1 == ' ')
2045 endcurr1 = endcurr2;
2046 else
2047 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002048 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002049 if ( p_js
2050 && (endcurr1 == '.'
2051 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2052 && (endcurr1 == '?' || endcurr1 == '!'))))
2053 ++spaces[t];
2054 }
2055 }
2056 currsize = (int)STRLEN(curr);
2057 sumsize += currsize + spaces[t];
2058 endcurr1 = endcurr2 = NUL;
2059 if (insert_space && currsize > 0)
2060 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002061 if (has_mbyte)
2062 {
2063 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002064 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 endcurr1 = (*mb_ptr2char)(cend);
2066 if (cend > curr)
2067 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002068 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002069 endcurr2 = (*mb_ptr2char)(cend);
2070 }
2071 }
2072 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002073 {
2074 endcurr1 = *(curr + currsize - 1);
2075 if (currsize > 1)
2076 endcurr2 = *(curr + currsize - 2);
2077 }
2078 }
2079 line_breakcheck();
2080 if (got_int)
2081 {
2082 ret = FAIL;
2083 goto theend;
2084 }
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002087 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002088 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002090 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002091 newp_len = sumsize + 1;
2092#ifdef FEAT_PROP_POPUP
2093 newp_len += propcount * sizeof(textprop_T);
2094#endif
2095 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002096 if (newp == NULL)
2097 {
2098 ret = FAIL;
2099 goto theend;
2100 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002101 cend = newp + sumsize;
2102 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 /*
2105 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002106 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002107 *
2108 * Move marks from each deleted line to the joined line, adjusting the
2109 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2110 * should not really be a problem.
2111 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002112#ifdef FEAT_PROP_POPUP
2113 props_remaining = propcount;
2114#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002115 for (t = count - 1; ; --t)
2116 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002117 int spaces_removed;
2118
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002119 cend -= currsize;
2120 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002121
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002122 if (spaces[t] > 0)
2123 {
2124 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002125 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002126 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002127
2128 // If deleting more spaces than adding, the cursor moves no more than
2129 // what is added if it is inside these spaces.
2130 spaces_removed = (curr - curr_start) - spaces[t];
2131
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002132 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002133 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002134#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002135 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2136 curwin->w_cursor.lnum + t, t == count - 1,
2137 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002138#endif
2139
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002140 if (t == 0)
2141 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002142 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002143 if (remove_comments)
2144 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002145 if (insert_space && t > 1)
2146 curr = skipwhite(curr);
2147 currsize = (int)STRLEN(curr);
2148 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002149
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002150 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
Bram Moolenaare1004402020-10-24 20:49:43 +02002152 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002153 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002154 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002155 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002156 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002157 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002159 // Only report the change in the first line here, del_lines() will report
2160 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 changed_lines(curwin->w_cursor.lnum, currsize,
2162 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002164 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 * briefly, and then move it back. After del_lines() the cursor may
2166 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
2168 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 curwin->w_cursor.lnum = t;
2172
2173 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002174 * Set the cursor column:
2175 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002176 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002178 curwin->w_cursor.col =
2179 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002181
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 curwin->w_set_curswant = TRUE;
2184
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002185theend:
2186 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002187 if (remove_comments)
2188 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002189 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190}
2191
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 * prepare a few things for block mode yank/delete/tilde
2194 *
2195 * for delete:
2196 * - textlen includes the first/last char to be (partly) deleted
2197 * - start/endspaces is the number of columns that are taken by the
2198 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002199 * deleted.
2200 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 * - textlen includes the first/last char to be wholly yanked
2202 * - start/endspaces is the number of columns of the first/last yanked char
2203 * that are to be yanked.
2204 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002205 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002206block_prep(
2207 oparg_T *oap,
2208 struct block_def *bdp,
2209 linenr_T lnum,
2210 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 int incr = 0;
2213 char_u *pend;
2214 char_u *pstart;
2215 char_u *line;
2216 char_u *prev_pstart;
2217 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002218#ifdef FEAT_LINEBREAK
2219 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002221 // Avoid a problem with unwanted linebreaks in block mode.
2222 curwin->w_p_lbr = FALSE;
2223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 bdp->startspaces = 0;
2225 bdp->endspaces = 0;
2226 bdp->textlen = 0;
2227 bdp->start_vcol = 0;
2228 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 bdp->is_short = FALSE;
2230 bdp->is_oneChar = FALSE;
2231 bdp->pre_whitesp = 0;
2232 bdp->pre_whitesp_c = 0;
2233 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 bdp->start_char_vcols = 0;
2235
2236 line = ml_get(lnum);
2237 pstart = line;
2238 prev_pstart = line;
2239 while (bdp->start_vcol < oap->start_vcol && *pstart)
2240 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002242 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002244 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 bdp->pre_whitesp += incr;
2247 bdp->pre_whitesp_c++;
2248 }
2249 else
2250 {
2251 bdp->pre_whitesp = 0;
2252 bdp->pre_whitesp_c = 0;
2253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002255 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002258 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
2260 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (!is_del || oap->op_type == OP_APPEND)
2263 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2264 }
2265 else
2266 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002267 // notice: this converts partly selected Multibyte characters to
2268 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2270 if (is_del && bdp->startspaces)
2271 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2272 pend = pstart;
2273 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002274 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (oap->op_type == OP_INSERT)
2278 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2279 else if (oap->op_type == OP_APPEND)
2280 {
2281 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2282 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2283 }
2284 else
2285 {
2286 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2287 if (is_del && oap->op_type != OP_LSHIFT)
2288 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 // just putting the sum of those two into
2290 // bdp->startspaces doesn't work for Visual replace,
2291 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 bdp->startspaces = bdp->start_char_vcols
2293 - (bdp->start_vcol - oap->start_vcol);
2294 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2295 }
2296 }
2297 }
2298 else
2299 {
2300 prev_pend = pend;
2301 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2302 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002305 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 bdp->end_vcol += incr;
2307 }
2308 if (bdp->end_vcol <= oap->end_vcol
2309 && (!is_del
2310 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002311 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002314 // Alternative: include spaces to fill up the block.
2315 // Disadvantage: can lead to trailing spaces when the line is
2316 // short where the text is put
2317 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (oap->op_type == OP_APPEND || virtual_op)
2319 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002320 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324 else if (bdp->end_vcol > oap->end_vcol)
2325 {
2326 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2327 if (!is_del && bdp->endspaces)
2328 {
2329 bdp->endspaces = incr - bdp->endspaces;
2330 if (pend != pstart)
2331 pend = prev_pend;
2332 }
2333 }
2334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (is_del && bdp->startspaces)
2337 pstart = prev_pstart;
2338 bdp->textlen = (int)(pend - pstart);
2339 }
2340 bdp->textcol = (colnr_T) (pstart - line);
2341 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002342#ifdef FEAT_LINEBREAK
2343 curwin->w_p_lbr = lbr_saved;
2344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002348 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002351op_addsub(
2352 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 linenr_T Prenum1, // Amount of add/subtract
2354 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002356 pos_T pos;
2357 struct block_def bd;
2358 int change_cnt = 0;
2359 linenr_T amount = Prenum1;
2360
Bram Moolenaar6b731882018-11-16 20:54:47 +01002361 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002362 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002363 // the call to changed_lines().
2364#ifdef FEAT_FOLDING
2365 disable_fold_update++;
2366#endif
2367
Bram Moolenaard79e5502016-01-10 22:13:02 +01002368 if (!VIsual_active)
2369 {
2370 pos = curwin->w_cursor;
2371 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002372 {
2373#ifdef FEAT_FOLDING
2374 disable_fold_update--;
2375#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002376 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002377 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002378 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002379#ifdef FEAT_FOLDING
2380 disable_fold_update--;
2381#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002382 if (change_cnt)
2383 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2384 }
2385 else
2386 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002387 int one_change;
2388 int length;
2389 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002390
2391 if (u_save((linenr_T)(oap->start.lnum - 1),
2392 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002393 {
2394#ifdef FEAT_FOLDING
2395 disable_fold_update--;
2396#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002397 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002398 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002399
2400 pos = oap->start;
2401 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2402 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002403 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002404 {
2405 block_prep(oap, &bd, pos.lnum, FALSE);
2406 pos.col = bd.textcol;
2407 length = bd.textlen;
2408 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002409 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002410 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002411 curwin->w_cursor.col = 0;
2412 pos.col = 0;
2413 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2414 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002415 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002416 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002417 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002418 dec(&(oap->end));
2419 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2420 pos.col = 0;
2421 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002422 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002423 pos.col += oap->start.col;
2424 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002425 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002426 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002427 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002428 length = (int)STRLEN(ml_get(oap->end.lnum));
2429 if (oap->end.col >= length)
2430 oap->end.col = length - 1;
2431 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002432 }
2433 }
2434 one_change = do_addsub(oap->op_type, &pos, length, amount);
2435 if (one_change)
2436 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002437 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002438 if (change_cnt == 0)
2439 startpos = curbuf->b_op_start;
2440 ++change_cnt;
2441 }
2442
2443#ifdef FEAT_NETBEANS_INTG
2444 if (netbeans_active() && one_change)
2445 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002446 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002447
2448 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002449 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002450 netbeans_inserted(curbuf, pos.lnum, pos.col,
2451 &ptr[pos.col], length);
2452 }
2453#endif
2454 if (g_cmd && one_change)
2455 amount += Prenum1;
2456 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002457
2458#ifdef FEAT_FOLDING
2459 disable_fold_update--;
2460#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002461 if (change_cnt)
2462 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2463
2464 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002465 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002466 redraw_curbuf_later(INVERTED);
2467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002468 // Set '[ mark if something changed. Keep the last end
2469 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002470 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002471 curbuf->b_op_start = startpos;
2472
2473 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002474 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002475 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002476 }
2477}
2478
2479/*
2480 * Add or subtract 'Prenum1' from a number in a line
2481 * op_type is OP_NR_ADD or OP_NR_SUB
2482 *
2483 * Returns TRUE if some character was changed.
2484 */
2485 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002486do_addsub(
2487 int op_type,
2488 pos_T *pos,
2489 int length,
2490 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002491{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 int col;
2493 char_u *buf1;
2494 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002495 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2496 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002497 uvarnumber_T n;
2498 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 char_u *ptr;
2500 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002502 int do_hex;
2503 int do_oct;
2504 int do_bin;
2505 int do_alpha;
2506 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002509 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002510 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002511 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002512 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002513 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002514 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002515 pos_T startpos;
2516 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002517 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002519 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2520 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2521 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2522 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2523 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002525 if (virtual_active())
2526 {
2527 save_coladd = pos->coladd;
2528 pos->coladd = 0;
2529 }
2530
Bram Moolenaard79e5502016-01-10 22:13:02 +01002531 curwin->w_cursor = *pos;
2532 ptr = ml_get(pos->lnum);
2533 col = pos->col;
2534
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002535 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002536 goto theend;
2537
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 /*
2539 * First check if we are on a hexadecimal number, after the "0x".
2540 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002541 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002543 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002544 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002545 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002546 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002547 if (has_mbyte)
2548 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002549 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002550
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002551 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002552 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002553 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002554 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002555 if (has_mbyte)
2556 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002557 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002558
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002559 if ( do_bin
2560 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002561 && ! ((col > 0
2562 && (ptr[col] == 'X'
2563 || ptr[col] == 'x')
2564 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002565 && (!has_mbyte ||
2566 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002567 && vim_isxdigit(ptr[col + 1]))))
2568 {
2569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002570 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002571
Bram Moolenaard79e5502016-01-10 22:13:02 +01002572 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002573
2574 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002575 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002576 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002577 if (has_mbyte)
2578 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002579 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002580 }
2581
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002582 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002583 && col > 0
2584 && (ptr[col] == 'X'
2585 || ptr[col] == 'x')
2586 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002587 && (!has_mbyte ||
2588 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002589 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002590 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002591 && col > 0
2592 && (ptr[col] == 'B'
2593 || ptr[col] == 'b')
2594 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002595 && (!has_mbyte ||
2596 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002597 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002599 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002600 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002601 if (has_mbyte)
2602 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002603 }
2604 else
2605 {
2606 /*
2607 * Search forward and then backward to find the start of number.
2608 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002609 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002610
2611 while (ptr[col] != NUL
2612 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002613 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002614 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002615
2616 while (col > 0
2617 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002618 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002619 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002620 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002621 if (has_mbyte)
2622 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002623 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002624 }
2625 }
2626
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002627 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002628 {
2629 while (ptr[col] != NUL && length > 0
2630 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002631 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002632 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002633 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002634
2635 col += mb_len;
2636 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002637 }
2638
2639 if (length == 0)
2640 goto theend;
2641
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002642 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002643 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2644 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002645 {
2646 negative = TRUE;
2647 was_positive = FALSE;
2648 }
2649 }
2650
2651 /*
2652 * If a number was found, and saving for undo works, replace the number.
2653 */
2654 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002655 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002656 {
2657 beep_flush();
2658 goto theend;
2659 }
2660
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002661 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002662 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002663 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002664 if (op_type == OP_NR_SUB)
2665 {
2666 if (CharOrd(firstdigit) < Prenum1)
2667 {
2668 if (isupper(firstdigit))
2669 firstdigit = 'A';
2670 else
2671 firstdigit = 'a';
2672 }
2673 else
2674#ifdef EBCDIC
2675 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2676#else
2677 firstdigit -= Prenum1;
2678#endif
2679 }
2680 else
2681 {
2682 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2683 {
2684 if (isupper(firstdigit))
2685 firstdigit = 'Z';
2686 else
2687 firstdigit = 'z';
2688 }
2689 else
2690#ifdef EBCDIC
2691 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2692#else
2693 firstdigit += Prenum1;
2694#endif
2695 }
2696 curwin->w_cursor.col = col;
2697 if (!did_change)
2698 startpos = curwin->w_cursor;
2699 did_change = TRUE;
2700 (void)del_char(FALSE);
2701 ins_char(firstdigit);
2702 endpos = curwin->w_cursor;
2703 curwin->w_cursor.col = col;
2704 }
2705 else
2706 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002707 pos_T save_pos;
2708 int i;
2709
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002710 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002711 && (!has_mbyte ||
2712 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002713 && !visual
2714 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002717 --col;
2718 negative = TRUE;
2719 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002721 if (visual && VIsual_mode != 'V')
2722 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2723 ? (int)STRLEN(ptr) - col
2724 : length);
2725
2726 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002727 0 + (do_bin ? STR2NR_BIN : 0)
2728 + (do_oct ? STR2NR_OCT : 0)
2729 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002730 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002731
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002733 if (pre && negative)
2734 {
2735 ++col;
2736 --length;
2737 negative = FALSE;
2738 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002740 subtract = FALSE;
2741 if (op_type == OP_NR_SUB)
2742 subtract ^= TRUE;
2743 if (negative)
2744 subtract ^= TRUE;
2745
2746 oldn = n;
2747 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002748 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002749 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002750 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002752 if (!pre)
2753 {
2754 if (subtract)
2755 {
2756 if (n > oldn)
2757 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002758 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002759 negative ^= TRUE;
2760 }
2761 }
2762 else
2763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002765 if (n < oldn)
2766 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002767 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002768 negative ^= TRUE;
2769 }
2770 }
2771 if (n == 0)
2772 negative = FALSE;
2773 }
2774
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002775 if (do_unsigned && negative)
2776 {
2777 if (subtract)
2778 // sticking at zero.
2779 n = (uvarnumber_T)0;
2780 else
2781 // sticking at 2^64 - 1.
2782 n = (uvarnumber_T)(-1);
2783 negative = FALSE;
2784 }
2785
Bram Moolenaard79e5502016-01-10 22:13:02 +01002786 if (visual && !was_positive && !negative && col > 0)
2787 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002788 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002789 col--;
2790 length++;
2791 }
2792
2793 /*
2794 * Delete the old number.
2795 */
2796 curwin->w_cursor.col = col;
2797 if (!did_change)
2798 startpos = curwin->w_cursor;
2799 did_change = TRUE;
2800 todel = length;
2801 c = gchar_cursor();
2802 /*
2803 * Don't include the '-' in the length, only the length of the
2804 * part after it is kept the same.
2805 */
2806 if (c == '-')
2807 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002808
2809 save_pos = curwin->w_cursor;
2810 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002811 {
2812 if (c < 0x100 && isalpha(c))
2813 {
2814 if (isupper(c))
2815 hexupper = TRUE;
2816 else
2817 hexupper = FALSE;
2818 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002819 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002820 c = gchar_cursor();
2821 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002822 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002823
2824 /*
2825 * Prepare the leading characters in buf1[].
2826 * When there are many leading zeros it could be very long.
2827 * Allocate a bit too much.
2828 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002829 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002830 if (buf1 == NULL)
2831 goto theend;
2832 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002833 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002834 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002835 if (pre)
2836 {
2837 *ptr++ = '0';
2838 --length;
2839 }
2840 if (pre == 'b' || pre == 'B' ||
2841 pre == 'x' || pre == 'X')
2842 {
2843 *ptr++ = pre;
2844 --length;
2845 }
2846
2847 /*
2848 * Put the number characters in buf2[].
2849 */
2850 if (pre == 'b' || pre == 'B')
2851 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002852 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002853 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002856 for (bit = bits; bit > 0; bit--)
2857 if ((n >> (bit - 1)) & 0x1) break;
2858
2859 for (i = 0; bit > 0; bit--)
2860 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2861
2862 buf2[i] = '\0';
2863 }
2864 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002865 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002866 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002867 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002868 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002869 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002870 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002871 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002872 length -= (int)STRLEN(buf2);
2873
2874 /*
2875 * Adjust number of zeros to the new number of digits, so the
2876 * total length of the number remains the same.
2877 * Don't do this when
2878 * the result may look like an octal number.
2879 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002880 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002881 while (length-- > 0)
2882 *ptr++ = '0';
2883 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002884
Bram Moolenaard79e5502016-01-10 22:13:02 +01002885 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002886
2887 // Insert just after the first character to be removed, so that any
2888 // text properties will be adjusted. Then delete the old number
2889 // afterwards.
2890 save_pos = curwin->w_cursor;
2891 if (todel > 0)
2892 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002893 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002894 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002895
2896 // del_char() will also mark line needing displaying
2897 if (todel > 0)
2898 {
2899 int bytes_after = (int)STRLEN(ml_get_curline())
2900 - curwin->w_cursor.col;
2901
2902 // Delete the one character before the insert.
2903 curwin->w_cursor = save_pos;
2904 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002905 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2906 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002907 --todel;
2908 }
2909 while (todel-- > 0)
2910 (void)del_char(FALSE);
2911
Bram Moolenaard79e5502016-01-10 22:13:02 +01002912 endpos = curwin->w_cursor;
2913 if (did_change && curwin->w_cursor.col)
2914 --curwin->w_cursor.col;
2915 }
2916
Bram Moolenaare1004402020-10-24 20:49:43 +02002917 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002920 curbuf->b_op_start = startpos;
2921 curbuf->b_op_end = endpos;
2922 if (curbuf->b_op_end.col > 0)
2923 --curbuf->b_op_end.col;
2924 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002925
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002926theend:
2927 if (visual)
2928 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002929 else if (did_change)
2930 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002931 else if (virtual_active())
2932 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002933
Bram Moolenaard79e5502016-01-10 22:13:02 +01002934 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
2936
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002938clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002940 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941}
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002944 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 *
2946 * "Words" are counted by looking for boundaries between non-space and
2947 * space characters. (it seems to produce results that match 'wc'.)
2948 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002949 * Return value is byte count; word count for the line is added to "*wc".
2950 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 *
2952 * The function will only examine the first "limit" characters in the
2953 * line, stopping if it encounters an end-of-line (NUL byte). In that
2954 * case, eol_size will be added to the character count to account for
2955 * the size of the EOL character.
2956 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002957 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002958line_count_info(
2959 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002960 varnumber_T *wc,
2961 varnumber_T *cc,
2962 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002963 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002965 varnumber_T i;
2966 varnumber_T words = 0;
2967 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 int is_word = 0;
2969
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002970 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {
2972 if (is_word)
2973 {
2974 if (vim_isspace(line[i]))
2975 {
2976 words++;
2977 is_word = 0;
2978 }
2979 }
2980 else if (!vim_isspace(line[i]))
2981 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002982 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002983 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
2985
2986 if (is_word)
2987 words++;
2988 *wc += words;
2989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002991 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002994 chars += eol_size;
2995 }
2996 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 return i;
2998}
2999
3000/*
3001 * Give some info about the position of the cursor (for "g CTRL-G").
3002 * In Visual mode, give some info about the selected region. (In this case,
3003 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003004 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 */
3006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003007cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003010 char_u buf1[50];
3011 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003013 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003014 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003015 varnumber_T byte_count_cursor = 0;
3016 varnumber_T char_count = 0;
3017 varnumber_T char_count_cursor = 0;
3018 varnumber_T word_count = 0;
3019 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003020 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003021 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 long line_count_selected = 0;
3023 pos_T min_pos, max_pos;
3024 oparg_T oparg;
3025 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027 /*
3028 * Compute the length of the file in characters.
3029 */
3030 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3031 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003032 if (dict == NULL)
3033 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003034 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003035 return;
3036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
3038 else
3039 {
3040 if (get_fileformat(curbuf) == EOL_DOS)
3041 eol_size = 2;
3042 else
3043 eol_size = 1;
3044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (VIsual_active)
3046 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003047 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 {
3049 min_pos = VIsual;
3050 max_pos = curwin->w_cursor;
3051 }
3052 else
3053 {
3054 min_pos = curwin->w_cursor;
3055 max_pos = VIsual;
3056 }
3057 if (*p_sel == 'e' && max_pos.col > 0)
3058 --max_pos.col;
3059
3060 if (VIsual_mode == Ctrl_V)
3061 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003062#ifdef FEAT_LINEBREAK
3063 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003064 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003066 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003067 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003068 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 oparg.is_VIsual = 1;
3071 oparg.block_mode = TRUE;
3072 oparg.op_type = OP_NOP;
3073 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003074 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003075#ifdef FEAT_LINEBREAK
3076 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003077 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003078#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003079 if (curwin->w_curswant == MAXCOL)
3080 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (oparg.end_vcol < oparg.start_vcol)
3083 {
3084 oparg.end_vcol += oparg.start_vcol;
3085 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3086 oparg.end_vcol -= oparg.start_vcol;
3087 }
3088 }
3089 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003095 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
3097 ui_breakcheck();
3098 if (got_int)
3099 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003100 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
3102
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003103 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (VIsual_active
3105 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3106 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003107 char_u *s = NULL;
3108 long len = 0L;
3109
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 switch (VIsual_mode)
3111 {
3112 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003116 s = bd.textstart;
3117 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 break;
3119 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003120 s = ml_get(lnum);
3121 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 break;
3123 case 'v':
3124 {
3125 colnr_T start_col = (lnum == min_pos.lnum)
3126 ? min_pos.col : 0;
3127 colnr_T end_col = (lnum == max_pos.lnum)
3128 ? max_pos.col - start_col + 1 : MAXCOL;
3129
Bram Moolenaardef9e822004-12-31 20:58:58 +00003130 s = ml_get(lnum) + start_col;
3131 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 }
3133 break;
3134 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003135 if (s != NULL)
3136 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003137 byte_count_cursor += line_count_info(s, &word_count_cursor,
3138 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003139 if (lnum == curbuf->b_ml.ml_line_count
3140 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003141 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003142 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003143 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
3146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003148 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (lnum == curwin->w_cursor.lnum)
3150 {
3151 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003152 char_count_cursor += char_count;
3153 byte_count_cursor = byte_count +
3154 line_count_info(ml_get(lnum),
3155 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003156 (varnumber_T)(curwin->w_cursor.col + 1),
3157 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003161 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003162 &char_count, (varnumber_T)MAXCOL,
3163 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
3165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003166 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003167 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003168 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Bram Moolenaared767a22016-01-03 22:49:16 +01003170 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003172 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003174 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3175 {
3176 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3177 &max_pos.col);
3178 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3179 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3180 }
3181 else
3182 buf1[0] = NUL;
3183
3184 if (char_count_cursor == byte_count_cursor
3185 && char_count == byte_count)
3186 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003187 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003188 buf1, line_count_selected,
3189 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003190 (varnumber_T)word_count_cursor,
3191 (varnumber_T)word_count,
3192 (varnumber_T)byte_count_cursor,
3193 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003194 else
3195 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003196 _("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 +01003197 buf1, line_count_selected,
3198 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003199 (varnumber_T)word_count_cursor,
3200 (varnumber_T)word_count,
3201 (varnumber_T)char_count_cursor,
3202 (varnumber_T)char_count,
3203 (varnumber_T)byte_count_cursor,
3204 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003207 {
3208 p = ml_get_curline();
3209 validate_virtcol();
3210 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3211 (int)curwin->w_virtcol + 1);
3212 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3213 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaared767a22016-01-03 22:49:16 +01003215 if (char_count_cursor == byte_count_cursor
3216 && char_count == byte_count)
3217 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003218 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003219 (char *)buf1, (char *)buf2,
3220 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003222 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3223 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003224 else
3225 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003226 _("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 +01003227 (char *)buf1, (char *)buf2,
3228 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003229 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003230 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3231 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3232 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003233 }
3234 }
3235
Bram Moolenaared767a22016-01-03 22:49:16 +01003236 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003237 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003238 {
3239 size_t len = STRLEN(IObuff);
3240
3241 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003242 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003243 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003244 if (dict == NULL)
3245 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003246 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003247 p = p_shm;
3248 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003249 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003250 p_shm = p;
3251 }
3252 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003253#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003254 if (dict != NULL)
3255 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003256 dict_add_number(dict, "words", word_count);
3257 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003258 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003259 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3260 byte_count_cursor);
3261 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3262 char_count_cursor);
3263 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3264 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003268
3269/*
3270 * Handle indent and format operators and visual mode ":".
3271 */
3272 static void
3273op_colon(oparg_T *oap)
3274{
3275 stuffcharReadbuff(':');
3276 if (oap->is_VIsual)
3277 stuffReadbuff((char_u *)"'<,'>");
3278 else
3279 {
3280 // Make the range look nice, so it can be repeated.
3281 if (oap->start.lnum == curwin->w_cursor.lnum)
3282 stuffcharReadbuff('.');
3283 else
3284 stuffnumReadbuff((long)oap->start.lnum);
3285 if (oap->end.lnum != oap->start.lnum)
3286 {
3287 stuffcharReadbuff(',');
3288 if (oap->end.lnum == curwin->w_cursor.lnum)
3289 stuffcharReadbuff('.');
3290 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3291 stuffcharReadbuff('$');
3292 else if (oap->start.lnum == curwin->w_cursor.lnum)
3293 {
3294 stuffReadbuff((char_u *)".+");
3295 stuffnumReadbuff((long)oap->line_count - 1);
3296 }
3297 else
3298 stuffnumReadbuff((long)oap->end.lnum);
3299 }
3300 }
3301 if (oap->op_type != OP_COLON)
3302 stuffReadbuff((char_u *)"!");
3303 if (oap->op_type == OP_INDENT)
3304 {
3305#ifndef FEAT_CINDENT
3306 if (*get_equalprg() == NUL)
3307 stuffReadbuff((char_u *)"indent");
3308 else
3309#endif
3310 stuffReadbuff(get_equalprg());
3311 stuffReadbuff((char_u *)"\n");
3312 }
3313 else if (oap->op_type == OP_FORMAT)
3314 {
3315 if (*curbuf->b_p_fp != NUL)
3316 stuffReadbuff(curbuf->b_p_fp);
3317 else if (*p_fp != NUL)
3318 stuffReadbuff(p_fp);
3319 else
3320 stuffReadbuff((char_u *)"fmt");
3321 stuffReadbuff((char_u *)"\n']");
3322 }
3323
3324 // do_cmdline() does the rest
3325}
3326
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003327// callback function for 'operatorfunc'
3328static callback_T opfunc_cb;
3329
3330/*
3331 * Process the 'operatorfunc' option value.
3332 * Returns OK or FAIL.
3333 */
3334 int
3335set_operatorfunc_option(void)
3336{
3337 return option_set_callback_func(p_opfunc, &opfunc_cb);
3338}
3339
3340#if defined(EXITFREE) || defined(PROTO)
3341 void
3342free_operatorfunc_option(void)
3343{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003344# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003345 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003346# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003347}
3348#endif
3349
Dominique Pelle748b3082022-01-08 12:41:16 +00003350#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003351/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003352 * Mark the global 'operatorfunc' callback with 'copyID' so that it is not
3353 * garbage collected.
3354 */
3355 int
3356set_ref_in_opfunc(int copyID UNUSED)
3357{
3358 int abort = FALSE;
3359
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003360 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003361
3362 return abort;
3363}
Dominique Pelle748b3082022-01-08 12:41:16 +00003364#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003365
3366/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003367 * Handle the "g@" operator: call 'operatorfunc'.
3368 */
3369 static void
3370op_function(oparg_T *oap UNUSED)
3371{
3372#ifdef FEAT_EVAL
3373 typval_T argv[2];
3374 int save_virtual_op = virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003375 int save_finish_op = finish_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003376 pos_T orig_start = curbuf->b_op_start;
3377 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003378 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003379
3380 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003381 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003382 else
3383 {
3384 // Set '[ and '] marks to text to be operated on.
3385 curbuf->b_op_start = oap->start;
3386 curbuf->b_op_end = oap->end;
3387 if (oap->motion_type != MLINE && !oap->inclusive)
3388 // Exclude the end position.
3389 decl(&curbuf->b_op_end);
3390
3391 argv[0].v_type = VAR_STRING;
3392 if (oap->block_mode)
3393 argv[0].vval.v_string = (char_u *)"block";
3394 else if (oap->motion_type == MLINE)
3395 argv[0].vval.v_string = (char_u *)"line";
3396 else
3397 argv[0].vval.v_string = (char_u *)"char";
3398 argv[1].v_type = VAR_UNKNOWN;
3399
3400 // Reset virtual_op so that 'virtualedit' can be changed in the
3401 // function.
3402 virtual_op = MAYBE;
3403
naohiro ono75c30e92021-10-19 11:15:41 +01003404 // Reset finish_op so that mode() returns the right value.
3405 finish_op = FALSE;
3406
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003407 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3408 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003409
3410 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003411 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003412 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003413 {
3414 curbuf->b_op_start = orig_start;
3415 curbuf->b_op_end = orig_end;
3416 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003417 }
3418#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003419 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003420#endif
3421}
3422
3423/*
3424 * Calculate start/end virtual columns for operating in block mode.
3425 */
3426 static void
3427get_op_vcol(
3428 oparg_T *oap,
3429 colnr_T redo_VIsual_vcol,
3430 int initial) // when TRUE adjust position for 'selectmode'
3431{
3432 colnr_T start, end;
3433
3434 if (VIsual_mode != Ctrl_V
3435 || (!initial && oap->end.col < curwin->w_width))
3436 return;
3437
3438 oap->block_mode = TRUE;
3439
3440 // prevent from moving onto a trail byte
3441 if (has_mbyte)
3442 mb_adjustpos(curwin->w_buffer, &oap->end);
3443
3444 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3445
3446 if (!redo_VIsual_busy)
3447 {
3448 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3449
3450 if (start < oap->start_vcol)
3451 oap->start_vcol = start;
3452 if (end > oap->end_vcol)
3453 {
3454 if (initial && *p_sel == 'e' && start >= 1
3455 && start - 1 >= oap->end_vcol)
3456 oap->end_vcol = start - 1;
3457 else
3458 oap->end_vcol = end;
3459 }
3460 }
3461
3462 // if '$' was used, get oap->end_vcol from longest line
3463 if (curwin->w_curswant == MAXCOL)
3464 {
3465 curwin->w_cursor.col = MAXCOL;
3466 oap->end_vcol = 0;
3467 for (curwin->w_cursor.lnum = oap->start.lnum;
3468 curwin->w_cursor.lnum <= oap->end.lnum;
3469 ++curwin->w_cursor.lnum)
3470 {
3471 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3472 if (end > oap->end_vcol)
3473 oap->end_vcol = end;
3474 }
3475 }
3476 else if (redo_VIsual_busy)
3477 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3478 // Correct oap->end.col and oap->start.col to be the
3479 // upper-left and lower-right corner of the block area.
3480 //
3481 // (Actually, this does convert column positions into character
3482 // positions)
3483 curwin->w_cursor.lnum = oap->end.lnum;
3484 coladvance(oap->end_vcol);
3485 oap->end = curwin->w_cursor;
3486
3487 curwin->w_cursor = oap->start;
3488 coladvance(oap->start_vcol);
3489 oap->start = curwin->w_cursor;
3490}
3491
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003492// Information for redoing the previous Visual selection.
3493typedef struct {
3494 int rv_mode; // 'v', 'V', or Ctrl-V
3495 linenr_T rv_line_count; // number of lines
3496 colnr_T rv_vcol; // number of cols or end column
3497 long rv_count; // count for Visual operator
3498 int rv_arg; // extra argument
3499} redo_VIsual_T;
3500
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003501 static int
3502is_ex_cmdchar(cmdarg_T *cap)
3503{
3504 return cap->cmdchar == ':'
3505 || cap->cmdchar == K_COMMAND
3506 || cap->cmdchar == K_SCRIPT_COMMAND;
3507}
3508
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003509/*
3510 * Handle an operator after Visual mode or when the movement is finished.
3511 * "gui_yank" is true when yanking text for the clipboard.
3512 */
3513 void
3514do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3515{
3516 oparg_T *oap = cap->oap;
3517 pos_T old_cursor;
3518 int empty_region_error;
3519 int restart_edit_save;
3520#ifdef FEAT_LINEBREAK
3521 int lbr_saved = curwin->w_p_lbr;
3522#endif
3523
3524 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003525 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3526
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003527 int include_line_break = FALSE;
3528
3529#if defined(FEAT_CLIPBOARD)
3530 // Yank the visual area into the GUI selection register before we operate
3531 // on it and lose it forever.
3532 // Don't do it if a specific register was specified, so that ""x"*P works.
3533 // This could call do_pending_operator() recursively, but that's OK
3534 // because gui_yank will be TRUE for the nested call.
3535 if ((clip_star.available || clip_plus.available)
3536 && oap->op_type != OP_NOP
3537 && !gui_yank
3538 && VIsual_active
3539 && !redo_VIsual_busy
3540 && oap->regname == 0)
3541 clip_auto_select();
3542#endif
3543 old_cursor = curwin->w_cursor;
3544
3545 // If an operation is pending, handle it...
3546 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3547 {
3548 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3549 // for the clipboard.
3550 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3551
3552#ifdef FEAT_LINEBREAK
3553 // Avoid a problem with unwanted linebreaks in block mode.
3554 if (curwin->w_p_lbr)
3555 curwin->w_valid &= ~VALID_VIRTCOL;
3556 curwin->w_p_lbr = FALSE;
3557#endif
3558 oap->is_VIsual = VIsual_active;
3559 if (oap->motion_force == 'V')
3560 oap->motion_type = MLINE;
3561 else if (oap->motion_force == 'v')
3562 {
3563 // If the motion was linewise, "inclusive" will not have been set.
3564 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3565 if (oap->motion_type == MLINE)
3566 oap->inclusive = FALSE;
3567 // If the motion already was characterwise, toggle "inclusive"
3568 else if (oap->motion_type == MCHAR)
3569 oap->inclusive = !oap->inclusive;
3570 oap->motion_type = MCHAR;
3571 }
3572 else if (oap->motion_force == Ctrl_V)
3573 {
3574 // Change line- or characterwise motion into Visual block mode.
3575 if (!VIsual_active)
3576 {
3577 VIsual_active = TRUE;
3578 VIsual = oap->start;
3579 }
3580 VIsual_mode = Ctrl_V;
3581 VIsual_select = FALSE;
3582 VIsual_reselect = FALSE;
3583 }
3584
3585 // Only redo yank when 'y' flag is in 'cpoptions'.
3586 // Never redo "zf" (define fold).
3587 if ((redo_yank || oap->op_type != OP_YANK)
3588 && ((!VIsual_active || oap->motion_force)
3589 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003590 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003591 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003592 && cap->cmdchar != 'D'
3593#ifdef FEAT_FOLDING
3594 && oap->op_type != OP_FOLD
3595 && oap->op_type != OP_FOLDOPEN
3596 && oap->op_type != OP_FOLDOPENREC
3597 && oap->op_type != OP_FOLDCLOSE
3598 && oap->op_type != OP_FOLDCLOSEREC
3599 && oap->op_type != OP_FOLDDEL
3600 && oap->op_type != OP_FOLDDELREC
3601#endif
3602 )
3603 {
3604 prep_redo(oap->regname, cap->count0,
3605 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3606 oap->motion_force, cap->cmdchar, cap->nchar);
3607 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3608 {
3609 // If 'cpoptions' does not contain 'r', insert the search
3610 // pattern to really repeat the same command.
3611 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3612 AppendToRedobuffLit(cap->searchbuf, -1);
3613 AppendToRedobuff(NL_STR);
3614 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003615 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003616 {
3617 // do_cmdline() has stored the first typed line in
3618 // "repeat_cmdline". When several lines are typed repeating
3619 // won't be possible.
3620 if (repeat_cmdline == NULL)
3621 ResetRedobuff();
3622 else
3623 {
3624 AppendToRedobuffLit(repeat_cmdline, -1);
3625 AppendToRedobuff(NL_STR);
3626 VIM_CLEAR(repeat_cmdline);
3627 }
3628 }
3629 }
3630
3631 if (redo_VIsual_busy)
3632 {
3633 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003634 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003635 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003636 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003637 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3638 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003639 VIsual_mode = redo_VIsual.rv_mode;
3640 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003641 {
3642 if (VIsual_mode == 'v')
3643 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003644 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003645 {
3646 validate_virtcol();
3647 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003648 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003649 }
3650 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003651 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003652 }
3653 else
3654 {
3655 curwin->w_curswant = MAXCOL;
3656 }
3657 coladvance(curwin->w_curswant);
3658 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003659 cap->count0 = redo_VIsual.rv_count;
3660 if (redo_VIsual.rv_count != 0)
3661 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003662 else
3663 cap->count1 = 1;
3664 }
3665 else if (VIsual_active)
3666 {
3667 if (!gui_yank)
3668 {
3669 // Save the current VIsual area for '< and '> marks, and "gv"
3670 curbuf->b_visual.vi_start = VIsual;
3671 curbuf->b_visual.vi_end = curwin->w_cursor;
3672 curbuf->b_visual.vi_mode = VIsual_mode;
3673 restore_visual_mode();
3674 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3675# ifdef FEAT_EVAL
3676 curbuf->b_visual_mode_eval = VIsual_mode;
3677# endif
3678 }
3679
3680 // In Select mode, a linewise selection is operated upon like a
3681 // characterwise selection.
3682 // Special case: gH<Del> deletes the last line.
3683 if (VIsual_select && VIsual_mode == 'V'
3684 && cap->oap->op_type != OP_DELETE)
3685 {
3686 if (LT_POS(VIsual, curwin->w_cursor))
3687 {
3688 VIsual.col = 0;
3689 curwin->w_cursor.col =
3690 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3691 }
3692 else
3693 {
3694 curwin->w_cursor.col = 0;
3695 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3696 }
3697 VIsual_mode = 'v';
3698 }
3699 // If 'selection' is "exclusive", backup one character for
3700 // charwise selections.
3701 else if (VIsual_mode == 'v')
3702 include_line_break = unadjust_for_sel();
3703
3704 oap->start = VIsual;
3705 if (VIsual_mode == 'V')
3706 {
3707 oap->start.col = 0;
3708 oap->start.coladd = 0;
3709 }
3710 }
3711
3712 // Set oap->start to the first position of the operated text, oap->end
3713 // to the end of the operated text. w_cursor is equal to oap->start.
3714 if (LT_POS(oap->start, curwin->w_cursor))
3715 {
3716#ifdef FEAT_FOLDING
3717 // Include folded lines completely.
3718 if (!VIsual_active)
3719 {
3720 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3721 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003722 if ((curwin->w_cursor.col > 0 || oap->inclusive
3723 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003724 && hasFolding(curwin->w_cursor.lnum, NULL,
3725 &curwin->w_cursor.lnum))
3726 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3727 }
3728#endif
3729 oap->end = curwin->w_cursor;
3730 curwin->w_cursor = oap->start;
3731
3732 // w_virtcol may have been updated; if the cursor goes back to its
3733 // previous position w_virtcol becomes invalid and isn't updated
3734 // automatically.
3735 curwin->w_valid &= ~VALID_VIRTCOL;
3736 }
3737 else
3738 {
3739#ifdef FEAT_FOLDING
3740 // Include folded lines completely.
3741 if (!VIsual_active && oap->motion_type == MLINE)
3742 {
3743 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3744 NULL))
3745 curwin->w_cursor.col = 0;
3746 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3747 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3748 }
3749#endif
3750 oap->end = oap->start;
3751 oap->start = curwin->w_cursor;
3752 }
3753
3754 // Just in case lines were deleted that make the position invalid.
3755 check_pos(curwin->w_buffer, &oap->end);
3756 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3757
3758 // Set "virtual_op" before resetting VIsual_active.
3759 virtual_op = virtual_active();
3760
3761 if (VIsual_active || redo_VIsual_busy)
3762 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003763 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003764
3765 if (!redo_VIsual_busy && !gui_yank)
3766 {
3767 // Prepare to reselect and redo Visual: this is based on the
3768 // size of the Visual text
3769 resel_VIsual_mode = VIsual_mode;
3770 if (curwin->w_curswant == MAXCOL)
3771 resel_VIsual_vcol = MAXCOL;
3772 else
3773 {
3774 if (VIsual_mode != Ctrl_V)
3775 getvvcol(curwin, &(oap->end),
3776 NULL, NULL, &oap->end_vcol);
3777 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3778 {
3779 if (VIsual_mode != Ctrl_V)
3780 getvvcol(curwin, &(oap->start),
3781 &oap->start_vcol, NULL, NULL);
3782 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3783 }
3784 else
3785 resel_VIsual_vcol = oap->end_vcol;
3786 }
3787 resel_VIsual_line_count = oap->line_count;
3788 }
3789
3790 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3791 if ((redo_yank || oap->op_type != OP_YANK)
3792 && oap->op_type != OP_COLON
3793#ifdef FEAT_FOLDING
3794 && oap->op_type != OP_FOLD
3795 && oap->op_type != OP_FOLDOPEN
3796 && oap->op_type != OP_FOLDOPENREC
3797 && oap->op_type != OP_FOLDCLOSE
3798 && oap->op_type != OP_FOLDCLOSEREC
3799 && oap->op_type != OP_FOLDDEL
3800 && oap->op_type != OP_FOLDDELREC
3801#endif
3802 && oap->motion_force == NUL
3803 )
3804 {
3805 // Prepare for redoing. Only use the nchar field for "r",
3806 // otherwise it might be the second char of the operator.
3807 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3808 || cap->nchar == 'N'))
3809 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003810 get_op_char(oap->op_type),
3811 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003812 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003813 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003814 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003815 int opchar = get_op_char(oap->op_type);
3816 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003817 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3818
3819 // reverse what nv_replace() did
3820 if (nchar == REPLACE_CR_NCHAR)
3821 nchar = CAR;
3822 else if (nchar == REPLACE_NL_NCHAR)
3823 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003824
3825 if (opchar == 'g' && extra_opchar == '@')
3826 // also repeat the count for 'operatorfunc'
3827 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3828 cap->count0, opchar, extra_opchar, nchar);
3829 else
3830 prep_redo(oap->regname, 0L, NUL, 'v',
3831 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003832 }
3833 if (!redo_VIsual_busy)
3834 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003835 redo_VIsual.rv_mode = resel_VIsual_mode;
3836 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3837 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3838 redo_VIsual.rv_count = cap->count0;
3839 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003840 }
3841 }
3842
3843 // oap->inclusive defaults to TRUE.
3844 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3845 // FALSE. This makes "d}P" and "v}dP" work the same.
3846 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3847 oap->inclusive = TRUE;
3848 if (VIsual_mode == 'V')
3849 oap->motion_type = MLINE;
3850 else
3851 {
3852 oap->motion_type = MCHAR;
3853 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3854 && (include_line_break || !virtual_op))
3855 {
3856 oap->inclusive = FALSE;
3857 // Try to include the newline, unless it's an operator
3858 // that works on lines only.
3859 if (*p_sel != 'o'
3860 && !op_on_lines(oap->op_type)
3861 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3862 {
3863 ++oap->end.lnum;
3864 oap->end.col = 0;
3865 oap->end.coladd = 0;
3866 ++oap->line_count;
3867 }
3868 }
3869 }
3870
3871 redo_VIsual_busy = FALSE;
3872
3873 // Switch Visual off now, so screen updating does
3874 // not show inverted text when the screen is redrawn.
3875 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3876 // no screen redraw, so it is done here to remove the inverted
3877 // part.
3878 if (!gui_yank)
3879 {
3880 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003881 setmouse();
3882 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003883 may_clear_cmdline();
3884 if ((oap->op_type == OP_YANK
3885 || oap->op_type == OP_COLON
3886 || oap->op_type == OP_FUNCTION
3887 || oap->op_type == OP_FILTER)
3888 && oap->motion_force == NUL)
3889 {
3890#ifdef FEAT_LINEBREAK
3891 // make sure redrawing is correct
3892 curwin->w_p_lbr = lbr_saved;
3893#endif
3894 redraw_curbuf_later(INVERTED);
3895 }
3896 }
3897 }
3898
3899 // Include the trailing byte of a multi-byte char.
3900 if (has_mbyte && oap->inclusive)
3901 {
3902 int l;
3903
3904 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3905 if (l > 1)
3906 oap->end.col += l - 1;
3907 }
3908 curwin->w_set_curswant = TRUE;
3909
3910 // oap->empty is set when start and end are the same. The inclusive
3911 // flag affects this too, unless yanking and the end is on a NUL.
3912 oap->empty = (oap->motion_type == MCHAR
3913 && (!oap->inclusive
3914 || (oap->op_type == OP_YANK
3915 && gchar_pos(&oap->end) == NUL))
3916 && EQUAL_POS(oap->start, oap->end)
3917 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3918 // For delete, change and yank, it's an error to operate on an
3919 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3920 empty_region_error = (oap->empty
3921 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3922
3923 // Force a redraw when operating on an empty Visual region, when
3924 // 'modifiable is off or creating a fold.
3925 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3926#ifdef FEAT_FOLDING
3927 || oap->op_type == OP_FOLD
3928#endif
3929 ))
3930 {
3931#ifdef FEAT_LINEBREAK
3932 curwin->w_p_lbr = lbr_saved;
3933#endif
3934 redraw_curbuf_later(INVERTED);
3935 }
3936
3937 // If the end of an operator is in column one while oap->motion_type
3938 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3939 // character in the previous line. If op_start is on or before the
3940 // first non-blank in the line, the operator becomes linewise
3941 // (strange, but that's the way vi does it).
3942 if ( oap->motion_type == MCHAR
3943 && oap->inclusive == FALSE
3944 && !(cap->retval & CA_NO_ADJ_OP_END)
3945 && oap->end.col == 0
3946 && (!oap->is_VIsual || *p_sel == 'o')
3947 && !oap->block_mode
3948 && oap->line_count > 1)
3949 {
3950 oap->end_adjusted = TRUE; // remember that we did this
3951 --oap->line_count;
3952 --oap->end.lnum;
3953 if (inindent(0))
3954 oap->motion_type = MLINE;
3955 else
3956 {
3957 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3958 if (oap->end.col)
3959 {
3960 --oap->end.col;
3961 oap->inclusive = TRUE;
3962 }
3963 }
3964 }
3965 else
3966 oap->end_adjusted = FALSE;
3967
3968 switch (oap->op_type)
3969 {
3970 case OP_LSHIFT:
3971 case OP_RSHIFT:
3972 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3973 auto_format(FALSE, TRUE);
3974 break;
3975
3976 case OP_JOIN_NS:
3977 case OP_JOIN:
3978 if (oap->line_count < 2)
3979 oap->line_count = 2;
3980 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3981 curbuf->b_ml.ml_line_count)
3982 beep_flush();
3983 else
3984 {
3985 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3986 TRUE, TRUE, TRUE);
3987 auto_format(FALSE, TRUE);
3988 }
3989 break;
3990
3991 case OP_DELETE:
3992 VIsual_reselect = FALSE; // don't reselect now
3993 if (empty_region_error)
3994 {
3995 vim_beep(BO_OPER);
3996 CancelRedo();
3997 }
3998 else
3999 {
4000 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004001 // save cursor line for undo if it wasn't saved yet
4002 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4003 && u_save_cursor() == OK)
4004 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004005 }
4006 break;
4007
4008 case OP_YANK:
4009 if (empty_region_error)
4010 {
4011 if (!gui_yank)
4012 {
4013 vim_beep(BO_OPER);
4014 CancelRedo();
4015 }
4016 }
4017 else
4018 {
4019#ifdef FEAT_LINEBREAK
4020 curwin->w_p_lbr = lbr_saved;
4021#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004022 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004023 (void)op_yank(oap, FALSE, !gui_yank);
4024 }
4025 check_cursor_col();
4026 break;
4027
4028 case OP_CHANGE:
4029 VIsual_reselect = FALSE; // don't reselect now
4030 if (empty_region_error)
4031 {
4032 vim_beep(BO_OPER);
4033 CancelRedo();
4034 }
4035 else
4036 {
4037 // This is a new edit command, not a restart. Need to
4038 // remember it to make 'insertmode' work with mappings for
4039 // Visual mode. But do this only once and not when typed and
4040 // 'insertmode' isn't set.
4041 if (p_im || !KeyTyped)
4042 restart_edit_save = restart_edit;
4043 else
4044 restart_edit_save = 0;
4045 restart_edit = 0;
4046#ifdef FEAT_LINEBREAK
4047 // Restore linebreak, so that when the user edits it looks as
4048 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004049 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004050#endif
4051 // Reset finish_op now, don't want it set inside edit().
4052 finish_op = FALSE;
4053 if (op_change(oap)) // will call edit()
4054 cap->retval |= CA_COMMAND_BUSY;
4055 if (restart_edit == 0)
4056 restart_edit = restart_edit_save;
4057 }
4058 break;
4059
4060 case OP_FILTER:
4061 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4062 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4063 else
4064 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4065 // FALLTHROUGH
4066
4067 case OP_INDENT:
4068 case OP_COLON:
4069
4070#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4071 // If 'equalprg' is empty, do the indenting internally.
4072 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4073 {
4074# ifdef FEAT_LISP
4075 if (curbuf->b_p_lisp)
4076 {
4077 op_reindent(oap, get_lisp_indent);
4078 break;
4079 }
4080# endif
4081# ifdef FEAT_CINDENT
4082 op_reindent(oap,
4083# ifdef FEAT_EVAL
4084 *curbuf->b_p_inde != NUL ? get_expr_indent :
4085# endif
4086 get_c_indent);
4087 break;
4088# endif
4089 }
4090#endif
4091
4092 op_colon(oap);
4093 break;
4094
4095 case OP_TILDE:
4096 case OP_UPPER:
4097 case OP_LOWER:
4098 case OP_ROT13:
4099 if (empty_region_error)
4100 {
4101 vim_beep(BO_OPER);
4102 CancelRedo();
4103 }
4104 else
4105 op_tilde(oap);
4106 check_cursor_col();
4107 break;
4108
4109 case OP_FORMAT:
4110#if defined(FEAT_EVAL)
4111 if (*curbuf->b_p_fex != NUL)
4112 op_formatexpr(oap); // use expression
4113 else
4114#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004115 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004116 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004117 op_colon(oap); // use external command
4118 else
4119 op_format(oap, FALSE); // use internal function
4120 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004121 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004122 case OP_FORMAT2:
4123 op_format(oap, TRUE); // use internal function
4124 break;
4125
4126 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004127 {
4128 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4129
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004130#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004131 // Restore linebreak, so that when the user edits it looks as
4132 // before.
4133 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004134#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004135 // call 'operatorfunc'
4136 op_function(oap);
4137
4138 // Restore the info for redoing Visual mode, the function may
4139 // invoke another operator and unintentionally change it.
4140 redo_VIsual = save_redo_VIsual;
4141 break;
4142 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004143
4144 case OP_INSERT:
4145 case OP_APPEND:
4146 VIsual_reselect = FALSE; // don't reselect now
4147 if (empty_region_error)
4148 {
4149 vim_beep(BO_OPER);
4150 CancelRedo();
4151 }
4152 else
4153 {
4154 // This is a new edit command, not a restart. Need to
4155 // remember it to make 'insertmode' work with mappings for
4156 // Visual mode. But do this only once.
4157 restart_edit_save = restart_edit;
4158 restart_edit = 0;
4159#ifdef FEAT_LINEBREAK
4160 // Restore linebreak, so that when the user edits it looks as
4161 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004162 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004163#endif
4164 op_insert(oap, cap->count1);
4165#ifdef FEAT_LINEBREAK
4166 // Reset linebreak, so that formatting works correctly.
4167 curwin->w_p_lbr = FALSE;
4168#endif
4169
4170 // TODO: when inserting in several lines, should format all
4171 // the lines.
4172 auto_format(FALSE, TRUE);
4173
4174 if (restart_edit == 0)
4175 restart_edit = restart_edit_save;
4176 else
4177 cap->retval |= CA_COMMAND_BUSY;
4178 }
4179 break;
4180
4181 case OP_REPLACE:
4182 VIsual_reselect = FALSE; // don't reselect now
4183 if (empty_region_error)
4184 {
4185 vim_beep(BO_OPER);
4186 CancelRedo();
4187 }
4188 else
4189 {
4190#ifdef FEAT_LINEBREAK
4191 // Restore linebreak, so that when the user edits it looks as
4192 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004193 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004194#endif
4195 op_replace(oap, cap->nchar);
4196 }
4197 break;
4198
4199#ifdef FEAT_FOLDING
4200 case OP_FOLD:
4201 VIsual_reselect = FALSE; // don't reselect now
4202 foldCreate(oap->start.lnum, oap->end.lnum);
4203 break;
4204
4205 case OP_FOLDOPEN:
4206 case OP_FOLDOPENREC:
4207 case OP_FOLDCLOSE:
4208 case OP_FOLDCLOSEREC:
4209 VIsual_reselect = FALSE; // don't reselect now
4210 opFoldRange(oap->start.lnum, oap->end.lnum,
4211 oap->op_type == OP_FOLDOPEN
4212 || oap->op_type == OP_FOLDOPENREC,
4213 oap->op_type == OP_FOLDOPENREC
4214 || oap->op_type == OP_FOLDCLOSEREC,
4215 oap->is_VIsual);
4216 break;
4217
4218 case OP_FOLDDEL:
4219 case OP_FOLDDELREC:
4220 VIsual_reselect = FALSE; // don't reselect now
4221 deleteFold(oap->start.lnum, oap->end.lnum,
4222 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4223 break;
4224#endif
4225 case OP_NR_ADD:
4226 case OP_NR_SUB:
4227 if (empty_region_error)
4228 {
4229 vim_beep(BO_OPER);
4230 CancelRedo();
4231 }
4232 else
4233 {
4234 VIsual_active = TRUE;
4235#ifdef FEAT_LINEBREAK
4236 curwin->w_p_lbr = lbr_saved;
4237#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004238 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004239 VIsual_active = FALSE;
4240 }
4241 check_cursor_col();
4242 break;
4243 default:
4244 clearopbeep(oap);
4245 }
4246 virtual_op = MAYBE;
4247 if (!gui_yank)
4248 {
4249 // if 'sol' not set, go back to old column for some commands
4250 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4251 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4252 || oap->op_type == OP_DELETE))
4253 {
4254#ifdef FEAT_LINEBREAK
4255 curwin->w_p_lbr = FALSE;
4256#endif
4257 coladvance(curwin->w_curswant = old_col);
4258 }
4259 }
4260 else
4261 {
4262 curwin->w_cursor = old_cursor;
4263 }
4264 oap->block_mode = FALSE;
4265 clearop(oap);
4266 motion_force = NUL;
4267 }
4268#ifdef FEAT_LINEBREAK
4269 curwin->w_p_lbr = lbr_saved;
4270#endif
4271}