blob: dcb48d32334261ddf9c6001f1c51940cc496fd99 [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
221/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100222 * Shift the current line one shiftwidth left (if left != 0) or right
223 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 */
225 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100226shift_line(
227 int left,
228 int round,
229 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200230 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
Ernie Raelfda700c2023-11-30 18:20:00 +0100232 vimlong_T count;
Ernie Rael2b0882f2023-11-23 20:21:45 +0100233 int i, j;
Gary Johnson88d4f252024-06-01 20:51:33 +0200234 int sw_val = trim_to_int(get_sw_value_indent(curbuf, left));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Christian Brabandt7737ce52024-06-02 16:04:43 +0200236 if (sw_val == 0)
237 sw_val = 1; // shouldn't happen, just in case
238
Ernie Raelfda700c2023-11-30 18:20:00 +0100239 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200241 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200243 i = count / sw_val; // number of 'shiftwidth' rounded down
244 j = count % sw_val; // extra spaces
245 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 --amount;
247 if (left)
248 {
249 i -= amount;
250 if (i < 0)
251 i = 0;
252 }
253 else
254 i += amount;
Ernie Raelfda700c2023-11-30 18:20:00 +0100255 count = (vimlong_T)i * (vimlong_T)sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200257 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 {
259 if (left)
260 {
Ernie Raelfda700c2023-11-30 18:20:00 +0100261 count -= (vimlong_T)sw_val * (vimlong_T)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 if (count < 0)
263 count = 0;
264 }
265 else
Ernie Raelfda700c2023-11-30 18:20:00 +0100266 count += (vimlong_T)sw_val * (vimlong_T)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 }
268
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200269 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 if (State & VREPLACE_FLAG)
Ernie Rael2b0882f2023-11-23 20:21:45 +0100271 change_indent(INDENT_SET, trim_to_int(count), FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 else
Ernie Rael2b0882f2023-11-23 20:21:45 +0100273 (void)set_indent(trim_to_int(count), call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274}
275
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276/*
277 * Shift one line of the current block one shiftwidth right or left.
278 * Leaves cursor on first character in block.
279 */
280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100281shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
283 int left = (oap->op_type == OP_LSHIFT);
284 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000285 int total;
286 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +0200287 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 int oldcol = curwin->w_cursor.col;
Gary Johnson88d4f252024-06-01 20:51:33 +0200289 int sw_val = (int)get_sw_value_indent(curbuf, left);
Bram Moolenaardac13472019-09-16 21:06:21 +0200290 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000293 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100294 int added;
John Marriott38b9f452024-04-25 21:39:18 +0200295 size_t new_line_len; // the length of the line after the
LemonBoy4b936742022-05-13 21:56:28 +0100296 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297#ifdef FEAT_RIGHTLEFT
298 int old_p_ri = p_ri;
299
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100300 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#endif
302
Bram Moolenaar24959102022-05-07 20:01:16 +0100303 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
305 if (bd.is_short)
306 return;
307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100308 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 total = (int)((unsigned)amount * (unsigned)sw_val);
310 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100311 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200312
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 oldp = ml_get_curline();
John Marriott38b9f452024-04-25 21:39:18 +0200314 oldlen = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315
316 if (!left)
317 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100318 int tabs = 0, spaces = 0;
319 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100320
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 /*
322 * 1. Get start vcol
323 * 2. Total ws vcols
324 * 3. Divvy into TABs & spp
325 * 4. Construct new string
326 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100327 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 ws_vcol = bd.start_vcol - bd.pre_whitesp;
329 if (bd.startspaces)
330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100332 {
333 if ((*mb_ptr2len)(bd.textstart) == 1)
334 ++bd.textstart;
335 else
336 {
337 ws_vcol = 0;
338 bd.startspaces = 0;
339 }
340 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000341 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000342 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100344
345 // TODO: is passing bd.textstart for start of the line OK?
346 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
347 bd.start_vcol, bd.textstart, bd.textstart);
348 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100350 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100352 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100354 bd.textstart = cts.cts_ptr;
355 bd.start_vcol = cts.cts_vcol;
356 clear_chartabsize_arg(&cts);
357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100358 // OK, now total=all the VWS reqd, and textstart points at the 1st
359 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200360#ifdef FEAT_VARTABS
361 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100363 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200364 else
LemonBoy4b936742022-05-13 21:56:28 +0100365 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200366#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100368 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
369 if (tabs > 0)
370 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 else
LemonBoy4b936742022-05-13 21:56:28 +0100372 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200373#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100374 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100376
John Marriott38b9f452024-04-25 21:39:18 +0200377 new_line_len = bd.textcol + tabs + spaces + (oldlen - (bd.textstart - oldp));
LemonBoy4b936742022-05-13 21:56:28 +0100378 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (newp == NULL)
380 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +0200382 newlen = bd.textcol;
383 vim_memset(newp + newlen, TAB, (size_t)tabs);
384 newlen += tabs;
385 vim_memset(newp + newlen, ' ', (size_t)spaces);
386 STRCPY(newp + newlen + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100388 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100390 colnr_T destination_col; // column to which text in block will
391 // be shifted
392 char_u *verbatim_copy_end; // end of the part of the line which is
393 // copied verbatim
394 colnr_T verbatim_copy_width;// the (displayed) width of this part
395 // of line
John Marriott38b9f452024-04-25 21:39:18 +0200396 size_t fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000397 size_t block_space_width;
398 size_t shift_amount;
399 char_u *non_white = bd.textstart;
400 colnr_T non_white_col;
John Marriott38b9f452024-04-25 21:39:18 +0200401 size_t fixedlen; // length of string left of the shift
402 // position (ie the string not being shifted)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100403 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000405 /*
406 * Firstly, let's find the first non-whitespace character that is
407 * displayed after the block's start column and the character's column
408 * number. Also, let's calculate the width of all the whitespace
409 * characters that are displayed in the block and precede the searched
410 * non-whitespace character.
411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // If "bd.startspaces" is set, "bd.textstart" points to the character,
414 // the part of which is displayed at the block's beginning. Let's start
415 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000416 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100417 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100419 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000420 non_white_col = bd.start_vcol;
421
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100422 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
423 non_white_col, bd.textstart, non_white);
424 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000425 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100426 incr = lbr_chartabsize_adv(&cts);
427 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100429 non_white_col = cts.cts_vcol;
430 non_white = cts.cts_ptr;
431 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000433 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100434 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000435 shift_amount = (block_space_width < (size_t)total
436 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000439 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100441 // Now let's find out how much of the beginning of the line we can
442 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000443 verbatim_copy_end = bd.textstart;
444 verbatim_copy_width = bd.start_vcol;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // If "bd.startspaces" is set, "bd.textstart" points to the character
447 // preceding the block. We have to subtract its width to obtain its
448 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000449 if (bd.startspaces)
450 verbatim_copy_width -= bd.start_char_vcols;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100451 init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
452 bd.textstart, verbatim_copy_end);
453 while (cts.cts_vcol < destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000454 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100455 incr = lbr_chartabsize(&cts);
456 if (cts.cts_vcol + incr > destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 cts.cts_vcol += incr;
459 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000460 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100461 verbatim_copy_width = cts.cts_vcol;
462 verbatim_copy_end = cts.cts_ptr;
463 clear_chartabsize_arg(&cts);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100465 // If "destination_col" is different from the width of the initial
466 // part of the line that will be copied, it means we encountered a tab
467 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000468 fill = destination_col - verbatim_copy_width;
469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100470 // The replacement line will consist of:
471 // - the beginning of the original line up to "verbatim_copy_end",
472 // - "fill" number of spaces,
473 // - the rest of the line, pointed to by non_white.
John Marriott38b9f452024-04-25 21:39:18 +0200474 fixedlen = verbatim_copy_end - oldp;
475 new_line_len = fixedlen + fill + (oldlen - (non_white - oldp));
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000476
LemonBoy4b936742022-05-13 21:56:28 +0100477 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (newp == NULL)
479 return;
John Marriott38b9f452024-04-25 21:39:18 +0200480 mch_memmove(newp, oldp, fixedlen);
481 newlen = fixedlen;
482 vim_memset(newp + newlen, ' ', (size_t)fill);
483 STRCPY(newp + newlen + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100485 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
John Marriott38b9f452024-04-25 21:39:18 +0200487
488 // compute the number of bytes added or subtracted.
489 // note new_line_len and oldlen are unsigned so we have
490 // to be careful about how we calculate this.
491 if (new_line_len >= oldlen)
492 added = (int)(new_line_len - oldlen);
493 else
494 added = 0 - (int)(oldlen - new_line_len);
LemonBoy4b936742022-05-13 21:56:28 +0100495 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 State = oldstate;
497 curwin->w_cursor.col = oldcol;
498#ifdef FEAT_RIGHTLEFT
499 p_ri = old_p_ri;
500#endif
501}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503/*
504 * Insert string "s" (b_insert ? before : after) block :AKelly
505 * Caller must prepare for undo.
506 */
507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100508block_insert(
509 oparg_T *oap,
510 char_u *s,
John Marriott38b9f452024-04-25 21:39:18 +0200511 size_t slen,
Bram Moolenaar9b578142016-01-30 19:39:49 +0100512 int b_insert,
513 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514{
Bram Moolenaardac13472019-09-16 21:06:21 +0200515 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100516 int count = 0; // extra spaces to replace a cut TAB
517 int spaces = 0; // non-zero if cutting a TAB
518 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200519 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100520 char_u *newp, *oldp; // new, old lines
521 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 int oldstate = State;
523
Bram Moolenaar24959102022-05-07 20:01:16 +0100524 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
526 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
527 {
528 block_prep(oap, bdp, lnum, TRUE);
529 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100530 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 oldp = ml_get(lnum);
533
534 if (b_insert)
535 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200536 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 spaces = bdp->startspaces;
538 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100539 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 offset = bdp->textcol;
541 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200544 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100545 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200547 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 offset = bdp->textcol + bdp->textlen - (spaces != 0);
551 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100552 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100554 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 if (!bdp->is_MAX)
556 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
557 count = spaces;
558 offset = bdp->textcol + bdp->textlen;
559 }
560 }
561
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200562 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000563 // avoid copying part of a multi-byte character
564 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100565
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200566 if (spaces < 0) // can happen when the cursor was moved
567 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200568
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000569 // Make sure the allocated size matches what is actually copied below.
John Marriott38b9f452024-04-25 21:39:18 +0200570 newp = alloc(ml_get_len(lnum) + spaces + slen
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000571 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
572 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (newp == NULL)
574 continue;
575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100576 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000577 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 oldp += offset;
579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100580 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200581 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200582 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100584 // copy the new text
John Marriott38b9f452024-04-25 21:39:18 +0200585 mch_memmove(newp + startcol, s, slen);
Mike Williams16b63bd2024-06-01 11:33:40 +0200586 offset += (int)slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000588 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000590 if (*oldp == TAB)
591 {
592 // insert post-padding
593 vim_memset(newp + offset + spaces, ' ',
594 (size_t)(ts_val - spaces));
595 // we're splitting a TAB, don't copy it
596 oldp++;
597 // We allowed for that TAB, remember this now
598 count++;
599 }
600 else
601 // Not a TAB, no extra spaces
602 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
604
605 if (spaces > 0)
606 offset += count;
John Marriott38b9f452024-04-25 21:39:18 +0200607 STRCPY(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609 ml_replace(lnum, newp, FALSE);
610
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200611 if (b_insert)
612 // correct any text properties
Mike Williams16b63bd2024-06-01 11:33:40 +0200613 inserted_bytes(lnum, startcol, (int)slen);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200614
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 if (lnum == oap->end.lnum)
616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // Set "']" mark to the end of the block instead of the end of
618 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 curbuf->b_op_end.lnum = oap->end.lnum;
620 curbuf->b_op_end.col = offset;
621 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100622 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
625
626 State = oldstate;
627}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200630 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200632 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 */
634 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100635op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 int n;
638 linenr_T lnum;
639 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 char_u *newp, *oldp;
641 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
643 int did_yank = FALSE;
644
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 return OK;
647
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100648 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (oap->empty)
650 return u_save_cursor();
651
652 if (!curbuf->b_p_ma)
653 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200654 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 return FAIL;
656 }
657
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000658 if (VIsual_select && oap->is_VIsual)
659 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100660 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#ifdef FEAT_CLIPBOARD
663 adjust_clip_reg(&oap->regname);
664#endif
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (has_mbyte)
667 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaard04b7502010-07-08 22:27:55 +0200669 /*
670 * Imitate the strange Vi behaviour: If the delete spans more than one
671 * line and motion_type == MCHAR and the result is a blank line, make the
672 * delete linewise. Don't do this for the change command or Visual mode.
673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000676 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100678 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 && oap->op_type == OP_DELETE)
680 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200681 ptr = ml_get(oap->end.lnum) + oap->end.col;
682 if (*ptr != NUL)
683 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ptr = skipwhite(ptr);
685 if (*ptr == NUL && inindent(0))
686 oap->motion_type = MLINE;
687 }
688
Bram Moolenaard04b7502010-07-08 22:27:55 +0200689 /*
690 * Check for trying to delete (e.g. "D") in an empty line.
691 * Note: For the change operator it is ok.
692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if ( oap->motion_type == MCHAR
694 && oap->line_count == 1
695 && oap->op_type == OP_DELETE
696 && *ml_get(oap->start.lnum) == NUL)
697 {
698 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000699 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 * 'cpoptions' (Vi compatible).
701 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000702 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100703 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
704 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000705 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
707 beep_flush();
708 return OK;
709 }
710
Bram Moolenaard04b7502010-07-08 22:27:55 +0200711 /*
712 * Do a yank of whatever we're about to delete.
713 * If a yank register was specified, put the deleted text into that
714 * register. For the black hole register '_' don't yank anything.
715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (oap->regname != '_')
717 {
718 if (oap->regname != 0)
719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (!valid_yank_reg(oap->regname, TRUE))
722 {
723 beep_flush();
724 return OK;
725 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100726 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
727 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 did_yank = TRUE;
729 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200730 else
731 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733 /*
734 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100735 * delete contains a line break, or when using a specific operator (Vi
736 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100738 if (oap->motion_type == MLINE || oap->line_count > 1
739 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100741 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 if (op_yank(oap, TRUE, FALSE) == OK)
743 did_yank = TRUE;
744 }
745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100746 // Yank into small delete register when no named register specified
747 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200748 if ((
749#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200750 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
751 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200752#endif
753 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 && oap->line_count == 1)
755 {
756 oap->regname = '-';
757 get_yank_register(oap->regname, TRUE);
758 if (op_yank(oap, TRUE, FALSE) == OK)
759 did_yank = TRUE;
760 oap->regname = 0;
761 }
762
763 /*
764 * If there's too much stuff to fit in the yank register, then get a
765 * confirmation before doing the delete. This is crude, but simple.
766 * And it avoids doing a delete of something we can't put back if we
767 * want.
768 */
769 if (!did_yank)
770 {
771 int msg_silent_save = msg_silent;
772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100773 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
775 msg_silent = msg_silent_save;
776 if (n != 'y')
777 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000778 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 return FAIL;
780 }
781 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100782
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100783#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100784 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200785 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788
Bram Moolenaard04b7502010-07-08 22:27:55 +0200789 /*
790 * block mode delete
791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (oap->block_mode)
793 {
794 if (u_save((linenr_T)(oap->start.lnum - 1),
795 (linenr_T)(oap->end.lnum + 1)) == FAIL)
796 return FAIL;
797
798 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
799 {
800 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 continue;
803
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100804 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 if (lnum == curwin->w_cursor.lnum)
806 {
807 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810
Bram Moolenaar8055d172019-05-17 22:57:26 +0200811 // "n" == number of chars deleted
812 // If we delete a TAB, it may be replaced by several characters.
813 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 n = bd.textlen - bd.startspaces - bd.endspaces;
815 oldp = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100816 newp = alloc(ml_get_len(lnum) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (newp == NULL)
818 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100821 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200822 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100824 // copy the part after the deleted part
John Marriott38b9f452024-04-25 21:39:18 +0200825 STRCPY(newp + bd.textcol + bd.startspaces + bd.endspaces,
826 oldp + bd.textcol + bd.textlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200829
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100830#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200831 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200832 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835
836 check_cursor_col();
837 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
838 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100839 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100841 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 if (oap->op_type == OP_CHANGE)
844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 // Delete the lines except the first one. Temporarily move the
846 // cursor to the next line. Save the current line number, if the
847 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (oap->line_count > 1)
849 {
850 lnum = curwin->w_cursor.lnum;
851 ++curwin->w_cursor.lnum;
852 del_lines((long)(oap->line_count - 1), TRUE);
853 curwin->w_cursor.lnum = lnum;
854 }
855 if (u_save_cursor() == FAIL)
856 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 beginline(BL_WHITE); // cursor on first non-white
860 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 ai_col = curwin->w_cursor.col;
862 }
863 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 beginline(0); // cursor in column 0
zeertzjq8c55d602024-03-13 20:42:26 +0100865 truncate_line(FALSE); // delete the rest of the line,
866 // leaving cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100868 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 else
871 {
872 del_lines(oap->line_count, TRUE);
873 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100874 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876 }
877 else
878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (virtual_op)
880 {
881 int endcol = 0;
882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (gchar_pos(&oap->start) == '\t')
885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 return FAIL;
888 if (oap->line_count == 1)
889 endcol = getviscol2(oap->end.col, oap->end.coladd);
890 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
891 oap->start = curwin->w_cursor;
892 if (oap->line_count == 1)
893 {
894 coladvance(endcol);
895 oap->end.col = curwin->w_cursor.col;
896 oap->end.coladd = curwin->w_cursor.coladd;
897 curwin->w_cursor = oap->start;
898 }
899 }
900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (gchar_pos(&oap->end) == '\t'
903 && (int)oap->end.coladd < oap->inclusive)
904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100905 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (u_save((linenr_T)(oap->end.lnum - 1),
907 (linenr_T)(oap->end.lnum + 1)) == FAIL)
908 return FAIL;
909 curwin->w_cursor = oap->end;
910 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
911 oap->end = curwin->w_cursor;
912 curwin->w_cursor = oap->start;
913 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100914 if (has_mbyte)
915 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return FAIL;
922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100923 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100924 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 && oap->op_type == OP_CHANGE
926 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100927 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 display_dollar(oap->end.col - !oap->inclusive);
929
930 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (virtual_op)
933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100934 // fix up things for virtualedit-delete:
935 // break the tabs which are going to get in our way
zeertzjq94b7c322024-03-12 21:50:32 +0100936 int len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
938 if (oap->end.coladd != 0
939 && (int)oap->end.col >= len - 1
940 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
941 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100942 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (n == 0 && oap->start.coladd != oap->end.coladd)
944 n = 1;
945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (gchar_cursor() != NUL)
948 curwin->w_cursor.coladd = 0;
949 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200950 (void)del_bytes((long)n, !virtual_op,
951 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 {
955 pos_T curpos;
956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
959 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
960 return FAIL;
961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100962 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100964 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 ++curwin->w_cursor.lnum;
966 del_lines((long)(oap->line_count - 2), FALSE);
967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100968 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200969 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200970 curwin->w_cursor.col = 0;
971 (void)del_bytes((long)n, !virtual_op,
972 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100973 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200974 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200976 if (oap->op_type == OP_DELETE)
977 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979
980 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
981
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000982setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200983 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100985 if (oap->block_mode)
986 {
987 curbuf->b_op_end.lnum = oap->end.lnum;
988 curbuf->b_op_end.col = oap->start.col;
989 }
990 else
991 curbuf->b_op_end = oap->start;
992 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 return OK;
996}
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998/*
999 * Adjust end of operating area for ending on a multi-byte character.
1000 * Used for deletion.
1001 */
1002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001003mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
zeertzjq048761b2024-02-24 14:21:39 +01001005 char_u *line;
1006 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001008 if (!oap->inclusive)
1009 return;
1010
zeertzjq048761b2024-02-24 14:21:39 +01001011 line = ml_get(oap->end.lnum);
1012 ptr = line + oap->end.col;
1013 if (*ptr != NUL)
1014 {
1015 ptr -= (*mb_head_off)(line, ptr);
1016 ptr += (*mb_ptr2len)(ptr) - 1;
1017 oap->end.col = ptr - line;
1018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
Bram Moolenaar630afe82018-06-28 19:26:28 +02001021/*
1022 * Replace the character under the cursor with "c".
1023 * This takes care of multi-byte characters.
1024 */
1025 static void
1026replace_character(int c)
1027{
1028 int n = State;
1029
Bram Moolenaar24959102022-05-07 20:01:16 +01001030 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001031 ins_char(c);
1032 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001033 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001034 dec_cursor();
1035}
1036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037/*
1038 * Replace a whole area with one character.
1039 */
1040 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001041op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +02001046 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001048 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001049 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001052 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001054 if (c == REPLACE_CR_NCHAR)
1055 {
1056 had_ctrl_v_cr = TRUE;
1057 c = CAR;
1058 }
1059 else if (c == REPLACE_NL_NCHAR)
1060 {
1061 had_ctrl_v_cr = TRUE;
1062 c = NL;
1063 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (has_mbyte)
1066 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068 if (u_save((linenr_T)(oap->start.lnum - 1),
1069 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1070 return FAIL;
1071
1072 /*
1073 * block mode replace
1074 */
1075 if (oap->block_mode)
1076 {
1077 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1078 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1079 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001080 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1082 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001083 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001085 // n == number of extra chars required
1086 // If we split a TAB, it may be replaced by several characters.
1087 // Thus the number of characters may increase!
1088 // If the range starts in virtual space, count the initial
1089 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1091 {
1092 pos_T vpos;
1093
Bram Moolenaara1381de2009-11-03 15:44:21 +00001094 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 getvpos(&vpos, oap->start_vcol);
1096 bd.startspaces += vpos.coladd;
1097 n = bd.startspaces;
1098 }
1099 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1102
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001103 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001107 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 numc = oap->end_vcol - oap->start_vcol + 1;
1109 if (bd.is_short && (!virtual_op || bd.is_MAX))
1110 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1111
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001112 // A double-wide character can be replaced only up to half the
1113 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if ((*mb_char2cells)(c) > 1)
1115 {
1116 if ((numc & 1) && !bd.is_short)
1117 {
1118 ++bd.endspaces;
1119 ++n;
1120 }
1121 numc = numc / 2;
1122 }
1123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001124 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 num_chars = numc;
1126 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001127 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 n += numc - bd.textlen;
1129
1130 oldp = ml_get_curline();
zeertzjq94b7c322024-03-12 21:50:32 +01001131 oldlen = ml_get_curline_len();
Bram Moolenaar964b3742019-05-24 18:54:09 +02001132 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (newp == NULL)
1134 continue;
1135 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001138 newlen = bd.textcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001139 // insert pre-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001140 vim_memset(newp + newlen, ' ', (size_t)bd.startspaces);
1141 newlen += bd.startspaces;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001142 // insert replacement chars CHECK FOR ALLOCATED SPACE
1143 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1144 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001145 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001147 if (has_mbyte)
1148 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001149 while (--num_chars >= 0)
John Marriott38b9f452024-04-25 21:39:18 +02001150 newlen += (*mb_char2bytes)(c, newp + newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001151 }
1152 else
John Marriott38b9f452024-04-25 21:39:18 +02001153 {
1154 vim_memset(newp + newlen, c, (size_t)numc);
1155 newlen += numc;
1156 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001157 if (!bd.is_short)
1158 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001159 // insert post-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001160 vim_memset(newp + newlen, ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001161 // copy the part after the changed part
John Marriott38b9f452024-04-25 21:39:18 +02001162 STRCPY(newp + newlen + bd.endspaces,
1163 oldp + bd.textcol + bd.textlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001168 // Replacing with \r or \n means splitting the line.
John Marriott38b9f452024-04-25 21:39:18 +02001169 after_p = alloc(oldlen + 1 + n - newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001170 if (after_p != NULL)
John Marriott38b9f452024-04-25 21:39:18 +02001171 STRCPY(after_p, oldp + bd.textcol + bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
John Marriott38b9f452024-04-25 21:39:18 +02001173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001174 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001176 if (after_p != NULL)
1177 {
1178 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1179 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1180 oap->end.lnum++;
1181 vim_free(after_p);
1182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 }
1185 else
1186 {
1187 /*
1188 * MCHAR and MLINE motion replace.
1189 */
1190 if (oap->motion_type == MLINE)
1191 {
1192 oap->start.col = 0;
1193 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001194 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (oap->end.col)
1196 --oap->end.col;
1197 }
1198 else if (!oap->inclusive)
1199 dec(&(oap->end));
1200
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001201 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001203 int done = FALSE;
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 n = gchar_cursor();
1206 if (n != NUL)
1207 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001208 int new_byte_len = (*mb_char2len)(c);
1209 int old_byte_len = mb_ptr2len(ml_get_cursor());
1210
1211 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001213 // This is slow, but it handles replacing a single-byte
1214 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001215 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001216 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001217 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001218 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (n == TAB)
1223 {
1224 int end_vcol = 0;
1225
1226 if (curwin->w_cursor.lnum == oap->end.lnum)
1227 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001228 // oap->end has to be recalculated when
1229 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 end_vcol = getviscol2(oap->end.col,
1231 oap->end.coladd);
1232 }
1233 coladvance_force(getviscol());
1234 if (curwin->w_cursor.lnum == oap->end.lnum)
1235 getvpos(&oap->end, end_vcol);
1236 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001237 // with "coladd" set may move to just after a TAB
1238 if (gchar_cursor() != NUL)
1239 {
1240 PBYTE(curwin->w_cursor, c);
1241 done = TRUE;
1242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001245 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 int virtcols = oap->end.coladd;
1248
1249 if (curwin->w_cursor.lnum == oap->start.lnum
1250 && oap->start.col == oap->end.col && oap->start.coladd)
1251 virtcols -= oap->start.coladd;
1252
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001253 // oap->end has been trimmed so it's effectively inclusive;
1254 // as a result an extra +1 must be counted so we don't
1255 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1257 curwin->w_cursor.col -= (virtcols + 1);
1258 for (; virtcols >= 0; virtcols--)
1259 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001260 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001261 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001262 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001263 PBYTE(curwin->w_cursor, c);
Gary Johnson88d4f252024-06-01 20:51:33 +02001264 if (inc(&curwin->w_cursor) == -1)
1265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001269 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (inc_cursor() == -1)
1271 break;
1272 }
1273 }
1274
1275 curwin->w_cursor = oap->start;
1276 check_cursor();
1277 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1278
Bram Moolenaare1004402020-10-24 20:49:43 +02001279 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001280 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001281 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001282 curbuf->b_op_start = oap->start;
1283 curbuf->b_op_end = oap->end;
1284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
1286 return OK;
1287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001289static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291/*
1292 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1293 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001295op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001299 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
1301 if (u_save((linenr_T)(oap->start.lnum - 1),
1302 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1303 return;
1304
1305 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
1308 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1309 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001310 int one_change;
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 block_prep(oap, &bd, pos.lnum, FALSE);
1313 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001314 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1315 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001316
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001317#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001318 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001320 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaar009b2592004-10-24 19:18:58 +00001322 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1323 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001324 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001325 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001327 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331 if (did_change)
1332 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1333 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001334 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 if (oap->motion_type == MLINE)
1337 {
1338 oap->start.col = 0;
1339 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001340 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (oap->end.col)
1342 --oap->end.col;
1343 }
1344 else if (!oap->inclusive)
1345 dec(&(oap->end));
1346
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001347 if (pos.lnum == oap->end.lnum)
1348 did_change = swapchars(oap->op_type, &pos,
1349 oap->end.col - pos.col + 1);
1350 else
1351 for (;;)
1352 {
1353 did_change |= swapchars(oap->op_type, &pos,
zeertzjq94b7c322024-03-12 21:50:32 +01001354 pos.lnum == oap->end.lnum ? oap->end.col + 1
1355 : ml_get_pos_len(&pos));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001356 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001357 break;
1358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (did_change)
1360 {
1361 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1362 0L);
1363#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001364 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
1366 char_u *ptr;
1367 int count;
1368
1369 pos = oap->start;
1370 while (pos.lnum < oap->end.lnum)
1371 {
1372 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001373 count = ml_get_buf_len(curbuf, pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001374 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001375 // get the line again, it may have been flushed
1376 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001378 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 pos.col = 0;
1380 pos.lnum++;
1381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001383 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001384 // get the line again, it may have been flushed
1385 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001387 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389#endif
1390 }
1391 }
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001394 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001395 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaare1004402020-10-24 20:49:43 +02001397 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001398 {
1399 // Set '[ and '] marks.
1400 curbuf->b_op_start = oap->start;
1401 curbuf->b_op_end = oap->end;
1402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001405 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001406 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407}
1408
1409/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001410 * Invoke swapchar() on "length" bytes at position "pos".
1411 * "pos" is advanced to just after the changed characters.
1412 * "length" is rounded up to include the whole last multi-byte character.
1413 * Also works correctly when the number of bytes changes.
1414 * Returns TRUE if some character was changed.
1415 */
1416 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001417swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001418{
1419 int todo;
1420 int did_change = 0;
1421
1422 for (todo = length; todo > 0; --todo)
1423 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001424 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001425 {
1426 int len = (*mb_ptr2len)(ml_get_pos(pos));
1427
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001428 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001429 if (len > 0)
1430 todo -= len - 1;
1431 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001432 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001433 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001434 break;
1435 }
1436 return did_change;
1437}
1438
1439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 * If op_type == OP_UPPER: make uppercase,
1441 * if op_type == OP_LOWER: make lowercase,
1442 * if op_type == OP_ROT13: do rot13 encoding,
1443 * else swap case of character at 'pos'
1444 * returns TRUE when something actually changed.
1445 */
1446 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001447swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 int c;
1450 int nc;
1451
1452 c = gchar_pos(pos);
1453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001454 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (c >= 0x80 && op_type == OP_ROT13)
1456 return FALSE;
1457
glepnirbd1232a2024-02-12 22:14:53 +01001458 // ~ is OP_NOP, g~ is OP_TILDE, gU is OP_UPPER
1459 if ((op_type == OP_UPPER || op_type == OP_NOP || op_type == OP_TILDE)
1460 && c == 0xdf
1461 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001462 {
1463 pos_T sp = curwin->w_cursor;
1464
glepnirbd1232a2024-02-12 22:14:53 +01001465 // Special handling for lowercase German sharp s (ß): convert to uppercase (ẞ).
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001466 curwin->w_cursor = *pos;
1467 del_char(FALSE);
glepnirbd1232a2024-02-12 22:14:53 +01001468 ins_char(0x1E9E);
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001469 curwin->w_cursor = sp;
glepnirbd1232a2024-02-12 22:14:53 +01001470 return TRUE;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001471 }
1472
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001473 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 nc = c;
1476 if (MB_ISLOWER(c))
1477 {
1478 if (op_type == OP_ROT13)
1479 nc = ROT13(c, 'a');
1480 else if (op_type != OP_LOWER)
1481 nc = MB_TOUPPER(c);
1482 }
1483 else if (MB_ISUPPER(c))
1484 {
1485 if (op_type == OP_ROT13)
1486 nc = ROT13(c, 'A');
1487 else if (op_type != OP_UPPER)
1488 nc = MB_TOLOWER(c);
1489 }
1490 if (nc != c)
1491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1493 {
1494 pos_T sp = curwin->w_cursor;
1495
1496 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001497 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001498 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 ins_char(nc);
1500 curwin->w_cursor = sp;
1501 }
1502 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001503 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 return TRUE;
1505 }
1506 return FALSE;
1507}
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509/*
1510 * op_insert - Insert and append operators for Visual mode.
1511 */
1512 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001513op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
zeertzjq8c55d602024-03-13 20:42:26 +01001515 long pre_textlen = 0;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001516 colnr_T ind_pre_col = 0, ind_post_col;
1517 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 struct block_def bd;
1519 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001520 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001521 pos_T start_insert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001523 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1525
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001526 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001528 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
1530 if (oap->block_mode)
1531 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001532 // When 'virtualedit' is used, need to insert the extra spaces before
1533 // doing block_prep(). When only "block" is used, virtual edit is
1534 // already disabled, but still need it when calling
1535 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001536 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001537 // state for the current window. To override that state, we need to
1538 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (curwin->w_cursor.coladd > 0)
1540 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001541 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if (u_save_cursor() == FAIL)
1544 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001545
Gary Johnson51ad8502021-08-03 18:33:08 +02001546 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 coladvance_force(oap->op_type == OP_APPEND
1548 ? oap->end_vcol + 1 : getviscol());
1549 if (oap->op_type == OP_APPEND)
1550 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001551 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001553 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001555 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001556 ind_pre_col = (colnr_T)getwhitecols_curline();
1557 ind_pre_vcol = get_indent();
zeertzjq94b7c322024-03-12 21:50:32 +01001558 pre_textlen = ml_get_len(oap->start.lnum) - bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (oap->op_type == OP_APPEND)
zeertzjq94b7c322024-03-12 21:50:32 +01001560 pre_textlen -= bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562
1563 if (oap->op_type == OP_APPEND)
1564 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001565 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001567 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 curwin->w_set_curswant = TRUE;
1569 while (*ml_get_cursor() != NUL
1570 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1571 ++curwin->w_cursor.col;
1572 if (bd.is_short && !bd.is_MAX)
1573 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001574 // First line was too short, make it longer and adjust the
1575 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (u_save_cursor() == FAIL)
1577 return;
1578 for (i = 0; i < bd.endspaces; ++i)
1579 ins_char(' ');
1580 bd.textlen += bd.endspaces;
1581 }
1582 }
1583 else
1584 {
1585 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001586 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001588 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001589 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 && oap->start_vcol != oap->end_vcol)
1591 inc_cursor();
1592 }
1593 }
1594
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001595 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001596 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001597 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001599 // When a tab was inserted, and the characters in front of the tab
1600 // have been converted to a tab as well, the column of the cursor
1601 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001602 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001603 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001604 oap->start = curbuf->b_op_start_orig;
1605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001606 // If user has moved off this line, we don't know what to do, so do
1607 // nothing.
1608 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001609 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 return;
1611
1612 if (oap->block_mode)
1613 {
Christian Brabandtd5c8c092024-05-08 22:17:19 +02001614 int ins_len;
zeertzjq8c55d602024-03-13 20:42:26 +01001615 char_u *firstline, *ins_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001617 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001618 size_t len;
Mike Williamsaca8f552024-04-07 18:26:22 +02001619 size_t add;
zeertzjq8c55d602024-03-13 20:42:26 +01001620 // offset when cursor was moved in insert mode
1621 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001623 // If indent kicked in, the firstline might have changed
1624 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001625 ind_post_col = (colnr_T)getwhitecols_curline();
1626 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001627 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001628 bd.textcol += ind_post_col - ind_pre_col;
1629 ind_post_vcol = get_indent();
1630 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001631 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001632 }
1633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 // The user may have moved the cursor before inserting something, try
1635 // to adjust the block for that. But only do it, if the difference
1636 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001637 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1638 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001639 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001640 int t = getviscol2(curbuf->b_op_start_orig.col,
1641 curbuf->b_op_start_orig.coladd);
1642
zeertzjq7745f142022-02-15 11:48:22 +00001643 if (oap->op_type == OP_INSERT
1644 && oap->start.col + oap->start.coladd
1645 != curbuf->b_op_start_orig.col
1646 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001647 {
zeertzjq7745f142022-02-15 11:48:22 +00001648 oap->start.col = curbuf->b_op_start_orig.col;
1649 pre_textlen -= t - oap->start_vcol;
1650 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001651 }
zeertzjq7745f142022-02-15 11:48:22 +00001652 else if (oap->op_type == OP_APPEND
1653 && oap->start.col + oap->start.coladd
1654 >= curbuf->b_op_start_orig.col
1655 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001656 {
zeertzjq7745f142022-02-15 11:48:22 +00001657 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001658 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001659 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001660 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001661 oap->start_vcol = t;
1662 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001663 }
1664 }
1665
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001666 // Spaces and tabs in the indent may have changed to other spaces and
1667 // tabs. Get the starting column again and correct the length.
1668 // Don't do this when "$" used, end-of-line will have changed.
1669 //
1670 // if indent was added and the inserted text was after the indent,
1671 // correct the selection for the new indent.
1672 if (did_indent && bd.textcol - ind_post_col > 0)
1673 {
1674 oap->start.col += ind_post_col - ind_pre_col;
1675 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1676 oap->end.col += ind_post_col - ind_pre_col;
1677 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001680 if (did_indent && bd.textcol - ind_post_col > 0)
1681 {
1682 // undo for where "oap" is used below
1683 oap->start.col -= ind_post_col - ind_pre_col;
1684 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1685 oap->end.col -= ind_post_col - ind_pre_col;
1686 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1689 {
1690 if (oap->op_type == OP_APPEND)
1691 {
1692 pre_textlen += bd2.textlen - bd.textlen;
1693 if (bd2.endspaces)
1694 --bd2.textlen;
1695 }
1696 bd.textcol = bd2.textcol;
1697 bd.textlen = bd2.textlen;
1698 }
1699
1700 /*
1701 * Subsequent calls to ml_get() flush the firstline data - take a
1702 * copy of the required string.
1703 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001704 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001705 len = ml_get_len(oap->start.lnum);
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001706 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001708 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001709 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001710 // account for pressing cursor in insert mode when '$' was used
1711 if (bd.is_MAX
1712 && (start_insert.lnum == Insstart.lnum
1713 && start_insert.col > Insstart.col))
1714 {
1715 offset = (start_insert.col - Insstart.col);
1716 add -= offset;
1717 if (oap->end_vcol > offset)
1718 oap->end_vcol -= (offset + 1);
1719 else
1720 // moved outside of the visual block, what to do?
1721 return;
1722 }
1723 }
Mike Williamsaca8f552024-04-07 18:26:22 +02001724 if (add > len)
zeertzjq94b7c322024-03-12 21:50:32 +01001725 add = len; // short line, point to the NUL
1726 firstline += add;
1727 len -= add;
Mike Williams16b63bd2024-06-01 11:33:40 +02001728 if (pre_textlen >= 0 && (ins_len = (int)len - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001730 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (ins_text != NULL)
1732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001733 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (u_save(oap->start.lnum,
1735 (linenr_T)(oap->end.lnum + 1)) == OK)
John Marriott38b9f452024-04-25 21:39:18 +02001736 block_insert(oap, ins_text, ins_len, (oap->op_type == OP_INSERT),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 &bd);
1738
1739 curwin->w_cursor.col = oap->start.col;
1740 check_cursor();
1741 vim_free(ins_text);
1742 }
1743 }
1744 }
1745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
1747/*
1748 * op_change - handle a change operation
1749 *
1750 * return TRUE if edit() returns because of a CTRL-O command
1751 */
1752 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001753op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 colnr_T l;
1756 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001758 long pre_textlen = 0;
1759 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 char_u *firstline;
1761 char_u *ins_text, *newp, *oldp;
1762 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764 l = oap->start.col;
1765 if (oap->motion_type == MLINE)
1766 {
1767 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001768 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001771 // First delete the text in the region. In an empty buffer only need to
1772 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1774 {
1775 if (u_save_cursor() == FAIL)
1776 return FALSE;
1777 }
1778 else if (op_delete(oap) == FAIL)
1779 return FALSE;
1780
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001781 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 && !virtual_op)
1783 inc_cursor();
1784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001785 // check for still on same line (<CR> in inserted text meaningless)
1786 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (oap->block_mode)
1788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001789 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (virtual_op && (curwin->w_cursor.coladd > 0
1791 || gchar_cursor() == NUL))
1792 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001793 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001794 pre_textlen = ml_get_len(oap->start.lnum);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001795 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 bd.textcol = curwin->w_cursor.col;
1797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (oap->motion_type == MLINE)
1800 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
zeertzjqcdeb6572022-11-15 13:46:12 +00001802 // Reset finish_op now, don't want it set inside edit().
1803 int save_finish_op = finish_op;
1804 finish_op = FALSE;
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 retval = edit(NUL, FALSE, (linenr_T)1);
1807
zeertzjqcdeb6572022-11-15 13:46:12 +00001808 finish_op = save_finish_op;
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001811 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001813 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001815 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Christian Brabandt1fb9eae2024-06-11 20:30:14 +02001817 int ins_len;
John Marriott38b9f452024-04-25 21:39:18 +02001818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001819 // Auto-indenting may have changed the indent. If the cursor was past
1820 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001822 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001824 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001825
1826 pre_textlen += new_indent - pre_indent;
1827 bd.textcol += new_indent - pre_indent;
1828 }
1829
Christian Brabandt1fb9eae2024-06-11 20:30:14 +02001830 ins_len = (int)ml_get_len(oap->start.lnum) - pre_textlen;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001831 if (ins_len > 0)
1832 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001833 // Subsequent calls to ml_get() flush the firstline data - take a
1834 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001835 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
John Marriott38b9f452024-04-25 21:39:18 +02001837 vim_strncpy(ins_text, firstline + bd.textcol, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1839 linenr++)
1840 {
1841 block_prep(oap, &bd, linenr, TRUE);
1842 if (!bd.is_short || virtual_op)
1843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 pos_T vpos;
John Marriott38b9f452024-04-25 21:39:18 +02001845 size_t newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001847 // If the block starts in virtual space, count the
1848 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (bd.is_short)
1850 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001851 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854 else
1855 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 oldp = ml_get(linenr);
John Marriott38b9f452024-04-25 21:39:18 +02001857 newp = alloc(ml_get_len(linenr) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (newp == NULL)
1859 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001860 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001862 newlen = bd.textcol;
1863 vim_memset(newp + newlen, ' ', (size_t)vpos.coladd);
1864 newlen += vpos.coladd;
1865 mch_memmove(newp + newlen, ins_text, ins_len);
1866 STRCPY(newp + newlen + ins_len, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001868#ifdef FEAT_PROP_POPUP
1869 // Shift the properties for linenr as edit() would do.
1870 if (curbuf->b_has_textprop)
1871 adjust_prop_columns(linenr, bd.textcol,
Mike Williams16b63bd2024-06-01 11:33:40 +02001872 vpos.coladd + (int)ins_len, 0);
LemonBoyb559b302022-05-15 13:08:02 +01001873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875 }
1876 check_cursor();
1877
1878 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1879 }
1880 vim_free(ins_text);
1881 }
1882 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001883 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 return retval;
1886}
1887
1888/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001889 * When the cursor is on the NUL past the end of the line and it should not be
1890 * there move it left.
1891 */
1892 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001893adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001894{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001895 unsigned int cur_ve_flags = get_ve_flags();
1896
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001897 int adj_cursor = (curwin->w_cursor.col > 0
1898 && gchar_cursor() == NUL
1899 && (cur_ve_flags & VE_ONEMORE) == 0
1900 && !(restart_edit || (State & MODE_INSERT)));
1901 if (!adj_cursor)
1902 return;
1903
1904 // Put the cursor on the last character in the line.
1905 dec_cursor();
1906
1907 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001909 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001910
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001911 // Coladd is set to the width of the last character.
1912 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1913 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915}
1916
Bram Moolenaar81340392012-06-06 16:12:59 +02001917/*
1918 * If "process" is TRUE and the line begins with a comment leader (possibly
1919 * after some white space), return a pointer to the text after it. Put a boolean
1920 * value indicating whether the line ends with an unclosed comment in
1921 * "is_comment".
1922 * line - line to be processed,
1923 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001924 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 * include_space - whether to also skip space following the comment leader,
1926 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001927 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001928 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001929 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001930skip_comment(
1931 char_u *line,
1932 int process,
1933 int include_space,
1934 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001935{
1936 char_u *comment_flags = NULL;
1937 int lead_len;
1938 int leader_offset = get_last_leader_offset(line, &comment_flags);
1939
1940 *is_comment = FALSE;
1941 if (leader_offset != -1)
1942 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001943 // Let's check whether the line ends with an unclosed comment.
1944 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001945 while (*comment_flags)
1946 {
1947 if (*comment_flags == COM_END
1948 || *comment_flags == ':')
1949 break;
1950 ++comment_flags;
1951 }
1952 if (*comment_flags != COM_END)
1953 *is_comment = TRUE;
1954 }
1955
1956 if (process == FALSE)
1957 return line;
1958
1959 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1960
1961 if (lead_len == 0)
1962 return line;
1963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 // Find:
1965 // - COM_END,
1966 // - colon,
1967 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001968 while (*comment_flags)
1969 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001970 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001971 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001972 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001973 ++comment_flags;
1974 }
1975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001976 // If we found a colon, it means that we are not processing a line
1977 // starting with a closing part of a three-part comment. That's good,
1978 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001979 if (*comment_flags == ':' || *comment_flags == NUL)
1980 line += lead_len;
1981
1982 return line;
1983}
Bram Moolenaar81340392012-06-06 16:12:59 +02001984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001986 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001987 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001988 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1989 * leaders should not be removed.
1990 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1991 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001993 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 */
1995 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001996do_join(
1997 long count,
1998 int insert_space,
1999 int save_undo,
2000 int use_formatoptions UNUSED,
2001 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002003 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002004 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002005 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002007 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002008 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02002009 int endcurr1 = NUL;
2010 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 int currsize = 0; // size of the current line
2012 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002014 colnr_T col = 0;
2015 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02002016 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002017 int remove_comments = (use_formatoptions == TRUE)
2018 && has_format_option(FO_REMOVE_COMS);
2019 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002020#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002021 int propcount = 0; // number of props over all joined lines
2022 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002025 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2026 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2027 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // Allocate an array to store the number of spaces inserted before each
2030 // line. We will use it to pre-compute the length of the new line and the
2031 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002032 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002033 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002035 if (remove_comments)
2036 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002037 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002038 if (comments == NULL)
2039 {
2040 vim_free(spaces);
2041 return FAIL;
2042 }
2043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002046 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002047 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002048 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002050 for (t = 0; t < count; ++t)
2051 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002052 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002053#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002054 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2055 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002056#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002057 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002058 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002059 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002060 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2061 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2062 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002063 if (remove_comments)
2064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002065 // We don't want to remove the comment leader if the
2066 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002067 if (t > 0 && prev_was_comment)
2068 {
2069
2070 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2071 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002072 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002073 curr = new_curr;
2074 }
2075 else
2076 curr = skip_comment(curr, FALSE, insert_space,
2077 &prev_was_comment);
2078 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002079
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002080 if (insert_space && t > 0)
2081 {
2082 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002083 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002084 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002085 && (!has_format_option(FO_MBYTE_JOIN)
2086 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2087 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002088 || (mb_ptr2char(curr) < 0x100
2089 && !(enc_utf8 && utf_eat_space(endcurr1)))
2090 || (endcurr1 < 0x100
2091 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002092 )
2093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002094 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002095 if (endcurr1 == ' ')
2096 endcurr1 = endcurr2;
2097 else
2098 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002099 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002100 if ( p_js
2101 && (endcurr1 == '.'
2102 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2103 && (endcurr1 == '?' || endcurr1 == '!'))))
2104 ++spaces[t];
2105 }
2106 }
2107 currsize = (int)STRLEN(curr);
2108 sumsize += currsize + spaces[t];
2109 endcurr1 = endcurr2 = NUL;
2110 if (insert_space && currsize > 0)
2111 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002112 if (has_mbyte)
2113 {
2114 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002115 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002116 endcurr1 = (*mb_ptr2char)(cend);
2117 if (cend > curr)
2118 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002119 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002120 endcurr2 = (*mb_ptr2char)(cend);
2121 }
2122 }
2123 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002124 {
2125 endcurr1 = *(curr + currsize - 1);
2126 if (currsize > 1)
2127 endcurr2 = *(curr + currsize - 2);
2128 }
2129 }
2130 line_breakcheck();
2131 if (got_int)
2132 {
2133 ret = FAIL;
2134 goto theend;
2135 }
2136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002138 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002139 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002142 newp_len = sumsize + 1;
2143#ifdef FEAT_PROP_POPUP
2144 newp_len += propcount * sizeof(textprop_T);
2145#endif
2146 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002147 if (newp == NULL)
2148 {
2149 ret = FAIL;
2150 goto theend;
2151 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002152 cend = newp + sumsize;
2153 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002155 /*
2156 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002157 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002158 *
2159 * Move marks from each deleted line to the joined line, adjusting the
2160 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2161 * should not really be a problem.
2162 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002163#ifdef FEAT_PROP_POPUP
2164 props_remaining = propcount;
2165#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002166 for (t = count - 1; ; --t)
2167 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002168 int spaces_removed;
2169
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 cend -= currsize;
2171 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002172
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002173 if (spaces[t] > 0)
2174 {
2175 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002176 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002177 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002178
2179 // If deleting more spaces than adding, the cursor moves no more than
2180 // what is added if it is inside these spaces.
2181 spaces_removed = (curr - curr_start) - spaces[t];
2182
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002183 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002184 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002185#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002186 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2187 curwin->w_cursor.lnum + t, t == count - 1,
2188 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002189#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002190 if (t == 0)
2191 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002192 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002193 if (remove_comments)
2194 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002195 if (insert_space && t > 1)
2196 curr = skipwhite(curr);
2197 currsize = (int)STRLEN(curr);
2198 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002199
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002200 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
Bram Moolenaare1004402020-10-24 20:49:43 +02002202 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002203 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002205 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002206 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002207 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 // Only report the change in the first line here, del_lines() will report
2210 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 changed_lines(curwin->w_cursor.lnum, currsize,
2212 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002214 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 * briefly, and then move it back. After del_lines() the cursor may
2216 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 */
2218 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002220 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 curwin->w_cursor.lnum = t;
2222
2223 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002224 * Set the cursor column:
2225 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002226 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002228 curwin->w_cursor.col =
2229 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 curwin->w_set_curswant = TRUE;
2234
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002235theend:
2236 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002237 if (remove_comments)
2238 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002239 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240}
2241
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002242#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002244 * Reset 'linebreak' and take care of side effects.
2245 * Returns the previous value, to be passed to restore_lbr().
2246 */
2247 static int
2248reset_lbr(void)
2249{
2250 if (!curwin->w_p_lbr)
2251 return FALSE;
2252 // changing 'linebreak' may require w_virtcol to be updated
2253 curwin->w_p_lbr = FALSE;
2254 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2255 return TRUE;
2256}
2257
2258/*
2259 * Restore 'linebreak' and take care of side effects.
2260 */
2261 static void
2262restore_lbr(int lbr_saved)
2263{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002264 if (curwin->w_p_lbr || !lbr_saved)
2265 return;
2266
2267 // changing 'linebreak' may require w_virtcol to be updated
2268 curwin->w_p_lbr = TRUE;
2269 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002270}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002271#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002272
2273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 * prepare a few things for block mode yank/delete/tilde
2275 *
2276 * for delete:
2277 * - textlen includes the first/last char to be (partly) deleted
2278 * - start/endspaces is the number of columns that are taken by the
2279 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002280 * deleted.
2281 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * - textlen includes the first/last char to be wholly yanked
2283 * - start/endspaces is the number of columns of the first/last yanked char
2284 * that are to be yanked.
2285 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287block_prep(
2288 oparg_T *oap,
2289 struct block_def *bdp,
2290 linenr_T lnum,
2291 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 int incr = 0;
2294 char_u *pend;
2295 char_u *pstart;
2296 char_u *line;
2297 char_u *prev_pstart;
2298 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002299 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002300#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002301 // Avoid a problem with unwanted linebreaks in block mode.
2302 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002303#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 bdp->startspaces = 0;
2306 bdp->endspaces = 0;
2307 bdp->textlen = 0;
2308 bdp->start_vcol = 0;
2309 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 bdp->is_short = FALSE;
2311 bdp->is_oneChar = FALSE;
2312 bdp->pre_whitesp = 0;
2313 bdp->pre_whitesp_c = 0;
2314 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 bdp->start_char_vcols = 0;
2316
2317 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002319 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2320 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002323 incr = lbr_chartabsize(&cts);
2324 cts.cts_vcol += incr;
2325 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 bdp->pre_whitesp += incr;
2328 bdp->pre_whitesp_c++;
2329 }
2330 else
2331 {
2332 bdp->pre_whitesp = 0;
2333 bdp->pre_whitesp_c = 0;
2334 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002335 prev_pstart = cts.cts_ptr;
2336 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002338 bdp->start_vcol = cts.cts_vcol;
2339 pstart = cts.cts_ptr;
2340 clear_chartabsize_arg(&cts);
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (!is_del || oap->op_type == OP_APPEND)
2348 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2349 }
2350 else
2351 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002352 // notice: this converts partly selected Multibyte characters to
2353 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2355 if (is_del && bdp->startspaces)
2356 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2357 pend = pstart;
2358 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002359 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (oap->op_type == OP_INSERT)
2363 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2364 else if (oap->op_type == OP_APPEND)
2365 {
2366 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2367 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2368 }
2369 else
2370 {
2371 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2372 if (is_del && oap->op_type != OP_LSHIFT)
2373 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002374 // just putting the sum of those two into
2375 // bdp->startspaces doesn't work for Visual replace,
2376 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 bdp->startspaces = bdp->start_char_vcols
2378 - (bdp->start_vcol - oap->start_vcol);
2379 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2380 }
2381 }
2382 }
2383 else
2384 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002385 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2386 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002388 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002390 // count a tab for what it's worth (if list mode not on)
2391 prev_pend = cts.cts_ptr;
2392 incr = lbr_chartabsize_adv(&cts);
2393 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002395 bdp->end_vcol = cts.cts_vcol;
2396 pend = cts.cts_ptr;
2397 clear_chartabsize_arg(&cts);
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (bdp->end_vcol <= oap->end_vcol
2400 && (!is_del
2401 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002405 // Alternative: include spaces to fill up the block.
2406 // Disadvantage: can lead to trailing spaces when the line is
2407 // short where the text is put
2408 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (oap->op_type == OP_APPEND || virtual_op)
2410 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002411 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002413 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415 else if (bdp->end_vcol > oap->end_vcol)
2416 {
2417 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2418 if (!is_del && bdp->endspaces)
2419 {
2420 bdp->endspaces = incr - bdp->endspaces;
2421 if (pend != pstart)
2422 pend = prev_pend;
2423 }
2424 }
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (is_del && bdp->startspaces)
2428 pstart = prev_pstart;
2429 bdp->textlen = (int)(pend - pstart);
2430 }
2431 bdp->textcol = (colnr_T) (pstart - line);
2432 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002433#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002434 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438/*
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002439 * Get block text from "start" to "end"
2440 */
2441 void
2442charwise_block_prep(
2443 pos_T start,
2444 pos_T end,
2445 struct block_def *bdp,
2446 linenr_T lnum,
2447 int inclusive)
2448{
2449 colnr_T startcol = 0, endcol = MAXCOL;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002450 colnr_T cs, ce;
2451 char_u *p;
2452
2453 p = ml_get(lnum);
2454 bdp->startspaces = 0;
2455 bdp->endspaces = 0;
zeertzjq52a6f342024-05-22 16:42:44 +02002456 bdp->is_oneChar = FALSE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002457 bdp->start_char_vcols = 0;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002458
2459 if (lnum == start.lnum)
2460 {
2461 startcol = start.col;
2462 if (virtual_op)
2463 {
2464 getvcol(curwin, &start, &cs, NULL, &ce);
2465 if (ce != cs && start.coladd > 0)
2466 {
2467 // Part of a tab selected -- but don't
2468 // double-count it.
zeertzjqc95e64f2024-05-20 14:00:31 +02002469 bdp->start_char_vcols = ce - cs + 1;
2470 bdp->startspaces = bdp->start_char_vcols - start.coladd;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002471 if (bdp->startspaces < 0)
2472 bdp->startspaces = 0;
2473 startcol++;
2474 }
2475 }
2476 }
2477
2478 if (lnum == end.lnum)
2479 {
2480 endcol = end.col;
2481 if (virtual_op)
2482 {
2483 getvcol(curwin, &end, &cs, NULL, &ce);
2484 if (p[endcol] == NUL || (cs + end.coladd < ce
2485 // Don't add space for double-wide
2486 // char; endcol will be on last byte
2487 // of multi-byte char.
2488 && (*mb_head_off)(p, p + endcol) == 0))
2489 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002490 if (start.lnum == end.lnum && start.col == end.col)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002491 {
2492 // Special case: inside a single char
zeertzjq52a6f342024-05-22 16:42:44 +02002493 bdp->is_oneChar = TRUE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002494 bdp->startspaces = end.coladd - start.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002495 endcol = startcol;
2496 }
2497 else
2498 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002499 bdp->endspaces = end.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002500 endcol -= inclusive;
2501 }
2502 }
2503 }
2504 }
2505 if (endcol == MAXCOL)
zeertzjq94b7c322024-03-12 21:50:32 +01002506 endcol = ml_get_len(lnum);
zeertzjq52a6f342024-05-22 16:42:44 +02002507 if (startcol > endcol || bdp->is_oneChar)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002508 bdp->textlen = 0;
2509 else
2510 bdp->textlen = endcol - startcol + inclusive;
zeertzjqc95e64f2024-05-20 14:00:31 +02002511 bdp->textcol = startcol;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002512 bdp->textstart = p + startcol;
2513}
2514
2515/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002516 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002519op_addsub(
2520 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002521 linenr_T Prenum1, // Amount of add/subtract
2522 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002524 pos_T pos;
2525 struct block_def bd;
2526 int change_cnt = 0;
2527 linenr_T amount = Prenum1;
2528
Gary Johnson88d4f252024-06-01 20:51:33 +02002529 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
2530 // buffer is not completely updated yet. Postpone updating folds until before
2531 // the call to changed_lines().
Bram Moolenaar6b731882018-11-16 20:54:47 +01002532#ifdef FEAT_FOLDING
Gary Johnson88d4f252024-06-01 20:51:33 +02002533 disable_fold_update++;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002534#endif
2535
Bram Moolenaard79e5502016-01-10 22:13:02 +01002536 if (!VIsual_active)
2537 {
2538 pos = curwin->w_cursor;
2539 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002540 {
2541#ifdef FEAT_FOLDING
2542 disable_fold_update--;
2543#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002544 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002545 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002546 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002547#ifdef FEAT_FOLDING
2548 disable_fold_update--;
2549#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002550 if (change_cnt)
2551 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2552 }
2553 else
2554 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002555 int one_change;
2556 int length;
2557 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002558
2559 if (u_save((linenr_T)(oap->start.lnum - 1),
2560 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002561 {
2562#ifdef FEAT_FOLDING
2563 disable_fold_update--;
2564#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002565 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002566 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002567
2568 pos = oap->start;
2569 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2570 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002571 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002572 {
2573 block_prep(oap, &bd, pos.lnum, FALSE);
2574 pos.col = bd.textcol;
2575 length = bd.textlen;
2576 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002577 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002578 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002579 curwin->w_cursor.col = 0;
2580 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01002581 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002582 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002583 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002584 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002585 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002586 dec(&(oap->end));
zeertzjq94b7c322024-03-12 21:50:32 +01002587 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002588 pos.col = 0;
2589 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002590 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002591 pos.col += oap->start.col;
2592 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002593 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002594 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002595 {
zeertzjq94b7c322024-03-12 21:50:32 +01002596 length = ml_get_len(oap->end.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002597 if (oap->end.col >= length)
2598 oap->end.col = length - 1;
2599 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002600 }
2601 }
2602 one_change = do_addsub(oap->op_type, &pos, length, amount);
2603 if (one_change)
2604 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002605 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002606 if (change_cnt == 0)
2607 startpos = curbuf->b_op_start;
2608 ++change_cnt;
2609 }
2610
2611#ifdef FEAT_NETBEANS_INTG
2612 if (netbeans_active() && one_change)
2613 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002614 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002615
2616 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002617 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002618 netbeans_inserted(curbuf, pos.lnum, pos.col,
2619 &ptr[pos.col], length);
2620 }
2621#endif
2622 if (g_cmd && one_change)
2623 amount += Prenum1;
2624 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002625
2626#ifdef FEAT_FOLDING
2627 disable_fold_update--;
2628#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002629 if (change_cnt)
2630 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2631
2632 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002633 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002634 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 // Set '[ mark if something changed. Keep the last end
2637 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002638 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002639 curbuf->b_op_start = startpos;
2640
2641 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002642 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002643 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002644 }
2645}
2646
2647/*
2648 * Add or subtract 'Prenum1' from a number in a line
2649 * op_type is OP_NR_ADD or OP_NR_SUB
2650 *
2651 * Returns TRUE if some character was changed.
2652 */
2653 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002654do_addsub(
2655 int op_type,
2656 pos_T *pos,
2657 int length,
2658 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002659{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int col;
2661 char_u *buf1;
2662 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002663 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2664 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002665 uvarnumber_T n;
2666 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 char_u *ptr;
zeertzjq8c55d602024-03-13 20:42:26 +01002668 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002671 int do_hex;
2672 int do_oct;
2673 int do_bin;
2674 int do_alpha;
2675 int do_unsigned;
distobs25ac6d62024-07-06 17:50:09 +02002676 int do_blank;
2677 int blank_unsigned = FALSE; // blank: treat as unsigned?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002680 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002681 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002682 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002683 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002684 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002685 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002686 pos_T startpos;
2687 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002688 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002690 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2691 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2692 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2693 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2694 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
distobs25ac6d62024-07-06 17:50:09 +02002695 do_blank = (vim_strchr(curbuf->b_p_nf, 'k') != NULL); // "blanK"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002697 if (virtual_active())
2698 {
2699 save_coladd = pos->coladd;
2700 pos->coladd = 0;
2701 }
2702
Bram Moolenaard79e5502016-01-10 22:13:02 +01002703 curwin->w_cursor = *pos;
2704 ptr = ml_get(pos->lnum);
zeertzjq8c55d602024-03-13 20:42:26 +01002705 linelen = ml_get_len(pos->lnum);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002706 col = pos->col;
2707
zeertzjq8c55d602024-03-13 20:42:26 +01002708 if (col + !!save_coladd >= linelen)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002709 goto theend;
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 /*
2712 * First check if we are on a hexadecimal number, after the "0x".
2713 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002714 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002716 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002717 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002718 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002719 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002720 if (has_mbyte)
2721 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002722 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002723
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002724 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002725 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002726 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002727 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002728 if (has_mbyte)
2729 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002730 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002731
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002732 if ( do_bin
2733 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002734 && ! ((col > 0
2735 && (ptr[col] == 'X'
2736 || ptr[col] == 'x')
2737 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002738 && (!has_mbyte ||
2739 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002740 && vim_isxdigit(ptr[col + 1]))))
2741 {
2742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002744
Bram Moolenaard79e5502016-01-10 22:13:02 +01002745 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002746
2747 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002748 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002749 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002750 if (has_mbyte)
2751 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002752 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002753 }
2754
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002755 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002756 && col > 0
2757 && (ptr[col] == 'X'
2758 || ptr[col] == 'x')
2759 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002760 && (!has_mbyte ||
2761 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002762 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002763 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002764 && col > 0
2765 && (ptr[col] == 'B'
2766 || ptr[col] == 'b')
2767 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002768 && (!has_mbyte ||
2769 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002770 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002773 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002774 if (has_mbyte)
2775 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002776 }
2777 else
2778 {
2779 /*
2780 * Search forward and then backward to find the start of number.
2781 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002782 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002783
2784 while (ptr[col] != NUL
2785 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002786 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002787 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002788
2789 while (col > 0
2790 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002791 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002792 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002793 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002794 if (has_mbyte)
2795 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002796 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002797 }
2798 }
2799
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002800 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002801 {
2802 while (ptr[col] != NUL && length > 0
2803 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002804 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002805 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002806 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002807
2808 col += mb_len;
2809 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002810 }
2811
2812 if (length == 0)
2813 goto theend;
2814
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002815 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002816 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2817 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002818 {
distobs25ac6d62024-07-06 17:50:09 +02002819 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
2820 blank_unsigned = TRUE;
2821 else
2822 {
2823 negative = TRUE;
2824 was_positive = FALSE;
2825 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002826 }
2827 }
2828
2829 /*
2830 * If a number was found, and saving for undo works, replace the number.
2831 */
2832 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002833 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002834 {
2835 beep_flush();
2836 goto theend;
2837 }
2838
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002839 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002842 if (op_type == OP_NR_SUB)
2843 {
2844 if (CharOrd(firstdigit) < Prenum1)
2845 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002846 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002847 firstdigit = 'A';
2848 else
2849 firstdigit = 'a';
2850 }
2851 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002852 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002853 }
2854 else
2855 {
2856 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2857 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002858 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002859 firstdigit = 'Z';
2860 else
2861 firstdigit = 'z';
2862 }
2863 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002864 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002865 }
2866 curwin->w_cursor.col = col;
2867 if (!did_change)
2868 startpos = curwin->w_cursor;
2869 did_change = TRUE;
2870 (void)del_char(FALSE);
2871 ins_char(firstdigit);
2872 endpos = curwin->w_cursor;
2873 curwin->w_cursor.col = col;
2874 }
2875 else
2876 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002877 pos_T save_pos;
2878 int i;
2879
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002880 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002881 && (!has_mbyte ||
2882 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002883 && !visual
2884 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002885 {
distobs25ac6d62024-07-06 17:50:09 +02002886 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
2887 blank_unsigned = TRUE;
2888 else
2889 {
2890 // negative number
2891 --col;
2892 negative = TRUE;
2893 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002894 }
distobs25ac6d62024-07-06 17:50:09 +02002895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002897 if (visual && VIsual_mode != 'V')
2898 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
zeertzjq8c55d602024-03-13 20:42:26 +01002899 ? linelen - col : length);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002900
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002901 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002902 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002903 0 + (do_bin ? STR2NR_BIN : 0)
2904 + (do_oct ? STR2NR_OCT : 0)
2905 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002906 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002909 if (pre && negative)
2910 {
2911 ++col;
2912 --length;
2913 negative = FALSE;
2914 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002916 subtract = FALSE;
2917 if (op_type == OP_NR_SUB)
2918 subtract ^= TRUE;
2919 if (negative)
2920 subtract ^= TRUE;
2921
2922 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002923 if (!overflow) // if number is too big don't add/subtract
2924 {
2925 if (subtract)
2926 n -= (uvarnumber_T)Prenum1;
2927 else
2928 n += (uvarnumber_T)Prenum1;
2929 }
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002932 if (!pre)
2933 {
2934 if (subtract)
2935 {
2936 if (n > oldn)
2937 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002938 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002939 negative ^= TRUE;
2940 }
2941 }
2942 else
2943 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002945 if (n < oldn)
2946 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002947 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002948 negative ^= TRUE;
2949 }
2950 }
2951 if (n == 0)
2952 negative = FALSE;
2953 }
2954
distobs25ac6d62024-07-06 17:50:09 +02002955 if ((do_unsigned || blank_unsigned) && negative)
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002956 {
2957 if (subtract)
2958 // sticking at zero.
2959 n = (uvarnumber_T)0;
2960 else
2961 // sticking at 2^64 - 1.
2962 n = (uvarnumber_T)(-1);
2963 negative = FALSE;
2964 }
2965
Bram Moolenaard79e5502016-01-10 22:13:02 +01002966 if (visual && !was_positive && !negative && col > 0)
2967 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002969 col--;
2970 length++;
2971 }
2972
2973 /*
2974 * Delete the old number.
2975 */
2976 curwin->w_cursor.col = col;
2977 if (!did_change)
2978 startpos = curwin->w_cursor;
2979 did_change = TRUE;
2980 todel = length;
2981 c = gchar_cursor();
2982 /*
2983 * Don't include the '-' in the length, only the length of the
2984 * part after it is kept the same.
2985 */
2986 if (c == '-')
2987 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002988
2989 save_pos = curwin->w_cursor;
2990 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002991 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002992 if (c < 0x100 && SAFE_isalpha(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002993 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002994 if (SAFE_isupper(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002995 hexupper = TRUE;
2996 else
2997 hexupper = FALSE;
2998 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002999 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01003000 c = gchar_cursor();
3001 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003002 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003003
3004 /*
3005 * Prepare the leading characters in buf1[].
3006 * When there are many leading zeros it could be very long.
3007 * Allocate a bit too much.
3008 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003009 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003010 if (buf1 == NULL)
3011 goto theend;
3012 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003013 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003014 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003015 if (pre)
3016 {
3017 *ptr++ = '0';
3018 --length;
3019 }
3020 if (pre == 'b' || pre == 'B' ||
3021 pre == 'x' || pre == 'X')
3022 {
3023 *ptr++ = pre;
3024 --length;
3025 }
3026
3027 /*
3028 * Put the number characters in buf2[].
3029 */
3030 if (pre == 'b' || pre == 'B')
3031 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01003032 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003033 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003035 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003036 for (bit = bits; bit > 0; bit--)
3037 if ((n >> (bit - 1)) & 0x1) break;
3038
Christian Brabandt889f6af2023-09-02 19:43:33 +02003039 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003040 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3041
3042 buf2[i] = '\0';
3043 }
3044 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003045 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003046 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003047 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003048 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003049 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003050 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003051 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003052 length -= (int)STRLEN(buf2);
3053
3054 /*
3055 * Adjust number of zeros to the new number of digits, so the
3056 * total length of the number remains the same.
3057 * Don't do this when
3058 * the result may look like an octal number.
3059 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003060 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003061 while (length-- > 0)
3062 *ptr++ = '0';
3063 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003064
Bram Moolenaard79e5502016-01-10 22:13:02 +01003065 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003066
3067 // Insert just after the first character to be removed, so that any
3068 // text properties will be adjusted. Then delete the old number
3069 // afterwards.
3070 save_pos = curwin->w_cursor;
3071 if (todel > 0)
3072 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003073 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003074 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003075
3076 // del_char() will also mark line needing displaying
3077 if (todel > 0)
3078 {
zeertzjq94b7c322024-03-12 21:50:32 +01003079 int bytes_after = ml_get_curline_len() - curwin->w_cursor.col;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003080
3081 // Delete the one character before the insert.
3082 curwin->w_cursor = save_pos;
3083 (void)del_char(FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01003084 curwin->w_cursor.col = ml_get_curline_len() - bytes_after;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003085 --todel;
3086 }
3087 while (todel-- > 0)
3088 (void)del_char(FALSE);
3089
Bram Moolenaard79e5502016-01-10 22:13:02 +01003090 endpos = curwin->w_cursor;
3091 if (did_change && curwin->w_cursor.col)
3092 --curwin->w_cursor.col;
3093 }
3094
Bram Moolenaare1004402020-10-24 20:49:43 +02003095 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003096 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003098 curbuf->b_op_start = startpos;
3099 curbuf->b_op_end = endpos;
3100 if (curbuf->b_op_end.col > 0)
3101 --curbuf->b_op_end.col;
3102 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003103
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003104theend:
3105 if (visual)
3106 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003107 else if (did_change)
3108 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003109 else if (virtual_active())
3110 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003111
Bram Moolenaard79e5502016-01-10 22:13:02 +01003112 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113}
3114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003116clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003118 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119}
3120
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003122 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 *
3124 * "Words" are counted by looking for boundaries between non-space and
3125 * space characters. (it seems to produce results that match 'wc'.)
3126 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003127 * Return value is byte count; word count for the line is added to "*wc".
3128 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 *
3130 * The function will only examine the first "limit" characters in the
3131 * line, stopping if it encounters an end-of-line (NUL byte). In that
3132 * case, eol_size will be added to the character count to account for
3133 * the size of the EOL character.
3134 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003135 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003136line_count_info(
3137 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003138 varnumber_T *wc,
3139 varnumber_T *cc,
3140 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003141 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003143 varnumber_T i;
3144 varnumber_T words = 0;
3145 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 int is_word = 0;
3147
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003148 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
3150 if (is_word)
3151 {
3152 if (vim_isspace(line[i]))
3153 {
3154 words++;
3155 is_word = 0;
3156 }
3157 }
3158 else if (!vim_isspace(line[i]))
3159 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003160 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003161 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163
3164 if (is_word)
3165 words++;
3166 *wc += words;
3167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003169 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003172 chars += eol_size;
3173 }
3174 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 return i;
3176}
3177
3178/*
3179 * Give some info about the position of the cursor (for "g CTRL-G").
3180 * In Visual mode, give some info about the selected region. (In this case,
3181 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003182 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 */
3184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003185cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003188 char_u buf1[50];
3189 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003191 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003192 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003193 varnumber_T byte_count_cursor = 0;
3194 varnumber_T char_count = 0;
3195 varnumber_T char_count_cursor = 0;
3196 varnumber_T word_count = 0;
3197 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003198 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003199 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 long line_count_selected = 0;
3201 pos_T min_pos, max_pos;
3202 oparg_T oparg;
3203 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 /*
3206 * Compute the length of the file in characters.
3207 */
3208 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3209 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003210 if (dict == NULL)
3211 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003212 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003213 return;
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216 else
3217 {
3218 if (get_fileformat(curbuf) == EOL_DOS)
3219 eol_size = 2;
3220 else
3221 eol_size = 1;
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (VIsual_active)
3224 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003225 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 min_pos = VIsual;
3228 max_pos = curwin->w_cursor;
3229 }
3230 else
3231 {
3232 min_pos = curwin->w_cursor;
3233 max_pos = VIsual;
3234 }
3235 if (*p_sel == 'e' && max_pos.col > 0)
3236 --max_pos.col;
3237
3238 if (VIsual_mode == Ctrl_V)
3239 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003240#ifdef FEAT_LINEBREAK
3241 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003242 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003244 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003245 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003246 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 oparg.is_VIsual = 1;
3249 oparg.block_mode = TRUE;
3250 oparg.op_type = OP_NOP;
3251 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003252 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003253#ifdef FEAT_LINEBREAK
3254 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003255 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003256#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003257 if (curwin->w_curswant == MAXCOL)
3258 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003259 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (oparg.end_vcol < oparg.start_vcol)
3261 {
3262 oparg.end_vcol += oparg.start_vcol;
3263 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3264 oparg.end_vcol -= oparg.start_vcol;
3265 }
3266 }
3267 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
3270 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3271 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003272 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003273 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
3275 ui_breakcheck();
3276 if (got_int)
3277 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003278 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003281 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (VIsual_active
3283 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3284 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003285 char_u *s = NULL;
3286 long len = 0L;
3287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 switch (VIsual_mode)
3289 {
3290 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003294 s = bd.textstart;
3295 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 break;
3297 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003298 s = ml_get(lnum);
3299 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 break;
3301 case 'v':
3302 {
3303 colnr_T start_col = (lnum == min_pos.lnum)
3304 ? min_pos.col : 0;
3305 colnr_T end_col = (lnum == max_pos.lnum)
3306 ? max_pos.col - start_col + 1 : MAXCOL;
3307
Bram Moolenaardef9e822004-12-31 20:58:58 +00003308 s = ml_get(lnum) + start_col;
3309 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311 break;
3312 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003313 if (s != NULL)
3314 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003315 byte_count_cursor += line_count_info(s, &word_count_cursor,
3316 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003317 if (lnum == curbuf->b_ml.ml_line_count
3318 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003319 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003320 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003321 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003326 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (lnum == curwin->w_cursor.lnum)
3328 {
3329 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003330 char_count_cursor += char_count;
3331 byte_count_cursor = byte_count +
3332 line_count_info(ml_get(lnum),
3333 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003334 (varnumber_T)(curwin->w_cursor.col + 1),
3335 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003339 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003340 &char_count, (varnumber_T)MAXCOL,
3341 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003344 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003345 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003346 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaared767a22016-01-03 22:49:16 +01003348 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003350 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003352 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3353 {
3354 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3355 &max_pos.col);
3356 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3357 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3358 }
3359 else
3360 buf1[0] = NUL;
3361
3362 if (char_count_cursor == byte_count_cursor
3363 && char_count == byte_count)
3364 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003365 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003366 buf1, line_count_selected,
3367 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003368 word_count_cursor,
3369 word_count,
3370 byte_count_cursor,
3371 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003372 else
3373 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003374 _("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 +01003375 buf1, line_count_selected,
3376 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003377 word_count_cursor,
3378 word_count,
3379 char_count_cursor,
3380 char_count,
3381 byte_count_cursor,
3382 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003385 {
3386 p = ml_get_curline();
3387 validate_virtcol();
3388 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3389 (int)curwin->w_virtcol + 1);
zeertzjq94b7c322024-03-12 21:50:32 +01003390 col_print(buf2, sizeof(buf2), ml_get_curline_len(),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003391 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaared767a22016-01-03 22:49:16 +01003393 if (char_count_cursor == byte_count_cursor
3394 && char_count == byte_count)
3395 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003396 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003397 (char *)buf1, (char *)buf2,
3398 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003400 word_count_cursor, word_count,
3401 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003402 else
3403 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003404 _("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 +01003405 (char *)buf1, (char *)buf2,
3406 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003407 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003408 word_count_cursor, word_count,
3409 char_count_cursor, char_count,
3410 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003411 }
3412 }
3413
Bram Moolenaared767a22016-01-03 22:49:16 +01003414 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003415 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003416 {
3417 size_t len = STRLEN(IObuff);
3418
3419 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003420 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003421 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003422 if (dict == NULL)
3423 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003424 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003425 p = p_shm;
3426 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003427 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003428 p_shm = p;
3429 }
3430 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003431#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003432 if (dict != NULL)
3433 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003434 dict_add_number(dict, "words", word_count);
3435 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003436 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003437 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3438 byte_count_cursor);
3439 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3440 char_count_cursor);
3441 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3442 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003446
3447/*
3448 * Handle indent and format operators and visual mode ":".
3449 */
3450 static void
3451op_colon(oparg_T *oap)
3452{
3453 stuffcharReadbuff(':');
3454 if (oap->is_VIsual)
3455 stuffReadbuff((char_u *)"'<,'>");
3456 else
3457 {
3458 // Make the range look nice, so it can be repeated.
3459 if (oap->start.lnum == curwin->w_cursor.lnum)
3460 stuffcharReadbuff('.');
3461 else
3462 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003463
3464#ifdef FEAT_FOLDING
3465 // When using !! on a closed fold the range ".!" works best to operate
3466 // on, it will be made the whole closed fold later.
3467 linenr_T endOfStartFold = oap->start.lnum;
3468 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3469#endif
3470 if (oap->end.lnum != oap->start.lnum
3471#ifdef FEAT_FOLDING
3472 && oap->end.lnum != endOfStartFold
3473#endif
3474 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003475 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003476 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003477 stuffcharReadbuff(',');
3478 if (oap->end.lnum == curwin->w_cursor.lnum)
3479 stuffcharReadbuff('.');
3480 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3481 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003482 else if (oap->start.lnum == curwin->w_cursor.lnum
3483#ifdef FEAT_FOLDING
3484 // do not use ".+number" for a closed fold, it would count
3485 // folded lines twice
3486 && !hasFolding(oap->end.lnum, NULL, NULL)
3487#endif
3488 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003489 {
3490 stuffReadbuff((char_u *)".+");
3491 stuffnumReadbuff((long)oap->line_count - 1);
3492 }
3493 else
3494 stuffnumReadbuff((long)oap->end.lnum);
3495 }
3496 }
3497 if (oap->op_type != OP_COLON)
3498 stuffReadbuff((char_u *)"!");
3499 if (oap->op_type == OP_INDENT)
3500 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003501 if (*get_equalprg() == NUL)
3502 stuffReadbuff((char_u *)"indent");
3503 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003504 stuffReadbuff(get_equalprg());
3505 stuffReadbuff((char_u *)"\n");
3506 }
3507 else if (oap->op_type == OP_FORMAT)
3508 {
3509 if (*curbuf->b_p_fp != NUL)
3510 stuffReadbuff(curbuf->b_p_fp);
3511 else if (*p_fp != NUL)
3512 stuffReadbuff(p_fp);
3513 else
3514 stuffReadbuff((char_u *)"fmt");
3515 stuffReadbuff((char_u *)"\n']");
3516 }
3517
3518 // do_cmdline() does the rest
3519}
3520
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003521// callback function for 'operatorfunc'
3522static callback_T opfunc_cb;
3523
3524/*
3525 * Process the 'operatorfunc' option value.
3526 * Returns OK or FAIL.
3527 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003528 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003529did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003530{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003531 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3532 return e_invalid_argument;
3533
3534 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003535}
3536
3537#if defined(EXITFREE) || defined(PROTO)
3538 void
3539free_operatorfunc_option(void)
3540{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003541# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003542 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003543# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003544}
3545#endif
3546
Dominique Pelle748b3082022-01-08 12:41:16 +00003547#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003548/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003549 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003550 * garbage collected.
3551 */
3552 int
3553set_ref_in_opfunc(int copyID UNUSED)
3554{
3555 int abort = FALSE;
3556
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003557 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003558
3559 return abort;
3560}
Dominique Pelle748b3082022-01-08 12:41:16 +00003561#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003562
3563/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003564 * Handle the "g@" operator: call 'operatorfunc'.
3565 */
3566 static void
3567op_function(oparg_T *oap UNUSED)
3568{
3569#ifdef FEAT_EVAL
3570 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003571 pos_T orig_start = curbuf->b_op_start;
3572 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003573 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003574
3575 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003576 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003577 else
3578 {
3579 // Set '[ and '] marks to text to be operated on.
3580 curbuf->b_op_start = oap->start;
3581 curbuf->b_op_end = oap->end;
3582 if (oap->motion_type != MLINE && !oap->inclusive)
3583 // Exclude the end position.
3584 decl(&curbuf->b_op_end);
3585
3586 argv[0].v_type = VAR_STRING;
3587 if (oap->block_mode)
3588 argv[0].vval.v_string = (char_u *)"block";
3589 else if (oap->motion_type == MLINE)
3590 argv[0].vval.v_string = (char_u *)"line";
3591 else
3592 argv[0].vval.v_string = (char_u *)"char";
3593 argv[1].v_type = VAR_UNKNOWN;
3594
3595 // Reset virtual_op so that 'virtualedit' can be changed in the
3596 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003597 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003598 virtual_op = MAYBE;
3599
naohiro ono75c30e92021-10-19 11:15:41 +01003600 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003601 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003602 finish_op = FALSE;
3603
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003604 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3605 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003606
3607 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003608 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003609 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003610 {
3611 curbuf->b_op_start = orig_start;
3612 curbuf->b_op_end = orig_end;
3613 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003614 }
3615#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003616 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003617#endif
3618}
3619
3620/*
3621 * Calculate start/end virtual columns for operating in block mode.
3622 */
3623 static void
3624get_op_vcol(
3625 oparg_T *oap,
3626 colnr_T redo_VIsual_vcol,
3627 int initial) // when TRUE adjust position for 'selectmode'
3628{
3629 colnr_T start, end;
3630
3631 if (VIsual_mode != Ctrl_V
3632 || (!initial && oap->end.col < curwin->w_width))
3633 return;
3634
3635 oap->block_mode = TRUE;
3636
3637 // prevent from moving onto a trail byte
3638 if (has_mbyte)
3639 mb_adjustpos(curwin->w_buffer, &oap->end);
3640
3641 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3642
3643 if (!redo_VIsual_busy)
3644 {
3645 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3646
3647 if (start < oap->start_vcol)
3648 oap->start_vcol = start;
3649 if (end > oap->end_vcol)
3650 {
3651 if (initial && *p_sel == 'e' && start >= 1
3652 && start - 1 >= oap->end_vcol)
3653 oap->end_vcol = start - 1;
3654 else
3655 oap->end_vcol = end;
3656 }
3657 }
3658
3659 // if '$' was used, get oap->end_vcol from longest line
3660 if (curwin->w_curswant == MAXCOL)
3661 {
3662 curwin->w_cursor.col = MAXCOL;
3663 oap->end_vcol = 0;
3664 for (curwin->w_cursor.lnum = oap->start.lnum;
3665 curwin->w_cursor.lnum <= oap->end.lnum;
3666 ++curwin->w_cursor.lnum)
3667 {
3668 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3669 if (end > oap->end_vcol)
3670 oap->end_vcol = end;
3671 }
3672 }
3673 else if (redo_VIsual_busy)
3674 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3675 // Correct oap->end.col and oap->start.col to be the
3676 // upper-left and lower-right corner of the block area.
3677 //
3678 // (Actually, this does convert column positions into character
3679 // positions)
3680 curwin->w_cursor.lnum = oap->end.lnum;
3681 coladvance(oap->end_vcol);
3682 oap->end = curwin->w_cursor;
3683
3684 curwin->w_cursor = oap->start;
3685 coladvance(oap->start_vcol);
3686 oap->start = curwin->w_cursor;
3687}
3688
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003689// Information for redoing the previous Visual selection.
3690typedef struct {
3691 int rv_mode; // 'v', 'V', or Ctrl-V
3692 linenr_T rv_line_count; // number of lines
3693 colnr_T rv_vcol; // number of cols or end column
3694 long rv_count; // count for Visual operator
3695 int rv_arg; // extra argument
3696} redo_VIsual_T;
3697
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003698 static int
3699is_ex_cmdchar(cmdarg_T *cap)
3700{
3701 return cap->cmdchar == ':'
3702 || cap->cmdchar == K_COMMAND
3703 || cap->cmdchar == K_SCRIPT_COMMAND;
3704}
3705
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003706/*
3707 * Handle an operator after Visual mode or when the movement is finished.
3708 * "gui_yank" is true when yanking text for the clipboard.
3709 */
3710 void
3711do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3712{
3713 oparg_T *oap = cap->oap;
3714 pos_T old_cursor;
3715 int empty_region_error;
3716 int restart_edit_save;
3717#ifdef FEAT_LINEBREAK
3718 int lbr_saved = curwin->w_p_lbr;
3719#endif
3720
3721 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003722 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3723
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003724 int include_line_break = FALSE;
3725
3726#if defined(FEAT_CLIPBOARD)
3727 // Yank the visual area into the GUI selection register before we operate
3728 // on it and lose it forever.
3729 // Don't do it if a specific register was specified, so that ""x"*P works.
3730 // This could call do_pending_operator() recursively, but that's OK
3731 // because gui_yank will be TRUE for the nested call.
3732 if ((clip_star.available || clip_plus.available)
3733 && oap->op_type != OP_NOP
3734 && !gui_yank
3735 && VIsual_active
3736 && !redo_VIsual_busy
3737 && oap->regname == 0)
3738 clip_auto_select();
3739#endif
3740 old_cursor = curwin->w_cursor;
3741
3742 // If an operation is pending, handle it...
3743 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3744 {
3745 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3746 // for the clipboard.
3747 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3748
3749#ifdef FEAT_LINEBREAK
3750 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003751 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003752#endif
3753 oap->is_VIsual = VIsual_active;
3754 if (oap->motion_force == 'V')
3755 oap->motion_type = MLINE;
3756 else if (oap->motion_force == 'v')
3757 {
3758 // If the motion was linewise, "inclusive" will not have been set.
3759 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3760 if (oap->motion_type == MLINE)
3761 oap->inclusive = FALSE;
3762 // If the motion already was characterwise, toggle "inclusive"
3763 else if (oap->motion_type == MCHAR)
3764 oap->inclusive = !oap->inclusive;
3765 oap->motion_type = MCHAR;
3766 }
3767 else if (oap->motion_force == Ctrl_V)
3768 {
3769 // Change line- or characterwise motion into Visual block mode.
3770 if (!VIsual_active)
3771 {
3772 VIsual_active = TRUE;
3773 VIsual = oap->start;
3774 }
3775 VIsual_mode = Ctrl_V;
3776 VIsual_select = FALSE;
3777 VIsual_reselect = FALSE;
3778 }
3779
3780 // Only redo yank when 'y' flag is in 'cpoptions'.
3781 // Never redo "zf" (define fold).
3782 if ((redo_yank || oap->op_type != OP_YANK)
3783 && ((!VIsual_active || oap->motion_force)
3784 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003785 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003786 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003787 && cap->cmdchar != 'D'
3788#ifdef FEAT_FOLDING
3789 && oap->op_type != OP_FOLD
3790 && oap->op_type != OP_FOLDOPEN
3791 && oap->op_type != OP_FOLDOPENREC
3792 && oap->op_type != OP_FOLDCLOSE
3793 && oap->op_type != OP_FOLDCLOSEREC
3794 && oap->op_type != OP_FOLDDEL
3795 && oap->op_type != OP_FOLDDELREC
3796#endif
3797 )
3798 {
3799 prep_redo(oap->regname, cap->count0,
3800 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3801 oap->motion_force, cap->cmdchar, cap->nchar);
3802 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3803 {
3804 // If 'cpoptions' does not contain 'r', insert the search
3805 // pattern to really repeat the same command.
3806 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3807 AppendToRedobuffLit(cap->searchbuf, -1);
3808 AppendToRedobuff(NL_STR);
3809 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003810 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003811 {
3812 // do_cmdline() has stored the first typed line in
3813 // "repeat_cmdline". When several lines are typed repeating
3814 // won't be possible.
3815 if (repeat_cmdline == NULL)
3816 ResetRedobuff();
3817 else
3818 {
zeertzjq30b6d612023-05-07 17:39:23 +01003819 if (cap->cmdchar == ':')
3820 AppendToRedobuffLit(repeat_cmdline, -1);
3821 else
3822 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003823 AppendToRedobuff(NL_STR);
3824 VIM_CLEAR(repeat_cmdline);
3825 }
3826 }
3827 }
3828
3829 if (redo_VIsual_busy)
3830 {
3831 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003832 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003833 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003834 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003835 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3836 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003837 VIsual_mode = redo_VIsual.rv_mode;
3838 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003839 {
3840 if (VIsual_mode == 'v')
3841 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003842 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003843 {
3844 validate_virtcol();
3845 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003846 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003847 }
3848 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003849 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003850 }
3851 else
3852 {
3853 curwin->w_curswant = MAXCOL;
3854 }
3855 coladvance(curwin->w_curswant);
3856 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003857 cap->count0 = redo_VIsual.rv_count;
3858 if (redo_VIsual.rv_count != 0)
3859 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003860 else
3861 cap->count1 = 1;
3862 }
3863 else if (VIsual_active)
3864 {
3865 if (!gui_yank)
3866 {
3867 // Save the current VIsual area for '< and '> marks, and "gv"
3868 curbuf->b_visual.vi_start = VIsual;
3869 curbuf->b_visual.vi_end = curwin->w_cursor;
3870 curbuf->b_visual.vi_mode = VIsual_mode;
3871 restore_visual_mode();
3872 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003873#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003874 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003875#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003876 }
3877
3878 // In Select mode, a linewise selection is operated upon like a
3879 // characterwise selection.
3880 // Special case: gH<Del> deletes the last line.
3881 if (VIsual_select && VIsual_mode == 'V'
3882 && cap->oap->op_type != OP_DELETE)
3883 {
3884 if (LT_POS(VIsual, curwin->w_cursor))
3885 {
3886 VIsual.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003887 curwin->w_cursor.col = ml_get_len(curwin->w_cursor.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003888 }
3889 else
3890 {
3891 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003892 VIsual.col = ml_get_len(VIsual.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003893 }
3894 VIsual_mode = 'v';
3895 }
3896 // If 'selection' is "exclusive", backup one character for
3897 // charwise selections.
3898 else if (VIsual_mode == 'v')
3899 include_line_break = unadjust_for_sel();
3900
3901 oap->start = VIsual;
3902 if (VIsual_mode == 'V')
3903 {
3904 oap->start.col = 0;
3905 oap->start.coladd = 0;
3906 }
3907 }
3908
3909 // Set oap->start to the first position of the operated text, oap->end
3910 // to the end of the operated text. w_cursor is equal to oap->start.
3911 if (LT_POS(oap->start, curwin->w_cursor))
3912 {
3913#ifdef FEAT_FOLDING
3914 // Include folded lines completely.
3915 if (!VIsual_active)
3916 {
3917 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3918 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003919 if ((curwin->w_cursor.col > 0 || oap->inclusive
3920 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003921 && hasFolding(curwin->w_cursor.lnum, NULL,
3922 &curwin->w_cursor.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003923 curwin->w_cursor.col = ml_get_curline_len();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003924 }
3925#endif
3926 oap->end = curwin->w_cursor;
3927 curwin->w_cursor = oap->start;
3928
3929 // w_virtcol may have been updated; if the cursor goes back to its
3930 // previous position w_virtcol becomes invalid and isn't updated
3931 // automatically.
3932 curwin->w_valid &= ~VALID_VIRTCOL;
3933 }
3934 else
3935 {
3936#ifdef FEAT_FOLDING
3937 // Include folded lines completely.
3938 if (!VIsual_active && oap->motion_type == MLINE)
3939 {
3940 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3941 NULL))
3942 curwin->w_cursor.col = 0;
3943 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003944 oap->start.col = ml_get_len(oap->start.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003945 }
3946#endif
3947 oap->end = oap->start;
3948 oap->start = curwin->w_cursor;
3949 }
3950
3951 // Just in case lines were deleted that make the position invalid.
3952 check_pos(curwin->w_buffer, &oap->end);
3953 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3954
3955 // Set "virtual_op" before resetting VIsual_active.
3956 virtual_op = virtual_active();
3957
3958 if (VIsual_active || redo_VIsual_busy)
3959 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003960 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003961
3962 if (!redo_VIsual_busy && !gui_yank)
3963 {
3964 // Prepare to reselect and redo Visual: this is based on the
3965 // size of the Visual text
3966 resel_VIsual_mode = VIsual_mode;
3967 if (curwin->w_curswant == MAXCOL)
3968 resel_VIsual_vcol = MAXCOL;
3969 else
3970 {
3971 if (VIsual_mode != Ctrl_V)
3972 getvvcol(curwin, &(oap->end),
3973 NULL, NULL, &oap->end_vcol);
3974 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3975 {
3976 if (VIsual_mode != Ctrl_V)
3977 getvvcol(curwin, &(oap->start),
3978 &oap->start_vcol, NULL, NULL);
3979 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3980 }
3981 else
3982 resel_VIsual_vcol = oap->end_vcol;
3983 }
3984 resel_VIsual_line_count = oap->line_count;
3985 }
3986
3987 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3988 if ((redo_yank || oap->op_type != OP_YANK)
3989 && oap->op_type != OP_COLON
3990#ifdef FEAT_FOLDING
3991 && oap->op_type != OP_FOLD
3992 && oap->op_type != OP_FOLDOPEN
3993 && oap->op_type != OP_FOLDOPENREC
3994 && oap->op_type != OP_FOLDCLOSE
3995 && oap->op_type != OP_FOLDCLOSEREC
3996 && oap->op_type != OP_FOLDDEL
3997 && oap->op_type != OP_FOLDDELREC
3998#endif
3999 && oap->motion_force == NUL
4000 )
4001 {
4002 // Prepare for redoing. Only use the nchar field for "r",
4003 // otherwise it might be the second char of the operator.
4004 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4005 || cap->nchar == 'N'))
4006 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004007 get_op_char(oap->op_type),
4008 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004009 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004010 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004011 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004012 int opchar = get_op_char(oap->op_type);
4013 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004014 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4015
4016 // reverse what nv_replace() did
4017 if (nchar == REPLACE_CR_NCHAR)
4018 nchar = CAR;
4019 else if (nchar == REPLACE_NL_NCHAR)
4020 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004021
4022 if (opchar == 'g' && extra_opchar == '@')
4023 // also repeat the count for 'operatorfunc'
4024 prep_redo_num2(oap->regname, 0L, NUL, 'v',
4025 cap->count0, opchar, extra_opchar, nchar);
4026 else
4027 prep_redo(oap->regname, 0L, NUL, 'v',
4028 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004029 }
4030 if (!redo_VIsual_busy)
4031 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004032 redo_VIsual.rv_mode = resel_VIsual_mode;
4033 redo_VIsual.rv_vcol = resel_VIsual_vcol;
4034 redo_VIsual.rv_line_count = resel_VIsual_line_count;
4035 redo_VIsual.rv_count = cap->count0;
4036 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004037 }
4038 }
4039
4040 // oap->inclusive defaults to TRUE.
4041 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4042 // FALSE. This makes "d}P" and "v}dP" work the same.
4043 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4044 oap->inclusive = TRUE;
4045 if (VIsual_mode == 'V')
4046 oap->motion_type = MLINE;
4047 else
4048 {
4049 oap->motion_type = MCHAR;
4050 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4051 && (include_line_break || !virtual_op))
4052 {
4053 oap->inclusive = FALSE;
4054 // Try to include the newline, unless it's an operator
4055 // that works on lines only.
4056 if (*p_sel != 'o'
4057 && !op_on_lines(oap->op_type)
4058 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4059 {
4060 ++oap->end.lnum;
4061 oap->end.col = 0;
4062 oap->end.coladd = 0;
4063 ++oap->line_count;
4064 }
4065 }
4066 }
4067
4068 redo_VIsual_busy = FALSE;
4069
4070 // Switch Visual off now, so screen updating does
4071 // not show inverted text when the screen is redrawn.
4072 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4073 // no screen redraw, so it is done here to remove the inverted
4074 // part.
4075 if (!gui_yank)
4076 {
4077 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004078 setmouse();
4079 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004080 may_clear_cmdline();
4081 if ((oap->op_type == OP_YANK
4082 || oap->op_type == OP_COLON
4083 || oap->op_type == OP_FUNCTION
4084 || oap->op_type == OP_FILTER)
4085 && oap->motion_force == NUL)
4086 {
4087#ifdef FEAT_LINEBREAK
4088 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01004089 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004090#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004091 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004092 }
4093 }
4094 }
4095
4096 // Include the trailing byte of a multi-byte char.
4097 if (has_mbyte && oap->inclusive)
4098 {
4099 int l;
4100
4101 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4102 if (l > 1)
4103 oap->end.col += l - 1;
4104 }
4105 curwin->w_set_curswant = TRUE;
4106
4107 // oap->empty is set when start and end are the same. The inclusive
4108 // flag affects this too, unless yanking and the end is on a NUL.
4109 oap->empty = (oap->motion_type == MCHAR
4110 && (!oap->inclusive
4111 || (oap->op_type == OP_YANK
4112 && gchar_pos(&oap->end) == NUL))
4113 && EQUAL_POS(oap->start, oap->end)
4114 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4115 // For delete, change and yank, it's an error to operate on an
4116 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4117 empty_region_error = (oap->empty
4118 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4119
4120 // Force a redraw when operating on an empty Visual region, when
4121 // 'modifiable is off or creating a fold.
4122 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4123#ifdef FEAT_FOLDING
4124 || oap->op_type == OP_FOLD
4125#endif
4126 ))
4127 {
4128#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004129 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004130#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004131 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004132 }
4133
4134 // If the end of an operator is in column one while oap->motion_type
4135 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4136 // character in the previous line. If op_start is on or before the
4137 // first non-blank in the line, the operator becomes linewise
4138 // (strange, but that's the way vi does it).
4139 if ( oap->motion_type == MCHAR
4140 && oap->inclusive == FALSE
4141 && !(cap->retval & CA_NO_ADJ_OP_END)
4142 && oap->end.col == 0
4143 && (!oap->is_VIsual || *p_sel == 'o')
4144 && !oap->block_mode
4145 && oap->line_count > 1)
4146 {
4147 oap->end_adjusted = TRUE; // remember that we did this
4148 --oap->line_count;
4149 --oap->end.lnum;
4150 if (inindent(0))
4151 oap->motion_type = MLINE;
4152 else
4153 {
zeertzjq94b7c322024-03-12 21:50:32 +01004154 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004155 if (oap->end.col)
4156 {
4157 --oap->end.col;
4158 oap->inclusive = TRUE;
4159 }
4160 }
4161 }
4162 else
4163 oap->end_adjusted = FALSE;
4164
4165 switch (oap->op_type)
4166 {
4167 case OP_LSHIFT:
4168 case OP_RSHIFT:
4169 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4170 auto_format(FALSE, TRUE);
4171 break;
4172
4173 case OP_JOIN_NS:
4174 case OP_JOIN:
4175 if (oap->line_count < 2)
4176 oap->line_count = 2;
4177 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4178 curbuf->b_ml.ml_line_count)
4179 beep_flush();
4180 else
4181 {
4182 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4183 TRUE, TRUE, TRUE);
4184 auto_format(FALSE, TRUE);
4185 }
4186 break;
4187
4188 case OP_DELETE:
4189 VIsual_reselect = FALSE; // don't reselect now
4190 if (empty_region_error)
4191 {
4192 vim_beep(BO_OPER);
4193 CancelRedo();
4194 }
4195 else
4196 {
4197 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004198 // save cursor line for undo if it wasn't saved yet
4199 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4200 && u_save_cursor() == OK)
4201 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004202 }
4203 break;
4204
4205 case OP_YANK:
4206 if (empty_region_error)
4207 {
4208 if (!gui_yank)
4209 {
4210 vim_beep(BO_OPER);
4211 CancelRedo();
4212 }
4213 }
4214 else
4215 {
4216#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004217 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004218#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004219 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004220 (void)op_yank(oap, FALSE, !gui_yank);
4221 }
4222 check_cursor_col();
4223 break;
4224
4225 case OP_CHANGE:
4226 VIsual_reselect = FALSE; // don't reselect now
4227 if (empty_region_error)
4228 {
4229 vim_beep(BO_OPER);
4230 CancelRedo();
4231 }
4232 else
4233 {
4234 // This is a new edit command, not a restart. Need to
4235 // remember it to make 'insertmode' work with mappings for
4236 // Visual mode. But do this only once and not when typed and
4237 // 'insertmode' isn't set.
4238 if (p_im || !KeyTyped)
4239 restart_edit_save = restart_edit;
4240 else
4241 restart_edit_save = 0;
4242 restart_edit = 0;
4243#ifdef FEAT_LINEBREAK
4244 // Restore linebreak, so that when the user edits it looks as
4245 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004246 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004247#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004248 // trigger TextChangedI
4249 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4250
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004251 if (op_change(oap)) // will call edit()
4252 cap->retval |= CA_COMMAND_BUSY;
4253 if (restart_edit == 0)
4254 restart_edit = restart_edit_save;
4255 }
4256 break;
4257
4258 case OP_FILTER:
4259 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4260 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4261 else
4262 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4263 // FALLTHROUGH
4264
4265 case OP_INDENT:
4266 case OP_COLON:
4267
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004268 // If 'equalprg' is empty, do the indenting internally.
4269 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4270 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004271 if (curbuf->b_p_lisp)
4272 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004273#ifdef FEAT_EVAL
4274 if (use_indentexpr_for_lisp())
4275 op_reindent(oap, get_expr_indent);
4276 else
4277#endif
4278 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004279 break;
4280 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004281 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004282#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004283 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004284#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004285 get_c_indent);
4286 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004287 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004288
4289 op_colon(oap);
4290 break;
4291
4292 case OP_TILDE:
4293 case OP_UPPER:
4294 case OP_LOWER:
4295 case OP_ROT13:
4296 if (empty_region_error)
4297 {
4298 vim_beep(BO_OPER);
4299 CancelRedo();
4300 }
4301 else
4302 op_tilde(oap);
4303 check_cursor_col();
4304 break;
4305
4306 case OP_FORMAT:
4307#if defined(FEAT_EVAL)
4308 if (*curbuf->b_p_fex != NUL)
4309 op_formatexpr(oap); // use expression
4310 else
4311#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004312 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004313 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004314 op_colon(oap); // use external command
4315 else
4316 op_format(oap, FALSE); // use internal function
4317 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004318 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004319 case OP_FORMAT2:
4320 op_format(oap, TRUE); // use internal function
4321 break;
4322
4323 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004324 {
4325 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4326
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004327#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004328 // Restore linebreak, so that when the user edits it looks as
4329 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004330 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004331#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004332 // call 'operatorfunc'
4333 op_function(oap);
4334
4335 // Restore the info for redoing Visual mode, the function may
4336 // invoke another operator and unintentionally change it.
4337 redo_VIsual = save_redo_VIsual;
4338 break;
4339 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004340
4341 case OP_INSERT:
4342 case OP_APPEND:
4343 VIsual_reselect = FALSE; // don't reselect now
4344 if (empty_region_error)
4345 {
4346 vim_beep(BO_OPER);
4347 CancelRedo();
4348 }
4349 else
4350 {
4351 // This is a new edit command, not a restart. Need to
4352 // remember it to make 'insertmode' work with mappings for
4353 // Visual mode. But do this only once.
4354 restart_edit_save = restart_edit;
4355 restart_edit = 0;
4356#ifdef FEAT_LINEBREAK
4357 // Restore linebreak, so that when the user edits it looks as
4358 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004359 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004360#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004361 // trigger TextChangedI
4362 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4363
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004364 op_insert(oap, cap->count1);
4365#ifdef FEAT_LINEBREAK
4366 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004367 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004368#endif
4369
4370 // TODO: when inserting in several lines, should format all
4371 // the lines.
4372 auto_format(FALSE, TRUE);
4373
4374 if (restart_edit == 0)
4375 restart_edit = restart_edit_save;
4376 else
4377 cap->retval |= CA_COMMAND_BUSY;
4378 }
4379 break;
4380
4381 case OP_REPLACE:
4382 VIsual_reselect = FALSE; // don't reselect now
4383 if (empty_region_error)
4384 {
4385 vim_beep(BO_OPER);
4386 CancelRedo();
4387 }
4388 else
4389 {
4390#ifdef FEAT_LINEBREAK
4391 // Restore linebreak, so that when the user edits it looks as
4392 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004393 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004394#endif
4395 op_replace(oap, cap->nchar);
4396 }
4397 break;
4398
4399#ifdef FEAT_FOLDING
4400 case OP_FOLD:
4401 VIsual_reselect = FALSE; // don't reselect now
4402 foldCreate(oap->start.lnum, oap->end.lnum);
4403 break;
4404
4405 case OP_FOLDOPEN:
4406 case OP_FOLDOPENREC:
4407 case OP_FOLDCLOSE:
4408 case OP_FOLDCLOSEREC:
4409 VIsual_reselect = FALSE; // don't reselect now
4410 opFoldRange(oap->start.lnum, oap->end.lnum,
4411 oap->op_type == OP_FOLDOPEN
4412 || oap->op_type == OP_FOLDOPENREC,
4413 oap->op_type == OP_FOLDOPENREC
4414 || oap->op_type == OP_FOLDCLOSEREC,
4415 oap->is_VIsual);
4416 break;
4417
4418 case OP_FOLDDEL:
4419 case OP_FOLDDELREC:
4420 VIsual_reselect = FALSE; // don't reselect now
4421 deleteFold(oap->start.lnum, oap->end.lnum,
4422 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4423 break;
4424#endif
4425 case OP_NR_ADD:
4426 case OP_NR_SUB:
4427 if (empty_region_error)
4428 {
4429 vim_beep(BO_OPER);
4430 CancelRedo();
4431 }
4432 else
4433 {
4434 VIsual_active = TRUE;
4435#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004436 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004437#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004438 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004439 VIsual_active = FALSE;
4440 }
4441 check_cursor_col();
4442 break;
4443 default:
4444 clearopbeep(oap);
4445 }
4446 virtual_op = MAYBE;
4447 if (!gui_yank)
4448 {
4449 // if 'sol' not set, go back to old column for some commands
4450 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4451 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4452 || oap->op_type == OP_DELETE))
4453 {
4454#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004455 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004456#endif
4457 coladvance(curwin->w_curswant = old_col);
4458 }
4459 }
4460 else
4461 {
4462 curwin->w_cursor = old_cursor;
4463 }
4464 oap->block_mode = FALSE;
4465 clearop(oap);
4466 motion_force = NUL;
4467 }
4468#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004469 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004470#endif
4471}
Christian Brabandtffb13672023-09-15 20:22:02 +02004472
4473// put byte 'c' at position 'lp', but
4474// verify, that the position to place
4475// is actually safe
4476 static void
4477pbyte(pos_T lp, int c)
4478{
4479 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4480 int len = curbuf->b_ml.ml_line_len;
4481
4482 // safety check
4483 if (lp.col >= len)
4484 lp.col = (len > 1 ? len - 2 : 0);
4485 *(p + lp.col) = c;
4486}