blob: 2de2557fbd1aabc7d1ec1344225cfca18b9736dd [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
Christian Brabandt22105fd2024-07-15 20:51:11 +0200679 && (vim_strchr(p_cpo, CPO_WORD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 && oap->op_type == OP_DELETE)
681 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200682 ptr = ml_get(oap->end.lnum) + oap->end.col;
683 if (*ptr != NUL)
684 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ptr = skipwhite(ptr);
686 if (*ptr == NUL && inindent(0))
687 oap->motion_type = MLINE;
688 }
689
Bram Moolenaard04b7502010-07-08 22:27:55 +0200690 /*
691 * Check for trying to delete (e.g. "D") in an empty line.
692 * Note: For the change operator it is ok.
693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if ( oap->motion_type == MCHAR
695 && oap->line_count == 1
696 && oap->op_type == OP_DELETE
697 && *ml_get(oap->start.lnum) == NUL)
698 {
699 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000700 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 * 'cpoptions' (Vi compatible).
702 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000703 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
705 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000706 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
708 beep_flush();
709 return OK;
710 }
711
Bram Moolenaard04b7502010-07-08 22:27:55 +0200712 /*
713 * Do a yank of whatever we're about to delete.
714 * If a yank register was specified, put the deleted text into that
715 * register. For the black hole register '_' don't yank anything.
716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (oap->regname != '_')
718 {
719 if (oap->regname != 0)
720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (!valid_yank_reg(oap->regname, TRUE))
723 {
724 beep_flush();
725 return OK;
726 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100727 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
728 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 did_yank = TRUE;
730 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200731 else
732 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 /*
735 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100736 * delete contains a line break, or when using a specific operator (Vi
737 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100739 if (oap->motion_type == MLINE || oap->line_count > 1
740 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100742 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 if (op_yank(oap, TRUE, FALSE) == OK)
744 did_yank = TRUE;
745 }
746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100747 // Yank into small delete register when no named register specified
748 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200749 if ((
750#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200751 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
752 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200753#endif
754 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 && oap->line_count == 1)
756 {
757 oap->regname = '-';
758 get_yank_register(oap->regname, TRUE);
759 if (op_yank(oap, TRUE, FALSE) == OK)
760 did_yank = TRUE;
761 oap->regname = 0;
762 }
763
764 /*
765 * If there's too much stuff to fit in the yank register, then get a
766 * confirmation before doing the delete. This is crude, but simple.
767 * And it avoids doing a delete of something we can't put back if we
768 * want.
769 */
770 if (!did_yank)
771 {
772 int msg_silent_save = msg_silent;
773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100774 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
776 msg_silent = msg_silent_save;
777 if (n != 'y')
778 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000779 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 return FAIL;
781 }
782 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100783
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100784#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100785 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200786 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
789
Bram Moolenaard04b7502010-07-08 22:27:55 +0200790 /*
791 * block mode delete
792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 if (oap->block_mode)
794 {
795 if (u_save((linenr_T)(oap->start.lnum - 1),
796 (linenr_T)(oap->end.lnum + 1)) == FAIL)
797 return FAIL;
798
799 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
800 {
801 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100802 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 continue;
804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100805 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (lnum == curwin->w_cursor.lnum)
807 {
808 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811
Bram Moolenaar8055d172019-05-17 22:57:26 +0200812 // "n" == number of chars deleted
813 // If we delete a TAB, it may be replaced by several characters.
814 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 n = bd.textlen - bd.startspaces - bd.endspaces;
816 oldp = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100817 newp = alloc(ml_get_len(lnum) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (newp == NULL)
819 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100820 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100822 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200823 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100825 // copy the part after the deleted part
John Marriott38b9f452024-04-25 21:39:18 +0200826 STRCPY(newp + bd.textcol + bd.startspaces + bd.endspaces,
827 oldp + bd.textcol + bd.textlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200830
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100831#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200832 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200833 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
837 check_cursor_col();
838 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
839 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100840 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100842 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {
844 if (oap->op_type == OP_CHANGE)
845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 // Delete the lines except the first one. Temporarily move the
847 // cursor to the next line. Save the current line number, if the
848 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (oap->line_count > 1)
850 {
851 lnum = curwin->w_cursor.lnum;
852 ++curwin->w_cursor.lnum;
853 del_lines((long)(oap->line_count - 1), TRUE);
854 curwin->w_cursor.lnum = lnum;
855 }
856 if (u_save_cursor() == FAIL)
857 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 beginline(BL_WHITE); // cursor on first non-white
861 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 ai_col = curwin->w_cursor.col;
863 }
864 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 beginline(0); // cursor in column 0
zeertzjq8c55d602024-03-13 20:42:26 +0100866 truncate_line(FALSE); // delete the rest of the line,
867 // leaving cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 else
872 {
873 del_lines(oap->line_count, TRUE);
874 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877 }
878 else
879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (virtual_op)
881 {
882 int endcol = 0;
883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100884 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (gchar_pos(&oap->start) == '\t')
886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100887 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 return FAIL;
889 if (oap->line_count == 1)
890 endcol = getviscol2(oap->end.col, oap->end.coladd);
891 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
892 oap->start = curwin->w_cursor;
893 if (oap->line_count == 1)
894 {
895 coladvance(endcol);
896 oap->end.col = curwin->w_cursor.col;
897 oap->end.coladd = curwin->w_cursor.coladd;
898 curwin->w_cursor = oap->start;
899 }
900 }
901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (gchar_pos(&oap->end) == '\t'
904 && (int)oap->end.coladd < oap->inclusive)
905 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100906 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (u_save((linenr_T)(oap->end.lnum - 1),
908 (linenr_T)(oap->end.lnum + 1)) == FAIL)
909 return FAIL;
910 curwin->w_cursor = oap->end;
911 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
912 oap->end = curwin->w_cursor;
913 curwin->w_cursor = oap->start;
914 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100915 if (has_mbyte)
916 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100919 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100921 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 return FAIL;
923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100925 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 && oap->op_type == OP_CHANGE
927 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100928 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 display_dollar(oap->end.col - !oap->inclusive);
930
931 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (virtual_op)
934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 // fix up things for virtualedit-delete:
936 // break the tabs which are going to get in our way
zeertzjq94b7c322024-03-12 21:50:32 +0100937 int len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939 if (oap->end.coladd != 0
940 && (int)oap->end.col >= len - 1
941 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
942 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100943 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (n == 0 && oap->start.coladd != oap->end.coladd)
945 n = 1;
946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (gchar_cursor() != NUL)
949 curwin->w_cursor.coladd = 0;
950 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200951 (void)del_bytes((long)n, !virtual_op,
952 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100954 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956 pos_T curpos;
957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
960 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
961 return FAIL;
962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100963 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100965 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 ++curwin->w_cursor.lnum;
967 del_lines((long)(oap->line_count - 2), FALSE);
968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100969 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200970 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200971 curwin->w_cursor.col = 0;
972 (void)del_bytes((long)n, !virtual_op,
973 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100974 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200975 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200977 if (oap->op_type == OP_DELETE)
978 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980
981 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
982
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000983setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200984 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100986 if (oap->block_mode)
987 {
988 curbuf->b_op_end.lnum = oap->end.lnum;
989 curbuf->b_op_end.col = oap->start.col;
990 }
991 else
992 curbuf->b_op_end = oap->start;
993 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 return OK;
997}
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999/*
1000 * Adjust end of operating area for ending on a multi-byte character.
1001 * Used for deletion.
1002 */
1003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001004mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
zeertzjq048761b2024-02-24 14:21:39 +01001006 char_u *line;
1007 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001009 if (!oap->inclusive)
1010 return;
1011
zeertzjq048761b2024-02-24 14:21:39 +01001012 line = ml_get(oap->end.lnum);
1013 ptr = line + oap->end.col;
1014 if (*ptr != NUL)
1015 {
1016 ptr -= (*mb_head_off)(line, ptr);
1017 ptr += (*mb_ptr2len)(ptr) - 1;
1018 oap->end.col = ptr - line;
1019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
Bram Moolenaar630afe82018-06-28 19:26:28 +02001022/*
1023 * Replace the character under the cursor with "c".
1024 * This takes care of multi-byte characters.
1025 */
1026 static void
1027replace_character(int c)
1028{
1029 int n = State;
1030
Bram Moolenaar24959102022-05-07 20:01:16 +01001031 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001032 ins_char(c);
1033 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001034 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001035 dec_cursor();
1036}
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038/*
1039 * Replace a whole area with one character.
1040 */
1041 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001042op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +02001047 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001049 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001050 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
1052 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001055 if (c == REPLACE_CR_NCHAR)
1056 {
1057 had_ctrl_v_cr = TRUE;
1058 c = CAR;
1059 }
1060 else if (c == REPLACE_NL_NCHAR)
1061 {
1062 had_ctrl_v_cr = TRUE;
1063 c = NL;
1064 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (has_mbyte)
1067 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 if (u_save((linenr_T)(oap->start.lnum - 1),
1070 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1071 return FAIL;
1072
1073 /*
1074 * block mode replace
1075 */
1076 if (oap->block_mode)
1077 {
1078 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1079 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1080 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001081 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1083 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001084 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001086 // n == number of extra chars required
1087 // If we split a TAB, it may be replaced by several characters.
1088 // Thus the number of characters may increase!
1089 // If the range starts in virtual space, count the initial
1090 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1092 {
1093 pos_T vpos;
1094
Bram Moolenaara1381de2009-11-03 15:44:21 +00001095 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 getvpos(&vpos, oap->start_vcol);
1097 bd.startspaces += vpos.coladd;
1098 n = bd.startspaces;
1099 }
1100 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001101 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001104 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001108 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 numc = oap->end_vcol - oap->start_vcol + 1;
1110 if (bd.is_short && (!virtual_op || bd.is_MAX))
1111 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001113 // A double-wide character can be replaced only up to half the
1114 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 if ((*mb_char2cells)(c) > 1)
1116 {
1117 if ((numc & 1) && !bd.is_short)
1118 {
1119 ++bd.endspaces;
1120 ++n;
1121 }
1122 numc = numc / 2;
1123 }
1124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001125 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 num_chars = numc;
1127 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001128 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 n += numc - bd.textlen;
1130
1131 oldp = ml_get_curline();
zeertzjq94b7c322024-03-12 21:50:32 +01001132 oldlen = ml_get_curline_len();
Bram Moolenaar964b3742019-05-24 18:54:09 +02001133 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (newp == NULL)
1135 continue;
1136 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001137 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001139 newlen = bd.textcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001140 // insert pre-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001141 vim_memset(newp + newlen, ' ', (size_t)bd.startspaces);
1142 newlen += bd.startspaces;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001143 // insert replacement chars CHECK FOR ALLOCATED SPACE
1144 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1145 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001146 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001148 if (has_mbyte)
1149 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001150 while (--num_chars >= 0)
John Marriott38b9f452024-04-25 21:39:18 +02001151 newlen += (*mb_char2bytes)(c, newp + newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001152 }
1153 else
John Marriott38b9f452024-04-25 21:39:18 +02001154 {
1155 vim_memset(newp + newlen, c, (size_t)numc);
1156 newlen += numc;
1157 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001158 if (!bd.is_short)
1159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001160 // insert post-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001161 vim_memset(newp + newlen, ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001162 // copy the part after the changed part
John Marriott38b9f452024-04-25 21:39:18 +02001163 STRCPY(newp + newlen + bd.endspaces,
1164 oldp + bd.textcol + bd.textlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001169 // Replacing with \r or \n means splitting the line.
John Marriott38b9f452024-04-25 21:39:18 +02001170 after_p = alloc(oldlen + 1 + n - newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001171 if (after_p != NULL)
John Marriott38b9f452024-04-25 21:39:18 +02001172 STRCPY(after_p, oldp + bd.textcol + bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
John Marriott38b9f452024-04-25 21:39:18 +02001174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001175 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001177 if (after_p != NULL)
1178 {
1179 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1180 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1181 oap->end.lnum++;
1182 vim_free(after_p);
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 }
1186 else
1187 {
1188 /*
1189 * MCHAR and MLINE motion replace.
1190 */
1191 if (oap->motion_type == MLINE)
1192 {
1193 oap->start.col = 0;
1194 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001195 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (oap->end.col)
1197 --oap->end.col;
1198 }
1199 else if (!oap->inclusive)
1200 dec(&(oap->end));
1201
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001202 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001204 int done = FALSE;
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 n = gchar_cursor();
1207 if (n != NUL)
1208 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001209 int new_byte_len = (*mb_char2len)(c);
1210 int old_byte_len = mb_ptr2len(ml_get_cursor());
1211
1212 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001214 // This is slow, but it handles replacing a single-byte
1215 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001216 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001217 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001218 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001219 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (n == TAB)
1224 {
1225 int end_vcol = 0;
1226
1227 if (curwin->w_cursor.lnum == oap->end.lnum)
1228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // oap->end has to be recalculated when
1230 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 end_vcol = getviscol2(oap->end.col,
1232 oap->end.coladd);
1233 }
1234 coladvance_force(getviscol());
1235 if (curwin->w_cursor.lnum == oap->end.lnum)
1236 getvpos(&oap->end, end_vcol);
1237 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001238 // with "coladd" set may move to just after a TAB
1239 if (gchar_cursor() != NUL)
1240 {
1241 PBYTE(curwin->w_cursor, c);
1242 done = TRUE;
1243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001246 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 int virtcols = oap->end.coladd;
1249
1250 if (curwin->w_cursor.lnum == oap->start.lnum
1251 && oap->start.col == oap->end.col && oap->start.coladd)
1252 virtcols -= oap->start.coladd;
1253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 // oap->end has been trimmed so it's effectively inclusive;
1255 // as a result an extra +1 must be counted so we don't
1256 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1258 curwin->w_cursor.col -= (virtcols + 1);
1259 for (; virtcols >= 0; virtcols--)
1260 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001261 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001262 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001263 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001264 PBYTE(curwin->w_cursor, c);
Gary Johnson88d4f252024-06-01 20:51:33 +02001265 if (inc(&curwin->w_cursor) == -1)
1266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001270 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (inc_cursor() == -1)
1272 break;
1273 }
1274 }
1275
1276 curwin->w_cursor = oap->start;
1277 check_cursor();
1278 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1279
Bram Moolenaare1004402020-10-24 20:49:43 +02001280 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001281 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001282 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001283 curbuf->b_op_start = oap->start;
1284 curbuf->b_op_end = oap->end;
1285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
1287 return OK;
1288}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001290static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292/*
1293 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1294 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001296op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001300 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
1302 if (u_save((linenr_T)(oap->start.lnum - 1),
1303 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1304 return;
1305
1306 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001307 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
1309 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1310 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001311 int one_change;
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 block_prep(oap, &bd, pos.lnum, FALSE);
1314 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001315 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1316 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001317
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001318#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001319 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001321 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
Bram Moolenaar009b2592004-10-24 19:18:58 +00001323 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1324 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001325 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001326 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001328 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 if (did_change)
1333 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1334 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001335 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337 if (oap->motion_type == MLINE)
1338 {
1339 oap->start.col = 0;
1340 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001341 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (oap->end.col)
1343 --oap->end.col;
1344 }
1345 else if (!oap->inclusive)
1346 dec(&(oap->end));
1347
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001348 if (pos.lnum == oap->end.lnum)
1349 did_change = swapchars(oap->op_type, &pos,
1350 oap->end.col - pos.col + 1);
1351 else
1352 for (;;)
1353 {
1354 did_change |= swapchars(oap->op_type, &pos,
zeertzjq94b7c322024-03-12 21:50:32 +01001355 pos.lnum == oap->end.lnum ? oap->end.col + 1
1356 : ml_get_pos_len(&pos));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001357 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001358 break;
1359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (did_change)
1361 {
1362 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1363 0L);
1364#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001365 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
1367 char_u *ptr;
1368 int count;
1369
1370 pos = oap->start;
1371 while (pos.lnum < oap->end.lnum)
1372 {
1373 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001374 count = ml_get_buf_len(curbuf, pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001375 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001376 // get the line again, it may have been flushed
1377 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001379 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 pos.col = 0;
1381 pos.lnum++;
1382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001384 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001385 // get the line again, it may have been flushed
1386 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001388 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390#endif
1391 }
1392 }
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001395 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001396 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
Bram Moolenaare1004402020-10-24 20:49:43 +02001398 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001399 {
1400 // Set '[ and '] marks.
1401 curbuf->b_op_start = oap->start;
1402 curbuf->b_op_end = oap->end;
1403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001406 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001407 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408}
1409
1410/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001411 * Invoke swapchar() on "length" bytes at position "pos".
1412 * "pos" is advanced to just after the changed characters.
1413 * "length" is rounded up to include the whole last multi-byte character.
1414 * Also works correctly when the number of bytes changes.
1415 * Returns TRUE if some character was changed.
1416 */
1417 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001418swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001419{
1420 int todo;
1421 int did_change = 0;
1422
1423 for (todo = length; todo > 0; --todo)
1424 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001425 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001426 {
1427 int len = (*mb_ptr2len)(ml_get_pos(pos));
1428
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001429 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001430 if (len > 0)
1431 todo -= len - 1;
1432 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001433 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001434 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001435 break;
1436 }
1437 return did_change;
1438}
1439
1440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 * If op_type == OP_UPPER: make uppercase,
1442 * if op_type == OP_LOWER: make lowercase,
1443 * if op_type == OP_ROT13: do rot13 encoding,
1444 * else swap case of character at 'pos'
1445 * returns TRUE when something actually changed.
1446 */
1447 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001448swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 int c;
1451 int nc;
1452
1453 c = gchar_pos(pos);
1454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001455 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (c >= 0x80 && op_type == OP_ROT13)
1457 return FALSE;
1458
glepnirbd1232a2024-02-12 22:14:53 +01001459 // ~ is OP_NOP, g~ is OP_TILDE, gU is OP_UPPER
1460 if ((op_type == OP_UPPER || op_type == OP_NOP || op_type == OP_TILDE)
1461 && c == 0xdf
1462 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001463 {
1464 pos_T sp = curwin->w_cursor;
1465
glepnirbd1232a2024-02-12 22:14:53 +01001466 // Special handling for lowercase German sharp s (ß): convert to uppercase (ẞ).
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001467 curwin->w_cursor = *pos;
1468 del_char(FALSE);
glepnirbd1232a2024-02-12 22:14:53 +01001469 ins_char(0x1E9E);
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001470 curwin->w_cursor = sp;
glepnirbd1232a2024-02-12 22:14:53 +01001471 return TRUE;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001472 }
1473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001474 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 nc = c;
1477 if (MB_ISLOWER(c))
1478 {
1479 if (op_type == OP_ROT13)
1480 nc = ROT13(c, 'a');
1481 else if (op_type != OP_LOWER)
1482 nc = MB_TOUPPER(c);
1483 }
1484 else if (MB_ISUPPER(c))
1485 {
1486 if (op_type == OP_ROT13)
1487 nc = ROT13(c, 'A');
1488 else if (op_type != OP_UPPER)
1489 nc = MB_TOLOWER(c);
1490 }
1491 if (nc != c)
1492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1494 {
1495 pos_T sp = curwin->w_cursor;
1496
1497 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001499 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 ins_char(nc);
1501 curwin->w_cursor = sp;
1502 }
1503 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001504 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 return TRUE;
1506 }
1507 return FALSE;
1508}
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
1511 * op_insert - Insert and append operators for Visual mode.
1512 */
1513 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001514op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
zeertzjq8c55d602024-03-13 20:42:26 +01001516 long pre_textlen = 0;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001517 colnr_T ind_pre_col = 0, ind_post_col;
1518 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 struct block_def bd;
1520 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001521 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001522 pos_T start_insert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001524 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1526
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001527 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001529 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
1531 if (oap->block_mode)
1532 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001533 // When 'virtualedit' is used, need to insert the extra spaces before
1534 // doing block_prep(). When only "block" is used, virtual edit is
1535 // already disabled, but still need it when calling
1536 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001537 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001538 // state for the current window. To override that state, we need to
1539 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (curwin->w_cursor.coladd > 0)
1541 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001542 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (u_save_cursor() == FAIL)
1545 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001546
Gary Johnson51ad8502021-08-03 18:33:08 +02001547 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 coladvance_force(oap->op_type == OP_APPEND
1549 ? oap->end_vcol + 1 : getviscol());
1550 if (oap->op_type == OP_APPEND)
1551 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001552 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001554 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001556 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001557 ind_pre_col = (colnr_T)getwhitecols_curline();
1558 ind_pre_vcol = get_indent();
zeertzjq94b7c322024-03-12 21:50:32 +01001559 pre_textlen = ml_get_len(oap->start.lnum) - bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (oap->op_type == OP_APPEND)
zeertzjq94b7c322024-03-12 21:50:32 +01001561 pre_textlen -= bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
1563
1564 if (oap->op_type == OP_APPEND)
1565 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001566 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 curwin->w_set_curswant = TRUE;
1570 while (*ml_get_cursor() != NUL
1571 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1572 ++curwin->w_cursor.col;
1573 if (bd.is_short && !bd.is_MAX)
1574 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001575 // First line was too short, make it longer and adjust the
1576 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (u_save_cursor() == FAIL)
1578 return;
1579 for (i = 0; i < bd.endspaces; ++i)
1580 ins_char(' ');
1581 bd.textlen += bd.endspaces;
1582 }
1583 }
1584 else
1585 {
1586 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001587 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001589 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001590 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 && oap->start_vcol != oap->end_vcol)
1592 inc_cursor();
1593 }
1594 }
1595
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001596 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001597 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001598 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001600 // When a tab was inserted, and the characters in front of the tab
1601 // have been converted to a tab as well, the column of the cursor
1602 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001603 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001604 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001605 oap->start = curbuf->b_op_start_orig;
1606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 // If user has moved off this line, we don't know what to do, so do
1608 // nothing.
1609 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001610 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 return;
1612
1613 if (oap->block_mode)
1614 {
Christian Brabandtd5c8c092024-05-08 22:17:19 +02001615 int ins_len;
zeertzjq8c55d602024-03-13 20:42:26 +01001616 char_u *firstline, *ins_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001618 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001619 size_t len;
Mike Williamsaca8f552024-04-07 18:26:22 +02001620 size_t add;
zeertzjq8c55d602024-03-13 20:42:26 +01001621 // offset when cursor was moved in insert mode
1622 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001624 // If indent kicked in, the firstline might have changed
1625 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001626 ind_post_col = (colnr_T)getwhitecols_curline();
1627 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001628 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001629 bd.textcol += ind_post_col - ind_pre_col;
1630 ind_post_vcol = get_indent();
1631 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001632 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001633 }
1634
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001635 // The user may have moved the cursor before inserting something, try
1636 // to adjust the block for that. But only do it, if the difference
1637 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001638 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1639 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001640 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001641 int t = getviscol2(curbuf->b_op_start_orig.col,
1642 curbuf->b_op_start_orig.coladd);
1643
zeertzjq7745f142022-02-15 11:48:22 +00001644 if (oap->op_type == OP_INSERT
1645 && oap->start.col + oap->start.coladd
1646 != curbuf->b_op_start_orig.col
1647 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001648 {
zeertzjq7745f142022-02-15 11:48:22 +00001649 oap->start.col = curbuf->b_op_start_orig.col;
1650 pre_textlen -= t - oap->start_vcol;
1651 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001652 }
zeertzjq7745f142022-02-15 11:48:22 +00001653 else if (oap->op_type == OP_APPEND
1654 && oap->start.col + oap->start.coladd
1655 >= curbuf->b_op_start_orig.col
1656 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001657 {
zeertzjq7745f142022-02-15 11:48:22 +00001658 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001659 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001660 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001661 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001662 oap->start_vcol = t;
1663 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001664 }
1665 }
1666
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001667 // Spaces and tabs in the indent may have changed to other spaces and
1668 // tabs. Get the starting column again and correct the length.
1669 // Don't do this when "$" used, end-of-line will have changed.
1670 //
1671 // if indent was added and the inserted text was after the indent,
1672 // correct the selection for the new indent.
1673 if (did_indent && bd.textcol - ind_post_col > 0)
1674 {
1675 oap->start.col += ind_post_col - ind_pre_col;
1676 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1677 oap->end.col += ind_post_col - ind_pre_col;
1678 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001681 if (did_indent && bd.textcol - ind_post_col > 0)
1682 {
1683 // undo for where "oap" is used below
1684 oap->start.col -= ind_post_col - ind_pre_col;
1685 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1686 oap->end.col -= ind_post_col - ind_pre_col;
1687 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1690 {
1691 if (oap->op_type == OP_APPEND)
1692 {
1693 pre_textlen += bd2.textlen - bd.textlen;
1694 if (bd2.endspaces)
1695 --bd2.textlen;
1696 }
1697 bd.textcol = bd2.textcol;
1698 bd.textlen = bd2.textlen;
1699 }
1700
1701 /*
1702 * Subsequent calls to ml_get() flush the firstline data - take a
1703 * copy of the required string.
1704 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001705 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001706 len = ml_get_len(oap->start.lnum);
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001707 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001709 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001710 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001711 // account for pressing cursor in insert mode when '$' was used
1712 if (bd.is_MAX
1713 && (start_insert.lnum == Insstart.lnum
1714 && start_insert.col > Insstart.col))
1715 {
1716 offset = (start_insert.col - Insstart.col);
1717 add -= offset;
1718 if (oap->end_vcol > offset)
1719 oap->end_vcol -= (offset + 1);
1720 else
1721 // moved outside of the visual block, what to do?
1722 return;
1723 }
1724 }
Mike Williamsaca8f552024-04-07 18:26:22 +02001725 if (add > len)
zeertzjq94b7c322024-03-12 21:50:32 +01001726 add = len; // short line, point to the NUL
1727 firstline += add;
1728 len -= add;
Mike Williams16b63bd2024-06-01 11:33:40 +02001729 if (pre_textlen >= 0 && (ins_len = (int)len - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001731 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (ins_text != NULL)
1733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001734 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (u_save(oap->start.lnum,
1736 (linenr_T)(oap->end.lnum + 1)) == OK)
John Marriott38b9f452024-04-25 21:39:18 +02001737 block_insert(oap, ins_text, ins_len, (oap->op_type == OP_INSERT),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 &bd);
1739
1740 curwin->w_cursor.col = oap->start.col;
1741 check_cursor();
1742 vim_free(ins_text);
1743 }
1744 }
1745 }
1746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748/*
1749 * op_change - handle a change operation
1750 *
1751 * return TRUE if edit() returns because of a CTRL-O command
1752 */
1753 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001754op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
1756 colnr_T l;
1757 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001759 long pre_textlen = 0;
1760 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 char_u *firstline;
1762 char_u *ins_text, *newp, *oldp;
1763 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765 l = oap->start.col;
1766 if (oap->motion_type == MLINE)
1767 {
1768 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001769 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 }
1771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001772 // First delete the text in the region. In an empty buffer only need to
1773 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1775 {
1776 if (u_save_cursor() == FAIL)
1777 return FALSE;
1778 }
1779 else if (op_delete(oap) == FAIL)
1780 return FALSE;
1781
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001782 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 && !virtual_op)
1784 inc_cursor();
1785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001786 // check for still on same line (<CR> in inserted text meaningless)
1787 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (oap->block_mode)
1789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001790 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (virtual_op && (curwin->w_cursor.coladd > 0
1792 || gchar_cursor() == NUL))
1793 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001794 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001795 pre_textlen = ml_get_len(oap->start.lnum);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001796 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 bd.textcol = curwin->w_cursor.col;
1798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (oap->motion_type == MLINE)
1801 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
zeertzjqcdeb6572022-11-15 13:46:12 +00001803 // Reset finish_op now, don't want it set inside edit().
1804 int save_finish_op = finish_op;
1805 finish_op = FALSE;
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 retval = edit(NUL, FALSE, (linenr_T)1);
1808
zeertzjqcdeb6572022-11-15 13:46:12 +00001809 finish_op = save_finish_op;
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001812 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001814 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001816 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
Christian Brabandt1fb9eae2024-06-11 20:30:14 +02001818 int ins_len;
John Marriott38b9f452024-04-25 21:39:18 +02001819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 // Auto-indenting may have changed the indent. If the cursor was past
1821 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001823 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001825 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001826
1827 pre_textlen += new_indent - pre_indent;
1828 bd.textcol += new_indent - pre_indent;
1829 }
1830
Christian Brabandt1fb9eae2024-06-11 20:30:14 +02001831 ins_len = (int)ml_get_len(oap->start.lnum) - pre_textlen;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001832 if (ins_len > 0)
1833 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001834 // Subsequent calls to ml_get() flush the firstline data - take a
1835 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001836 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
John Marriott38b9f452024-04-25 21:39:18 +02001838 vim_strncpy(ins_text, firstline + bd.textcol, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1840 linenr++)
1841 {
1842 block_prep(oap, &bd, linenr, TRUE);
1843 if (!bd.is_short || virtual_op)
1844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 pos_T vpos;
John Marriott38b9f452024-04-25 21:39:18 +02001846 size_t newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001848 // If the block starts in virtual space, count the
1849 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (bd.is_short)
1851 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001852 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855 else
1856 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 oldp = ml_get(linenr);
John Marriott38b9f452024-04-25 21:39:18 +02001858 newp = alloc(ml_get_len(linenr) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (newp == NULL)
1860 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001861 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001863 newlen = bd.textcol;
1864 vim_memset(newp + newlen, ' ', (size_t)vpos.coladd);
1865 newlen += vpos.coladd;
1866 mch_memmove(newp + newlen, ins_text, ins_len);
1867 STRCPY(newp + newlen + ins_len, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001869#ifdef FEAT_PROP_POPUP
1870 // Shift the properties for linenr as edit() would do.
1871 if (curbuf->b_has_textprop)
1872 adjust_prop_columns(linenr, bd.textcol,
Mike Williams16b63bd2024-06-01 11:33:40 +02001873 vpos.coladd + (int)ins_len, 0);
LemonBoyb559b302022-05-15 13:08:02 +01001874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 }
1877 check_cursor();
1878
1879 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1880 }
1881 vim_free(ins_text);
1882 }
1883 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001884 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 return retval;
1887}
1888
1889/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001890 * When the cursor is on the NUL past the end of the line and it should not be
1891 * there move it left.
1892 */
1893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001894adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001895{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001896 unsigned int cur_ve_flags = get_ve_flags();
1897
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001898 int adj_cursor = (curwin->w_cursor.col > 0
1899 && gchar_cursor() == NUL
1900 && (cur_ve_flags & VE_ONEMORE) == 0
1901 && !(restart_edit || (State & MODE_INSERT)));
1902 if (!adj_cursor)
1903 return;
1904
1905 // Put the cursor on the last character in the line.
1906 dec_cursor();
1907
1908 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001910 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001911
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001912 // Coladd is set to the width of the last character.
1913 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1914 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916}
1917
Bram Moolenaar81340392012-06-06 16:12:59 +02001918/*
1919 * If "process" is TRUE and the line begins with a comment leader (possibly
1920 * after some white space), return a pointer to the text after it. Put a boolean
1921 * value indicating whether the line ends with an unclosed comment in
1922 * "is_comment".
1923 * line - line to be processed,
1924 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001925 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001926 * include_space - whether to also skip space following the comment leader,
1927 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001928 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001929 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001930 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001931skip_comment(
1932 char_u *line,
1933 int process,
1934 int include_space,
1935 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001936{
1937 char_u *comment_flags = NULL;
1938 int lead_len;
1939 int leader_offset = get_last_leader_offset(line, &comment_flags);
1940
1941 *is_comment = FALSE;
1942 if (leader_offset != -1)
1943 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001944 // Let's check whether the line ends with an unclosed comment.
1945 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001946 while (*comment_flags)
1947 {
1948 if (*comment_flags == COM_END
1949 || *comment_flags == ':')
1950 break;
1951 ++comment_flags;
1952 }
1953 if (*comment_flags != COM_END)
1954 *is_comment = TRUE;
1955 }
1956
1957 if (process == FALSE)
1958 return line;
1959
1960 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1961
1962 if (lead_len == 0)
1963 return line;
1964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001965 // Find:
1966 // - COM_END,
1967 // - colon,
1968 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001969 while (*comment_flags)
1970 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001971 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001972 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001973 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001974 ++comment_flags;
1975 }
1976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001977 // If we found a colon, it means that we are not processing a line
1978 // starting with a closing part of a three-part comment. That's good,
1979 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001980 if (*comment_flags == ':' || *comment_flags == NUL)
1981 line += lead_len;
1982
1983 return line;
1984}
Bram Moolenaar81340392012-06-06 16:12:59 +02001985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001987 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001988 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001989 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1990 * leaders should not be removed.
1991 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1992 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001994 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 */
1996 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001997do_join(
1998 long count,
1999 int insert_space,
2000 int save_undo,
2001 int use_formatoptions UNUSED,
2002 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002004 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002005 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002006 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002008 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002009 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02002010 int endcurr1 = NUL;
2011 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002012 int currsize = 0; // size of the current line
2013 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002015 colnr_T col = 0;
2016 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02002017 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002018 int remove_comments = (use_formatoptions == TRUE)
2019 && has_format_option(FO_REMOVE_COMS);
2020 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002021#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002022 int propcount = 0; // number of props over all joined lines
2023 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002026 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2027 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2028 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002030 // Allocate an array to store the number of spaces inserted before each
2031 // line. We will use it to pre-compute the length of the new line and the
2032 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002033 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002034 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002036 if (remove_comments)
2037 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002038 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002039 if (comments == NULL)
2040 {
2041 vim_free(spaces);
2042 return FAIL;
2043 }
2044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002047 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002048 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002049 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002051 for (t = 0; t < count; ++t)
2052 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002053 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002054#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002055 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2056 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002057#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002058 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002059 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002060 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002061 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2062 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2063 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002064 if (remove_comments)
2065 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002066 // We don't want to remove the comment leader if the
2067 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002068 if (t > 0 && prev_was_comment)
2069 {
2070
2071 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2072 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002073 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002074 curr = new_curr;
2075 }
2076 else
2077 curr = skip_comment(curr, FALSE, insert_space,
2078 &prev_was_comment);
2079 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002080
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002081 if (insert_space && t > 0)
2082 {
2083 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002084 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002085 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002086 && (!has_format_option(FO_MBYTE_JOIN)
2087 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2088 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002089 || (mb_ptr2char(curr) < 0x100
2090 && !(enc_utf8 && utf_eat_space(endcurr1)))
2091 || (endcurr1 < 0x100
2092 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002093 )
2094 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002095 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002096 if (endcurr1 == ' ')
2097 endcurr1 = endcurr2;
2098 else
2099 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002100 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002101 if ( p_js
2102 && (endcurr1 == '.'
2103 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2104 && (endcurr1 == '?' || endcurr1 == '!'))))
2105 ++spaces[t];
2106 }
2107 }
2108 currsize = (int)STRLEN(curr);
2109 sumsize += currsize + spaces[t];
2110 endcurr1 = endcurr2 = NUL;
2111 if (insert_space && currsize > 0)
2112 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002113 if (has_mbyte)
2114 {
2115 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002116 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002117 endcurr1 = (*mb_ptr2char)(cend);
2118 if (cend > curr)
2119 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002120 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002121 endcurr2 = (*mb_ptr2char)(cend);
2122 }
2123 }
2124 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002125 {
2126 endcurr1 = *(curr + currsize - 1);
2127 if (currsize > 1)
2128 endcurr2 = *(curr + currsize - 2);
2129 }
2130 }
2131 line_breakcheck();
2132 if (got_int)
2133 {
2134 ret = FAIL;
2135 goto theend;
2136 }
2137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002139 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002140 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002142 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002143 newp_len = sumsize + 1;
2144#ifdef FEAT_PROP_POPUP
2145 newp_len += propcount * sizeof(textprop_T);
2146#endif
2147 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002148 if (newp == NULL)
2149 {
2150 ret = FAIL;
2151 goto theend;
2152 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002153 cend = newp + sumsize;
2154 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002156 /*
2157 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002158 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002159 *
2160 * Move marks from each deleted line to the joined line, adjusting the
2161 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2162 * should not really be a problem.
2163 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002164#ifdef FEAT_PROP_POPUP
2165 props_remaining = propcount;
2166#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002167 for (t = count - 1; ; --t)
2168 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002169 int spaces_removed;
2170
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002171 cend -= currsize;
2172 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002173
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002174 if (spaces[t] > 0)
2175 {
2176 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002177 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002178 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002179
2180 // If deleting more spaces than adding, the cursor moves no more than
2181 // what is added if it is inside these spaces.
2182 spaces_removed = (curr - curr_start) - spaces[t];
2183
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002184 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002185 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002186#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002187 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2188 curwin->w_cursor.lnum + t, t == count - 1,
2189 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002190#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002191 if (t == 0)
2192 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002193 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002194 if (remove_comments)
2195 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002196 if (insert_space && t > 1)
2197 curr = skipwhite(curr);
2198 currsize = (int)STRLEN(curr);
2199 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002200
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002201 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
Bram Moolenaare1004402020-10-24 20:49:43 +02002203 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002204 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002205 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002206 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002207 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002208 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002210 // Only report the change in the first line here, del_lines() will report
2211 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 changed_lines(curwin->w_cursor.lnum, currsize,
2213 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002215 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 * briefly, and then move it back. After del_lines() the cursor may
2217 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 */
2219 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002221 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 curwin->w_cursor.lnum = t;
2223
2224 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002225 * Set the cursor column:
2226 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002227 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002229 curwin->w_cursor.col =
2230 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 curwin->w_set_curswant = TRUE;
2235
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002236theend:
2237 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002238 if (remove_comments)
2239 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002240 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241}
2242
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002243#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002245 * Reset 'linebreak' and take care of side effects.
2246 * Returns the previous value, to be passed to restore_lbr().
2247 */
2248 static int
2249reset_lbr(void)
2250{
2251 if (!curwin->w_p_lbr)
2252 return FALSE;
2253 // changing 'linebreak' may require w_virtcol to be updated
2254 curwin->w_p_lbr = FALSE;
2255 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2256 return TRUE;
2257}
2258
2259/*
2260 * Restore 'linebreak' and take care of side effects.
2261 */
2262 static void
2263restore_lbr(int lbr_saved)
2264{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002265 if (curwin->w_p_lbr || !lbr_saved)
2266 return;
2267
2268 // changing 'linebreak' may require w_virtcol to be updated
2269 curwin->w_p_lbr = TRUE;
2270 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002271}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002272#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002273
2274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 * prepare a few things for block mode yank/delete/tilde
2276 *
2277 * for delete:
2278 * - textlen includes the first/last char to be (partly) deleted
2279 * - start/endspaces is the number of columns that are taken by the
2280 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002281 * deleted.
2282 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 * - textlen includes the first/last char to be wholly yanked
2284 * - start/endspaces is the number of columns of the first/last yanked char
2285 * that are to be yanked.
2286 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002287 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002288block_prep(
2289 oparg_T *oap,
2290 struct block_def *bdp,
2291 linenr_T lnum,
2292 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 int incr = 0;
2295 char_u *pend;
2296 char_u *pstart;
2297 char_u *line;
2298 char_u *prev_pstart;
2299 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002300 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002301#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002302 // Avoid a problem with unwanted linebreaks in block mode.
2303 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002304#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 bdp->startspaces = 0;
2307 bdp->endspaces = 0;
2308 bdp->textlen = 0;
2309 bdp->start_vcol = 0;
2310 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 bdp->is_short = FALSE;
2312 bdp->is_oneChar = FALSE;
2313 bdp->pre_whitesp = 0;
2314 bdp->pre_whitesp_c = 0;
2315 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 bdp->start_char_vcols = 0;
2317
2318 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002320 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2321 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002323 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002324 incr = lbr_chartabsize(&cts);
2325 cts.cts_vcol += incr;
2326 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 bdp->pre_whitesp += incr;
2329 bdp->pre_whitesp_c++;
2330 }
2331 else
2332 {
2333 bdp->pre_whitesp = 0;
2334 bdp->pre_whitesp_c = 0;
2335 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002336 prev_pstart = cts.cts_ptr;
2337 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002339 bdp->start_vcol = cts.cts_vcol;
2340 pstart = cts.cts_ptr;
2341 clear_chartabsize_arg(&cts);
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002344 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (!is_del || oap->op_type == OP_APPEND)
2349 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2350 }
2351 else
2352 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 // notice: this converts partly selected Multibyte characters to
2354 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2356 if (is_del && bdp->startspaces)
2357 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2358 pend = pstart;
2359 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002360 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (oap->op_type == OP_INSERT)
2364 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2365 else if (oap->op_type == OP_APPEND)
2366 {
2367 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2368 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2369 }
2370 else
2371 {
2372 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2373 if (is_del && oap->op_type != OP_LSHIFT)
2374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 // just putting the sum of those two into
2376 // bdp->startspaces doesn't work for Visual replace,
2377 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 bdp->startspaces = bdp->start_char_vcols
2379 - (bdp->start_vcol - oap->start_vcol);
2380 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2381 }
2382 }
2383 }
2384 else
2385 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002386 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2387 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002389 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002391 // count a tab for what it's worth (if list mode not on)
2392 prev_pend = cts.cts_ptr;
2393 incr = lbr_chartabsize_adv(&cts);
2394 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002396 bdp->end_vcol = cts.cts_vcol;
2397 pend = cts.cts_ptr;
2398 clear_chartabsize_arg(&cts);
2399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (bdp->end_vcol <= oap->end_vcol
2401 && (!is_del
2402 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002403 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002406 // Alternative: include spaces to fill up the block.
2407 // Disadvantage: can lead to trailing spaces when the line is
2408 // short where the text is put
2409 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (oap->op_type == OP_APPEND || virtual_op)
2411 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002412 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002414 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 else if (bdp->end_vcol > oap->end_vcol)
2417 {
2418 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2419 if (!is_del && bdp->endspaces)
2420 {
2421 bdp->endspaces = incr - bdp->endspaces;
2422 if (pend != pstart)
2423 pend = prev_pend;
2424 }
2425 }
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 if (is_del && bdp->startspaces)
2429 pstart = prev_pstart;
2430 bdp->textlen = (int)(pend - pstart);
2431 }
2432 bdp->textcol = (colnr_T) (pstart - line);
2433 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002434#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002435 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439/*
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002440 * Get block text from "start" to "end"
2441 */
2442 void
2443charwise_block_prep(
2444 pos_T start,
2445 pos_T end,
2446 struct block_def *bdp,
2447 linenr_T lnum,
2448 int inclusive)
2449{
2450 colnr_T startcol = 0, endcol = MAXCOL;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002451 colnr_T cs, ce;
2452 char_u *p;
2453
2454 p = ml_get(lnum);
2455 bdp->startspaces = 0;
2456 bdp->endspaces = 0;
zeertzjq52a6f342024-05-22 16:42:44 +02002457 bdp->is_oneChar = FALSE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002458 bdp->start_char_vcols = 0;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002459
2460 if (lnum == start.lnum)
2461 {
2462 startcol = start.col;
2463 if (virtual_op)
2464 {
2465 getvcol(curwin, &start, &cs, NULL, &ce);
2466 if (ce != cs && start.coladd > 0)
2467 {
2468 // Part of a tab selected -- but don't
2469 // double-count it.
zeertzjqc95e64f2024-05-20 14:00:31 +02002470 bdp->start_char_vcols = ce - cs + 1;
2471 bdp->startspaces = bdp->start_char_vcols - start.coladd;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002472 if (bdp->startspaces < 0)
2473 bdp->startspaces = 0;
2474 startcol++;
2475 }
2476 }
2477 }
2478
2479 if (lnum == end.lnum)
2480 {
2481 endcol = end.col;
2482 if (virtual_op)
2483 {
2484 getvcol(curwin, &end, &cs, NULL, &ce);
2485 if (p[endcol] == NUL || (cs + end.coladd < ce
2486 // Don't add space for double-wide
2487 // char; endcol will be on last byte
2488 // of multi-byte char.
2489 && (*mb_head_off)(p, p + endcol) == 0))
2490 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002491 if (start.lnum == end.lnum && start.col == end.col)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002492 {
2493 // Special case: inside a single char
zeertzjq52a6f342024-05-22 16:42:44 +02002494 bdp->is_oneChar = TRUE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002495 bdp->startspaces = end.coladd - start.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002496 endcol = startcol;
2497 }
2498 else
2499 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002500 bdp->endspaces = end.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002501 endcol -= inclusive;
2502 }
2503 }
2504 }
2505 }
2506 if (endcol == MAXCOL)
zeertzjq94b7c322024-03-12 21:50:32 +01002507 endcol = ml_get_len(lnum);
zeertzjq52a6f342024-05-22 16:42:44 +02002508 if (startcol > endcol || bdp->is_oneChar)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002509 bdp->textlen = 0;
2510 else
2511 bdp->textlen = endcol - startcol + inclusive;
zeertzjqc95e64f2024-05-20 14:00:31 +02002512 bdp->textcol = startcol;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002513 bdp->textstart = p + startcol;
2514}
2515
2516/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002517 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002519 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002520op_addsub(
2521 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002522 linenr_T Prenum1, // Amount of add/subtract
2523 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002525 pos_T pos;
2526 struct block_def bd;
2527 int change_cnt = 0;
2528 linenr_T amount = Prenum1;
2529
Gary Johnson88d4f252024-06-01 20:51:33 +02002530 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
2531 // buffer is not completely updated yet. Postpone updating folds until before
2532 // the call to changed_lines().
Bram Moolenaar6b731882018-11-16 20:54:47 +01002533#ifdef FEAT_FOLDING
Gary Johnson88d4f252024-06-01 20:51:33 +02002534 disable_fold_update++;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002535#endif
2536
Bram Moolenaard79e5502016-01-10 22:13:02 +01002537 if (!VIsual_active)
2538 {
2539 pos = curwin->w_cursor;
2540 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002541 {
2542#ifdef FEAT_FOLDING
2543 disable_fold_update--;
2544#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002545 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002546 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002547 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002548#ifdef FEAT_FOLDING
2549 disable_fold_update--;
2550#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002551 if (change_cnt)
2552 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2553 }
2554 else
2555 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002556 int one_change;
2557 int length;
2558 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002559
2560 if (u_save((linenr_T)(oap->start.lnum - 1),
2561 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002562 {
2563#ifdef FEAT_FOLDING
2564 disable_fold_update--;
2565#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002566 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002567 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002568
2569 pos = oap->start;
2570 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002572 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002573 {
2574 block_prep(oap, &bd, pos.lnum, FALSE);
2575 pos.col = bd.textcol;
2576 length = bd.textlen;
2577 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002578 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002579 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002580 curwin->w_cursor.col = 0;
2581 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01002582 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002583 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002584 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002585 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002586 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002587 dec(&(oap->end));
zeertzjq94b7c322024-03-12 21:50:32 +01002588 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002589 pos.col = 0;
2590 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002591 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002592 pos.col += oap->start.col;
2593 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002594 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002595 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002596 {
zeertzjq94b7c322024-03-12 21:50:32 +01002597 length = ml_get_len(oap->end.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002598 if (oap->end.col >= length)
2599 oap->end.col = length - 1;
2600 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002601 }
2602 }
2603 one_change = do_addsub(oap->op_type, &pos, length, amount);
2604 if (one_change)
2605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002606 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002607 if (change_cnt == 0)
2608 startpos = curbuf->b_op_start;
2609 ++change_cnt;
2610 }
2611
2612#ifdef FEAT_NETBEANS_INTG
2613 if (netbeans_active() && one_change)
2614 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002615 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002616
2617 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002618 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002619 netbeans_inserted(curbuf, pos.lnum, pos.col,
2620 &ptr[pos.col], length);
2621 }
2622#endif
2623 if (g_cmd && one_change)
2624 amount += Prenum1;
2625 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002626
2627#ifdef FEAT_FOLDING
2628 disable_fold_update--;
2629#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002630 if (change_cnt)
2631 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2632
2633 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002634 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002635 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 // Set '[ mark if something changed. Keep the last end
2638 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002639 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002640 curbuf->b_op_start = startpos;
2641
2642 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002643 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002644 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002645 }
2646}
2647
2648/*
2649 * Add or subtract 'Prenum1' from a number in a line
2650 * op_type is OP_NR_ADD or OP_NR_SUB
2651 *
2652 * Returns TRUE if some character was changed.
2653 */
2654 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002655do_addsub(
2656 int op_type,
2657 pos_T *pos,
2658 int length,
2659 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002660{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 int col;
2662 char_u *buf1;
2663 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2665 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002666 uvarnumber_T n;
2667 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 char_u *ptr;
zeertzjq8c55d602024-03-13 20:42:26 +01002669 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002672 int do_hex;
2673 int do_oct;
2674 int do_bin;
2675 int do_alpha;
2676 int do_unsigned;
distobs25ac6d62024-07-06 17:50:09 +02002677 int do_blank;
2678 int blank_unsigned = FALSE; // blank: treat as unsigned?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002681 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002682 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002683 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002684 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002685 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002686 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002687 pos_T startpos;
2688 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002689 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002691 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2692 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2693 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2694 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2695 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
distobs25ac6d62024-07-06 17:50:09 +02002696 do_blank = (vim_strchr(curbuf->b_p_nf, 'k') != NULL); // "blanK"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002698 if (virtual_active())
2699 {
2700 save_coladd = pos->coladd;
2701 pos->coladd = 0;
2702 }
2703
Bram Moolenaard79e5502016-01-10 22:13:02 +01002704 curwin->w_cursor = *pos;
2705 ptr = ml_get(pos->lnum);
zeertzjq8c55d602024-03-13 20:42:26 +01002706 linelen = ml_get_len(pos->lnum);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002707 col = pos->col;
2708
zeertzjq8c55d602024-03-13 20:42:26 +01002709 if (col + !!save_coladd >= linelen)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002710 goto theend;
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 /*
2713 * First check if we are on a hexadecimal number, after the "0x".
2714 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002715 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002717 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002718 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002719 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002720 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002721 if (has_mbyte)
2722 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002723 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002724
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002725 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002726 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002727 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002728 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002729 if (has_mbyte)
2730 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002731 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002732
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002733 if ( do_bin
2734 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002735 && ! ((col > 0
2736 && (ptr[col] == 'X'
2737 || ptr[col] == 'x')
2738 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002739 && (!has_mbyte ||
2740 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002741 && vim_isxdigit(ptr[col + 1]))))
2742 {
2743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002745
Bram Moolenaard79e5502016-01-10 22:13:02 +01002746 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002747
2748 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002749 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002750 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002751 if (has_mbyte)
2752 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002753 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002754 }
2755
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002756 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002757 && col > 0
2758 && (ptr[col] == 'X'
2759 || ptr[col] == 'x')
2760 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002761 && (!has_mbyte ||
2762 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002763 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002764 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002765 && col > 0
2766 && (ptr[col] == 'B'
2767 || ptr[col] == 'b')
2768 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002769 && (!has_mbyte ||
2770 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002771 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002774 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002775 if (has_mbyte)
2776 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002777 }
2778 else
2779 {
2780 /*
2781 * Search forward and then backward to find the start of number.
2782 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002783 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002784
2785 while (ptr[col] != NUL
2786 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002787 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002788 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002789
2790 while (col > 0
2791 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002792 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002793 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002794 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002795 if (has_mbyte)
2796 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002797 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002798 }
2799 }
2800
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002801 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002802 {
2803 while (ptr[col] != NUL && length > 0
2804 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002805 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002806 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002807 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002808
2809 col += mb_len;
2810 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002811 }
2812
2813 if (length == 0)
2814 goto theend;
2815
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002816 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002817 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2818 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002819 {
distobs25ac6d62024-07-06 17:50:09 +02002820 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
2821 blank_unsigned = TRUE;
2822 else
2823 {
2824 negative = TRUE;
2825 was_positive = FALSE;
2826 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002827 }
2828 }
2829
2830 /*
2831 * If a number was found, and saving for undo works, replace the number.
2832 */
2833 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002834 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002835 {
2836 beep_flush();
2837 goto theend;
2838 }
2839
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002840 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002841 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002843 if (op_type == OP_NR_SUB)
2844 {
2845 if (CharOrd(firstdigit) < Prenum1)
2846 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002847 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002848 firstdigit = 'A';
2849 else
2850 firstdigit = 'a';
2851 }
2852 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002853 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002854 }
2855 else
2856 {
2857 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2858 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002859 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002860 firstdigit = 'Z';
2861 else
2862 firstdigit = 'z';
2863 }
2864 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002865 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002866 }
2867 curwin->w_cursor.col = col;
2868 if (!did_change)
2869 startpos = curwin->w_cursor;
2870 did_change = TRUE;
2871 (void)del_char(FALSE);
2872 ins_char(firstdigit);
2873 endpos = curwin->w_cursor;
2874 curwin->w_cursor.col = col;
2875 }
2876 else
2877 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002878 pos_T save_pos;
2879 int i;
2880
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002881 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002882 && (!has_mbyte ||
2883 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002884 && !visual
2885 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002886 {
distobs25ac6d62024-07-06 17:50:09 +02002887 if (do_blank && col >= 2 && !VIM_ISWHITE(ptr[col - 2]))
2888 blank_unsigned = TRUE;
2889 else
2890 {
2891 // negative number
2892 --col;
2893 negative = TRUE;
2894 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002895 }
distobs25ac6d62024-07-06 17:50:09 +02002896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002898 if (visual && VIsual_mode != 'V')
2899 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
zeertzjq8c55d602024-03-13 20:42:26 +01002900 ? linelen - col : length);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002901
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002902 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002903 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002904 0 + (do_bin ? STR2NR_BIN : 0)
2905 + (do_oct ? STR2NR_OCT : 0)
2906 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002907 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002910 if (pre && negative)
2911 {
2912 ++col;
2913 --length;
2914 negative = FALSE;
2915 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002917 subtract = FALSE;
2918 if (op_type == OP_NR_SUB)
2919 subtract ^= TRUE;
2920 if (negative)
2921 subtract ^= TRUE;
2922
2923 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002924 if (!overflow) // if number is too big don't add/subtract
2925 {
2926 if (subtract)
2927 n -= (uvarnumber_T)Prenum1;
2928 else
2929 n += (uvarnumber_T)Prenum1;
2930 }
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002933 if (!pre)
2934 {
2935 if (subtract)
2936 {
2937 if (n > oldn)
2938 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002939 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002940 negative ^= TRUE;
2941 }
2942 }
2943 else
2944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002946 if (n < oldn)
2947 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002948 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002949 negative ^= TRUE;
2950 }
2951 }
2952 if (n == 0)
2953 negative = FALSE;
2954 }
2955
distobs25ac6d62024-07-06 17:50:09 +02002956 if ((do_unsigned || blank_unsigned) && negative)
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002957 {
2958 if (subtract)
2959 // sticking at zero.
2960 n = (uvarnumber_T)0;
2961 else
2962 // sticking at 2^64 - 1.
2963 n = (uvarnumber_T)(-1);
2964 negative = FALSE;
2965 }
2966
Bram Moolenaard79e5502016-01-10 22:13:02 +01002967 if (visual && !was_positive && !negative && col > 0)
2968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002970 col--;
2971 length++;
2972 }
2973
2974 /*
2975 * Delete the old number.
2976 */
2977 curwin->w_cursor.col = col;
2978 if (!did_change)
2979 startpos = curwin->w_cursor;
2980 did_change = TRUE;
2981 todel = length;
2982 c = gchar_cursor();
2983 /*
2984 * Don't include the '-' in the length, only the length of the
2985 * part after it is kept the same.
2986 */
2987 if (c == '-')
2988 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002989
2990 save_pos = curwin->w_cursor;
2991 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002992 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002993 if (c < 0x100 && SAFE_isalpha(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002994 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002995 if (SAFE_isupper(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002996 hexupper = TRUE;
2997 else
2998 hexupper = FALSE;
2999 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003000 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01003001 c = gchar_cursor();
3002 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003003 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003004
3005 /*
3006 * Prepare the leading characters in buf1[].
3007 * When there are many leading zeros it could be very long.
3008 * Allocate a bit too much.
3009 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003010 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003011 if (buf1 == NULL)
3012 goto theend;
3013 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003014 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003015 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003016 if (pre)
3017 {
3018 *ptr++ = '0';
3019 --length;
3020 }
3021 if (pre == 'b' || pre == 'B' ||
3022 pre == 'x' || pre == 'X')
3023 {
3024 *ptr++ = pre;
3025 --length;
3026 }
3027
3028 /*
3029 * Put the number characters in buf2[].
3030 */
3031 if (pre == 'b' || pre == 'B')
3032 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01003033 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003034 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003037 for (bit = bits; bit > 0; bit--)
3038 if ((n >> (bit - 1)) & 0x1) break;
3039
Christian Brabandt889f6af2023-09-02 19:43:33 +02003040 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003041 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3042
3043 buf2[i] = '\0';
3044 }
3045 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003046 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003047 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003048 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003049 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003050 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003051 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003052 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003053 length -= (int)STRLEN(buf2);
3054
3055 /*
3056 * Adjust number of zeros to the new number of digits, so the
3057 * total length of the number remains the same.
3058 * Don't do this when
3059 * the result may look like an octal number.
3060 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003061 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003062 while (length-- > 0)
3063 *ptr++ = '0';
3064 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003065
Bram Moolenaard79e5502016-01-10 22:13:02 +01003066 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003067
3068 // Insert just after the first character to be removed, so that any
3069 // text properties will be adjusted. Then delete the old number
3070 // afterwards.
3071 save_pos = curwin->w_cursor;
3072 if (todel > 0)
3073 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003074 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003075 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003076
3077 // del_char() will also mark line needing displaying
3078 if (todel > 0)
3079 {
zeertzjq94b7c322024-03-12 21:50:32 +01003080 int bytes_after = ml_get_curline_len() - curwin->w_cursor.col;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003081
3082 // Delete the one character before the insert.
3083 curwin->w_cursor = save_pos;
3084 (void)del_char(FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01003085 curwin->w_cursor.col = ml_get_curline_len() - bytes_after;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003086 --todel;
3087 }
3088 while (todel-- > 0)
3089 (void)del_char(FALSE);
3090
Bram Moolenaard79e5502016-01-10 22:13:02 +01003091 endpos = curwin->w_cursor;
3092 if (did_change && curwin->w_cursor.col)
3093 --curwin->w_cursor.col;
3094 }
3095
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003097 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003099 curbuf->b_op_start = startpos;
3100 curbuf->b_op_end = endpos;
3101 if (curbuf->b_op_end.col > 0)
3102 --curbuf->b_op_end.col;
3103 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003104
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003105theend:
3106 if (visual)
3107 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003108 else if (did_change)
3109 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003110 else if (virtual_active())
3111 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003112
Bram Moolenaard79e5502016-01-10 22:13:02 +01003113 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114}
3115
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003117clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003119 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120}
3121
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003123 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 *
3125 * "Words" are counted by looking for boundaries between non-space and
3126 * space characters. (it seems to produce results that match 'wc'.)
3127 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003128 * Return value is byte count; word count for the line is added to "*wc".
3129 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 *
3131 * The function will only examine the first "limit" characters in the
3132 * line, stopping if it encounters an end-of-line (NUL byte). In that
3133 * case, eol_size will be added to the character count to account for
3134 * the size of the EOL character.
3135 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003136 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003137line_count_info(
3138 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003139 varnumber_T *wc,
3140 varnumber_T *cc,
3141 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003142 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003144 varnumber_T i;
3145 varnumber_T words = 0;
3146 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int is_word = 0;
3148
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003149 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
3151 if (is_word)
3152 {
3153 if (vim_isspace(line[i]))
3154 {
3155 words++;
3156 is_word = 0;
3157 }
3158 }
3159 else if (!vim_isspace(line[i]))
3160 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003161 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003162 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164
3165 if (is_word)
3166 words++;
3167 *wc += words;
3168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003169 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003170 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003173 chars += eol_size;
3174 }
3175 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 return i;
3177}
3178
3179/*
3180 * Give some info about the position of the cursor (for "g CTRL-G").
3181 * In Visual mode, give some info about the selected region. (In this case,
3182 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003183 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 */
3185 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003186cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003189 char_u buf1[50];
3190 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003192 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003193 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003194 varnumber_T byte_count_cursor = 0;
3195 varnumber_T char_count = 0;
3196 varnumber_T char_count_cursor = 0;
3197 varnumber_T word_count = 0;
3198 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003199 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003200 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 long line_count_selected = 0;
3202 pos_T min_pos, max_pos;
3203 oparg_T oparg;
3204 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 /*
3207 * Compute the length of the file in characters.
3208 */
3209 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3210 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003211 if (dict == NULL)
3212 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003213 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003214 return;
3215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217 else
3218 {
3219 if (get_fileformat(curbuf) == EOL_DOS)
3220 eol_size = 2;
3221 else
3222 eol_size = 1;
3223
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (VIsual_active)
3225 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003226 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 min_pos = VIsual;
3229 max_pos = curwin->w_cursor;
3230 }
3231 else
3232 {
3233 min_pos = curwin->w_cursor;
3234 max_pos = VIsual;
3235 }
3236 if (*p_sel == 'e' && max_pos.col > 0)
3237 --max_pos.col;
3238
3239 if (VIsual_mode == Ctrl_V)
3240 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003241#ifdef FEAT_LINEBREAK
3242 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003243 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003245 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003246 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003247 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 oparg.is_VIsual = 1;
3250 oparg.block_mode = TRUE;
3251 oparg.op_type = OP_NOP;
3252 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003253 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003254#ifdef FEAT_LINEBREAK
3255 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003256 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003257#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003258 if (curwin->w_curswant == MAXCOL)
3259 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003260 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (oparg.end_vcol < oparg.start_vcol)
3262 {
3263 oparg.end_vcol += oparg.start_vcol;
3264 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3265 oparg.end_vcol -= oparg.start_vcol;
3266 }
3267 }
3268 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3272 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003273 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003274 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
3276 ui_breakcheck();
3277 if (got_int)
3278 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003279 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (VIsual_active
3284 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3285 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003286 char_u *s = NULL;
3287 long len = 0L;
3288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 switch (VIsual_mode)
3290 {
3291 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003295 s = bd.textstart;
3296 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 break;
3298 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003299 s = ml_get(lnum);
3300 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 break;
3302 case 'v':
3303 {
3304 colnr_T start_col = (lnum == min_pos.lnum)
3305 ? min_pos.col : 0;
3306 colnr_T end_col = (lnum == max_pos.lnum)
3307 ? max_pos.col - start_col + 1 : MAXCOL;
3308
Bram Moolenaardef9e822004-12-31 20:58:58 +00003309 s = ml_get(lnum) + start_col;
3310 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312 break;
3313 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003314 if (s != NULL)
3315 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003316 byte_count_cursor += line_count_info(s, &word_count_cursor,
3317 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003318 if (lnum == curbuf->b_ml.ml_line_count
3319 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003320 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003321 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003322 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003327 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (lnum == curwin->w_cursor.lnum)
3329 {
3330 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003331 char_count_cursor += char_count;
3332 byte_count_cursor = byte_count +
3333 line_count_info(ml_get(lnum),
3334 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003335 (varnumber_T)(curwin->w_cursor.col + 1),
3336 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003340 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003341 &char_count, (varnumber_T)MAXCOL,
3342 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003345 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003346 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003347 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaared767a22016-01-03 22:49:16 +01003349 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003351 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003353 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3354 {
3355 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3356 &max_pos.col);
3357 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3358 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3359 }
3360 else
3361 buf1[0] = NUL;
3362
3363 if (char_count_cursor == byte_count_cursor
3364 && char_count == byte_count)
3365 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003366 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003367 buf1, line_count_selected,
3368 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003369 word_count_cursor,
3370 word_count,
3371 byte_count_cursor,
3372 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003373 else
3374 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003375 _("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 +01003376 buf1, line_count_selected,
3377 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003378 word_count_cursor,
3379 word_count,
3380 char_count_cursor,
3381 char_count,
3382 byte_count_cursor,
3383 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003386 {
3387 p = ml_get_curline();
3388 validate_virtcol();
3389 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3390 (int)curwin->w_virtcol + 1);
zeertzjq94b7c322024-03-12 21:50:32 +01003391 col_print(buf2, sizeof(buf2), ml_get_curline_len(),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003392 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaared767a22016-01-03 22:49:16 +01003394 if (char_count_cursor == byte_count_cursor
3395 && char_count == byte_count)
3396 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003397 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003398 (char *)buf1, (char *)buf2,
3399 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003401 word_count_cursor, word_count,
3402 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003403 else
3404 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003405 _("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 +01003406 (char *)buf1, (char *)buf2,
3407 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003408 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003409 word_count_cursor, word_count,
3410 char_count_cursor, char_count,
3411 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003412 }
3413 }
3414
Bram Moolenaared767a22016-01-03 22:49:16 +01003415 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003416 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003417 {
3418 size_t len = STRLEN(IObuff);
3419
3420 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003421 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003422 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003423 if (dict == NULL)
3424 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003425 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003426 p = p_shm;
3427 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003428 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003429 p_shm = p;
3430 }
3431 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003432#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003433 if (dict != NULL)
3434 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003435 dict_add_number(dict, "words", word_count);
3436 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003437 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003438 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3439 byte_count_cursor);
3440 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3441 char_count_cursor);
3442 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3443 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003447
3448/*
3449 * Handle indent and format operators and visual mode ":".
3450 */
3451 static void
3452op_colon(oparg_T *oap)
3453{
3454 stuffcharReadbuff(':');
3455 if (oap->is_VIsual)
3456 stuffReadbuff((char_u *)"'<,'>");
3457 else
3458 {
3459 // Make the range look nice, so it can be repeated.
3460 if (oap->start.lnum == curwin->w_cursor.lnum)
3461 stuffcharReadbuff('.');
3462 else
3463 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003464
3465#ifdef FEAT_FOLDING
3466 // When using !! on a closed fold the range ".!" works best to operate
3467 // on, it will be made the whole closed fold later.
3468 linenr_T endOfStartFold = oap->start.lnum;
3469 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3470#endif
3471 if (oap->end.lnum != oap->start.lnum
3472#ifdef FEAT_FOLDING
3473 && oap->end.lnum != endOfStartFold
3474#endif
3475 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003476 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003477 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003478 stuffcharReadbuff(',');
3479 if (oap->end.lnum == curwin->w_cursor.lnum)
3480 stuffcharReadbuff('.');
3481 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3482 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003483 else if (oap->start.lnum == curwin->w_cursor.lnum
3484#ifdef FEAT_FOLDING
3485 // do not use ".+number" for a closed fold, it would count
3486 // folded lines twice
3487 && !hasFolding(oap->end.lnum, NULL, NULL)
3488#endif
3489 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003490 {
3491 stuffReadbuff((char_u *)".+");
3492 stuffnumReadbuff((long)oap->line_count - 1);
3493 }
3494 else
3495 stuffnumReadbuff((long)oap->end.lnum);
3496 }
3497 }
3498 if (oap->op_type != OP_COLON)
3499 stuffReadbuff((char_u *)"!");
3500 if (oap->op_type == OP_INDENT)
3501 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003502 if (*get_equalprg() == NUL)
3503 stuffReadbuff((char_u *)"indent");
3504 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003505 stuffReadbuff(get_equalprg());
3506 stuffReadbuff((char_u *)"\n");
3507 }
3508 else if (oap->op_type == OP_FORMAT)
3509 {
3510 if (*curbuf->b_p_fp != NUL)
3511 stuffReadbuff(curbuf->b_p_fp);
3512 else if (*p_fp != NUL)
3513 stuffReadbuff(p_fp);
3514 else
3515 stuffReadbuff((char_u *)"fmt");
3516 stuffReadbuff((char_u *)"\n']");
3517 }
3518
3519 // do_cmdline() does the rest
3520}
3521
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003522// callback function for 'operatorfunc'
3523static callback_T opfunc_cb;
3524
3525/*
3526 * Process the 'operatorfunc' option value.
3527 * Returns OK or FAIL.
3528 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003529 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003530did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003531{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003532 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3533 return e_invalid_argument;
3534
3535 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003536}
3537
3538#if defined(EXITFREE) || defined(PROTO)
3539 void
3540free_operatorfunc_option(void)
3541{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003542# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003543 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003544# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003545}
3546#endif
3547
Dominique Pelle748b3082022-01-08 12:41:16 +00003548#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003549/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003550 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003551 * garbage collected.
3552 */
3553 int
3554set_ref_in_opfunc(int copyID UNUSED)
3555{
3556 int abort = FALSE;
3557
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003558 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003559
3560 return abort;
3561}
Dominique Pelle748b3082022-01-08 12:41:16 +00003562#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003563
3564/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003565 * Handle the "g@" operator: call 'operatorfunc'.
3566 */
3567 static void
3568op_function(oparg_T *oap UNUSED)
3569{
3570#ifdef FEAT_EVAL
3571 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003572 pos_T orig_start = curbuf->b_op_start;
3573 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003574 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003575
3576 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003577 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003578 else
3579 {
3580 // Set '[ and '] marks to text to be operated on.
3581 curbuf->b_op_start = oap->start;
3582 curbuf->b_op_end = oap->end;
3583 if (oap->motion_type != MLINE && !oap->inclusive)
3584 // Exclude the end position.
3585 decl(&curbuf->b_op_end);
3586
3587 argv[0].v_type = VAR_STRING;
3588 if (oap->block_mode)
3589 argv[0].vval.v_string = (char_u *)"block";
3590 else if (oap->motion_type == MLINE)
3591 argv[0].vval.v_string = (char_u *)"line";
3592 else
3593 argv[0].vval.v_string = (char_u *)"char";
3594 argv[1].v_type = VAR_UNKNOWN;
3595
3596 // Reset virtual_op so that 'virtualedit' can be changed in the
3597 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003598 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003599 virtual_op = MAYBE;
3600
naohiro ono75c30e92021-10-19 11:15:41 +01003601 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003602 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003603 finish_op = FALSE;
3604
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003605 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3606 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003607
3608 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003609 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003610 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003611 {
3612 curbuf->b_op_start = orig_start;
3613 curbuf->b_op_end = orig_end;
3614 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003615 }
3616#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003617 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003618#endif
3619}
3620
3621/*
3622 * Calculate start/end virtual columns for operating in block mode.
3623 */
3624 static void
3625get_op_vcol(
3626 oparg_T *oap,
3627 colnr_T redo_VIsual_vcol,
3628 int initial) // when TRUE adjust position for 'selectmode'
3629{
3630 colnr_T start, end;
3631
3632 if (VIsual_mode != Ctrl_V
3633 || (!initial && oap->end.col < curwin->w_width))
3634 return;
3635
3636 oap->block_mode = TRUE;
3637
3638 // prevent from moving onto a trail byte
3639 if (has_mbyte)
3640 mb_adjustpos(curwin->w_buffer, &oap->end);
3641
3642 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3643
3644 if (!redo_VIsual_busy)
3645 {
3646 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3647
3648 if (start < oap->start_vcol)
3649 oap->start_vcol = start;
3650 if (end > oap->end_vcol)
3651 {
3652 if (initial && *p_sel == 'e' && start >= 1
3653 && start - 1 >= oap->end_vcol)
3654 oap->end_vcol = start - 1;
3655 else
3656 oap->end_vcol = end;
3657 }
3658 }
3659
3660 // if '$' was used, get oap->end_vcol from longest line
3661 if (curwin->w_curswant == MAXCOL)
3662 {
3663 curwin->w_cursor.col = MAXCOL;
3664 oap->end_vcol = 0;
3665 for (curwin->w_cursor.lnum = oap->start.lnum;
3666 curwin->w_cursor.lnum <= oap->end.lnum;
3667 ++curwin->w_cursor.lnum)
3668 {
3669 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3670 if (end > oap->end_vcol)
3671 oap->end_vcol = end;
3672 }
3673 }
3674 else if (redo_VIsual_busy)
3675 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3676 // Correct oap->end.col and oap->start.col to be the
3677 // upper-left and lower-right corner of the block area.
3678 //
3679 // (Actually, this does convert column positions into character
3680 // positions)
3681 curwin->w_cursor.lnum = oap->end.lnum;
3682 coladvance(oap->end_vcol);
3683 oap->end = curwin->w_cursor;
3684
3685 curwin->w_cursor = oap->start;
3686 coladvance(oap->start_vcol);
3687 oap->start = curwin->w_cursor;
3688}
3689
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003690// Information for redoing the previous Visual selection.
3691typedef struct {
3692 int rv_mode; // 'v', 'V', or Ctrl-V
3693 linenr_T rv_line_count; // number of lines
3694 colnr_T rv_vcol; // number of cols or end column
3695 long rv_count; // count for Visual operator
3696 int rv_arg; // extra argument
3697} redo_VIsual_T;
3698
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003699 static int
3700is_ex_cmdchar(cmdarg_T *cap)
3701{
3702 return cap->cmdchar == ':'
3703 || cap->cmdchar == K_COMMAND
3704 || cap->cmdchar == K_SCRIPT_COMMAND;
3705}
3706
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003707/*
3708 * Handle an operator after Visual mode or when the movement is finished.
3709 * "gui_yank" is true when yanking text for the clipboard.
3710 */
3711 void
3712do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3713{
3714 oparg_T *oap = cap->oap;
3715 pos_T old_cursor;
3716 int empty_region_error;
3717 int restart_edit_save;
3718#ifdef FEAT_LINEBREAK
3719 int lbr_saved = curwin->w_p_lbr;
3720#endif
3721
3722 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003723 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3724
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003725 int include_line_break = FALSE;
3726
3727#if defined(FEAT_CLIPBOARD)
3728 // Yank the visual area into the GUI selection register before we operate
3729 // on it and lose it forever.
3730 // Don't do it if a specific register was specified, so that ""x"*P works.
3731 // This could call do_pending_operator() recursively, but that's OK
3732 // because gui_yank will be TRUE for the nested call.
3733 if ((clip_star.available || clip_plus.available)
3734 && oap->op_type != OP_NOP
3735 && !gui_yank
3736 && VIsual_active
3737 && !redo_VIsual_busy
3738 && oap->regname == 0)
3739 clip_auto_select();
3740#endif
3741 old_cursor = curwin->w_cursor;
3742
3743 // If an operation is pending, handle it...
3744 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3745 {
3746 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3747 // for the clipboard.
3748 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3749
3750#ifdef FEAT_LINEBREAK
3751 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003752 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003753#endif
3754 oap->is_VIsual = VIsual_active;
3755 if (oap->motion_force == 'V')
3756 oap->motion_type = MLINE;
3757 else if (oap->motion_force == 'v')
3758 {
3759 // If the motion was linewise, "inclusive" will not have been set.
3760 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3761 if (oap->motion_type == MLINE)
3762 oap->inclusive = FALSE;
3763 // If the motion already was characterwise, toggle "inclusive"
3764 else if (oap->motion_type == MCHAR)
3765 oap->inclusive = !oap->inclusive;
3766 oap->motion_type = MCHAR;
3767 }
3768 else if (oap->motion_force == Ctrl_V)
3769 {
3770 // Change line- or characterwise motion into Visual block mode.
3771 if (!VIsual_active)
3772 {
3773 VIsual_active = TRUE;
3774 VIsual = oap->start;
3775 }
3776 VIsual_mode = Ctrl_V;
3777 VIsual_select = FALSE;
3778 VIsual_reselect = FALSE;
3779 }
3780
3781 // Only redo yank when 'y' flag is in 'cpoptions'.
3782 // Never redo "zf" (define fold).
3783 if ((redo_yank || oap->op_type != OP_YANK)
3784 && ((!VIsual_active || oap->motion_force)
3785 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003786 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003787 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003788 && cap->cmdchar != 'D'
3789#ifdef FEAT_FOLDING
3790 && oap->op_type != OP_FOLD
3791 && oap->op_type != OP_FOLDOPEN
3792 && oap->op_type != OP_FOLDOPENREC
3793 && oap->op_type != OP_FOLDCLOSE
3794 && oap->op_type != OP_FOLDCLOSEREC
3795 && oap->op_type != OP_FOLDDEL
3796 && oap->op_type != OP_FOLDDELREC
3797#endif
3798 )
3799 {
3800 prep_redo(oap->regname, cap->count0,
3801 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3802 oap->motion_force, cap->cmdchar, cap->nchar);
3803 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3804 {
3805 // If 'cpoptions' does not contain 'r', insert the search
3806 // pattern to really repeat the same command.
3807 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3808 AppendToRedobuffLit(cap->searchbuf, -1);
3809 AppendToRedobuff(NL_STR);
3810 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003811 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003812 {
3813 // do_cmdline() has stored the first typed line in
3814 // "repeat_cmdline". When several lines are typed repeating
3815 // won't be possible.
3816 if (repeat_cmdline == NULL)
3817 ResetRedobuff();
3818 else
3819 {
zeertzjq30b6d612023-05-07 17:39:23 +01003820 if (cap->cmdchar == ':')
3821 AppendToRedobuffLit(repeat_cmdline, -1);
3822 else
3823 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003824 AppendToRedobuff(NL_STR);
3825 VIM_CLEAR(repeat_cmdline);
3826 }
3827 }
3828 }
3829
3830 if (redo_VIsual_busy)
3831 {
3832 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003833 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003834 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003835 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003836 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3837 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003838 VIsual_mode = redo_VIsual.rv_mode;
3839 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003840 {
3841 if (VIsual_mode == 'v')
3842 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003843 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003844 {
3845 validate_virtcol();
3846 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003847 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003848 }
3849 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003850 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003851 }
3852 else
3853 {
3854 curwin->w_curswant = MAXCOL;
3855 }
3856 coladvance(curwin->w_curswant);
3857 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003858 cap->count0 = redo_VIsual.rv_count;
3859 if (redo_VIsual.rv_count != 0)
3860 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003861 else
3862 cap->count1 = 1;
3863 }
3864 else if (VIsual_active)
3865 {
3866 if (!gui_yank)
3867 {
3868 // Save the current VIsual area for '< and '> marks, and "gv"
3869 curbuf->b_visual.vi_start = VIsual;
3870 curbuf->b_visual.vi_end = curwin->w_cursor;
3871 curbuf->b_visual.vi_mode = VIsual_mode;
3872 restore_visual_mode();
3873 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003874#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003875 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003876#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003877 }
3878
3879 // In Select mode, a linewise selection is operated upon like a
3880 // characterwise selection.
3881 // Special case: gH<Del> deletes the last line.
3882 if (VIsual_select && VIsual_mode == 'V'
3883 && cap->oap->op_type != OP_DELETE)
3884 {
3885 if (LT_POS(VIsual, curwin->w_cursor))
3886 {
3887 VIsual.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003888 curwin->w_cursor.col = ml_get_len(curwin->w_cursor.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003889 }
3890 else
3891 {
3892 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003893 VIsual.col = ml_get_len(VIsual.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003894 }
3895 VIsual_mode = 'v';
3896 }
3897 // If 'selection' is "exclusive", backup one character for
3898 // charwise selections.
3899 else if (VIsual_mode == 'v')
3900 include_line_break = unadjust_for_sel();
3901
3902 oap->start = VIsual;
3903 if (VIsual_mode == 'V')
3904 {
3905 oap->start.col = 0;
3906 oap->start.coladd = 0;
3907 }
3908 }
3909
3910 // Set oap->start to the first position of the operated text, oap->end
3911 // to the end of the operated text. w_cursor is equal to oap->start.
3912 if (LT_POS(oap->start, curwin->w_cursor))
3913 {
3914#ifdef FEAT_FOLDING
3915 // Include folded lines completely.
3916 if (!VIsual_active)
3917 {
3918 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3919 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003920 if ((curwin->w_cursor.col > 0 || oap->inclusive
3921 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003922 && hasFolding(curwin->w_cursor.lnum, NULL,
3923 &curwin->w_cursor.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003924 curwin->w_cursor.col = ml_get_curline_len();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003925 }
3926#endif
3927 oap->end = curwin->w_cursor;
3928 curwin->w_cursor = oap->start;
3929
3930 // w_virtcol may have been updated; if the cursor goes back to its
3931 // previous position w_virtcol becomes invalid and isn't updated
3932 // automatically.
3933 curwin->w_valid &= ~VALID_VIRTCOL;
3934 }
3935 else
3936 {
3937#ifdef FEAT_FOLDING
3938 // Include folded lines completely.
3939 if (!VIsual_active && oap->motion_type == MLINE)
3940 {
3941 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3942 NULL))
3943 curwin->w_cursor.col = 0;
3944 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003945 oap->start.col = ml_get_len(oap->start.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003946 }
3947#endif
3948 oap->end = oap->start;
3949 oap->start = curwin->w_cursor;
3950 }
3951
3952 // Just in case lines were deleted that make the position invalid.
3953 check_pos(curwin->w_buffer, &oap->end);
3954 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3955
3956 // Set "virtual_op" before resetting VIsual_active.
3957 virtual_op = virtual_active();
3958
3959 if (VIsual_active || redo_VIsual_busy)
3960 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003961 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003962
3963 if (!redo_VIsual_busy && !gui_yank)
3964 {
3965 // Prepare to reselect and redo Visual: this is based on the
3966 // size of the Visual text
3967 resel_VIsual_mode = VIsual_mode;
3968 if (curwin->w_curswant == MAXCOL)
3969 resel_VIsual_vcol = MAXCOL;
3970 else
3971 {
3972 if (VIsual_mode != Ctrl_V)
3973 getvvcol(curwin, &(oap->end),
3974 NULL, NULL, &oap->end_vcol);
3975 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3976 {
3977 if (VIsual_mode != Ctrl_V)
3978 getvvcol(curwin, &(oap->start),
3979 &oap->start_vcol, NULL, NULL);
3980 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3981 }
3982 else
3983 resel_VIsual_vcol = oap->end_vcol;
3984 }
3985 resel_VIsual_line_count = oap->line_count;
3986 }
3987
3988 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3989 if ((redo_yank || oap->op_type != OP_YANK)
3990 && oap->op_type != OP_COLON
3991#ifdef FEAT_FOLDING
3992 && oap->op_type != OP_FOLD
3993 && oap->op_type != OP_FOLDOPEN
3994 && oap->op_type != OP_FOLDOPENREC
3995 && oap->op_type != OP_FOLDCLOSE
3996 && oap->op_type != OP_FOLDCLOSEREC
3997 && oap->op_type != OP_FOLDDEL
3998 && oap->op_type != OP_FOLDDELREC
3999#endif
4000 && oap->motion_force == NUL
4001 )
4002 {
4003 // Prepare for redoing. Only use the nchar field for "r",
4004 // otherwise it might be the second char of the operator.
4005 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4006 || cap->nchar == 'N'))
4007 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004008 get_op_char(oap->op_type),
4009 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004010 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004011 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004012 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004013 int opchar = get_op_char(oap->op_type);
4014 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004015 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4016
4017 // reverse what nv_replace() did
4018 if (nchar == REPLACE_CR_NCHAR)
4019 nchar = CAR;
4020 else if (nchar == REPLACE_NL_NCHAR)
4021 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004022
4023 if (opchar == 'g' && extra_opchar == '@')
4024 // also repeat the count for 'operatorfunc'
4025 prep_redo_num2(oap->regname, 0L, NUL, 'v',
4026 cap->count0, opchar, extra_opchar, nchar);
4027 else
4028 prep_redo(oap->regname, 0L, NUL, 'v',
4029 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004030 }
4031 if (!redo_VIsual_busy)
4032 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004033 redo_VIsual.rv_mode = resel_VIsual_mode;
4034 redo_VIsual.rv_vcol = resel_VIsual_vcol;
4035 redo_VIsual.rv_line_count = resel_VIsual_line_count;
4036 redo_VIsual.rv_count = cap->count0;
4037 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004038 }
4039 }
4040
4041 // oap->inclusive defaults to TRUE.
4042 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4043 // FALSE. This makes "d}P" and "v}dP" work the same.
4044 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4045 oap->inclusive = TRUE;
4046 if (VIsual_mode == 'V')
4047 oap->motion_type = MLINE;
4048 else
4049 {
4050 oap->motion_type = MCHAR;
4051 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4052 && (include_line_break || !virtual_op))
4053 {
4054 oap->inclusive = FALSE;
4055 // Try to include the newline, unless it's an operator
4056 // that works on lines only.
4057 if (*p_sel != 'o'
4058 && !op_on_lines(oap->op_type)
4059 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4060 {
4061 ++oap->end.lnum;
4062 oap->end.col = 0;
4063 oap->end.coladd = 0;
4064 ++oap->line_count;
4065 }
4066 }
4067 }
4068
4069 redo_VIsual_busy = FALSE;
4070
4071 // Switch Visual off now, so screen updating does
4072 // not show inverted text when the screen is redrawn.
4073 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4074 // no screen redraw, so it is done here to remove the inverted
4075 // part.
4076 if (!gui_yank)
4077 {
4078 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004079 setmouse();
4080 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004081 may_clear_cmdline();
4082 if ((oap->op_type == OP_YANK
4083 || oap->op_type == OP_COLON
4084 || oap->op_type == OP_FUNCTION
4085 || oap->op_type == OP_FILTER)
4086 && oap->motion_force == NUL)
4087 {
4088#ifdef FEAT_LINEBREAK
4089 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01004090 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004091#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004092 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004093 }
4094 }
4095 }
4096
4097 // Include the trailing byte of a multi-byte char.
4098 if (has_mbyte && oap->inclusive)
4099 {
4100 int l;
4101
4102 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4103 if (l > 1)
4104 oap->end.col += l - 1;
4105 }
4106 curwin->w_set_curswant = TRUE;
4107
4108 // oap->empty is set when start and end are the same. The inclusive
4109 // flag affects this too, unless yanking and the end is on a NUL.
4110 oap->empty = (oap->motion_type == MCHAR
4111 && (!oap->inclusive
4112 || (oap->op_type == OP_YANK
4113 && gchar_pos(&oap->end) == NUL))
4114 && EQUAL_POS(oap->start, oap->end)
4115 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4116 // For delete, change and yank, it's an error to operate on an
4117 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4118 empty_region_error = (oap->empty
4119 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4120
4121 // Force a redraw when operating on an empty Visual region, when
4122 // 'modifiable is off or creating a fold.
4123 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4124#ifdef FEAT_FOLDING
4125 || oap->op_type == OP_FOLD
4126#endif
4127 ))
4128 {
4129#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004130 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004131#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004132 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004133 }
4134
4135 // If the end of an operator is in column one while oap->motion_type
4136 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4137 // character in the previous line. If op_start is on or before the
4138 // first non-blank in the line, the operator becomes linewise
4139 // (strange, but that's the way vi does it).
4140 if ( oap->motion_type == MCHAR
4141 && oap->inclusive == FALSE
4142 && !(cap->retval & CA_NO_ADJ_OP_END)
4143 && oap->end.col == 0
4144 && (!oap->is_VIsual || *p_sel == 'o')
4145 && !oap->block_mode
4146 && oap->line_count > 1)
4147 {
4148 oap->end_adjusted = TRUE; // remember that we did this
4149 --oap->line_count;
4150 --oap->end.lnum;
4151 if (inindent(0))
4152 oap->motion_type = MLINE;
4153 else
4154 {
zeertzjq94b7c322024-03-12 21:50:32 +01004155 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004156 if (oap->end.col)
4157 {
4158 --oap->end.col;
4159 oap->inclusive = TRUE;
4160 }
4161 }
4162 }
4163 else
4164 oap->end_adjusted = FALSE;
4165
4166 switch (oap->op_type)
4167 {
4168 case OP_LSHIFT:
4169 case OP_RSHIFT:
4170 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4171 auto_format(FALSE, TRUE);
4172 break;
4173
4174 case OP_JOIN_NS:
4175 case OP_JOIN:
4176 if (oap->line_count < 2)
4177 oap->line_count = 2;
4178 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4179 curbuf->b_ml.ml_line_count)
4180 beep_flush();
4181 else
4182 {
4183 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4184 TRUE, TRUE, TRUE);
4185 auto_format(FALSE, TRUE);
4186 }
4187 break;
4188
4189 case OP_DELETE:
4190 VIsual_reselect = FALSE; // don't reselect now
4191 if (empty_region_error)
4192 {
4193 vim_beep(BO_OPER);
4194 CancelRedo();
4195 }
4196 else
4197 {
4198 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004199 // save cursor line for undo if it wasn't saved yet
4200 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4201 && u_save_cursor() == OK)
4202 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004203 }
4204 break;
4205
4206 case OP_YANK:
4207 if (empty_region_error)
4208 {
4209 if (!gui_yank)
4210 {
4211 vim_beep(BO_OPER);
4212 CancelRedo();
4213 }
4214 }
4215 else
4216 {
4217#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004218 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004219#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004220 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004221 (void)op_yank(oap, FALSE, !gui_yank);
4222 }
4223 check_cursor_col();
4224 break;
4225
4226 case OP_CHANGE:
4227 VIsual_reselect = FALSE; // don't reselect now
4228 if (empty_region_error)
4229 {
4230 vim_beep(BO_OPER);
4231 CancelRedo();
4232 }
4233 else
4234 {
4235 // This is a new edit command, not a restart. Need to
4236 // remember it to make 'insertmode' work with mappings for
4237 // Visual mode. But do this only once and not when typed and
4238 // 'insertmode' isn't set.
4239 if (p_im || !KeyTyped)
4240 restart_edit_save = restart_edit;
4241 else
4242 restart_edit_save = 0;
4243 restart_edit = 0;
4244#ifdef FEAT_LINEBREAK
4245 // Restore linebreak, so that when the user edits it looks as
4246 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004247 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004248#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004249 // trigger TextChangedI
4250 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4251
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004252 if (op_change(oap)) // will call edit()
4253 cap->retval |= CA_COMMAND_BUSY;
4254 if (restart_edit == 0)
4255 restart_edit = restart_edit_save;
4256 }
4257 break;
4258
4259 case OP_FILTER:
4260 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4261 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4262 else
4263 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4264 // FALLTHROUGH
4265
4266 case OP_INDENT:
4267 case OP_COLON:
4268
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004269 // If 'equalprg' is empty, do the indenting internally.
4270 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4271 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004272 if (curbuf->b_p_lisp)
4273 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004274#ifdef FEAT_EVAL
4275 if (use_indentexpr_for_lisp())
4276 op_reindent(oap, get_expr_indent);
4277 else
4278#endif
4279 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004280 break;
4281 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004282 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004283#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004284 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004285#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004286 get_c_indent);
4287 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004288 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004289
4290 op_colon(oap);
4291 break;
4292
4293 case OP_TILDE:
4294 case OP_UPPER:
4295 case OP_LOWER:
4296 case OP_ROT13:
4297 if (empty_region_error)
4298 {
4299 vim_beep(BO_OPER);
4300 CancelRedo();
4301 }
4302 else
4303 op_tilde(oap);
4304 check_cursor_col();
4305 break;
4306
4307 case OP_FORMAT:
4308#if defined(FEAT_EVAL)
4309 if (*curbuf->b_p_fex != NUL)
4310 op_formatexpr(oap); // use expression
4311 else
4312#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004313 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004314 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004315 op_colon(oap); // use external command
4316 else
4317 op_format(oap, FALSE); // use internal function
4318 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004319 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004320 case OP_FORMAT2:
4321 op_format(oap, TRUE); // use internal function
4322 break;
4323
4324 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004325 {
4326 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4327
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004328#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004329 // Restore linebreak, so that when the user edits it looks as
4330 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004331 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004332#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004333 // call 'operatorfunc'
4334 op_function(oap);
4335
4336 // Restore the info for redoing Visual mode, the function may
4337 // invoke another operator and unintentionally change it.
4338 redo_VIsual = save_redo_VIsual;
4339 break;
4340 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004341
4342 case OP_INSERT:
4343 case OP_APPEND:
4344 VIsual_reselect = FALSE; // don't reselect now
4345 if (empty_region_error)
4346 {
4347 vim_beep(BO_OPER);
4348 CancelRedo();
4349 }
4350 else
4351 {
4352 // This is a new edit command, not a restart. Need to
4353 // remember it to make 'insertmode' work with mappings for
4354 // Visual mode. But do this only once.
4355 restart_edit_save = restart_edit;
4356 restart_edit = 0;
4357#ifdef FEAT_LINEBREAK
4358 // Restore linebreak, so that when the user edits it looks as
4359 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004360 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004361#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004362 // trigger TextChangedI
4363 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4364
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004365 op_insert(oap, cap->count1);
4366#ifdef FEAT_LINEBREAK
4367 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004368 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004369#endif
4370
4371 // TODO: when inserting in several lines, should format all
4372 // the lines.
4373 auto_format(FALSE, TRUE);
4374
4375 if (restart_edit == 0)
4376 restart_edit = restart_edit_save;
4377 else
4378 cap->retval |= CA_COMMAND_BUSY;
4379 }
4380 break;
4381
4382 case OP_REPLACE:
4383 VIsual_reselect = FALSE; // don't reselect now
4384 if (empty_region_error)
4385 {
4386 vim_beep(BO_OPER);
4387 CancelRedo();
4388 }
4389 else
4390 {
4391#ifdef FEAT_LINEBREAK
4392 // Restore linebreak, so that when the user edits it looks as
4393 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004394 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004395#endif
4396 op_replace(oap, cap->nchar);
4397 }
4398 break;
4399
4400#ifdef FEAT_FOLDING
4401 case OP_FOLD:
4402 VIsual_reselect = FALSE; // don't reselect now
4403 foldCreate(oap->start.lnum, oap->end.lnum);
4404 break;
4405
4406 case OP_FOLDOPEN:
4407 case OP_FOLDOPENREC:
4408 case OP_FOLDCLOSE:
4409 case OP_FOLDCLOSEREC:
4410 VIsual_reselect = FALSE; // don't reselect now
4411 opFoldRange(oap->start.lnum, oap->end.lnum,
4412 oap->op_type == OP_FOLDOPEN
4413 || oap->op_type == OP_FOLDOPENREC,
4414 oap->op_type == OP_FOLDOPENREC
4415 || oap->op_type == OP_FOLDCLOSEREC,
4416 oap->is_VIsual);
4417 break;
4418
4419 case OP_FOLDDEL:
4420 case OP_FOLDDELREC:
4421 VIsual_reselect = FALSE; // don't reselect now
4422 deleteFold(oap->start.lnum, oap->end.lnum,
4423 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4424 break;
4425#endif
4426 case OP_NR_ADD:
4427 case OP_NR_SUB:
4428 if (empty_region_error)
4429 {
4430 vim_beep(BO_OPER);
4431 CancelRedo();
4432 }
4433 else
4434 {
4435 VIsual_active = TRUE;
4436#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004437 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004438#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004439 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004440 VIsual_active = FALSE;
4441 }
4442 check_cursor_col();
4443 break;
4444 default:
4445 clearopbeep(oap);
4446 }
4447 virtual_op = MAYBE;
4448 if (!gui_yank)
4449 {
4450 // if 'sol' not set, go back to old column for some commands
4451 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4452 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4453 || oap->op_type == OP_DELETE))
4454 {
4455#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004456 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004457#endif
4458 coladvance(curwin->w_curswant = old_col);
4459 }
4460 }
4461 else
4462 {
4463 curwin->w_cursor = old_cursor;
4464 }
4465 oap->block_mode = FALSE;
4466 clearop(oap);
4467 motion_force = NUL;
4468 }
4469#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004470 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004471#endif
4472}
Christian Brabandtffb13672023-09-15 20:22:02 +02004473
4474// put byte 'c' at position 'lp', but
4475// verify, that the position to place
4476// is actually safe
4477 static void
4478pbyte(pos_T lp, int c)
4479{
4480 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4481 int len = curbuf->b_ml.ml_line_len;
4482
4483 // safety check
4484 if (lp.col >= len)
4485 lp.col = (len > 1 ? len - 2 : 0);
4486 *(p + lp.col) = c;
4487}