blob: 9efef383d1883a5a4b9661c6e27971178e894891 [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
251 // Perform the summation for indeces within the actual array.
252 for (i = 1; i <= index && i <= vts_array[0]; i++)
253 sum += vts_array[i];
254
255 // Add topstops whose indeces exceed the actual array.
256 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;
2799 char_u *buf1;
2800 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2802 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002803 uvarnumber_T n;
2804 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 char_u *ptr;
zeertzjq8c55d602024-03-13 20:42:26 +01002806 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002809 int do_hex;
2810 int do_oct;
2811 int do_bin;
2812 int do_alpha;
2813 int do_unsigned;
distobs25ac6d62024-07-06 17:50:09 +02002814 int do_blank;
2815 int blank_unsigned = FALSE; // blank: treat as unsigned?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002818 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002819 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002820 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002821 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002822 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002823 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002824 pos_T startpos;
2825 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002826 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002828 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2829 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2830 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2831 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2832 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
distobs25ac6d62024-07-06 17:50:09 +02002833 do_blank = (vim_strchr(curbuf->b_p_nf, 'k') != NULL); // "blanK"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002835 if (virtual_active())
2836 {
2837 save_coladd = pos->coladd;
2838 pos->coladd = 0;
2839 }
2840
Bram Moolenaard79e5502016-01-10 22:13:02 +01002841 curwin->w_cursor = *pos;
2842 ptr = ml_get(pos->lnum);
zeertzjq8c55d602024-03-13 20:42:26 +01002843 linelen = ml_get_len(pos->lnum);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002844 col = pos->col;
2845
zeertzjq8c55d602024-03-13 20:42:26 +01002846 if (col + !!save_coladd >= linelen)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002847 goto theend;
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 /*
2850 * First check if we are on a hexadecimal number, after the "0x".
2851 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002852 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002854 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002855 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002856 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002857 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002858 if (has_mbyte)
2859 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002860 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002861
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002862 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002863 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002864 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002865 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002866 if (has_mbyte)
2867 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002868 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002869
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002870 if ( do_bin
2871 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002872 && ! ((col > 0
2873 && (ptr[col] == 'X'
2874 || ptr[col] == 'x')
2875 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002876 && (!has_mbyte ||
2877 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002878 && vim_isxdigit(ptr[col + 1]))))
2879 {
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002882
Bram Moolenaard79e5502016-01-10 22:13:02 +01002883 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002884
2885 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002886 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002887 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002888 if (has_mbyte)
2889 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002890 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002891 }
2892
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002893 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002894 && col > 0
2895 && (ptr[col] == 'X'
2896 || ptr[col] == 'x')
2897 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002898 && (!has_mbyte ||
2899 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002900 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002901 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002902 && col > 0
2903 && (ptr[col] == 'B'
2904 || ptr[col] == 'b')
2905 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002906 && (!has_mbyte ||
2907 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002908 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002911 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002912 if (has_mbyte)
2913 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002914 }
2915 else
2916 {
2917 /*
2918 * Search forward and then backward to find the start of number.
2919 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002920 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002921
2922 while (ptr[col] != NUL
2923 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002924 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002925 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002926
2927 while (col > 0
2928 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002929 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002930 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002931 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002932 if (has_mbyte)
2933 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002934 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002935 }
2936 }
2937
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002938 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002939 {
2940 while (ptr[col] != NUL && length > 0
2941 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002942 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002943 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002944 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002945
2946 col += mb_len;
2947 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002948 }
2949
2950 if (length == 0)
2951 goto theend;
2952
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002953 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002954 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2955 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002956 {
distobs25ac6d62024-07-06 17:50:09 +02002957 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
2958 blank_unsigned = TRUE;
2959 else
2960 {
2961 negative = TRUE;
2962 was_positive = FALSE;
2963 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002964 }
2965 }
2966
2967 /*
2968 * If a number was found, and saving for undo works, replace the number.
2969 */
2970 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002971 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002972 {
2973 beep_flush();
2974 goto theend;
2975 }
2976
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002977 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002978 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002980 if (op_type == OP_NR_SUB)
2981 {
2982 if (CharOrd(firstdigit) < Prenum1)
2983 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002984 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002985 firstdigit = 'A';
2986 else
2987 firstdigit = 'a';
2988 }
2989 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002990 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002991 }
2992 else
2993 {
2994 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2995 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002996 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002997 firstdigit = 'Z';
2998 else
2999 firstdigit = 'z';
3000 }
3001 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01003002 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003003 }
3004 curwin->w_cursor.col = col;
3005 if (!did_change)
3006 startpos = curwin->w_cursor;
3007 did_change = TRUE;
3008 (void)del_char(FALSE);
3009 ins_char(firstdigit);
3010 endpos = curwin->w_cursor;
3011 curwin->w_cursor.col = col;
3012 }
3013 else
3014 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003015 pos_T save_pos;
3016 int i;
3017
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003018 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003019 && (!has_mbyte ||
3020 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003021 && !visual
3022 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003023 {
distobs25ac6d62024-07-06 17:50:09 +02003024 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
3025 blank_unsigned = TRUE;
3026 else
3027 {
3028 // negative number
3029 --col;
3030 negative = TRUE;
3031 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003032 }
distobs25ac6d62024-07-06 17:50:09 +02003033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003035 if (visual && VIsual_mode != 'V')
3036 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
zeertzjq8c55d602024-03-13 20:42:26 +01003037 ? linelen - col : length);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003038
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00003039 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003040 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003041 0 + (do_bin ? STR2NR_BIN : 0)
3042 + (do_oct ? STR2NR_OCT : 0)
3043 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00003044 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003046 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01003047 if (pre && negative)
3048 {
3049 ++col;
3050 --length;
3051 negative = FALSE;
3052 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003053 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01003054 subtract = FALSE;
3055 if (op_type == OP_NR_SUB)
3056 subtract ^= TRUE;
3057 if (negative)
3058 subtract ^= TRUE;
3059
3060 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00003061 if (!overflow) // if number is too big don't add/subtract
3062 {
3063 if (subtract)
3064 n -= (uvarnumber_T)Prenum1;
3065 else
3066 n += (uvarnumber_T)Prenum1;
3067 }
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01003070 if (!pre)
3071 {
3072 if (subtract)
3073 {
3074 if (n > oldn)
3075 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003076 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003077 negative ^= TRUE;
3078 }
3079 }
3080 else
3081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01003083 if (n < oldn)
3084 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003085 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003086 negative ^= TRUE;
3087 }
3088 }
3089 if (n == 0)
3090 negative = FALSE;
3091 }
3092
distobs25ac6d62024-07-06 17:50:09 +02003093 if ((do_unsigned || blank_unsigned) && negative)
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003094 {
3095 if (subtract)
3096 // sticking at zero.
3097 n = (uvarnumber_T)0;
3098 else
3099 // sticking at 2^64 - 1.
3100 n = (uvarnumber_T)(-1);
3101 negative = FALSE;
3102 }
3103
Bram Moolenaard79e5502016-01-10 22:13:02 +01003104 if (visual && !was_positive && !negative && col > 0)
3105 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003106 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01003107 col--;
3108 length++;
3109 }
3110
3111 /*
3112 * Delete the old number.
3113 */
3114 curwin->w_cursor.col = col;
3115 if (!did_change)
3116 startpos = curwin->w_cursor;
3117 did_change = TRUE;
3118 todel = length;
3119 c = gchar_cursor();
3120 /*
3121 * Don't include the '-' in the length, only the length of the
3122 * part after it is kept the same.
3123 */
3124 if (c == '-')
3125 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003126
3127 save_pos = curwin->w_cursor;
3128 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003129 {
Keith Thompson184f71c2024-01-04 21:19:04 +01003130 if (c < 0x100 && SAFE_isalpha(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003131 {
Keith Thompson184f71c2024-01-04 21:19:04 +01003132 if (SAFE_isupper(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003133 hexupper = TRUE;
3134 else
3135 hexupper = FALSE;
3136 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003137 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01003138 c = gchar_cursor();
3139 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003140 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003141
3142 /*
3143 * Prepare the leading characters in buf1[].
3144 * When there are many leading zeros it could be very long.
3145 * Allocate a bit too much.
3146 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003147 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003148 if (buf1 == NULL)
3149 goto theend;
3150 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003151 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003152 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003153 if (pre)
3154 {
3155 *ptr++ = '0';
3156 --length;
3157 }
3158 if (pre == 'b' || pre == 'B' ||
3159 pre == 'x' || pre == 'X')
3160 {
3161 *ptr++ = pre;
3162 --length;
3163 }
3164
3165 /*
3166 * Put the number characters in buf2[].
3167 */
3168 if (pre == 'b' || pre == 'B')
3169 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01003170 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003171 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003174 for (bit = bits; bit > 0; bit--)
3175 if ((n >> (bit - 1)) & 0x1) break;
3176
Christian Brabandt889f6af2023-09-02 19:43:33 +02003177 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003178 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3179
3180 buf2[i] = '\0';
3181 }
3182 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003183 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003184 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003185 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003186 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003187 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003188 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003189 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003190 length -= (int)STRLEN(buf2);
3191
3192 /*
3193 * Adjust number of zeros to the new number of digits, so the
3194 * total length of the number remains the same.
3195 * Don't do this when
3196 * the result may look like an octal number.
3197 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003198 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003199 while (length-- > 0)
3200 *ptr++ = '0';
3201 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003202
Bram Moolenaard79e5502016-01-10 22:13:02 +01003203 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003204
3205 // Insert just after the first character to be removed, so that any
3206 // text properties will be adjusted. Then delete the old number
3207 // afterwards.
3208 save_pos = curwin->w_cursor;
3209 if (todel > 0)
3210 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003212 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003213
3214 // del_char() will also mark line needing displaying
3215 if (todel > 0)
3216 {
zeertzjq94b7c322024-03-12 21:50:32 +01003217 int bytes_after = ml_get_curline_len() - curwin->w_cursor.col;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003218
3219 // Delete the one character before the insert.
3220 curwin->w_cursor = save_pos;
3221 (void)del_char(FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01003222 curwin->w_cursor.col = ml_get_curline_len() - bytes_after;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003223 --todel;
3224 }
3225 while (todel-- > 0)
3226 (void)del_char(FALSE);
3227
Bram Moolenaard79e5502016-01-10 22:13:02 +01003228 endpos = curwin->w_cursor;
3229 if (did_change && curwin->w_cursor.col)
3230 --curwin->w_cursor.col;
3231 }
3232
Bram Moolenaare1004402020-10-24 20:49:43 +02003233 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003234 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003235 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003236 curbuf->b_op_start = startpos;
3237 curbuf->b_op_end = endpos;
3238 if (curbuf->b_op_end.col > 0)
3239 --curbuf->b_op_end.col;
3240 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003241
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003242theend:
3243 if (visual)
3244 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003245 else if (did_change)
3246 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003247 else if (virtual_active())
3248 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003249
Bram Moolenaard79e5502016-01-10 22:13:02 +01003250 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251}
3252
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003256 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003260 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 *
3262 * "Words" are counted by looking for boundaries between non-space and
3263 * space characters. (it seems to produce results that match 'wc'.)
3264 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003265 * Return value is byte count; word count for the line is added to "*wc".
3266 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 *
3268 * The function will only examine the first "limit" characters in the
3269 * line, stopping if it encounters an end-of-line (NUL byte). In that
3270 * case, eol_size will be added to the character count to account for
3271 * the size of the EOL character.
3272 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003273 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003274line_count_info(
3275 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003276 varnumber_T *wc,
3277 varnumber_T *cc,
3278 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003279 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003281 varnumber_T i;
3282 varnumber_T words = 0;
3283 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 int is_word = 0;
3285
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003286 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 if (is_word)
3289 {
3290 if (vim_isspace(line[i]))
3291 {
3292 words++;
3293 is_word = 0;
3294 }
3295 }
3296 else if (!vim_isspace(line[i]))
3297 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003298 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003299 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301
3302 if (is_word)
3303 words++;
3304 *wc += words;
3305
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003306 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003307 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003310 chars += eol_size;
3311 }
3312 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 return i;
3314}
3315
3316/*
3317 * Give some info about the position of the cursor (for "g CTRL-G").
3318 * In Visual mode, give some info about the selected region. (In this case,
3319 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003320 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
3322 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003323cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003326 char_u buf1[50];
3327 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003329 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003330 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003331 varnumber_T byte_count_cursor = 0;
3332 varnumber_T char_count = 0;
3333 varnumber_T char_count_cursor = 0;
3334 varnumber_T word_count = 0;
3335 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003336 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003337 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 long line_count_selected = 0;
3339 pos_T min_pos, max_pos;
3340 oparg_T oparg;
3341 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343 /*
3344 * Compute the length of the file in characters.
3345 */
3346 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3347 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003348 if (dict == NULL)
3349 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003350 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003351 return;
3352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354 else
3355 {
3356 if (get_fileformat(curbuf) == EOL_DOS)
3357 eol_size = 2;
3358 else
3359 eol_size = 1;
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 if (VIsual_active)
3362 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003363 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 min_pos = VIsual;
3366 max_pos = curwin->w_cursor;
3367 }
3368 else
3369 {
3370 min_pos = curwin->w_cursor;
3371 max_pos = VIsual;
3372 }
3373 if (*p_sel == 'e' && max_pos.col > 0)
3374 --max_pos.col;
3375
3376 if (VIsual_mode == Ctrl_V)
3377 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003378#ifdef FEAT_LINEBREAK
3379 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003380 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003381
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003382 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003383 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003384 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 oparg.is_VIsual = 1;
3387 oparg.block_mode = TRUE;
3388 oparg.op_type = OP_NOP;
3389 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003390 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003391#ifdef FEAT_LINEBREAK
3392 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003393 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003394#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003395 if (curwin->w_curswant == MAXCOL)
3396 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003397 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (oparg.end_vcol < oparg.start_vcol)
3399 {
3400 oparg.end_vcol += oparg.start_vcol;
3401 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3402 oparg.end_vcol -= oparg.start_vcol;
3403 }
3404 }
3405 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3409 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003410 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003411 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
3413 ui_breakcheck();
3414 if (got_int)
3415 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003416 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (VIsual_active
3421 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3422 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003423 char_u *s = NULL;
3424 long len = 0L;
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 switch (VIsual_mode)
3427 {
3428 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003432 s = bd.textstart;
3433 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 break;
3435 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003436 s = ml_get(lnum);
3437 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 break;
3439 case 'v':
3440 {
3441 colnr_T start_col = (lnum == min_pos.lnum)
3442 ? min_pos.col : 0;
3443 colnr_T end_col = (lnum == max_pos.lnum)
3444 ? max_pos.col - start_col + 1 : MAXCOL;
3445
Bram Moolenaardef9e822004-12-31 20:58:58 +00003446 s = ml_get(lnum) + start_col;
3447 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449 break;
3450 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003451 if (s != NULL)
3452 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003453 byte_count_cursor += line_count_info(s, &word_count_cursor,
3454 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003455 if (lnum == curbuf->b_ml.ml_line_count
3456 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003457 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003458 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003459 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (lnum == curwin->w_cursor.lnum)
3466 {
3467 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003468 char_count_cursor += char_count;
3469 byte_count_cursor = byte_count +
3470 line_count_info(ml_get(lnum),
3471 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003472 (varnumber_T)(curwin->w_cursor.col + 1),
3473 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003476 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003477 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003478 &char_count, (varnumber_T)MAXCOL,
3479 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003483 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003484 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
Bram Moolenaared767a22016-01-03 22:49:16 +01003486 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003488 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003490 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3491 {
3492 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3493 &max_pos.col);
3494 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3495 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3496 }
3497 else
3498 buf1[0] = NUL;
3499
3500 if (char_count_cursor == byte_count_cursor
3501 && char_count == byte_count)
3502 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003503 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003504 buf1, line_count_selected,
3505 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003506 word_count_cursor,
3507 word_count,
3508 byte_count_cursor,
3509 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003510 else
3511 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003512 _("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 +01003513 buf1, line_count_selected,
3514 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003515 word_count_cursor,
3516 word_count,
3517 char_count_cursor,
3518 char_count,
3519 byte_count_cursor,
3520 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003523 {
3524 p = ml_get_curline();
3525 validate_virtcol();
3526 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3527 (int)curwin->w_virtcol + 1);
zeertzjq94b7c322024-03-12 21:50:32 +01003528 col_print(buf2, sizeof(buf2), ml_get_curline_len(),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003529 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaared767a22016-01-03 22:49:16 +01003531 if (char_count_cursor == byte_count_cursor
3532 && char_count == byte_count)
3533 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003534 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003535 (char *)buf1, (char *)buf2,
3536 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003538 word_count_cursor, word_count,
3539 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003540 else
3541 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003542 _("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 +01003543 (char *)buf1, (char *)buf2,
3544 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003545 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003546 word_count_cursor, word_count,
3547 char_count_cursor, char_count,
3548 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003549 }
3550 }
3551
Bram Moolenaared767a22016-01-03 22:49:16 +01003552 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003553 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003554 {
3555 size_t len = STRLEN(IObuff);
3556
3557 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003558 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003559 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003560 if (dict == NULL)
3561 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003562 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003563 p = p_shm;
3564 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003565 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003566 p_shm = p;
3567 }
3568 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003569#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003570 if (dict != NULL)
3571 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003572 dict_add_number(dict, "words", word_count);
3573 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003574 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003575 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3576 byte_count_cursor);
3577 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3578 char_count_cursor);
3579 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3580 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003584
3585/*
3586 * Handle indent and format operators and visual mode ":".
3587 */
3588 static void
3589op_colon(oparg_T *oap)
3590{
3591 stuffcharReadbuff(':');
3592 if (oap->is_VIsual)
3593 stuffReadbuff((char_u *)"'<,'>");
3594 else
3595 {
3596 // Make the range look nice, so it can be repeated.
3597 if (oap->start.lnum == curwin->w_cursor.lnum)
3598 stuffcharReadbuff('.');
3599 else
3600 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003601
3602#ifdef FEAT_FOLDING
3603 // When using !! on a closed fold the range ".!" works best to operate
3604 // on, it will be made the whole closed fold later.
3605 linenr_T endOfStartFold = oap->start.lnum;
3606 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3607#endif
3608 if (oap->end.lnum != oap->start.lnum
3609#ifdef FEAT_FOLDING
3610 && oap->end.lnum != endOfStartFold
3611#endif
3612 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003613 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003614 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003615 stuffcharReadbuff(',');
3616 if (oap->end.lnum == curwin->w_cursor.lnum)
3617 stuffcharReadbuff('.');
3618 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3619 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003620 else if (oap->start.lnum == curwin->w_cursor.lnum
3621#ifdef FEAT_FOLDING
3622 // do not use ".+number" for a closed fold, it would count
3623 // folded lines twice
3624 && !hasFolding(oap->end.lnum, NULL, NULL)
3625#endif
3626 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003627 {
3628 stuffReadbuff((char_u *)".+");
3629 stuffnumReadbuff((long)oap->line_count - 1);
3630 }
3631 else
3632 stuffnumReadbuff((long)oap->end.lnum);
3633 }
3634 }
3635 if (oap->op_type != OP_COLON)
3636 stuffReadbuff((char_u *)"!");
3637 if (oap->op_type == OP_INDENT)
3638 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003639 if (*get_equalprg() == NUL)
3640 stuffReadbuff((char_u *)"indent");
3641 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003642 stuffReadbuff(get_equalprg());
3643 stuffReadbuff((char_u *)"\n");
3644 }
3645 else if (oap->op_type == OP_FORMAT)
3646 {
3647 if (*curbuf->b_p_fp != NUL)
3648 stuffReadbuff(curbuf->b_p_fp);
3649 else if (*p_fp != NUL)
3650 stuffReadbuff(p_fp);
3651 else
3652 stuffReadbuff((char_u *)"fmt");
3653 stuffReadbuff((char_u *)"\n']");
3654 }
3655
3656 // do_cmdline() does the rest
3657}
3658
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003659// callback function for 'operatorfunc'
3660static callback_T opfunc_cb;
3661
3662/*
3663 * Process the 'operatorfunc' option value.
3664 * Returns OK or FAIL.
3665 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003666 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003667did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003668{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003669 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3670 return e_invalid_argument;
3671
3672 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003673}
3674
3675#if defined(EXITFREE) || defined(PROTO)
3676 void
3677free_operatorfunc_option(void)
3678{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003679# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003680 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003681# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003682}
3683#endif
3684
Dominique Pelle748b3082022-01-08 12:41:16 +00003685#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003686/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003687 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003688 * garbage collected.
3689 */
3690 int
3691set_ref_in_opfunc(int copyID UNUSED)
3692{
3693 int abort = FALSE;
3694
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003695 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003696
3697 return abort;
3698}
Dominique Pelle748b3082022-01-08 12:41:16 +00003699#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003700
3701/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003702 * Handle the "g@" operator: call 'operatorfunc'.
3703 */
3704 static void
3705op_function(oparg_T *oap UNUSED)
3706{
3707#ifdef FEAT_EVAL
3708 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003709 pos_T orig_start = curbuf->b_op_start;
3710 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003711 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003712
3713 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003714 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003715 else
3716 {
3717 // Set '[ and '] marks to text to be operated on.
3718 curbuf->b_op_start = oap->start;
3719 curbuf->b_op_end = oap->end;
3720 if (oap->motion_type != MLINE && !oap->inclusive)
3721 // Exclude the end position.
3722 decl(&curbuf->b_op_end);
3723
3724 argv[0].v_type = VAR_STRING;
3725 if (oap->block_mode)
3726 argv[0].vval.v_string = (char_u *)"block";
3727 else if (oap->motion_type == MLINE)
3728 argv[0].vval.v_string = (char_u *)"line";
3729 else
3730 argv[0].vval.v_string = (char_u *)"char";
3731 argv[1].v_type = VAR_UNKNOWN;
3732
3733 // Reset virtual_op so that 'virtualedit' can be changed in the
3734 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003735 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003736 virtual_op = MAYBE;
3737
naohiro ono75c30e92021-10-19 11:15:41 +01003738 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003739 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003740 finish_op = FALSE;
3741
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003742 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3743 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003744
3745 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003746 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003747 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003748 {
3749 curbuf->b_op_start = orig_start;
3750 curbuf->b_op_end = orig_end;
3751 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003752 }
3753#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003754 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003755#endif
3756}
3757
3758/*
3759 * Calculate start/end virtual columns for operating in block mode.
3760 */
3761 static void
3762get_op_vcol(
3763 oparg_T *oap,
3764 colnr_T redo_VIsual_vcol,
3765 int initial) // when TRUE adjust position for 'selectmode'
3766{
3767 colnr_T start, end;
3768
3769 if (VIsual_mode != Ctrl_V
3770 || (!initial && oap->end.col < curwin->w_width))
3771 return;
3772
3773 oap->block_mode = TRUE;
3774
3775 // prevent from moving onto a trail byte
3776 if (has_mbyte)
3777 mb_adjustpos(curwin->w_buffer, &oap->end);
3778
3779 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3780
3781 if (!redo_VIsual_busy)
3782 {
3783 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3784
3785 if (start < oap->start_vcol)
3786 oap->start_vcol = start;
3787 if (end > oap->end_vcol)
3788 {
3789 if (initial && *p_sel == 'e' && start >= 1
3790 && start - 1 >= oap->end_vcol)
3791 oap->end_vcol = start - 1;
3792 else
3793 oap->end_vcol = end;
3794 }
3795 }
3796
3797 // if '$' was used, get oap->end_vcol from longest line
3798 if (curwin->w_curswant == MAXCOL)
3799 {
3800 curwin->w_cursor.col = MAXCOL;
3801 oap->end_vcol = 0;
3802 for (curwin->w_cursor.lnum = oap->start.lnum;
3803 curwin->w_cursor.lnum <= oap->end.lnum;
3804 ++curwin->w_cursor.lnum)
3805 {
3806 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3807 if (end > oap->end_vcol)
3808 oap->end_vcol = end;
3809 }
3810 }
3811 else if (redo_VIsual_busy)
3812 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3813 // Correct oap->end.col and oap->start.col to be the
3814 // upper-left and lower-right corner of the block area.
3815 //
3816 // (Actually, this does convert column positions into character
3817 // positions)
3818 curwin->w_cursor.lnum = oap->end.lnum;
3819 coladvance(oap->end_vcol);
3820 oap->end = curwin->w_cursor;
3821
3822 curwin->w_cursor = oap->start;
3823 coladvance(oap->start_vcol);
3824 oap->start = curwin->w_cursor;
3825}
3826
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003827// Information for redoing the previous Visual selection.
3828typedef struct {
3829 int rv_mode; // 'v', 'V', or Ctrl-V
3830 linenr_T rv_line_count; // number of lines
3831 colnr_T rv_vcol; // number of cols or end column
3832 long rv_count; // count for Visual operator
3833 int rv_arg; // extra argument
3834} redo_VIsual_T;
3835
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003836 static int
3837is_ex_cmdchar(cmdarg_T *cap)
3838{
3839 return cap->cmdchar == ':'
3840 || cap->cmdchar == K_COMMAND
3841 || cap->cmdchar == K_SCRIPT_COMMAND;
3842}
3843
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003844/*
3845 * Handle an operator after Visual mode or when the movement is finished.
3846 * "gui_yank" is true when yanking text for the clipboard.
3847 */
3848 void
3849do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3850{
3851 oparg_T *oap = cap->oap;
3852 pos_T old_cursor;
3853 int empty_region_error;
3854 int restart_edit_save;
3855#ifdef FEAT_LINEBREAK
3856 int lbr_saved = curwin->w_p_lbr;
3857#endif
3858
3859 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003860 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3861
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003862 int include_line_break = FALSE;
3863
3864#if defined(FEAT_CLIPBOARD)
3865 // Yank the visual area into the GUI selection register before we operate
3866 // on it and lose it forever.
3867 // Don't do it if a specific register was specified, so that ""x"*P works.
3868 // This could call do_pending_operator() recursively, but that's OK
3869 // because gui_yank will be TRUE for the nested call.
3870 if ((clip_star.available || clip_plus.available)
3871 && oap->op_type != OP_NOP
3872 && !gui_yank
3873 && VIsual_active
3874 && !redo_VIsual_busy
3875 && oap->regname == 0)
3876 clip_auto_select();
3877#endif
3878 old_cursor = curwin->w_cursor;
3879
3880 // If an operation is pending, handle it...
3881 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3882 {
3883 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3884 // for the clipboard.
3885 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3886
3887#ifdef FEAT_LINEBREAK
3888 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003889 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003890#endif
3891 oap->is_VIsual = VIsual_active;
3892 if (oap->motion_force == 'V')
3893 oap->motion_type = MLINE;
3894 else if (oap->motion_force == 'v')
3895 {
3896 // If the motion was linewise, "inclusive" will not have been set.
3897 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3898 if (oap->motion_type == MLINE)
3899 oap->inclusive = FALSE;
3900 // If the motion already was characterwise, toggle "inclusive"
3901 else if (oap->motion_type == MCHAR)
3902 oap->inclusive = !oap->inclusive;
3903 oap->motion_type = MCHAR;
3904 }
3905 else if (oap->motion_force == Ctrl_V)
3906 {
3907 // Change line- or characterwise motion into Visual block mode.
3908 if (!VIsual_active)
3909 {
3910 VIsual_active = TRUE;
3911 VIsual = oap->start;
3912 }
3913 VIsual_mode = Ctrl_V;
3914 VIsual_select = FALSE;
3915 VIsual_reselect = FALSE;
3916 }
3917
3918 // Only redo yank when 'y' flag is in 'cpoptions'.
3919 // Never redo "zf" (define fold).
3920 if ((redo_yank || oap->op_type != OP_YANK)
3921 && ((!VIsual_active || oap->motion_force)
3922 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003923 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003924 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003925 && cap->cmdchar != 'D'
3926#ifdef FEAT_FOLDING
3927 && oap->op_type != OP_FOLD
3928 && oap->op_type != OP_FOLDOPEN
3929 && oap->op_type != OP_FOLDOPENREC
3930 && oap->op_type != OP_FOLDCLOSE
3931 && oap->op_type != OP_FOLDCLOSEREC
3932 && oap->op_type != OP_FOLDDEL
3933 && oap->op_type != OP_FOLDDELREC
3934#endif
3935 )
3936 {
3937 prep_redo(oap->regname, cap->count0,
3938 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3939 oap->motion_force, cap->cmdchar, cap->nchar);
3940 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3941 {
3942 // If 'cpoptions' does not contain 'r', insert the search
3943 // pattern to really repeat the same command.
3944 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3945 AppendToRedobuffLit(cap->searchbuf, -1);
3946 AppendToRedobuff(NL_STR);
3947 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003948 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003949 {
3950 // do_cmdline() has stored the first typed line in
3951 // "repeat_cmdline". When several lines are typed repeating
3952 // won't be possible.
3953 if (repeat_cmdline == NULL)
3954 ResetRedobuff();
3955 else
3956 {
zeertzjq30b6d612023-05-07 17:39:23 +01003957 if (cap->cmdchar == ':')
3958 AppendToRedobuffLit(repeat_cmdline, -1);
3959 else
3960 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003961 AppendToRedobuff(NL_STR);
3962 VIM_CLEAR(repeat_cmdline);
3963 }
3964 }
3965 }
3966
3967 if (redo_VIsual_busy)
3968 {
3969 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003970 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003971 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003972 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003973 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3974 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003975 VIsual_mode = redo_VIsual.rv_mode;
3976 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003977 {
3978 if (VIsual_mode == 'v')
3979 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003980 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003981 {
3982 validate_virtcol();
3983 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003984 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003985 }
3986 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003987 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003988 }
3989 else
3990 {
3991 curwin->w_curswant = MAXCOL;
3992 }
3993 coladvance(curwin->w_curswant);
3994 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003995 cap->count0 = redo_VIsual.rv_count;
3996 if (redo_VIsual.rv_count != 0)
3997 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003998 else
3999 cap->count1 = 1;
4000 }
4001 else if (VIsual_active)
4002 {
4003 if (!gui_yank)
4004 {
4005 // Save the current VIsual area for '< and '> marks, and "gv"
4006 curbuf->b_visual.vi_start = VIsual;
4007 curbuf->b_visual.vi_end = curwin->w_cursor;
4008 curbuf->b_visual.vi_mode = VIsual_mode;
4009 restore_visual_mode();
4010 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00004011#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004012 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00004013#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004014 }
4015
4016 // In Select mode, a linewise selection is operated upon like a
4017 // characterwise selection.
4018 // Special case: gH<Del> deletes the last line.
4019 if (VIsual_select && VIsual_mode == 'V'
4020 && cap->oap->op_type != OP_DELETE)
4021 {
4022 if (LT_POS(VIsual, curwin->w_cursor))
4023 {
4024 VIsual.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01004025 curwin->w_cursor.col = ml_get_len(curwin->w_cursor.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004026 }
4027 else
4028 {
4029 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01004030 VIsual.col = ml_get_len(VIsual.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004031 }
4032 VIsual_mode = 'v';
4033 }
4034 // If 'selection' is "exclusive", backup one character for
4035 // charwise selections.
4036 else if (VIsual_mode == 'v')
4037 include_line_break = unadjust_for_sel();
4038
4039 oap->start = VIsual;
4040 if (VIsual_mode == 'V')
4041 {
4042 oap->start.col = 0;
4043 oap->start.coladd = 0;
4044 }
4045 }
4046
4047 // Set oap->start to the first position of the operated text, oap->end
4048 // to the end of the operated text. w_cursor is equal to oap->start.
4049 if (LT_POS(oap->start, curwin->w_cursor))
4050 {
4051#ifdef FEAT_FOLDING
4052 // Include folded lines completely.
4053 if (!VIsual_active)
4054 {
4055 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
4056 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01004057 if ((curwin->w_cursor.col > 0 || oap->inclusive
4058 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004059 && hasFolding(curwin->w_cursor.lnum, NULL,
4060 &curwin->w_cursor.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01004061 curwin->w_cursor.col = ml_get_curline_len();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004062 }
4063#endif
4064 oap->end = curwin->w_cursor;
4065 curwin->w_cursor = oap->start;
4066
4067 // w_virtcol may have been updated; if the cursor goes back to its
4068 // previous position w_virtcol becomes invalid and isn't updated
4069 // automatically.
4070 curwin->w_valid &= ~VALID_VIRTCOL;
4071 }
4072 else
4073 {
4074#ifdef FEAT_FOLDING
4075 // Include folded lines completely.
4076 if (!VIsual_active && oap->motion_type == MLINE)
4077 {
4078 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
4079 NULL))
4080 curwin->w_cursor.col = 0;
4081 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01004082 oap->start.col = ml_get_len(oap->start.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004083 }
4084#endif
4085 oap->end = oap->start;
4086 oap->start = curwin->w_cursor;
4087 }
4088
4089 // Just in case lines were deleted that make the position invalid.
4090 check_pos(curwin->w_buffer, &oap->end);
4091 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
4092
4093 // Set "virtual_op" before resetting VIsual_active.
4094 virtual_op = virtual_active();
4095
4096 if (VIsual_active || redo_VIsual_busy)
4097 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004098 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004099
4100 if (!redo_VIsual_busy && !gui_yank)
4101 {
4102 // Prepare to reselect and redo Visual: this is based on the
4103 // size of the Visual text
4104 resel_VIsual_mode = VIsual_mode;
4105 if (curwin->w_curswant == MAXCOL)
4106 resel_VIsual_vcol = MAXCOL;
4107 else
4108 {
4109 if (VIsual_mode != Ctrl_V)
4110 getvvcol(curwin, &(oap->end),
4111 NULL, NULL, &oap->end_vcol);
4112 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
4113 {
4114 if (VIsual_mode != Ctrl_V)
4115 getvvcol(curwin, &(oap->start),
4116 &oap->start_vcol, NULL, NULL);
4117 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
4118 }
4119 else
4120 resel_VIsual_vcol = oap->end_vcol;
4121 }
4122 resel_VIsual_line_count = oap->line_count;
4123 }
4124
4125 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
4126 if ((redo_yank || oap->op_type != OP_YANK)
4127 && oap->op_type != OP_COLON
4128#ifdef FEAT_FOLDING
4129 && oap->op_type != OP_FOLD
4130 && oap->op_type != OP_FOLDOPEN
4131 && oap->op_type != OP_FOLDOPENREC
4132 && oap->op_type != OP_FOLDCLOSE
4133 && oap->op_type != OP_FOLDCLOSEREC
4134 && oap->op_type != OP_FOLDDEL
4135 && oap->op_type != OP_FOLDDELREC
4136#endif
4137 && oap->motion_force == NUL
4138 )
4139 {
4140 // Prepare for redoing. Only use the nchar field for "r",
4141 // otherwise it might be the second char of the operator.
4142 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4143 || cap->nchar == 'N'))
4144 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004145 get_op_char(oap->op_type),
4146 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004147 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004148 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004149 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004150 int opchar = get_op_char(oap->op_type);
4151 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004152 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4153
4154 // reverse what nv_replace() did
4155 if (nchar == REPLACE_CR_NCHAR)
4156 nchar = CAR;
4157 else if (nchar == REPLACE_NL_NCHAR)
4158 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004159
4160 if (opchar == 'g' && extra_opchar == '@')
4161 // also repeat the count for 'operatorfunc'
4162 prep_redo_num2(oap->regname, 0L, NUL, 'v',
4163 cap->count0, opchar, extra_opchar, nchar);
4164 else
4165 prep_redo(oap->regname, 0L, NUL, 'v',
4166 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004167 }
4168 if (!redo_VIsual_busy)
4169 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004170 redo_VIsual.rv_mode = resel_VIsual_mode;
4171 redo_VIsual.rv_vcol = resel_VIsual_vcol;
4172 redo_VIsual.rv_line_count = resel_VIsual_line_count;
4173 redo_VIsual.rv_count = cap->count0;
4174 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004175 }
4176 }
4177
4178 // oap->inclusive defaults to TRUE.
4179 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4180 // FALSE. This makes "d}P" and "v}dP" work the same.
4181 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4182 oap->inclusive = TRUE;
4183 if (VIsual_mode == 'V')
4184 oap->motion_type = MLINE;
4185 else
4186 {
4187 oap->motion_type = MCHAR;
4188 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4189 && (include_line_break || !virtual_op))
4190 {
4191 oap->inclusive = FALSE;
4192 // Try to include the newline, unless it's an operator
4193 // that works on lines only.
4194 if (*p_sel != 'o'
4195 && !op_on_lines(oap->op_type)
4196 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4197 {
4198 ++oap->end.lnum;
4199 oap->end.col = 0;
4200 oap->end.coladd = 0;
4201 ++oap->line_count;
4202 }
4203 }
4204 }
4205
4206 redo_VIsual_busy = FALSE;
4207
4208 // Switch Visual off now, so screen updating does
4209 // not show inverted text when the screen is redrawn.
4210 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4211 // no screen redraw, so it is done here to remove the inverted
4212 // part.
4213 if (!gui_yank)
4214 {
4215 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004216 setmouse();
4217 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004218 may_clear_cmdline();
4219 if ((oap->op_type == OP_YANK
4220 || oap->op_type == OP_COLON
4221 || oap->op_type == OP_FUNCTION
4222 || oap->op_type == OP_FILTER)
4223 && oap->motion_force == NUL)
4224 {
4225#ifdef FEAT_LINEBREAK
4226 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01004227 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004228#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004229 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004230 }
4231 }
4232 }
4233
4234 // Include the trailing byte of a multi-byte char.
4235 if (has_mbyte && oap->inclusive)
4236 {
4237 int l;
4238
4239 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4240 if (l > 1)
4241 oap->end.col += l - 1;
4242 }
4243 curwin->w_set_curswant = TRUE;
4244
4245 // oap->empty is set when start and end are the same. The inclusive
4246 // flag affects this too, unless yanking and the end is on a NUL.
4247 oap->empty = (oap->motion_type == MCHAR
4248 && (!oap->inclusive
4249 || (oap->op_type == OP_YANK
4250 && gchar_pos(&oap->end) == NUL))
4251 && EQUAL_POS(oap->start, oap->end)
4252 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4253 // For delete, change and yank, it's an error to operate on an
4254 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4255 empty_region_error = (oap->empty
4256 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4257
4258 // Force a redraw when operating on an empty Visual region, when
4259 // 'modifiable is off or creating a fold.
4260 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4261#ifdef FEAT_FOLDING
4262 || oap->op_type == OP_FOLD
4263#endif
4264 ))
4265 {
4266#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004267 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004268#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004269 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004270 }
4271
4272 // If the end of an operator is in column one while oap->motion_type
4273 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4274 // character in the previous line. If op_start is on or before the
4275 // first non-blank in the line, the operator becomes linewise
4276 // (strange, but that's the way vi does it).
4277 if ( oap->motion_type == MCHAR
4278 && oap->inclusive == FALSE
4279 && !(cap->retval & CA_NO_ADJ_OP_END)
4280 && oap->end.col == 0
4281 && (!oap->is_VIsual || *p_sel == 'o')
4282 && !oap->block_mode
4283 && oap->line_count > 1)
4284 {
4285 oap->end_adjusted = TRUE; // remember that we did this
4286 --oap->line_count;
4287 --oap->end.lnum;
4288 if (inindent(0))
4289 oap->motion_type = MLINE;
4290 else
4291 {
zeertzjq94b7c322024-03-12 21:50:32 +01004292 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004293 if (oap->end.col)
4294 {
4295 --oap->end.col;
4296 oap->inclusive = TRUE;
4297 }
4298 }
4299 }
4300 else
4301 oap->end_adjusted = FALSE;
4302
4303 switch (oap->op_type)
4304 {
4305 case OP_LSHIFT:
4306 case OP_RSHIFT:
4307 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4308 auto_format(FALSE, TRUE);
4309 break;
4310
4311 case OP_JOIN_NS:
4312 case OP_JOIN:
4313 if (oap->line_count < 2)
4314 oap->line_count = 2;
4315 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4316 curbuf->b_ml.ml_line_count)
4317 beep_flush();
4318 else
4319 {
4320 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4321 TRUE, TRUE, TRUE);
4322 auto_format(FALSE, TRUE);
4323 }
4324 break;
4325
4326 case OP_DELETE:
4327 VIsual_reselect = FALSE; // don't reselect now
4328 if (empty_region_error)
4329 {
4330 vim_beep(BO_OPER);
4331 CancelRedo();
4332 }
4333 else
4334 {
4335 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004336 // save cursor line for undo if it wasn't saved yet
4337 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4338 && u_save_cursor() == OK)
4339 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004340 }
4341 break;
4342
4343 case OP_YANK:
4344 if (empty_region_error)
4345 {
4346 if (!gui_yank)
4347 {
4348 vim_beep(BO_OPER);
4349 CancelRedo();
4350 }
4351 }
4352 else
4353 {
4354#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004355 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004356#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004357 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004358 (void)op_yank(oap, FALSE, !gui_yank);
4359 }
4360 check_cursor_col();
4361 break;
4362
4363 case OP_CHANGE:
4364 VIsual_reselect = FALSE; // don't reselect now
4365 if (empty_region_error)
4366 {
4367 vim_beep(BO_OPER);
4368 CancelRedo();
4369 }
4370 else
4371 {
4372 // This is a new edit command, not a restart. Need to
4373 // remember it to make 'insertmode' work with mappings for
4374 // Visual mode. But do this only once and not when typed and
4375 // 'insertmode' isn't set.
4376 if (p_im || !KeyTyped)
4377 restart_edit_save = restart_edit;
4378 else
4379 restart_edit_save = 0;
4380 restart_edit = 0;
4381#ifdef FEAT_LINEBREAK
4382 // Restore linebreak, so that when the user edits it looks as
4383 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004384 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004385#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004386 // trigger TextChangedI
4387 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4388
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004389 if (op_change(oap)) // will call edit()
4390 cap->retval |= CA_COMMAND_BUSY;
4391 if (restart_edit == 0)
4392 restart_edit = restart_edit_save;
4393 }
4394 break;
4395
4396 case OP_FILTER:
4397 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4398 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4399 else
4400 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4401 // FALLTHROUGH
4402
4403 case OP_INDENT:
4404 case OP_COLON:
4405
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004406 // If 'equalprg' is empty, do the indenting internally.
4407 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4408 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004409 if (curbuf->b_p_lisp)
4410 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004411#ifdef FEAT_EVAL
4412 if (use_indentexpr_for_lisp())
4413 op_reindent(oap, get_expr_indent);
4414 else
4415#endif
4416 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004417 break;
4418 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004419 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004420#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004421 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004422#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004423 get_c_indent);
4424 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004425 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004426
4427 op_colon(oap);
4428 break;
4429
4430 case OP_TILDE:
4431 case OP_UPPER:
4432 case OP_LOWER:
4433 case OP_ROT13:
4434 if (empty_region_error)
4435 {
4436 vim_beep(BO_OPER);
4437 CancelRedo();
4438 }
4439 else
4440 op_tilde(oap);
4441 check_cursor_col();
4442 break;
4443
4444 case OP_FORMAT:
4445#if defined(FEAT_EVAL)
4446 if (*curbuf->b_p_fex != NUL)
4447 op_formatexpr(oap); // use expression
4448 else
4449#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004450 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004451 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004452 op_colon(oap); // use external command
4453 else
4454 op_format(oap, FALSE); // use internal function
4455 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004456 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004457 case OP_FORMAT2:
4458 op_format(oap, TRUE); // use internal function
4459 break;
4460
4461 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004462 {
4463 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4464
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004465#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004466 // Restore linebreak, so that when the user edits it looks as
4467 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004468 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004469#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004470 // call 'operatorfunc'
4471 op_function(oap);
4472
4473 // Restore the info for redoing Visual mode, the function may
4474 // invoke another operator and unintentionally change it.
4475 redo_VIsual = save_redo_VIsual;
4476 break;
4477 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004478
4479 case OP_INSERT:
4480 case OP_APPEND:
4481 VIsual_reselect = FALSE; // don't reselect now
4482 if (empty_region_error)
4483 {
4484 vim_beep(BO_OPER);
4485 CancelRedo();
4486 }
4487 else
4488 {
4489 // This is a new edit command, not a restart. Need to
4490 // remember it to make 'insertmode' work with mappings for
4491 // Visual mode. But do this only once.
4492 restart_edit_save = restart_edit;
4493 restart_edit = 0;
4494#ifdef FEAT_LINEBREAK
4495 // Restore linebreak, so that when the user edits it looks as
4496 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004497 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004498#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004499 // trigger TextChangedI
4500 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4501
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004502 op_insert(oap, cap->count1);
4503#ifdef FEAT_LINEBREAK
4504 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004505 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004506#endif
4507
4508 // TODO: when inserting in several lines, should format all
4509 // the lines.
4510 auto_format(FALSE, TRUE);
4511
4512 if (restart_edit == 0)
4513 restart_edit = restart_edit_save;
4514 else
4515 cap->retval |= CA_COMMAND_BUSY;
4516 }
4517 break;
4518
4519 case OP_REPLACE:
4520 VIsual_reselect = FALSE; // don't reselect now
4521 if (empty_region_error)
4522 {
4523 vim_beep(BO_OPER);
4524 CancelRedo();
4525 }
4526 else
4527 {
4528#ifdef FEAT_LINEBREAK
4529 // Restore linebreak, so that when the user edits it looks as
4530 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004531 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004532#endif
4533 op_replace(oap, cap->nchar);
4534 }
4535 break;
4536
4537#ifdef FEAT_FOLDING
4538 case OP_FOLD:
4539 VIsual_reselect = FALSE; // don't reselect now
4540 foldCreate(oap->start.lnum, oap->end.lnum);
4541 break;
4542
4543 case OP_FOLDOPEN:
4544 case OP_FOLDOPENREC:
4545 case OP_FOLDCLOSE:
4546 case OP_FOLDCLOSEREC:
4547 VIsual_reselect = FALSE; // don't reselect now
4548 opFoldRange(oap->start.lnum, oap->end.lnum,
4549 oap->op_type == OP_FOLDOPEN
4550 || oap->op_type == OP_FOLDOPENREC,
4551 oap->op_type == OP_FOLDOPENREC
4552 || oap->op_type == OP_FOLDCLOSEREC,
4553 oap->is_VIsual);
4554 break;
4555
4556 case OP_FOLDDEL:
4557 case OP_FOLDDELREC:
4558 VIsual_reselect = FALSE; // don't reselect now
4559 deleteFold(oap->start.lnum, oap->end.lnum,
4560 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4561 break;
4562#endif
4563 case OP_NR_ADD:
4564 case OP_NR_SUB:
4565 if (empty_region_error)
4566 {
4567 vim_beep(BO_OPER);
4568 CancelRedo();
4569 }
4570 else
4571 {
4572 VIsual_active = TRUE;
4573#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004574 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004575#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004576 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004577 VIsual_active = FALSE;
4578 }
4579 check_cursor_col();
4580 break;
4581 default:
4582 clearopbeep(oap);
4583 }
4584 virtual_op = MAYBE;
4585 if (!gui_yank)
4586 {
4587 // if 'sol' not set, go back to old column for some commands
4588 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4589 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4590 || oap->op_type == OP_DELETE))
4591 {
4592#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004593 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004594#endif
4595 coladvance(curwin->w_curswant = old_col);
4596 }
4597 }
4598 else
4599 {
4600 curwin->w_cursor = old_cursor;
4601 }
4602 oap->block_mode = FALSE;
4603 clearop(oap);
4604 motion_force = NUL;
4605 }
4606#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004607 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004608#endif
4609}
Christian Brabandtffb13672023-09-15 20:22:02 +02004610
4611// put byte 'c' at position 'lp', but
4612// verify, that the position to place
4613// is actually safe
4614 static void
4615pbyte(pos_T lp, int c)
4616{
4617 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4618 int len = curbuf->b_ml.ml_line_len;
4619
4620 // safety check
4621 if (lp.col >= len)
4622 lp.col = (len > 1 ? len - 2 : 0);
4623 *(p + lp.col) = c;
4624}