blob: cdc30806ce60c1b3b3f93ee83a29f890107b4c50 [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;
234 int sw_val = trim_to_int(get_sw_value_indent(curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Ernie Raelfda700c2023-11-30 18:20:00 +0100236 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200238 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200240 i = count / sw_val; // number of 'shiftwidth' rounded down
241 j = count % sw_val; // extra spaces
242 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 --amount;
244 if (left)
245 {
246 i -= amount;
247 if (i < 0)
248 i = 0;
249 }
250 else
251 i += amount;
Ernie Raelfda700c2023-11-30 18:20:00 +0100252 count = (vimlong_T)i * (vimlong_T)sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200254 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {
256 if (left)
257 {
Ernie Raelfda700c2023-11-30 18:20:00 +0100258 count -= (vimlong_T)sw_val * (vimlong_T)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (count < 0)
260 count = 0;
261 }
262 else
Ernie Raelfda700c2023-11-30 18:20:00 +0100263 count += (vimlong_T)sw_val * (vimlong_T)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200266 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 if (State & VREPLACE_FLAG)
Ernie Rael2b0882f2023-11-23 20:21:45 +0100268 change_indent(INDENT_SET, trim_to_int(count), FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 else
Ernie Rael2b0882f2023-11-23 20:21:45 +0100270 (void)set_indent(trim_to_int(count), call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271}
272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273/*
274 * Shift one line of the current block one shiftwidth right or left.
275 * Leaves cursor on first character in block.
276 */
277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100278shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280 int left = (oap->op_type == OP_LSHIFT);
281 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000282 int total;
283 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +0200284 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200286 int sw_val = (int)get_sw_value_indent(curbuf);
287 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000290 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100291 int added;
John Marriott38b9f452024-04-25 21:39:18 +0200292 size_t new_line_len; // the length of the line after the
LemonBoy4b936742022-05-13 21:56:28 +0100293 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#ifdef FEAT_RIGHTLEFT
295 int old_p_ri = p_ri;
296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100297 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298#endif
299
Bram Moolenaar24959102022-05-07 20:01:16 +0100300 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
302 if (bd.is_short)
303 return;
304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100305 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 total = (int)((unsigned)amount * (unsigned)sw_val);
307 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100308 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200309
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 oldp = ml_get_curline();
John Marriott38b9f452024-04-25 21:39:18 +0200311 oldlen = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313 if (!left)
314 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100315 int tabs = 0, spaces = 0;
316 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100317
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 /*
319 * 1. Get start vcol
320 * 2. Total ws vcols
321 * 3. Divvy into TABs & spp
322 * 4. Construct new string
323 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100324 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 ws_vcol = bd.start_vcol - bd.pre_whitesp;
326 if (bd.startspaces)
327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100329 {
330 if ((*mb_ptr2len)(bd.textstart) == 1)
331 ++bd.textstart;
332 else
333 {
334 ws_vcol = 0;
335 bd.startspaces = 0;
336 }
337 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000338 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000339 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100341
342 // TODO: is passing bd.textstart for start of the line OK?
343 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
344 bd.start_vcol, bd.textstart, bd.textstart);
345 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100347 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100349 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100351 bd.textstart = cts.cts_ptr;
352 bd.start_vcol = cts.cts_vcol;
353 clear_chartabsize_arg(&cts);
354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100355 // OK, now total=all the VWS reqd, and textstart points at the 1st
356 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#ifdef FEAT_VARTABS
358 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200359 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100360 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200361 else
LemonBoy4b936742022-05-13 21:56:28 +0100362 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200363#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100365 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
366 if (tabs > 0)
367 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else
LemonBoy4b936742022-05-13 21:56:28 +0100369 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200370#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100371 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100373
John Marriott38b9f452024-04-25 21:39:18 +0200374 new_line_len = bd.textcol + tabs + spaces + (oldlen - (bd.textstart - oldp));
LemonBoy4b936742022-05-13 21:56:28 +0100375 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (newp == NULL)
377 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +0200379 newlen = bd.textcol;
380 vim_memset(newp + newlen, TAB, (size_t)tabs);
381 newlen += tabs;
382 vim_memset(newp + newlen, ' ', (size_t)spaces);
383 STRCPY(newp + newlen + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100385 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387 colnr_T destination_col; // column to which text in block will
388 // be shifted
389 char_u *verbatim_copy_end; // end of the part of the line which is
390 // copied verbatim
391 colnr_T verbatim_copy_width;// the (displayed) width of this part
392 // of line
John Marriott38b9f452024-04-25 21:39:18 +0200393 size_t fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000394 size_t block_space_width;
395 size_t shift_amount;
396 char_u *non_white = bd.textstart;
397 colnr_T non_white_col;
John Marriott38b9f452024-04-25 21:39:18 +0200398 size_t fixedlen; // length of string left of the shift
399 // position (ie the string not being shifted)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100400 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000402 /*
403 * Firstly, let's find the first non-whitespace character that is
404 * displayed after the block's start column and the character's column
405 * number. Also, let's calculate the width of all the whitespace
406 * characters that are displayed in the block and precede the searched
407 * non-whitespace character.
408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100410 // If "bd.startspaces" is set, "bd.textstart" points to the character,
411 // the part of which is displayed at the block's beginning. Let's start
412 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000413 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100414 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100416 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417 non_white_col = bd.start_vcol;
418
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100419 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
420 non_white_col, bd.textstart, non_white);
421 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000422 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100423 incr = lbr_chartabsize_adv(&cts);
424 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100426 non_white_col = cts.cts_vcol;
427 non_white = cts.cts_ptr;
428 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000430 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100431 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000432 shift_amount = (block_space_width < (size_t)total
433 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100435 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000436 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // Now let's find out how much of the beginning of the line we can
439 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000440 verbatim_copy_end = bd.textstart;
441 verbatim_copy_width = bd.start_vcol;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // If "bd.startspaces" is set, "bd.textstart" points to the character
444 // preceding the block. We have to subtract its width to obtain its
445 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000446 if (bd.startspaces)
447 verbatim_copy_width -= bd.start_char_vcols;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100448 init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
449 bd.textstart, verbatim_copy_end);
450 while (cts.cts_vcol < destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000451 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100452 incr = lbr_chartabsize(&cts);
453 if (cts.cts_vcol + incr > destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000454 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100455 cts.cts_vcol += incr;
456 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 verbatim_copy_width = cts.cts_vcol;
459 verbatim_copy_end = cts.cts_ptr;
460 clear_chartabsize_arg(&cts);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100462 // If "destination_col" is different from the width of the initial
463 // part of the line that will be copied, it means we encountered a tab
464 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000465 fill = destination_col - verbatim_copy_width;
466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100467 // The replacement line will consist of:
468 // - the beginning of the original line up to "verbatim_copy_end",
469 // - "fill" number of spaces,
470 // - the rest of the line, pointed to by non_white.
John Marriott38b9f452024-04-25 21:39:18 +0200471 fixedlen = verbatim_copy_end - oldp;
472 new_line_len = fixedlen + fill + (oldlen - (non_white - oldp));
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000473
LemonBoy4b936742022-05-13 21:56:28 +0100474 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 if (newp == NULL)
476 return;
John Marriott38b9f452024-04-25 21:39:18 +0200477 mch_memmove(newp, oldp, fixedlen);
478 newlen = fixedlen;
479 vim_memset(newp + newlen, ' ', (size_t)fill);
480 STRCPY(newp + newlen + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100482 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
John Marriott38b9f452024-04-25 21:39:18 +0200484
485 // compute the number of bytes added or subtracted.
486 // note new_line_len and oldlen are unsigned so we have
487 // to be careful about how we calculate this.
488 if (new_line_len >= oldlen)
489 added = (int)(new_line_len - oldlen);
490 else
491 added = 0 - (int)(oldlen - new_line_len);
LemonBoy4b936742022-05-13 21:56:28 +0100492 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 State = oldstate;
494 curwin->w_cursor.col = oldcol;
495#ifdef FEAT_RIGHTLEFT
496 p_ri = old_p_ri;
497#endif
498}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500/*
501 * Insert string "s" (b_insert ? before : after) block :AKelly
502 * Caller must prepare for undo.
503 */
504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100505block_insert(
506 oparg_T *oap,
507 char_u *s,
John Marriott38b9f452024-04-25 21:39:18 +0200508 size_t slen,
Bram Moolenaar9b578142016-01-30 19:39:49 +0100509 int b_insert,
510 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
Bram Moolenaardac13472019-09-16 21:06:21 +0200512 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100513 int count = 0; // extra spaces to replace a cut TAB
514 int spaces = 0; // non-zero if cutting a TAB
515 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200516 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100517 char_u *newp, *oldp; // new, old lines
518 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 int oldstate = State;
520
Bram Moolenaar24959102022-05-07 20:01:16 +0100521 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
523 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
524 {
525 block_prep(oap, bdp, lnum, TRUE);
526 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100527 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
529 oldp = ml_get(lnum);
530
531 if (b_insert)
532 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200533 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 spaces = bdp->startspaces;
535 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100536 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 offset = bdp->textcol;
538 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100539 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200541 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200544 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100546 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 offset = bdp->textcol + bdp->textlen - (spaces != 0);
548 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100551 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (!bdp->is_MAX)
553 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
554 count = spaces;
555 offset = bdp->textcol + bdp->textlen;
556 }
557 }
558
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200559 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000560 // avoid copying part of a multi-byte character
561 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100562
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200563 if (spaces < 0) // can happen when the cursor was moved
564 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200565
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000566 // Make sure the allocated size matches what is actually copied below.
John Marriott38b9f452024-04-25 21:39:18 +0200567 newp = alloc(ml_get_len(lnum) + spaces + slen
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000568 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
569 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (newp == NULL)
571 continue;
572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100573 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000574 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 oldp += offset;
576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100577 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200578 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200579 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100581 // copy the new text
John Marriott38b9f452024-04-25 21:39:18 +0200582 mch_memmove(newp + startcol, s, slen);
583 offset += slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000585 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000587 if (*oldp == TAB)
588 {
589 // insert post-padding
590 vim_memset(newp + offset + spaces, ' ',
591 (size_t)(ts_val - spaces));
592 // we're splitting a TAB, don't copy it
593 oldp++;
594 // We allowed for that TAB, remember this now
595 count++;
596 }
597 else
598 // Not a TAB, no extra spaces
599 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601
602 if (spaces > 0)
603 offset += count;
John Marriott38b9f452024-04-25 21:39:18 +0200604 STRCPY(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 ml_replace(lnum, newp, FALSE);
607
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200608 if (b_insert)
609 // correct any text properties
John Marriott38b9f452024-04-25 21:39:18 +0200610 inserted_bytes(lnum, startcol, slen);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (lnum == oap->end.lnum)
613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 // Set "']" mark to the end of the block instead of the end of
615 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 curbuf->b_op_end.lnum = oap->end.lnum;
617 curbuf->b_op_end.col = offset;
618 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100619 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
622
623 State = oldstate;
624}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200627 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200629 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 */
631 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100632op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 int n;
635 linenr_T lnum;
636 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 char_u *newp, *oldp;
638 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
640 int did_yank = FALSE;
641
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100642 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 return OK;
644
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if (oap->empty)
647 return u_save_cursor();
648
649 if (!curbuf->b_p_ma)
650 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200651 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 return FAIL;
653 }
654
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000655 if (VIsual_select && oap->is_VIsual)
656 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100657 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000658
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#ifdef FEAT_CLIPBOARD
660 adjust_clip_reg(&oap->regname);
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (has_mbyte)
664 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
Bram Moolenaard04b7502010-07-08 22:27:55 +0200666 /*
667 * Imitate the strange Vi behaviour: If the delete spans more than one
668 * line and motion_type == MCHAR and the result is a blank line, make the
669 * delete linewise. Don't do this for the change command or Visual mode.
670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000673 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100675 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && oap->op_type == OP_DELETE)
677 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200678 ptr = ml_get(oap->end.lnum) + oap->end.col;
679 if (*ptr != NUL)
680 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 ptr = skipwhite(ptr);
682 if (*ptr == NUL && inindent(0))
683 oap->motion_type = MLINE;
684 }
685
Bram Moolenaard04b7502010-07-08 22:27:55 +0200686 /*
687 * Check for trying to delete (e.g. "D") in an empty line.
688 * Note: For the change operator it is ok.
689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if ( oap->motion_type == MCHAR
691 && oap->line_count == 1
692 && oap->op_type == OP_DELETE
693 && *ml_get(oap->start.lnum) == NUL)
694 {
695 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000696 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 * 'cpoptions' (Vi compatible).
698 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000699 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100700 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
701 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000702 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
704 beep_flush();
705 return OK;
706 }
707
Bram Moolenaard04b7502010-07-08 22:27:55 +0200708 /*
709 * Do a yank of whatever we're about to delete.
710 * If a yank register was specified, put the deleted text into that
711 * register. For the black hole register '_' don't yank anything.
712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (oap->regname != '_')
714 {
715 if (oap->regname != 0)
716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100717 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (!valid_yank_reg(oap->regname, TRUE))
719 {
720 beep_flush();
721 return OK;
722 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100723 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
724 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 did_yank = TRUE;
726 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200727 else
728 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 /*
731 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100732 * delete contains a line break, or when using a specific operator (Vi
733 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100735 if (oap->motion_type == MLINE || oap->line_count > 1
736 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100738 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (op_yank(oap, TRUE, FALSE) == OK)
740 did_yank = TRUE;
741 }
742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100743 // Yank into small delete register when no named register specified
744 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200745 if ((
746#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200747 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
748 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200749#endif
750 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 && oap->line_count == 1)
752 {
753 oap->regname = '-';
754 get_yank_register(oap->regname, TRUE);
755 if (op_yank(oap, TRUE, FALSE) == OK)
756 did_yank = TRUE;
757 oap->regname = 0;
758 }
759
760 /*
761 * If there's too much stuff to fit in the yank register, then get a
762 * confirmation before doing the delete. This is crude, but simple.
763 * And it avoids doing a delete of something we can't put back if we
764 * want.
765 */
766 if (!did_yank)
767 {
768 int msg_silent_save = msg_silent;
769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100770 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
772 msg_silent = msg_silent_save;
773 if (n != 'y')
774 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000775 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 return FAIL;
777 }
778 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100779
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100780#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100781 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200782 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785
Bram Moolenaard04b7502010-07-08 22:27:55 +0200786 /*
787 * block mode delete
788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 if (oap->block_mode)
790 {
791 if (u_save((linenr_T)(oap->start.lnum - 1),
792 (linenr_T)(oap->end.lnum + 1)) == FAIL)
793 return FAIL;
794
795 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
796 {
797 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100798 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 continue;
800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (lnum == curwin->w_cursor.lnum)
803 {
804 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807
Bram Moolenaar8055d172019-05-17 22:57:26 +0200808 // "n" == number of chars deleted
809 // If we delete a TAB, it may be replaced by several characters.
810 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 n = bd.textlen - bd.startspaces - bd.endspaces;
812 oldp = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100813 newp = alloc(ml_get_len(lnum) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (newp == NULL)
815 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200819 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100821 // copy the part after the deleted part
John Marriott38b9f452024-04-25 21:39:18 +0200822 STRCPY(newp + bd.textcol + bd.startspaces + bd.endspaces,
823 oldp + bd.textcol + bd.textlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100824 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200826
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100827#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200828 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200829 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832
833 check_cursor_col();
834 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
835 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100838 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
840 if (oap->op_type == OP_CHANGE)
841 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // Delete the lines except the first one. Temporarily move the
843 // cursor to the next line. Save the current line number, if the
844 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (oap->line_count > 1)
846 {
847 lnum = curwin->w_cursor.lnum;
848 ++curwin->w_cursor.lnum;
849 del_lines((long)(oap->line_count - 1), TRUE);
850 curwin->w_cursor.lnum = lnum;
851 }
852 if (u_save_cursor() == FAIL)
853 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 beginline(BL_WHITE); // cursor on first non-white
857 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 ai_col = curwin->w_cursor.col;
859 }
860 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 beginline(0); // cursor in column 0
zeertzjq8c55d602024-03-13 20:42:26 +0100862 truncate_line(FALSE); // delete the rest of the line,
863 // leaving cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 else
868 {
869 del_lines(oap->line_count, TRUE);
870 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 }
874 else
875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (virtual_op)
877 {
878 int endcol = 0;
879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100880 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (gchar_pos(&oap->start) == '\t')
882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 return FAIL;
885 if (oap->line_count == 1)
886 endcol = getviscol2(oap->end.col, oap->end.coladd);
887 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
888 oap->start = curwin->w_cursor;
889 if (oap->line_count == 1)
890 {
891 coladvance(endcol);
892 oap->end.col = curwin->w_cursor.col;
893 oap->end.coladd = curwin->w_cursor.coladd;
894 curwin->w_cursor = oap->start;
895 }
896 }
897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100898 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (gchar_pos(&oap->end) == '\t'
900 && (int)oap->end.coladd < oap->inclusive)
901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (u_save((linenr_T)(oap->end.lnum - 1),
904 (linenr_T)(oap->end.lnum + 1)) == FAIL)
905 return FAIL;
906 curwin->w_cursor = oap->end;
907 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
908 oap->end = curwin->w_cursor;
909 curwin->w_cursor = oap->start;
910 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100911 if (has_mbyte)
912 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100915 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 return FAIL;
919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100921 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 && oap->op_type == OP_CHANGE
923 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100924 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 display_dollar(oap->end.col - !oap->inclusive);
926
927 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (virtual_op)
930 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 // fix up things for virtualedit-delete:
932 // break the tabs which are going to get in our way
zeertzjq94b7c322024-03-12 21:50:32 +0100933 int len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935 if (oap->end.coladd != 0
936 && (int)oap->end.col >= len - 1
937 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
938 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100939 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (n == 0 && oap->start.coladd != oap->end.coladd)
941 n = 1;
942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100943 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (gchar_cursor() != NUL)
945 curwin->w_cursor.coladd = 0;
946 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200947 (void)del_bytes((long)n, !virtual_op,
948 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100950 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 {
952 pos_T curpos;
953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100954 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
956 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
957 return FAIL;
958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++curwin->w_cursor.lnum;
963 del_lines((long)(oap->line_count - 2), FALSE);
964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100965 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200966 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200967 curwin->w_cursor.col = 0;
968 (void)del_bytes((long)n, !virtual_op,
969 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100970 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200971 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200973 if (oap->op_type == OP_DELETE)
974 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976
977 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
978
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000979setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200980 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100982 if (oap->block_mode)
983 {
984 curbuf->b_op_end.lnum = oap->end.lnum;
985 curbuf->b_op_end.col = oap->start.col;
986 }
987 else
988 curbuf->b_op_end = oap->start;
989 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 return OK;
993}
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Adjust end of operating area for ending on a multi-byte character.
997 * Used for deletion.
998 */
999 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
zeertzjq048761b2024-02-24 14:21:39 +01001002 char_u *line;
1003 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001005 if (!oap->inclusive)
1006 return;
1007
zeertzjq048761b2024-02-24 14:21:39 +01001008 line = ml_get(oap->end.lnum);
1009 ptr = line + oap->end.col;
1010 if (*ptr != NUL)
1011 {
1012 ptr -= (*mb_head_off)(line, ptr);
1013 ptr += (*mb_ptr2len)(ptr) - 1;
1014 oap->end.col = ptr - line;
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaar630afe82018-06-28 19:26:28 +02001018/*
1019 * Replace the character under the cursor with "c".
1020 * This takes care of multi-byte characters.
1021 */
1022 static void
1023replace_character(int c)
1024{
1025 int n = State;
1026
Bram Moolenaar24959102022-05-07 20:01:16 +01001027 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001028 ins_char(c);
1029 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001030 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001031 dec_cursor();
1032}
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/*
1035 * Replace a whole area with one character.
1036 */
1037 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001038op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
1040 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +02001043 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001045 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001046 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
1048 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001051 if (c == REPLACE_CR_NCHAR)
1052 {
1053 had_ctrl_v_cr = TRUE;
1054 c = CAR;
1055 }
1056 else if (c == REPLACE_NL_NCHAR)
1057 {
1058 had_ctrl_v_cr = TRUE;
1059 c = NL;
1060 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (has_mbyte)
1063 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 if (u_save((linenr_T)(oap->start.lnum - 1),
1066 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1067 return FAIL;
1068
1069 /*
1070 * block mode replace
1071 */
1072 if (oap->block_mode)
1073 {
1074 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1075 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1079 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001080 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001082 // n == number of extra chars required
1083 // If we split a TAB, it may be replaced by several characters.
1084 // Thus the number of characters may increase!
1085 // If the range starts in virtual space, count the initial
1086 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1088 {
1089 pos_T vpos;
1090
Bram Moolenaara1381de2009-11-03 15:44:21 +00001091 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 getvpos(&vpos, oap->start_vcol);
1093 bd.startspaces += vpos.coladd;
1094 n = bd.startspaces;
1095 }
1096 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001097 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001104 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 numc = oap->end_vcol - oap->start_vcol + 1;
1106 if (bd.is_short && (!virtual_op || bd.is_MAX))
1107 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001109 // A double-wide character can be replaced only up to half the
1110 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if ((*mb_char2cells)(c) > 1)
1112 {
1113 if ((numc & 1) && !bd.is_short)
1114 {
1115 ++bd.endspaces;
1116 ++n;
1117 }
1118 numc = numc / 2;
1119 }
1120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001121 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 num_chars = numc;
1123 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001124 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 n += numc - bd.textlen;
1126
1127 oldp = ml_get_curline();
zeertzjq94b7c322024-03-12 21:50:32 +01001128 oldlen = ml_get_curline_len();
Bram Moolenaar964b3742019-05-24 18:54:09 +02001129 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (newp == NULL)
1131 continue;
1132 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001133 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001135 newlen = bd.textcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // insert pre-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001137 vim_memset(newp + newlen, ' ', (size_t)bd.startspaces);
1138 newlen += bd.startspaces;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001139 // insert replacement chars CHECK FOR ALLOCATED SPACE
1140 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1141 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001142 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001144 if (has_mbyte)
1145 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001146 while (--num_chars >= 0)
John Marriott38b9f452024-04-25 21:39:18 +02001147 newlen += (*mb_char2bytes)(c, newp + newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001148 }
1149 else
John Marriott38b9f452024-04-25 21:39:18 +02001150 {
1151 vim_memset(newp + newlen, c, (size_t)numc);
1152 newlen += numc;
1153 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001154 if (!bd.is_short)
1155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // insert post-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001157 vim_memset(newp + newlen, ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001158 // copy the part after the changed part
John Marriott38b9f452024-04-25 21:39:18 +02001159 STRCPY(newp + newlen + bd.endspaces,
1160 oldp + bd.textcol + bd.textlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001165 // Replacing with \r or \n means splitting the line.
John Marriott38b9f452024-04-25 21:39:18 +02001166 after_p = alloc(oldlen + 1 + n - newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001167 if (after_p != NULL)
John Marriott38b9f452024-04-25 21:39:18 +02001168 STRCPY(after_p, oldp + bd.textcol + bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
John Marriott38b9f452024-04-25 21:39:18 +02001170
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001171 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001173 if (after_p != NULL)
1174 {
1175 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1176 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1177 oap->end.lnum++;
1178 vim_free(after_p);
1179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
1181 }
1182 else
1183 {
1184 /*
1185 * MCHAR and MLINE motion replace.
1186 */
1187 if (oap->motion_type == MLINE)
1188 {
1189 oap->start.col = 0;
1190 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001191 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (oap->end.col)
1193 --oap->end.col;
1194 }
1195 else if (!oap->inclusive)
1196 dec(&(oap->end));
1197
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001198 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001200 int done = FALSE;
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 n = gchar_cursor();
1203 if (n != NUL)
1204 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001205 int new_byte_len = (*mb_char2len)(c);
1206 int old_byte_len = mb_ptr2len(ml_get_cursor());
1207
1208 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 // This is slow, but it handles replacing a single-byte
1211 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001212 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001213 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001214 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001215 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (n == TAB)
1220 {
1221 int end_vcol = 0;
1222
1223 if (curwin->w_cursor.lnum == oap->end.lnum)
1224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001225 // oap->end has to be recalculated when
1226 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 end_vcol = getviscol2(oap->end.col,
1228 oap->end.coladd);
1229 }
1230 coladvance_force(getviscol());
1231 if (curwin->w_cursor.lnum == oap->end.lnum)
1232 getvpos(&oap->end, end_vcol);
1233 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001234 // with "coladd" set may move to just after a TAB
1235 if (gchar_cursor() != NUL)
1236 {
1237 PBYTE(curwin->w_cursor, c);
1238 done = TRUE;
1239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001242 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 int virtcols = oap->end.coladd;
1245
1246 if (curwin->w_cursor.lnum == oap->start.lnum
1247 && oap->start.col == oap->end.col && oap->start.coladd)
1248 virtcols -= oap->start.coladd;
1249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 // oap->end has been trimmed so it's effectively inclusive;
1251 // as a result an extra +1 must be counted so we don't
1252 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1254 curwin->w_cursor.col -= (virtcols + 1);
1255 for (; virtcols >= 0; virtcols--)
1256 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001257 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001258 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001259 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001260 PBYTE(curwin->w_cursor, c);
1261 if (inc(&curwin->w_cursor) == -1)
1262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001266 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (inc_cursor() == -1)
1268 break;
1269 }
1270 }
1271
1272 curwin->w_cursor = oap->start;
1273 check_cursor();
1274 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1275
Bram Moolenaare1004402020-10-24 20:49:43 +02001276 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001277 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001278 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001279 curbuf->b_op_start = oap->start;
1280 curbuf->b_op_end = oap->end;
1281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 return OK;
1284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001286static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288/*
1289 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1290 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001296 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298 if (u_save((linenr_T)(oap->start.lnum - 1),
1299 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1300 return;
1301
1302 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001303 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
1305 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1306 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001307 int one_change;
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 block_prep(oap, &bd, pos.lnum, FALSE);
1310 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001311 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1312 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001313
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001314#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001315 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001317 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
Bram Moolenaar009b2592004-10-24 19:18:58 +00001319 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1320 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001321 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001322 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001324 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 if (did_change)
1329 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1330 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
1333 if (oap->motion_type == MLINE)
1334 {
1335 oap->start.col = 0;
1336 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001337 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (oap->end.col)
1339 --oap->end.col;
1340 }
1341 else if (!oap->inclusive)
1342 dec(&(oap->end));
1343
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001344 if (pos.lnum == oap->end.lnum)
1345 did_change = swapchars(oap->op_type, &pos,
1346 oap->end.col - pos.col + 1);
1347 else
1348 for (;;)
1349 {
1350 did_change |= swapchars(oap->op_type, &pos,
zeertzjq94b7c322024-03-12 21:50:32 +01001351 pos.lnum == oap->end.lnum ? oap->end.col + 1
1352 : ml_get_pos_len(&pos));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001353 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001354 break;
1355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (did_change)
1357 {
1358 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1359 0L);
1360#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001361 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
1363 char_u *ptr;
1364 int count;
1365
1366 pos = oap->start;
1367 while (pos.lnum < oap->end.lnum)
1368 {
1369 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001370 count = ml_get_buf_len(curbuf, pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001371 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001372 // get the line again, it may have been flushed
1373 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001375 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 pos.col = 0;
1377 pos.lnum++;
1378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001380 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001381 // get the line again, it may have been flushed
1382 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001384 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386#endif
1387 }
1388 }
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001391 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001392 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaare1004402020-10-24 20:49:43 +02001394 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001395 {
1396 // Set '[ and '] marks.
1397 curbuf->b_op_start = oap->start;
1398 curbuf->b_op_end = oap->end;
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
1401 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001403 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404}
1405
1406/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001407 * Invoke swapchar() on "length" bytes at position "pos".
1408 * "pos" is advanced to just after the changed characters.
1409 * "length" is rounded up to include the whole last multi-byte character.
1410 * Also works correctly when the number of bytes changes.
1411 * Returns TRUE if some character was changed.
1412 */
1413 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001414swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001415{
1416 int todo;
1417 int did_change = 0;
1418
1419 for (todo = length; todo > 0; --todo)
1420 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001421 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001422 {
1423 int len = (*mb_ptr2len)(ml_get_pos(pos));
1424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001426 if (len > 0)
1427 todo -= len - 1;
1428 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001429 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001430 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001431 break;
1432 }
1433 return did_change;
1434}
1435
1436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 * If op_type == OP_UPPER: make uppercase,
1438 * if op_type == OP_LOWER: make lowercase,
1439 * if op_type == OP_ROT13: do rot13 encoding,
1440 * else swap case of character at 'pos'
1441 * returns TRUE when something actually changed.
1442 */
1443 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001444swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 int c;
1447 int nc;
1448
1449 c = gchar_pos(pos);
1450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001451 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (c >= 0x80 && op_type == OP_ROT13)
1453 return FALSE;
1454
glepnirbd1232a2024-02-12 22:14:53 +01001455 // ~ is OP_NOP, g~ is OP_TILDE, gU is OP_UPPER
1456 if ((op_type == OP_UPPER || op_type == OP_NOP || op_type == OP_TILDE)
1457 && c == 0xdf
1458 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001459 {
1460 pos_T sp = curwin->w_cursor;
1461
glepnirbd1232a2024-02-12 22:14:53 +01001462 // Special handling for lowercase German sharp s (ß): convert to uppercase (ẞ).
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001463 curwin->w_cursor = *pos;
1464 del_char(FALSE);
glepnirbd1232a2024-02-12 22:14:53 +01001465 ins_char(0x1E9E);
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001466 curwin->w_cursor = sp;
glepnirbd1232a2024-02-12 22:14:53 +01001467 return TRUE;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001468 }
1469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001470 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 nc = c;
1473 if (MB_ISLOWER(c))
1474 {
1475 if (op_type == OP_ROT13)
1476 nc = ROT13(c, 'a');
1477 else if (op_type != OP_LOWER)
1478 nc = MB_TOUPPER(c);
1479 }
1480 else if (MB_ISUPPER(c))
1481 {
1482 if (op_type == OP_ROT13)
1483 nc = ROT13(c, 'A');
1484 else if (op_type != OP_UPPER)
1485 nc = MB_TOLOWER(c);
1486 }
1487 if (nc != c)
1488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1490 {
1491 pos_T sp = curwin->w_cursor;
1492
1493 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001494 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001495 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ins_char(nc);
1497 curwin->w_cursor = sp;
1498 }
1499 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001500 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 return TRUE;
1502 }
1503 return FALSE;
1504}
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506/*
1507 * op_insert - Insert and append operators for Visual mode.
1508 */
1509 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001510op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
zeertzjq8c55d602024-03-13 20:42:26 +01001512 long pre_textlen = 0;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001513 colnr_T ind_pre_col = 0, ind_post_col;
1514 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 struct block_def bd;
1516 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001517 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001518 pos_T start_insert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001520 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1522
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001523 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001525 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 if (oap->block_mode)
1528 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 // When 'virtualedit' is used, need to insert the extra spaces before
1530 // doing block_prep(). When only "block" is used, virtual edit is
1531 // already disabled, but still need it when calling
1532 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001533 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001534 // state for the current window. To override that state, we need to
1535 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (curwin->w_cursor.coladd > 0)
1537 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001538 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (u_save_cursor() == FAIL)
1541 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001542
Gary Johnson51ad8502021-08-03 18:33:08 +02001543 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 coladvance_force(oap->op_type == OP_APPEND
1545 ? oap->end_vcol + 1 : getviscol());
1546 if (oap->op_type == OP_APPEND)
1547 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001548 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001550 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001552 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001553 ind_pre_col = (colnr_T)getwhitecols_curline();
1554 ind_pre_vcol = get_indent();
zeertzjq94b7c322024-03-12 21:50:32 +01001555 pre_textlen = ml_get_len(oap->start.lnum) - bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (oap->op_type == OP_APPEND)
zeertzjq94b7c322024-03-12 21:50:32 +01001557 pre_textlen -= bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559
1560 if (oap->op_type == OP_APPEND)
1561 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001562 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001564 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 curwin->w_set_curswant = TRUE;
1566 while (*ml_get_cursor() != NUL
1567 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1568 ++curwin->w_cursor.col;
1569 if (bd.is_short && !bd.is_MAX)
1570 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001571 // First line was too short, make it longer and adjust the
1572 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (u_save_cursor() == FAIL)
1574 return;
1575 for (i = 0; i < bd.endspaces; ++i)
1576 ins_char(' ');
1577 bd.textlen += bd.endspaces;
1578 }
1579 }
1580 else
1581 {
1582 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001583 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001585 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001586 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 && oap->start_vcol != oap->end_vcol)
1588 inc_cursor();
1589 }
1590 }
1591
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001592 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001593 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001594 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // When a tab was inserted, and the characters in front of the tab
1597 // have been converted to a tab as well, the column of the cursor
1598 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001599 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001600 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001601 oap->start = curbuf->b_op_start_orig;
1602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001603 // If user has moved off this line, we don't know what to do, so do
1604 // nothing.
1605 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001606 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 return;
1608
1609 if (oap->block_mode)
1610 {
Christian Brabandtd5c8c092024-05-08 22:17:19 +02001611 int ins_len;
zeertzjq8c55d602024-03-13 20:42:26 +01001612 char_u *firstline, *ins_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001614 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001615 size_t len;
Mike Williamsaca8f552024-04-07 18:26:22 +02001616 size_t add;
zeertzjq8c55d602024-03-13 20:42:26 +01001617 // offset when cursor was moved in insert mode
1618 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001620 // If indent kicked in, the firstline might have changed
1621 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001622 ind_post_col = (colnr_T)getwhitecols_curline();
1623 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001624 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001625 bd.textcol += ind_post_col - ind_pre_col;
1626 ind_post_vcol = get_indent();
1627 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001628 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001629 }
1630
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001631 // The user may have moved the cursor before inserting something, try
1632 // to adjust the block for that. But only do it, if the difference
1633 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001634 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1635 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001636 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001637 int t = getviscol2(curbuf->b_op_start_orig.col,
1638 curbuf->b_op_start_orig.coladd);
1639
zeertzjq7745f142022-02-15 11:48:22 +00001640 if (oap->op_type == OP_INSERT
1641 && oap->start.col + oap->start.coladd
1642 != curbuf->b_op_start_orig.col
1643 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001644 {
zeertzjq7745f142022-02-15 11:48:22 +00001645 oap->start.col = curbuf->b_op_start_orig.col;
1646 pre_textlen -= t - oap->start_vcol;
1647 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001648 }
zeertzjq7745f142022-02-15 11:48:22 +00001649 else if (oap->op_type == OP_APPEND
1650 && oap->start.col + oap->start.coladd
1651 >= curbuf->b_op_start_orig.col
1652 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001653 {
zeertzjq7745f142022-02-15 11:48:22 +00001654 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001655 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001656 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001657 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001658 oap->start_vcol = t;
1659 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001660 }
1661 }
1662
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001663 // Spaces and tabs in the indent may have changed to other spaces and
1664 // tabs. Get the starting column again and correct the length.
1665 // Don't do this when "$" used, end-of-line will have changed.
1666 //
1667 // if indent was added and the inserted text was after the indent,
1668 // correct the selection for the new indent.
1669 if (did_indent && bd.textcol - ind_post_col > 0)
1670 {
1671 oap->start.col += ind_post_col - ind_pre_col;
1672 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1673 oap->end.col += ind_post_col - ind_pre_col;
1674 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001677 if (did_indent && bd.textcol - ind_post_col > 0)
1678 {
1679 // undo for where "oap" is used below
1680 oap->start.col -= ind_post_col - ind_pre_col;
1681 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1682 oap->end.col -= ind_post_col - ind_pre_col;
1683 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1686 {
1687 if (oap->op_type == OP_APPEND)
1688 {
1689 pre_textlen += bd2.textlen - bd.textlen;
1690 if (bd2.endspaces)
1691 --bd2.textlen;
1692 }
1693 bd.textcol = bd2.textcol;
1694 bd.textlen = bd2.textlen;
1695 }
1696
1697 /*
1698 * Subsequent calls to ml_get() flush the firstline data - take a
1699 * copy of the required string.
1700 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001701 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001702 len = ml_get_len(oap->start.lnum);
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001703 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001705 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001706 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001707 // account for pressing cursor in insert mode when '$' was used
1708 if (bd.is_MAX
1709 && (start_insert.lnum == Insstart.lnum
1710 && start_insert.col > Insstart.col))
1711 {
1712 offset = (start_insert.col - Insstart.col);
1713 add -= offset;
1714 if (oap->end_vcol > offset)
1715 oap->end_vcol -= (offset + 1);
1716 else
1717 // moved outside of the visual block, what to do?
1718 return;
1719 }
1720 }
Mike Williamsaca8f552024-04-07 18:26:22 +02001721 if (add > len)
zeertzjq94b7c322024-03-12 21:50:32 +01001722 add = len; // short line, point to the NUL
1723 firstline += add;
1724 len -= add;
1725 if (pre_textlen >= 0 && (ins_len = len - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001727 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (ins_text != NULL)
1729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (u_save(oap->start.lnum,
1732 (linenr_T)(oap->end.lnum + 1)) == OK)
John Marriott38b9f452024-04-25 21:39:18 +02001733 block_insert(oap, ins_text, ins_len, (oap->op_type == OP_INSERT),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 &bd);
1735
1736 curwin->w_cursor.col = oap->start.col;
1737 check_cursor();
1738 vim_free(ins_text);
1739 }
1740 }
1741 }
1742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744/*
1745 * op_change - handle a change operation
1746 *
1747 * return TRUE if edit() returns because of a CTRL-O command
1748 */
1749 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 colnr_T l;
1753 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001755 long pre_textlen = 0;
1756 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 char_u *firstline;
1758 char_u *ins_text, *newp, *oldp;
1759 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761 l = oap->start.col;
1762 if (oap->motion_type == MLINE)
1763 {
1764 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001765 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001768 // First delete the text in the region. In an empty buffer only need to
1769 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1771 {
1772 if (u_save_cursor() == FAIL)
1773 return FALSE;
1774 }
1775 else if (op_delete(oap) == FAIL)
1776 return FALSE;
1777
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001778 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 && !virtual_op)
1780 inc_cursor();
1781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001782 // check for still on same line (<CR> in inserted text meaningless)
1783 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (oap->block_mode)
1785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001786 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (virtual_op && (curwin->w_cursor.coladd > 0
1788 || gchar_cursor() == NUL))
1789 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001790 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001791 pre_textlen = ml_get_len(oap->start.lnum);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001792 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 bd.textcol = curwin->w_cursor.col;
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (oap->motion_type == MLINE)
1797 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
zeertzjqcdeb6572022-11-15 13:46:12 +00001799 // Reset finish_op now, don't want it set inside edit().
1800 int save_finish_op = finish_op;
1801 finish_op = FALSE;
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 retval = edit(NUL, FALSE, (linenr_T)1);
1804
zeertzjqcdeb6572022-11-15 13:46:12 +00001805 finish_op = save_finish_op;
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001808 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001810 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001812 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
John Marriott38b9f452024-04-25 21:39:18 +02001814 size_t ins_len;
1815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001816 // Auto-indenting may have changed the indent. If the cursor was past
1817 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001819 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001821 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001822
1823 pre_textlen += new_indent - pre_indent;
1824 bd.textcol += new_indent - pre_indent;
1825 }
1826
zeertzjq94b7c322024-03-12 21:50:32 +01001827 ins_len = ml_get_len(oap->start.lnum) - pre_textlen;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001828 if (ins_len > 0)
1829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 // Subsequent calls to ml_get() flush the firstline data - take a
1831 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001832 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
John Marriott38b9f452024-04-25 21:39:18 +02001834 vim_strncpy(ins_text, firstline + bd.textcol, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1836 linenr++)
1837 {
1838 block_prep(oap, &bd, linenr, TRUE);
1839 if (!bd.is_short || virtual_op)
1840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 pos_T vpos;
John Marriott38b9f452024-04-25 21:39:18 +02001842 size_t newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001844 // If the block starts in virtual space, count the
1845 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (bd.is_short)
1847 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001848 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851 else
1852 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 oldp = ml_get(linenr);
John Marriott38b9f452024-04-25 21:39:18 +02001854 newp = alloc(ml_get_len(linenr) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (newp == NULL)
1856 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001857 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001859 newlen = bd.textcol;
1860 vim_memset(newp + newlen, ' ', (size_t)vpos.coladd);
1861 newlen += vpos.coladd;
1862 mch_memmove(newp + newlen, ins_text, ins_len);
1863 STRCPY(newp + newlen + ins_len, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001865#ifdef FEAT_PROP_POPUP
1866 // Shift the properties for linenr as edit() would do.
1867 if (curbuf->b_has_textprop)
1868 adjust_prop_columns(linenr, bd.textcol,
1869 vpos.coladd + ins_len, 0);
1870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 }
1873 check_cursor();
1874
1875 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1876 }
1877 vim_free(ins_text);
1878 }
1879 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001880 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
1882 return retval;
1883}
1884
1885/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001886 * When the cursor is on the NUL past the end of the line and it should not be
1887 * there move it left.
1888 */
1889 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001890adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001891{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001892 unsigned int cur_ve_flags = get_ve_flags();
1893
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001894 int adj_cursor = (curwin->w_cursor.col > 0
1895 && gchar_cursor() == NUL
1896 && (cur_ve_flags & VE_ONEMORE) == 0
1897 && !(restart_edit || (State & MODE_INSERT)));
1898 if (!adj_cursor)
1899 return;
1900
1901 // Put the cursor on the last character in the line.
1902 dec_cursor();
1903
1904 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001906 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001907
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001908 // Coladd is set to the width of the last character.
1909 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1910 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912}
1913
Bram Moolenaar81340392012-06-06 16:12:59 +02001914/*
1915 * If "process" is TRUE and the line begins with a comment leader (possibly
1916 * after some white space), return a pointer to the text after it. Put a boolean
1917 * value indicating whether the line ends with an unclosed comment in
1918 * "is_comment".
1919 * line - line to be processed,
1920 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001921 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001922 * include_space - whether to also skip space following the comment leader,
1923 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001924 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001926 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001927skip_comment(
1928 char_u *line,
1929 int process,
1930 int include_space,
1931 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001932{
1933 char_u *comment_flags = NULL;
1934 int lead_len;
1935 int leader_offset = get_last_leader_offset(line, &comment_flags);
1936
1937 *is_comment = FALSE;
1938 if (leader_offset != -1)
1939 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001940 // Let's check whether the line ends with an unclosed comment.
1941 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001942 while (*comment_flags)
1943 {
1944 if (*comment_flags == COM_END
1945 || *comment_flags == ':')
1946 break;
1947 ++comment_flags;
1948 }
1949 if (*comment_flags != COM_END)
1950 *is_comment = TRUE;
1951 }
1952
1953 if (process == FALSE)
1954 return line;
1955
1956 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1957
1958 if (lead_len == 0)
1959 return line;
1960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 // Find:
1962 // - COM_END,
1963 // - colon,
1964 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001965 while (*comment_flags)
1966 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001967 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001968 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001969 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001970 ++comment_flags;
1971 }
1972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001973 // If we found a colon, it means that we are not processing a line
1974 // starting with a closing part of a three-part comment. That's good,
1975 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001976 if (*comment_flags == ':' || *comment_flags == NUL)
1977 line += lead_len;
1978
1979 return line;
1980}
Bram Moolenaar81340392012-06-06 16:12:59 +02001981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001983 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001984 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001985 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1986 * leaders should not be removed.
1987 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1988 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001990 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 */
1992 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001993do_join(
1994 long count,
1995 int insert_space,
1996 int save_undo,
1997 int use_formatoptions UNUSED,
1998 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002000 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002001 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002002 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002004 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002005 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02002006 int endcurr1 = NUL;
2007 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002008 int currsize = 0; // size of the current line
2009 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002011 colnr_T col = 0;
2012 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02002013 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002014 int remove_comments = (use_formatoptions == TRUE)
2015 && has_format_option(FO_REMOVE_COMS);
2016 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002017#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002018 int propcount = 0; // number of props over all joined lines
2019 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002022 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2023 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2024 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002026 // Allocate an array to store the number of spaces inserted before each
2027 // line. We will use it to pre-compute the length of the new line and the
2028 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002029 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002030 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002032 if (remove_comments)
2033 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002034 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002035 if (comments == NULL)
2036 {
2037 vim_free(spaces);
2038 return FAIL;
2039 }
2040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002043 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002045 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002047 for (t = 0; t < count; ++t)
2048 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002049 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002050#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002051 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2052 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002053#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002054 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002055 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002056 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002057 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2058 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2059 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002060 if (remove_comments)
2061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002062 // We don't want to remove the comment leader if the
2063 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002064 if (t > 0 && prev_was_comment)
2065 {
2066
2067 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2068 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002069 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002070 curr = new_curr;
2071 }
2072 else
2073 curr = skip_comment(curr, FALSE, insert_space,
2074 &prev_was_comment);
2075 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002076
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002077 if (insert_space && t > 0)
2078 {
2079 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002080 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002081 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002082 && (!has_format_option(FO_MBYTE_JOIN)
2083 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2084 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002085 || (mb_ptr2char(curr) < 0x100
2086 && !(enc_utf8 && utf_eat_space(endcurr1)))
2087 || (endcurr1 < 0x100
2088 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002089 )
2090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002091 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002092 if (endcurr1 == ' ')
2093 endcurr1 = endcurr2;
2094 else
2095 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002096 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002097 if ( p_js
2098 && (endcurr1 == '.'
2099 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2100 && (endcurr1 == '?' || endcurr1 == '!'))))
2101 ++spaces[t];
2102 }
2103 }
2104 currsize = (int)STRLEN(curr);
2105 sumsize += currsize + spaces[t];
2106 endcurr1 = endcurr2 = NUL;
2107 if (insert_space && currsize > 0)
2108 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002109 if (has_mbyte)
2110 {
2111 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002112 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002113 endcurr1 = (*mb_ptr2char)(cend);
2114 if (cend > curr)
2115 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002116 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002117 endcurr2 = (*mb_ptr2char)(cend);
2118 }
2119 }
2120 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002121 {
2122 endcurr1 = *(curr + currsize - 1);
2123 if (currsize > 1)
2124 endcurr2 = *(curr + currsize - 2);
2125 }
2126 }
2127 line_breakcheck();
2128 if (got_int)
2129 {
2130 ret = FAIL;
2131 goto theend;
2132 }
2133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002136 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002138 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002139 newp_len = sumsize + 1;
2140#ifdef FEAT_PROP_POPUP
2141 newp_len += propcount * sizeof(textprop_T);
2142#endif
2143 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002144 if (newp == NULL)
2145 {
2146 ret = FAIL;
2147 goto theend;
2148 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002149 cend = newp + sumsize;
2150 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002152 /*
2153 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002154 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002155 *
2156 * Move marks from each deleted line to the joined line, adjusting the
2157 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2158 * should not really be a problem.
2159 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002160#ifdef FEAT_PROP_POPUP
2161 props_remaining = propcount;
2162#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002163 for (t = count - 1; ; --t)
2164 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002165 int spaces_removed;
2166
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002167 cend -= currsize;
2168 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002169
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 if (spaces[t] > 0)
2171 {
2172 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002173 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002174 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002175
2176 // If deleting more spaces than adding, the cursor moves no more than
2177 // what is added if it is inside these spaces.
2178 spaces_removed = (curr - curr_start) - spaces[t];
2179
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002180 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002181 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002182#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002183 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2184 curwin->w_cursor.lnum + t, t == count - 1,
2185 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002186#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002187 if (t == 0)
2188 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002189 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002190 if (remove_comments)
2191 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002192 if (insert_space && t > 1)
2193 curr = skipwhite(curr);
2194 currsize = (int)STRLEN(curr);
2195 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002196
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002197 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
Bram Moolenaare1004402020-10-24 20:49:43 +02002199 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002202 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002203 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002204 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 // Only report the change in the first line here, del_lines() will report
2207 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 changed_lines(curwin->w_cursor.lnum, currsize,
2209 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002211 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * briefly, and then move it back. After del_lines() the cursor may
2213 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 */
2215 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002217 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 curwin->w_cursor.lnum = t;
2219
2220 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002221 * Set the cursor column:
2222 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002223 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002225 curwin->w_cursor.col =
2226 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 curwin->w_set_curswant = TRUE;
2231
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002232theend:
2233 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002234 if (remove_comments)
2235 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002236 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237}
2238
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002239#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002241 * Reset 'linebreak' and take care of side effects.
2242 * Returns the previous value, to be passed to restore_lbr().
2243 */
2244 static int
2245reset_lbr(void)
2246{
2247 if (!curwin->w_p_lbr)
2248 return FALSE;
2249 // changing 'linebreak' may require w_virtcol to be updated
2250 curwin->w_p_lbr = FALSE;
2251 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2252 return TRUE;
2253}
2254
2255/*
2256 * Restore 'linebreak' and take care of side effects.
2257 */
2258 static void
2259restore_lbr(int lbr_saved)
2260{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002261 if (curwin->w_p_lbr || !lbr_saved)
2262 return;
2263
2264 // changing 'linebreak' may require w_virtcol to be updated
2265 curwin->w_p_lbr = TRUE;
2266 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002267}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002268#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002269
2270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * prepare a few things for block mode yank/delete/tilde
2272 *
2273 * for delete:
2274 * - textlen includes the first/last char to be (partly) deleted
2275 * - start/endspaces is the number of columns that are taken by the
2276 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002277 * deleted.
2278 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 * - textlen includes the first/last char to be wholly yanked
2280 * - start/endspaces is the number of columns of the first/last yanked char
2281 * that are to be yanked.
2282 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002283 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284block_prep(
2285 oparg_T *oap,
2286 struct block_def *bdp,
2287 linenr_T lnum,
2288 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 int incr = 0;
2291 char_u *pend;
2292 char_u *pstart;
2293 char_u *line;
2294 char_u *prev_pstart;
2295 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002296 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002297#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002298 // Avoid a problem with unwanted linebreaks in block mode.
2299 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002300#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 bdp->startspaces = 0;
2303 bdp->endspaces = 0;
2304 bdp->textlen = 0;
2305 bdp->start_vcol = 0;
2306 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 bdp->is_short = FALSE;
2308 bdp->is_oneChar = FALSE;
2309 bdp->pre_whitesp = 0;
2310 bdp->pre_whitesp_c = 0;
2311 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 bdp->start_char_vcols = 0;
2313
2314 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002316 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2317 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002319 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002320 incr = lbr_chartabsize(&cts);
2321 cts.cts_vcol += incr;
2322 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 bdp->pre_whitesp += incr;
2325 bdp->pre_whitesp_c++;
2326 }
2327 else
2328 {
2329 bdp->pre_whitesp = 0;
2330 bdp->pre_whitesp_c = 0;
2331 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002332 prev_pstart = cts.cts_ptr;
2333 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002335 bdp->start_vcol = cts.cts_vcol;
2336 pstart = cts.cts_ptr;
2337 clear_chartabsize_arg(&cts);
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (!is_del || oap->op_type == OP_APPEND)
2345 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2346 }
2347 else
2348 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 // notice: this converts partly selected Multibyte characters to
2350 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2352 if (is_del && bdp->startspaces)
2353 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2354 pend = pstart;
2355 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002356 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (oap->op_type == OP_INSERT)
2360 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2361 else if (oap->op_type == OP_APPEND)
2362 {
2363 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2364 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2365 }
2366 else
2367 {
2368 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2369 if (is_del && oap->op_type != OP_LSHIFT)
2370 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 // just putting the sum of those two into
2372 // bdp->startspaces doesn't work for Visual replace,
2373 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 bdp->startspaces = bdp->start_char_vcols
2375 - (bdp->start_vcol - oap->start_vcol);
2376 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2377 }
2378 }
2379 }
2380 else
2381 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002382 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2383 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002385 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002387 // count a tab for what it's worth (if list mode not on)
2388 prev_pend = cts.cts_ptr;
2389 incr = lbr_chartabsize_adv(&cts);
2390 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002392 bdp->end_vcol = cts.cts_vcol;
2393 pend = cts.cts_ptr;
2394 clear_chartabsize_arg(&cts);
2395
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (bdp->end_vcol <= oap->end_vcol
2397 && (!is_del
2398 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002399 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 // Alternative: include spaces to fill up the block.
2403 // Disadvantage: can lead to trailing spaces when the line is
2404 // short where the text is put
2405 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (oap->op_type == OP_APPEND || virtual_op)
2407 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002408 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002410 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412 else if (bdp->end_vcol > oap->end_vcol)
2413 {
2414 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2415 if (!is_del && bdp->endspaces)
2416 {
2417 bdp->endspaces = incr - bdp->endspaces;
2418 if (pend != pstart)
2419 pend = prev_pend;
2420 }
2421 }
2422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (is_del && bdp->startspaces)
2425 pstart = prev_pstart;
2426 bdp->textlen = (int)(pend - pstart);
2427 }
2428 bdp->textcol = (colnr_T) (pstart - line);
2429 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002430#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002431 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435/*
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002436 * Get block text from "start" to "end"
2437 */
2438 void
2439charwise_block_prep(
2440 pos_T start,
2441 pos_T end,
2442 struct block_def *bdp,
2443 linenr_T lnum,
2444 int inclusive)
2445{
2446 colnr_T startcol = 0, endcol = MAXCOL;
2447 int is_oneChar = FALSE;
2448 colnr_T cs, ce;
2449 char_u *p;
2450
2451 p = ml_get(lnum);
2452 bdp->startspaces = 0;
2453 bdp->endspaces = 0;
2454
2455 if (lnum == start.lnum)
2456 {
2457 startcol = start.col;
2458 if (virtual_op)
2459 {
2460 getvcol(curwin, &start, &cs, NULL, &ce);
2461 if (ce != cs && start.coladd > 0)
2462 {
2463 // Part of a tab selected -- but don't
2464 // double-count it.
2465 bdp->startspaces = (ce - cs + 1)
2466 - start.coladd;
2467 if (bdp->startspaces < 0)
2468 bdp->startspaces = 0;
2469 startcol++;
2470 }
2471 }
2472 }
2473
2474 if (lnum == end.lnum)
2475 {
2476 endcol = end.col;
2477 if (virtual_op)
2478 {
2479 getvcol(curwin, &end, &cs, NULL, &ce);
2480 if (p[endcol] == NUL || (cs + end.coladd < ce
2481 // Don't add space for double-wide
2482 // char; endcol will be on last byte
2483 // of multi-byte char.
2484 && (*mb_head_off)(p, p + endcol) == 0))
2485 {
2486 if (start.lnum == end.lnum
2487 && start.col == end.col)
2488 {
2489 // Special case: inside a single char
2490 is_oneChar = TRUE;
2491 bdp->startspaces = end.coladd
2492 - start.coladd + inclusive;
2493 endcol = startcol;
2494 }
2495 else
2496 {
2497 bdp->endspaces = end.coladd
2498 + inclusive;
2499 endcol -= inclusive;
2500 }
2501 }
2502 }
2503 }
2504 if (endcol == MAXCOL)
zeertzjq94b7c322024-03-12 21:50:32 +01002505 endcol = ml_get_len(lnum);
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002506 if (startcol > endcol || is_oneChar)
2507 bdp->textlen = 0;
2508 else
2509 bdp->textlen = endcol - startcol + inclusive;
2510 bdp->textstart = p + startcol;
2511}
2512
2513/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002514 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002517op_addsub(
2518 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002519 linenr_T Prenum1, // Amount of add/subtract
2520 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002522 pos_T pos;
2523 struct block_def bd;
2524 int change_cnt = 0;
2525 linenr_T amount = Prenum1;
2526
Bram Moolenaar6b731882018-11-16 20:54:47 +01002527 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002528 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002529 // the call to changed_lines().
2530#ifdef FEAT_FOLDING
2531 disable_fold_update++;
2532#endif
2533
Bram Moolenaard79e5502016-01-10 22:13:02 +01002534 if (!VIsual_active)
2535 {
2536 pos = curwin->w_cursor;
2537 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002538 {
2539#ifdef FEAT_FOLDING
2540 disable_fold_update--;
2541#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002542 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002543 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002544 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002545#ifdef FEAT_FOLDING
2546 disable_fold_update--;
2547#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002548 if (change_cnt)
2549 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2550 }
2551 else
2552 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002553 int one_change;
2554 int length;
2555 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002556
2557 if (u_save((linenr_T)(oap->start.lnum - 1),
2558 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002559 {
2560#ifdef FEAT_FOLDING
2561 disable_fold_update--;
2562#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002563 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002564 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002565
2566 pos = oap->start;
2567 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2568 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002569 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002570 {
2571 block_prep(oap, &bd, pos.lnum, FALSE);
2572 pos.col = bd.textcol;
2573 length = bd.textlen;
2574 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002575 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002576 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002577 curwin->w_cursor.col = 0;
2578 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01002579 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002580 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002581 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002582 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002583 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002584 dec(&(oap->end));
zeertzjq94b7c322024-03-12 21:50:32 +01002585 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002586 pos.col = 0;
2587 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002588 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002589 pos.col += oap->start.col;
2590 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002591 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002592 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002593 {
zeertzjq94b7c322024-03-12 21:50:32 +01002594 length = ml_get_len(oap->end.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002595 if (oap->end.col >= length)
2596 oap->end.col = length - 1;
2597 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002598 }
2599 }
2600 one_change = do_addsub(oap->op_type, &pos, length, amount);
2601 if (one_change)
2602 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002603 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002604 if (change_cnt == 0)
2605 startpos = curbuf->b_op_start;
2606 ++change_cnt;
2607 }
2608
2609#ifdef FEAT_NETBEANS_INTG
2610 if (netbeans_active() && one_change)
2611 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002612 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002613
2614 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002615 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002616 netbeans_inserted(curbuf, pos.lnum, pos.col,
2617 &ptr[pos.col], length);
2618 }
2619#endif
2620 if (g_cmd && one_change)
2621 amount += Prenum1;
2622 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002623
2624#ifdef FEAT_FOLDING
2625 disable_fold_update--;
2626#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002627 if (change_cnt)
2628 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2629
2630 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002631 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002632 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002634 // Set '[ mark if something changed. Keep the last end
2635 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002636 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002637 curbuf->b_op_start = startpos;
2638
2639 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002640 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002641 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002642 }
2643}
2644
2645/*
2646 * Add or subtract 'Prenum1' from a number in a line
2647 * op_type is OP_NR_ADD or OP_NR_SUB
2648 *
2649 * Returns TRUE if some character was changed.
2650 */
2651 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002652do_addsub(
2653 int op_type,
2654 pos_T *pos,
2655 int length,
2656 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002657{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 int col;
2659 char_u *buf1;
2660 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002661 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2662 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002663 uvarnumber_T n;
2664 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 char_u *ptr;
zeertzjq8c55d602024-03-13 20:42:26 +01002666 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002669 int do_hex;
2670 int do_oct;
2671 int do_bin;
2672 int do_alpha;
2673 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002676 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002677 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002678 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002679 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002680 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002681 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002682 pos_T startpos;
2683 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002684 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002686 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2687 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2688 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2689 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2690 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002692 if (virtual_active())
2693 {
2694 save_coladd = pos->coladd;
2695 pos->coladd = 0;
2696 }
2697
Bram Moolenaard79e5502016-01-10 22:13:02 +01002698 curwin->w_cursor = *pos;
2699 ptr = ml_get(pos->lnum);
zeertzjq8c55d602024-03-13 20:42:26 +01002700 linelen = ml_get_len(pos->lnum);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002701 col = pos->col;
2702
zeertzjq8c55d602024-03-13 20:42:26 +01002703 if (col + !!save_coladd >= linelen)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002704 goto theend;
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 /*
2707 * First check if we are on a hexadecimal number, after the "0x".
2708 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002709 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002711 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002712 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002713 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002714 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002715 if (has_mbyte)
2716 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002717 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002718
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002719 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002720 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002721 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002722 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002723 if (has_mbyte)
2724 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002725 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002726
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002727 if ( do_bin
2728 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002729 && ! ((col > 0
2730 && (ptr[col] == 'X'
2731 || ptr[col] == 'x')
2732 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002733 && (!has_mbyte ||
2734 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002735 && vim_isxdigit(ptr[col + 1]))))
2736 {
2737
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002738 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002739
Bram Moolenaard79e5502016-01-10 22:13:02 +01002740 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002741
2742 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002743 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002744 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002745 if (has_mbyte)
2746 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002747 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002748 }
2749
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002750 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002751 && col > 0
2752 && (ptr[col] == 'X'
2753 || ptr[col] == 'x')
2754 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002755 && (!has_mbyte ||
2756 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002757 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002758 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002759 && col > 0
2760 && (ptr[col] == 'B'
2761 || ptr[col] == 'b')
2762 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002763 && (!has_mbyte ||
2764 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002765 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002768 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002769 if (has_mbyte)
2770 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002771 }
2772 else
2773 {
2774 /*
2775 * Search forward and then backward to find the start of number.
2776 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002777 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002778
2779 while (ptr[col] != NUL
2780 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002781 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002782 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002783
2784 while (col > 0
2785 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002786 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002787 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002788 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002789 if (has_mbyte)
2790 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002791 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002792 }
2793 }
2794
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002795 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002796 {
2797 while (ptr[col] != NUL && length > 0
2798 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002799 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002800 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002801 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002802
2803 col += mb_len;
2804 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002805 }
2806
2807 if (length == 0)
2808 goto theend;
2809
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002810 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002811 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2812 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002813 {
2814 negative = TRUE;
2815 was_positive = FALSE;
2816 }
2817 }
2818
2819 /*
2820 * If a number was found, and saving for undo works, replace the number.
2821 */
2822 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002823 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002824 {
2825 beep_flush();
2826 goto theend;
2827 }
2828
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002829 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002830 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002832 if (op_type == OP_NR_SUB)
2833 {
2834 if (CharOrd(firstdigit) < Prenum1)
2835 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002836 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002837 firstdigit = 'A';
2838 else
2839 firstdigit = 'a';
2840 }
2841 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002842 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002843 }
2844 else
2845 {
2846 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2847 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002848 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002849 firstdigit = 'Z';
2850 else
2851 firstdigit = 'z';
2852 }
2853 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002854 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002855 }
2856 curwin->w_cursor.col = col;
2857 if (!did_change)
2858 startpos = curwin->w_cursor;
2859 did_change = TRUE;
2860 (void)del_char(FALSE);
2861 ins_char(firstdigit);
2862 endpos = curwin->w_cursor;
2863 curwin->w_cursor.col = col;
2864 }
2865 else
2866 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002867 pos_T save_pos;
2868 int i;
2869
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002870 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002871 && (!has_mbyte ||
2872 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002873 && !visual
2874 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002877 --col;
2878 negative = TRUE;
2879 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002880 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002881 if (visual && VIsual_mode != 'V')
2882 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
zeertzjq8c55d602024-03-13 20:42:26 +01002883 ? linelen - col : length);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002884
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002885 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002886 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002887 0 + (do_bin ? STR2NR_BIN : 0)
2888 + (do_oct ? STR2NR_OCT : 0)
2889 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002890 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002893 if (pre && negative)
2894 {
2895 ++col;
2896 --length;
2897 negative = FALSE;
2898 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002900 subtract = FALSE;
2901 if (op_type == OP_NR_SUB)
2902 subtract ^= TRUE;
2903 if (negative)
2904 subtract ^= TRUE;
2905
2906 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002907 if (!overflow) // if number is too big don't add/subtract
2908 {
2909 if (subtract)
2910 n -= (uvarnumber_T)Prenum1;
2911 else
2912 n += (uvarnumber_T)Prenum1;
2913 }
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002916 if (!pre)
2917 {
2918 if (subtract)
2919 {
2920 if (n > oldn)
2921 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002922 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002923 negative ^= TRUE;
2924 }
2925 }
2926 else
2927 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002929 if (n < oldn)
2930 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002931 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002932 negative ^= TRUE;
2933 }
2934 }
2935 if (n == 0)
2936 negative = FALSE;
2937 }
2938
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002939 if (do_unsigned && negative)
2940 {
2941 if (subtract)
2942 // sticking at zero.
2943 n = (uvarnumber_T)0;
2944 else
2945 // sticking at 2^64 - 1.
2946 n = (uvarnumber_T)(-1);
2947 negative = FALSE;
2948 }
2949
Bram Moolenaard79e5502016-01-10 22:13:02 +01002950 if (visual && !was_positive && !negative && col > 0)
2951 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002953 col--;
2954 length++;
2955 }
2956
2957 /*
2958 * Delete the old number.
2959 */
2960 curwin->w_cursor.col = col;
2961 if (!did_change)
2962 startpos = curwin->w_cursor;
2963 did_change = TRUE;
2964 todel = length;
2965 c = gchar_cursor();
2966 /*
2967 * Don't include the '-' in the length, only the length of the
2968 * part after it is kept the same.
2969 */
2970 if (c == '-')
2971 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002972
2973 save_pos = curwin->w_cursor;
2974 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002975 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002976 if (c < 0x100 && SAFE_isalpha(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002977 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002978 if (SAFE_isupper(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002979 hexupper = TRUE;
2980 else
2981 hexupper = FALSE;
2982 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002983 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002984 c = gchar_cursor();
2985 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002986 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002987
2988 /*
2989 * Prepare the leading characters in buf1[].
2990 * When there are many leading zeros it could be very long.
2991 * Allocate a bit too much.
2992 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002993 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002994 if (buf1 == NULL)
2995 goto theend;
2996 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002997 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002998 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002999 if (pre)
3000 {
3001 *ptr++ = '0';
3002 --length;
3003 }
3004 if (pre == 'b' || pre == 'B' ||
3005 pre == 'x' || pre == 'X')
3006 {
3007 *ptr++ = pre;
3008 --length;
3009 }
3010
3011 /*
3012 * Put the number characters in buf2[].
3013 */
3014 if (pre == 'b' || pre == 'B')
3015 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01003016 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003017 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003018
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003019 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003020 for (bit = bits; bit > 0; bit--)
3021 if ((n >> (bit - 1)) & 0x1) break;
3022
Christian Brabandt889f6af2023-09-02 19:43:33 +02003023 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003024 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3025
3026 buf2[i] = '\0';
3027 }
3028 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003029 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003030 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003031 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003032 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003033 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003034 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003035 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003036 length -= (int)STRLEN(buf2);
3037
3038 /*
3039 * Adjust number of zeros to the new number of digits, so the
3040 * total length of the number remains the same.
3041 * Don't do this when
3042 * the result may look like an octal number.
3043 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003044 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003045 while (length-- > 0)
3046 *ptr++ = '0';
3047 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003048
Bram Moolenaard79e5502016-01-10 22:13:02 +01003049 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003050
3051 // Insert just after the first character to be removed, so that any
3052 // text properties will be adjusted. Then delete the old number
3053 // afterwards.
3054 save_pos = curwin->w_cursor;
3055 if (todel > 0)
3056 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003058 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003059
3060 // del_char() will also mark line needing displaying
3061 if (todel > 0)
3062 {
zeertzjq94b7c322024-03-12 21:50:32 +01003063 int bytes_after = ml_get_curline_len() - curwin->w_cursor.col;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003064
3065 // Delete the one character before the insert.
3066 curwin->w_cursor = save_pos;
3067 (void)del_char(FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01003068 curwin->w_cursor.col = ml_get_curline_len() - bytes_after;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003069 --todel;
3070 }
3071 while (todel-- > 0)
3072 (void)del_char(FALSE);
3073
Bram Moolenaard79e5502016-01-10 22:13:02 +01003074 endpos = curwin->w_cursor;
3075 if (did_change && curwin->w_cursor.col)
3076 --curwin->w_cursor.col;
3077 }
3078
Bram Moolenaare1004402020-10-24 20:49:43 +02003079 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003080 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003082 curbuf->b_op_start = startpos;
3083 curbuf->b_op_end = endpos;
3084 if (curbuf->b_op_end.col > 0)
3085 --curbuf->b_op_end.col;
3086 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003087
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003088theend:
3089 if (visual)
3090 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003091 else if (did_change)
3092 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003093 else if (virtual_active())
3094 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003095
Bram Moolenaard79e5502016-01-10 22:13:02 +01003096 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097}
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003100clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003102 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003106 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 *
3108 * "Words" are counted by looking for boundaries between non-space and
3109 * space characters. (it seems to produce results that match 'wc'.)
3110 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003111 * Return value is byte count; word count for the line is added to "*wc".
3112 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 *
3114 * The function will only examine the first "limit" characters in the
3115 * line, stopping if it encounters an end-of-line (NUL byte). In that
3116 * case, eol_size will be added to the character count to account for
3117 * the size of the EOL character.
3118 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003119 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003120line_count_info(
3121 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003122 varnumber_T *wc,
3123 varnumber_T *cc,
3124 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003125 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003127 varnumber_T i;
3128 varnumber_T words = 0;
3129 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 int is_word = 0;
3131
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003132 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134 if (is_word)
3135 {
3136 if (vim_isspace(line[i]))
3137 {
3138 words++;
3139 is_word = 0;
3140 }
3141 }
3142 else if (!vim_isspace(line[i]))
3143 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003144 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003145 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
3147
3148 if (is_word)
3149 words++;
3150 *wc += words;
3151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003153 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003156 chars += eol_size;
3157 }
3158 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return i;
3160}
3161
3162/*
3163 * Give some info about the position of the cursor (for "g CTRL-G").
3164 * In Visual mode, give some info about the selected region. (In this case,
3165 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003166 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 */
3168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003169cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
3171 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003172 char_u buf1[50];
3173 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003175 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003176 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003177 varnumber_T byte_count_cursor = 0;
3178 varnumber_T char_count = 0;
3179 varnumber_T char_count_cursor = 0;
3180 varnumber_T word_count = 0;
3181 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003182 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003183 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 long line_count_selected = 0;
3185 pos_T min_pos, max_pos;
3186 oparg_T oparg;
3187 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /*
3190 * Compute the length of the file in characters.
3191 */
3192 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3193 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003194 if (dict == NULL)
3195 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003196 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003197 return;
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200 else
3201 {
3202 if (get_fileformat(curbuf) == EOL_DOS)
3203 eol_size = 2;
3204 else
3205 eol_size = 1;
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (VIsual_active)
3208 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003209 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
3211 min_pos = VIsual;
3212 max_pos = curwin->w_cursor;
3213 }
3214 else
3215 {
3216 min_pos = curwin->w_cursor;
3217 max_pos = VIsual;
3218 }
3219 if (*p_sel == 'e' && max_pos.col > 0)
3220 --max_pos.col;
3221
3222 if (VIsual_mode == Ctrl_V)
3223 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003224#ifdef FEAT_LINEBREAK
3225 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003226 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003227
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003228 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003229 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003230 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 oparg.is_VIsual = 1;
3233 oparg.block_mode = TRUE;
3234 oparg.op_type = OP_NOP;
3235 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003236 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003237#ifdef FEAT_LINEBREAK
3238 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003239 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003240#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003241 if (curwin->w_curswant == MAXCOL)
3242 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003243 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (oparg.end_vcol < oparg.start_vcol)
3245 {
3246 oparg.end_vcol += oparg.start_vcol;
3247 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3248 oparg.end_vcol -= oparg.start_vcol;
3249 }
3250 }
3251 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
3254 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003256 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003257 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 ui_breakcheck();
3260 if (got_int)
3261 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003262 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003265 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (VIsual_active
3267 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3268 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003269 char_u *s = NULL;
3270 long len = 0L;
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 switch (VIsual_mode)
3273 {
3274 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003278 s = bd.textstart;
3279 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 break;
3281 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003282 s = ml_get(lnum);
3283 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 break;
3285 case 'v':
3286 {
3287 colnr_T start_col = (lnum == min_pos.lnum)
3288 ? min_pos.col : 0;
3289 colnr_T end_col = (lnum == max_pos.lnum)
3290 ? max_pos.col - start_col + 1 : MAXCOL;
3291
Bram Moolenaardef9e822004-12-31 20:58:58 +00003292 s = ml_get(lnum) + start_col;
3293 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295 break;
3296 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003297 if (s != NULL)
3298 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003299 byte_count_cursor += line_count_info(s, &word_count_cursor,
3300 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003301 if (lnum == curbuf->b_ml.ml_line_count
3302 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003303 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003304 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003305 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003310 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (lnum == curwin->w_cursor.lnum)
3312 {
3313 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003314 char_count_cursor += char_count;
3315 byte_count_cursor = byte_count +
3316 line_count_info(ml_get(lnum),
3317 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003318 (varnumber_T)(curwin->w_cursor.col + 1),
3319 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003322 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003323 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003324 &char_count, (varnumber_T)MAXCOL,
3325 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003328 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003329 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003330 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaared767a22016-01-03 22:49:16 +01003332 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003334 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003336 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3337 {
3338 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3339 &max_pos.col);
3340 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3341 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3342 }
3343 else
3344 buf1[0] = NUL;
3345
3346 if (char_count_cursor == byte_count_cursor
3347 && char_count == byte_count)
3348 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003349 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003350 buf1, line_count_selected,
3351 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003352 word_count_cursor,
3353 word_count,
3354 byte_count_cursor,
3355 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003356 else
3357 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003358 _("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 +01003359 buf1, line_count_selected,
3360 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003361 word_count_cursor,
3362 word_count,
3363 char_count_cursor,
3364 char_count,
3365 byte_count_cursor,
3366 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003369 {
3370 p = ml_get_curline();
3371 validate_virtcol();
3372 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3373 (int)curwin->w_virtcol + 1);
zeertzjq94b7c322024-03-12 21:50:32 +01003374 col_print(buf2, sizeof(buf2), ml_get_curline_len(),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003375 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaared767a22016-01-03 22:49:16 +01003377 if (char_count_cursor == byte_count_cursor
3378 && char_count == byte_count)
3379 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003380 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003381 (char *)buf1, (char *)buf2,
3382 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003384 word_count_cursor, word_count,
3385 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003386 else
3387 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003388 _("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 +01003389 (char *)buf1, (char *)buf2,
3390 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003391 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003392 word_count_cursor, word_count,
3393 char_count_cursor, char_count,
3394 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003395 }
3396 }
3397
Bram Moolenaared767a22016-01-03 22:49:16 +01003398 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003399 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003400 {
3401 size_t len = STRLEN(IObuff);
3402
3403 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003404 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003405 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003406 if (dict == NULL)
3407 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003408 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003409 p = p_shm;
3410 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003411 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003412 p_shm = p;
3413 }
3414 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003415#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003416 if (dict != NULL)
3417 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003418 dict_add_number(dict, "words", word_count);
3419 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003420 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003421 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3422 byte_count_cursor);
3423 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3424 char_count_cursor);
3425 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3426 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003430
3431/*
3432 * Handle indent and format operators and visual mode ":".
3433 */
3434 static void
3435op_colon(oparg_T *oap)
3436{
3437 stuffcharReadbuff(':');
3438 if (oap->is_VIsual)
3439 stuffReadbuff((char_u *)"'<,'>");
3440 else
3441 {
3442 // Make the range look nice, so it can be repeated.
3443 if (oap->start.lnum == curwin->w_cursor.lnum)
3444 stuffcharReadbuff('.');
3445 else
3446 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003447
3448#ifdef FEAT_FOLDING
3449 // When using !! on a closed fold the range ".!" works best to operate
3450 // on, it will be made the whole closed fold later.
3451 linenr_T endOfStartFold = oap->start.lnum;
3452 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3453#endif
3454 if (oap->end.lnum != oap->start.lnum
3455#ifdef FEAT_FOLDING
3456 && oap->end.lnum != endOfStartFold
3457#endif
3458 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003459 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003460 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003461 stuffcharReadbuff(',');
3462 if (oap->end.lnum == curwin->w_cursor.lnum)
3463 stuffcharReadbuff('.');
3464 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3465 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003466 else if (oap->start.lnum == curwin->w_cursor.lnum
3467#ifdef FEAT_FOLDING
3468 // do not use ".+number" for a closed fold, it would count
3469 // folded lines twice
3470 && !hasFolding(oap->end.lnum, NULL, NULL)
3471#endif
3472 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003473 {
3474 stuffReadbuff((char_u *)".+");
3475 stuffnumReadbuff((long)oap->line_count - 1);
3476 }
3477 else
3478 stuffnumReadbuff((long)oap->end.lnum);
3479 }
3480 }
3481 if (oap->op_type != OP_COLON)
3482 stuffReadbuff((char_u *)"!");
3483 if (oap->op_type == OP_INDENT)
3484 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003485 if (*get_equalprg() == NUL)
3486 stuffReadbuff((char_u *)"indent");
3487 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003488 stuffReadbuff(get_equalprg());
3489 stuffReadbuff((char_u *)"\n");
3490 }
3491 else if (oap->op_type == OP_FORMAT)
3492 {
3493 if (*curbuf->b_p_fp != NUL)
3494 stuffReadbuff(curbuf->b_p_fp);
3495 else if (*p_fp != NUL)
3496 stuffReadbuff(p_fp);
3497 else
3498 stuffReadbuff((char_u *)"fmt");
3499 stuffReadbuff((char_u *)"\n']");
3500 }
3501
3502 // do_cmdline() does the rest
3503}
3504
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003505// callback function for 'operatorfunc'
3506static callback_T opfunc_cb;
3507
3508/*
3509 * Process the 'operatorfunc' option value.
3510 * Returns OK or FAIL.
3511 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003512 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003513did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003514{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003515 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3516 return e_invalid_argument;
3517
3518 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003519}
3520
3521#if defined(EXITFREE) || defined(PROTO)
3522 void
3523free_operatorfunc_option(void)
3524{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003525# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003526 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003527# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003528}
3529#endif
3530
Dominique Pelle748b3082022-01-08 12:41:16 +00003531#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003532/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003533 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003534 * garbage collected.
3535 */
3536 int
3537set_ref_in_opfunc(int copyID UNUSED)
3538{
3539 int abort = FALSE;
3540
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003541 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003542
3543 return abort;
3544}
Dominique Pelle748b3082022-01-08 12:41:16 +00003545#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003546
3547/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003548 * Handle the "g@" operator: call 'operatorfunc'.
3549 */
3550 static void
3551op_function(oparg_T *oap UNUSED)
3552{
3553#ifdef FEAT_EVAL
3554 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003555 pos_T orig_start = curbuf->b_op_start;
3556 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003557 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003558
3559 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003560 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003561 else
3562 {
3563 // Set '[ and '] marks to text to be operated on.
3564 curbuf->b_op_start = oap->start;
3565 curbuf->b_op_end = oap->end;
3566 if (oap->motion_type != MLINE && !oap->inclusive)
3567 // Exclude the end position.
3568 decl(&curbuf->b_op_end);
3569
3570 argv[0].v_type = VAR_STRING;
3571 if (oap->block_mode)
3572 argv[0].vval.v_string = (char_u *)"block";
3573 else if (oap->motion_type == MLINE)
3574 argv[0].vval.v_string = (char_u *)"line";
3575 else
3576 argv[0].vval.v_string = (char_u *)"char";
3577 argv[1].v_type = VAR_UNKNOWN;
3578
3579 // Reset virtual_op so that 'virtualedit' can be changed in the
3580 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003581 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003582 virtual_op = MAYBE;
3583
naohiro ono75c30e92021-10-19 11:15:41 +01003584 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003585 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003586 finish_op = FALSE;
3587
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003588 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3589 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003590
3591 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003592 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003593 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003594 {
3595 curbuf->b_op_start = orig_start;
3596 curbuf->b_op_end = orig_end;
3597 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003598 }
3599#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003600 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003601#endif
3602}
3603
3604/*
3605 * Calculate start/end virtual columns for operating in block mode.
3606 */
3607 static void
3608get_op_vcol(
3609 oparg_T *oap,
3610 colnr_T redo_VIsual_vcol,
3611 int initial) // when TRUE adjust position for 'selectmode'
3612{
3613 colnr_T start, end;
3614
3615 if (VIsual_mode != Ctrl_V
3616 || (!initial && oap->end.col < curwin->w_width))
3617 return;
3618
3619 oap->block_mode = TRUE;
3620
3621 // prevent from moving onto a trail byte
3622 if (has_mbyte)
3623 mb_adjustpos(curwin->w_buffer, &oap->end);
3624
3625 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3626
3627 if (!redo_VIsual_busy)
3628 {
3629 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3630
3631 if (start < oap->start_vcol)
3632 oap->start_vcol = start;
3633 if (end > oap->end_vcol)
3634 {
3635 if (initial && *p_sel == 'e' && start >= 1
3636 && start - 1 >= oap->end_vcol)
3637 oap->end_vcol = start - 1;
3638 else
3639 oap->end_vcol = end;
3640 }
3641 }
3642
3643 // if '$' was used, get oap->end_vcol from longest line
3644 if (curwin->w_curswant == MAXCOL)
3645 {
3646 curwin->w_cursor.col = MAXCOL;
3647 oap->end_vcol = 0;
3648 for (curwin->w_cursor.lnum = oap->start.lnum;
3649 curwin->w_cursor.lnum <= oap->end.lnum;
3650 ++curwin->w_cursor.lnum)
3651 {
3652 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3653 if (end > oap->end_vcol)
3654 oap->end_vcol = end;
3655 }
3656 }
3657 else if (redo_VIsual_busy)
3658 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3659 // Correct oap->end.col and oap->start.col to be the
3660 // upper-left and lower-right corner of the block area.
3661 //
3662 // (Actually, this does convert column positions into character
3663 // positions)
3664 curwin->w_cursor.lnum = oap->end.lnum;
3665 coladvance(oap->end_vcol);
3666 oap->end = curwin->w_cursor;
3667
3668 curwin->w_cursor = oap->start;
3669 coladvance(oap->start_vcol);
3670 oap->start = curwin->w_cursor;
3671}
3672
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003673// Information for redoing the previous Visual selection.
3674typedef struct {
3675 int rv_mode; // 'v', 'V', or Ctrl-V
3676 linenr_T rv_line_count; // number of lines
3677 colnr_T rv_vcol; // number of cols or end column
3678 long rv_count; // count for Visual operator
3679 int rv_arg; // extra argument
3680} redo_VIsual_T;
3681
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003682 static int
3683is_ex_cmdchar(cmdarg_T *cap)
3684{
3685 return cap->cmdchar == ':'
3686 || cap->cmdchar == K_COMMAND
3687 || cap->cmdchar == K_SCRIPT_COMMAND;
3688}
3689
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003690/*
3691 * Handle an operator after Visual mode or when the movement is finished.
3692 * "gui_yank" is true when yanking text for the clipboard.
3693 */
3694 void
3695do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3696{
3697 oparg_T *oap = cap->oap;
3698 pos_T old_cursor;
3699 int empty_region_error;
3700 int restart_edit_save;
3701#ifdef FEAT_LINEBREAK
3702 int lbr_saved = curwin->w_p_lbr;
3703#endif
3704
3705 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003706 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3707
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003708 int include_line_break = FALSE;
3709
3710#if defined(FEAT_CLIPBOARD)
3711 // Yank the visual area into the GUI selection register before we operate
3712 // on it and lose it forever.
3713 // Don't do it if a specific register was specified, so that ""x"*P works.
3714 // This could call do_pending_operator() recursively, but that's OK
3715 // because gui_yank will be TRUE for the nested call.
3716 if ((clip_star.available || clip_plus.available)
3717 && oap->op_type != OP_NOP
3718 && !gui_yank
3719 && VIsual_active
3720 && !redo_VIsual_busy
3721 && oap->regname == 0)
3722 clip_auto_select();
3723#endif
3724 old_cursor = curwin->w_cursor;
3725
3726 // If an operation is pending, handle it...
3727 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3728 {
3729 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3730 // for the clipboard.
3731 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3732
3733#ifdef FEAT_LINEBREAK
3734 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003735 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003736#endif
3737 oap->is_VIsual = VIsual_active;
3738 if (oap->motion_force == 'V')
3739 oap->motion_type = MLINE;
3740 else if (oap->motion_force == 'v')
3741 {
3742 // If the motion was linewise, "inclusive" will not have been set.
3743 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3744 if (oap->motion_type == MLINE)
3745 oap->inclusive = FALSE;
3746 // If the motion already was characterwise, toggle "inclusive"
3747 else if (oap->motion_type == MCHAR)
3748 oap->inclusive = !oap->inclusive;
3749 oap->motion_type = MCHAR;
3750 }
3751 else if (oap->motion_force == Ctrl_V)
3752 {
3753 // Change line- or characterwise motion into Visual block mode.
3754 if (!VIsual_active)
3755 {
3756 VIsual_active = TRUE;
3757 VIsual = oap->start;
3758 }
3759 VIsual_mode = Ctrl_V;
3760 VIsual_select = FALSE;
3761 VIsual_reselect = FALSE;
3762 }
3763
3764 // Only redo yank when 'y' flag is in 'cpoptions'.
3765 // Never redo "zf" (define fold).
3766 if ((redo_yank || oap->op_type != OP_YANK)
3767 && ((!VIsual_active || oap->motion_force)
3768 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003769 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003770 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003771 && cap->cmdchar != 'D'
3772#ifdef FEAT_FOLDING
3773 && oap->op_type != OP_FOLD
3774 && oap->op_type != OP_FOLDOPEN
3775 && oap->op_type != OP_FOLDOPENREC
3776 && oap->op_type != OP_FOLDCLOSE
3777 && oap->op_type != OP_FOLDCLOSEREC
3778 && oap->op_type != OP_FOLDDEL
3779 && oap->op_type != OP_FOLDDELREC
3780#endif
3781 )
3782 {
3783 prep_redo(oap->regname, cap->count0,
3784 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3785 oap->motion_force, cap->cmdchar, cap->nchar);
3786 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3787 {
3788 // If 'cpoptions' does not contain 'r', insert the search
3789 // pattern to really repeat the same command.
3790 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3791 AppendToRedobuffLit(cap->searchbuf, -1);
3792 AppendToRedobuff(NL_STR);
3793 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003794 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003795 {
3796 // do_cmdline() has stored the first typed line in
3797 // "repeat_cmdline". When several lines are typed repeating
3798 // won't be possible.
3799 if (repeat_cmdline == NULL)
3800 ResetRedobuff();
3801 else
3802 {
zeertzjq30b6d612023-05-07 17:39:23 +01003803 if (cap->cmdchar == ':')
3804 AppendToRedobuffLit(repeat_cmdline, -1);
3805 else
3806 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003807 AppendToRedobuff(NL_STR);
3808 VIM_CLEAR(repeat_cmdline);
3809 }
3810 }
3811 }
3812
3813 if (redo_VIsual_busy)
3814 {
3815 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003816 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003817 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003818 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003819 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3820 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003821 VIsual_mode = redo_VIsual.rv_mode;
3822 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003823 {
3824 if (VIsual_mode == 'v')
3825 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003826 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003827 {
3828 validate_virtcol();
3829 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003830 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003831 }
3832 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003833 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003834 }
3835 else
3836 {
3837 curwin->w_curswant = MAXCOL;
3838 }
3839 coladvance(curwin->w_curswant);
3840 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003841 cap->count0 = redo_VIsual.rv_count;
3842 if (redo_VIsual.rv_count != 0)
3843 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003844 else
3845 cap->count1 = 1;
3846 }
3847 else if (VIsual_active)
3848 {
3849 if (!gui_yank)
3850 {
3851 // Save the current VIsual area for '< and '> marks, and "gv"
3852 curbuf->b_visual.vi_start = VIsual;
3853 curbuf->b_visual.vi_end = curwin->w_cursor;
3854 curbuf->b_visual.vi_mode = VIsual_mode;
3855 restore_visual_mode();
3856 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003857#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003858 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003859#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003860 }
3861
3862 // In Select mode, a linewise selection is operated upon like a
3863 // characterwise selection.
3864 // Special case: gH<Del> deletes the last line.
3865 if (VIsual_select && VIsual_mode == 'V'
3866 && cap->oap->op_type != OP_DELETE)
3867 {
3868 if (LT_POS(VIsual, curwin->w_cursor))
3869 {
3870 VIsual.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003871 curwin->w_cursor.col = ml_get_len(curwin->w_cursor.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003872 }
3873 else
3874 {
3875 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003876 VIsual.col = ml_get_len(VIsual.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003877 }
3878 VIsual_mode = 'v';
3879 }
3880 // If 'selection' is "exclusive", backup one character for
3881 // charwise selections.
3882 else if (VIsual_mode == 'v')
3883 include_line_break = unadjust_for_sel();
3884
3885 oap->start = VIsual;
3886 if (VIsual_mode == 'V')
3887 {
3888 oap->start.col = 0;
3889 oap->start.coladd = 0;
3890 }
3891 }
3892
3893 // Set oap->start to the first position of the operated text, oap->end
3894 // to the end of the operated text. w_cursor is equal to oap->start.
3895 if (LT_POS(oap->start, curwin->w_cursor))
3896 {
3897#ifdef FEAT_FOLDING
3898 // Include folded lines completely.
3899 if (!VIsual_active)
3900 {
3901 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3902 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003903 if ((curwin->w_cursor.col > 0 || oap->inclusive
3904 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003905 && hasFolding(curwin->w_cursor.lnum, NULL,
3906 &curwin->w_cursor.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003907 curwin->w_cursor.col = ml_get_curline_len();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003908 }
3909#endif
3910 oap->end = curwin->w_cursor;
3911 curwin->w_cursor = oap->start;
3912
3913 // w_virtcol may have been updated; if the cursor goes back to its
3914 // previous position w_virtcol becomes invalid and isn't updated
3915 // automatically.
3916 curwin->w_valid &= ~VALID_VIRTCOL;
3917 }
3918 else
3919 {
3920#ifdef FEAT_FOLDING
3921 // Include folded lines completely.
3922 if (!VIsual_active && oap->motion_type == MLINE)
3923 {
3924 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3925 NULL))
3926 curwin->w_cursor.col = 0;
3927 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003928 oap->start.col = ml_get_len(oap->start.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003929 }
3930#endif
3931 oap->end = oap->start;
3932 oap->start = curwin->w_cursor;
3933 }
3934
3935 // Just in case lines were deleted that make the position invalid.
3936 check_pos(curwin->w_buffer, &oap->end);
3937 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3938
3939 // Set "virtual_op" before resetting VIsual_active.
3940 virtual_op = virtual_active();
3941
3942 if (VIsual_active || redo_VIsual_busy)
3943 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003944 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003945
3946 if (!redo_VIsual_busy && !gui_yank)
3947 {
3948 // Prepare to reselect and redo Visual: this is based on the
3949 // size of the Visual text
3950 resel_VIsual_mode = VIsual_mode;
3951 if (curwin->w_curswant == MAXCOL)
3952 resel_VIsual_vcol = MAXCOL;
3953 else
3954 {
3955 if (VIsual_mode != Ctrl_V)
3956 getvvcol(curwin, &(oap->end),
3957 NULL, NULL, &oap->end_vcol);
3958 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3959 {
3960 if (VIsual_mode != Ctrl_V)
3961 getvvcol(curwin, &(oap->start),
3962 &oap->start_vcol, NULL, NULL);
3963 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3964 }
3965 else
3966 resel_VIsual_vcol = oap->end_vcol;
3967 }
3968 resel_VIsual_line_count = oap->line_count;
3969 }
3970
3971 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3972 if ((redo_yank || oap->op_type != OP_YANK)
3973 && oap->op_type != OP_COLON
3974#ifdef FEAT_FOLDING
3975 && oap->op_type != OP_FOLD
3976 && oap->op_type != OP_FOLDOPEN
3977 && oap->op_type != OP_FOLDOPENREC
3978 && oap->op_type != OP_FOLDCLOSE
3979 && oap->op_type != OP_FOLDCLOSEREC
3980 && oap->op_type != OP_FOLDDEL
3981 && oap->op_type != OP_FOLDDELREC
3982#endif
3983 && oap->motion_force == NUL
3984 )
3985 {
3986 // Prepare for redoing. Only use the nchar field for "r",
3987 // otherwise it might be the second char of the operator.
3988 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3989 || cap->nchar == 'N'))
3990 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003991 get_op_char(oap->op_type),
3992 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003993 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003994 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003995 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003996 int opchar = get_op_char(oap->op_type);
3997 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003998 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3999
4000 // reverse what nv_replace() did
4001 if (nchar == REPLACE_CR_NCHAR)
4002 nchar = CAR;
4003 else if (nchar == REPLACE_NL_NCHAR)
4004 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004005
4006 if (opchar == 'g' && extra_opchar == '@')
4007 // also repeat the count for 'operatorfunc'
4008 prep_redo_num2(oap->regname, 0L, NUL, 'v',
4009 cap->count0, opchar, extra_opchar, nchar);
4010 else
4011 prep_redo(oap->regname, 0L, NUL, 'v',
4012 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004013 }
4014 if (!redo_VIsual_busy)
4015 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004016 redo_VIsual.rv_mode = resel_VIsual_mode;
4017 redo_VIsual.rv_vcol = resel_VIsual_vcol;
4018 redo_VIsual.rv_line_count = resel_VIsual_line_count;
4019 redo_VIsual.rv_count = cap->count0;
4020 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004021 }
4022 }
4023
4024 // oap->inclusive defaults to TRUE.
4025 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4026 // FALSE. This makes "d}P" and "v}dP" work the same.
4027 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4028 oap->inclusive = TRUE;
4029 if (VIsual_mode == 'V')
4030 oap->motion_type = MLINE;
4031 else
4032 {
4033 oap->motion_type = MCHAR;
4034 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4035 && (include_line_break || !virtual_op))
4036 {
4037 oap->inclusive = FALSE;
4038 // Try to include the newline, unless it's an operator
4039 // that works on lines only.
4040 if (*p_sel != 'o'
4041 && !op_on_lines(oap->op_type)
4042 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4043 {
4044 ++oap->end.lnum;
4045 oap->end.col = 0;
4046 oap->end.coladd = 0;
4047 ++oap->line_count;
4048 }
4049 }
4050 }
4051
4052 redo_VIsual_busy = FALSE;
4053
4054 // Switch Visual off now, so screen updating does
4055 // not show inverted text when the screen is redrawn.
4056 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4057 // no screen redraw, so it is done here to remove the inverted
4058 // part.
4059 if (!gui_yank)
4060 {
4061 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004062 setmouse();
4063 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004064 may_clear_cmdline();
4065 if ((oap->op_type == OP_YANK
4066 || oap->op_type == OP_COLON
4067 || oap->op_type == OP_FUNCTION
4068 || oap->op_type == OP_FILTER)
4069 && oap->motion_force == NUL)
4070 {
4071#ifdef FEAT_LINEBREAK
4072 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01004073 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004074#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004075 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004076 }
4077 }
4078 }
4079
4080 // Include the trailing byte of a multi-byte char.
4081 if (has_mbyte && oap->inclusive)
4082 {
4083 int l;
4084
4085 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4086 if (l > 1)
4087 oap->end.col += l - 1;
4088 }
4089 curwin->w_set_curswant = TRUE;
4090
4091 // oap->empty is set when start and end are the same. The inclusive
4092 // flag affects this too, unless yanking and the end is on a NUL.
4093 oap->empty = (oap->motion_type == MCHAR
4094 && (!oap->inclusive
4095 || (oap->op_type == OP_YANK
4096 && gchar_pos(&oap->end) == NUL))
4097 && EQUAL_POS(oap->start, oap->end)
4098 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4099 // For delete, change and yank, it's an error to operate on an
4100 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4101 empty_region_error = (oap->empty
4102 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4103
4104 // Force a redraw when operating on an empty Visual region, when
4105 // 'modifiable is off or creating a fold.
4106 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4107#ifdef FEAT_FOLDING
4108 || oap->op_type == OP_FOLD
4109#endif
4110 ))
4111 {
4112#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004113 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004114#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004115 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004116 }
4117
4118 // If the end of an operator is in column one while oap->motion_type
4119 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4120 // character in the previous line. If op_start is on or before the
4121 // first non-blank in the line, the operator becomes linewise
4122 // (strange, but that's the way vi does it).
4123 if ( oap->motion_type == MCHAR
4124 && oap->inclusive == FALSE
4125 && !(cap->retval & CA_NO_ADJ_OP_END)
4126 && oap->end.col == 0
4127 && (!oap->is_VIsual || *p_sel == 'o')
4128 && !oap->block_mode
4129 && oap->line_count > 1)
4130 {
4131 oap->end_adjusted = TRUE; // remember that we did this
4132 --oap->line_count;
4133 --oap->end.lnum;
4134 if (inindent(0))
4135 oap->motion_type = MLINE;
4136 else
4137 {
zeertzjq94b7c322024-03-12 21:50:32 +01004138 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004139 if (oap->end.col)
4140 {
4141 --oap->end.col;
4142 oap->inclusive = TRUE;
4143 }
4144 }
4145 }
4146 else
4147 oap->end_adjusted = FALSE;
4148
4149 switch (oap->op_type)
4150 {
4151 case OP_LSHIFT:
4152 case OP_RSHIFT:
4153 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4154 auto_format(FALSE, TRUE);
4155 break;
4156
4157 case OP_JOIN_NS:
4158 case OP_JOIN:
4159 if (oap->line_count < 2)
4160 oap->line_count = 2;
4161 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4162 curbuf->b_ml.ml_line_count)
4163 beep_flush();
4164 else
4165 {
4166 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4167 TRUE, TRUE, TRUE);
4168 auto_format(FALSE, TRUE);
4169 }
4170 break;
4171
4172 case OP_DELETE:
4173 VIsual_reselect = FALSE; // don't reselect now
4174 if (empty_region_error)
4175 {
4176 vim_beep(BO_OPER);
4177 CancelRedo();
4178 }
4179 else
4180 {
4181 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004182 // save cursor line for undo if it wasn't saved yet
4183 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4184 && u_save_cursor() == OK)
4185 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004186 }
4187 break;
4188
4189 case OP_YANK:
4190 if (empty_region_error)
4191 {
4192 if (!gui_yank)
4193 {
4194 vim_beep(BO_OPER);
4195 CancelRedo();
4196 }
4197 }
4198 else
4199 {
4200#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004201 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004202#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004203 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004204 (void)op_yank(oap, FALSE, !gui_yank);
4205 }
4206 check_cursor_col();
4207 break;
4208
4209 case OP_CHANGE:
4210 VIsual_reselect = FALSE; // don't reselect now
4211 if (empty_region_error)
4212 {
4213 vim_beep(BO_OPER);
4214 CancelRedo();
4215 }
4216 else
4217 {
4218 // This is a new edit command, not a restart. Need to
4219 // remember it to make 'insertmode' work with mappings for
4220 // Visual mode. But do this only once and not when typed and
4221 // 'insertmode' isn't set.
4222 if (p_im || !KeyTyped)
4223 restart_edit_save = restart_edit;
4224 else
4225 restart_edit_save = 0;
4226 restart_edit = 0;
4227#ifdef FEAT_LINEBREAK
4228 // Restore linebreak, so that when the user edits it looks as
4229 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004230 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004231#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004232 // trigger TextChangedI
4233 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4234
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004235 if (op_change(oap)) // will call edit()
4236 cap->retval |= CA_COMMAND_BUSY;
4237 if (restart_edit == 0)
4238 restart_edit = restart_edit_save;
4239 }
4240 break;
4241
4242 case OP_FILTER:
4243 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4244 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4245 else
4246 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4247 // FALLTHROUGH
4248
4249 case OP_INDENT:
4250 case OP_COLON:
4251
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004252 // If 'equalprg' is empty, do the indenting internally.
4253 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4254 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004255 if (curbuf->b_p_lisp)
4256 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004257#ifdef FEAT_EVAL
4258 if (use_indentexpr_for_lisp())
4259 op_reindent(oap, get_expr_indent);
4260 else
4261#endif
4262 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004263 break;
4264 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004265 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004266#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004267 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004268#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004269 get_c_indent);
4270 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004271 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004272
4273 op_colon(oap);
4274 break;
4275
4276 case OP_TILDE:
4277 case OP_UPPER:
4278 case OP_LOWER:
4279 case OP_ROT13:
4280 if (empty_region_error)
4281 {
4282 vim_beep(BO_OPER);
4283 CancelRedo();
4284 }
4285 else
4286 op_tilde(oap);
4287 check_cursor_col();
4288 break;
4289
4290 case OP_FORMAT:
4291#if defined(FEAT_EVAL)
4292 if (*curbuf->b_p_fex != NUL)
4293 op_formatexpr(oap); // use expression
4294 else
4295#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004296 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004297 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004298 op_colon(oap); // use external command
4299 else
4300 op_format(oap, FALSE); // use internal function
4301 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004302 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004303 case OP_FORMAT2:
4304 op_format(oap, TRUE); // use internal function
4305 break;
4306
4307 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004308 {
4309 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4310
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004311#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004312 // Restore linebreak, so that when the user edits it looks as
4313 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004314 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004315#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004316 // call 'operatorfunc'
4317 op_function(oap);
4318
4319 // Restore the info for redoing Visual mode, the function may
4320 // invoke another operator and unintentionally change it.
4321 redo_VIsual = save_redo_VIsual;
4322 break;
4323 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004324
4325 case OP_INSERT:
4326 case OP_APPEND:
4327 VIsual_reselect = FALSE; // don't reselect now
4328 if (empty_region_error)
4329 {
4330 vim_beep(BO_OPER);
4331 CancelRedo();
4332 }
4333 else
4334 {
4335 // This is a new edit command, not a restart. Need to
4336 // remember it to make 'insertmode' work with mappings for
4337 // Visual mode. But do this only once.
4338 restart_edit_save = restart_edit;
4339 restart_edit = 0;
4340#ifdef FEAT_LINEBREAK
4341 // Restore linebreak, so that when the user edits it looks as
4342 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004343 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004344#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004345 // trigger TextChangedI
4346 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4347
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004348 op_insert(oap, cap->count1);
4349#ifdef FEAT_LINEBREAK
4350 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004351 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004352#endif
4353
4354 // TODO: when inserting in several lines, should format all
4355 // the lines.
4356 auto_format(FALSE, TRUE);
4357
4358 if (restart_edit == 0)
4359 restart_edit = restart_edit_save;
4360 else
4361 cap->retval |= CA_COMMAND_BUSY;
4362 }
4363 break;
4364
4365 case OP_REPLACE:
4366 VIsual_reselect = FALSE; // don't reselect now
4367 if (empty_region_error)
4368 {
4369 vim_beep(BO_OPER);
4370 CancelRedo();
4371 }
4372 else
4373 {
4374#ifdef FEAT_LINEBREAK
4375 // Restore linebreak, so that when the user edits it looks as
4376 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004377 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004378#endif
4379 op_replace(oap, cap->nchar);
4380 }
4381 break;
4382
4383#ifdef FEAT_FOLDING
4384 case OP_FOLD:
4385 VIsual_reselect = FALSE; // don't reselect now
4386 foldCreate(oap->start.lnum, oap->end.lnum);
4387 break;
4388
4389 case OP_FOLDOPEN:
4390 case OP_FOLDOPENREC:
4391 case OP_FOLDCLOSE:
4392 case OP_FOLDCLOSEREC:
4393 VIsual_reselect = FALSE; // don't reselect now
4394 opFoldRange(oap->start.lnum, oap->end.lnum,
4395 oap->op_type == OP_FOLDOPEN
4396 || oap->op_type == OP_FOLDOPENREC,
4397 oap->op_type == OP_FOLDOPENREC
4398 || oap->op_type == OP_FOLDCLOSEREC,
4399 oap->is_VIsual);
4400 break;
4401
4402 case OP_FOLDDEL:
4403 case OP_FOLDDELREC:
4404 VIsual_reselect = FALSE; // don't reselect now
4405 deleteFold(oap->start.lnum, oap->end.lnum,
4406 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4407 break;
4408#endif
4409 case OP_NR_ADD:
4410 case OP_NR_SUB:
4411 if (empty_region_error)
4412 {
4413 vim_beep(BO_OPER);
4414 CancelRedo();
4415 }
4416 else
4417 {
4418 VIsual_active = TRUE;
4419#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004420 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004421#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004422 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004423 VIsual_active = FALSE;
4424 }
4425 check_cursor_col();
4426 break;
4427 default:
4428 clearopbeep(oap);
4429 }
4430 virtual_op = MAYBE;
4431 if (!gui_yank)
4432 {
4433 // if 'sol' not set, go back to old column for some commands
4434 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4435 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4436 || oap->op_type == OP_DELETE))
4437 {
4438#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004439 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004440#endif
4441 coladvance(curwin->w_curswant = old_col);
4442 }
4443 }
4444 else
4445 {
4446 curwin->w_cursor = old_cursor;
4447 }
4448 oap->block_mode = FALSE;
4449 clearop(oap);
4450 motion_force = NUL;
4451 }
4452#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004453 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004454#endif
4455}
Christian Brabandtffb13672023-09-15 20:22:02 +02004456
4457// put byte 'c' at position 'lp', but
4458// verify, that the position to place
4459// is actually safe
4460 static void
4461pbyte(pos_T lp, int c)
4462{
4463 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4464 int len = curbuf->b_ml.ml_line_len;
4465
4466 // safety check
4467 if (lp.col >= len)
4468 lp.col = (len > 1 ? len - 2 : 0);
4469 *(p + lp.col) = c;
4470}