blob: 33fedba05a47f003abbcbf2ccd2f24aa2786903f [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 (first_char != '#' || !preprocs_left())
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100163 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 ++curwin->w_cursor.lnum;
165 }
166
167 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (oap->block_mode)
169 {
170 curwin->w_cursor.lnum = oap->start.lnum;
171 curwin->w_cursor.col = block_col;
172 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100173 else if (curs_top) // put cursor on first line, for ">>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 {
175 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100176 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 }
178 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100179 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100181#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100182 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183 foldOpenCursor();
184#endif
185
186
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (oap->line_count > p_report)
188 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200189 char *op;
190 char *msg_line_single;
191 char *msg_line_plural;
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200194 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200196 op = "<";
197 msg_line_single = NGETTEXT("%ld line %sed %d time",
198 "%ld line %sed %d times", amount);
199 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
200 "%ld lines %sed %d times", amount);
201 vim_snprintf((char *)IObuff, IOSIZE,
202 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
203 oap->line_count, op, amount);
Bram Moolenaare4fc7462020-03-15 19:17:50 +0100204 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 }
206
Bram Moolenaare1004402020-10-24 20:49:43 +0200207 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100208 {
209 // Set "'[" and "']" marks.
210 curbuf->b_op_start = oap->start;
211 curbuf->b_op_end.lnum = oap->end.lnum;
212 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
213 if (curbuf->b_op_end.col > 0)
214 --curbuf->b_op_end.col;
215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216}
217
218/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100219 * Shift the current line one shiftwidth left (if left != 0) or right
220 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 */
222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100223shift_line(
224 int left,
225 int round,
226 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200227 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 int count;
230 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200231 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200233 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200235 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200237 i = count / sw_val; // number of 'shiftwidth' rounded down
238 j = count % sw_val; // extra spaces
239 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 --amount;
241 if (left)
242 {
243 i -= amount;
244 if (i < 0)
245 i = 0;
246 }
247 else
248 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200249 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200251 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {
253 if (left)
254 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200255 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 if (count < 0)
257 count = 0;
258 }
259 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200260 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200263 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000265 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000267 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268}
269
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270/*
271 * Shift one line of the current block one shiftwidth right or left.
272 * Leaves cursor on first character in block.
273 */
274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100275shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
277 int left = (oap->op_type == OP_LSHIFT);
278 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000279 int total;
280 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200282 int sw_val = (int)get_sw_value_indent(curbuf);
283 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000286 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100287 int added;
288 unsigned new_line_len; // the length of the line after the
289 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#ifdef FEAT_RIGHTLEFT
291 int old_p_ri = p_ri;
292
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100293 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#endif
295
Bram Moolenaar24959102022-05-07 20:01:16 +0100296 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
298 if (bd.is_short)
299 return;
300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100301 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 total = (int)((unsigned)amount * (unsigned)sw_val);
303 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 oldp = ml_get_curline();
307
308 if (!left)
309 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100310 int tabs = 0, spaces = 0;
311 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100312
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 /*
314 * 1. Get start vcol
315 * 2. Total ws vcols
316 * 3. Divvy into TABs & spp
317 * 4. Construct new string
318 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100319 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 ws_vcol = bd.start_vcol - bd.pre_whitesp;
321 if (bd.startspaces)
322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100324 {
325 if ((*mb_ptr2len)(bd.textstart) == 1)
326 ++bd.textstart;
327 else
328 {
329 ws_vcol = 0;
330 bd.startspaces = 0;
331 }
332 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000333 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000334 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100336
337 // TODO: is passing bd.textstart for start of the line OK?
338 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
339 bd.start_vcol, bd.textstart, bd.textstart);
340 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100342 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100344 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100346 bd.textstart = cts.cts_ptr;
347 bd.start_vcol = cts.cts_vcol;
348 clear_chartabsize_arg(&cts);
349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100350 // OK, now total=all the VWS reqd, and textstart points at the 1st
351 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200352#ifdef FEAT_VARTABS
353 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200354 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100355 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200356 else
LemonBoy4b936742022-05-13 21:56:28 +0100357 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200358#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100360 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
361 if (tabs > 0)
362 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 else
LemonBoy4b936742022-05-13 21:56:28 +0100364 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200365#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100366 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100368
369 new_line_len = bd.textcol + tabs + spaces + (int)STRLEN(bd.textstart);
370 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 if (newp == NULL)
372 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 mch_memmove(newp, oldp, (size_t)bd.textcol);
LemonBoy4b936742022-05-13 21:56:28 +0100374 vim_memset(newp + bd.textcol, TAB, (size_t)tabs);
375 vim_memset(newp + bd.textcol + tabs, ' ', (size_t)spaces);
376 // Note that STRMOVE() copies the trailing NUL.
377 STRMOVE(newp + bd.textcol + tabs + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100381 colnr_T destination_col; // column to which text in block will
382 // be shifted
383 char_u *verbatim_copy_end; // end of the part of the line which is
384 // copied verbatim
385 colnr_T verbatim_copy_width;// the (displayed) width of this part
386 // of line
387 unsigned fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000388 size_t block_space_width;
389 size_t shift_amount;
390 char_u *non_white = bd.textstart;
391 colnr_T non_white_col;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100392 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000394 /*
395 * Firstly, let's find the first non-whitespace character that is
396 * displayed after the block's start column and the character's column
397 * number. Also, let's calculate the width of all the whitespace
398 * characters that are displayed in the block and precede the searched
399 * non-whitespace character.
400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100402 // If "bd.startspaces" is set, "bd.textstart" points to the character,
403 // the part of which is displayed at the block's beginning. Let's start
404 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000405 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100406 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000407
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100408 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000409 non_white_col = bd.start_vcol;
410
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100411 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
412 non_white_col, bd.textstart, non_white);
413 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000414 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100415 incr = lbr_chartabsize_adv(&cts);
416 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100418 non_white_col = cts.cts_vcol;
419 non_white = cts.cts_ptr;
420 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000422 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100423 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000424 shift_amount = (block_space_width < (size_t)total
425 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000426
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100427 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000428 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100430 // Now let's find out how much of the beginning of the line we can
431 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000432 verbatim_copy_end = bd.textstart;
433 verbatim_copy_width = bd.start_vcol;
434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100435 // If "bd.startspaces" is set, "bd.textstart" points to the character
436 // preceding the block. We have to subtract its width to obtain its
437 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000438 if (bd.startspaces)
439 verbatim_copy_width -= bd.start_char_vcols;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100440 init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
441 bd.textstart, verbatim_copy_end);
442 while (cts.cts_vcol < destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000443 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100444 incr = lbr_chartabsize(&cts);
445 if (cts.cts_vcol + incr > destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000446 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100447 cts.cts_vcol += incr;
448 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000449 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100450 verbatim_copy_width = cts.cts_vcol;
451 verbatim_copy_end = cts.cts_ptr;
452 clear_chartabsize_arg(&cts);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100454 // If "destination_col" is different from the width of the initial
455 // part of the line that will be copied, it means we encountered a tab
456 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 fill = destination_col - verbatim_copy_width;
458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100459 // The replacement line will consist of:
460 // - the beginning of the original line up to "verbatim_copy_end",
461 // - "fill" number of spaces,
462 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000463 new_line_len = (unsigned)(verbatim_copy_end - oldp)
464 + fill
LemonBoy4b936742022-05-13 21:56:28 +0100465 + (unsigned)STRLEN(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000466
LemonBoy4b936742022-05-13 21:56:28 +0100467 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 if (newp == NULL)
469 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000470 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200471 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
LemonBoy4b936742022-05-13 21:56:28 +0100472 // Note that STRMOVE() copies the trailing NUL.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000473 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100475 // replace the line
LemonBoy4b936742022-05-13 21:56:28 +0100476 added = new_line_len - (int)STRLEN(oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
LemonBoy4b936742022-05-13 21:56:28 +0100478 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 State = oldstate;
480 curwin->w_cursor.col = oldcol;
481#ifdef FEAT_RIGHTLEFT
482 p_ri = old_p_ri;
483#endif
484}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486/*
487 * Insert string "s" (b_insert ? before : after) block :AKelly
488 * Caller must prepare for undo.
489 */
490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100491block_insert(
492 oparg_T *oap,
493 char_u *s,
494 int b_insert,
495 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
Bram Moolenaardac13472019-09-16 21:06:21 +0200497 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100498 int count = 0; // extra spaces to replace a cut TAB
499 int spaces = 0; // non-zero if cutting a TAB
500 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200501 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100502 unsigned s_len; // STRLEN(s)
503 char_u *newp, *oldp; // new, old lines
504 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 int oldstate = State;
506
Bram Moolenaar24959102022-05-07 20:01:16 +0100507 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 s_len = (unsigned)STRLEN(s);
509
510 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
511 {
512 block_prep(oap, bdp, lnum, TRUE);
513 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100514 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516 oldp = ml_get(lnum);
517
518 if (b_insert)
519 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200520 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 spaces = bdp->startspaces;
522 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100523 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 offset = bdp->textcol;
525 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100526 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200528 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100529 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200531 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 offset = bdp->textcol + bdp->textlen - (spaces != 0);
535 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100536 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100538 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 if (!bdp->is_MAX)
540 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
541 count = spaces;
542 offset = bdp->textcol + bdp->textlen;
543 }
544 }
545
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200546 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000547 // avoid copying part of a multi-byte character
548 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100549
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200550 if (spaces < 0) // can happen when the cursor was moved
551 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200552
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000553 // Make sure the allocated size matches what is actually copied below.
554 newp = alloc(STRLEN(oldp) + spaces + s_len
555 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
556 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (newp == NULL)
558 continue;
559
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100560 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000561 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 oldp += offset;
563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100564 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200565 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200566 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100568 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200569 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 offset += s_len;
571
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000572 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000574 if (*oldp == TAB)
575 {
576 // insert post-padding
577 vim_memset(newp + offset + spaces, ' ',
578 (size_t)(ts_val - spaces));
579 // we're splitting a TAB, don't copy it
580 oldp++;
581 // We allowed for that TAB, remember this now
582 count++;
583 }
584 else
585 // Not a TAB, no extra spaces
586 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588
589 if (spaces > 0)
590 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000591 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ml_replace(lnum, newp, FALSE);
594
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200595 if (b_insert)
596 // correct any text properties
597 inserted_bytes(lnum, startcol, s_len);
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (lnum == oap->end.lnum)
600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100601 // Set "']" mark to the end of the block instead of the end of
602 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 curbuf->b_op_end.lnum = oap->end.lnum;
604 curbuf->b_op_end.col = offset;
605 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100606 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
609
610 State = oldstate;
611}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200614 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200616 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
618 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int n;
622 linenr_T lnum;
623 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 char_u *newp, *oldp;
625 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
627 int did_yank = FALSE;
628
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100629 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 return OK;
631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100632 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (oap->empty)
634 return u_save_cursor();
635
636 if (!curbuf->b_p_ma)
637 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200638 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 return FAIL;
640 }
641
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000642 if (VIsual_select && oap->is_VIsual)
643 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100644 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#ifdef FEAT_CLIPBOARD
647 adjust_clip_reg(&oap->regname);
648#endif
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (has_mbyte)
651 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaard04b7502010-07-08 22:27:55 +0200653 /*
654 * Imitate the strange Vi behaviour: If the delete spans more than one
655 * line and motion_type == MCHAR and the result is a blank line, make the
656 * delete linewise. Don't do this for the change command or Visual mode.
657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000660 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100662 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 && oap->op_type == OP_DELETE)
664 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200665 ptr = ml_get(oap->end.lnum) + oap->end.col;
666 if (*ptr != NUL)
667 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 ptr = skipwhite(ptr);
669 if (*ptr == NUL && inindent(0))
670 oap->motion_type = MLINE;
671 }
672
Bram Moolenaard04b7502010-07-08 22:27:55 +0200673 /*
674 * Check for trying to delete (e.g. "D") in an empty line.
675 * Note: For the change operator it is ok.
676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if ( oap->motion_type == MCHAR
678 && oap->line_count == 1
679 && oap->op_type == OP_DELETE
680 && *ml_get(oap->start.lnum) == NUL)
681 {
682 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000683 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * 'cpoptions' (Vi compatible).
685 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000686 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
688 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000689 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
691 beep_flush();
692 return OK;
693 }
694
Bram Moolenaard04b7502010-07-08 22:27:55 +0200695 /*
696 * Do a yank of whatever we're about to delete.
697 * If a yank register was specified, put the deleted text into that
698 * register. For the black hole register '_' don't yank anything.
699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (oap->regname != '_')
701 {
702 if (oap->regname != 0)
703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 if (!valid_yank_reg(oap->regname, TRUE))
706 {
707 beep_flush();
708 return OK;
709 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100710 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
711 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 did_yank = TRUE;
713 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200714 else
715 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
717 /*
718 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100719 * delete contains a line break, or when using a specific operator (Vi
720 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100722 if (oap->motion_type == MLINE || oap->line_count > 1
723 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100725 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (op_yank(oap, TRUE, FALSE) == OK)
727 did_yank = TRUE;
728 }
729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100730 // Yank into small delete register when no named register specified
731 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200732 if ((
733#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200734 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
735 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200736#endif
737 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 && oap->line_count == 1)
739 {
740 oap->regname = '-';
741 get_yank_register(oap->regname, TRUE);
742 if (op_yank(oap, TRUE, FALSE) == OK)
743 did_yank = TRUE;
744 oap->regname = 0;
745 }
746
747 /*
748 * If there's too much stuff to fit in the yank register, then get a
749 * confirmation before doing the delete. This is crude, but simple.
750 * And it avoids doing a delete of something we can't put back if we
751 * want.
752 */
753 if (!did_yank)
754 {
755 int msg_silent_save = msg_silent;
756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100757 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
759 msg_silent = msg_silent_save;
760 if (n != 'y')
761 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000762 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 return FAIL;
764 }
765 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100766
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100767#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100768 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200769 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772
Bram Moolenaard04b7502010-07-08 22:27:55 +0200773 /*
774 * block mode delete
775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (oap->block_mode)
777 {
778 if (u_save((linenr_T)(oap->start.lnum - 1),
779 (linenr_T)(oap->end.lnum + 1)) == FAIL)
780 return FAIL;
781
782 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
783 {
784 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 continue;
787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100788 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 if (lnum == curwin->w_cursor.lnum)
790 {
791 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794
Bram Moolenaar8055d172019-05-17 22:57:26 +0200795 // "n" == number of chars deleted
796 // If we delete a TAB, it may be replaced by several characters.
797 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 n = bd.textlen - bd.startspaces - bd.endspaces;
799 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200800 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if (newp == NULL)
802 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100803 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100805 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200806 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100808 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000810 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100811 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200813
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100814#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200815 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200816 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
819
820 check_cursor_col();
821 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
822 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100823 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100825 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 if (oap->op_type == OP_CHANGE)
828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 // Delete the lines except the first one. Temporarily move the
830 // cursor to the next line. Save the current line number, if the
831 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (oap->line_count > 1)
833 {
834 lnum = curwin->w_cursor.lnum;
835 ++curwin->w_cursor.lnum;
836 del_lines((long)(oap->line_count - 1), TRUE);
837 curwin->w_cursor.lnum = lnum;
838 }
839 if (u_save_cursor() == FAIL)
840 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100843 beginline(BL_WHITE); // cursor on first non-white
844 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 ai_col = curwin->w_cursor.col;
846 }
847 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100848 beginline(0); // cursor in column 0
849 truncate_line(FALSE); // delete the rest of the line
850 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100852 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 else
855 {
856 del_lines(oap->line_count, TRUE);
857 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860 }
861 else
862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 if (virtual_op)
864 {
865 int endcol = 0;
866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (gchar_pos(&oap->start) == '\t')
869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100870 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 return FAIL;
872 if (oap->line_count == 1)
873 endcol = getviscol2(oap->end.col, oap->end.coladd);
874 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
875 oap->start = curwin->w_cursor;
876 if (oap->line_count == 1)
877 {
878 coladvance(endcol);
879 oap->end.col = curwin->w_cursor.col;
880 oap->end.coladd = curwin->w_cursor.coladd;
881 curwin->w_cursor = oap->start;
882 }
883 }
884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100885 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (gchar_pos(&oap->end) == '\t'
887 && (int)oap->end.coladd < oap->inclusive)
888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100889 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (u_save((linenr_T)(oap->end.lnum - 1),
891 (linenr_T)(oap->end.lnum + 1)) == FAIL)
892 return FAIL;
893 curwin->w_cursor = oap->end;
894 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
895 oap->end = curwin->w_cursor;
896 curwin->w_cursor = oap->start;
897 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100898 if (has_mbyte)
899 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100904 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 return FAIL;
906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100907 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100908 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 && oap->op_type == OP_CHANGE
910 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100911 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 display_dollar(oap->end.col - !oap->inclusive);
913
914 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if (virtual_op)
917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // fix up things for virtualedit-delete:
919 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 char_u *curline = ml_get_curline();
921 int len = (int)STRLEN(curline);
922
923 if (oap->end.coladd != 0
924 && (int)oap->end.col >= len - 1
925 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
926 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100927 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (n == 0 && oap->start.coladd != oap->end.coladd)
929 n = 1;
930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (gchar_cursor() != NUL)
933 curwin->w_cursor.coladd = 0;
934 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200935 (void)del_bytes((long)n, !virtual_op,
936 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100938 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 pos_T curpos;
941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100942 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
944 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
945 return FAIL;
946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100949 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 ++curwin->w_cursor.lnum;
951 del_lines((long)(oap->line_count - 2), FALSE);
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200954 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200955 curwin->w_cursor.col = 0;
956 (void)del_bytes((long)n, !virtual_op,
957 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200959 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200961 if (oap->op_type == OP_DELETE)
962 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 }
964
965 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
966
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000967setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200968 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100970 if (oap->block_mode)
971 {
972 curbuf->b_op_end.lnum = oap->end.lnum;
973 curbuf->b_op_end.col = oap->start.col;
974 }
975 else
976 curbuf->b_op_end = oap->start;
977 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 return OK;
981}
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Adjust end of operating area for ending on a multi-byte character.
985 * Used for deletion.
986 */
987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100988mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 char_u *p;
991
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000992 if (!oap->inclusive)
993 return;
994
995 p = ml_get(oap->end.lnum);
996 oap->end.col += mb_tail_off(p, p + oap->end.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar630afe82018-06-28 19:26:28 +0200999/*
1000 * Replace the character under the cursor with "c".
1001 * This takes care of multi-byte characters.
1002 */
1003 static void
1004replace_character(int c)
1005{
1006 int n = State;
1007
Bram Moolenaar24959102022-05-07 20:01:16 +01001008 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001009 ins_char(c);
1010 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001011 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001012 dec_cursor();
1013}
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015/*
1016 * Replace a whole area with one character.
1017 */
1018 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001019op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 char_u *newp, *oldp;
1024 size_t oldlen;
1025 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001026 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001027 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001030 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001032 if (c == REPLACE_CR_NCHAR)
1033 {
1034 had_ctrl_v_cr = TRUE;
1035 c = CAR;
1036 }
1037 else if (c == REPLACE_NL_NCHAR)
1038 {
1039 had_ctrl_v_cr = TRUE;
1040 c = NL;
1041 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001042
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (has_mbyte)
1044 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 if (u_save((linenr_T)(oap->start.lnum - 1),
1047 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1048 return FAIL;
1049
1050 /*
1051 * block mode replace
1052 */
1053 if (oap->block_mode)
1054 {
1055 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1056 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001058 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1060 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001061 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // n == number of extra chars required
1064 // If we split a TAB, it may be replaced by several characters.
1065 // Thus the number of characters may increase!
1066 // If the range starts in virtual space, count the initial
1067 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1069 {
1070 pos_T vpos;
1071
Bram Moolenaara1381de2009-11-03 15:44:21 +00001072 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 getvpos(&vpos, oap->start_vcol);
1074 bd.startspaces += vpos.coladd;
1075 n = bd.startspaces;
1076 }
1077 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001078 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001081 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001085 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 numc = oap->end_vcol - oap->start_vcol + 1;
1087 if (bd.is_short && (!virtual_op || bd.is_MAX))
1088 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001090 // A double-wide character can be replaced only up to half the
1091 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if ((*mb_char2cells)(c) > 1)
1093 {
1094 if ((numc & 1) && !bd.is_short)
1095 {
1096 ++bd.endspaces;
1097 ++n;
1098 }
1099 numc = numc / 2;
1100 }
1101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001102 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 num_chars = numc;
1104 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001105 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 n += numc - bd.textlen;
1107
1108 oldp = ml_get_curline();
1109 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001110 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (newp == NULL)
1112 continue;
1113 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 mch_memmove(newp, oldp, (size_t)bd.textcol);
1116 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001117 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001118 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001119 // insert replacement chars CHECK FOR ALLOCATED SPACE
1120 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1121 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001122 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001124 if (has_mbyte)
1125 {
1126 n = (int)STRLEN(newp);
1127 while (--num_chars >= 0)
1128 n += (*mb_char2bytes)(c, newp + n);
1129 }
1130 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001131 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001132 if (!bd.is_short)
1133 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001134 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001135 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001137 STRMOVE(newp + STRLEN(newp), oldp);
1138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001142 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001143 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001144 if (after_p != NULL)
1145 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001147 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001149 if (after_p != NULL)
1150 {
1151 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1152 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1153 oap->end.lnum++;
1154 vim_free(after_p);
1155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157 }
1158 else
1159 {
1160 /*
1161 * MCHAR and MLINE motion replace.
1162 */
1163 if (oap->motion_type == MLINE)
1164 {
1165 oap->start.col = 0;
1166 curwin->w_cursor.col = 0;
1167 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1168 if (oap->end.col)
1169 --oap->end.col;
1170 }
1171 else if (!oap->inclusive)
1172 dec(&(oap->end));
1173
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001174 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001176 int done = FALSE;
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 n = gchar_cursor();
1179 if (n != NUL)
1180 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001181 int new_byte_len = (*mb_char2len)(c);
1182 int old_byte_len = mb_ptr2len(ml_get_cursor());
1183
1184 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001186 // This is slow, but it handles replacing a single-byte
1187 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001188 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001189 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001190 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001191 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (n == TAB)
1196 {
1197 int end_vcol = 0;
1198
1199 if (curwin->w_cursor.lnum == oap->end.lnum)
1200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 // oap->end has to be recalculated when
1202 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 end_vcol = getviscol2(oap->end.col,
1204 oap->end.coladd);
1205 }
1206 coladvance_force(getviscol());
1207 if (curwin->w_cursor.lnum == oap->end.lnum)
1208 getvpos(&oap->end, end_vcol);
1209 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001210 // with "coladd" set may move to just after a TAB
1211 if (gchar_cursor() != NUL)
1212 {
1213 PBYTE(curwin->w_cursor, c);
1214 done = TRUE;
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001218 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
1220 int virtcols = oap->end.coladd;
1221
1222 if (curwin->w_cursor.lnum == oap->start.lnum
1223 && oap->start.col == oap->end.col && oap->start.coladd)
1224 virtcols -= oap->start.coladd;
1225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001226 // oap->end has been trimmed so it's effectively inclusive;
1227 // as a result an extra +1 must be counted so we don't
1228 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1230 curwin->w_cursor.col -= (virtcols + 1);
1231 for (; virtcols >= 0; virtcols--)
1232 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001233 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001234 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001235 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001236 PBYTE(curwin->w_cursor, c);
1237 if (inc(&curwin->w_cursor) == -1)
1238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001242 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (inc_cursor() == -1)
1244 break;
1245 }
1246 }
1247
1248 curwin->w_cursor = oap->start;
1249 check_cursor();
1250 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1251
Bram Moolenaare1004402020-10-24 20:49:43 +02001252 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001253 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001255 curbuf->b_op_start = oap->start;
1256 curbuf->b_op_end = oap->end;
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
1259 return OK;
1260}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001262static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264/*
1265 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1266 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001268op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001272 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
1274 if (u_save((linenr_T)(oap->start.lnum - 1),
1275 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1276 return;
1277
1278 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001279 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
1281 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1282 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001283 int one_change;
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 block_prep(oap, &bd, pos.lnum, FALSE);
1286 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001287 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1288 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001289
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001290#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001291 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001293 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
Bram Moolenaar009b2592004-10-24 19:18:58 +00001295 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1296 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001297 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001298 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001300 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 if (did_change)
1305 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1306 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001307 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
1309 if (oap->motion_type == MLINE)
1310 {
1311 oap->start.col = 0;
1312 pos.col = 0;
1313 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1314 if (oap->end.col)
1315 --oap->end.col;
1316 }
1317 else if (!oap->inclusive)
1318 dec(&(oap->end));
1319
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001320 if (pos.lnum == oap->end.lnum)
1321 did_change = swapchars(oap->op_type, &pos,
1322 oap->end.col - pos.col + 1);
1323 else
1324 for (;;)
1325 {
1326 did_change |= swapchars(oap->op_type, &pos,
1327 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1328 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001329 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001330 break;
1331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (did_change)
1333 {
1334 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1335 0L);
1336#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001337 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
1339 char_u *ptr;
1340 int count;
1341
1342 pos = oap->start;
1343 while (pos.lnum < oap->end.lnum)
1344 {
1345 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001346 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001347 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001348 // get the line again, it may have been flushed
1349 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001351 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 pos.col = 0;
1353 pos.lnum++;
1354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001356 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001357 // get the line again, it may have been flushed
1358 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001360 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362#endif
1363 }
1364 }
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001367 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001368 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaare1004402020-10-24 20:49:43 +02001370 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001371 {
1372 // Set '[ and '] marks.
1373 curbuf->b_op_start = oap->start;
1374 curbuf->b_op_end = oap->end;
1375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001378 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001379 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380}
1381
1382/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001383 * Invoke swapchar() on "length" bytes at position "pos".
1384 * "pos" is advanced to just after the changed characters.
1385 * "length" is rounded up to include the whole last multi-byte character.
1386 * Also works correctly when the number of bytes changes.
1387 * Returns TRUE if some character was changed.
1388 */
1389 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001390swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001391{
1392 int todo;
1393 int did_change = 0;
1394
1395 for (todo = length; todo > 0; --todo)
1396 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001397 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001398 {
1399 int len = (*mb_ptr2len)(ml_get_pos(pos));
1400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001402 if (len > 0)
1403 todo -= len - 1;
1404 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001405 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001407 break;
1408 }
1409 return did_change;
1410}
1411
1412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 * If op_type == OP_UPPER: make uppercase,
1414 * if op_type == OP_LOWER: make lowercase,
1415 * if op_type == OP_ROT13: do rot13 encoding,
1416 * else swap case of character at 'pos'
1417 * returns TRUE when something actually changed.
1418 */
1419 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001420swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 int c;
1423 int nc;
1424
1425 c = gchar_pos(pos);
1426
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001427 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (c >= 0x80 && op_type == OP_ROT13)
1429 return FALSE;
1430
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001431 if (op_type == OP_UPPER && c == 0xdf
1432 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001433 {
1434 pos_T sp = curwin->w_cursor;
1435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001437 curwin->w_cursor = *pos;
1438 del_char(FALSE);
1439 ins_char('S');
1440 ins_char('S');
1441 curwin->w_cursor = sp;
1442 inc(pos);
1443 }
1444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001445 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 nc = c;
1448 if (MB_ISLOWER(c))
1449 {
1450 if (op_type == OP_ROT13)
1451 nc = ROT13(c, 'a');
1452 else if (op_type != OP_LOWER)
1453 nc = MB_TOUPPER(c);
1454 }
1455 else if (MB_ISUPPER(c))
1456 {
1457 if (op_type == OP_ROT13)
1458 nc = ROT13(c, 'A');
1459 else if (op_type != OP_UPPER)
1460 nc = MB_TOLOWER(c);
1461 }
1462 if (nc != c)
1463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1465 {
1466 pos_T sp = curwin->w_cursor;
1467
1468 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001469 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001470 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 ins_char(nc);
1472 curwin->w_cursor = sp;
1473 }
1474 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001475 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 return TRUE;
1477 }
1478 return FALSE;
1479}
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * op_insert - Insert and append operators for Visual mode.
1483 */
1484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001485op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 long ins_len, pre_textlen = 0;
1488 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001489 colnr_T ind_pre_col = 0, ind_post_col;
1490 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 struct block_def bd;
1492 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001493 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001494 pos_T start_insert;
1495 // offset when cursor was moved in insert mode
1496 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001501 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001503 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 if (oap->block_mode)
1506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001507 // When 'virtualedit' is used, need to insert the extra spaces before
1508 // doing block_prep(). When only "block" is used, virtual edit is
1509 // already disabled, but still need it when calling
1510 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001511 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001512 // state for the current window. To override that state, we need to
1513 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 if (curwin->w_cursor.coladd > 0)
1515 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001516 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if (u_save_cursor() == FAIL)
1519 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001520
Gary Johnson51ad8502021-08-03 18:33:08 +02001521 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 coladvance_force(oap->op_type == OP_APPEND
1523 ? oap->end_vcol + 1 : getviscol());
1524 if (oap->op_type == OP_APPEND)
1525 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001526 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001530 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001531 ind_pre_col = (colnr_T)getwhitecols_curline();
1532 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if (oap->op_type == OP_APPEND)
1536 firstline += bd.textlen;
1537 pre_textlen = (long)STRLEN(firstline);
1538 }
1539
1540 if (oap->op_type == OP_APPEND)
1541 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001542 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001544 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 curwin->w_set_curswant = TRUE;
1546 while (*ml_get_cursor() != NUL
1547 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1548 ++curwin->w_cursor.col;
1549 if (bd.is_short && !bd.is_MAX)
1550 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001551 // First line was too short, make it longer and adjust the
1552 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 if (u_save_cursor() == FAIL)
1554 return;
1555 for (i = 0; i < bd.endspaces; ++i)
1556 ins_char(' ');
1557 bd.textlen += bd.endspaces;
1558 }
1559 }
1560 else
1561 {
1562 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001563 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001565 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001566 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 && oap->start_vcol != oap->end_vcol)
1568 inc_cursor();
1569 }
1570 }
1571
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001572 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001573 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001574 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001576 // When a tab was inserted, and the characters in front of the tab
1577 // have been converted to a tab as well, the column of the cursor
1578 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001579 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001580 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001581 oap->start = curbuf->b_op_start_orig;
1582
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001583 // If user has moved off this line, we don't know what to do, so do
1584 // nothing.
1585 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001586 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 return;
1588
1589 if (oap->block_mode)
1590 {
1591 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001592 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001593 size_t len;
1594 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // If indent kicked in, the firstline might have changed
1597 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001598 ind_post_col = (colnr_T)getwhitecols_curline();
1599 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001600 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001601 bd.textcol += ind_post_col - ind_pre_col;
1602 ind_post_vcol = get_indent();
1603 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001604 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001605 }
1606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 // The user may have moved the cursor before inserting something, try
1608 // to adjust the block for that. But only do it, if the difference
1609 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001610 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1611 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001612 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001613 int t = getviscol2(curbuf->b_op_start_orig.col,
1614 curbuf->b_op_start_orig.coladd);
1615
zeertzjq7745f142022-02-15 11:48:22 +00001616 if (oap->op_type == OP_INSERT
1617 && oap->start.col + oap->start.coladd
1618 != curbuf->b_op_start_orig.col
1619 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001620 {
zeertzjq7745f142022-02-15 11:48:22 +00001621 oap->start.col = curbuf->b_op_start_orig.col;
1622 pre_textlen -= t - oap->start_vcol;
1623 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001624 }
zeertzjq7745f142022-02-15 11:48:22 +00001625 else if (oap->op_type == OP_APPEND
1626 && oap->start.col + oap->start.coladd
1627 >= curbuf->b_op_start_orig.col
1628 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001629 {
zeertzjq7745f142022-02-15 11:48:22 +00001630 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001631 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001632 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001633 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001634 oap->start_vcol = t;
1635 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001636 }
1637 }
1638
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001639 // Spaces and tabs in the indent may have changed to other spaces and
1640 // tabs. Get the starting column again and correct the length.
1641 // Don't do this when "$" used, end-of-line will have changed.
1642 //
1643 // if indent was added and the inserted text was after the indent,
1644 // correct the selection for the new indent.
1645 if (did_indent && bd.textcol - ind_post_col > 0)
1646 {
1647 oap->start.col += ind_post_col - ind_pre_col;
1648 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1649 oap->end.col += ind_post_col - ind_pre_col;
1650 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001653 if (did_indent && bd.textcol - ind_post_col > 0)
1654 {
1655 // undo for where "oap" is used below
1656 oap->start.col -= ind_post_col - ind_pre_col;
1657 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1658 oap->end.col -= ind_post_col - ind_pre_col;
1659 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1662 {
1663 if (oap->op_type == OP_APPEND)
1664 {
1665 pre_textlen += bd2.textlen - bd.textlen;
1666 if (bd2.endspaces)
1667 --bd2.textlen;
1668 }
1669 bd.textcol = bd2.textcol;
1670 bd.textlen = bd2.textlen;
1671 }
1672
1673 /*
1674 * Subsequent calls to ml_get() flush the firstline data - take a
1675 * copy of the required string.
1676 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001677 firstline = ml_get(oap->start.lnum);
1678 len = STRLEN(firstline);
1679 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001681 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001682 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001683 // account for pressing cursor in insert mode when '$' was used
1684 if (bd.is_MAX
1685 && (start_insert.lnum == Insstart.lnum
1686 && start_insert.col > Insstart.col))
1687 {
1688 offset = (start_insert.col - Insstart.col);
1689 add -= offset;
1690 if (oap->end_vcol > offset)
1691 oap->end_vcol -= (offset + 1);
1692 else
1693 // moved outside of the visual block, what to do?
1694 return;
1695 }
1696 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001697 if ((size_t)add > len)
1698 firstline += len; // short line, point to the NUL
1699 else
1700 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001701 if (pre_textlen >= 0 && (ins_len =
1702 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001704 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 if (ins_text != NULL)
1706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001707 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (u_save(oap->start.lnum,
1709 (linenr_T)(oap->end.lnum + 1)) == OK)
1710 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1711 &bd);
1712
1713 curwin->w_cursor.col = oap->start.col;
1714 check_cursor();
1715 vim_free(ins_text);
1716 }
1717 }
1718 }
1719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721/*
1722 * op_change - handle a change operation
1723 *
1724 * return TRUE if edit() returns because of a CTRL-O command
1725 */
1726 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001727op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 colnr_T l;
1730 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 long offset;
1732 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001733 long ins_len;
1734 long pre_textlen = 0;
1735 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 char_u *firstline;
1737 char_u *ins_text, *newp, *oldp;
1738 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 l = oap->start.col;
1741 if (oap->motion_type == MLINE)
1742 {
1743 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001744 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
1746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001747 // First delete the text in the region. In an empty buffer only need to
1748 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1750 {
1751 if (u_save_cursor() == FAIL)
1752 return FALSE;
1753 }
1754 else if (op_delete(oap) == FAIL)
1755 return FALSE;
1756
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001757 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 && !virtual_op)
1759 inc_cursor();
1760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001761 // check for still on same line (<CR> in inserted text meaningless)
1762 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (oap->block_mode)
1764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001765 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (virtual_op && (curwin->w_cursor.coladd > 0
1767 || gchar_cursor() == NUL))
1768 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001769 firstline = ml_get(oap->start.lnum);
1770 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001771 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 bd.textcol = curwin->w_cursor.col;
1773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (oap->motion_type == MLINE)
1776 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
zeertzjqcdeb6572022-11-15 13:46:12 +00001778 // Reset finish_op now, don't want it set inside edit().
1779 int save_finish_op = finish_op;
1780 finish_op = FALSE;
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 retval = edit(NUL, FALSE, (linenr_T)1);
1783
zeertzjqcdeb6572022-11-15 13:46:12 +00001784 finish_op = save_finish_op;
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001787 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001789 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001791 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001793 // Auto-indenting may have changed the indent. If the cursor was past
1794 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001796 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001798 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001799
1800 pre_textlen += new_indent - pre_indent;
1801 bd.textcol += new_indent - pre_indent;
1802 }
1803
1804 ins_len = (long)STRLEN(firstline) - pre_textlen;
1805 if (ins_len > 0)
1806 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001807 // Subsequent calls to ml_get() flush the firstline data - take a
1808 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001809 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001811 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1813 linenr++)
1814 {
1815 block_prep(oap, &bd, linenr, TRUE);
1816 if (!bd.is_short || virtual_op)
1817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 pos_T vpos;
1819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 // If the block starts in virtual space, count the
1821 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (bd.is_short)
1823 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001824 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 else
1828 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001830 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (newp == NULL)
1832 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001833 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 mch_memmove(newp, oldp, (size_t)bd.textcol);
1835 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001836 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1839 offset += ins_len;
1840 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001841 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001843#ifdef FEAT_PROP_POPUP
1844 // Shift the properties for linenr as edit() would do.
1845 if (curbuf->b_has_textprop)
1846 adjust_prop_columns(linenr, bd.textcol,
1847 vpos.coladd + ins_len, 0);
1848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 }
1851 check_cursor();
1852
1853 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1854 }
1855 vim_free(ins_text);
1856 }
1857 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001858 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
1860 return retval;
1861}
1862
1863/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001864 * When the cursor is on the NUL past the end of the line and it should not be
1865 * there move it left.
1866 */
1867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001868adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001869{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001870 unsigned int cur_ve_flags = get_ve_flags();
1871
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001872 int adj_cursor = (curwin->w_cursor.col > 0
1873 && gchar_cursor() == NUL
1874 && (cur_ve_flags & VE_ONEMORE) == 0
1875 && !(restart_edit || (State & MODE_INSERT)));
1876 if (!adj_cursor)
1877 return;
1878
1879 // Put the cursor on the last character in the line.
1880 dec_cursor();
1881
1882 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001884 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001885
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001886 // Coladd is set to the width of the last character.
1887 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1888 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890}
1891
Bram Moolenaar81340392012-06-06 16:12:59 +02001892/*
1893 * If "process" is TRUE and the line begins with a comment leader (possibly
1894 * after some white space), return a pointer to the text after it. Put a boolean
1895 * value indicating whether the line ends with an unclosed comment in
1896 * "is_comment".
1897 * line - line to be processed,
1898 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001899 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001900 * include_space - whether to also skip space following the comment leader,
1901 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001902 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001903 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001904 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001905skip_comment(
1906 char_u *line,
1907 int process,
1908 int include_space,
1909 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001910{
1911 char_u *comment_flags = NULL;
1912 int lead_len;
1913 int leader_offset = get_last_leader_offset(line, &comment_flags);
1914
1915 *is_comment = FALSE;
1916 if (leader_offset != -1)
1917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // Let's check whether the line ends with an unclosed comment.
1919 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001920 while (*comment_flags)
1921 {
1922 if (*comment_flags == COM_END
1923 || *comment_flags == ':')
1924 break;
1925 ++comment_flags;
1926 }
1927 if (*comment_flags != COM_END)
1928 *is_comment = TRUE;
1929 }
1930
1931 if (process == FALSE)
1932 return line;
1933
1934 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1935
1936 if (lead_len == 0)
1937 return line;
1938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001939 // Find:
1940 // - COM_END,
1941 // - colon,
1942 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001943 while (*comment_flags)
1944 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001945 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001946 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001947 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001948 ++comment_flags;
1949 }
1950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001951 // If we found a colon, it means that we are not processing a line
1952 // starting with a closing part of a three-part comment. That's good,
1953 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001954 if (*comment_flags == ':' || *comment_flags == NUL)
1955 line += lead_len;
1956
1957 return line;
1958}
Bram Moolenaar81340392012-06-06 16:12:59 +02001959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001961 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001962 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001963 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1964 * leaders should not be removed.
1965 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1966 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001968 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 */
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971do_join(
1972 long count,
1973 int insert_space,
1974 int save_undo,
1975 int use_formatoptions UNUSED,
1976 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001978 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001979 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001980 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001982 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001983 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001984 int endcurr1 = NUL;
1985 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001986 int currsize = 0; // size of the current line
1987 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001989 colnr_T col = 0;
1990 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001991 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001992 int remove_comments = (use_formatoptions == TRUE)
1993 && has_format_option(FO_REMOVE_COMS);
1994 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001995#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001996 int propcount = 0; // number of props over all joined lines
1997 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002000 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2001 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2002 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002004 // Allocate an array to store the number of spaces inserted before each
2005 // line. We will use it to pre-compute the length of the new line and the
2006 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002007 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002008 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002010 if (remove_comments)
2011 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002012 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002013 if (comments == NULL)
2014 {
2015 vim_free(spaces);
2016 return FAIL;
2017 }
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002021 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002022 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002023 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002025 for (t = 0; t < count; ++t)
2026 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002027 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002028#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002029 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2030 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002031#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002032 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002033 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002034 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002035 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2036 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2037 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002038 if (remove_comments)
2039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002040 // We don't want to remove the comment leader if the
2041 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002042 if (t > 0 && prev_was_comment)
2043 {
2044
2045 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2046 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002047 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002048 curr = new_curr;
2049 }
2050 else
2051 curr = skip_comment(curr, FALSE, insert_space,
2052 &prev_was_comment);
2053 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002054
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002055 if (insert_space && t > 0)
2056 {
2057 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002058 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002059 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002060 && (!has_format_option(FO_MBYTE_JOIN)
2061 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2062 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002063 || (mb_ptr2char(curr) < 0x100
2064 && !(enc_utf8 && utf_eat_space(endcurr1)))
2065 || (endcurr1 < 0x100
2066 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002067 )
2068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002069 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002070 if (endcurr1 == ' ')
2071 endcurr1 = endcurr2;
2072 else
2073 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002074 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002075 if ( p_js
2076 && (endcurr1 == '.'
2077 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2078 && (endcurr1 == '?' || endcurr1 == '!'))))
2079 ++spaces[t];
2080 }
2081 }
2082 currsize = (int)STRLEN(curr);
2083 sumsize += currsize + spaces[t];
2084 endcurr1 = endcurr2 = NUL;
2085 if (insert_space && currsize > 0)
2086 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002087 if (has_mbyte)
2088 {
2089 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002090 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002091 endcurr1 = (*mb_ptr2char)(cend);
2092 if (cend > curr)
2093 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002094 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002095 endcurr2 = (*mb_ptr2char)(cend);
2096 }
2097 }
2098 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002099 {
2100 endcurr1 = *(curr + currsize - 1);
2101 if (currsize > 1)
2102 endcurr2 = *(curr + currsize - 2);
2103 }
2104 }
2105 line_breakcheck();
2106 if (got_int)
2107 {
2108 ret = FAIL;
2109 goto theend;
2110 }
2111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002113 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002114 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002116 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002117 newp_len = sumsize + 1;
2118#ifdef FEAT_PROP_POPUP
2119 newp_len += propcount * sizeof(textprop_T);
2120#endif
2121 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002122 if (newp == NULL)
2123 {
2124 ret = FAIL;
2125 goto theend;
2126 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002127 cend = newp + sumsize;
2128 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002130 /*
2131 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002132 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002133 *
2134 * Move marks from each deleted line to the joined line, adjusting the
2135 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2136 * should not really be a problem.
2137 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002138#ifdef FEAT_PROP_POPUP
2139 props_remaining = propcount;
2140#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002141 for (t = count - 1; ; --t)
2142 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002143 int spaces_removed;
2144
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002145 cend -= currsize;
2146 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002147
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002148 if (spaces[t] > 0)
2149 {
2150 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002151 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002152 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002153
2154 // If deleting more spaces than adding, the cursor moves no more than
2155 // what is added if it is inside these spaces.
2156 spaces_removed = (curr - curr_start) - spaces[t];
2157
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002158 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002159 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002160#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002161 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2162 curwin->w_cursor.lnum + t, t == count - 1,
2163 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002164#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002165 if (t == 0)
2166 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002167 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002168 if (remove_comments)
2169 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 if (insert_space && t > 1)
2171 curr = skipwhite(curr);
2172 currsize = (int)STRLEN(curr);
2173 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002174
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002175 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Bram Moolenaare1004402020-10-24 20:49:43 +02002177 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002178 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002179 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002180 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002181 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002182 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002184 // Only report the change in the first line here, del_lines() will report
2185 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 changed_lines(curwin->w_cursor.lnum, currsize,
2187 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002189 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * briefly, and then move it back. After del_lines() the cursor may
2191 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 */
2193 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002195 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 curwin->w_cursor.lnum = t;
2197
2198 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002199 * Set the cursor column:
2200 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002201 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002203 curwin->w_cursor.col =
2204 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 curwin->w_set_curswant = TRUE;
2209
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002210theend:
2211 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002212 if (remove_comments)
2213 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002214 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215}
2216
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002217#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002219 * Reset 'linebreak' and take care of side effects.
2220 * Returns the previous value, to be passed to restore_lbr().
2221 */
2222 static int
2223reset_lbr(void)
2224{
2225 if (!curwin->w_p_lbr)
2226 return FALSE;
2227 // changing 'linebreak' may require w_virtcol to be updated
2228 curwin->w_p_lbr = FALSE;
2229 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2230 return TRUE;
2231}
2232
2233/*
2234 * Restore 'linebreak' and take care of side effects.
2235 */
2236 static void
2237restore_lbr(int lbr_saved)
2238{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002239 if (curwin->w_p_lbr || !lbr_saved)
2240 return;
2241
2242 // changing 'linebreak' may require w_virtcol to be updated
2243 curwin->w_p_lbr = TRUE;
2244 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002245}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002246#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002247
2248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 * prepare a few things for block mode yank/delete/tilde
2250 *
2251 * for delete:
2252 * - textlen includes the first/last char to be (partly) deleted
2253 * - start/endspaces is the number of columns that are taken by the
2254 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002255 * deleted.
2256 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * - textlen includes the first/last char to be wholly yanked
2258 * - start/endspaces is the number of columns of the first/last yanked char
2259 * that are to be yanked.
2260 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002261 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002262block_prep(
2263 oparg_T *oap,
2264 struct block_def *bdp,
2265 linenr_T lnum,
2266 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 int incr = 0;
2269 char_u *pend;
2270 char_u *pstart;
2271 char_u *line;
2272 char_u *prev_pstart;
2273 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002274 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002275#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002276 // Avoid a problem with unwanted linebreaks in block mode.
2277 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002278#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 bdp->startspaces = 0;
2281 bdp->endspaces = 0;
2282 bdp->textlen = 0;
2283 bdp->start_vcol = 0;
2284 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 bdp->is_short = FALSE;
2286 bdp->is_oneChar = FALSE;
2287 bdp->pre_whitesp = 0;
2288 bdp->pre_whitesp_c = 0;
2289 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 bdp->start_char_vcols = 0;
2291
2292 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002294 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2295 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002297 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002298 incr = lbr_chartabsize(&cts);
2299 cts.cts_vcol += incr;
2300 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 bdp->pre_whitesp += incr;
2303 bdp->pre_whitesp_c++;
2304 }
2305 else
2306 {
2307 bdp->pre_whitesp = 0;
2308 bdp->pre_whitesp_c = 0;
2309 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002310 prev_pstart = cts.cts_ptr;
2311 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002313 bdp->start_vcol = cts.cts_vcol;
2314 pstart = cts.cts_ptr;
2315 clear_chartabsize_arg(&cts);
2316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002318 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (!is_del || oap->op_type == OP_APPEND)
2323 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2324 }
2325 else
2326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002327 // notice: this converts partly selected Multibyte characters to
2328 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2330 if (is_del && bdp->startspaces)
2331 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2332 pend = pstart;
2333 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002334 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (oap->op_type == OP_INSERT)
2338 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2339 else if (oap->op_type == OP_APPEND)
2340 {
2341 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2342 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2343 }
2344 else
2345 {
2346 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2347 if (is_del && oap->op_type != OP_LSHIFT)
2348 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 // just putting the sum of those two into
2350 // bdp->startspaces doesn't work for Visual replace,
2351 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 bdp->startspaces = bdp->start_char_vcols
2353 - (bdp->start_vcol - oap->start_vcol);
2354 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2355 }
2356 }
2357 }
2358 else
2359 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002360 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2361 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002363 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002365 // count a tab for what it's worth (if list mode not on)
2366 prev_pend = cts.cts_ptr;
2367 incr = lbr_chartabsize_adv(&cts);
2368 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002370 bdp->end_vcol = cts.cts_vcol;
2371 pend = cts.cts_ptr;
2372 clear_chartabsize_arg(&cts);
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (bdp->end_vcol <= oap->end_vcol
2375 && (!is_del
2376 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002377 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 // Alternative: include spaces to fill up the block.
2381 // Disadvantage: can lead to trailing spaces when the line is
2382 // short where the text is put
2383 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (oap->op_type == OP_APPEND || virtual_op)
2385 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002386 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 else if (bdp->end_vcol > oap->end_vcol)
2391 {
2392 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2393 if (!is_del && bdp->endspaces)
2394 {
2395 bdp->endspaces = incr - bdp->endspaces;
2396 if (pend != pstart)
2397 pend = prev_pend;
2398 }
2399 }
2400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (is_del && bdp->startspaces)
2403 pstart = prev_pstart;
2404 bdp->textlen = (int)(pend - pstart);
2405 }
2406 bdp->textcol = (colnr_T) (pstart - line);
2407 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002408#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002409 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002414 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417op_addsub(
2418 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002419 linenr_T Prenum1, // Amount of add/subtract
2420 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002422 pos_T pos;
2423 struct block_def bd;
2424 int change_cnt = 0;
2425 linenr_T amount = Prenum1;
2426
Bram Moolenaar6b731882018-11-16 20:54:47 +01002427 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002428 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002429 // the call to changed_lines().
2430#ifdef FEAT_FOLDING
2431 disable_fold_update++;
2432#endif
2433
Bram Moolenaard79e5502016-01-10 22:13:02 +01002434 if (!VIsual_active)
2435 {
2436 pos = curwin->w_cursor;
2437 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002438 {
2439#ifdef FEAT_FOLDING
2440 disable_fold_update--;
2441#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002442 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002443 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002444 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002445#ifdef FEAT_FOLDING
2446 disable_fold_update--;
2447#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002448 if (change_cnt)
2449 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2450 }
2451 else
2452 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002453 int one_change;
2454 int length;
2455 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002456
2457 if (u_save((linenr_T)(oap->start.lnum - 1),
2458 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002459 {
2460#ifdef FEAT_FOLDING
2461 disable_fold_update--;
2462#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002463 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002464 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002465
2466 pos = oap->start;
2467 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2468 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002469 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002470 {
2471 block_prep(oap, &bd, pos.lnum, FALSE);
2472 pos.col = bd.textcol;
2473 length = bd.textlen;
2474 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002475 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002476 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002477 curwin->w_cursor.col = 0;
2478 pos.col = 0;
2479 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2480 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002482 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002483 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002484 dec(&(oap->end));
2485 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2486 pos.col = 0;
2487 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002488 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002489 pos.col += oap->start.col;
2490 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002491 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002492 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002493 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002494 length = (int)STRLEN(ml_get(oap->end.lnum));
2495 if (oap->end.col >= length)
2496 oap->end.col = length - 1;
2497 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002498 }
2499 }
2500 one_change = do_addsub(oap->op_type, &pos, length, amount);
2501 if (one_change)
2502 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002503 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002504 if (change_cnt == 0)
2505 startpos = curbuf->b_op_start;
2506 ++change_cnt;
2507 }
2508
2509#ifdef FEAT_NETBEANS_INTG
2510 if (netbeans_active() && one_change)
2511 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002512 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002513
2514 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002515 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002516 netbeans_inserted(curbuf, pos.lnum, pos.col,
2517 &ptr[pos.col], length);
2518 }
2519#endif
2520 if (g_cmd && one_change)
2521 amount += Prenum1;
2522 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002523
2524#ifdef FEAT_FOLDING
2525 disable_fold_update--;
2526#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002527 if (change_cnt)
2528 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2529
2530 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002532 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002534 // Set '[ mark if something changed. Keep the last end
2535 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002536 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002537 curbuf->b_op_start = startpos;
2538
2539 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002540 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002541 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002542 }
2543}
2544
2545/*
2546 * Add or subtract 'Prenum1' from a number in a line
2547 * op_type is OP_NR_ADD or OP_NR_SUB
2548 *
2549 * Returns TRUE if some character was changed.
2550 */
2551 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002552do_addsub(
2553 int op_type,
2554 pos_T *pos,
2555 int length,
2556 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002557{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 int col;
2559 char_u *buf1;
2560 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002561 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2562 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002563 uvarnumber_T n;
2564 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 char_u *ptr;
2566 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002568 int do_hex;
2569 int do_oct;
2570 int do_bin;
2571 int do_alpha;
2572 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002575 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002576 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002577 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002578 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002579 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002580 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002581 pos_T startpos;
2582 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002583 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002585 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2586 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2587 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2588 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2589 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002591 if (virtual_active())
2592 {
2593 save_coladd = pos->coladd;
2594 pos->coladd = 0;
2595 }
2596
Bram Moolenaard79e5502016-01-10 22:13:02 +01002597 curwin->w_cursor = *pos;
2598 ptr = ml_get(pos->lnum);
2599 col = pos->col;
2600
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002601 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002602 goto theend;
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 /*
2605 * First check if we are on a hexadecimal number, after the "0x".
2606 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002607 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002609 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002610 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002611 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002612 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002613 if (has_mbyte)
2614 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002615 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002616
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002617 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002618 while (col > 0 && vim_isxdigit(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 Moolenaar887c1fe2016-01-02 17:56:35 +01002624
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002625 if ( do_bin
2626 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002627 && ! ((col > 0
2628 && (ptr[col] == 'X'
2629 || ptr[col] == 'x')
2630 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002631 && (!has_mbyte ||
2632 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002633 && vim_isxdigit(ptr[col + 1]))))
2634 {
2635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002637
Bram Moolenaard79e5502016-01-10 22:13:02 +01002638 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002639
2640 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002641 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002642 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002643 if (has_mbyte)
2644 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002645 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002646 }
2647
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002648 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002649 && col > 0
2650 && (ptr[col] == 'X'
2651 || ptr[col] == 'x')
2652 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002653 && (!has_mbyte ||
2654 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002655 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002656 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002657 && col > 0
2658 && (ptr[col] == 'B'
2659 || ptr[col] == 'b')
2660 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002661 && (!has_mbyte ||
2662 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002663 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002665 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002666 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002667 if (has_mbyte)
2668 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002669 }
2670 else
2671 {
2672 /*
2673 * Search forward and then backward to find the start of number.
2674 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002675 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002676
2677 while (ptr[col] != NUL
2678 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002679 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002680 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002681
2682 while (col > 0
2683 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002684 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002685 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002686 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002687 if (has_mbyte)
2688 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002689 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002690 }
2691 }
2692
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002693 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002694 {
2695 while (ptr[col] != NUL && length > 0
2696 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002697 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002698 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002699 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002700
2701 col += mb_len;
2702 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002703 }
2704
2705 if (length == 0)
2706 goto theend;
2707
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002708 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002709 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2710 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002711 {
2712 negative = TRUE;
2713 was_positive = FALSE;
2714 }
2715 }
2716
2717 /*
2718 * If a number was found, and saving for undo works, replace the number.
2719 */
2720 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002721 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002722 {
2723 beep_flush();
2724 goto theend;
2725 }
2726
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002727 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002730 if (op_type == OP_NR_SUB)
2731 {
2732 if (CharOrd(firstdigit) < Prenum1)
2733 {
2734 if (isupper(firstdigit))
2735 firstdigit = 'A';
2736 else
2737 firstdigit = 'a';
2738 }
2739 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002740 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002741 }
2742 else
2743 {
2744 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2745 {
2746 if (isupper(firstdigit))
2747 firstdigit = 'Z';
2748 else
2749 firstdigit = 'z';
2750 }
2751 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002752 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002753 }
2754 curwin->w_cursor.col = col;
2755 if (!did_change)
2756 startpos = curwin->w_cursor;
2757 did_change = TRUE;
2758 (void)del_char(FALSE);
2759 ins_char(firstdigit);
2760 endpos = curwin->w_cursor;
2761 curwin->w_cursor.col = col;
2762 }
2763 else
2764 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002765 pos_T save_pos;
2766 int i;
2767
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002768 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002769 && (!has_mbyte ||
2770 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002771 && !visual
2772 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002773 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002775 --col;
2776 negative = TRUE;
2777 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002779 if (visual && VIsual_mode != 'V')
2780 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2781 ? (int)STRLEN(ptr) - col
2782 : length);
2783
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002784 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002785 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002786 0 + (do_bin ? STR2NR_BIN : 0)
2787 + (do_oct ? STR2NR_OCT : 0)
2788 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002789 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002792 if (pre && negative)
2793 {
2794 ++col;
2795 --length;
2796 negative = FALSE;
2797 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002798 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002799 subtract = FALSE;
2800 if (op_type == OP_NR_SUB)
2801 subtract ^= TRUE;
2802 if (negative)
2803 subtract ^= TRUE;
2804
2805 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002806 if (!overflow) // if number is too big don't add/subtract
2807 {
2808 if (subtract)
2809 n -= (uvarnumber_T)Prenum1;
2810 else
2811 n += (uvarnumber_T)Prenum1;
2812 }
2813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002815 if (!pre)
2816 {
2817 if (subtract)
2818 {
2819 if (n > oldn)
2820 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002821 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002822 negative ^= TRUE;
2823 }
2824 }
2825 else
2826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002828 if (n < oldn)
2829 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002830 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002831 negative ^= TRUE;
2832 }
2833 }
2834 if (n == 0)
2835 negative = FALSE;
2836 }
2837
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002838 if (do_unsigned && negative)
2839 {
2840 if (subtract)
2841 // sticking at zero.
2842 n = (uvarnumber_T)0;
2843 else
2844 // sticking at 2^64 - 1.
2845 n = (uvarnumber_T)(-1);
2846 negative = FALSE;
2847 }
2848
Bram Moolenaard79e5502016-01-10 22:13:02 +01002849 if (visual && !was_positive && !negative && col > 0)
2850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002852 col--;
2853 length++;
2854 }
2855
2856 /*
2857 * Delete the old number.
2858 */
2859 curwin->w_cursor.col = col;
2860 if (!did_change)
2861 startpos = curwin->w_cursor;
2862 did_change = TRUE;
2863 todel = length;
2864 c = gchar_cursor();
2865 /*
2866 * Don't include the '-' in the length, only the length of the
2867 * part after it is kept the same.
2868 */
2869 if (c == '-')
2870 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002871
2872 save_pos = curwin->w_cursor;
2873 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002874 {
2875 if (c < 0x100 && isalpha(c))
2876 {
2877 if (isupper(c))
2878 hexupper = TRUE;
2879 else
2880 hexupper = FALSE;
2881 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002882 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002883 c = gchar_cursor();
2884 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002885 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002886
2887 /*
2888 * Prepare the leading characters in buf1[].
2889 * When there are many leading zeros it could be very long.
2890 * Allocate a bit too much.
2891 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002892 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002893 if (buf1 == NULL)
2894 goto theend;
2895 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002896 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002897 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002898 if (pre)
2899 {
2900 *ptr++ = '0';
2901 --length;
2902 }
2903 if (pre == 'b' || pre == 'B' ||
2904 pre == 'x' || pre == 'X')
2905 {
2906 *ptr++ = pre;
2907 --length;
2908 }
2909
2910 /*
2911 * Put the number characters in buf2[].
2912 */
2913 if (pre == 'b' || pre == 'B')
2914 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002915 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002916 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002919 for (bit = bits; bit > 0; bit--)
2920 if ((n >> (bit - 1)) & 0x1) break;
2921
2922 for (i = 0; bit > 0; bit--)
2923 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2924
2925 buf2[i] = '\0';
2926 }
2927 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002928 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002929 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002930 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002931 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002932 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002933 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002934 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002935 length -= (int)STRLEN(buf2);
2936
2937 /*
2938 * Adjust number of zeros to the new number of digits, so the
2939 * total length of the number remains the same.
2940 * Don't do this when
2941 * the result may look like an octal number.
2942 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002943 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002944 while (length-- > 0)
2945 *ptr++ = '0';
2946 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002947
Bram Moolenaard79e5502016-01-10 22:13:02 +01002948 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002949
2950 // Insert just after the first character to be removed, so that any
2951 // text properties will be adjusted. Then delete the old number
2952 // afterwards.
2953 save_pos = curwin->w_cursor;
2954 if (todel > 0)
2955 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002957 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002958
2959 // del_char() will also mark line needing displaying
2960 if (todel > 0)
2961 {
2962 int bytes_after = (int)STRLEN(ml_get_curline())
2963 - curwin->w_cursor.col;
2964
2965 // Delete the one character before the insert.
2966 curwin->w_cursor = save_pos;
2967 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002968 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2969 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002970 --todel;
2971 }
2972 while (todel-- > 0)
2973 (void)del_char(FALSE);
2974
Bram Moolenaard79e5502016-01-10 22:13:02 +01002975 endpos = curwin->w_cursor;
2976 if (did_change && curwin->w_cursor.col)
2977 --curwin->w_cursor.col;
2978 }
2979
Bram Moolenaare1004402020-10-24 20:49:43 +02002980 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002981 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002983 curbuf->b_op_start = startpos;
2984 curbuf->b_op_end = endpos;
2985 if (curbuf->b_op_end.col > 0)
2986 --curbuf->b_op_end.col;
2987 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002988
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002989theend:
2990 if (visual)
2991 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002992 else if (did_change)
2993 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002994 else if (virtual_active())
2995 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002996
Bram Moolenaard79e5502016-01-10 22:13:02 +01002997 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998}
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003001clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003003 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004}
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003007 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 *
3009 * "Words" are counted by looking for boundaries between non-space and
3010 * space characters. (it seems to produce results that match 'wc'.)
3011 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003012 * Return value is byte count; word count for the line is added to "*wc".
3013 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 *
3015 * The function will only examine the first "limit" characters in the
3016 * line, stopping if it encounters an end-of-line (NUL byte). In that
3017 * case, eol_size will be added to the character count to account for
3018 * the size of the EOL character.
3019 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003020 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003021line_count_info(
3022 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003023 varnumber_T *wc,
3024 varnumber_T *cc,
3025 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003026 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003028 varnumber_T i;
3029 varnumber_T words = 0;
3030 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 int is_word = 0;
3032
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003033 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 {
3035 if (is_word)
3036 {
3037 if (vim_isspace(line[i]))
3038 {
3039 words++;
3040 is_word = 0;
3041 }
3042 }
3043 else if (!vim_isspace(line[i]))
3044 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003045 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003046 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
3048
3049 if (is_word)
3050 words++;
3051 *wc += words;
3052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003053 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003054 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003057 chars += eol_size;
3058 }
3059 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return i;
3061}
3062
3063/*
3064 * Give some info about the position of the cursor (for "g CTRL-G").
3065 * In Visual mode, give some info about the selected region. (In this case,
3066 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003067 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 */
3069 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003073 char_u buf1[50];
3074 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003076 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003077 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003078 varnumber_T byte_count_cursor = 0;
3079 varnumber_T char_count = 0;
3080 varnumber_T char_count_cursor = 0;
3081 varnumber_T word_count = 0;
3082 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003083 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003084 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 long line_count_selected = 0;
3086 pos_T min_pos, max_pos;
3087 oparg_T oparg;
3088 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 /*
3091 * Compute the length of the file in characters.
3092 */
3093 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3094 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003095 if (dict == NULL)
3096 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003097 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003098 return;
3099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101 else
3102 {
3103 if (get_fileformat(curbuf) == EOL_DOS)
3104 eol_size = 2;
3105 else
3106 eol_size = 1;
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (VIsual_active)
3109 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003110 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 min_pos = VIsual;
3113 max_pos = curwin->w_cursor;
3114 }
3115 else
3116 {
3117 min_pos = curwin->w_cursor;
3118 max_pos = VIsual;
3119 }
3120 if (*p_sel == 'e' && max_pos.col > 0)
3121 --max_pos.col;
3122
3123 if (VIsual_mode == Ctrl_V)
3124 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003125#ifdef FEAT_LINEBREAK
3126 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003127 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003128
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003130 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003131 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 oparg.is_VIsual = 1;
3134 oparg.block_mode = TRUE;
3135 oparg.op_type = OP_NOP;
3136 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003137 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003138#ifdef FEAT_LINEBREAK
3139 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003140 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003141#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003142 if (curwin->w_curswant == MAXCOL)
3143 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003144 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (oparg.end_vcol < oparg.start_vcol)
3146 {
3147 oparg.end_vcol += oparg.start_vcol;
3148 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3149 oparg.end_vcol -= oparg.start_vcol;
3150 }
3151 }
3152 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3156 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003158 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
3160 ui_breakcheck();
3161 if (got_int)
3162 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003163 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
3165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003166 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (VIsual_active
3168 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3169 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003170 char_u *s = NULL;
3171 long len = 0L;
3172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 switch (VIsual_mode)
3174 {
3175 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003179 s = bd.textstart;
3180 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003183 s = ml_get(lnum);
3184 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 break;
3186 case 'v':
3187 {
3188 colnr_T start_col = (lnum == min_pos.lnum)
3189 ? min_pos.col : 0;
3190 colnr_T end_col = (lnum == max_pos.lnum)
3191 ? max_pos.col - start_col + 1 : MAXCOL;
3192
Bram Moolenaardef9e822004-12-31 20:58:58 +00003193 s = ml_get(lnum) + start_col;
3194 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 break;
3197 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003198 if (s != NULL)
3199 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003200 byte_count_cursor += line_count_info(s, &word_count_cursor,
3201 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003202 if (lnum == curbuf->b_ml.ml_line_count
3203 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003204 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003205 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003206 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
3209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (lnum == curwin->w_cursor.lnum)
3213 {
3214 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003215 char_count_cursor += char_count;
3216 byte_count_cursor = byte_count +
3217 line_count_info(ml_get(lnum),
3218 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003219 (varnumber_T)(curwin->w_cursor.col + 1),
3220 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
3222 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003223 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003224 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003225 &char_count, (varnumber_T)MAXCOL,
3226 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003230 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003231 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
Bram Moolenaared767a22016-01-03 22:49:16 +01003233 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003235 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003237 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3238 {
3239 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3240 &max_pos.col);
3241 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3242 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3243 }
3244 else
3245 buf1[0] = NUL;
3246
3247 if (char_count_cursor == byte_count_cursor
3248 && char_count == byte_count)
3249 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003250 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003251 buf1, line_count_selected,
3252 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003253 word_count_cursor,
3254 word_count,
3255 byte_count_cursor,
3256 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003257 else
3258 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003259 _("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 +01003260 buf1, line_count_selected,
3261 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003262 word_count_cursor,
3263 word_count,
3264 char_count_cursor,
3265 char_count,
3266 byte_count_cursor,
3267 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003270 {
3271 p = ml_get_curline();
3272 validate_virtcol();
3273 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3274 (int)curwin->w_virtcol + 1);
3275 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003276 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
Bram Moolenaared767a22016-01-03 22:49:16 +01003278 if (char_count_cursor == byte_count_cursor
3279 && char_count == byte_count)
3280 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003281 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003282 (char *)buf1, (char *)buf2,
3283 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003285 word_count_cursor, word_count,
3286 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003287 else
3288 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003289 _("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 +01003290 (char *)buf1, (char *)buf2,
3291 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003292 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003293 word_count_cursor, word_count,
3294 char_count_cursor, char_count,
3295 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003296 }
3297 }
3298
Bram Moolenaared767a22016-01-03 22:49:16 +01003299 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003300 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003301 {
3302 size_t len = STRLEN(IObuff);
3303
3304 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003305 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003306 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003307 if (dict == NULL)
3308 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003309 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003310 p = p_shm;
3311 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003312 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003313 p_shm = p;
3314 }
3315 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003316#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003317 if (dict != NULL)
3318 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003319 dict_add_number(dict, "words", word_count);
3320 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003321 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003322 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3323 byte_count_cursor);
3324 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3325 char_count_cursor);
3326 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3327 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003331
3332/*
3333 * Handle indent and format operators and visual mode ":".
3334 */
3335 static void
3336op_colon(oparg_T *oap)
3337{
3338 stuffcharReadbuff(':');
3339 if (oap->is_VIsual)
3340 stuffReadbuff((char_u *)"'<,'>");
3341 else
3342 {
3343 // Make the range look nice, so it can be repeated.
3344 if (oap->start.lnum == curwin->w_cursor.lnum)
3345 stuffcharReadbuff('.');
3346 else
3347 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003348
3349#ifdef FEAT_FOLDING
3350 // When using !! on a closed fold the range ".!" works best to operate
3351 // on, it will be made the whole closed fold later.
3352 linenr_T endOfStartFold = oap->start.lnum;
3353 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3354#endif
3355 if (oap->end.lnum != oap->start.lnum
3356#ifdef FEAT_FOLDING
3357 && oap->end.lnum != endOfStartFold
3358#endif
3359 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003360 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003361 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003362 stuffcharReadbuff(',');
3363 if (oap->end.lnum == curwin->w_cursor.lnum)
3364 stuffcharReadbuff('.');
3365 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3366 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003367 else if (oap->start.lnum == curwin->w_cursor.lnum
3368#ifdef FEAT_FOLDING
3369 // do not use ".+number" for a closed fold, it would count
3370 // folded lines twice
3371 && !hasFolding(oap->end.lnum, NULL, NULL)
3372#endif
3373 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003374 {
3375 stuffReadbuff((char_u *)".+");
3376 stuffnumReadbuff((long)oap->line_count - 1);
3377 }
3378 else
3379 stuffnumReadbuff((long)oap->end.lnum);
3380 }
3381 }
3382 if (oap->op_type != OP_COLON)
3383 stuffReadbuff((char_u *)"!");
3384 if (oap->op_type == OP_INDENT)
3385 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003386 if (*get_equalprg() == NUL)
3387 stuffReadbuff((char_u *)"indent");
3388 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003389 stuffReadbuff(get_equalprg());
3390 stuffReadbuff((char_u *)"\n");
3391 }
3392 else if (oap->op_type == OP_FORMAT)
3393 {
3394 if (*curbuf->b_p_fp != NUL)
3395 stuffReadbuff(curbuf->b_p_fp);
3396 else if (*p_fp != NUL)
3397 stuffReadbuff(p_fp);
3398 else
3399 stuffReadbuff((char_u *)"fmt");
3400 stuffReadbuff((char_u *)"\n']");
3401 }
3402
3403 // do_cmdline() does the rest
3404}
3405
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003406// callback function for 'operatorfunc'
3407static callback_T opfunc_cb;
3408
3409/*
3410 * Process the 'operatorfunc' option value.
3411 * Returns OK or FAIL.
3412 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003413 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003414did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003415{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003416 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3417 return e_invalid_argument;
3418
3419 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003420}
3421
3422#if defined(EXITFREE) || defined(PROTO)
3423 void
3424free_operatorfunc_option(void)
3425{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003426# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003427 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003428# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003429}
3430#endif
3431
Dominique Pelle748b3082022-01-08 12:41:16 +00003432#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003433/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003434 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003435 * garbage collected.
3436 */
3437 int
3438set_ref_in_opfunc(int copyID UNUSED)
3439{
3440 int abort = FALSE;
3441
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003442 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003443
3444 return abort;
3445}
Dominique Pelle748b3082022-01-08 12:41:16 +00003446#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003447
3448/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003449 * Handle the "g@" operator: call 'operatorfunc'.
3450 */
3451 static void
3452op_function(oparg_T *oap UNUSED)
3453{
3454#ifdef FEAT_EVAL
3455 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003456 pos_T orig_start = curbuf->b_op_start;
3457 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003458 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003459
3460 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003461 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003462 else
3463 {
3464 // Set '[ and '] marks to text to be operated on.
3465 curbuf->b_op_start = oap->start;
3466 curbuf->b_op_end = oap->end;
3467 if (oap->motion_type != MLINE && !oap->inclusive)
3468 // Exclude the end position.
3469 decl(&curbuf->b_op_end);
3470
3471 argv[0].v_type = VAR_STRING;
3472 if (oap->block_mode)
3473 argv[0].vval.v_string = (char_u *)"block";
3474 else if (oap->motion_type == MLINE)
3475 argv[0].vval.v_string = (char_u *)"line";
3476 else
3477 argv[0].vval.v_string = (char_u *)"char";
3478 argv[1].v_type = VAR_UNKNOWN;
3479
3480 // Reset virtual_op so that 'virtualedit' can be changed in the
3481 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003482 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003483 virtual_op = MAYBE;
3484
naohiro ono75c30e92021-10-19 11:15:41 +01003485 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003486 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003487 finish_op = FALSE;
3488
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003489 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3490 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003491
3492 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003493 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003494 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003495 {
3496 curbuf->b_op_start = orig_start;
3497 curbuf->b_op_end = orig_end;
3498 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003499 }
3500#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003501 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003502#endif
3503}
3504
3505/*
3506 * Calculate start/end virtual columns for operating in block mode.
3507 */
3508 static void
3509get_op_vcol(
3510 oparg_T *oap,
3511 colnr_T redo_VIsual_vcol,
3512 int initial) // when TRUE adjust position for 'selectmode'
3513{
3514 colnr_T start, end;
3515
3516 if (VIsual_mode != Ctrl_V
3517 || (!initial && oap->end.col < curwin->w_width))
3518 return;
3519
3520 oap->block_mode = TRUE;
3521
3522 // prevent from moving onto a trail byte
3523 if (has_mbyte)
3524 mb_adjustpos(curwin->w_buffer, &oap->end);
3525
3526 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3527
3528 if (!redo_VIsual_busy)
3529 {
3530 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3531
3532 if (start < oap->start_vcol)
3533 oap->start_vcol = start;
3534 if (end > oap->end_vcol)
3535 {
3536 if (initial && *p_sel == 'e' && start >= 1
3537 && start - 1 >= oap->end_vcol)
3538 oap->end_vcol = start - 1;
3539 else
3540 oap->end_vcol = end;
3541 }
3542 }
3543
3544 // if '$' was used, get oap->end_vcol from longest line
3545 if (curwin->w_curswant == MAXCOL)
3546 {
3547 curwin->w_cursor.col = MAXCOL;
3548 oap->end_vcol = 0;
3549 for (curwin->w_cursor.lnum = oap->start.lnum;
3550 curwin->w_cursor.lnum <= oap->end.lnum;
3551 ++curwin->w_cursor.lnum)
3552 {
3553 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3554 if (end > oap->end_vcol)
3555 oap->end_vcol = end;
3556 }
3557 }
3558 else if (redo_VIsual_busy)
3559 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3560 // Correct oap->end.col and oap->start.col to be the
3561 // upper-left and lower-right corner of the block area.
3562 //
3563 // (Actually, this does convert column positions into character
3564 // positions)
3565 curwin->w_cursor.lnum = oap->end.lnum;
3566 coladvance(oap->end_vcol);
3567 oap->end = curwin->w_cursor;
3568
3569 curwin->w_cursor = oap->start;
3570 coladvance(oap->start_vcol);
3571 oap->start = curwin->w_cursor;
3572}
3573
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003574// Information for redoing the previous Visual selection.
3575typedef struct {
3576 int rv_mode; // 'v', 'V', or Ctrl-V
3577 linenr_T rv_line_count; // number of lines
3578 colnr_T rv_vcol; // number of cols or end column
3579 long rv_count; // count for Visual operator
3580 int rv_arg; // extra argument
3581} redo_VIsual_T;
3582
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003583 static int
3584is_ex_cmdchar(cmdarg_T *cap)
3585{
3586 return cap->cmdchar == ':'
3587 || cap->cmdchar == K_COMMAND
3588 || cap->cmdchar == K_SCRIPT_COMMAND;
3589}
3590
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003591/*
3592 * Handle an operator after Visual mode or when the movement is finished.
3593 * "gui_yank" is true when yanking text for the clipboard.
3594 */
3595 void
3596do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3597{
3598 oparg_T *oap = cap->oap;
3599 pos_T old_cursor;
3600 int empty_region_error;
3601 int restart_edit_save;
3602#ifdef FEAT_LINEBREAK
3603 int lbr_saved = curwin->w_p_lbr;
3604#endif
3605
3606 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003607 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3608
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003609 int include_line_break = FALSE;
3610
3611#if defined(FEAT_CLIPBOARD)
3612 // Yank the visual area into the GUI selection register before we operate
3613 // on it and lose it forever.
3614 // Don't do it if a specific register was specified, so that ""x"*P works.
3615 // This could call do_pending_operator() recursively, but that's OK
3616 // because gui_yank will be TRUE for the nested call.
3617 if ((clip_star.available || clip_plus.available)
3618 && oap->op_type != OP_NOP
3619 && !gui_yank
3620 && VIsual_active
3621 && !redo_VIsual_busy
3622 && oap->regname == 0)
3623 clip_auto_select();
3624#endif
3625 old_cursor = curwin->w_cursor;
3626
3627 // If an operation is pending, handle it...
3628 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3629 {
3630 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3631 // for the clipboard.
3632 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3633
3634#ifdef FEAT_LINEBREAK
3635 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003636 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003637#endif
3638 oap->is_VIsual = VIsual_active;
3639 if (oap->motion_force == 'V')
3640 oap->motion_type = MLINE;
3641 else if (oap->motion_force == 'v')
3642 {
3643 // If the motion was linewise, "inclusive" will not have been set.
3644 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3645 if (oap->motion_type == MLINE)
3646 oap->inclusive = FALSE;
3647 // If the motion already was characterwise, toggle "inclusive"
3648 else if (oap->motion_type == MCHAR)
3649 oap->inclusive = !oap->inclusive;
3650 oap->motion_type = MCHAR;
3651 }
3652 else if (oap->motion_force == Ctrl_V)
3653 {
3654 // Change line- or characterwise motion into Visual block mode.
3655 if (!VIsual_active)
3656 {
3657 VIsual_active = TRUE;
3658 VIsual = oap->start;
3659 }
3660 VIsual_mode = Ctrl_V;
3661 VIsual_select = FALSE;
3662 VIsual_reselect = FALSE;
3663 }
3664
3665 // Only redo yank when 'y' flag is in 'cpoptions'.
3666 // Never redo "zf" (define fold).
3667 if ((redo_yank || oap->op_type != OP_YANK)
3668 && ((!VIsual_active || oap->motion_force)
3669 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003670 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003671 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003672 && cap->cmdchar != 'D'
3673#ifdef FEAT_FOLDING
3674 && oap->op_type != OP_FOLD
3675 && oap->op_type != OP_FOLDOPEN
3676 && oap->op_type != OP_FOLDOPENREC
3677 && oap->op_type != OP_FOLDCLOSE
3678 && oap->op_type != OP_FOLDCLOSEREC
3679 && oap->op_type != OP_FOLDDEL
3680 && oap->op_type != OP_FOLDDELREC
3681#endif
3682 )
3683 {
3684 prep_redo(oap->regname, cap->count0,
3685 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3686 oap->motion_force, cap->cmdchar, cap->nchar);
3687 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3688 {
3689 // If 'cpoptions' does not contain 'r', insert the search
3690 // pattern to really repeat the same command.
3691 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3692 AppendToRedobuffLit(cap->searchbuf, -1);
3693 AppendToRedobuff(NL_STR);
3694 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003695 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003696 {
3697 // do_cmdline() has stored the first typed line in
3698 // "repeat_cmdline". When several lines are typed repeating
3699 // won't be possible.
3700 if (repeat_cmdline == NULL)
3701 ResetRedobuff();
3702 else
3703 {
3704 AppendToRedobuffLit(repeat_cmdline, -1);
3705 AppendToRedobuff(NL_STR);
3706 VIM_CLEAR(repeat_cmdline);
3707 }
3708 }
3709 }
3710
3711 if (redo_VIsual_busy)
3712 {
3713 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003714 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003715 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003716 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003717 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3718 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003719 VIsual_mode = redo_VIsual.rv_mode;
3720 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003721 {
3722 if (VIsual_mode == 'v')
3723 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003724 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003725 {
3726 validate_virtcol();
3727 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003728 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003729 }
3730 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003731 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003732 }
3733 else
3734 {
3735 curwin->w_curswant = MAXCOL;
3736 }
3737 coladvance(curwin->w_curswant);
3738 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003739 cap->count0 = redo_VIsual.rv_count;
3740 if (redo_VIsual.rv_count != 0)
3741 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003742 else
3743 cap->count1 = 1;
3744 }
3745 else if (VIsual_active)
3746 {
3747 if (!gui_yank)
3748 {
3749 // Save the current VIsual area for '< and '> marks, and "gv"
3750 curbuf->b_visual.vi_start = VIsual;
3751 curbuf->b_visual.vi_end = curwin->w_cursor;
3752 curbuf->b_visual.vi_mode = VIsual_mode;
3753 restore_visual_mode();
3754 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003755#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003756 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003757#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003758 }
3759
3760 // In Select mode, a linewise selection is operated upon like a
3761 // characterwise selection.
3762 // Special case: gH<Del> deletes the last line.
3763 if (VIsual_select && VIsual_mode == 'V'
3764 && cap->oap->op_type != OP_DELETE)
3765 {
3766 if (LT_POS(VIsual, curwin->w_cursor))
3767 {
3768 VIsual.col = 0;
3769 curwin->w_cursor.col =
3770 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3771 }
3772 else
3773 {
3774 curwin->w_cursor.col = 0;
3775 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3776 }
3777 VIsual_mode = 'v';
3778 }
3779 // If 'selection' is "exclusive", backup one character for
3780 // charwise selections.
3781 else if (VIsual_mode == 'v')
3782 include_line_break = unadjust_for_sel();
3783
3784 oap->start = VIsual;
3785 if (VIsual_mode == 'V')
3786 {
3787 oap->start.col = 0;
3788 oap->start.coladd = 0;
3789 }
3790 }
3791
3792 // Set oap->start to the first position of the operated text, oap->end
3793 // to the end of the operated text. w_cursor is equal to oap->start.
3794 if (LT_POS(oap->start, curwin->w_cursor))
3795 {
3796#ifdef FEAT_FOLDING
3797 // Include folded lines completely.
3798 if (!VIsual_active)
3799 {
3800 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3801 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003802 if ((curwin->w_cursor.col > 0 || oap->inclusive
3803 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003804 && hasFolding(curwin->w_cursor.lnum, NULL,
3805 &curwin->w_cursor.lnum))
3806 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3807 }
3808#endif
3809 oap->end = curwin->w_cursor;
3810 curwin->w_cursor = oap->start;
3811
3812 // w_virtcol may have been updated; if the cursor goes back to its
3813 // previous position w_virtcol becomes invalid and isn't updated
3814 // automatically.
3815 curwin->w_valid &= ~VALID_VIRTCOL;
3816 }
3817 else
3818 {
3819#ifdef FEAT_FOLDING
3820 // Include folded lines completely.
3821 if (!VIsual_active && oap->motion_type == MLINE)
3822 {
3823 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3824 NULL))
3825 curwin->w_cursor.col = 0;
3826 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3827 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3828 }
3829#endif
3830 oap->end = oap->start;
3831 oap->start = curwin->w_cursor;
3832 }
3833
3834 // Just in case lines were deleted that make the position invalid.
3835 check_pos(curwin->w_buffer, &oap->end);
3836 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3837
3838 // Set "virtual_op" before resetting VIsual_active.
3839 virtual_op = virtual_active();
3840
3841 if (VIsual_active || redo_VIsual_busy)
3842 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003843 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003844
3845 if (!redo_VIsual_busy && !gui_yank)
3846 {
3847 // Prepare to reselect and redo Visual: this is based on the
3848 // size of the Visual text
3849 resel_VIsual_mode = VIsual_mode;
3850 if (curwin->w_curswant == MAXCOL)
3851 resel_VIsual_vcol = MAXCOL;
3852 else
3853 {
3854 if (VIsual_mode != Ctrl_V)
3855 getvvcol(curwin, &(oap->end),
3856 NULL, NULL, &oap->end_vcol);
3857 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3858 {
3859 if (VIsual_mode != Ctrl_V)
3860 getvvcol(curwin, &(oap->start),
3861 &oap->start_vcol, NULL, NULL);
3862 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3863 }
3864 else
3865 resel_VIsual_vcol = oap->end_vcol;
3866 }
3867 resel_VIsual_line_count = oap->line_count;
3868 }
3869
3870 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3871 if ((redo_yank || oap->op_type != OP_YANK)
3872 && oap->op_type != OP_COLON
3873#ifdef FEAT_FOLDING
3874 && oap->op_type != OP_FOLD
3875 && oap->op_type != OP_FOLDOPEN
3876 && oap->op_type != OP_FOLDOPENREC
3877 && oap->op_type != OP_FOLDCLOSE
3878 && oap->op_type != OP_FOLDCLOSEREC
3879 && oap->op_type != OP_FOLDDEL
3880 && oap->op_type != OP_FOLDDELREC
3881#endif
3882 && oap->motion_force == NUL
3883 )
3884 {
3885 // Prepare for redoing. Only use the nchar field for "r",
3886 // otherwise it might be the second char of the operator.
3887 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3888 || cap->nchar == 'N'))
3889 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003890 get_op_char(oap->op_type),
3891 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003892 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003893 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003894 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003895 int opchar = get_op_char(oap->op_type);
3896 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003897 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3898
3899 // reverse what nv_replace() did
3900 if (nchar == REPLACE_CR_NCHAR)
3901 nchar = CAR;
3902 else if (nchar == REPLACE_NL_NCHAR)
3903 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003904
3905 if (opchar == 'g' && extra_opchar == '@')
3906 // also repeat the count for 'operatorfunc'
3907 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3908 cap->count0, opchar, extra_opchar, nchar);
3909 else
3910 prep_redo(oap->regname, 0L, NUL, 'v',
3911 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003912 }
3913 if (!redo_VIsual_busy)
3914 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003915 redo_VIsual.rv_mode = resel_VIsual_mode;
3916 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3917 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3918 redo_VIsual.rv_count = cap->count0;
3919 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003920 }
3921 }
3922
3923 // oap->inclusive defaults to TRUE.
3924 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3925 // FALSE. This makes "d}P" and "v}dP" work the same.
3926 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3927 oap->inclusive = TRUE;
3928 if (VIsual_mode == 'V')
3929 oap->motion_type = MLINE;
3930 else
3931 {
3932 oap->motion_type = MCHAR;
3933 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3934 && (include_line_break || !virtual_op))
3935 {
3936 oap->inclusive = FALSE;
3937 // Try to include the newline, unless it's an operator
3938 // that works on lines only.
3939 if (*p_sel != 'o'
3940 && !op_on_lines(oap->op_type)
3941 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3942 {
3943 ++oap->end.lnum;
3944 oap->end.col = 0;
3945 oap->end.coladd = 0;
3946 ++oap->line_count;
3947 }
3948 }
3949 }
3950
3951 redo_VIsual_busy = FALSE;
3952
3953 // Switch Visual off now, so screen updating does
3954 // not show inverted text when the screen is redrawn.
3955 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3956 // no screen redraw, so it is done here to remove the inverted
3957 // part.
3958 if (!gui_yank)
3959 {
3960 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003961 setmouse();
3962 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003963 may_clear_cmdline();
3964 if ((oap->op_type == OP_YANK
3965 || oap->op_type == OP_COLON
3966 || oap->op_type == OP_FUNCTION
3967 || oap->op_type == OP_FILTER)
3968 && oap->motion_force == NUL)
3969 {
3970#ifdef FEAT_LINEBREAK
3971 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01003972 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003973#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003974 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003975 }
3976 }
3977 }
3978
3979 // Include the trailing byte of a multi-byte char.
3980 if (has_mbyte && oap->inclusive)
3981 {
3982 int l;
3983
3984 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3985 if (l > 1)
3986 oap->end.col += l - 1;
3987 }
3988 curwin->w_set_curswant = TRUE;
3989
3990 // oap->empty is set when start and end are the same. The inclusive
3991 // flag affects this too, unless yanking and the end is on a NUL.
3992 oap->empty = (oap->motion_type == MCHAR
3993 && (!oap->inclusive
3994 || (oap->op_type == OP_YANK
3995 && gchar_pos(&oap->end) == NUL))
3996 && EQUAL_POS(oap->start, oap->end)
3997 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3998 // For delete, change and yank, it's an error to operate on an
3999 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4000 empty_region_error = (oap->empty
4001 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4002
4003 // Force a redraw when operating on an empty Visual region, when
4004 // 'modifiable is off or creating a fold.
4005 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4006#ifdef FEAT_FOLDING
4007 || oap->op_type == OP_FOLD
4008#endif
4009 ))
4010 {
4011#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004012 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004013#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004014 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004015 }
4016
4017 // If the end of an operator is in column one while oap->motion_type
4018 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4019 // character in the previous line. If op_start is on or before the
4020 // first non-blank in the line, the operator becomes linewise
4021 // (strange, but that's the way vi does it).
4022 if ( oap->motion_type == MCHAR
4023 && oap->inclusive == FALSE
4024 && !(cap->retval & CA_NO_ADJ_OP_END)
4025 && oap->end.col == 0
4026 && (!oap->is_VIsual || *p_sel == 'o')
4027 && !oap->block_mode
4028 && oap->line_count > 1)
4029 {
4030 oap->end_adjusted = TRUE; // remember that we did this
4031 --oap->line_count;
4032 --oap->end.lnum;
4033 if (inindent(0))
4034 oap->motion_type = MLINE;
4035 else
4036 {
4037 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
4038 if (oap->end.col)
4039 {
4040 --oap->end.col;
4041 oap->inclusive = TRUE;
4042 }
4043 }
4044 }
4045 else
4046 oap->end_adjusted = FALSE;
4047
4048 switch (oap->op_type)
4049 {
4050 case OP_LSHIFT:
4051 case OP_RSHIFT:
4052 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4053 auto_format(FALSE, TRUE);
4054 break;
4055
4056 case OP_JOIN_NS:
4057 case OP_JOIN:
4058 if (oap->line_count < 2)
4059 oap->line_count = 2;
4060 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4061 curbuf->b_ml.ml_line_count)
4062 beep_flush();
4063 else
4064 {
4065 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4066 TRUE, TRUE, TRUE);
4067 auto_format(FALSE, TRUE);
4068 }
4069 break;
4070
4071 case OP_DELETE:
4072 VIsual_reselect = FALSE; // don't reselect now
4073 if (empty_region_error)
4074 {
4075 vim_beep(BO_OPER);
4076 CancelRedo();
4077 }
4078 else
4079 {
4080 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004081 // save cursor line for undo if it wasn't saved yet
4082 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4083 && u_save_cursor() == OK)
4084 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004085 }
4086 break;
4087
4088 case OP_YANK:
4089 if (empty_region_error)
4090 {
4091 if (!gui_yank)
4092 {
4093 vim_beep(BO_OPER);
4094 CancelRedo();
4095 }
4096 }
4097 else
4098 {
4099#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004100 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004101#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004102 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004103 (void)op_yank(oap, FALSE, !gui_yank);
4104 }
4105 check_cursor_col();
4106 break;
4107
4108 case OP_CHANGE:
4109 VIsual_reselect = FALSE; // don't reselect now
4110 if (empty_region_error)
4111 {
4112 vim_beep(BO_OPER);
4113 CancelRedo();
4114 }
4115 else
4116 {
4117 // This is a new edit command, not a restart. Need to
4118 // remember it to make 'insertmode' work with mappings for
4119 // Visual mode. But do this only once and not when typed and
4120 // 'insertmode' isn't set.
4121 if (p_im || !KeyTyped)
4122 restart_edit_save = restart_edit;
4123 else
4124 restart_edit_save = 0;
4125 restart_edit = 0;
4126#ifdef FEAT_LINEBREAK
4127 // Restore linebreak, so that when the user edits it looks as
4128 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004129 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004130#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004131 if (op_change(oap)) // will call edit()
4132 cap->retval |= CA_COMMAND_BUSY;
4133 if (restart_edit == 0)
4134 restart_edit = restart_edit_save;
4135 }
4136 break;
4137
4138 case OP_FILTER:
4139 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4140 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4141 else
4142 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4143 // FALLTHROUGH
4144
4145 case OP_INDENT:
4146 case OP_COLON:
4147
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004148 // If 'equalprg' is empty, do the indenting internally.
4149 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4150 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004151 if (curbuf->b_p_lisp)
4152 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004153#ifdef FEAT_EVAL
4154 if (use_indentexpr_for_lisp())
4155 op_reindent(oap, get_expr_indent);
4156 else
4157#endif
4158 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004159 break;
4160 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004161 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004162#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004163 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004164#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004165 get_c_indent);
4166 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004167 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004168
4169 op_colon(oap);
4170 break;
4171
4172 case OP_TILDE:
4173 case OP_UPPER:
4174 case OP_LOWER:
4175 case OP_ROT13:
4176 if (empty_region_error)
4177 {
4178 vim_beep(BO_OPER);
4179 CancelRedo();
4180 }
4181 else
4182 op_tilde(oap);
4183 check_cursor_col();
4184 break;
4185
4186 case OP_FORMAT:
4187#if defined(FEAT_EVAL)
4188 if (*curbuf->b_p_fex != NUL)
4189 op_formatexpr(oap); // use expression
4190 else
4191#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004192 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004193 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004194 op_colon(oap); // use external command
4195 else
4196 op_format(oap, FALSE); // use internal function
4197 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004198 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004199 case OP_FORMAT2:
4200 op_format(oap, TRUE); // use internal function
4201 break;
4202
4203 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004204 {
4205 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4206
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004207#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004208 // Restore linebreak, so that when the user edits it looks as
4209 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004210 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004211#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004212 // call 'operatorfunc'
4213 op_function(oap);
4214
4215 // Restore the info for redoing Visual mode, the function may
4216 // invoke another operator and unintentionally change it.
4217 redo_VIsual = save_redo_VIsual;
4218 break;
4219 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004220
4221 case OP_INSERT:
4222 case OP_APPEND:
4223 VIsual_reselect = FALSE; // don't reselect now
4224 if (empty_region_error)
4225 {
4226 vim_beep(BO_OPER);
4227 CancelRedo();
4228 }
4229 else
4230 {
4231 // This is a new edit command, not a restart. Need to
4232 // remember it to make 'insertmode' work with mappings for
4233 // Visual mode. But do this only once.
4234 restart_edit_save = restart_edit;
4235 restart_edit = 0;
4236#ifdef FEAT_LINEBREAK
4237 // Restore linebreak, so that when the user edits it looks as
4238 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004239 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004240#endif
4241 op_insert(oap, cap->count1);
4242#ifdef FEAT_LINEBREAK
4243 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004244 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004245#endif
4246
4247 // TODO: when inserting in several lines, should format all
4248 // the lines.
4249 auto_format(FALSE, TRUE);
4250
4251 if (restart_edit == 0)
4252 restart_edit = restart_edit_save;
4253 else
4254 cap->retval |= CA_COMMAND_BUSY;
4255 }
4256 break;
4257
4258 case OP_REPLACE:
4259 VIsual_reselect = FALSE; // don't reselect now
4260 if (empty_region_error)
4261 {
4262 vim_beep(BO_OPER);
4263 CancelRedo();
4264 }
4265 else
4266 {
4267#ifdef FEAT_LINEBREAK
4268 // Restore linebreak, so that when the user edits it looks as
4269 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004270 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004271#endif
4272 op_replace(oap, cap->nchar);
4273 }
4274 break;
4275
4276#ifdef FEAT_FOLDING
4277 case OP_FOLD:
4278 VIsual_reselect = FALSE; // don't reselect now
4279 foldCreate(oap->start.lnum, oap->end.lnum);
4280 break;
4281
4282 case OP_FOLDOPEN:
4283 case OP_FOLDOPENREC:
4284 case OP_FOLDCLOSE:
4285 case OP_FOLDCLOSEREC:
4286 VIsual_reselect = FALSE; // don't reselect now
4287 opFoldRange(oap->start.lnum, oap->end.lnum,
4288 oap->op_type == OP_FOLDOPEN
4289 || oap->op_type == OP_FOLDOPENREC,
4290 oap->op_type == OP_FOLDOPENREC
4291 || oap->op_type == OP_FOLDCLOSEREC,
4292 oap->is_VIsual);
4293 break;
4294
4295 case OP_FOLDDEL:
4296 case OP_FOLDDELREC:
4297 VIsual_reselect = FALSE; // don't reselect now
4298 deleteFold(oap->start.lnum, oap->end.lnum,
4299 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4300 break;
4301#endif
4302 case OP_NR_ADD:
4303 case OP_NR_SUB:
4304 if (empty_region_error)
4305 {
4306 vim_beep(BO_OPER);
4307 CancelRedo();
4308 }
4309 else
4310 {
4311 VIsual_active = TRUE;
4312#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004313 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004314#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004315 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004316 VIsual_active = FALSE;
4317 }
4318 check_cursor_col();
4319 break;
4320 default:
4321 clearopbeep(oap);
4322 }
4323 virtual_op = MAYBE;
4324 if (!gui_yank)
4325 {
4326 // if 'sol' not set, go back to old column for some commands
4327 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4328 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4329 || oap->op_type == OP_DELETE))
4330 {
4331#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004332 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004333#endif
4334 coladvance(curwin->w_curswant = old_col);
4335 }
4336 }
4337 else
4338 {
4339 curwin->w_cursor = old_cursor;
4340 }
4341 oap->block_mode = FALSE;
4342 clearop(oap);
4343 motion_force = NUL;
4344 }
4345#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004346 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004347#endif
4348}