blob: b4b59de226ec349461158be5169d43a1df7fc79b [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);
Christian Brabandtffb13672023-09-15 20:22:02 +020020static void pbyte(pos_T lp, int c);
21#define PBYTE(lp, c) pbyte(lp, c)
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaarf2732452018-06-03 14:47:35 +020024// Flags for third item in "opchars".
25#define OPF_LINES 1 // operator always works on lines
26#define OPF_CHANGE 2 // operator changes text
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028/*
29 * The names of operators.
30 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020031 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 */
33static char opchars[][3] =
34{
Bram Moolenaarf2732452018-06-03 14:47:35 +020035 {NUL, NUL, 0}, // OP_NOP
36 {'d', NUL, OPF_CHANGE}, // OP_DELETE
37 {'y', NUL, 0}, // OP_YANK
38 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
39 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
40 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
41 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
42 {'g', '~', OPF_CHANGE}, // OP_TILDE
43 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
44 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
45 {':', NUL, OPF_LINES}, // OP_COLON
46 {'g', 'U', OPF_CHANGE}, // OP_UPPER
47 {'g', 'u', OPF_CHANGE}, // OP_LOWER
48 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
49 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
50 {'g', '?', OPF_CHANGE}, // OP_ROT13
51 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
52 {'I', NUL, OPF_CHANGE}, // OP_INSERT
53 {'A', NUL, OPF_CHANGE}, // OP_APPEND
54 {'z', 'f', OPF_LINES}, // OP_FOLD
55 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
56 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
57 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
58 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
59 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
60 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
61 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
62 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
63 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
64 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000065};
66
67/*
68 * Translate a command name into an operator type.
69 * Must only be called with a valid operator name!
70 */
71 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010072get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
74 int i;
75
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010076 if (char1 == 'r') // ignore second character
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 return OP_REPLACE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010078 if (char1 == '~') // when tilde is an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 return OP_TILDE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010080 if (char1 == 'g' && char2 == Ctrl_A) // add
Bram Moolenaard79e5502016-01-10 22:13:02 +010081 return OP_NR_ADD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010082 if (char1 == 'g' && char2 == Ctrl_X) // subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +010083 return OP_NR_SUB;
Christian Brabandt544a38e2021-06-10 19:39:11 +020084 if (char1 == 'z' && char2 == 'y') // OP_YANK
85 return OP_YANK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 if (opchars[i][0] == char1 && opchars[i][1] == char2)
89 break;
K.Takataeeec2542021-06-02 13:28:16 +020090 if (i == (int)ARRAY_LENGTH(opchars) - 1)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010091 {
92 internal_error("get_op_type()");
93 break;
94 }
95 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 return i;
97}
98
Bram Moolenaar071d4272004-06-13 20:20:40 +000099/*
100 * Return TRUE if operator "op" always works on whole lines.
101 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +0200102 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100103op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200105 return opchars[op][2] & OPF_LINES;
106}
107
Bram Moolenaar113e1072019-01-20 15:30:40 +0100108#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200109/*
110 * Return TRUE if operator "op" changes text.
111 */
112 int
113op_is_change(int op)
114{
115 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
120 * Get first operator command character.
121 * Returns 'g' or 'z' if there is another command character.
122 */
123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100124get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 return opchars[optype][0];
127}
128
129/*
130 * Get second operator command character.
131 */
132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100133get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134{
135 return opchars[optype][1];
136}
137
138/*
139 * op_shift - handle a shift operation
140 */
141 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100142op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143{
144 long i;
145 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
148 if (u_save((linenr_T)(oap->start.lnum - 1),
149 (linenr_T)(oap->end.lnum + 1)) == FAIL)
150 return;
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 if (oap->block_mode)
153 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155 for (i = oap->line_count; --i >= 0; )
156 {
157 first_char = *ml_get_curline();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100158 if (first_char == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 else if (oap->block_mode)
161 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // Move the line right if it doesn't start with '#', 'smartindent'
164 // isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 if (first_char != '#' || !preprocs_left())
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100166 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 ++curwin->w_cursor.lnum;
168 }
169
170 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (oap->block_mode)
172 {
173 curwin->w_cursor.lnum = oap->start.lnum;
174 curwin->w_cursor.col = block_col;
175 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100176 else if (curs_top) // put cursor on first line, for ">>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 {
178 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100179 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 }
181 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100182 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100184#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100185 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100186 foldOpenCursor();
187#endif
188
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 if (oap->line_count > p_report)
191 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200192 char *op;
193 char *msg_line_single;
194 char *msg_line_plural;
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200197 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200199 op = "<";
200 msg_line_single = NGETTEXT("%ld line %sed %d time",
201 "%ld line %sed %d times", amount);
202 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
203 "%ld lines %sed %d times", amount);
204 vim_snprintf((char *)IObuff, IOSIZE,
205 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
206 oap->line_count, op, amount);
Bram Moolenaare4fc7462020-03-15 19:17:50 +0100207 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 }
209
Bram Moolenaare1004402020-10-24 20:49:43 +0200210 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100211 {
212 // Set "'[" and "']" marks.
213 curbuf->b_op_start = oap->start;
214 curbuf->b_op_end.lnum = oap->end.lnum;
zeertzjq94b7c322024-03-12 21:50:32 +0100215 curbuf->b_op_end.col = ml_get_len(oap->end.lnum);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100216 if (curbuf->b_op_end.col > 0)
217 --curbuf->b_op_end.col;
218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219}
220
Gary Johnsoneed63f92024-12-09 21:03:48 +0100221#ifdef FEAT_VARTABS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222/*
Gary Johnsoneed63f92024-12-09 21:03:48 +0100223 * Return the tabstop width at the index of the variable tabstop array. If an
224 * index greater than the length of the array is given, the last tabstop width
225 * in the array is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 */
Gary Johnsoneed63f92024-12-09 21:03:48 +0100227 static int
228get_vts(int *vts_array, int index)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
Gary Johnsoneed63f92024-12-09 21:03:48 +0100230 int ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
Gary Johnsoneed63f92024-12-09 21:03:48 +0100232 if (index < 1)
233 ts = 0;
234 else if (index <= vts_array[0])
235 ts = vts_array[index];
236 else
237 ts = vts_array[vts_array[0]];
Christian Brabandt7737ce52024-06-02 16:04:43 +0200238
Gary Johnsoneed63f92024-12-09 21:03:48 +0100239 return ts;
240}
241
242/*
243 * Return the sum of all the tabstops through the index-th.
244 */
245 static int
246get_vts_sum(int *vts_array, int index)
247{
248 int sum = 0;
249 int i;
250
zeertzjq98800972025-04-18 10:57:33 +0200251 // Perform the summation for indices within the actual array.
Gary Johnsoneed63f92024-12-09 21:03:48 +0100252 for (i = 1; i <= index && i <= vts_array[0]; i++)
253 sum += vts_array[i];
254
zeertzjq98800972025-04-18 10:57:33 +0200255 // Add tabstops whose indices exceed the actual array.
Gary Johnsoneed63f92024-12-09 21:03:48 +0100256 if (i <= index)
257 sum += vts_array[vts_array[0]] * (index - vts_array[0]);
258
259 return sum;
260}
261#endif
262
263 static vimlong_T
264get_new_sw_indent(
265 int left, // TRUE if shift is to the left
266 int round, // TRUE if new indent is to be to a tabstop
267 vimlong_T amount, // Number of shifts
268 vimlong_T sw_val)
269{
270 vimlong_T count = get_indent();
271 vimlong_T i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200273 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
Christian Brabandt220474d2024-07-20 13:26:44 +0200275 i = trim_to_int(count) / sw_val; // number of 'shiftwidth' rounded down
276 j = trim_to_int(count) % sw_val; // extra spaces
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200277 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 --amount;
279 if (left)
280 {
281 i -= amount;
282 if (i < 0)
283 i = 0;
284 }
285 else
286 i += amount;
Gary Johnsoneed63f92024-12-09 21:03:48 +0100287 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
Gary Johnsoneed63f92024-12-09 21:03:48 +0100289 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 {
291 if (left)
292 {
Gary Johnsoneed63f92024-12-09 21:03:48 +0100293 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if (count < 0)
295 count = 0;
296 }
297 else
Gary Johnsoneed63f92024-12-09 21:03:48 +0100298 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 }
300
Gary Johnsoneed63f92024-12-09 21:03:48 +0100301 return count;
302}
303
304#ifdef FEAT_VARTABS
305 static vimlong_T
306get_new_vts_indent(
307 int left, // TRUE if shift is to the left
308 int round, // TRUE if new indent is to be to a tabstop
309 int amount, // Number of shifts
310 int *vts_array)
311{
312 vimlong_T indent = get_indent();
313 int vtsi = 0;
314 int vts_indent = 0;
315 int ts = 0; // Silence uninitialized variable warning.
316 int offset; // Extra indent spaces to the right of the
317 // tabstop
318
319 // Find the tabstop at or to the left of the current indent.
320 while (vts_indent <= indent)
321 {
322 vtsi++;
323 ts = get_vts(vts_array, vtsi);
324 vts_indent += ts;
325 }
326 vts_indent -= ts;
327 vtsi--;
328
329 offset = indent - vts_indent;
330
331 if (round)
332 {
333 if (left)
334 {
335 if (offset == 0)
336 indent = get_vts_sum(vts_array, vtsi - amount);
337 else
338 indent = get_vts_sum(vts_array, vtsi - (amount - 1));
339 }
340 else
341 indent = get_vts_sum(vts_array, vtsi + amount);
342 }
343 else
344 {
345 if (left)
346 {
347 if (amount > vtsi)
348 indent = 0;
349 else
350 indent = get_vts_sum(vts_array, vtsi - amount) + offset;
351 }
352 else
353 indent = get_vts_sum(vts_array, vtsi + amount) + offset;
354 }
355
356 return indent;
357}
358#endif
359
360/*
361 * Shift the current line 'amount' shiftwidth(s) left (if 'left' is TRUE) or
362 * right.
363 *
364 * The rules for choosing a shiftwidth are: If 'shiftwidth' is non-zero, use
365 * 'shiftwidth'; else if 'vartabstop' is not empty, use 'vartabstop'; else use
366 * 'tabstop'. The Vim documentation says nothing about 'softtabstop' or
367 * 'varsofttabstop' affecting the shiftwidth, and neither affects the
368 * shiftwidth in current versions of Vim, so they are not considered here.
369 */
370 void
371shift_line(
372 int left, // TRUE if shift is to the left
373 int round, // TRUE if new indent is to be to a tabstop
374 int amount, // Number of shifts
375 int call_changed_bytes) // call changed_bytes()
376{
377 vimlong_T count;
378 long sw_val = curbuf->b_p_sw;
379 long ts_val = curbuf->b_p_ts;
380#ifdef FEAT_VARTABS
381 int *vts_array = curbuf->b_p_vts_array;
382#endif
383
384 if (sw_val != 0)
385 // 'shiftwidth' is not zero; use it as the shift size.
386 count = get_new_sw_indent(left, round, amount, sw_val);
387 else
388#ifdef FEAT_VARTABS
389 if ((vts_array == NULL) || (vts_array[0] == 0))
390#endif
391 {
392 // 'shiftwidth is zero and 'vartabstop' is empty; use 'tabstop' as the
393 // shift size.
394 count = get_new_sw_indent(left, round, amount, ts_val);
395 }
396#ifdef FEAT_VARTABS
397 else
398 {
399 // 'shiftwidth is zero and 'vartabstop' is defined; use 'vartabstop'
400 // to determine the new indent.
401 count = get_new_vts_indent(left, round, amount, vts_array);
402 }
403#endif
404
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200405 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (State & VREPLACE_FLAG)
Ernie Rael2b0882f2023-11-23 20:21:45 +0100407 change_indent(INDENT_SET, trim_to_int(count), FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 else
Ernie Rael2b0882f2023-11-23 20:21:45 +0100409 (void)set_indent(trim_to_int(count), call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410}
411
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412/*
413 * Shift one line of the current block one shiftwidth right or left.
414 * Leaves cursor on first character in block.
415 */
416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100417shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418{
419 int left = (oap->op_type == OP_LSHIFT);
420 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000421 int total;
422 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +0200423 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 int oldcol = curwin->w_cursor.col;
Gary Johnson88d4f252024-06-01 20:51:33 +0200425 int sw_val = (int)get_sw_value_indent(curbuf, left);
Bram Moolenaardac13472019-09-16 21:06:21 +0200426 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000429 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100430 int added;
John Marriott38b9f452024-04-25 21:39:18 +0200431 size_t new_line_len; // the length of the line after the
LemonBoy4b936742022-05-13 21:56:28 +0100432 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_RIGHTLEFT
434 int old_p_ri = p_ri;
435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100436 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#endif
438
Bram Moolenaar24959102022-05-07 20:01:16 +0100439 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
441 if (bd.is_short)
442 return;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200445 total = (int)((unsigned)amount * (unsigned)sw_val);
446 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100447 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200448
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 oldp = ml_get_curline();
John Marriott38b9f452024-04-25 21:39:18 +0200450 oldlen = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452 if (!left)
453 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100454 int tabs = 0, spaces = 0;
455 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100456
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 /*
458 * 1. Get start vcol
459 * 2. Total ws vcols
460 * 3. Divvy into TABs & spp
461 * 4. Construct new string
462 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100463 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 ws_vcol = bd.start_vcol - bd.pre_whitesp;
465 if (bd.startspaces)
466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100468 {
469 if ((*mb_ptr2len)(bd.textstart) == 1)
470 ++bd.textstart;
471 else
472 {
473 ws_vcol = 0;
474 bd.startspaces = 0;
475 }
476 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000477 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000478 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100480
481 // TODO: is passing bd.textstart for start of the line OK?
482 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
483 bd.start_vcol, bd.textstart, bd.textstart);
484 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100486 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100488 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100490 bd.textstart = cts.cts_ptr;
491 bd.start_vcol = cts.cts_vcol;
492 clear_chartabsize_arg(&cts);
493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 // OK, now total=all the VWS reqd, and textstart points at the 1st
495 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200496#ifdef FEAT_VARTABS
497 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200498 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100499 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200500 else
LemonBoy4b936742022-05-13 21:56:28 +0100501 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200502#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100504 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
505 if (tabs > 0)
506 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 else
LemonBoy4b936742022-05-13 21:56:28 +0100508 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200509#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100510 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100512
John Marriott38b9f452024-04-25 21:39:18 +0200513 new_line_len = bd.textcol + tabs + spaces + (oldlen - (bd.textstart - oldp));
LemonBoy4b936742022-05-13 21:56:28 +0100514 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (newp == NULL)
516 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +0200518 newlen = bd.textcol;
519 vim_memset(newp + newlen, TAB, (size_t)tabs);
520 newlen += tabs;
521 vim_memset(newp + newlen, ' ', (size_t)spaces);
522 STRCPY(newp + newlen + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100524 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100526 colnr_T destination_col; // column to which text in block will
527 // be shifted
528 char_u *verbatim_copy_end; // end of the part of the line which is
529 // copied verbatim
530 colnr_T verbatim_copy_width;// the (displayed) width of this part
531 // of line
John Marriott38b9f452024-04-25 21:39:18 +0200532 size_t fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000533 size_t block_space_width;
534 size_t shift_amount;
535 char_u *non_white = bd.textstart;
536 colnr_T non_white_col;
John Marriott38b9f452024-04-25 21:39:18 +0200537 size_t fixedlen; // length of string left of the shift
538 // position (ie the string not being shifted)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100539 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000541 /*
542 * Firstly, let's find the first non-whitespace character that is
543 * displayed after the block's start column and the character's column
544 * number. Also, let's calculate the width of all the whitespace
545 * characters that are displayed in the block and precede the searched
546 * non-whitespace character.
547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 // If "bd.startspaces" is set, "bd.textstart" points to the character,
550 // the part of which is displayed at the block's beginning. Let's start
551 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000552 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100553 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100555 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000556 non_white_col = bd.start_vcol;
557
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100558 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
559 non_white_col, bd.textstart, non_white);
560 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000561 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100562 incr = lbr_chartabsize_adv(&cts);
563 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100565 non_white_col = cts.cts_vcol;
566 non_white = cts.cts_ptr;
567 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000569 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100570 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000571 shift_amount = (block_space_width < (size_t)total
572 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100574 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000575 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100577 // Now let's find out how much of the beginning of the line we can
578 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000579 verbatim_copy_end = bd.textstart;
580 verbatim_copy_width = bd.start_vcol;
581
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100582 // If "bd.startspaces" is set, "bd.textstart" points to the character
583 // preceding the block. We have to subtract its width to obtain its
584 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000585 if (bd.startspaces)
586 verbatim_copy_width -= bd.start_char_vcols;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100587 init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
588 bd.textstart, verbatim_copy_end);
589 while (cts.cts_vcol < destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000590 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100591 incr = lbr_chartabsize(&cts);
592 if (cts.cts_vcol + incr > destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000593 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100594 cts.cts_vcol += incr;
595 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000596 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100597 verbatim_copy_width = cts.cts_vcol;
598 verbatim_copy_end = cts.cts_ptr;
599 clear_chartabsize_arg(&cts);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000600
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100601 // If "destination_col" is different from the width of the initial
602 // part of the line that will be copied, it means we encountered a tab
603 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000604 fill = destination_col - verbatim_copy_width;
605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100606 // The replacement line will consist of:
607 // - the beginning of the original line up to "verbatim_copy_end",
608 // - "fill" number of spaces,
609 // - the rest of the line, pointed to by non_white.
John Marriott38b9f452024-04-25 21:39:18 +0200610 fixedlen = verbatim_copy_end - oldp;
611 new_line_len = fixedlen + fill + (oldlen - (non_white - oldp));
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000612
LemonBoy4b936742022-05-13 21:56:28 +0100613 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (newp == NULL)
615 return;
John Marriott38b9f452024-04-25 21:39:18 +0200616 mch_memmove(newp, oldp, fixedlen);
617 newlen = fixedlen;
618 vim_memset(newp + newlen, ' ', (size_t)fill);
619 STRCPY(newp + newlen + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100621 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
John Marriott38b9f452024-04-25 21:39:18 +0200623
624 // compute the number of bytes added or subtracted.
625 // note new_line_len and oldlen are unsigned so we have
626 // to be careful about how we calculate this.
627 if (new_line_len >= oldlen)
628 added = (int)(new_line_len - oldlen);
629 else
630 added = 0 - (int)(oldlen - new_line_len);
LemonBoy4b936742022-05-13 21:56:28 +0100631 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 State = oldstate;
633 curwin->w_cursor.col = oldcol;
634#ifdef FEAT_RIGHTLEFT
635 p_ri = old_p_ri;
636#endif
637}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639/*
640 * Insert string "s" (b_insert ? before : after) block :AKelly
641 * Caller must prepare for undo.
642 */
643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644block_insert(
645 oparg_T *oap,
646 char_u *s,
John Marriott38b9f452024-04-25 21:39:18 +0200647 size_t slen,
Bram Moolenaar9b578142016-01-30 19:39:49 +0100648 int b_insert,
649 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
Bram Moolenaardac13472019-09-16 21:06:21 +0200651 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100652 int count = 0; // extra spaces to replace a cut TAB
653 int spaces = 0; // non-zero if cutting a TAB
654 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200655 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100656 char_u *newp, *oldp; // new, old lines
657 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 int oldstate = State;
659
Bram Moolenaar24959102022-05-07 20:01:16 +0100660 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
663 {
664 block_prep(oap, bdp, lnum, TRUE);
665 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668 oldp = ml_get(lnum);
669
670 if (b_insert)
671 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200672 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 spaces = bdp->startspaces;
674 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100675 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 offset = bdp->textcol;
677 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200680 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100681 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200683 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 offset = bdp->textcol + bdp->textlen - (spaces != 0);
687 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100688 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100690 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 if (!bdp->is_MAX)
692 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
693 count = spaces;
694 offset = bdp->textcol + bdp->textlen;
695 }
696 }
697
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200698 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000699 // avoid copying part of a multi-byte character
700 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100701
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200702 if (spaces < 0) // can happen when the cursor was moved
703 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200704
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000705 // Make sure the allocated size matches what is actually copied below.
John Marriott38b9f452024-04-25 21:39:18 +0200706 newp = alloc(ml_get_len(lnum) + spaces + slen
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000707 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
708 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (newp == NULL)
710 continue;
711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000713 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 oldp += offset;
715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100716 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200717 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200718 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 // copy the new text
John Marriott38b9f452024-04-25 21:39:18 +0200721 mch_memmove(newp + startcol, s, slen);
Mike Williams16b63bd2024-06-01 11:33:40 +0200722 offset += (int)slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000724 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000726 if (*oldp == TAB)
727 {
728 // insert post-padding
729 vim_memset(newp + offset + spaces, ' ',
730 (size_t)(ts_val - spaces));
731 // we're splitting a TAB, don't copy it
732 oldp++;
733 // We allowed for that TAB, remember this now
734 count++;
735 }
736 else
737 // Not a TAB, no extra spaces
738 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740
741 if (spaces > 0)
742 offset += count;
John Marriott38b9f452024-04-25 21:39:18 +0200743 STRCPY(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745 ml_replace(lnum, newp, FALSE);
746
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200747 if (b_insert)
748 // correct any text properties
Mike Williams16b63bd2024-06-01 11:33:40 +0200749 inserted_bytes(lnum, startcol, (int)slen);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (lnum == oap->end.lnum)
752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100753 // Set "']" mark to the end of the block instead of the end of
754 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 curbuf->b_op_end.lnum = oap->end.lnum;
756 curbuf->b_op_end.col = offset;
757 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100758 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
761
762 State = oldstate;
763}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200766 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200768 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 */
770 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100771op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
773 int n;
774 linenr_T lnum;
775 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 char_u *newp, *oldp;
777 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
779 int did_yank = FALSE;
780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100781 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 return OK;
783
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100784 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (oap->empty)
786 return u_save_cursor();
787
788 if (!curbuf->b_p_ma)
789 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200790 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 return FAIL;
792 }
793
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000794 if (VIsual_select && oap->is_VIsual)
795 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100796 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798#ifdef FEAT_CLIPBOARD
799 adjust_clip_reg(&oap->regname);
800#endif
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (has_mbyte)
803 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaard04b7502010-07-08 22:27:55 +0200805 /*
806 * Imitate the strange Vi behaviour: If the delete spans more than one
807 * line and motion_type == MCHAR and the result is a blank line, make the
808 * delete linewise. Don't do this for the change command or Visual mode.
809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000812 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100814 && oap->motion_force == NUL
Christian Brabandt22105fd2024-07-15 20:51:11 +0200815 && (vim_strchr(p_cpo, CPO_WORD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 && oap->op_type == OP_DELETE)
817 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200818 ptr = ml_get(oap->end.lnum) + oap->end.col;
819 if (*ptr != NUL)
820 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 ptr = skipwhite(ptr);
822 if (*ptr == NUL && inindent(0))
823 oap->motion_type = MLINE;
824 }
825
Bram Moolenaard04b7502010-07-08 22:27:55 +0200826 /*
827 * Check for trying to delete (e.g. "D") in an empty line.
828 * Note: For the change operator it is ok.
829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if ( oap->motion_type == MCHAR
831 && oap->line_count == 1
832 && oap->op_type == OP_DELETE
833 && *ml_get(oap->start.lnum) == NUL)
834 {
835 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000836 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 * 'cpoptions' (Vi compatible).
838 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000839 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100840 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
841 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000842 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
844 beep_flush();
845 return OK;
846 }
847
Bram Moolenaard04b7502010-07-08 22:27:55 +0200848 /*
849 * Do a yank of whatever we're about to delete.
850 * If a yank register was specified, put the deleted text into that
851 * register. For the black hole register '_' don't yank anything.
852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (oap->regname != '_')
854 {
855 if (oap->regname != 0)
856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (!valid_yank_reg(oap->regname, TRUE))
859 {
860 beep_flush();
861 return OK;
862 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
864 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 did_yank = TRUE;
866 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200867 else
868 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 /*
871 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100872 * delete contains a line break, or when using a specific operator (Vi
873 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100875 if (oap->motion_type == MLINE || oap->line_count > 1
876 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100878 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (op_yank(oap, TRUE, FALSE) == OK)
880 did_yank = TRUE;
881 }
882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 // Yank into small delete register when no named register specified
884 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200885 if ((
886#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200887 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
888 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200889#endif
890 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 && oap->line_count == 1)
892 {
893 oap->regname = '-';
894 get_yank_register(oap->regname, TRUE);
895 if (op_yank(oap, TRUE, FALSE) == OK)
896 did_yank = TRUE;
897 oap->regname = 0;
898 }
899
900 /*
901 * If there's too much stuff to fit in the yank register, then get a
902 * confirmation before doing the delete. This is crude, but simple.
903 * And it avoids doing a delete of something we can't put back if we
904 * want.
905 */
906 if (!did_yank)
907 {
908 int msg_silent_save = msg_silent;
909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
912 msg_silent = msg_silent_save;
913 if (n != 'y')
914 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000915 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 return FAIL;
917 }
918 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100919
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100920#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100921 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200922 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925
Bram Moolenaard04b7502010-07-08 22:27:55 +0200926 /*
927 * block mode delete
928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (oap->block_mode)
930 {
931 if (u_save((linenr_T)(oap->start.lnum - 1),
932 (linenr_T)(oap->end.lnum + 1)) == FAIL)
933 return FAIL;
934
935 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
936 {
937 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100938 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 continue;
940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100941 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 if (lnum == curwin->w_cursor.lnum)
943 {
944 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947
Bram Moolenaar8055d172019-05-17 22:57:26 +0200948 // "n" == number of chars deleted
949 // If we delete a TAB, it may be replaced by several characters.
950 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 n = bd.textlen - bd.startspaces - bd.endspaces;
952 oldp = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100953 newp = alloc(ml_get_len(lnum) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (newp == NULL)
955 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100956 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200959 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 // copy the part after the deleted part
John Marriott38b9f452024-04-25 21:39:18 +0200962 STRCPY(newp + bd.textcol + bd.startspaces + bd.endspaces,
963 oldp + bd.textcol + bd.textlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100964 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200966
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100967#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200968 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200969 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972
973 check_cursor_col();
974 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
975 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100978 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 if (oap->op_type == OP_CHANGE)
981 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100982 // Delete the lines except the first one. Temporarily move the
983 // cursor to the next line. Save the current line number, if the
984 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (oap->line_count > 1)
986 {
987 lnum = curwin->w_cursor.lnum;
988 ++curwin->w_cursor.lnum;
989 del_lines((long)(oap->line_count - 1), TRUE);
990 curwin->w_cursor.lnum = lnum;
991 }
992 if (u_save_cursor() == FAIL)
993 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100994 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100996 beginline(BL_WHITE); // cursor on first non-white
997 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 ai_col = curwin->w_cursor.col;
999 }
1000 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 beginline(0); // cursor in column 0
zeertzjq8c55d602024-03-13 20:42:26 +01001002 truncate_line(FALSE); // delete the rest of the line,
1003 // leaving cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001005 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 else
1008 {
1009 del_lines(oap->line_count, TRUE);
1010 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001011 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014 else
1015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (virtual_op)
1017 {
1018 int endcol = 0;
1019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001020 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (gchar_pos(&oap->start) == '\t')
1022 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001023 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 return FAIL;
1025 if (oap->line_count == 1)
1026 endcol = getviscol2(oap->end.col, oap->end.coladd);
1027 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1028 oap->start = curwin->w_cursor;
1029 if (oap->line_count == 1)
1030 {
1031 coladvance(endcol);
1032 oap->end.col = curwin->w_cursor.col;
1033 oap->end.coladd = curwin->w_cursor.coladd;
1034 curwin->w_cursor = oap->start;
1035 }
1036 }
1037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001038 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (gchar_pos(&oap->end) == '\t'
1040 && (int)oap->end.coladd < oap->inclusive)
1041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001042 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (u_save((linenr_T)(oap->end.lnum - 1),
1044 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1045 return FAIL;
1046 curwin->w_cursor = oap->end;
1047 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1048 oap->end = curwin->w_cursor;
1049 curwin->w_cursor = oap->start;
1050 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +01001051 if (has_mbyte)
1052 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001055 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001057 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 return FAIL;
1059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001060 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001061 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 && oap->op_type == OP_CHANGE
1063 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001064 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 display_dollar(oap->end.col - !oap->inclusive);
1066
1067 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (virtual_op)
1070 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 // fix up things for virtualedit-delete:
1072 // break the tabs which are going to get in our way
zeertzjq94b7c322024-03-12 21:50:32 +01001073 int len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 if (oap->end.coladd != 0
1076 && (int)oap->end.col >= len - 1
1077 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1078 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001079 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (n == 0 && oap->start.coladd != oap->end.coladd)
1081 n = 1;
1082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001083 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (gchar_cursor() != NUL)
1085 curwin->w_cursor.coladd = 0;
1086 }
Bram Moolenaard009e862015-06-09 20:20:03 +02001087 (void)del_bytes((long)n, !virtual_op,
1088 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001090 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
1092 pos_T curpos;
1093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001094 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1096 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1097 return FAIL;
1098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001099 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001101 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ++curwin->w_cursor.lnum;
1103 del_lines((long)(oap->line_count - 2), FALSE);
1104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001105 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001106 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001107 curwin->w_cursor.col = 0;
1108 (void)del_bytes((long)n, !virtual_op,
1109 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001110 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +02001111 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001113 if (oap->op_type == OP_DELETE)
1114 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 }
1116
1117 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1118
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001119setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +02001120 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001122 if (oap->block_mode)
1123 {
1124 curbuf->b_op_end.lnum = oap->end.lnum;
1125 curbuf->b_op_end.col = oap->start.col;
1126 }
1127 else
1128 curbuf->b_op_end = oap->start;
1129 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132 return OK;
1133}
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135/*
1136 * Adjust end of operating area for ending on a multi-byte character.
1137 * Used for deletion.
1138 */
1139 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001140mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141{
zeertzjq048761b2024-02-24 14:21:39 +01001142 char_u *line;
1143 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001145 if (!oap->inclusive)
1146 return;
1147
zeertzjq048761b2024-02-24 14:21:39 +01001148 line = ml_get(oap->end.lnum);
1149 ptr = line + oap->end.col;
1150 if (*ptr != NUL)
1151 {
1152 ptr -= (*mb_head_off)(line, ptr);
1153 ptr += (*mb_ptr2len)(ptr) - 1;
1154 oap->end.col = ptr - line;
1155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar630afe82018-06-28 19:26:28 +02001158/*
1159 * Replace the character under the cursor with "c".
1160 * This takes care of multi-byte characters.
1161 */
1162 static void
1163replace_character(int c)
1164{
1165 int n = State;
1166
Bram Moolenaar24959102022-05-07 20:01:16 +01001167 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001168 ins_char(c);
1169 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001171 dec_cursor();
1172}
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174/*
1175 * Replace a whole area with one character.
1176 */
1177 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001178op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +02001183 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001185 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001186 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
1188 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001191 if (c == REPLACE_CR_NCHAR)
1192 {
1193 had_ctrl_v_cr = TRUE;
1194 c = CAR;
1195 }
1196 else if (c == REPLACE_NL_NCHAR)
1197 {
1198 had_ctrl_v_cr = TRUE;
1199 c = NL;
1200 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (has_mbyte)
1203 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
1205 if (u_save((linenr_T)(oap->start.lnum - 1),
1206 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1207 return FAIL;
1208
1209 /*
1210 * block mode replace
1211 */
1212 if (oap->block_mode)
1213 {
1214 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1215 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1219 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001222 // n == number of extra chars required
1223 // If we split a TAB, it may be replaced by several characters.
1224 // Thus the number of characters may increase!
1225 // If the range starts in virtual space, count the initial
1226 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1228 {
1229 pos_T vpos;
1230
Bram Moolenaara1381de2009-11-03 15:44:21 +00001231 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 getvpos(&vpos, oap->start_vcol);
1233 bd.startspaces += vpos.coladd;
1234 n = bd.startspaces;
1235 }
1236 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001237 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001244 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 numc = oap->end_vcol - oap->start_vcol + 1;
1246 if (bd.is_short && (!virtual_op || bd.is_MAX))
1247 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001249 // A double-wide character can be replaced only up to half the
1250 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if ((*mb_char2cells)(c) > 1)
1252 {
1253 if ((numc & 1) && !bd.is_short)
1254 {
1255 ++bd.endspaces;
1256 ++n;
1257 }
1258 numc = numc / 2;
1259 }
1260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 num_chars = numc;
1263 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 n += numc - bd.textlen;
1266
1267 oldp = ml_get_curline();
zeertzjq94b7c322024-03-12 21:50:32 +01001268 oldlen = ml_get_curline_len();
Bram Moolenaar964b3742019-05-24 18:54:09 +02001269 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (newp == NULL)
1271 continue;
1272 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001273 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001275 newlen = bd.textcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001276 // insert pre-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001277 vim_memset(newp + newlen, ' ', (size_t)bd.startspaces);
1278 newlen += bd.startspaces;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001279 // insert replacement chars CHECK FOR ALLOCATED SPACE
1280 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1281 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001282 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001284 if (has_mbyte)
1285 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001286 while (--num_chars >= 0)
John Marriott38b9f452024-04-25 21:39:18 +02001287 newlen += (*mb_char2bytes)(c, newp + newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001288 }
1289 else
John Marriott38b9f452024-04-25 21:39:18 +02001290 {
1291 vim_memset(newp + newlen, c, (size_t)numc);
1292 newlen += numc;
1293 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001294 if (!bd.is_short)
1295 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001296 // insert post-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001297 vim_memset(newp + newlen, ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001298 // copy the part after the changed part
John Marriott38b9f452024-04-25 21:39:18 +02001299 STRCPY(newp + newlen + bd.endspaces,
1300 oldp + bd.textcol + bd.textlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 }
1303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001305 // Replacing with \r or \n means splitting the line.
John Marriott38b9f452024-04-25 21:39:18 +02001306 after_p = alloc(oldlen + 1 + n - newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001307 if (after_p != NULL)
John Marriott38b9f452024-04-25 21:39:18 +02001308 STRCPY(after_p, oldp + bd.textcol + bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
John Marriott38b9f452024-04-25 21:39:18 +02001310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001311 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001313 if (after_p != NULL)
1314 {
1315 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1316 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1317 oap->end.lnum++;
1318 vim_free(after_p);
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 }
1322 else
1323 {
1324 /*
1325 * MCHAR and MLINE motion replace.
1326 */
1327 if (oap->motion_type == MLINE)
1328 {
1329 oap->start.col = 0;
1330 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001331 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (oap->end.col)
1333 --oap->end.col;
1334 }
1335 else if (!oap->inclusive)
1336 dec(&(oap->end));
1337
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001338 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001340 int done = FALSE;
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 n = gchar_cursor();
1343 if (n != NUL)
1344 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001345 int new_byte_len = (*mb_char2len)(c);
1346 int old_byte_len = mb_ptr2len(ml_get_cursor());
1347
1348 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 // This is slow, but it handles replacing a single-byte
1351 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001352 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001353 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001354 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001355 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (n == TAB)
1360 {
1361 int end_vcol = 0;
1362
1363 if (curwin->w_cursor.lnum == oap->end.lnum)
1364 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001365 // oap->end has to be recalculated when
1366 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 end_vcol = getviscol2(oap->end.col,
1368 oap->end.coladd);
1369 }
1370 coladvance_force(getviscol());
1371 if (curwin->w_cursor.lnum == oap->end.lnum)
1372 getvpos(&oap->end, end_vcol);
1373 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001374 // with "coladd" set may move to just after a TAB
1375 if (gchar_cursor() != NUL)
1376 {
1377 PBYTE(curwin->w_cursor, c);
1378 done = TRUE;
1379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001382 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
1384 int virtcols = oap->end.coladd;
1385
1386 if (curwin->w_cursor.lnum == oap->start.lnum
1387 && oap->start.col == oap->end.col && oap->start.coladd)
1388 virtcols -= oap->start.coladd;
1389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 // oap->end has been trimmed so it's effectively inclusive;
1391 // as a result an extra +1 must be counted so we don't
1392 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1394 curwin->w_cursor.col -= (virtcols + 1);
1395 for (; virtcols >= 0; virtcols--)
1396 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001397 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001398 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001399 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001400 PBYTE(curwin->w_cursor, c);
Gary Johnson88d4f252024-06-01 20:51:33 +02001401 if (inc(&curwin->w_cursor) == -1)
1402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 if (inc_cursor() == -1)
1408 break;
1409 }
1410 }
1411
1412 curwin->w_cursor = oap->start;
1413 check_cursor();
1414 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1415
Bram Moolenaare1004402020-10-24 20:49:43 +02001416 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001417 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001418 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001419 curbuf->b_op_start = oap->start;
1420 curbuf->b_op_end = oap->end;
1421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
1423 return OK;
1424}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001426static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428/*
1429 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1430 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001432op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001436 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 if (u_save((linenr_T)(oap->start.lnum - 1),
1439 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1440 return;
1441
1442 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001443 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 {
1445 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1446 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001447 int one_change;
1448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 block_prep(oap, &bd, pos.lnum, FALSE);
1450 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001451 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1452 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001453
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001454#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001455 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001457 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaar009b2592004-10-24 19:18:58 +00001459 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1460 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001461 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001462 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001464 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 if (did_change)
1469 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1470 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
1473 if (oap->motion_type == MLINE)
1474 {
1475 oap->start.col = 0;
1476 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001477 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (oap->end.col)
1479 --oap->end.col;
1480 }
1481 else if (!oap->inclusive)
1482 dec(&(oap->end));
1483
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001484 if (pos.lnum == oap->end.lnum)
1485 did_change = swapchars(oap->op_type, &pos,
1486 oap->end.col - pos.col + 1);
1487 else
1488 for (;;)
1489 {
1490 did_change |= swapchars(oap->op_type, &pos,
zeertzjq94b7c322024-03-12 21:50:32 +01001491 pos.lnum == oap->end.lnum ? oap->end.col + 1
1492 : ml_get_pos_len(&pos));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001493 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001494 break;
1495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (did_change)
1497 {
1498 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1499 0L);
1500#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001501 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 char_u *ptr;
1504 int count;
1505
1506 pos = oap->start;
1507 while (pos.lnum < oap->end.lnum)
1508 {
1509 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001510 count = ml_get_buf_len(curbuf, pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001511 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001512 // get the line again, it may have been flushed
1513 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001515 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 pos.col = 0;
1517 pos.lnum++;
1518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001520 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001521 // get the line again, it may have been flushed
1522 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001524 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526#endif
1527 }
1528 }
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001532 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaare1004402020-10-24 20:49:43 +02001534 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001535 {
1536 // Set '[ and '] marks.
1537 curbuf->b_op_start = oap->start;
1538 curbuf->b_op_end = oap->end;
1539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001542 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001543 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544}
1545
1546/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001547 * Invoke swapchar() on "length" bytes at position "pos".
1548 * "pos" is advanced to just after the changed characters.
1549 * "length" is rounded up to include the whole last multi-byte character.
1550 * Also works correctly when the number of bytes changes.
1551 * Returns TRUE if some character was changed.
1552 */
1553 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001554swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001555{
1556 int todo;
1557 int did_change = 0;
1558
1559 for (todo = length; todo > 0; --todo)
1560 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001561 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001562 {
1563 int len = (*mb_ptr2len)(ml_get_pos(pos));
1564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001565 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001566 if (len > 0)
1567 todo -= len - 1;
1568 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001569 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001570 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001571 break;
1572 }
1573 return did_change;
1574}
1575
1576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 * If op_type == OP_UPPER: make uppercase,
1578 * if op_type == OP_LOWER: make lowercase,
1579 * if op_type == OP_ROT13: do rot13 encoding,
1580 * else swap case of character at 'pos'
1581 * returns TRUE when something actually changed.
1582 */
1583 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001584swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 int c;
1587 int nc;
1588
1589 c = gchar_pos(pos);
1590
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if (c >= 0x80 && op_type == OP_ROT13)
1593 return FALSE;
1594
glepnirbd1232a2024-02-12 22:14:53 +01001595 // ~ is OP_NOP, g~ is OP_TILDE, gU is OP_UPPER
1596 if ((op_type == OP_UPPER || op_type == OP_NOP || op_type == OP_TILDE)
1597 && c == 0xdf
1598 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001599 {
1600 pos_T sp = curwin->w_cursor;
1601
glepnirbd1232a2024-02-12 22:14:53 +01001602 // Special handling for lowercase German sharp s (ß): convert to uppercase (ẞ).
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001603 curwin->w_cursor = *pos;
1604 del_char(FALSE);
glepnirbd1232a2024-02-12 22:14:53 +01001605 ins_char(0x1E9E);
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001606 curwin->w_cursor = sp;
glepnirbd1232a2024-02-12 22:14:53 +01001607 return TRUE;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001608 }
1609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 nc = c;
1613 if (MB_ISLOWER(c))
1614 {
1615 if (op_type == OP_ROT13)
1616 nc = ROT13(c, 'a');
1617 else if (op_type != OP_LOWER)
1618 nc = MB_TOUPPER(c);
1619 }
1620 else if (MB_ISUPPER(c))
1621 {
1622 if (op_type == OP_ROT13)
1623 nc = ROT13(c, 'A');
1624 else if (op_type != OP_UPPER)
1625 nc = MB_TOLOWER(c);
1626 }
1627 if (nc != c)
1628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1630 {
1631 pos_T sp = curwin->w_cursor;
1632
1633 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001635 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 ins_char(nc);
1637 curwin->w_cursor = sp;
1638 }
1639 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001640 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 return TRUE;
1642 }
1643 return FALSE;
1644}
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646/*
1647 * op_insert - Insert and append operators for Visual mode.
1648 */
1649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001650op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
zeertzjq8c55d602024-03-13 20:42:26 +01001652 long pre_textlen = 0;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001653 colnr_T ind_pre_col = 0, ind_post_col;
1654 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 struct block_def bd;
1656 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001657 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001658 pos_T start_insert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1662
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001663 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001665 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
1667 if (oap->block_mode)
1668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 // When 'virtualedit' is used, need to insert the extra spaces before
1670 // doing block_prep(). When only "block" is used, virtual edit is
1671 // already disabled, but still need it when calling
1672 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001673 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001674 // state for the current window. To override that state, we need to
1675 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (curwin->w_cursor.coladd > 0)
1677 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001678 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (u_save_cursor() == FAIL)
1681 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001682
Gary Johnson51ad8502021-08-03 18:33:08 +02001683 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 coladvance_force(oap->op_type == OP_APPEND
1685 ? oap->end_vcol + 1 : getviscol());
1686 if (oap->op_type == OP_APPEND)
1687 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001688 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001690 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001692 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001693 ind_pre_col = (colnr_T)getwhitecols_curline();
1694 ind_pre_vcol = get_indent();
zeertzjq94b7c322024-03-12 21:50:32 +01001695 pre_textlen = ml_get_len(oap->start.lnum) - bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (oap->op_type == OP_APPEND)
zeertzjq94b7c322024-03-12 21:50:32 +01001697 pre_textlen -= bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699
1700 if (oap->op_type == OP_APPEND)
1701 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001702 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001704 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 curwin->w_set_curswant = TRUE;
1706 while (*ml_get_cursor() != NUL
1707 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1708 ++curwin->w_cursor.col;
1709 if (bd.is_short && !bd.is_MAX)
1710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001711 // First line was too short, make it longer and adjust the
1712 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (u_save_cursor() == FAIL)
1714 return;
1715 for (i = 0; i < bd.endspaces; ++i)
1716 ins_char(' ');
1717 bd.textlen += bd.endspaces;
1718 }
1719 }
1720 else
1721 {
1722 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001723 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001725 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001726 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 && oap->start_vcol != oap->end_vcol)
1728 inc_cursor();
1729 }
1730 }
1731
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001732 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001733 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001734 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001736 // When a tab was inserted, and the characters in front of the tab
1737 // have been converted to a tab as well, the column of the cursor
1738 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001739 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001740 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001741 oap->start = curbuf->b_op_start_orig;
1742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001743 // If user has moved off this line, we don't know what to do, so do
1744 // nothing.
1745 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001746 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 return;
1748
1749 if (oap->block_mode)
1750 {
Christian Brabandtd5c8c092024-05-08 22:17:19 +02001751 int ins_len;
zeertzjq8c55d602024-03-13 20:42:26 +01001752 char_u *firstline, *ins_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001754 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001755 size_t len;
Mike Williamsaca8f552024-04-07 18:26:22 +02001756 size_t add;
zeertzjq8c55d602024-03-13 20:42:26 +01001757 // offset when cursor was moved in insert mode
1758 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001760 // If indent kicked in, the firstline might have changed
1761 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001762 ind_post_col = (colnr_T)getwhitecols_curline();
1763 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001764 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001765 bd.textcol += ind_post_col - ind_pre_col;
1766 ind_post_vcol = get_indent();
1767 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001768 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001769 }
1770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001771 // The user may have moved the cursor before inserting something, try
1772 // to adjust the block for that. But only do it, if the difference
1773 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001774 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1775 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001776 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001777 int t = getviscol2(curbuf->b_op_start_orig.col,
1778 curbuf->b_op_start_orig.coladd);
1779
zeertzjq7745f142022-02-15 11:48:22 +00001780 if (oap->op_type == OP_INSERT
1781 && oap->start.col + oap->start.coladd
1782 != curbuf->b_op_start_orig.col
1783 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001784 {
zeertzjq7745f142022-02-15 11:48:22 +00001785 oap->start.col = curbuf->b_op_start_orig.col;
1786 pre_textlen -= t - oap->start_vcol;
1787 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001788 }
zeertzjq7745f142022-02-15 11:48:22 +00001789 else if (oap->op_type == OP_APPEND
1790 && oap->start.col + oap->start.coladd
1791 >= curbuf->b_op_start_orig.col
1792 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001793 {
zeertzjq7745f142022-02-15 11:48:22 +00001794 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001795 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001796 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001797 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001798 oap->start_vcol = t;
1799 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001800 }
1801 }
1802
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001803 // Spaces and tabs in the indent may have changed to other spaces and
1804 // tabs. Get the starting column again and correct the length.
1805 // Don't do this when "$" used, end-of-line will have changed.
1806 //
1807 // if indent was added and the inserted text was after the indent,
1808 // correct the selection for the new indent.
1809 if (did_indent && bd.textcol - ind_post_col > 0)
1810 {
1811 oap->start.col += ind_post_col - ind_pre_col;
1812 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1813 oap->end.col += ind_post_col - ind_pre_col;
1814 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001817 if (did_indent && bd.textcol - ind_post_col > 0)
1818 {
1819 // undo for where "oap" is used below
1820 oap->start.col -= ind_post_col - ind_pre_col;
1821 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1822 oap->end.col -= ind_post_col - ind_pre_col;
1823 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1826 {
1827 if (oap->op_type == OP_APPEND)
1828 {
1829 pre_textlen += bd2.textlen - bd.textlen;
1830 if (bd2.endspaces)
1831 --bd2.textlen;
1832 }
1833 bd.textcol = bd2.textcol;
1834 bd.textlen = bd2.textlen;
1835 }
1836
1837 /*
1838 * Subsequent calls to ml_get() flush the firstline data - take a
1839 * copy of the required string.
1840 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001841 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001842 len = ml_get_len(oap->start.lnum);
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001843 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001845 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001846 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001847 // account for pressing cursor in insert mode when '$' was used
1848 if (bd.is_MAX
1849 && (start_insert.lnum == Insstart.lnum
1850 && start_insert.col > Insstart.col))
1851 {
1852 offset = (start_insert.col - Insstart.col);
1853 add -= offset;
1854 if (oap->end_vcol > offset)
1855 oap->end_vcol -= (offset + 1);
1856 else
1857 // moved outside of the visual block, what to do?
1858 return;
1859 }
1860 }
Mike Williamsaca8f552024-04-07 18:26:22 +02001861 if (add > len)
zeertzjq94b7c322024-03-12 21:50:32 +01001862 add = len; // short line, point to the NUL
1863 firstline += add;
1864 len -= add;
Mike Williams16b63bd2024-06-01 11:33:40 +02001865 if (pre_textlen >= 0 && (ins_len = (int)len - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001867 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (ins_text != NULL)
1869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001870 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (u_save(oap->start.lnum,
1872 (linenr_T)(oap->end.lnum + 1)) == OK)
John Marriott38b9f452024-04-25 21:39:18 +02001873 block_insert(oap, ins_text, ins_len, (oap->op_type == OP_INSERT),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 &bd);
1875
1876 curwin->w_cursor.col = oap->start.col;
1877 check_cursor();
1878 vim_free(ins_text);
1879 }
1880 }
1881 }
1882}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884/*
1885 * op_change - handle a change operation
1886 *
1887 * return TRUE if edit() returns because of a CTRL-O command
1888 */
1889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001890op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 colnr_T l;
1893 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001895 long pre_textlen = 0;
1896 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 char_u *firstline;
1898 char_u *ins_text, *newp, *oldp;
1899 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 l = oap->start.col;
1902 if (oap->motion_type == MLINE)
1903 {
1904 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001905 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001908 // First delete the text in the region. In an empty buffer only need to
1909 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1911 {
1912 if (u_save_cursor() == FAIL)
1913 return FALSE;
1914 }
1915 else if (op_delete(oap) == FAIL)
1916 return FALSE;
1917
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001918 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 && !virtual_op)
1920 inc_cursor();
1921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001922 // check for still on same line (<CR> in inserted text meaningless)
1923 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (oap->block_mode)
1925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001926 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (virtual_op && (curwin->w_cursor.coladd > 0
1928 || gchar_cursor() == NUL))
1929 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001930 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001931 pre_textlen = ml_get_len(oap->start.lnum);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001932 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 bd.textcol = curwin->w_cursor.col;
1934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (oap->motion_type == MLINE)
1937 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
zeertzjqcdeb6572022-11-15 13:46:12 +00001939 // Reset finish_op now, don't want it set inside edit().
1940 int save_finish_op = finish_op;
1941 finish_op = FALSE;
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 retval = edit(NUL, FALSE, (linenr_T)1);
1944
zeertzjqcdeb6572022-11-15 13:46:12 +00001945 finish_op = save_finish_op;
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001948 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001950 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001952 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
Christian Brabandt1fb9eae2024-06-11 20:30:14 +02001954 int ins_len;
John Marriott38b9f452024-04-25 21:39:18 +02001955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001956 // Auto-indenting may have changed the indent. If the cursor was past
1957 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001959 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001961 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001962
1963 pre_textlen += new_indent - pre_indent;
1964 bd.textcol += new_indent - pre_indent;
1965 }
1966
Christian Brabandt1fb9eae2024-06-11 20:30:14 +02001967 ins_len = (int)ml_get_len(oap->start.lnum) - pre_textlen;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001968 if (ins_len > 0)
1969 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001970 // Subsequent calls to ml_get() flush the firstline data - take a
1971 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001972 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
John Marriott38b9f452024-04-25 21:39:18 +02001974 vim_strncpy(ins_text, firstline + bd.textcol, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1976 linenr++)
1977 {
1978 block_prep(oap, &bd, linenr, TRUE);
1979 if (!bd.is_short || virtual_op)
1980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 pos_T vpos;
John Marriott38b9f452024-04-25 21:39:18 +02001982 size_t newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // If the block starts in virtual space, count the
1985 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (bd.is_short)
1987 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001988 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 else
1992 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 oldp = ml_get(linenr);
John Marriott38b9f452024-04-25 21:39:18 +02001994 newp = alloc(ml_get_len(linenr) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (newp == NULL)
1996 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001997 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001999 newlen = bd.textcol;
2000 vim_memset(newp + newlen, ' ', (size_t)vpos.coladd);
2001 newlen += vpos.coladd;
2002 mch_memmove(newp + newlen, ins_text, ins_len);
2003 STRCPY(newp + newlen + ins_len, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01002005#ifdef FEAT_PROP_POPUP
2006 // Shift the properties for linenr as edit() would do.
2007 if (curbuf->b_has_textprop)
2008 adjust_prop_columns(linenr, bd.textcol,
Mike Williams16b63bd2024-06-01 11:33:40 +02002009 vpos.coladd + (int)ins_len, 0);
LemonBoyb559b302022-05-15 13:08:02 +01002010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 }
2013 check_cursor();
2014
2015 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2016 }
2017 vim_free(ins_text);
2018 }
2019 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02002020 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 return retval;
2023}
2024
2025/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002026 * When the cursor is on the NUL past the end of the line and it should not be
2027 * there move it left.
2028 */
2029 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002031{
Gary Johnson53ba05b2021-07-26 22:19:10 +02002032 unsigned int cur_ve_flags = get_ve_flags();
2033
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002034 int adj_cursor = (curwin->w_cursor.col > 0
2035 && gchar_cursor() == NUL
2036 && (cur_ve_flags & VE_ONEMORE) == 0
2037 && !(restart_edit || (State & MODE_INSERT)));
2038 if (!adj_cursor)
2039 return;
2040
2041 // Put the cursor on the last character in the line.
2042 dec_cursor();
2043
2044 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002046 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002047
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002048 // Coladd is set to the width of the last character.
2049 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
2050 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052}
2053
Bram Moolenaar81340392012-06-06 16:12:59 +02002054/*
2055 * If "process" is TRUE and the line begins with a comment leader (possibly
2056 * after some white space), return a pointer to the text after it. Put a boolean
2057 * value indicating whether the line ends with an unclosed comment in
2058 * "is_comment".
2059 * line - line to be processed,
2060 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002061 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02002062 * include_space - whether to also skip space following the comment leader,
2063 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002064 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002065 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01002066 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002067skip_comment(
2068 char_u *line,
2069 int process,
2070 int include_space,
2071 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02002072{
2073 char_u *comment_flags = NULL;
2074 int lead_len;
2075 int leader_offset = get_last_leader_offset(line, &comment_flags);
2076
2077 *is_comment = FALSE;
2078 if (leader_offset != -1)
2079 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002080 // Let's check whether the line ends with an unclosed comment.
2081 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002082 while (*comment_flags)
2083 {
2084 if (*comment_flags == COM_END
2085 || *comment_flags == ':')
2086 break;
2087 ++comment_flags;
2088 }
2089 if (*comment_flags != COM_END)
2090 *is_comment = TRUE;
2091 }
2092
2093 if (process == FALSE)
2094 return line;
2095
2096 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
2097
2098 if (lead_len == 0)
2099 return line;
2100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002101 // Find:
2102 // - COM_END,
2103 // - colon,
2104 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02002105 while (*comment_flags)
2106 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02002107 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02002108 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02002109 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02002110 ++comment_flags;
2111 }
2112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002113 // If we found a colon, it means that we are not processing a line
2114 // starting with a closing part of a three-part comment. That's good,
2115 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02002116 if (*comment_flags == ':' || *comment_flags == NUL)
2117 line += lead_len;
2118
2119 return line;
2120}
Bram Moolenaar81340392012-06-06 16:12:59 +02002121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01002123 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002124 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002125 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
2126 * leaders should not be removed.
2127 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
2128 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00002130 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 */
2132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002133do_join(
2134 long count,
2135 int insert_space,
2136 int save_undo,
2137 int use_formatoptions UNUSED,
2138 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002140 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002141 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002142 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002144 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02002146 int endcurr1 = NUL;
2147 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002148 int currsize = 0; // size of the current line
2149 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002151 colnr_T col = 0;
2152 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02002153 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002154 int remove_comments = (use_formatoptions == TRUE)
2155 && has_format_option(FO_REMOVE_COMS);
2156 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002157#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002158 int propcount = 0; // number of props over all joined lines
2159 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002162 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2163 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002166 // Allocate an array to store the number of spaces inserted before each
2167 // line. We will use it to pre-compute the length of the new line and the
2168 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002169 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002172 if (remove_comments)
2173 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002174 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002175 if (comments == NULL)
2176 {
2177 vim_free(spaces);
2178 return FAIL;
2179 }
2180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
2182 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002183 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002184 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002185 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002187 for (t = 0; t < count; ++t)
2188 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002189 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002190#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002191 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2192 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002193#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002194 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002195 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002196 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002197 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2198 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2199 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002200 if (remove_comments)
2201 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 // We don't want to remove the comment leader if the
2203 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002204 if (t > 0 && prev_was_comment)
2205 {
2206
2207 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2208 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002209 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002210 curr = new_curr;
2211 }
2212 else
2213 curr = skip_comment(curr, FALSE, insert_space,
2214 &prev_was_comment);
2215 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002216
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002217 if (insert_space && t > 0)
2218 {
2219 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002220 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002221 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002222 && (!has_format_option(FO_MBYTE_JOIN)
2223 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2224 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002225 || (mb_ptr2char(curr) < 0x100
2226 && !(enc_utf8 && utf_eat_space(endcurr1)))
2227 || (endcurr1 < 0x100
2228 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002229 )
2230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002231 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002232 if (endcurr1 == ' ')
2233 endcurr1 = endcurr2;
2234 else
2235 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002236 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002237 if ( p_js
2238 && (endcurr1 == '.'
2239 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2240 && (endcurr1 == '?' || endcurr1 == '!'))))
2241 ++spaces[t];
2242 }
2243 }
2244 currsize = (int)STRLEN(curr);
2245 sumsize += currsize + spaces[t];
2246 endcurr1 = endcurr2 = NUL;
2247 if (insert_space && currsize > 0)
2248 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002249 if (has_mbyte)
2250 {
2251 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002252 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002253 endcurr1 = (*mb_ptr2char)(cend);
2254 if (cend > curr)
2255 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002256 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002257 endcurr2 = (*mb_ptr2char)(cend);
2258 }
2259 }
2260 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002261 {
2262 endcurr1 = *(curr + currsize - 1);
2263 if (currsize > 1)
2264 endcurr2 = *(curr + currsize - 2);
2265 }
2266 }
2267 line_breakcheck();
2268 if (got_int)
2269 {
2270 ret = FAIL;
2271 goto theend;
2272 }
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002275 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002276 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002278 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002279 newp_len = sumsize + 1;
2280#ifdef FEAT_PROP_POPUP
2281 newp_len += propcount * sizeof(textprop_T);
2282#endif
2283 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002284 if (newp == NULL)
2285 {
2286 ret = FAIL;
2287 goto theend;
2288 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002289 cend = newp + sumsize;
2290 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002292 /*
2293 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002294 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002295 *
2296 * Move marks from each deleted line to the joined line, adjusting the
2297 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2298 * should not really be a problem.
2299 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002300#ifdef FEAT_PROP_POPUP
2301 props_remaining = propcount;
2302#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002303 for (t = count - 1; ; --t)
2304 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002305 int spaces_removed;
2306
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002307 cend -= currsize;
2308 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002309
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002310 if (spaces[t] > 0)
2311 {
2312 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002313 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002314 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002315
2316 // If deleting more spaces than adding, the cursor moves no more than
2317 // what is added if it is inside these spaces.
2318 spaces_removed = (curr - curr_start) - spaces[t];
2319
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002320 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002321 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002322#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002323 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2324 curwin->w_cursor.lnum + t, t == count - 1,
2325 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002326#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002327 if (t == 0)
2328 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002329 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002330 if (remove_comments)
2331 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002332 if (insert_space && t > 1)
2333 curr = skipwhite(curr);
2334 currsize = (int)STRLEN(curr);
2335 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002336
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002337 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaare1004402020-10-24 20:49:43 +02002339 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002341 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002342 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002343 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002344 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002345
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002346 // Only report the change in the first line here, del_lines() will report
2347 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 changed_lines(curwin->w_cursor.lnum, currsize,
2349 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002351 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 * briefly, and then move it back. After del_lines() the cursor may
2353 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 */
2355 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002357 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 curwin->w_cursor.lnum = t;
2359
2360 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002361 * Set the cursor column:
2362 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002363 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002365 curwin->w_cursor.col =
2366 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 curwin->w_set_curswant = TRUE;
2371
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002372theend:
2373 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002374 if (remove_comments)
2375 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002376 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377}
2378
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002379#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002381 * Reset 'linebreak' and take care of side effects.
2382 * Returns the previous value, to be passed to restore_lbr().
2383 */
2384 static int
2385reset_lbr(void)
2386{
2387 if (!curwin->w_p_lbr)
2388 return FALSE;
2389 // changing 'linebreak' may require w_virtcol to be updated
2390 curwin->w_p_lbr = FALSE;
2391 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2392 return TRUE;
2393}
2394
2395/*
2396 * Restore 'linebreak' and take care of side effects.
2397 */
2398 static void
2399restore_lbr(int lbr_saved)
2400{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002401 if (curwin->w_p_lbr || !lbr_saved)
2402 return;
2403
2404 // changing 'linebreak' may require w_virtcol to be updated
2405 curwin->w_p_lbr = TRUE;
2406 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002407}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002408#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002409
2410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 * prepare a few things for block mode yank/delete/tilde
2412 *
2413 * for delete:
2414 * - textlen includes the first/last char to be (partly) deleted
2415 * - start/endspaces is the number of columns that are taken by the
2416 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002417 * deleted.
2418 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 * - textlen includes the first/last char to be wholly yanked
2420 * - start/endspaces is the number of columns of the first/last yanked char
2421 * that are to be yanked.
2422 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002423 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002424block_prep(
2425 oparg_T *oap,
2426 struct block_def *bdp,
2427 linenr_T lnum,
2428 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 int incr = 0;
2431 char_u *pend;
2432 char_u *pstart;
2433 char_u *line;
2434 char_u *prev_pstart;
2435 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002436 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002437#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002438 // Avoid a problem with unwanted linebreaks in block mode.
2439 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002440#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002441
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 bdp->startspaces = 0;
2443 bdp->endspaces = 0;
2444 bdp->textlen = 0;
2445 bdp->start_vcol = 0;
2446 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 bdp->is_short = FALSE;
2448 bdp->is_oneChar = FALSE;
2449 bdp->pre_whitesp = 0;
2450 bdp->pre_whitesp_c = 0;
2451 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 bdp->start_char_vcols = 0;
2453
2454 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002456 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2457 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002459 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002460 incr = lbr_chartabsize(&cts);
2461 cts.cts_vcol += incr;
2462 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464 bdp->pre_whitesp += incr;
2465 bdp->pre_whitesp_c++;
2466 }
2467 else
2468 {
2469 bdp->pre_whitesp = 0;
2470 bdp->pre_whitesp_c = 0;
2471 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002472 prev_pstart = cts.cts_ptr;
2473 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002475 bdp->start_vcol = cts.cts_vcol;
2476 pstart = cts.cts_ptr;
2477 clear_chartabsize_arg(&cts);
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (!is_del || oap->op_type == OP_APPEND)
2485 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2486 }
2487 else
2488 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002489 // notice: this converts partly selected Multibyte characters to
2490 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2492 if (is_del && bdp->startspaces)
2493 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2494 pend = pstart;
2495 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002496 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (oap->op_type == OP_INSERT)
2500 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2501 else if (oap->op_type == OP_APPEND)
2502 {
2503 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2504 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2505 }
2506 else
2507 {
2508 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2509 if (is_del && oap->op_type != OP_LSHIFT)
2510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002511 // just putting the sum of those two into
2512 // bdp->startspaces doesn't work for Visual replace,
2513 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 bdp->startspaces = bdp->start_char_vcols
2515 - (bdp->start_vcol - oap->start_vcol);
2516 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2517 }
2518 }
2519 }
2520 else
2521 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002522 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2523 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002525 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002527 // count a tab for what it's worth (if list mode not on)
2528 prev_pend = cts.cts_ptr;
2529 incr = lbr_chartabsize_adv(&cts);
2530 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002532 bdp->end_vcol = cts.cts_vcol;
2533 pend = cts.cts_ptr;
2534 clear_chartabsize_arg(&cts);
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (bdp->end_vcol <= oap->end_vcol
2537 && (!is_del
2538 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002542 // Alternative: include spaces to fill up the block.
2543 // Disadvantage: can lead to trailing spaces when the line is
2544 // short where the text is put
2545 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (oap->op_type == OP_APPEND || virtual_op)
2547 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002548 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002550 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552 else if (bdp->end_vcol > oap->end_vcol)
2553 {
2554 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2555 if (!is_del && bdp->endspaces)
2556 {
2557 bdp->endspaces = incr - bdp->endspaces;
2558 if (pend != pstart)
2559 pend = prev_pend;
2560 }
2561 }
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (is_del && bdp->startspaces)
2565 pstart = prev_pstart;
2566 bdp->textlen = (int)(pend - pstart);
2567 }
2568 bdp->textcol = (colnr_T) (pstart - line);
2569 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002570#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002571 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/*
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002576 * Get block text from "start" to "end"
2577 */
2578 void
2579charwise_block_prep(
2580 pos_T start,
2581 pos_T end,
2582 struct block_def *bdp,
2583 linenr_T lnum,
2584 int inclusive)
2585{
2586 colnr_T startcol = 0, endcol = MAXCOL;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002587 colnr_T cs, ce;
2588 char_u *p;
Christian Brabandtc9a1e252025-01-11 15:25:00 +01002589 int plen = ml_get_len(lnum);
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002590
2591 p = ml_get(lnum);
2592 bdp->startspaces = 0;
2593 bdp->endspaces = 0;
zeertzjq52a6f342024-05-22 16:42:44 +02002594 bdp->is_oneChar = FALSE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002595 bdp->start_char_vcols = 0;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002596
2597 if (lnum == start.lnum)
2598 {
2599 startcol = start.col;
2600 if (virtual_op)
2601 {
2602 getvcol(curwin, &start, &cs, NULL, &ce);
2603 if (ce != cs && start.coladd > 0)
2604 {
2605 // Part of a tab selected -- but don't
2606 // double-count it.
zeertzjqc95e64f2024-05-20 14:00:31 +02002607 bdp->start_char_vcols = ce - cs + 1;
2608 bdp->startspaces = bdp->start_char_vcols - start.coladd;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002609 if (bdp->startspaces < 0)
2610 bdp->startspaces = 0;
2611 startcol++;
2612 }
2613 }
2614 }
2615
2616 if (lnum == end.lnum)
2617 {
2618 endcol = end.col;
2619 if (virtual_op)
2620 {
2621 getvcol(curwin, &end, &cs, NULL, &ce);
2622 if (p[endcol] == NUL || (cs + end.coladd < ce
2623 // Don't add space for double-wide
2624 // char; endcol will be on last byte
2625 // of multi-byte char.
2626 && (*mb_head_off)(p, p + endcol) == 0))
2627 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002628 if (start.lnum == end.lnum && start.col == end.col)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002629 {
2630 // Special case: inside a single char
zeertzjq52a6f342024-05-22 16:42:44 +02002631 bdp->is_oneChar = TRUE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002632 bdp->startspaces = end.coladd - start.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002633 endcol = startcol;
2634 }
2635 else
2636 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002637 bdp->endspaces = end.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002638 endcol -= inclusive;
2639 }
2640 }
2641 }
2642 }
2643 if (endcol == MAXCOL)
zeertzjq94b7c322024-03-12 21:50:32 +01002644 endcol = ml_get_len(lnum);
zeertzjq52a6f342024-05-22 16:42:44 +02002645 if (startcol > endcol || bdp->is_oneChar)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002646 bdp->textlen = 0;
2647 else
2648 bdp->textlen = endcol - startcol + inclusive;
zeertzjqc95e64f2024-05-20 14:00:31 +02002649 bdp->textcol = startcol;
Christian Brabandtc9a1e252025-01-11 15:25:00 +01002650 bdp->textstart = startcol <= plen ? p + startcol : p;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002651}
2652
2653/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002654 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002657op_addsub(
2658 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 linenr_T Prenum1, // Amount of add/subtract
2660 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002662 pos_T pos;
2663 struct block_def bd;
2664 int change_cnt = 0;
2665 linenr_T amount = Prenum1;
2666
Gary Johnson88d4f252024-06-01 20:51:33 +02002667 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
2668 // buffer is not completely updated yet. Postpone updating folds until before
2669 // the call to changed_lines().
Bram Moolenaar6b731882018-11-16 20:54:47 +01002670#ifdef FEAT_FOLDING
Gary Johnson88d4f252024-06-01 20:51:33 +02002671 disable_fold_update++;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002672#endif
2673
Bram Moolenaard79e5502016-01-10 22:13:02 +01002674 if (!VIsual_active)
2675 {
2676 pos = curwin->w_cursor;
2677 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002678 {
2679#ifdef FEAT_FOLDING
2680 disable_fold_update--;
2681#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002682 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002683 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002684 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002685#ifdef FEAT_FOLDING
2686 disable_fold_update--;
2687#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002688 if (change_cnt)
2689 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2690 }
2691 else
2692 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002693 int one_change;
2694 int length;
2695 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002696
2697 if (u_save((linenr_T)(oap->start.lnum - 1),
2698 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002699 {
2700#ifdef FEAT_FOLDING
2701 disable_fold_update--;
2702#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002703 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002704 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002705
2706 pos = oap->start;
2707 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002710 {
2711 block_prep(oap, &bd, pos.lnum, FALSE);
2712 pos.col = bd.textcol;
2713 length = bd.textlen;
2714 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002715 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002716 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002717 curwin->w_cursor.col = 0;
2718 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01002719 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002720 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002722 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002723 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002724 dec(&(oap->end));
zeertzjq94b7c322024-03-12 21:50:32 +01002725 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002726 pos.col = 0;
2727 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002728 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002729 pos.col += oap->start.col;
2730 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002731 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002732 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002733 {
zeertzjq94b7c322024-03-12 21:50:32 +01002734 length = ml_get_len(oap->end.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002735 if (oap->end.col >= length)
2736 oap->end.col = length - 1;
2737 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002738 }
2739 }
2740 one_change = do_addsub(oap->op_type, &pos, length, amount);
2741 if (one_change)
2742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002744 if (change_cnt == 0)
2745 startpos = curbuf->b_op_start;
2746 ++change_cnt;
2747 }
2748
2749#ifdef FEAT_NETBEANS_INTG
2750 if (netbeans_active() && one_change)
2751 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002752 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002753
2754 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002755 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002756 netbeans_inserted(curbuf, pos.lnum, pos.col,
2757 &ptr[pos.col], length);
2758 }
2759#endif
2760 if (g_cmd && one_change)
2761 amount += Prenum1;
2762 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002763
2764#ifdef FEAT_FOLDING
2765 disable_fold_update--;
2766#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002767 if (change_cnt)
2768 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2769
2770 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002772 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // Set '[ mark if something changed. Keep the last end
2775 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002776 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002777 curbuf->b_op_start = startpos;
2778
2779 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002780 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002781 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002782 }
2783}
2784
2785/*
2786 * Add or subtract 'Prenum1' from a number in a line
2787 * op_type is OP_NR_ADD or OP_NR_SUB
2788 *
2789 * Returns TRUE if some character was changed.
2790 */
2791 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002792do_addsub(
2793 int op_type,
2794 pos_T *pos,
2795 int length,
2796 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002797{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 int col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2800 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002801 uvarnumber_T n;
2802 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 char_u *ptr;
zeertzjq8c55d602024-03-13 20:42:26 +01002804 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002807 int do_hex;
2808 int do_oct;
2809 int do_bin;
2810 int do_alpha;
2811 int do_unsigned;
distobs25ac6d62024-07-06 17:50:09 +02002812 int do_blank;
2813 int blank_unsigned = FALSE; // blank: treat as unsigned?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002816 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002817 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002818 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002819 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002820 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002821 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002822 pos_T startpos;
2823 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002824 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002826 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2827 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2828 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2829 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2830 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
distobs25ac6d62024-07-06 17:50:09 +02002831 do_blank = (vim_strchr(curbuf->b_p_nf, 'k') != NULL); // "blanK"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002833 if (virtual_active())
2834 {
2835 save_coladd = pos->coladd;
2836 pos->coladd = 0;
2837 }
2838
Bram Moolenaard79e5502016-01-10 22:13:02 +01002839 curwin->w_cursor = *pos;
2840 ptr = ml_get(pos->lnum);
zeertzjq8c55d602024-03-13 20:42:26 +01002841 linelen = ml_get_len(pos->lnum);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002842 col = pos->col;
2843
zeertzjq8c55d602024-03-13 20:42:26 +01002844 if (col + !!save_coladd >= linelen)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002845 goto theend;
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 /*
2848 * First check if we are on a hexadecimal number, after the "0x".
2849 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002850 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002852 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002853 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002854 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002855 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002856 if (has_mbyte)
2857 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002858 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002859
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002860 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002861 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002862 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002863 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002864 if (has_mbyte)
2865 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002866 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002867
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002868 if ( do_bin
2869 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002870 && ! ((col > 0
2871 && (ptr[col] == 'X'
2872 || ptr[col] == 'x')
2873 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002874 && (!has_mbyte ||
2875 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002876 && vim_isxdigit(ptr[col + 1]))))
2877 {
2878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002880
Bram Moolenaard79e5502016-01-10 22:13:02 +01002881 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002882
2883 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002884 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002885 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002886 if (has_mbyte)
2887 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002888 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002889 }
2890
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002891 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002892 && col > 0
2893 && (ptr[col] == 'X'
2894 || ptr[col] == 'x')
2895 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002896 && (!has_mbyte ||
2897 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002898 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002899 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002900 && col > 0
2901 && (ptr[col] == 'B'
2902 || ptr[col] == 'b')
2903 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002904 && (!has_mbyte ||
2905 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002906 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002909 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002910 if (has_mbyte)
2911 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002912 }
2913 else
2914 {
2915 /*
2916 * Search forward and then backward to find the start of number.
2917 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002918 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002919
2920 while (ptr[col] != NUL
2921 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002922 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002923 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002924
2925 while (col > 0
2926 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002927 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002928 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002929 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002930 if (has_mbyte)
2931 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002932 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002933 }
2934 }
2935
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002936 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002937 {
2938 while (ptr[col] != NUL && length > 0
2939 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002940 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002941 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002942 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002943
2944 col += mb_len;
2945 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002946 }
2947
2948 if (length == 0)
2949 goto theend;
2950
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002951 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002952 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2953 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002954 {
distobs25ac6d62024-07-06 17:50:09 +02002955 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
2956 blank_unsigned = TRUE;
2957 else
2958 {
2959 negative = TRUE;
2960 was_positive = FALSE;
2961 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002962 }
2963 }
2964
2965 /*
2966 * If a number was found, and saving for undo works, replace the number.
2967 */
2968 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002969 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002970 {
2971 beep_flush();
2972 goto theend;
2973 }
2974
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002975 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002976 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002978 if (op_type == OP_NR_SUB)
2979 {
2980 if (CharOrd(firstdigit) < Prenum1)
2981 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002982 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002983 firstdigit = 'A';
2984 else
2985 firstdigit = 'a';
2986 }
2987 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002988 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002989 }
2990 else
2991 {
2992 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2993 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002994 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002995 firstdigit = 'Z';
2996 else
2997 firstdigit = 'z';
2998 }
2999 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01003000 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003001 }
3002 curwin->w_cursor.col = col;
3003 if (!did_change)
3004 startpos = curwin->w_cursor;
3005 did_change = TRUE;
3006 (void)del_char(FALSE);
3007 ins_char(firstdigit);
3008 endpos = curwin->w_cursor;
3009 curwin->w_cursor.col = col;
3010 }
3011 else
3012 {
John Marriottf4b36412025-02-23 09:09:59 +01003013 char_u *buf1;
3014 int buf1len;
3015 char_u buf2[NUMBUFLEN];
3016 int buf2len;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003017 pos_T save_pos;
3018 int i;
3019
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003020 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003021 && (!has_mbyte ||
3022 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003023 && !visual
3024 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003025 {
distobs25ac6d62024-07-06 17:50:09 +02003026 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
3027 blank_unsigned = TRUE;
3028 else
3029 {
3030 // negative number
3031 --col;
3032 negative = TRUE;
3033 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003034 }
distobs25ac6d62024-07-06 17:50:09 +02003035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003037 if (visual && VIsual_mode != 'V')
3038 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
zeertzjq8c55d602024-03-13 20:42:26 +01003039 ? linelen - col : length);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003040
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00003041 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003042 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003043 0 + (do_bin ? STR2NR_BIN : 0)
3044 + (do_oct ? STR2NR_OCT : 0)
3045 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00003046 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003048 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01003049 if (pre && negative)
3050 {
3051 ++col;
3052 --length;
3053 negative = FALSE;
3054 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01003056 subtract = FALSE;
3057 if (op_type == OP_NR_SUB)
3058 subtract ^= TRUE;
3059 if (negative)
3060 subtract ^= TRUE;
3061
3062 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00003063 if (!overflow) // if number is too big don't add/subtract
3064 {
3065 if (subtract)
3066 n -= (uvarnumber_T)Prenum1;
3067 else
3068 n += (uvarnumber_T)Prenum1;
3069 }
3070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003071 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01003072 if (!pre)
3073 {
3074 if (subtract)
3075 {
3076 if (n > oldn)
3077 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003078 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003079 negative ^= TRUE;
3080 }
3081 }
3082 else
3083 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01003085 if (n < oldn)
3086 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003087 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003088 negative ^= TRUE;
3089 }
3090 }
3091 if (n == 0)
3092 negative = FALSE;
3093 }
3094
distobs25ac6d62024-07-06 17:50:09 +02003095 if ((do_unsigned || blank_unsigned) && negative)
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003096 {
3097 if (subtract)
3098 // sticking at zero.
3099 n = (uvarnumber_T)0;
3100 else
3101 // sticking at 2^64 - 1.
3102 n = (uvarnumber_T)(-1);
3103 negative = FALSE;
3104 }
3105
Bram Moolenaard79e5502016-01-10 22:13:02 +01003106 if (visual && !was_positive && !negative && col > 0)
3107 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01003109 col--;
3110 length++;
3111 }
3112
3113 /*
3114 * Delete the old number.
3115 */
3116 curwin->w_cursor.col = col;
3117 if (!did_change)
3118 startpos = curwin->w_cursor;
3119 did_change = TRUE;
3120 todel = length;
3121 c = gchar_cursor();
3122 /*
3123 * Don't include the '-' in the length, only the length of the
3124 * part after it is kept the same.
3125 */
3126 if (c == '-')
3127 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003128
3129 save_pos = curwin->w_cursor;
3130 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003131 {
Keith Thompson184f71c2024-01-04 21:19:04 +01003132 if (c < 0x100 && SAFE_isalpha(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003133 {
Keith Thompson184f71c2024-01-04 21:19:04 +01003134 if (SAFE_isupper(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003135 hexupper = TRUE;
3136 else
3137 hexupper = FALSE;
3138 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003139 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01003140 c = gchar_cursor();
3141 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003142 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003143
3144 /*
3145 * Prepare the leading characters in buf1[].
3146 * When there are many leading zeros it could be very long.
3147 * Allocate a bit too much.
3148 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003149 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003150 if (buf1 == NULL)
3151 goto theend;
3152 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003153 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003154 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003155 if (pre)
3156 {
3157 *ptr++ = '0';
3158 --length;
3159 }
3160 if (pre == 'b' || pre == 'B' ||
3161 pre == 'x' || pre == 'X')
3162 {
3163 *ptr++ = pre;
3164 --length;
3165 }
3166
3167 /*
3168 * Put the number characters in buf2[].
3169 */
3170 if (pre == 'b' || pre == 'B')
3171 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01003172 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003173 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003176 for (bit = bits; bit > 0; bit--)
3177 if ((n >> (bit - 1)) & 0x1) break;
3178
John Marriottf4b36412025-02-23 09:09:59 +01003179 for (buf2len = 0; bit > 0 && buf2len < (NUMBUFLEN - 1); bit--)
3180 buf2[buf2len++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003181
John Marriottf4b36412025-02-23 09:09:59 +01003182 buf2[buf2len] = NUL;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003183 }
3184 else if (pre == 0)
John Marriottf4b36412025-02-23 09:09:59 +01003185 buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003186 else if (pre == '0')
John Marriottf4b36412025-02-23 09:09:59 +01003187 buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003188 else if (pre && hexupper)
John Marriottf4b36412025-02-23 09:09:59 +01003189 buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003190 else
John Marriottf4b36412025-02-23 09:09:59 +01003191 buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
3192 length -= buf2len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003193
3194 /*
3195 * Adjust number of zeros to the new number of digits, so the
3196 * total length of the number remains the same.
3197 * Don't do this when
3198 * the result may look like an octal number.
3199 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003200 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003201 while (length-- > 0)
3202 *ptr++ = '0';
3203 *ptr = NUL;
John Marriottf4b36412025-02-23 09:09:59 +01003204 buf1len = (int)(ptr - buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003205
John Marriottf4b36412025-02-23 09:09:59 +01003206 STRCPY(buf1 + buf1len, buf2);
3207 buf1len += buf2len;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003208
3209 // Insert just after the first character to be removed, so that any
3210 // text properties will be adjusted. Then delete the old number
3211 // afterwards.
3212 save_pos = curwin->w_cursor;
3213 if (todel > 0)
3214 inc_cursor();
John Marriottf4b36412025-02-23 09:09:59 +01003215 ins_str(buf1, (size_t)buf1len); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003216 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003217
3218 // del_char() will also mark line needing displaying
3219 if (todel > 0)
3220 {
zeertzjq94b7c322024-03-12 21:50:32 +01003221 int bytes_after = ml_get_curline_len() - curwin->w_cursor.col;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003222
3223 // Delete the one character before the insert.
3224 curwin->w_cursor = save_pos;
3225 (void)del_char(FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01003226 curwin->w_cursor.col = ml_get_curline_len() - bytes_after;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003227 --todel;
3228 }
3229 while (todel-- > 0)
3230 (void)del_char(FALSE);
3231
Bram Moolenaard79e5502016-01-10 22:13:02 +01003232 endpos = curwin->w_cursor;
3233 if (did_change && curwin->w_cursor.col)
3234 --curwin->w_cursor.col;
3235 }
3236
Bram Moolenaare1004402020-10-24 20:49:43 +02003237 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003238 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003239 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003240 curbuf->b_op_start = startpos;
3241 curbuf->b_op_end = endpos;
3242 if (curbuf->b_op_end.col > 0)
3243 --curbuf->b_op_end.col;
3244 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003245
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003246theend:
3247 if (visual)
3248 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003249 else if (did_change)
3250 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003251 else if (virtual_active())
3252 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003253
Bram Moolenaard79e5502016-01-10 22:13:02 +01003254 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255}
3256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003258clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003260 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003264 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 *
3266 * "Words" are counted by looking for boundaries between non-space and
3267 * space characters. (it seems to produce results that match 'wc'.)
3268 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003269 * Return value is byte count; word count for the line is added to "*wc".
3270 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 *
3272 * The function will only examine the first "limit" characters in the
3273 * line, stopping if it encounters an end-of-line (NUL byte). In that
3274 * case, eol_size will be added to the character count to account for
3275 * the size of the EOL character.
3276 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003277 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003278line_count_info(
3279 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003280 varnumber_T *wc,
3281 varnumber_T *cc,
3282 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003285 varnumber_T i;
3286 varnumber_T words = 0;
3287 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 int is_word = 0;
3289
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003290 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
3292 if (is_word)
3293 {
3294 if (vim_isspace(line[i]))
3295 {
3296 words++;
3297 is_word = 0;
3298 }
3299 }
3300 else if (!vim_isspace(line[i]))
3301 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003302 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003303 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305
3306 if (is_word)
3307 words++;
3308 *wc += words;
3309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003310 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003311 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003314 chars += eol_size;
3315 }
3316 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return i;
3318}
3319
3320/*
3321 * Give some info about the position of the cursor (for "g CTRL-G").
3322 * In Visual mode, give some info about the selected region. (In this case,
3323 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003324 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 */
3326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003327cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003330 char_u buf1[50];
3331 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003333 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003334 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003335 varnumber_T byte_count_cursor = 0;
3336 varnumber_T char_count = 0;
3337 varnumber_T char_count_cursor = 0;
3338 varnumber_T word_count = 0;
3339 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003340 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003341 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 long line_count_selected = 0;
3343 pos_T min_pos, max_pos;
3344 oparg_T oparg;
3345 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347 /*
3348 * Compute the length of the file in characters.
3349 */
3350 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3351 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003352 if (dict == NULL)
3353 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003354 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003355 return;
3356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358 else
3359 {
3360 if (get_fileformat(curbuf) == EOL_DOS)
3361 eol_size = 2;
3362 else
3363 eol_size = 1;
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (VIsual_active)
3366 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003367 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
3369 min_pos = VIsual;
3370 max_pos = curwin->w_cursor;
3371 }
3372 else
3373 {
3374 min_pos = curwin->w_cursor;
3375 max_pos = VIsual;
3376 }
3377 if (*p_sel == 'e' && max_pos.col > 0)
3378 --max_pos.col;
3379
3380 if (VIsual_mode == Ctrl_V)
3381 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003382#ifdef FEAT_LINEBREAK
3383 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003384 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003386 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003387 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003388 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 oparg.is_VIsual = 1;
3391 oparg.block_mode = TRUE;
3392 oparg.op_type = OP_NOP;
3393 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003394 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003395#ifdef FEAT_LINEBREAK
3396 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003397 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003398#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003399 if (curwin->w_curswant == MAXCOL)
3400 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003401 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (oparg.end_vcol < oparg.start_vcol)
3403 {
3404 oparg.end_vcol += oparg.start_vcol;
3405 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3406 oparg.end_vcol -= oparg.start_vcol;
3407 }
3408 }
3409 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3413 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003414 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003415 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
3417 ui_breakcheck();
3418 if (got_int)
3419 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003420 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (VIsual_active
3425 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3426 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003427 char_u *s = NULL;
3428 long len = 0L;
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 switch (VIsual_mode)
3431 {
3432 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003436 s = bd.textstart;
3437 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 break;
3439 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003440 s = ml_get(lnum);
3441 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 break;
3443 case 'v':
3444 {
3445 colnr_T start_col = (lnum == min_pos.lnum)
3446 ? min_pos.col : 0;
3447 colnr_T end_col = (lnum == max_pos.lnum)
3448 ? max_pos.col - start_col + 1 : MAXCOL;
3449
Bram Moolenaardef9e822004-12-31 20:58:58 +00003450 s = ml_get(lnum) + start_col;
3451 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 break;
3454 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003455 if (s != NULL)
3456 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003457 byte_count_cursor += line_count_info(s, &word_count_cursor,
3458 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003459 if (lnum == curbuf->b_ml.ml_line_count
3460 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003461 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003462 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003463 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (lnum == curwin->w_cursor.lnum)
3470 {
3471 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003472 char_count_cursor += char_count;
3473 byte_count_cursor = byte_count +
3474 line_count_info(ml_get(lnum),
3475 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003476 (varnumber_T)(curwin->w_cursor.col + 1),
3477 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003480 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003481 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003482 &char_count, (varnumber_T)MAXCOL,
3483 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003486 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003487 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003488 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaared767a22016-01-03 22:49:16 +01003490 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003492 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003494 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3495 {
3496 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3497 &max_pos.col);
3498 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3499 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3500 }
3501 else
3502 buf1[0] = NUL;
3503
3504 if (char_count_cursor == byte_count_cursor
3505 && char_count == byte_count)
3506 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003507 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003508 buf1, line_count_selected,
3509 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003510 word_count_cursor,
3511 word_count,
3512 byte_count_cursor,
3513 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003514 else
3515 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003516 _("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 +01003517 buf1, line_count_selected,
3518 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003519 word_count_cursor,
3520 word_count,
3521 char_count_cursor,
3522 char_count,
3523 byte_count_cursor,
3524 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003527 {
3528 p = ml_get_curline();
3529 validate_virtcol();
3530 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3531 (int)curwin->w_virtcol + 1);
zeertzjq94b7c322024-03-12 21:50:32 +01003532 col_print(buf2, sizeof(buf2), ml_get_curline_len(),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003533 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaared767a22016-01-03 22:49:16 +01003535 if (char_count_cursor == byte_count_cursor
3536 && char_count == byte_count)
3537 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003538 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003539 (char *)buf1, (char *)buf2,
3540 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003542 word_count_cursor, word_count,
3543 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003544 else
3545 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003546 _("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 +01003547 (char *)buf1, (char *)buf2,
3548 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003549 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003550 word_count_cursor, word_count,
3551 char_count_cursor, char_count,
3552 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003553 }
3554 }
3555
Bram Moolenaared767a22016-01-03 22:49:16 +01003556 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003557 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003558 {
3559 size_t len = STRLEN(IObuff);
3560
3561 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003562 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003563 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003564 if (dict == NULL)
3565 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003566 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003567 p = p_shm;
3568 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003569 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003570 p_shm = p;
3571 }
3572 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003573#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003574 if (dict != NULL)
3575 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003576 dict_add_number(dict, "words", word_count);
3577 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003578 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003579 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3580 byte_count_cursor);
3581 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3582 char_count_cursor);
3583 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3584 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003588
3589/*
3590 * Handle indent and format operators and visual mode ":".
3591 */
3592 static void
3593op_colon(oparg_T *oap)
3594{
3595 stuffcharReadbuff(':');
3596 if (oap->is_VIsual)
3597 stuffReadbuff((char_u *)"'<,'>");
3598 else
3599 {
3600 // Make the range look nice, so it can be repeated.
3601 if (oap->start.lnum == curwin->w_cursor.lnum)
3602 stuffcharReadbuff('.');
3603 else
3604 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003605
3606#ifdef FEAT_FOLDING
3607 // When using !! on a closed fold the range ".!" works best to operate
3608 // on, it will be made the whole closed fold later.
3609 linenr_T endOfStartFold = oap->start.lnum;
3610 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3611#endif
3612 if (oap->end.lnum != oap->start.lnum
3613#ifdef FEAT_FOLDING
3614 && oap->end.lnum != endOfStartFold
3615#endif
3616 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003617 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003618 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003619 stuffcharReadbuff(',');
3620 if (oap->end.lnum == curwin->w_cursor.lnum)
3621 stuffcharReadbuff('.');
3622 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3623 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003624 else if (oap->start.lnum == curwin->w_cursor.lnum
3625#ifdef FEAT_FOLDING
3626 // do not use ".+number" for a closed fold, it would count
3627 // folded lines twice
3628 && !hasFolding(oap->end.lnum, NULL, NULL)
3629#endif
3630 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003631 {
3632 stuffReadbuff((char_u *)".+");
3633 stuffnumReadbuff((long)oap->line_count - 1);
3634 }
3635 else
3636 stuffnumReadbuff((long)oap->end.lnum);
3637 }
3638 }
3639 if (oap->op_type != OP_COLON)
3640 stuffReadbuff((char_u *)"!");
3641 if (oap->op_type == OP_INDENT)
3642 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003643 if (*get_equalprg() == NUL)
3644 stuffReadbuff((char_u *)"indent");
3645 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003646 stuffReadbuff(get_equalprg());
3647 stuffReadbuff((char_u *)"\n");
3648 }
3649 else if (oap->op_type == OP_FORMAT)
3650 {
3651 if (*curbuf->b_p_fp != NUL)
3652 stuffReadbuff(curbuf->b_p_fp);
3653 else if (*p_fp != NUL)
3654 stuffReadbuff(p_fp);
3655 else
3656 stuffReadbuff((char_u *)"fmt");
3657 stuffReadbuff((char_u *)"\n']");
3658 }
3659
3660 // do_cmdline() does the rest
3661}
3662
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003663// callback function for 'operatorfunc'
3664static callback_T opfunc_cb;
3665
3666/*
3667 * Process the 'operatorfunc' option value.
3668 * Returns OK or FAIL.
3669 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003670 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003671did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003672{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003673 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3674 return e_invalid_argument;
3675
3676 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003677}
3678
3679#if defined(EXITFREE) || defined(PROTO)
3680 void
3681free_operatorfunc_option(void)
3682{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003683# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003684 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003685# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003686}
3687#endif
3688
Dominique Pelle748b3082022-01-08 12:41:16 +00003689#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003690/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003691 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003692 * garbage collected.
3693 */
3694 int
3695set_ref_in_opfunc(int copyID UNUSED)
3696{
3697 int abort = FALSE;
3698
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003699 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003700
3701 return abort;
3702}
Dominique Pelle748b3082022-01-08 12:41:16 +00003703#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003704
3705/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003706 * Handle the "g@" operator: call 'operatorfunc'.
3707 */
3708 static void
3709op_function(oparg_T *oap UNUSED)
3710{
3711#ifdef FEAT_EVAL
3712 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003713 pos_T orig_start = curbuf->b_op_start;
3714 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003715 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003716
3717 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003718 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003719 else
3720 {
3721 // Set '[ and '] marks to text to be operated on.
3722 curbuf->b_op_start = oap->start;
3723 curbuf->b_op_end = oap->end;
3724 if (oap->motion_type != MLINE && !oap->inclusive)
3725 // Exclude the end position.
3726 decl(&curbuf->b_op_end);
3727
3728 argv[0].v_type = VAR_STRING;
3729 if (oap->block_mode)
3730 argv[0].vval.v_string = (char_u *)"block";
3731 else if (oap->motion_type == MLINE)
3732 argv[0].vval.v_string = (char_u *)"line";
3733 else
3734 argv[0].vval.v_string = (char_u *)"char";
3735 argv[1].v_type = VAR_UNKNOWN;
3736
3737 // Reset virtual_op so that 'virtualedit' can be changed in the
3738 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003739 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003740 virtual_op = MAYBE;
3741
naohiro ono75c30e92021-10-19 11:15:41 +01003742 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003743 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003744 finish_op = FALSE;
3745
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003746 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3747 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003748
3749 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003750 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003751 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003752 {
3753 curbuf->b_op_start = orig_start;
3754 curbuf->b_op_end = orig_end;
3755 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003756 }
3757#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003758 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003759#endif
3760}
3761
3762/*
3763 * Calculate start/end virtual columns for operating in block mode.
3764 */
3765 static void
3766get_op_vcol(
3767 oparg_T *oap,
3768 colnr_T redo_VIsual_vcol,
3769 int initial) // when TRUE adjust position for 'selectmode'
3770{
3771 colnr_T start, end;
3772
3773 if (VIsual_mode != Ctrl_V
3774 || (!initial && oap->end.col < curwin->w_width))
3775 return;
3776
3777 oap->block_mode = TRUE;
3778
3779 // prevent from moving onto a trail byte
3780 if (has_mbyte)
3781 mb_adjustpos(curwin->w_buffer, &oap->end);
3782
3783 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3784
3785 if (!redo_VIsual_busy)
3786 {
3787 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3788
3789 if (start < oap->start_vcol)
3790 oap->start_vcol = start;
3791 if (end > oap->end_vcol)
3792 {
3793 if (initial && *p_sel == 'e' && start >= 1
3794 && start - 1 >= oap->end_vcol)
3795 oap->end_vcol = start - 1;
3796 else
3797 oap->end_vcol = end;
3798 }
3799 }
3800
3801 // if '$' was used, get oap->end_vcol from longest line
3802 if (curwin->w_curswant == MAXCOL)
3803 {
3804 curwin->w_cursor.col = MAXCOL;
3805 oap->end_vcol = 0;
3806 for (curwin->w_cursor.lnum = oap->start.lnum;
3807 curwin->w_cursor.lnum <= oap->end.lnum;
3808 ++curwin->w_cursor.lnum)
3809 {
3810 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3811 if (end > oap->end_vcol)
3812 oap->end_vcol = end;
3813 }
3814 }
3815 else if (redo_VIsual_busy)
3816 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3817 // Correct oap->end.col and oap->start.col to be the
3818 // upper-left and lower-right corner of the block area.
3819 //
3820 // (Actually, this does convert column positions into character
3821 // positions)
3822 curwin->w_cursor.lnum = oap->end.lnum;
3823 coladvance(oap->end_vcol);
3824 oap->end = curwin->w_cursor;
3825
3826 curwin->w_cursor = oap->start;
3827 coladvance(oap->start_vcol);
3828 oap->start = curwin->w_cursor;
3829}
3830
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003831// Information for redoing the previous Visual selection.
3832typedef struct {
3833 int rv_mode; // 'v', 'V', or Ctrl-V
3834 linenr_T rv_line_count; // number of lines
3835 colnr_T rv_vcol; // number of cols or end column
3836 long rv_count; // count for Visual operator
3837 int rv_arg; // extra argument
3838} redo_VIsual_T;
3839
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003840 static int
3841is_ex_cmdchar(cmdarg_T *cap)
3842{
3843 return cap->cmdchar == ':'
3844 || cap->cmdchar == K_COMMAND
3845 || cap->cmdchar == K_SCRIPT_COMMAND;
3846}
3847
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003848/*
3849 * Handle an operator after Visual mode or when the movement is finished.
3850 * "gui_yank" is true when yanking text for the clipboard.
3851 */
3852 void
3853do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3854{
3855 oparg_T *oap = cap->oap;
3856 pos_T old_cursor;
3857 int empty_region_error;
3858 int restart_edit_save;
3859#ifdef FEAT_LINEBREAK
3860 int lbr_saved = curwin->w_p_lbr;
3861#endif
3862
3863 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003864 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3865
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003866 int include_line_break = FALSE;
3867
3868#if defined(FEAT_CLIPBOARD)
3869 // Yank the visual area into the GUI selection register before we operate
3870 // on it and lose it forever.
3871 // Don't do it if a specific register was specified, so that ""x"*P works.
3872 // This could call do_pending_operator() recursively, but that's OK
3873 // because gui_yank will be TRUE for the nested call.
3874 if ((clip_star.available || clip_plus.available)
3875 && oap->op_type != OP_NOP
3876 && !gui_yank
3877 && VIsual_active
3878 && !redo_VIsual_busy
3879 && oap->regname == 0)
3880 clip_auto_select();
3881#endif
3882 old_cursor = curwin->w_cursor;
3883
3884 // If an operation is pending, handle it...
3885 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3886 {
3887 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3888 // for the clipboard.
3889 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3890
3891#ifdef FEAT_LINEBREAK
3892 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003893 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003894#endif
3895 oap->is_VIsual = VIsual_active;
3896 if (oap->motion_force == 'V')
3897 oap->motion_type = MLINE;
3898 else if (oap->motion_force == 'v')
3899 {
3900 // If the motion was linewise, "inclusive" will not have been set.
3901 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3902 if (oap->motion_type == MLINE)
3903 oap->inclusive = FALSE;
3904 // If the motion already was characterwise, toggle "inclusive"
3905 else if (oap->motion_type == MCHAR)
3906 oap->inclusive = !oap->inclusive;
3907 oap->motion_type = MCHAR;
3908 }
3909 else if (oap->motion_force == Ctrl_V)
3910 {
3911 // Change line- or characterwise motion into Visual block mode.
3912 if (!VIsual_active)
3913 {
3914 VIsual_active = TRUE;
3915 VIsual = oap->start;
3916 }
3917 VIsual_mode = Ctrl_V;
3918 VIsual_select = FALSE;
3919 VIsual_reselect = FALSE;
3920 }
3921
3922 // Only redo yank when 'y' flag is in 'cpoptions'.
3923 // Never redo "zf" (define fold).
3924 if ((redo_yank || oap->op_type != OP_YANK)
3925 && ((!VIsual_active || oap->motion_force)
3926 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003927 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003928 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003929 && cap->cmdchar != 'D'
3930#ifdef FEAT_FOLDING
3931 && oap->op_type != OP_FOLD
3932 && oap->op_type != OP_FOLDOPEN
3933 && oap->op_type != OP_FOLDOPENREC
3934 && oap->op_type != OP_FOLDCLOSE
3935 && oap->op_type != OP_FOLDCLOSEREC
3936 && oap->op_type != OP_FOLDDEL
3937 && oap->op_type != OP_FOLDDELREC
3938#endif
3939 )
3940 {
3941 prep_redo(oap->regname, cap->count0,
3942 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3943 oap->motion_force, cap->cmdchar, cap->nchar);
3944 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3945 {
3946 // If 'cpoptions' does not contain 'r', insert the search
3947 // pattern to really repeat the same command.
3948 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3949 AppendToRedobuffLit(cap->searchbuf, -1);
3950 AppendToRedobuff(NL_STR);
3951 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003952 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003953 {
3954 // do_cmdline() has stored the first typed line in
3955 // "repeat_cmdline". When several lines are typed repeating
3956 // won't be possible.
3957 if (repeat_cmdline == NULL)
3958 ResetRedobuff();
3959 else
3960 {
zeertzjq30b6d612023-05-07 17:39:23 +01003961 if (cap->cmdchar == ':')
3962 AppendToRedobuffLit(repeat_cmdline, -1);
3963 else
3964 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003965 AppendToRedobuff(NL_STR);
3966 VIM_CLEAR(repeat_cmdline);
3967 }
3968 }
3969 }
3970
3971 if (redo_VIsual_busy)
3972 {
3973 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003974 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003975 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003976 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003977 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3978 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003979 VIsual_mode = redo_VIsual.rv_mode;
3980 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003981 {
3982 if (VIsual_mode == 'v')
3983 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003984 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003985 {
3986 validate_virtcol();
3987 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003988 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003989 }
3990 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003991 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003992 }
3993 else
3994 {
3995 curwin->w_curswant = MAXCOL;
3996 }
3997 coladvance(curwin->w_curswant);
3998 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003999 cap->count0 = redo_VIsual.rv_count;
4000 if (redo_VIsual.rv_count != 0)
4001 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004002 else
4003 cap->count1 = 1;
4004 }
4005 else if (VIsual_active)
4006 {
4007 if (!gui_yank)
4008 {
4009 // Save the current VIsual area for '< and '> marks, and "gv"
4010 curbuf->b_visual.vi_start = VIsual;
4011 curbuf->b_visual.vi_end = curwin->w_cursor;
4012 curbuf->b_visual.vi_mode = VIsual_mode;
4013 restore_visual_mode();
4014 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00004015#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004016 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00004017#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004018 }
4019
4020 // In Select mode, a linewise selection is operated upon like a
4021 // characterwise selection.
4022 // Special case: gH<Del> deletes the last line.
4023 if (VIsual_select && VIsual_mode == 'V'
4024 && cap->oap->op_type != OP_DELETE)
4025 {
4026 if (LT_POS(VIsual, curwin->w_cursor))
4027 {
4028 VIsual.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01004029 curwin->w_cursor.col = ml_get_len(curwin->w_cursor.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004030 }
4031 else
4032 {
4033 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01004034 VIsual.col = ml_get_len(VIsual.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004035 }
4036 VIsual_mode = 'v';
4037 }
4038 // If 'selection' is "exclusive", backup one character for
4039 // charwise selections.
4040 else if (VIsual_mode == 'v')
4041 include_line_break = unadjust_for_sel();
4042
4043 oap->start = VIsual;
4044 if (VIsual_mode == 'V')
4045 {
4046 oap->start.col = 0;
4047 oap->start.coladd = 0;
4048 }
4049 }
4050
4051 // Set oap->start to the first position of the operated text, oap->end
4052 // to the end of the operated text. w_cursor is equal to oap->start.
4053 if (LT_POS(oap->start, curwin->w_cursor))
4054 {
4055#ifdef FEAT_FOLDING
4056 // Include folded lines completely.
4057 if (!VIsual_active)
4058 {
4059 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
4060 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01004061 if ((curwin->w_cursor.col > 0 || oap->inclusive
4062 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004063 && hasFolding(curwin->w_cursor.lnum, NULL,
4064 &curwin->w_cursor.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01004065 curwin->w_cursor.col = ml_get_curline_len();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004066 }
4067#endif
4068 oap->end = curwin->w_cursor;
4069 curwin->w_cursor = oap->start;
4070
4071 // w_virtcol may have been updated; if the cursor goes back to its
4072 // previous position w_virtcol becomes invalid and isn't updated
4073 // automatically.
4074 curwin->w_valid &= ~VALID_VIRTCOL;
4075 }
4076 else
4077 {
4078#ifdef FEAT_FOLDING
4079 // Include folded lines completely.
4080 if (!VIsual_active && oap->motion_type == MLINE)
4081 {
4082 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
4083 NULL))
4084 curwin->w_cursor.col = 0;
4085 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01004086 oap->start.col = ml_get_len(oap->start.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004087 }
4088#endif
4089 oap->end = oap->start;
4090 oap->start = curwin->w_cursor;
4091 }
4092
4093 // Just in case lines were deleted that make the position invalid.
4094 check_pos(curwin->w_buffer, &oap->end);
4095 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
4096
4097 // Set "virtual_op" before resetting VIsual_active.
4098 virtual_op = virtual_active();
4099
4100 if (VIsual_active || redo_VIsual_busy)
4101 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004102 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004103
4104 if (!redo_VIsual_busy && !gui_yank)
4105 {
4106 // Prepare to reselect and redo Visual: this is based on the
4107 // size of the Visual text
4108 resel_VIsual_mode = VIsual_mode;
4109 if (curwin->w_curswant == MAXCOL)
4110 resel_VIsual_vcol = MAXCOL;
4111 else
4112 {
4113 if (VIsual_mode != Ctrl_V)
4114 getvvcol(curwin, &(oap->end),
4115 NULL, NULL, &oap->end_vcol);
4116 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
4117 {
4118 if (VIsual_mode != Ctrl_V)
4119 getvvcol(curwin, &(oap->start),
4120 &oap->start_vcol, NULL, NULL);
4121 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
4122 }
4123 else
4124 resel_VIsual_vcol = oap->end_vcol;
4125 }
4126 resel_VIsual_line_count = oap->line_count;
4127 }
4128
4129 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
4130 if ((redo_yank || oap->op_type != OP_YANK)
4131 && oap->op_type != OP_COLON
4132#ifdef FEAT_FOLDING
4133 && oap->op_type != OP_FOLD
4134 && oap->op_type != OP_FOLDOPEN
4135 && oap->op_type != OP_FOLDOPENREC
4136 && oap->op_type != OP_FOLDCLOSE
4137 && oap->op_type != OP_FOLDCLOSEREC
4138 && oap->op_type != OP_FOLDDEL
4139 && oap->op_type != OP_FOLDDELREC
4140#endif
4141 && oap->motion_force == NUL
4142 )
4143 {
4144 // Prepare for redoing. Only use the nchar field for "r",
4145 // otherwise it might be the second char of the operator.
4146 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4147 || cap->nchar == 'N'))
4148 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004149 get_op_char(oap->op_type),
4150 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004151 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004152 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004153 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004154 int opchar = get_op_char(oap->op_type);
4155 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004156 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4157
4158 // reverse what nv_replace() did
4159 if (nchar == REPLACE_CR_NCHAR)
4160 nchar = CAR;
4161 else if (nchar == REPLACE_NL_NCHAR)
4162 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004163
4164 if (opchar == 'g' && extra_opchar == '@')
4165 // also repeat the count for 'operatorfunc'
4166 prep_redo_num2(oap->regname, 0L, NUL, 'v',
4167 cap->count0, opchar, extra_opchar, nchar);
4168 else
4169 prep_redo(oap->regname, 0L, NUL, 'v',
4170 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004171 }
4172 if (!redo_VIsual_busy)
4173 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004174 redo_VIsual.rv_mode = resel_VIsual_mode;
4175 redo_VIsual.rv_vcol = resel_VIsual_vcol;
4176 redo_VIsual.rv_line_count = resel_VIsual_line_count;
4177 redo_VIsual.rv_count = cap->count0;
4178 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004179 }
4180 }
4181
4182 // oap->inclusive defaults to TRUE.
4183 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4184 // FALSE. This makes "d}P" and "v}dP" work the same.
4185 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4186 oap->inclusive = TRUE;
4187 if (VIsual_mode == 'V')
4188 oap->motion_type = MLINE;
4189 else
4190 {
4191 oap->motion_type = MCHAR;
4192 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4193 && (include_line_break || !virtual_op))
4194 {
4195 oap->inclusive = FALSE;
4196 // Try to include the newline, unless it's an operator
4197 // that works on lines only.
4198 if (*p_sel != 'o'
4199 && !op_on_lines(oap->op_type)
4200 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4201 {
4202 ++oap->end.lnum;
4203 oap->end.col = 0;
4204 oap->end.coladd = 0;
4205 ++oap->line_count;
4206 }
4207 }
4208 }
4209
4210 redo_VIsual_busy = FALSE;
4211
4212 // Switch Visual off now, so screen updating does
4213 // not show inverted text when the screen is redrawn.
4214 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4215 // no screen redraw, so it is done here to remove the inverted
4216 // part.
4217 if (!gui_yank)
4218 {
4219 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004220 setmouse();
4221 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004222 may_clear_cmdline();
4223 if ((oap->op_type == OP_YANK
4224 || oap->op_type == OP_COLON
4225 || oap->op_type == OP_FUNCTION
4226 || oap->op_type == OP_FILTER)
4227 && oap->motion_force == NUL)
4228 {
4229#ifdef FEAT_LINEBREAK
4230 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01004231 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004232#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004233 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004234 }
4235 }
4236 }
4237
4238 // Include the trailing byte of a multi-byte char.
4239 if (has_mbyte && oap->inclusive)
4240 {
4241 int l;
4242
4243 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4244 if (l > 1)
4245 oap->end.col += l - 1;
4246 }
4247 curwin->w_set_curswant = TRUE;
4248
4249 // oap->empty is set when start and end are the same. The inclusive
4250 // flag affects this too, unless yanking and the end is on a NUL.
4251 oap->empty = (oap->motion_type == MCHAR
4252 && (!oap->inclusive
4253 || (oap->op_type == OP_YANK
4254 && gchar_pos(&oap->end) == NUL))
4255 && EQUAL_POS(oap->start, oap->end)
4256 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4257 // For delete, change and yank, it's an error to operate on an
4258 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4259 empty_region_error = (oap->empty
4260 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4261
4262 // Force a redraw when operating on an empty Visual region, when
4263 // 'modifiable is off or creating a fold.
4264 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4265#ifdef FEAT_FOLDING
4266 || oap->op_type == OP_FOLD
4267#endif
4268 ))
4269 {
4270#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004271 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004272#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004273 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004274 }
4275
4276 // If the end of an operator is in column one while oap->motion_type
4277 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4278 // character in the previous line. If op_start is on or before the
4279 // first non-blank in the line, the operator becomes linewise
4280 // (strange, but that's the way vi does it).
4281 if ( oap->motion_type == MCHAR
4282 && oap->inclusive == FALSE
4283 && !(cap->retval & CA_NO_ADJ_OP_END)
4284 && oap->end.col == 0
4285 && (!oap->is_VIsual || *p_sel == 'o')
4286 && !oap->block_mode
4287 && oap->line_count > 1)
4288 {
4289 oap->end_adjusted = TRUE; // remember that we did this
4290 --oap->line_count;
4291 --oap->end.lnum;
4292 if (inindent(0))
4293 oap->motion_type = MLINE;
4294 else
4295 {
zeertzjq94b7c322024-03-12 21:50:32 +01004296 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004297 if (oap->end.col)
4298 {
4299 --oap->end.col;
4300 oap->inclusive = TRUE;
4301 }
4302 }
4303 }
4304 else
4305 oap->end_adjusted = FALSE;
4306
4307 switch (oap->op_type)
4308 {
4309 case OP_LSHIFT:
4310 case OP_RSHIFT:
4311 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4312 auto_format(FALSE, TRUE);
4313 break;
4314
4315 case OP_JOIN_NS:
4316 case OP_JOIN:
4317 if (oap->line_count < 2)
4318 oap->line_count = 2;
4319 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4320 curbuf->b_ml.ml_line_count)
4321 beep_flush();
4322 else
4323 {
4324 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4325 TRUE, TRUE, TRUE);
4326 auto_format(FALSE, TRUE);
4327 }
4328 break;
4329
4330 case OP_DELETE:
4331 VIsual_reselect = FALSE; // don't reselect now
4332 if (empty_region_error)
4333 {
4334 vim_beep(BO_OPER);
4335 CancelRedo();
4336 }
4337 else
4338 {
4339 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004340 // save cursor line for undo if it wasn't saved yet
4341 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4342 && u_save_cursor() == OK)
4343 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004344 }
4345 break;
4346
4347 case OP_YANK:
4348 if (empty_region_error)
4349 {
4350 if (!gui_yank)
4351 {
4352 vim_beep(BO_OPER);
4353 CancelRedo();
4354 }
4355 }
4356 else
4357 {
4358#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004359 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004360#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004361 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004362 (void)op_yank(oap, FALSE, !gui_yank);
4363 }
4364 check_cursor_col();
4365 break;
4366
4367 case OP_CHANGE:
4368 VIsual_reselect = FALSE; // don't reselect now
4369 if (empty_region_error)
4370 {
4371 vim_beep(BO_OPER);
4372 CancelRedo();
4373 }
4374 else
4375 {
4376 // This is a new edit command, not a restart. Need to
4377 // remember it to make 'insertmode' work with mappings for
4378 // Visual mode. But do this only once and not when typed and
4379 // 'insertmode' isn't set.
4380 if (p_im || !KeyTyped)
4381 restart_edit_save = restart_edit;
4382 else
4383 restart_edit_save = 0;
4384 restart_edit = 0;
4385#ifdef FEAT_LINEBREAK
4386 // Restore linebreak, so that when the user edits it looks as
4387 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004388 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004389#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004390 // trigger TextChangedI
4391 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4392
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004393 if (op_change(oap)) // will call edit()
4394 cap->retval |= CA_COMMAND_BUSY;
4395 if (restart_edit == 0)
4396 restart_edit = restart_edit_save;
4397 }
4398 break;
4399
4400 case OP_FILTER:
4401 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4402 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4403 else
4404 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4405 // FALLTHROUGH
4406
4407 case OP_INDENT:
4408 case OP_COLON:
4409
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004410 // If 'equalprg' is empty, do the indenting internally.
4411 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4412 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004413 if (curbuf->b_p_lisp)
4414 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004415#ifdef FEAT_EVAL
4416 if (use_indentexpr_for_lisp())
4417 op_reindent(oap, get_expr_indent);
4418 else
4419#endif
4420 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004421 break;
4422 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004423 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004424#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004425 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004426#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004427 get_c_indent);
4428 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004429 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004430
4431 op_colon(oap);
4432 break;
4433
4434 case OP_TILDE:
4435 case OP_UPPER:
4436 case OP_LOWER:
4437 case OP_ROT13:
4438 if (empty_region_error)
4439 {
4440 vim_beep(BO_OPER);
4441 CancelRedo();
4442 }
4443 else
4444 op_tilde(oap);
4445 check_cursor_col();
4446 break;
4447
4448 case OP_FORMAT:
4449#if defined(FEAT_EVAL)
4450 if (*curbuf->b_p_fex != NUL)
4451 op_formatexpr(oap); // use expression
4452 else
4453#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004454 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004455 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004456 op_colon(oap); // use external command
4457 else
4458 op_format(oap, FALSE); // use internal function
4459 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004460 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004461 case OP_FORMAT2:
4462 op_format(oap, TRUE); // use internal function
4463 break;
4464
4465 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004466 {
4467 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4468
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004469#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004470 // Restore linebreak, so that when the user edits it looks as
4471 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004472 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004473#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004474 // call 'operatorfunc'
4475 op_function(oap);
4476
4477 // Restore the info for redoing Visual mode, the function may
4478 // invoke another operator and unintentionally change it.
4479 redo_VIsual = save_redo_VIsual;
4480 break;
4481 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004482
4483 case OP_INSERT:
4484 case OP_APPEND:
4485 VIsual_reselect = FALSE; // don't reselect now
4486 if (empty_region_error)
4487 {
4488 vim_beep(BO_OPER);
4489 CancelRedo();
4490 }
4491 else
4492 {
4493 // This is a new edit command, not a restart. Need to
4494 // remember it to make 'insertmode' work with mappings for
4495 // Visual mode. But do this only once.
4496 restart_edit_save = restart_edit;
4497 restart_edit = 0;
4498#ifdef FEAT_LINEBREAK
4499 // Restore linebreak, so that when the user edits it looks as
4500 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004501 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004502#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004503 // trigger TextChangedI
4504 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4505
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004506 op_insert(oap, cap->count1);
4507#ifdef FEAT_LINEBREAK
4508 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004509 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004510#endif
4511
4512 // TODO: when inserting in several lines, should format all
4513 // the lines.
4514 auto_format(FALSE, TRUE);
4515
4516 if (restart_edit == 0)
4517 restart_edit = restart_edit_save;
4518 else
4519 cap->retval |= CA_COMMAND_BUSY;
4520 }
4521 break;
4522
4523 case OP_REPLACE:
4524 VIsual_reselect = FALSE; // don't reselect now
4525 if (empty_region_error)
4526 {
4527 vim_beep(BO_OPER);
4528 CancelRedo();
4529 }
4530 else
4531 {
4532#ifdef FEAT_LINEBREAK
4533 // Restore linebreak, so that when the user edits it looks as
4534 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004535 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004536#endif
4537 op_replace(oap, cap->nchar);
4538 }
4539 break;
4540
4541#ifdef FEAT_FOLDING
4542 case OP_FOLD:
4543 VIsual_reselect = FALSE; // don't reselect now
4544 foldCreate(oap->start.lnum, oap->end.lnum);
4545 break;
4546
4547 case OP_FOLDOPEN:
4548 case OP_FOLDOPENREC:
4549 case OP_FOLDCLOSE:
4550 case OP_FOLDCLOSEREC:
4551 VIsual_reselect = FALSE; // don't reselect now
4552 opFoldRange(oap->start.lnum, oap->end.lnum,
4553 oap->op_type == OP_FOLDOPEN
4554 || oap->op_type == OP_FOLDOPENREC,
4555 oap->op_type == OP_FOLDOPENREC
4556 || oap->op_type == OP_FOLDCLOSEREC,
4557 oap->is_VIsual);
4558 break;
4559
4560 case OP_FOLDDEL:
4561 case OP_FOLDDELREC:
4562 VIsual_reselect = FALSE; // don't reselect now
4563 deleteFold(oap->start.lnum, oap->end.lnum,
4564 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4565 break;
4566#endif
4567 case OP_NR_ADD:
4568 case OP_NR_SUB:
4569 if (empty_region_error)
4570 {
4571 vim_beep(BO_OPER);
4572 CancelRedo();
4573 }
4574 else
4575 {
4576 VIsual_active = TRUE;
4577#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004578 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004579#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004580 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004581 VIsual_active = FALSE;
4582 }
4583 check_cursor_col();
4584 break;
4585 default:
4586 clearopbeep(oap);
4587 }
4588 virtual_op = MAYBE;
4589 if (!gui_yank)
4590 {
4591 // if 'sol' not set, go back to old column for some commands
4592 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4593 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4594 || oap->op_type == OP_DELETE))
4595 {
4596#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004597 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004598#endif
4599 coladvance(curwin->w_curswant = old_col);
4600 }
4601 }
4602 else
4603 {
4604 curwin->w_cursor = old_cursor;
4605 }
4606 oap->block_mode = FALSE;
4607 clearop(oap);
4608 motion_force = NUL;
4609 }
4610#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004611 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004612#endif
4613}
Christian Brabandtffb13672023-09-15 20:22:02 +02004614
4615// put byte 'c' at position 'lp', but
4616// verify, that the position to place
4617// is actually safe
4618 static void
4619pbyte(pos_T lp, int c)
4620{
4621 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4622 int len = curbuf->b_ml.ml_line_len;
4623
4624 // safety check
4625 if (lp.col >= len)
4626 lp.col = (len > 1 ? len - 2 : 0);
4627 *(p + lp.col) = c;
4628}