blob: 0ab0d005db5627d475c410f440fa9911cddc0124 [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;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200285 int sw_val = (int)get_sw_value_indent(curbuf);
286 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000289 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100290 int added;
291 unsigned new_line_len; // the length of the line after the
292 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293#ifdef FEAT_RIGHTLEFT
294 int old_p_ri = p_ri;
295
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100296 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297#endif
298
Bram Moolenaar24959102022-05-07 20:01:16 +0100299 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
301 if (bd.is_short)
302 return;
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200305 total = (int)((unsigned)amount * (unsigned)sw_val);
306 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100307 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200308
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 oldp = ml_get_curline();
310
311 if (!left)
312 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100313 int tabs = 0, spaces = 0;
314 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100315
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 /*
317 * 1. Get start vcol
318 * 2. Total ws vcols
319 * 3. Divvy into TABs & spp
320 * 4. Construct new string
321 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100322 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 ws_vcol = bd.start_vcol - bd.pre_whitesp;
324 if (bd.startspaces)
325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100327 {
328 if ((*mb_ptr2len)(bd.textstart) == 1)
329 ++bd.textstart;
330 else
331 {
332 ws_vcol = 0;
333 bd.startspaces = 0;
334 }
335 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000336 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000337 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100339
340 // TODO: is passing bd.textstart for start of the line OK?
341 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
342 bd.start_vcol, bd.textstart, bd.textstart);
343 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100345 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100347 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100349 bd.textstart = cts.cts_ptr;
350 bd.start_vcol = cts.cts_vcol;
351 clear_chartabsize_arg(&cts);
352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100353 // OK, now total=all the VWS reqd, and textstart points at the 1st
354 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200355#ifdef FEAT_VARTABS
356 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200357 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100358 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200359 else
LemonBoy4b936742022-05-13 21:56:28 +0100360 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200361#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100363 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
364 if (tabs > 0)
365 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 else
LemonBoy4b936742022-05-13 21:56:28 +0100367 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200368#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100369 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100371
372 new_line_len = bd.textcol + tabs + spaces + (int)STRLEN(bd.textstart);
373 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (newp == NULL)
375 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 mch_memmove(newp, oldp, (size_t)bd.textcol);
LemonBoy4b936742022-05-13 21:56:28 +0100377 vim_memset(newp + bd.textcol, TAB, (size_t)tabs);
378 vim_memset(newp + bd.textcol + tabs, ' ', (size_t)spaces);
379 // Note that STRMOVE() copies the trailing NUL.
380 STRMOVE(newp + bd.textcol + tabs + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100382 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384 colnr_T destination_col; // column to which text in block will
385 // be shifted
386 char_u *verbatim_copy_end; // end of the part of the line which is
387 // copied verbatim
388 colnr_T verbatim_copy_width;// the (displayed) width of this part
389 // of line
390 unsigned fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000391 size_t block_space_width;
392 size_t shift_amount;
393 char_u *non_white = bd.textstart;
394 colnr_T non_white_col;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100395 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000397 /*
398 * Firstly, let's find the first non-whitespace character that is
399 * displayed after the block's start column and the character's column
400 * number. Also, let's calculate the width of all the whitespace
401 * characters that are displayed in the block and precede the searched
402 * non-whitespace character.
403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100405 // If "bd.startspaces" is set, "bd.textstart" points to the character,
406 // the part of which is displayed at the block's beginning. Let's start
407 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000408 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100409 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100411 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000412 non_white_col = bd.start_vcol;
413
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100414 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
415 non_white_col, bd.textstart, non_white);
416 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100418 incr = lbr_chartabsize_adv(&cts);
419 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100421 non_white_col = cts.cts_vcol;
422 non_white = cts.cts_ptr;
423 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000425 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100426 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000427 shift_amount = (block_space_width < (size_t)total
428 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100430 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000431 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100433 // Now let's find out how much of the beginning of the line we can
434 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000435 verbatim_copy_end = bd.textstart;
436 verbatim_copy_width = bd.start_vcol;
437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // If "bd.startspaces" is set, "bd.textstart" points to the character
439 // preceding the block. We have to subtract its width to obtain its
440 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000441 if (bd.startspaces)
442 verbatim_copy_width -= bd.start_char_vcols;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100443 init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
444 bd.textstart, verbatim_copy_end);
445 while (cts.cts_vcol < destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000446 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100447 incr = lbr_chartabsize(&cts);
448 if (cts.cts_vcol + incr > destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000449 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100450 cts.cts_vcol += incr;
451 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000452 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100453 verbatim_copy_width = cts.cts_vcol;
454 verbatim_copy_end = cts.cts_ptr;
455 clear_chartabsize_arg(&cts);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100457 // If "destination_col" is different from the width of the initial
458 // part of the line that will be copied, it means we encountered a tab
459 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000460 fill = destination_col - verbatim_copy_width;
461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100462 // The replacement line will consist of:
463 // - the beginning of the original line up to "verbatim_copy_end",
464 // - "fill" number of spaces,
465 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000466 new_line_len = (unsigned)(verbatim_copy_end - oldp)
467 + fill
LemonBoy4b936742022-05-13 21:56:28 +0100468 + (unsigned)STRLEN(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000469
LemonBoy4b936742022-05-13 21:56:28 +0100470 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (newp == NULL)
472 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000473 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200474 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
LemonBoy4b936742022-05-13 21:56:28 +0100475 // Note that STRMOVE() copies the trailing NUL.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000476 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100478 // replace the line
zeertzjq94b7c322024-03-12 21:50:32 +0100479 added = new_line_len - ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
LemonBoy4b936742022-05-13 21:56:28 +0100481 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 State = oldstate;
483 curwin->w_cursor.col = oldcol;
484#ifdef FEAT_RIGHTLEFT
485 p_ri = old_p_ri;
486#endif
487}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Insert string "s" (b_insert ? before : after) block :AKelly
491 * Caller must prepare for undo.
492 */
493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100494block_insert(
495 oparg_T *oap,
496 char_u *s,
497 int b_insert,
498 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
Bram Moolenaardac13472019-09-16 21:06:21 +0200500 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100501 int count = 0; // extra spaces to replace a cut TAB
502 int spaces = 0; // non-zero if cutting a TAB
503 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200504 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100505 unsigned s_len; // STRLEN(s)
506 char_u *newp, *oldp; // new, old lines
507 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 int oldstate = State;
509
Bram Moolenaar24959102022-05-07 20:01:16 +0100510 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 s_len = (unsigned)STRLEN(s);
512
513 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
514 {
515 block_prep(oap, bdp, lnum, TRUE);
516 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100517 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
519 oldp = ml_get(lnum);
520
521 if (b_insert)
522 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200523 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 spaces = bdp->startspaces;
525 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100526 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 offset = bdp->textcol;
528 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100529 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200531 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200534 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 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 + bdp->textlen - (spaces != 0);
538 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100539 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (!bdp->is_MAX)
543 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
544 count = spaces;
545 offset = bdp->textcol + bdp->textlen;
546 }
547 }
548
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200549 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000550 // avoid copying part of a multi-byte character
551 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100552
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200553 if (spaces < 0) // can happen when the cursor was moved
554 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200555
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000556 // Make sure the allocated size matches what is actually copied below.
zeertzjq94b7c322024-03-12 21:50:32 +0100557 newp = alloc(ml_get_len(lnum) + spaces + s_len
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000558 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
559 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (newp == NULL)
561 continue;
562
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100563 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000564 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 oldp += offset;
566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200568 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200569 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100571 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200572 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 offset += s_len;
574
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000575 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000577 if (*oldp == TAB)
578 {
579 // insert post-padding
580 vim_memset(newp + offset + spaces, ' ',
581 (size_t)(ts_val - spaces));
582 // we're splitting a TAB, don't copy it
583 oldp++;
584 // We allowed for that TAB, remember this now
585 count++;
586 }
587 else
588 // Not a TAB, no extra spaces
589 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 }
591
592 if (spaces > 0)
593 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000594 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 ml_replace(lnum, newp, FALSE);
597
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200598 if (b_insert)
599 // correct any text properties
600 inserted_bytes(lnum, startcol, s_len);
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (lnum == oap->end.lnum)
603 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 // Set "']" mark to the end of the block instead of the end of
605 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 curbuf->b_op_end.lnum = oap->end.lnum;
607 curbuf->b_op_end.col = offset;
608 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
612
613 State = oldstate;
614}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200617 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200619 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 */
621 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100622op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 int n;
625 linenr_T lnum;
626 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 char_u *newp, *oldp;
628 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
630 int did_yank = FALSE;
631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100632 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 return OK;
634
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100635 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (oap->empty)
637 return u_save_cursor();
638
639 if (!curbuf->b_p_ma)
640 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200641 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 return FAIL;
643 }
644
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000645 if (VIsual_select && oap->is_VIsual)
646 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100647 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#ifdef FEAT_CLIPBOARD
650 adjust_clip_reg(&oap->regname);
651#endif
652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (has_mbyte)
654 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaard04b7502010-07-08 22:27:55 +0200656 /*
657 * Imitate the strange Vi behaviour: If the delete spans more than one
658 * line and motion_type == MCHAR and the result is a blank line, make the
659 * delete linewise. Don't do this for the change command or Visual mode.
660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000663 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100665 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 && oap->op_type == OP_DELETE)
667 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200668 ptr = ml_get(oap->end.lnum) + oap->end.col;
669 if (*ptr != NUL)
670 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 ptr = skipwhite(ptr);
672 if (*ptr == NUL && inindent(0))
673 oap->motion_type = MLINE;
674 }
675
Bram Moolenaard04b7502010-07-08 22:27:55 +0200676 /*
677 * Check for trying to delete (e.g. "D") in an empty line.
678 * Note: For the change operator it is ok.
679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if ( oap->motion_type == MCHAR
681 && oap->line_count == 1
682 && oap->op_type == OP_DELETE
683 && *ml_get(oap->start.lnum) == NUL)
684 {
685 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000686 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 * 'cpoptions' (Vi compatible).
688 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000689 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100690 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
691 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000692 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
694 beep_flush();
695 return OK;
696 }
697
Bram Moolenaard04b7502010-07-08 22:27:55 +0200698 /*
699 * Do a yank of whatever we're about to delete.
700 * If a yank register was specified, put the deleted text into that
701 * register. For the black hole register '_' don't yank anything.
702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (oap->regname != '_')
704 {
705 if (oap->regname != 0)
706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100707 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (!valid_yank_reg(oap->regname, TRUE))
709 {
710 beep_flush();
711 return OK;
712 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
714 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 did_yank = TRUE;
716 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200717 else
718 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
720 /*
721 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100722 * delete contains a line break, or when using a specific operator (Vi
723 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100725 if (oap->motion_type == MLINE || oap->line_count > 1
726 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100728 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (op_yank(oap, TRUE, FALSE) == OK)
730 did_yank = TRUE;
731 }
732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100733 // Yank into small delete register when no named register specified
734 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200735 if ((
736#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200737 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
738 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200739#endif
740 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 && oap->line_count == 1)
742 {
743 oap->regname = '-';
744 get_yank_register(oap->regname, TRUE);
745 if (op_yank(oap, TRUE, FALSE) == OK)
746 did_yank = TRUE;
747 oap->regname = 0;
748 }
749
750 /*
751 * If there's too much stuff to fit in the yank register, then get a
752 * confirmation before doing the delete. This is crude, but simple.
753 * And it avoids doing a delete of something we can't put back if we
754 * want.
755 */
756 if (!did_yank)
757 {
758 int msg_silent_save = msg_silent;
759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100760 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
762 msg_silent = msg_silent_save;
763 if (n != 'y')
764 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000765 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 return FAIL;
767 }
768 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100769
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100770#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100771 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200772 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
775
Bram Moolenaard04b7502010-07-08 22:27:55 +0200776 /*
777 * block mode delete
778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 if (oap->block_mode)
780 {
781 if (u_save((linenr_T)(oap->start.lnum - 1),
782 (linenr_T)(oap->end.lnum + 1)) == FAIL)
783 return FAIL;
784
785 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
786 {
787 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100788 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 continue;
790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (lnum == curwin->w_cursor.lnum)
793 {
794 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797
Bram Moolenaar8055d172019-05-17 22:57:26 +0200798 // "n" == number of chars deleted
799 // If we delete a TAB, it may be replaced by several characters.
800 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 n = bd.textlen - bd.startspaces - bd.endspaces;
802 oldp = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100803 newp = alloc(ml_get_len(lnum) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (newp == NULL)
805 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100806 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100808 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200809 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100811 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000813 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100814 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200816
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100817#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200818 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200819 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 }
822
823 check_cursor_col();
824 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
825 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100828 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {
830 if (oap->op_type == OP_CHANGE)
831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100832 // Delete the lines except the first one. Temporarily move the
833 // cursor to the next line. Save the current line number, if the
834 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (oap->line_count > 1)
836 {
837 lnum = curwin->w_cursor.lnum;
838 ++curwin->w_cursor.lnum;
839 del_lines((long)(oap->line_count - 1), TRUE);
840 curwin->w_cursor.lnum = lnum;
841 }
842 if (u_save_cursor() == FAIL)
843 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 beginline(BL_WHITE); // cursor on first non-white
847 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 ai_col = curwin->w_cursor.col;
849 }
850 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100851 beginline(0); // cursor in column 0
zeertzjq8c55d602024-03-13 20:42:26 +0100852 truncate_line(FALSE); // delete the rest of the line,
853 // leaving cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857 else
858 {
859 del_lines(oap->line_count, TRUE);
860 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 }
864 else
865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (virtual_op)
867 {
868 int endcol = 0;
869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100870 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (gchar_pos(&oap->start) == '\t')
872 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100873 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 return FAIL;
875 if (oap->line_count == 1)
876 endcol = getviscol2(oap->end.col, oap->end.coladd);
877 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
878 oap->start = curwin->w_cursor;
879 if (oap->line_count == 1)
880 {
881 coladvance(endcol);
882 oap->end.col = curwin->w_cursor.col;
883 oap->end.coladd = curwin->w_cursor.coladd;
884 curwin->w_cursor = oap->start;
885 }
886 }
887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100888 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (gchar_pos(&oap->end) == '\t'
890 && (int)oap->end.coladd < oap->inclusive)
891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100892 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (u_save((linenr_T)(oap->end.lnum - 1),
894 (linenr_T)(oap->end.lnum + 1)) == FAIL)
895 return FAIL;
896 curwin->w_cursor = oap->end;
897 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
898 oap->end = curwin->w_cursor;
899 curwin->w_cursor = oap->start;
900 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100901 if (has_mbyte)
902 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100905 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100907 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return FAIL;
909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100911 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 && oap->op_type == OP_CHANGE
913 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100914 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 display_dollar(oap->end.col - !oap->inclusive);
916
917 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (virtual_op)
920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100921 // fix up things for virtualedit-delete:
922 // break the tabs which are going to get in our way
zeertzjq94b7c322024-03-12 21:50:32 +0100923 int len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
925 if (oap->end.coladd != 0
926 && (int)oap->end.col >= len - 1
927 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
928 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100929 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (n == 0 && oap->start.coladd != oap->end.coladd)
931 n = 1;
932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100933 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (gchar_cursor() != NUL)
935 curwin->w_cursor.coladd = 0;
936 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200937 (void)del_bytes((long)n, !virtual_op,
938 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100940 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 pos_T curpos;
943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100944 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
946 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
947 return FAIL;
948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100949 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100951 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 ++curwin->w_cursor.lnum;
953 del_lines((long)(oap->line_count - 2), FALSE);
954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100955 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200956 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200957 curwin->w_cursor.col = 0;
958 (void)del_bytes((long)n, !virtual_op,
959 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100960 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200961 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200963 if (oap->op_type == OP_DELETE)
964 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966
967 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
968
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000969setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200970 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100972 if (oap->block_mode)
973 {
974 curbuf->b_op_end.lnum = oap->end.lnum;
975 curbuf->b_op_end.col = oap->start.col;
976 }
977 else
978 curbuf->b_op_end = oap->start;
979 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 return OK;
983}
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Adjust end of operating area for ending on a multi-byte character.
987 * Used for deletion.
988 */
989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100990mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
zeertzjq048761b2024-02-24 14:21:39 +0100992 char_u *line;
993 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000995 if (!oap->inclusive)
996 return;
997
zeertzjq048761b2024-02-24 14:21:39 +0100998 line = ml_get(oap->end.lnum);
999 ptr = line + oap->end.col;
1000 if (*ptr != NUL)
1001 {
1002 ptr -= (*mb_head_off)(line, ptr);
1003 ptr += (*mb_ptr2len)(ptr) - 1;
1004 oap->end.col = ptr - line;
1005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
Bram Moolenaar630afe82018-06-28 19:26:28 +02001008/*
1009 * Replace the character under the cursor with "c".
1010 * This takes care of multi-byte characters.
1011 */
1012 static void
1013replace_character(int c)
1014{
1015 int n = State;
1016
Bram Moolenaar24959102022-05-07 20:01:16 +01001017 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001018 ins_char(c);
1019 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001020 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001021 dec_cursor();
1022}
1023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024/*
1025 * Replace a whole area with one character.
1026 */
1027 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001028op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
1030 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 char_u *newp, *oldp;
1033 size_t oldlen;
1034 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001035 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001036 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001039 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001041 if (c == REPLACE_CR_NCHAR)
1042 {
1043 had_ctrl_v_cr = TRUE;
1044 c = CAR;
1045 }
1046 else if (c == REPLACE_NL_NCHAR)
1047 {
1048 had_ctrl_v_cr = TRUE;
1049 c = NL;
1050 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (has_mbyte)
1053 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
1055 if (u_save((linenr_T)(oap->start.lnum - 1),
1056 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1057 return FAIL;
1058
1059 /*
1060 * block mode replace
1061 */
1062 if (oap->block_mode)
1063 {
1064 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1065 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1069 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001070 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001072 // n == number of extra chars required
1073 // If we split a TAB, it may be replaced by several characters.
1074 // Thus the number of characters may increase!
1075 // If the range starts in virtual space, count the initial
1076 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1078 {
1079 pos_T vpos;
1080
Bram Moolenaara1381de2009-11-03 15:44:21 +00001081 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 getvpos(&vpos, oap->start_vcol);
1083 bd.startspaces += vpos.coladd;
1084 n = bd.startspaces;
1085 }
1086 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001087 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001090 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001094 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 numc = oap->end_vcol - oap->start_vcol + 1;
1096 if (bd.is_short && (!virtual_op || bd.is_MAX))
1097 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001099 // A double-wide character can be replaced only up to half the
1100 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if ((*mb_char2cells)(c) > 1)
1102 {
1103 if ((numc & 1) && !bd.is_short)
1104 {
1105 ++bd.endspaces;
1106 ++n;
1107 }
1108 numc = numc / 2;
1109 }
1110
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001111 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 num_chars = numc;
1113 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 n += numc - bd.textlen;
1116
1117 oldp = ml_get_curline();
zeertzjq94b7c322024-03-12 21:50:32 +01001118 oldlen = ml_get_curline_len();
Bram Moolenaar964b3742019-05-24 18:54:09 +02001119 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (newp == NULL)
1121 continue;
1122 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001123 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 mch_memmove(newp, oldp, (size_t)bd.textcol);
1125 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001126 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001127 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001128 // insert replacement chars CHECK FOR ALLOCATED SPACE
1129 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1130 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001131 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001133 if (has_mbyte)
1134 {
1135 n = (int)STRLEN(newp);
1136 while (--num_chars >= 0)
1137 n += (*mb_char2bytes)(c, newp + n);
1138 }
1139 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001140 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001141 if (!bd.is_short)
1142 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001143 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001144 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001145 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001146 STRMOVE(newp + STRLEN(newp), oldp);
1147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001151 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001152 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001153 if (after_p != NULL)
1154 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001158 if (after_p != NULL)
1159 {
1160 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1161 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1162 oap->end.lnum++;
1163 vim_free(after_p);
1164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 }
1167 else
1168 {
1169 /*
1170 * MCHAR and MLINE motion replace.
1171 */
1172 if (oap->motion_type == MLINE)
1173 {
1174 oap->start.col = 0;
1175 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001176 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (oap->end.col)
1178 --oap->end.col;
1179 }
1180 else if (!oap->inclusive)
1181 dec(&(oap->end));
1182
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001183 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001185 int done = FALSE;
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 n = gchar_cursor();
1188 if (n != NUL)
1189 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001190 int new_byte_len = (*mb_char2len)(c);
1191 int old_byte_len = mb_ptr2len(ml_get_cursor());
1192
1193 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001195 // This is slow, but it handles replacing a single-byte
1196 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001197 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001198 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001199 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001200 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (n == TAB)
1205 {
1206 int end_vcol = 0;
1207
1208 if (curwin->w_cursor.lnum == oap->end.lnum)
1209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 // oap->end has to be recalculated when
1211 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 end_vcol = getviscol2(oap->end.col,
1213 oap->end.coladd);
1214 }
1215 coladvance_force(getviscol());
1216 if (curwin->w_cursor.lnum == oap->end.lnum)
1217 getvpos(&oap->end, end_vcol);
1218 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001219 // with "coladd" set may move to just after a TAB
1220 if (gchar_cursor() != NUL)
1221 {
1222 PBYTE(curwin->w_cursor, c);
1223 done = TRUE;
1224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
1226 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001227 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
1229 int virtcols = oap->end.coladd;
1230
1231 if (curwin->w_cursor.lnum == oap->start.lnum
1232 && oap->start.col == oap->end.col && oap->start.coladd)
1233 virtcols -= oap->start.coladd;
1234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // oap->end has been trimmed so it's effectively inclusive;
1236 // as a result an extra +1 must be counted so we don't
1237 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1239 curwin->w_cursor.col -= (virtcols + 1);
1240 for (; virtcols >= 0; virtcols--)
1241 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001242 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001243 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001244 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001245 PBYTE(curwin->w_cursor, c);
1246 if (inc(&curwin->w_cursor) == -1)
1247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001251 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (inc_cursor() == -1)
1253 break;
1254 }
1255 }
1256
1257 curwin->w_cursor = oap->start;
1258 check_cursor();
1259 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1260
Bram Moolenaare1004402020-10-24 20:49:43 +02001261 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001262 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001263 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001264 curbuf->b_op_start = oap->start;
1265 curbuf->b_op_end = oap->end;
1266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
1268 return OK;
1269}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001271static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273/*
1274 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1275 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001277op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001281 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 if (u_save((linenr_T)(oap->start.lnum - 1),
1284 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1285 return;
1286
1287 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001288 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 {
1290 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1291 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001292 int one_change;
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 block_prep(oap, &bd, pos.lnum, FALSE);
1295 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001296 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1297 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001298
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001299#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001300 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001302 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
Bram Moolenaar009b2592004-10-24 19:18:58 +00001304 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1305 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001306 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001307 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001309 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 if (did_change)
1314 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1315 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001316 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
1318 if (oap->motion_type == MLINE)
1319 {
1320 oap->start.col = 0;
1321 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001322 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (oap->end.col)
1324 --oap->end.col;
1325 }
1326 else if (!oap->inclusive)
1327 dec(&(oap->end));
1328
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001329 if (pos.lnum == oap->end.lnum)
1330 did_change = swapchars(oap->op_type, &pos,
1331 oap->end.col - pos.col + 1);
1332 else
1333 for (;;)
1334 {
1335 did_change |= swapchars(oap->op_type, &pos,
zeertzjq94b7c322024-03-12 21:50:32 +01001336 pos.lnum == oap->end.lnum ? oap->end.col + 1
1337 : ml_get_pos_len(&pos));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001338 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001339 break;
1340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (did_change)
1342 {
1343 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1344 0L);
1345#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001346 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 char_u *ptr;
1349 int count;
1350
1351 pos = oap->start;
1352 while (pos.lnum < oap->end.lnum)
1353 {
1354 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001355 count = ml_get_buf_len(curbuf, pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001356 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001357 // get the line again, it may have been flushed
1358 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001360 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 pos.col = 0;
1362 pos.lnum++;
1363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001365 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001366 // get the line again, it may have been flushed
1367 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001369 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371#endif
1372 }
1373 }
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001376 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001377 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaare1004402020-10-24 20:49:43 +02001379 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001380 {
1381 // Set '[ and '] marks.
1382 curbuf->b_op_start = oap->start;
1383 curbuf->b_op_end = oap->end;
1384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001387 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001388 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389}
1390
1391/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001392 * Invoke swapchar() on "length" bytes at position "pos".
1393 * "pos" is advanced to just after the changed characters.
1394 * "length" is rounded up to include the whole last multi-byte character.
1395 * Also works correctly when the number of bytes changes.
1396 * Returns TRUE if some character was changed.
1397 */
1398 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001399swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001400{
1401 int todo;
1402 int did_change = 0;
1403
1404 for (todo = length; todo > 0; --todo)
1405 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001406 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001407 {
1408 int len = (*mb_ptr2len)(ml_get_pos(pos));
1409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001410 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001411 if (len > 0)
1412 todo -= len - 1;
1413 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001414 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001416 break;
1417 }
1418 return did_change;
1419}
1420
1421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 * If op_type == OP_UPPER: make uppercase,
1423 * if op_type == OP_LOWER: make lowercase,
1424 * if op_type == OP_ROT13: do rot13 encoding,
1425 * else swap case of character at 'pos'
1426 * returns TRUE when something actually changed.
1427 */
1428 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001429swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 int c;
1432 int nc;
1433
1434 c = gchar_pos(pos);
1435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (c >= 0x80 && op_type == OP_ROT13)
1438 return FALSE;
1439
glepnirbd1232a2024-02-12 22:14:53 +01001440 // ~ is OP_NOP, g~ is OP_TILDE, gU is OP_UPPER
1441 if ((op_type == OP_UPPER || op_type == OP_NOP || op_type == OP_TILDE)
1442 && c == 0xdf
1443 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001444 {
1445 pos_T sp = curwin->w_cursor;
1446
glepnirbd1232a2024-02-12 22:14:53 +01001447 // Special handling for lowercase German sharp s (ß): convert to uppercase (ẞ).
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001448 curwin->w_cursor = *pos;
1449 del_char(FALSE);
glepnirbd1232a2024-02-12 22:14:53 +01001450 ins_char(0x1E9E);
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001451 curwin->w_cursor = sp;
glepnirbd1232a2024-02-12 22:14:53 +01001452 return TRUE;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001453 }
1454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001455 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 nc = c;
1458 if (MB_ISLOWER(c))
1459 {
1460 if (op_type == OP_ROT13)
1461 nc = ROT13(c, 'a');
1462 else if (op_type != OP_LOWER)
1463 nc = MB_TOUPPER(c);
1464 }
1465 else if (MB_ISUPPER(c))
1466 {
1467 if (op_type == OP_ROT13)
1468 nc = ROT13(c, 'A');
1469 else if (op_type != OP_UPPER)
1470 nc = MB_TOLOWER(c);
1471 }
1472 if (nc != c)
1473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1475 {
1476 pos_T sp = curwin->w_cursor;
1477
1478 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001479 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001480 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 ins_char(nc);
1482 curwin->w_cursor = sp;
1483 }
1484 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001485 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 return TRUE;
1487 }
1488 return FALSE;
1489}
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491/*
1492 * op_insert - Insert and append operators for Visual mode.
1493 */
1494 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001495op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
zeertzjq8c55d602024-03-13 20:42:26 +01001497 long pre_textlen = 0;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001498 colnr_T ind_pre_col = 0, ind_post_col;
1499 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 struct block_def bd;
1501 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001502 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001503 pos_T start_insert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001505 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001508 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001510 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
1512 if (oap->block_mode)
1513 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001514 // When 'virtualedit' is used, need to insert the extra spaces before
1515 // doing block_prep(). When only "block" is used, virtual edit is
1516 // already disabled, but still need it when calling
1517 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001518 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001519 // state for the current window. To override that state, we need to
1520 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (curwin->w_cursor.coladd > 0)
1522 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001523 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (u_save_cursor() == FAIL)
1526 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001527
Gary Johnson51ad8502021-08-03 18:33:08 +02001528 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 coladvance_force(oap->op_type == OP_APPEND
1530 ? oap->end_vcol + 1 : getviscol());
1531 if (oap->op_type == OP_APPEND)
1532 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001533 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001535 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001537 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001538 ind_pre_col = (colnr_T)getwhitecols_curline();
1539 ind_pre_vcol = get_indent();
zeertzjq94b7c322024-03-12 21:50:32 +01001540 pre_textlen = ml_get_len(oap->start.lnum) - bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (oap->op_type == OP_APPEND)
zeertzjq94b7c322024-03-12 21:50:32 +01001542 pre_textlen -= bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544
1545 if (oap->op_type == OP_APPEND)
1546 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001547 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001549 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 curwin->w_set_curswant = TRUE;
1551 while (*ml_get_cursor() != NUL
1552 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1553 ++curwin->w_cursor.col;
1554 if (bd.is_short && !bd.is_MAX)
1555 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001556 // First line was too short, make it longer and adjust the
1557 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (u_save_cursor() == FAIL)
1559 return;
1560 for (i = 0; i < bd.endspaces; ++i)
1561 ins_char(' ');
1562 bd.textlen += bd.endspaces;
1563 }
1564 }
1565 else
1566 {
1567 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001568 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001570 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001571 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 && oap->start_vcol != oap->end_vcol)
1573 inc_cursor();
1574 }
1575 }
1576
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001577 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001578 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001579 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581 // When a tab was inserted, and the characters in front of the tab
1582 // have been converted to a tab as well, the column of the cursor
1583 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001584 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001585 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001586 oap->start = curbuf->b_op_start_orig;
1587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001588 // If user has moved off this line, we don't know what to do, so do
1589 // nothing.
1590 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001591 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 return;
1593
1594 if (oap->block_mode)
1595 {
Mike Williamsaca8f552024-04-07 18:26:22 +02001596 size_t ins_len;
zeertzjq8c55d602024-03-13 20:42:26 +01001597 char_u *firstline, *ins_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001599 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001600 size_t len;
Mike Williamsaca8f552024-04-07 18:26:22 +02001601 size_t add;
zeertzjq8c55d602024-03-13 20:42:26 +01001602 // offset when cursor was moved in insert mode
1603 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001605 // If indent kicked in, the firstline might have changed
1606 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001607 ind_post_col = (colnr_T)getwhitecols_curline();
1608 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001609 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001610 bd.textcol += ind_post_col - ind_pre_col;
1611 ind_post_vcol = get_indent();
1612 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001613 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001614 }
1615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001616 // The user may have moved the cursor before inserting something, try
1617 // to adjust the block for that. But only do it, if the difference
1618 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001619 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1620 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001621 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001622 int t = getviscol2(curbuf->b_op_start_orig.col,
1623 curbuf->b_op_start_orig.coladd);
1624
zeertzjq7745f142022-02-15 11:48:22 +00001625 if (oap->op_type == OP_INSERT
1626 && oap->start.col + oap->start.coladd
1627 != curbuf->b_op_start_orig.col
1628 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001629 {
zeertzjq7745f142022-02-15 11:48:22 +00001630 oap->start.col = curbuf->b_op_start_orig.col;
1631 pre_textlen -= t - oap->start_vcol;
1632 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001633 }
zeertzjq7745f142022-02-15 11:48:22 +00001634 else if (oap->op_type == OP_APPEND
1635 && oap->start.col + oap->start.coladd
1636 >= curbuf->b_op_start_orig.col
1637 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001638 {
zeertzjq7745f142022-02-15 11:48:22 +00001639 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001640 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001641 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001642 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001643 oap->start_vcol = t;
1644 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001645 }
1646 }
1647
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001648 // Spaces and tabs in the indent may have changed to other spaces and
1649 // tabs. Get the starting column again and correct the length.
1650 // Don't do this when "$" used, end-of-line will have changed.
1651 //
1652 // if indent was added and the inserted text was after the indent,
1653 // correct the selection for the new indent.
1654 if (did_indent && bd.textcol - ind_post_col > 0)
1655 {
1656 oap->start.col += ind_post_col - ind_pre_col;
1657 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1658 oap->end.col += ind_post_col - ind_pre_col;
1659 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001662 if (did_indent && bd.textcol - ind_post_col > 0)
1663 {
1664 // undo for where "oap" is used below
1665 oap->start.col -= ind_post_col - ind_pre_col;
1666 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1667 oap->end.col -= ind_post_col - ind_pre_col;
1668 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1671 {
1672 if (oap->op_type == OP_APPEND)
1673 {
1674 pre_textlen += bd2.textlen - bd.textlen;
1675 if (bd2.endspaces)
1676 --bd2.textlen;
1677 }
1678 bd.textcol = bd2.textcol;
1679 bd.textlen = bd2.textlen;
1680 }
1681
1682 /*
1683 * Subsequent calls to ml_get() flush the firstline data - take a
1684 * copy of the required string.
1685 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001686 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001687 len = ml_get_len(oap->start.lnum);
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001688 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001690 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001691 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001692 // account for pressing cursor in insert mode when '$' was used
1693 if (bd.is_MAX
1694 && (start_insert.lnum == Insstart.lnum
1695 && start_insert.col > Insstart.col))
1696 {
1697 offset = (start_insert.col - Insstart.col);
1698 add -= offset;
1699 if (oap->end_vcol > offset)
1700 oap->end_vcol -= (offset + 1);
1701 else
1702 // moved outside of the visual block, what to do?
1703 return;
1704 }
1705 }
Mike Williamsaca8f552024-04-07 18:26:22 +02001706 if (add > len)
zeertzjq94b7c322024-03-12 21:50:32 +01001707 add = len; // short line, point to the NUL
1708 firstline += add;
1709 len -= add;
1710 if (pre_textlen >= 0 && (ins_len = len - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001712 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (ins_text != NULL)
1714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001715 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (u_save(oap->start.lnum,
1717 (linenr_T)(oap->end.lnum + 1)) == OK)
1718 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1719 &bd);
1720
1721 curwin->w_cursor.col = oap->start.col;
1722 check_cursor();
1723 vim_free(ins_text);
1724 }
1725 }
1726 }
1727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729/*
1730 * op_change - handle a change operation
1731 *
1732 * return TRUE if edit() returns because of a CTRL-O command
1733 */
1734 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001735op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 colnr_T l;
1738 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 long offset;
1740 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001741 long ins_len;
1742 long pre_textlen = 0;
1743 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 char_u *firstline;
1745 char_u *ins_text, *newp, *oldp;
1746 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748 l = oap->start.col;
1749 if (oap->motion_type == MLINE)
1750 {
1751 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001752 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001755 // First delete the text in the region. In an empty buffer only need to
1756 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1758 {
1759 if (u_save_cursor() == FAIL)
1760 return FALSE;
1761 }
1762 else if (op_delete(oap) == FAIL)
1763 return FALSE;
1764
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001765 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 && !virtual_op)
1767 inc_cursor();
1768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001769 // check for still on same line (<CR> in inserted text meaningless)
1770 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (oap->block_mode)
1772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001773 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (virtual_op && (curwin->w_cursor.coladd > 0
1775 || gchar_cursor() == NUL))
1776 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001777 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001778 pre_textlen = ml_get_len(oap->start.lnum);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001779 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 bd.textcol = curwin->w_cursor.col;
1781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (oap->motion_type == MLINE)
1784 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
zeertzjqcdeb6572022-11-15 13:46:12 +00001786 // Reset finish_op now, don't want it set inside edit().
1787 int save_finish_op = finish_op;
1788 finish_op = FALSE;
1789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = edit(NUL, FALSE, (linenr_T)1);
1791
zeertzjqcdeb6572022-11-15 13:46:12 +00001792 finish_op = save_finish_op;
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001795 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001797 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001799 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001801 // Auto-indenting may have changed the indent. If the cursor was past
1802 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001804 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001806 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001807
1808 pre_textlen += new_indent - pre_indent;
1809 bd.textcol += new_indent - pre_indent;
1810 }
1811
zeertzjq94b7c322024-03-12 21:50:32 +01001812 ins_len = ml_get_len(oap->start.lnum) - pre_textlen;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001813 if (ins_len > 0)
1814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001815 // Subsequent calls to ml_get() flush the firstline data - take a
1816 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001817 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001819 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1821 linenr++)
1822 {
1823 block_prep(oap, &bd, linenr, TRUE);
1824 if (!bd.is_short || virtual_op)
1825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 pos_T vpos;
1827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001828 // If the block starts in virtual space, count the
1829 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (bd.is_short)
1831 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001832 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
1835 else
1836 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 oldp = ml_get(linenr);
zeertzjq94b7c322024-03-12 21:50:32 +01001838 newp = alloc(ml_get_len(linenr)
1839 + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (newp == NULL)
1841 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001842 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 mch_memmove(newp, oldp, (size_t)bd.textcol);
1844 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001845 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1848 offset += ins_len;
1849 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001850 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001852#ifdef FEAT_PROP_POPUP
1853 // Shift the properties for linenr as edit() would do.
1854 if (curbuf->b_has_textprop)
1855 adjust_prop_columns(linenr, bd.textcol,
1856 vpos.coladd + ins_len, 0);
1857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 }
1860 check_cursor();
1861
1862 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1863 }
1864 vim_free(ins_text);
1865 }
1866 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001867 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 return retval;
1870}
1871
1872/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001873 * When the cursor is on the NUL past the end of the line and it should not be
1874 * there move it left.
1875 */
1876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001877adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001878{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001879 unsigned int cur_ve_flags = get_ve_flags();
1880
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001881 int adj_cursor = (curwin->w_cursor.col > 0
1882 && gchar_cursor() == NUL
1883 && (cur_ve_flags & VE_ONEMORE) == 0
1884 && !(restart_edit || (State & MODE_INSERT)));
1885 if (!adj_cursor)
1886 return;
1887
1888 // Put the cursor on the last character in the line.
1889 dec_cursor();
1890
1891 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001893 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001894
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001895 // Coladd is set to the width of the last character.
1896 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1897 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899}
1900
Bram Moolenaar81340392012-06-06 16:12:59 +02001901/*
1902 * If "process" is TRUE and the line begins with a comment leader (possibly
1903 * after some white space), return a pointer to the text after it. Put a boolean
1904 * value indicating whether the line ends with an unclosed comment in
1905 * "is_comment".
1906 * line - line to be processed,
1907 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001908 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001909 * include_space - whether to also skip space following the comment leader,
1910 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001911 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001912 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001913 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001914skip_comment(
1915 char_u *line,
1916 int process,
1917 int include_space,
1918 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001919{
1920 char_u *comment_flags = NULL;
1921 int lead_len;
1922 int leader_offset = get_last_leader_offset(line, &comment_flags);
1923
1924 *is_comment = FALSE;
1925 if (leader_offset != -1)
1926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001927 // Let's check whether the line ends with an unclosed comment.
1928 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001929 while (*comment_flags)
1930 {
1931 if (*comment_flags == COM_END
1932 || *comment_flags == ':')
1933 break;
1934 ++comment_flags;
1935 }
1936 if (*comment_flags != COM_END)
1937 *is_comment = TRUE;
1938 }
1939
1940 if (process == FALSE)
1941 return line;
1942
1943 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1944
1945 if (lead_len == 0)
1946 return line;
1947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001948 // Find:
1949 // - COM_END,
1950 // - colon,
1951 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 while (*comment_flags)
1953 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001954 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001955 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001956 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001957 ++comment_flags;
1958 }
1959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001960 // If we found a colon, it means that we are not processing a line
1961 // starting with a closing part of a three-part comment. That's good,
1962 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001963 if (*comment_flags == ':' || *comment_flags == NUL)
1964 line += lead_len;
1965
1966 return line;
1967}
Bram Moolenaar81340392012-06-06 16:12:59 +02001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001970 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001971 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001972 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1973 * leaders should not be removed.
1974 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1975 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001977 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 */
1979 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980do_join(
1981 long count,
1982 int insert_space,
1983 int save_undo,
1984 int use_formatoptions UNUSED,
1985 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001987 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001988 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001989 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001991 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001992 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001993 int endcurr1 = NUL;
1994 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001995 int currsize = 0; // size of the current line
1996 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001998 colnr_T col = 0;
1999 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02002000 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002001 int remove_comments = (use_formatoptions == TRUE)
2002 && has_format_option(FO_REMOVE_COMS);
2003 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002004#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002005 int propcount = 0; // number of props over all joined lines
2006 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002009 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2010 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2011 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002013 // Allocate an array to store the number of spaces inserted before each
2014 // line. We will use it to pre-compute the length of the new line and the
2015 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002016 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002017 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002019 if (remove_comments)
2020 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002021 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002022 if (comments == NULL)
2023 {
2024 vim_free(spaces);
2025 return FAIL;
2026 }
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002030 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002031 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002032 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002034 for (t = 0; t < count; ++t)
2035 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002036 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002037#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002038 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2039 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002040#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002041 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002044 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2045 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2046 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002047 if (remove_comments)
2048 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002049 // We don't want to remove the comment leader if the
2050 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002051 if (t > 0 && prev_was_comment)
2052 {
2053
2054 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2055 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002056 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002057 curr = new_curr;
2058 }
2059 else
2060 curr = skip_comment(curr, FALSE, insert_space,
2061 &prev_was_comment);
2062 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002063
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002064 if (insert_space && t > 0)
2065 {
2066 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002067 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002068 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002069 && (!has_format_option(FO_MBYTE_JOIN)
2070 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2071 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002072 || (mb_ptr2char(curr) < 0x100
2073 && !(enc_utf8 && utf_eat_space(endcurr1)))
2074 || (endcurr1 < 0x100
2075 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002076 )
2077 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002078 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002079 if (endcurr1 == ' ')
2080 endcurr1 = endcurr2;
2081 else
2082 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002083 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002084 if ( p_js
2085 && (endcurr1 == '.'
2086 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2087 && (endcurr1 == '?' || endcurr1 == '!'))))
2088 ++spaces[t];
2089 }
2090 }
2091 currsize = (int)STRLEN(curr);
2092 sumsize += currsize + spaces[t];
2093 endcurr1 = endcurr2 = NUL;
2094 if (insert_space && currsize > 0)
2095 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002096 if (has_mbyte)
2097 {
2098 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002099 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002100 endcurr1 = (*mb_ptr2char)(cend);
2101 if (cend > curr)
2102 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002103 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 endcurr2 = (*mb_ptr2char)(cend);
2105 }
2106 }
2107 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002108 {
2109 endcurr1 = *(curr + currsize - 1);
2110 if (currsize > 1)
2111 endcurr2 = *(curr + currsize - 2);
2112 }
2113 }
2114 line_breakcheck();
2115 if (got_int)
2116 {
2117 ret = FAIL;
2118 goto theend;
2119 }
2120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002122 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002123 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002125 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002126 newp_len = sumsize + 1;
2127#ifdef FEAT_PROP_POPUP
2128 newp_len += propcount * sizeof(textprop_T);
2129#endif
2130 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002131 if (newp == NULL)
2132 {
2133 ret = FAIL;
2134 goto theend;
2135 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002136 cend = newp + sumsize;
2137 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002139 /*
2140 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002141 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002142 *
2143 * Move marks from each deleted line to the joined line, adjusting the
2144 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2145 * should not really be a problem.
2146 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002147#ifdef FEAT_PROP_POPUP
2148 props_remaining = propcount;
2149#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002150 for (t = count - 1; ; --t)
2151 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002152 int spaces_removed;
2153
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002154 cend -= currsize;
2155 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002156
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002157 if (spaces[t] > 0)
2158 {
2159 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002160 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002161 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002162
2163 // If deleting more spaces than adding, the cursor moves no more than
2164 // what is added if it is inside these spaces.
2165 spaces_removed = (curr - curr_start) - spaces[t];
2166
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002167 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002168 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002169#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002170 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2171 curwin->w_cursor.lnum + t, t == count - 1,
2172 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002173#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002174 if (t == 0)
2175 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002176 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002177 if (remove_comments)
2178 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002179 if (insert_space && t > 1)
2180 curr = skipwhite(curr);
2181 currsize = (int)STRLEN(curr);
2182 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002183
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002184 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaare1004402020-10-24 20:49:43 +02002186 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002187 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002188 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002189 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002190 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002191 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // Only report the change in the first line here, del_lines() will report
2194 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 changed_lines(curwin->w_cursor.lnum, currsize,
2196 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002198 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 * briefly, and then move it back. After del_lines() the cursor may
2200 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 */
2202 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002204 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 curwin->w_cursor.lnum = t;
2206
2207 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002208 * Set the cursor column:
2209 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002210 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002212 curwin->w_cursor.col =
2213 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 curwin->w_set_curswant = TRUE;
2218
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002219theend:
2220 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002221 if (remove_comments)
2222 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002223 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224}
2225
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002226#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002228 * Reset 'linebreak' and take care of side effects.
2229 * Returns the previous value, to be passed to restore_lbr().
2230 */
2231 static int
2232reset_lbr(void)
2233{
2234 if (!curwin->w_p_lbr)
2235 return FALSE;
2236 // changing 'linebreak' may require w_virtcol to be updated
2237 curwin->w_p_lbr = FALSE;
2238 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2239 return TRUE;
2240}
2241
2242/*
2243 * Restore 'linebreak' and take care of side effects.
2244 */
2245 static void
2246restore_lbr(int lbr_saved)
2247{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002248 if (curwin->w_p_lbr || !lbr_saved)
2249 return;
2250
2251 // changing 'linebreak' may require w_virtcol to be updated
2252 curwin->w_p_lbr = TRUE;
2253 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002254}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002255#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002256
2257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * prepare a few things for block mode yank/delete/tilde
2259 *
2260 * for delete:
2261 * - textlen includes the first/last char to be (partly) deleted
2262 * - start/endspaces is the number of columns that are taken by the
2263 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002264 * deleted.
2265 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 * - textlen includes the first/last char to be wholly yanked
2267 * - start/endspaces is the number of columns of the first/last yanked char
2268 * that are to be yanked.
2269 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002270 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002271block_prep(
2272 oparg_T *oap,
2273 struct block_def *bdp,
2274 linenr_T lnum,
2275 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 int incr = 0;
2278 char_u *pend;
2279 char_u *pstart;
2280 char_u *line;
2281 char_u *prev_pstart;
2282 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002283 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002284#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002285 // Avoid a problem with unwanted linebreaks in block mode.
2286 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002287#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 bdp->startspaces = 0;
2290 bdp->endspaces = 0;
2291 bdp->textlen = 0;
2292 bdp->start_vcol = 0;
2293 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 bdp->is_short = FALSE;
2295 bdp->is_oneChar = FALSE;
2296 bdp->pre_whitesp = 0;
2297 bdp->pre_whitesp_c = 0;
2298 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 bdp->start_char_vcols = 0;
2300
2301 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002303 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2304 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002307 incr = lbr_chartabsize(&cts);
2308 cts.cts_vcol += incr;
2309 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 bdp->pre_whitesp += incr;
2312 bdp->pre_whitesp_c++;
2313 }
2314 else
2315 {
2316 bdp->pre_whitesp = 0;
2317 bdp->pre_whitesp_c = 0;
2318 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002319 prev_pstart = cts.cts_ptr;
2320 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002322 bdp->start_vcol = cts.cts_vcol;
2323 pstart = cts.cts_ptr;
2324 clear_chartabsize_arg(&cts);
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002327 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 if (!is_del || oap->op_type == OP_APPEND)
2332 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2333 }
2334 else
2335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002336 // notice: this converts partly selected Multibyte characters to
2337 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2339 if (is_del && bdp->startspaces)
2340 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2341 pend = pstart;
2342 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (oap->op_type == OP_INSERT)
2347 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2348 else if (oap->op_type == OP_APPEND)
2349 {
2350 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2351 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2352 }
2353 else
2354 {
2355 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2356 if (is_del && oap->op_type != OP_LSHIFT)
2357 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002358 // just putting the sum of those two into
2359 // bdp->startspaces doesn't work for Visual replace,
2360 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 bdp->startspaces = bdp->start_char_vcols
2362 - (bdp->start_vcol - oap->start_vcol);
2363 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2364 }
2365 }
2366 }
2367 else
2368 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002369 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2370 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002372 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002374 // count a tab for what it's worth (if list mode not on)
2375 prev_pend = cts.cts_ptr;
2376 incr = lbr_chartabsize_adv(&cts);
2377 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002379 bdp->end_vcol = cts.cts_vcol;
2380 pend = cts.cts_ptr;
2381 clear_chartabsize_arg(&cts);
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (bdp->end_vcol <= oap->end_vcol
2384 && (!is_del
2385 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002389 // Alternative: include spaces to fill up the block.
2390 // Disadvantage: can lead to trailing spaces when the line is
2391 // short where the text is put
2392 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (oap->op_type == OP_APPEND || virtual_op)
2394 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002395 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002397 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
2399 else if (bdp->end_vcol > oap->end_vcol)
2400 {
2401 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2402 if (!is_del && bdp->endspaces)
2403 {
2404 bdp->endspaces = incr - bdp->endspaces;
2405 if (pend != pstart)
2406 pend = prev_pend;
2407 }
2408 }
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (is_del && bdp->startspaces)
2412 pstart = prev_pstart;
2413 bdp->textlen = (int)(pend - pstart);
2414 }
2415 bdp->textcol = (colnr_T) (pstart - line);
2416 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002417#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002418 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422/*
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002423 * Get block text from "start" to "end"
2424 */
2425 void
2426charwise_block_prep(
2427 pos_T start,
2428 pos_T end,
2429 struct block_def *bdp,
2430 linenr_T lnum,
2431 int inclusive)
2432{
2433 colnr_T startcol = 0, endcol = MAXCOL;
2434 int is_oneChar = FALSE;
2435 colnr_T cs, ce;
2436 char_u *p;
2437
2438 p = ml_get(lnum);
2439 bdp->startspaces = 0;
2440 bdp->endspaces = 0;
2441
2442 if (lnum == start.lnum)
2443 {
2444 startcol = start.col;
2445 if (virtual_op)
2446 {
2447 getvcol(curwin, &start, &cs, NULL, &ce);
2448 if (ce != cs && start.coladd > 0)
2449 {
2450 // Part of a tab selected -- but don't
2451 // double-count it.
2452 bdp->startspaces = (ce - cs + 1)
2453 - start.coladd;
2454 if (bdp->startspaces < 0)
2455 bdp->startspaces = 0;
2456 startcol++;
2457 }
2458 }
2459 }
2460
2461 if (lnum == end.lnum)
2462 {
2463 endcol = end.col;
2464 if (virtual_op)
2465 {
2466 getvcol(curwin, &end, &cs, NULL, &ce);
2467 if (p[endcol] == NUL || (cs + end.coladd < ce
2468 // Don't add space for double-wide
2469 // char; endcol will be on last byte
2470 // of multi-byte char.
2471 && (*mb_head_off)(p, p + endcol) == 0))
2472 {
2473 if (start.lnum == end.lnum
2474 && start.col == end.col)
2475 {
2476 // Special case: inside a single char
2477 is_oneChar = TRUE;
2478 bdp->startspaces = end.coladd
2479 - start.coladd + inclusive;
2480 endcol = startcol;
2481 }
2482 else
2483 {
2484 bdp->endspaces = end.coladd
2485 + inclusive;
2486 endcol -= inclusive;
2487 }
2488 }
2489 }
2490 }
2491 if (endcol == MAXCOL)
zeertzjq94b7c322024-03-12 21:50:32 +01002492 endcol = ml_get_len(lnum);
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002493 if (startcol > endcol || is_oneChar)
2494 bdp->textlen = 0;
2495 else
2496 bdp->textlen = endcol - startcol + inclusive;
2497 bdp->textstart = p + startcol;
2498}
2499
2500/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002501 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002504op_addsub(
2505 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002506 linenr_T Prenum1, // Amount of add/subtract
2507 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002509 pos_T pos;
2510 struct block_def bd;
2511 int change_cnt = 0;
2512 linenr_T amount = Prenum1;
2513
Bram Moolenaar6b731882018-11-16 20:54:47 +01002514 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002515 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002516 // the call to changed_lines().
2517#ifdef FEAT_FOLDING
2518 disable_fold_update++;
2519#endif
2520
Bram Moolenaard79e5502016-01-10 22:13:02 +01002521 if (!VIsual_active)
2522 {
2523 pos = curwin->w_cursor;
2524 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002525 {
2526#ifdef FEAT_FOLDING
2527 disable_fold_update--;
2528#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002529 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002530 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002531 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002532#ifdef FEAT_FOLDING
2533 disable_fold_update--;
2534#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002535 if (change_cnt)
2536 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2537 }
2538 else
2539 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002540 int one_change;
2541 int length;
2542 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002543
2544 if (u_save((linenr_T)(oap->start.lnum - 1),
2545 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002546 {
2547#ifdef FEAT_FOLDING
2548 disable_fold_update--;
2549#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002550 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002551 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002552
2553 pos = oap->start;
2554 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2555 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002556 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002557 {
2558 block_prep(oap, &bd, pos.lnum, FALSE);
2559 pos.col = bd.textcol;
2560 length = bd.textlen;
2561 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002562 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002563 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002564 curwin->w_cursor.col = 0;
2565 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01002566 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002567 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002568 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002569 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002570 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002571 dec(&(oap->end));
zeertzjq94b7c322024-03-12 21:50:32 +01002572 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002573 pos.col = 0;
2574 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002575 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002576 pos.col += oap->start.col;
2577 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002578 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002579 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002580 {
zeertzjq94b7c322024-03-12 21:50:32 +01002581 length = ml_get_len(oap->end.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002582 if (oap->end.col >= length)
2583 oap->end.col = length - 1;
2584 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002585 }
2586 }
2587 one_change = do_addsub(oap->op_type, &pos, length, amount);
2588 if (one_change)
2589 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002590 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002591 if (change_cnt == 0)
2592 startpos = curbuf->b_op_start;
2593 ++change_cnt;
2594 }
2595
2596#ifdef FEAT_NETBEANS_INTG
2597 if (netbeans_active() && one_change)
2598 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002599 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002600
2601 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002602 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002603 netbeans_inserted(curbuf, pos.lnum, pos.col,
2604 &ptr[pos.col], length);
2605 }
2606#endif
2607 if (g_cmd && one_change)
2608 amount += Prenum1;
2609 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002610
2611#ifdef FEAT_FOLDING
2612 disable_fold_update--;
2613#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002614 if (change_cnt)
2615 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2616
2617 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002618 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002619 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002621 // Set '[ mark if something changed. Keep the last end
2622 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002623 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002624 curbuf->b_op_start = startpos;
2625
2626 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002627 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002628 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002629 }
2630}
2631
2632/*
2633 * Add or subtract 'Prenum1' from a number in a line
2634 * op_type is OP_NR_ADD or OP_NR_SUB
2635 *
2636 * Returns TRUE if some character was changed.
2637 */
2638 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002639do_addsub(
2640 int op_type,
2641 pos_T *pos,
2642 int length,
2643 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002644{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 int col;
2646 char_u *buf1;
2647 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002648 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2649 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002650 uvarnumber_T n;
2651 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 char_u *ptr;
zeertzjq8c55d602024-03-13 20:42:26 +01002653 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002656 int do_hex;
2657 int do_oct;
2658 int do_bin;
2659 int do_alpha;
2660 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002663 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002664 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002665 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002666 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002667 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002668 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002669 pos_T startpos;
2670 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002671 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002673 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2674 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2675 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2676 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2677 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002679 if (virtual_active())
2680 {
2681 save_coladd = pos->coladd;
2682 pos->coladd = 0;
2683 }
2684
Bram Moolenaard79e5502016-01-10 22:13:02 +01002685 curwin->w_cursor = *pos;
2686 ptr = ml_get(pos->lnum);
zeertzjq8c55d602024-03-13 20:42:26 +01002687 linelen = ml_get_len(pos->lnum);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002688 col = pos->col;
2689
zeertzjq8c55d602024-03-13 20:42:26 +01002690 if (col + !!save_coladd >= linelen)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002691 goto theend;
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 /*
2694 * First check if we are on a hexadecimal number, after the "0x".
2695 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002696 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002698 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002699 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002700 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002701 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002702 if (has_mbyte)
2703 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002704 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002705
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002706 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002707 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002708 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002709 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002710 if (has_mbyte)
2711 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002712 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002713
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002714 if ( do_bin
2715 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002716 && ! ((col > 0
2717 && (ptr[col] == 'X'
2718 || ptr[col] == 'x')
2719 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002720 && (!has_mbyte ||
2721 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002722 && vim_isxdigit(ptr[col + 1]))))
2723 {
2724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002726
Bram Moolenaard79e5502016-01-10 22:13:02 +01002727 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002728
2729 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002730 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002731 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002732 if (has_mbyte)
2733 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002734 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002735 }
2736
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002737 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002738 && col > 0
2739 && (ptr[col] == 'X'
2740 || ptr[col] == 'x')
2741 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002742 && (!has_mbyte ||
2743 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002744 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002745 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002746 && col > 0
2747 && (ptr[col] == 'B'
2748 || ptr[col] == 'b')
2749 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002750 && (!has_mbyte ||
2751 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002752 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002753 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002755 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002756 if (has_mbyte)
2757 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002758 }
2759 else
2760 {
2761 /*
2762 * Search forward and then backward to find the start of number.
2763 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002764 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002765
2766 while (ptr[col] != NUL
2767 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002768 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002769 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002770
2771 while (col > 0
2772 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002773 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002774 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002775 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002776 if (has_mbyte)
2777 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002778 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002779 }
2780 }
2781
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002782 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002783 {
2784 while (ptr[col] != NUL && length > 0
2785 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002786 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002787 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002788 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002789
2790 col += mb_len;
2791 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002792 }
2793
2794 if (length == 0)
2795 goto theend;
2796
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002797 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002798 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2799 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002800 {
2801 negative = TRUE;
2802 was_positive = FALSE;
2803 }
2804 }
2805
2806 /*
2807 * If a number was found, and saving for undo works, replace the number.
2808 */
2809 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002810 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002811 {
2812 beep_flush();
2813 goto theend;
2814 }
2815
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002816 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002817 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002818 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002819 if (op_type == OP_NR_SUB)
2820 {
2821 if (CharOrd(firstdigit) < Prenum1)
2822 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002823 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002824 firstdigit = 'A';
2825 else
2826 firstdigit = 'a';
2827 }
2828 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002829 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002830 }
2831 else
2832 {
2833 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2834 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002835 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002836 firstdigit = 'Z';
2837 else
2838 firstdigit = 'z';
2839 }
2840 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002841 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002842 }
2843 curwin->w_cursor.col = col;
2844 if (!did_change)
2845 startpos = curwin->w_cursor;
2846 did_change = TRUE;
2847 (void)del_char(FALSE);
2848 ins_char(firstdigit);
2849 endpos = curwin->w_cursor;
2850 curwin->w_cursor.col = col;
2851 }
2852 else
2853 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002854 pos_T save_pos;
2855 int i;
2856
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002857 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002858 && (!has_mbyte ||
2859 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002860 && !visual
2861 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002864 --col;
2865 negative = TRUE;
2866 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002868 if (visual && VIsual_mode != 'V')
2869 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
zeertzjq8c55d602024-03-13 20:42:26 +01002870 ? linelen - col : length);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002871
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002872 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002873 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002874 0 + (do_bin ? STR2NR_BIN : 0)
2875 + (do_oct ? STR2NR_OCT : 0)
2876 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002877 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002880 if (pre && negative)
2881 {
2882 ++col;
2883 --length;
2884 negative = FALSE;
2885 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002886 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002887 subtract = FALSE;
2888 if (op_type == OP_NR_SUB)
2889 subtract ^= TRUE;
2890 if (negative)
2891 subtract ^= TRUE;
2892
2893 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002894 if (!overflow) // if number is too big don't add/subtract
2895 {
2896 if (subtract)
2897 n -= (uvarnumber_T)Prenum1;
2898 else
2899 n += (uvarnumber_T)Prenum1;
2900 }
2901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002903 if (!pre)
2904 {
2905 if (subtract)
2906 {
2907 if (n > oldn)
2908 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002909 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002910 negative ^= TRUE;
2911 }
2912 }
2913 else
2914 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002916 if (n < oldn)
2917 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002918 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002919 negative ^= TRUE;
2920 }
2921 }
2922 if (n == 0)
2923 negative = FALSE;
2924 }
2925
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002926 if (do_unsigned && negative)
2927 {
2928 if (subtract)
2929 // sticking at zero.
2930 n = (uvarnumber_T)0;
2931 else
2932 // sticking at 2^64 - 1.
2933 n = (uvarnumber_T)(-1);
2934 negative = FALSE;
2935 }
2936
Bram Moolenaard79e5502016-01-10 22:13:02 +01002937 if (visual && !was_positive && !negative && col > 0)
2938 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002940 col--;
2941 length++;
2942 }
2943
2944 /*
2945 * Delete the old number.
2946 */
2947 curwin->w_cursor.col = col;
2948 if (!did_change)
2949 startpos = curwin->w_cursor;
2950 did_change = TRUE;
2951 todel = length;
2952 c = gchar_cursor();
2953 /*
2954 * Don't include the '-' in the length, only the length of the
2955 * part after it is kept the same.
2956 */
2957 if (c == '-')
2958 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002959
2960 save_pos = curwin->w_cursor;
2961 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002962 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002963 if (c < 0x100 && SAFE_isalpha(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002964 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002965 if (SAFE_isupper(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002966 hexupper = TRUE;
2967 else
2968 hexupper = FALSE;
2969 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002970 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002971 c = gchar_cursor();
2972 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002973 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002974
2975 /*
2976 * Prepare the leading characters in buf1[].
2977 * When there are many leading zeros it could be very long.
2978 * Allocate a bit too much.
2979 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002980 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002981 if (buf1 == NULL)
2982 goto theend;
2983 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002984 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002985 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002986 if (pre)
2987 {
2988 *ptr++ = '0';
2989 --length;
2990 }
2991 if (pre == 'b' || pre == 'B' ||
2992 pre == 'x' || pre == 'X')
2993 {
2994 *ptr++ = pre;
2995 --length;
2996 }
2997
2998 /*
2999 * Put the number characters in buf2[].
3000 */
3001 if (pre == 'b' || pre == 'B')
3002 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01003003 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003004 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003007 for (bit = bits; bit > 0; bit--)
3008 if ((n >> (bit - 1)) & 0x1) break;
3009
Christian Brabandt889f6af2023-09-02 19:43:33 +02003010 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003011 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3012
3013 buf2[i] = '\0';
3014 }
3015 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003016 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003017 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003018 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003019 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003020 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003021 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003022 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003023 length -= (int)STRLEN(buf2);
3024
3025 /*
3026 * Adjust number of zeros to the new number of digits, so the
3027 * total length of the number remains the same.
3028 * Don't do this when
3029 * the result may look like an octal number.
3030 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003031 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003032 while (length-- > 0)
3033 *ptr++ = '0';
3034 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003035
Bram Moolenaard79e5502016-01-10 22:13:02 +01003036 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003037
3038 // Insert just after the first character to be removed, so that any
3039 // text properties will be adjusted. Then delete the old number
3040 // afterwards.
3041 save_pos = curwin->w_cursor;
3042 if (todel > 0)
3043 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003045 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003046
3047 // del_char() will also mark line needing displaying
3048 if (todel > 0)
3049 {
zeertzjq94b7c322024-03-12 21:50:32 +01003050 int bytes_after = ml_get_curline_len() - curwin->w_cursor.col;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003051
3052 // Delete the one character before the insert.
3053 curwin->w_cursor = save_pos;
3054 (void)del_char(FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01003055 curwin->w_cursor.col = ml_get_curline_len() - bytes_after;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003056 --todel;
3057 }
3058 while (todel-- > 0)
3059 (void)del_char(FALSE);
3060
Bram Moolenaard79e5502016-01-10 22:13:02 +01003061 endpos = curwin->w_cursor;
3062 if (did_change && curwin->w_cursor.col)
3063 --curwin->w_cursor.col;
3064 }
3065
Bram Moolenaare1004402020-10-24 20:49:43 +02003066 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003069 curbuf->b_op_start = startpos;
3070 curbuf->b_op_end = endpos;
3071 if (curbuf->b_op_end.col > 0)
3072 --curbuf->b_op_end.col;
3073 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003074
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003075theend:
3076 if (visual)
3077 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003078 else if (did_change)
3079 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003080 else if (virtual_active())
3081 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003082
Bram Moolenaard79e5502016-01-10 22:13:02 +01003083 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084}
3085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003087clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003089 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090}
3091
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003093 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 *
3095 * "Words" are counted by looking for boundaries between non-space and
3096 * space characters. (it seems to produce results that match 'wc'.)
3097 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003098 * Return value is byte count; word count for the line is added to "*wc".
3099 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 *
3101 * The function will only examine the first "limit" characters in the
3102 * line, stopping if it encounters an end-of-line (NUL byte). In that
3103 * case, eol_size will be added to the character count to account for
3104 * the size of the EOL character.
3105 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003106 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003107line_count_info(
3108 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003109 varnumber_T *wc,
3110 varnumber_T *cc,
3111 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003112 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003114 varnumber_T i;
3115 varnumber_T words = 0;
3116 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int is_word = 0;
3118
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003119 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
3121 if (is_word)
3122 {
3123 if (vim_isspace(line[i]))
3124 {
3125 words++;
3126 is_word = 0;
3127 }
3128 }
3129 else if (!vim_isspace(line[i]))
3130 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003131 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003132 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
3134
3135 if (is_word)
3136 words++;
3137 *wc += words;
3138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003139 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003140 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003143 chars += eol_size;
3144 }
3145 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 return i;
3147}
3148
3149/*
3150 * Give some info about the position of the cursor (for "g CTRL-G").
3151 * In Visual mode, give some info about the selected region. (In this case,
3152 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003153 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 */
3155 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003156cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003159 char_u buf1[50];
3160 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003162 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003163 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003164 varnumber_T byte_count_cursor = 0;
3165 varnumber_T char_count = 0;
3166 varnumber_T char_count_cursor = 0;
3167 varnumber_T word_count = 0;
3168 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003169 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003170 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 long line_count_selected = 0;
3172 pos_T min_pos, max_pos;
3173 oparg_T oparg;
3174 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /*
3177 * Compute the length of the file in characters.
3178 */
3179 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3180 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003181 if (dict == NULL)
3182 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003183 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003184 return;
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 else
3188 {
3189 if (get_fileformat(curbuf) == EOL_DOS)
3190 eol_size = 2;
3191 else
3192 eol_size = 1;
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (VIsual_active)
3195 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003196 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
3198 min_pos = VIsual;
3199 max_pos = curwin->w_cursor;
3200 }
3201 else
3202 {
3203 min_pos = curwin->w_cursor;
3204 max_pos = VIsual;
3205 }
3206 if (*p_sel == 'e' && max_pos.col > 0)
3207 --max_pos.col;
3208
3209 if (VIsual_mode == Ctrl_V)
3210 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003211#ifdef FEAT_LINEBREAK
3212 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003213 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003216 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003217 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 oparg.is_VIsual = 1;
3220 oparg.block_mode = TRUE;
3221 oparg.op_type = OP_NOP;
3222 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003223 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003224#ifdef FEAT_LINEBREAK
3225 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003226 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003227#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003228 if (curwin->w_curswant == MAXCOL)
3229 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003230 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (oparg.end_vcol < oparg.start_vcol)
3232 {
3233 oparg.end_vcol += oparg.start_vcol;
3234 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3235 oparg.end_vcol -= oparg.start_vcol;
3236 }
3237 }
3238 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
3241 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3242 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003243 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003244 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
3246 ui_breakcheck();
3247 if (got_int)
3248 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003249 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003252 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (VIsual_active
3254 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3255 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003256 char_u *s = NULL;
3257 long len = 0L;
3258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 switch (VIsual_mode)
3260 {
3261 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003265 s = bd.textstart;
3266 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 break;
3268 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003269 s = ml_get(lnum);
3270 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 break;
3272 case 'v':
3273 {
3274 colnr_T start_col = (lnum == min_pos.lnum)
3275 ? min_pos.col : 0;
3276 colnr_T end_col = (lnum == max_pos.lnum)
3277 ? max_pos.col - start_col + 1 : MAXCOL;
3278
Bram Moolenaardef9e822004-12-31 20:58:58 +00003279 s = ml_get(lnum) + start_col;
3280 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 break;
3283 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003284 if (s != NULL)
3285 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003286 byte_count_cursor += line_count_info(s, &word_count_cursor,
3287 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003288 if (lnum == curbuf->b_ml.ml_line_count
3289 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003290 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003291 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003292 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003297 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (lnum == curwin->w_cursor.lnum)
3299 {
3300 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003301 char_count_cursor += char_count;
3302 byte_count_cursor = byte_count +
3303 line_count_info(ml_get(lnum),
3304 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003305 (varnumber_T)(curwin->w_cursor.col + 1),
3306 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003309 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003310 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003311 &char_count, (varnumber_T)MAXCOL,
3312 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003315 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003316 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003317 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
Bram Moolenaared767a22016-01-03 22:49:16 +01003319 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003321 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003323 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3324 {
3325 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3326 &max_pos.col);
3327 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3328 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3329 }
3330 else
3331 buf1[0] = NUL;
3332
3333 if (char_count_cursor == byte_count_cursor
3334 && char_count == byte_count)
3335 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003336 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003337 buf1, line_count_selected,
3338 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003339 word_count_cursor,
3340 word_count,
3341 byte_count_cursor,
3342 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003343 else
3344 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003345 _("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 +01003346 buf1, line_count_selected,
3347 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003348 word_count_cursor,
3349 word_count,
3350 char_count_cursor,
3351 char_count,
3352 byte_count_cursor,
3353 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
3355 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003356 {
3357 p = ml_get_curline();
3358 validate_virtcol();
3359 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3360 (int)curwin->w_virtcol + 1);
zeertzjq94b7c322024-03-12 21:50:32 +01003361 col_print(buf2, sizeof(buf2), ml_get_curline_len(),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003362 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaared767a22016-01-03 22:49:16 +01003364 if (char_count_cursor == byte_count_cursor
3365 && char_count == byte_count)
3366 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003367 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003368 (char *)buf1, (char *)buf2,
3369 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003371 word_count_cursor, word_count,
3372 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003373 else
3374 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003375 _("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 +01003376 (char *)buf1, (char *)buf2,
3377 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003378 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003379 word_count_cursor, word_count,
3380 char_count_cursor, char_count,
3381 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003382 }
3383 }
3384
Bram Moolenaared767a22016-01-03 22:49:16 +01003385 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003386 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003387 {
3388 size_t len = STRLEN(IObuff);
3389
3390 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003391 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003392 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003393 if (dict == NULL)
3394 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003395 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003396 p = p_shm;
3397 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003398 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003399 p_shm = p;
3400 }
3401 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003402#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003403 if (dict != NULL)
3404 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003405 dict_add_number(dict, "words", word_count);
3406 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003407 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003408 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3409 byte_count_cursor);
3410 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3411 char_count_cursor);
3412 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3413 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003417
3418/*
3419 * Handle indent and format operators and visual mode ":".
3420 */
3421 static void
3422op_colon(oparg_T *oap)
3423{
3424 stuffcharReadbuff(':');
3425 if (oap->is_VIsual)
3426 stuffReadbuff((char_u *)"'<,'>");
3427 else
3428 {
3429 // Make the range look nice, so it can be repeated.
3430 if (oap->start.lnum == curwin->w_cursor.lnum)
3431 stuffcharReadbuff('.');
3432 else
3433 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003434
3435#ifdef FEAT_FOLDING
3436 // When using !! on a closed fold the range ".!" works best to operate
3437 // on, it will be made the whole closed fold later.
3438 linenr_T endOfStartFold = oap->start.lnum;
3439 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3440#endif
3441 if (oap->end.lnum != oap->start.lnum
3442#ifdef FEAT_FOLDING
3443 && oap->end.lnum != endOfStartFold
3444#endif
3445 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003446 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003447 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003448 stuffcharReadbuff(',');
3449 if (oap->end.lnum == curwin->w_cursor.lnum)
3450 stuffcharReadbuff('.');
3451 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3452 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003453 else if (oap->start.lnum == curwin->w_cursor.lnum
3454#ifdef FEAT_FOLDING
3455 // do not use ".+number" for a closed fold, it would count
3456 // folded lines twice
3457 && !hasFolding(oap->end.lnum, NULL, NULL)
3458#endif
3459 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003460 {
3461 stuffReadbuff((char_u *)".+");
3462 stuffnumReadbuff((long)oap->line_count - 1);
3463 }
3464 else
3465 stuffnumReadbuff((long)oap->end.lnum);
3466 }
3467 }
3468 if (oap->op_type != OP_COLON)
3469 stuffReadbuff((char_u *)"!");
3470 if (oap->op_type == OP_INDENT)
3471 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003472 if (*get_equalprg() == NUL)
3473 stuffReadbuff((char_u *)"indent");
3474 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003475 stuffReadbuff(get_equalprg());
3476 stuffReadbuff((char_u *)"\n");
3477 }
3478 else if (oap->op_type == OP_FORMAT)
3479 {
3480 if (*curbuf->b_p_fp != NUL)
3481 stuffReadbuff(curbuf->b_p_fp);
3482 else if (*p_fp != NUL)
3483 stuffReadbuff(p_fp);
3484 else
3485 stuffReadbuff((char_u *)"fmt");
3486 stuffReadbuff((char_u *)"\n']");
3487 }
3488
3489 // do_cmdline() does the rest
3490}
3491
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003492// callback function for 'operatorfunc'
3493static callback_T opfunc_cb;
3494
3495/*
3496 * Process the 'operatorfunc' option value.
3497 * Returns OK or FAIL.
3498 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003499 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003500did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003501{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003502 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3503 return e_invalid_argument;
3504
3505 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003506}
3507
3508#if defined(EXITFREE) || defined(PROTO)
3509 void
3510free_operatorfunc_option(void)
3511{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003512# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003513 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003514# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003515}
3516#endif
3517
Dominique Pelle748b3082022-01-08 12:41:16 +00003518#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003519/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003520 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003521 * garbage collected.
3522 */
3523 int
3524set_ref_in_opfunc(int copyID UNUSED)
3525{
3526 int abort = FALSE;
3527
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003528 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003529
3530 return abort;
3531}
Dominique Pelle748b3082022-01-08 12:41:16 +00003532#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003533
3534/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003535 * Handle the "g@" operator: call 'operatorfunc'.
3536 */
3537 static void
3538op_function(oparg_T *oap UNUSED)
3539{
3540#ifdef FEAT_EVAL
3541 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003542 pos_T orig_start = curbuf->b_op_start;
3543 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003544 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003545
3546 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003547 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003548 else
3549 {
3550 // Set '[ and '] marks to text to be operated on.
3551 curbuf->b_op_start = oap->start;
3552 curbuf->b_op_end = oap->end;
3553 if (oap->motion_type != MLINE && !oap->inclusive)
3554 // Exclude the end position.
3555 decl(&curbuf->b_op_end);
3556
3557 argv[0].v_type = VAR_STRING;
3558 if (oap->block_mode)
3559 argv[0].vval.v_string = (char_u *)"block";
3560 else if (oap->motion_type == MLINE)
3561 argv[0].vval.v_string = (char_u *)"line";
3562 else
3563 argv[0].vval.v_string = (char_u *)"char";
3564 argv[1].v_type = VAR_UNKNOWN;
3565
3566 // Reset virtual_op so that 'virtualedit' can be changed in the
3567 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003568 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003569 virtual_op = MAYBE;
3570
naohiro ono75c30e92021-10-19 11:15:41 +01003571 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003572 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003573 finish_op = FALSE;
3574
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003575 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3576 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003577
3578 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003579 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003580 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003581 {
3582 curbuf->b_op_start = orig_start;
3583 curbuf->b_op_end = orig_end;
3584 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003585 }
3586#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003587 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003588#endif
3589}
3590
3591/*
3592 * Calculate start/end virtual columns for operating in block mode.
3593 */
3594 static void
3595get_op_vcol(
3596 oparg_T *oap,
3597 colnr_T redo_VIsual_vcol,
3598 int initial) // when TRUE adjust position for 'selectmode'
3599{
3600 colnr_T start, end;
3601
3602 if (VIsual_mode != Ctrl_V
3603 || (!initial && oap->end.col < curwin->w_width))
3604 return;
3605
3606 oap->block_mode = TRUE;
3607
3608 // prevent from moving onto a trail byte
3609 if (has_mbyte)
3610 mb_adjustpos(curwin->w_buffer, &oap->end);
3611
3612 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3613
3614 if (!redo_VIsual_busy)
3615 {
3616 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3617
3618 if (start < oap->start_vcol)
3619 oap->start_vcol = start;
3620 if (end > oap->end_vcol)
3621 {
3622 if (initial && *p_sel == 'e' && start >= 1
3623 && start - 1 >= oap->end_vcol)
3624 oap->end_vcol = start - 1;
3625 else
3626 oap->end_vcol = end;
3627 }
3628 }
3629
3630 // if '$' was used, get oap->end_vcol from longest line
3631 if (curwin->w_curswant == MAXCOL)
3632 {
3633 curwin->w_cursor.col = MAXCOL;
3634 oap->end_vcol = 0;
3635 for (curwin->w_cursor.lnum = oap->start.lnum;
3636 curwin->w_cursor.lnum <= oap->end.lnum;
3637 ++curwin->w_cursor.lnum)
3638 {
3639 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3640 if (end > oap->end_vcol)
3641 oap->end_vcol = end;
3642 }
3643 }
3644 else if (redo_VIsual_busy)
3645 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3646 // Correct oap->end.col and oap->start.col to be the
3647 // upper-left and lower-right corner of the block area.
3648 //
3649 // (Actually, this does convert column positions into character
3650 // positions)
3651 curwin->w_cursor.lnum = oap->end.lnum;
3652 coladvance(oap->end_vcol);
3653 oap->end = curwin->w_cursor;
3654
3655 curwin->w_cursor = oap->start;
3656 coladvance(oap->start_vcol);
3657 oap->start = curwin->w_cursor;
3658}
3659
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003660// Information for redoing the previous Visual selection.
3661typedef struct {
3662 int rv_mode; // 'v', 'V', or Ctrl-V
3663 linenr_T rv_line_count; // number of lines
3664 colnr_T rv_vcol; // number of cols or end column
3665 long rv_count; // count for Visual operator
3666 int rv_arg; // extra argument
3667} redo_VIsual_T;
3668
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003669 static int
3670is_ex_cmdchar(cmdarg_T *cap)
3671{
3672 return cap->cmdchar == ':'
3673 || cap->cmdchar == K_COMMAND
3674 || cap->cmdchar == K_SCRIPT_COMMAND;
3675}
3676
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003677/*
3678 * Handle an operator after Visual mode or when the movement is finished.
3679 * "gui_yank" is true when yanking text for the clipboard.
3680 */
3681 void
3682do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3683{
3684 oparg_T *oap = cap->oap;
3685 pos_T old_cursor;
3686 int empty_region_error;
3687 int restart_edit_save;
3688#ifdef FEAT_LINEBREAK
3689 int lbr_saved = curwin->w_p_lbr;
3690#endif
3691
3692 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003693 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3694
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003695 int include_line_break = FALSE;
3696
3697#if defined(FEAT_CLIPBOARD)
3698 // Yank the visual area into the GUI selection register before we operate
3699 // on it and lose it forever.
3700 // Don't do it if a specific register was specified, so that ""x"*P works.
3701 // This could call do_pending_operator() recursively, but that's OK
3702 // because gui_yank will be TRUE for the nested call.
3703 if ((clip_star.available || clip_plus.available)
3704 && oap->op_type != OP_NOP
3705 && !gui_yank
3706 && VIsual_active
3707 && !redo_VIsual_busy
3708 && oap->regname == 0)
3709 clip_auto_select();
3710#endif
3711 old_cursor = curwin->w_cursor;
3712
3713 // If an operation is pending, handle it...
3714 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3715 {
3716 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3717 // for the clipboard.
3718 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3719
3720#ifdef FEAT_LINEBREAK
3721 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003722 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003723#endif
3724 oap->is_VIsual = VIsual_active;
3725 if (oap->motion_force == 'V')
3726 oap->motion_type = MLINE;
3727 else if (oap->motion_force == 'v')
3728 {
3729 // If the motion was linewise, "inclusive" will not have been set.
3730 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3731 if (oap->motion_type == MLINE)
3732 oap->inclusive = FALSE;
3733 // If the motion already was characterwise, toggle "inclusive"
3734 else if (oap->motion_type == MCHAR)
3735 oap->inclusive = !oap->inclusive;
3736 oap->motion_type = MCHAR;
3737 }
3738 else if (oap->motion_force == Ctrl_V)
3739 {
3740 // Change line- or characterwise motion into Visual block mode.
3741 if (!VIsual_active)
3742 {
3743 VIsual_active = TRUE;
3744 VIsual = oap->start;
3745 }
3746 VIsual_mode = Ctrl_V;
3747 VIsual_select = FALSE;
3748 VIsual_reselect = FALSE;
3749 }
3750
3751 // Only redo yank when 'y' flag is in 'cpoptions'.
3752 // Never redo "zf" (define fold).
3753 if ((redo_yank || oap->op_type != OP_YANK)
3754 && ((!VIsual_active || oap->motion_force)
3755 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003756 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003757 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003758 && cap->cmdchar != 'D'
3759#ifdef FEAT_FOLDING
3760 && oap->op_type != OP_FOLD
3761 && oap->op_type != OP_FOLDOPEN
3762 && oap->op_type != OP_FOLDOPENREC
3763 && oap->op_type != OP_FOLDCLOSE
3764 && oap->op_type != OP_FOLDCLOSEREC
3765 && oap->op_type != OP_FOLDDEL
3766 && oap->op_type != OP_FOLDDELREC
3767#endif
3768 )
3769 {
3770 prep_redo(oap->regname, cap->count0,
3771 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3772 oap->motion_force, cap->cmdchar, cap->nchar);
3773 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3774 {
3775 // If 'cpoptions' does not contain 'r', insert the search
3776 // pattern to really repeat the same command.
3777 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3778 AppendToRedobuffLit(cap->searchbuf, -1);
3779 AppendToRedobuff(NL_STR);
3780 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003781 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003782 {
3783 // do_cmdline() has stored the first typed line in
3784 // "repeat_cmdline". When several lines are typed repeating
3785 // won't be possible.
3786 if (repeat_cmdline == NULL)
3787 ResetRedobuff();
3788 else
3789 {
zeertzjq30b6d612023-05-07 17:39:23 +01003790 if (cap->cmdchar == ':')
3791 AppendToRedobuffLit(repeat_cmdline, -1);
3792 else
3793 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003794 AppendToRedobuff(NL_STR);
3795 VIM_CLEAR(repeat_cmdline);
3796 }
3797 }
3798 }
3799
3800 if (redo_VIsual_busy)
3801 {
3802 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003803 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003804 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003805 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003806 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3807 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003808 VIsual_mode = redo_VIsual.rv_mode;
3809 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003810 {
3811 if (VIsual_mode == 'v')
3812 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003813 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003814 {
3815 validate_virtcol();
3816 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003817 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003818 }
3819 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003820 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003821 }
3822 else
3823 {
3824 curwin->w_curswant = MAXCOL;
3825 }
3826 coladvance(curwin->w_curswant);
3827 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003828 cap->count0 = redo_VIsual.rv_count;
3829 if (redo_VIsual.rv_count != 0)
3830 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003831 else
3832 cap->count1 = 1;
3833 }
3834 else if (VIsual_active)
3835 {
3836 if (!gui_yank)
3837 {
3838 // Save the current VIsual area for '< and '> marks, and "gv"
3839 curbuf->b_visual.vi_start = VIsual;
3840 curbuf->b_visual.vi_end = curwin->w_cursor;
3841 curbuf->b_visual.vi_mode = VIsual_mode;
3842 restore_visual_mode();
3843 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003844#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003845 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003846#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003847 }
3848
3849 // In Select mode, a linewise selection is operated upon like a
3850 // characterwise selection.
3851 // Special case: gH<Del> deletes the last line.
3852 if (VIsual_select && VIsual_mode == 'V'
3853 && cap->oap->op_type != OP_DELETE)
3854 {
3855 if (LT_POS(VIsual, curwin->w_cursor))
3856 {
3857 VIsual.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003858 curwin->w_cursor.col = ml_get_len(curwin->w_cursor.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003859 }
3860 else
3861 {
3862 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003863 VIsual.col = ml_get_len(VIsual.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003864 }
3865 VIsual_mode = 'v';
3866 }
3867 // If 'selection' is "exclusive", backup one character for
3868 // charwise selections.
3869 else if (VIsual_mode == 'v')
3870 include_line_break = unadjust_for_sel();
3871
3872 oap->start = VIsual;
3873 if (VIsual_mode == 'V')
3874 {
3875 oap->start.col = 0;
3876 oap->start.coladd = 0;
3877 }
3878 }
3879
3880 // Set oap->start to the first position of the operated text, oap->end
3881 // to the end of the operated text. w_cursor is equal to oap->start.
3882 if (LT_POS(oap->start, curwin->w_cursor))
3883 {
3884#ifdef FEAT_FOLDING
3885 // Include folded lines completely.
3886 if (!VIsual_active)
3887 {
3888 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3889 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003890 if ((curwin->w_cursor.col > 0 || oap->inclusive
3891 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003892 && hasFolding(curwin->w_cursor.lnum, NULL,
3893 &curwin->w_cursor.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003894 curwin->w_cursor.col = ml_get_curline_len();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003895 }
3896#endif
3897 oap->end = curwin->w_cursor;
3898 curwin->w_cursor = oap->start;
3899
3900 // w_virtcol may have been updated; if the cursor goes back to its
3901 // previous position w_virtcol becomes invalid and isn't updated
3902 // automatically.
3903 curwin->w_valid &= ~VALID_VIRTCOL;
3904 }
3905 else
3906 {
3907#ifdef FEAT_FOLDING
3908 // Include folded lines completely.
3909 if (!VIsual_active && oap->motion_type == MLINE)
3910 {
3911 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3912 NULL))
3913 curwin->w_cursor.col = 0;
3914 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003915 oap->start.col = ml_get_len(oap->start.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003916 }
3917#endif
3918 oap->end = oap->start;
3919 oap->start = curwin->w_cursor;
3920 }
3921
3922 // Just in case lines were deleted that make the position invalid.
3923 check_pos(curwin->w_buffer, &oap->end);
3924 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3925
3926 // Set "virtual_op" before resetting VIsual_active.
3927 virtual_op = virtual_active();
3928
3929 if (VIsual_active || redo_VIsual_busy)
3930 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003931 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003932
3933 if (!redo_VIsual_busy && !gui_yank)
3934 {
3935 // Prepare to reselect and redo Visual: this is based on the
3936 // size of the Visual text
3937 resel_VIsual_mode = VIsual_mode;
3938 if (curwin->w_curswant == MAXCOL)
3939 resel_VIsual_vcol = MAXCOL;
3940 else
3941 {
3942 if (VIsual_mode != Ctrl_V)
3943 getvvcol(curwin, &(oap->end),
3944 NULL, NULL, &oap->end_vcol);
3945 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3946 {
3947 if (VIsual_mode != Ctrl_V)
3948 getvvcol(curwin, &(oap->start),
3949 &oap->start_vcol, NULL, NULL);
3950 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3951 }
3952 else
3953 resel_VIsual_vcol = oap->end_vcol;
3954 }
3955 resel_VIsual_line_count = oap->line_count;
3956 }
3957
3958 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3959 if ((redo_yank || oap->op_type != OP_YANK)
3960 && oap->op_type != OP_COLON
3961#ifdef FEAT_FOLDING
3962 && oap->op_type != OP_FOLD
3963 && oap->op_type != OP_FOLDOPEN
3964 && oap->op_type != OP_FOLDOPENREC
3965 && oap->op_type != OP_FOLDCLOSE
3966 && oap->op_type != OP_FOLDCLOSEREC
3967 && oap->op_type != OP_FOLDDEL
3968 && oap->op_type != OP_FOLDDELREC
3969#endif
3970 && oap->motion_force == NUL
3971 )
3972 {
3973 // Prepare for redoing. Only use the nchar field for "r",
3974 // otherwise it might be the second char of the operator.
3975 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3976 || cap->nchar == 'N'))
3977 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003978 get_op_char(oap->op_type),
3979 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003980 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003981 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003982 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003983 int opchar = get_op_char(oap->op_type);
3984 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003985 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3986
3987 // reverse what nv_replace() did
3988 if (nchar == REPLACE_CR_NCHAR)
3989 nchar = CAR;
3990 else if (nchar == REPLACE_NL_NCHAR)
3991 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003992
3993 if (opchar == 'g' && extra_opchar == '@')
3994 // also repeat the count for 'operatorfunc'
3995 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3996 cap->count0, opchar, extra_opchar, nchar);
3997 else
3998 prep_redo(oap->regname, 0L, NUL, 'v',
3999 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004000 }
4001 if (!redo_VIsual_busy)
4002 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004003 redo_VIsual.rv_mode = resel_VIsual_mode;
4004 redo_VIsual.rv_vcol = resel_VIsual_vcol;
4005 redo_VIsual.rv_line_count = resel_VIsual_line_count;
4006 redo_VIsual.rv_count = cap->count0;
4007 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004008 }
4009 }
4010
4011 // oap->inclusive defaults to TRUE.
4012 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4013 // FALSE. This makes "d}P" and "v}dP" work the same.
4014 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4015 oap->inclusive = TRUE;
4016 if (VIsual_mode == 'V')
4017 oap->motion_type = MLINE;
4018 else
4019 {
4020 oap->motion_type = MCHAR;
4021 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4022 && (include_line_break || !virtual_op))
4023 {
4024 oap->inclusive = FALSE;
4025 // Try to include the newline, unless it's an operator
4026 // that works on lines only.
4027 if (*p_sel != 'o'
4028 && !op_on_lines(oap->op_type)
4029 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4030 {
4031 ++oap->end.lnum;
4032 oap->end.col = 0;
4033 oap->end.coladd = 0;
4034 ++oap->line_count;
4035 }
4036 }
4037 }
4038
4039 redo_VIsual_busy = FALSE;
4040
4041 // Switch Visual off now, so screen updating does
4042 // not show inverted text when the screen is redrawn.
4043 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4044 // no screen redraw, so it is done here to remove the inverted
4045 // part.
4046 if (!gui_yank)
4047 {
4048 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004049 setmouse();
4050 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004051 may_clear_cmdline();
4052 if ((oap->op_type == OP_YANK
4053 || oap->op_type == OP_COLON
4054 || oap->op_type == OP_FUNCTION
4055 || oap->op_type == OP_FILTER)
4056 && oap->motion_force == NUL)
4057 {
4058#ifdef FEAT_LINEBREAK
4059 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01004060 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004061#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004062 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004063 }
4064 }
4065 }
4066
4067 // Include the trailing byte of a multi-byte char.
4068 if (has_mbyte && oap->inclusive)
4069 {
4070 int l;
4071
4072 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4073 if (l > 1)
4074 oap->end.col += l - 1;
4075 }
4076 curwin->w_set_curswant = TRUE;
4077
4078 // oap->empty is set when start and end are the same. The inclusive
4079 // flag affects this too, unless yanking and the end is on a NUL.
4080 oap->empty = (oap->motion_type == MCHAR
4081 && (!oap->inclusive
4082 || (oap->op_type == OP_YANK
4083 && gchar_pos(&oap->end) == NUL))
4084 && EQUAL_POS(oap->start, oap->end)
4085 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4086 // For delete, change and yank, it's an error to operate on an
4087 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4088 empty_region_error = (oap->empty
4089 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4090
4091 // Force a redraw when operating on an empty Visual region, when
4092 // 'modifiable is off or creating a fold.
4093 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4094#ifdef FEAT_FOLDING
4095 || oap->op_type == OP_FOLD
4096#endif
4097 ))
4098 {
4099#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004100 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004101#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004102 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004103 }
4104
4105 // If the end of an operator is in column one while oap->motion_type
4106 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4107 // character in the previous line. If op_start is on or before the
4108 // first non-blank in the line, the operator becomes linewise
4109 // (strange, but that's the way vi does it).
4110 if ( oap->motion_type == MCHAR
4111 && oap->inclusive == FALSE
4112 && !(cap->retval & CA_NO_ADJ_OP_END)
4113 && oap->end.col == 0
4114 && (!oap->is_VIsual || *p_sel == 'o')
4115 && !oap->block_mode
4116 && oap->line_count > 1)
4117 {
4118 oap->end_adjusted = TRUE; // remember that we did this
4119 --oap->line_count;
4120 --oap->end.lnum;
4121 if (inindent(0))
4122 oap->motion_type = MLINE;
4123 else
4124 {
zeertzjq94b7c322024-03-12 21:50:32 +01004125 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004126 if (oap->end.col)
4127 {
4128 --oap->end.col;
4129 oap->inclusive = TRUE;
4130 }
4131 }
4132 }
4133 else
4134 oap->end_adjusted = FALSE;
4135
4136 switch (oap->op_type)
4137 {
4138 case OP_LSHIFT:
4139 case OP_RSHIFT:
4140 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4141 auto_format(FALSE, TRUE);
4142 break;
4143
4144 case OP_JOIN_NS:
4145 case OP_JOIN:
4146 if (oap->line_count < 2)
4147 oap->line_count = 2;
4148 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4149 curbuf->b_ml.ml_line_count)
4150 beep_flush();
4151 else
4152 {
4153 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4154 TRUE, TRUE, TRUE);
4155 auto_format(FALSE, TRUE);
4156 }
4157 break;
4158
4159 case OP_DELETE:
4160 VIsual_reselect = FALSE; // don't reselect now
4161 if (empty_region_error)
4162 {
4163 vim_beep(BO_OPER);
4164 CancelRedo();
4165 }
4166 else
4167 {
4168 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004169 // save cursor line for undo if it wasn't saved yet
4170 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4171 && u_save_cursor() == OK)
4172 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004173 }
4174 break;
4175
4176 case OP_YANK:
4177 if (empty_region_error)
4178 {
4179 if (!gui_yank)
4180 {
4181 vim_beep(BO_OPER);
4182 CancelRedo();
4183 }
4184 }
4185 else
4186 {
4187#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004188 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004189#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004190 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004191 (void)op_yank(oap, FALSE, !gui_yank);
4192 }
4193 check_cursor_col();
4194 break;
4195
4196 case OP_CHANGE:
4197 VIsual_reselect = FALSE; // don't reselect now
4198 if (empty_region_error)
4199 {
4200 vim_beep(BO_OPER);
4201 CancelRedo();
4202 }
4203 else
4204 {
4205 // This is a new edit command, not a restart. Need to
4206 // remember it to make 'insertmode' work with mappings for
4207 // Visual mode. But do this only once and not when typed and
4208 // 'insertmode' isn't set.
4209 if (p_im || !KeyTyped)
4210 restart_edit_save = restart_edit;
4211 else
4212 restart_edit_save = 0;
4213 restart_edit = 0;
4214#ifdef FEAT_LINEBREAK
4215 // Restore linebreak, so that when the user edits it looks as
4216 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004217 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004218#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004219 // trigger TextChangedI
4220 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4221
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004222 if (op_change(oap)) // will call edit()
4223 cap->retval |= CA_COMMAND_BUSY;
4224 if (restart_edit == 0)
4225 restart_edit = restart_edit_save;
4226 }
4227 break;
4228
4229 case OP_FILTER:
4230 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4231 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4232 else
4233 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4234 // FALLTHROUGH
4235
4236 case OP_INDENT:
4237 case OP_COLON:
4238
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004239 // If 'equalprg' is empty, do the indenting internally.
4240 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4241 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004242 if (curbuf->b_p_lisp)
4243 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004244#ifdef FEAT_EVAL
4245 if (use_indentexpr_for_lisp())
4246 op_reindent(oap, get_expr_indent);
4247 else
4248#endif
4249 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004250 break;
4251 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004252 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004253#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004254 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004255#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004256 get_c_indent);
4257 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004258 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004259
4260 op_colon(oap);
4261 break;
4262
4263 case OP_TILDE:
4264 case OP_UPPER:
4265 case OP_LOWER:
4266 case OP_ROT13:
4267 if (empty_region_error)
4268 {
4269 vim_beep(BO_OPER);
4270 CancelRedo();
4271 }
4272 else
4273 op_tilde(oap);
4274 check_cursor_col();
4275 break;
4276
4277 case OP_FORMAT:
4278#if defined(FEAT_EVAL)
4279 if (*curbuf->b_p_fex != NUL)
4280 op_formatexpr(oap); // use expression
4281 else
4282#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004283 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004284 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004285 op_colon(oap); // use external command
4286 else
4287 op_format(oap, FALSE); // use internal function
4288 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004289 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004290 case OP_FORMAT2:
4291 op_format(oap, TRUE); // use internal function
4292 break;
4293
4294 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004295 {
4296 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4297
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004298#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004299 // Restore linebreak, so that when the user edits it looks as
4300 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004301 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004302#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004303 // call 'operatorfunc'
4304 op_function(oap);
4305
4306 // Restore the info for redoing Visual mode, the function may
4307 // invoke another operator and unintentionally change it.
4308 redo_VIsual = save_redo_VIsual;
4309 break;
4310 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004311
4312 case OP_INSERT:
4313 case OP_APPEND:
4314 VIsual_reselect = FALSE; // don't reselect now
4315 if (empty_region_error)
4316 {
4317 vim_beep(BO_OPER);
4318 CancelRedo();
4319 }
4320 else
4321 {
4322 // This is a new edit command, not a restart. Need to
4323 // remember it to make 'insertmode' work with mappings for
4324 // Visual mode. But do this only once.
4325 restart_edit_save = restart_edit;
4326 restart_edit = 0;
4327#ifdef FEAT_LINEBREAK
4328 // Restore linebreak, so that when the user edits it looks as
4329 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004330 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004331#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004332 // trigger TextChangedI
4333 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4334
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004335 op_insert(oap, cap->count1);
4336#ifdef FEAT_LINEBREAK
4337 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004338 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004339#endif
4340
4341 // TODO: when inserting in several lines, should format all
4342 // the lines.
4343 auto_format(FALSE, TRUE);
4344
4345 if (restart_edit == 0)
4346 restart_edit = restart_edit_save;
4347 else
4348 cap->retval |= CA_COMMAND_BUSY;
4349 }
4350 break;
4351
4352 case OP_REPLACE:
4353 VIsual_reselect = FALSE; // don't reselect now
4354 if (empty_region_error)
4355 {
4356 vim_beep(BO_OPER);
4357 CancelRedo();
4358 }
4359 else
4360 {
4361#ifdef FEAT_LINEBREAK
4362 // Restore linebreak, so that when the user edits it looks as
4363 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004364 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004365#endif
4366 op_replace(oap, cap->nchar);
4367 }
4368 break;
4369
4370#ifdef FEAT_FOLDING
4371 case OP_FOLD:
4372 VIsual_reselect = FALSE; // don't reselect now
4373 foldCreate(oap->start.lnum, oap->end.lnum);
4374 break;
4375
4376 case OP_FOLDOPEN:
4377 case OP_FOLDOPENREC:
4378 case OP_FOLDCLOSE:
4379 case OP_FOLDCLOSEREC:
4380 VIsual_reselect = FALSE; // don't reselect now
4381 opFoldRange(oap->start.lnum, oap->end.lnum,
4382 oap->op_type == OP_FOLDOPEN
4383 || oap->op_type == OP_FOLDOPENREC,
4384 oap->op_type == OP_FOLDOPENREC
4385 || oap->op_type == OP_FOLDCLOSEREC,
4386 oap->is_VIsual);
4387 break;
4388
4389 case OP_FOLDDEL:
4390 case OP_FOLDDELREC:
4391 VIsual_reselect = FALSE; // don't reselect now
4392 deleteFold(oap->start.lnum, oap->end.lnum,
4393 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4394 break;
4395#endif
4396 case OP_NR_ADD:
4397 case OP_NR_SUB:
4398 if (empty_region_error)
4399 {
4400 vim_beep(BO_OPER);
4401 CancelRedo();
4402 }
4403 else
4404 {
4405 VIsual_active = TRUE;
4406#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004407 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004408#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004409 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004410 VIsual_active = FALSE;
4411 }
4412 check_cursor_col();
4413 break;
4414 default:
4415 clearopbeep(oap);
4416 }
4417 virtual_op = MAYBE;
4418 if (!gui_yank)
4419 {
4420 // if 'sol' not set, go back to old column for some commands
4421 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4422 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4423 || oap->op_type == OP_DELETE))
4424 {
4425#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004426 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004427#endif
4428 coladvance(curwin->w_curswant = old_col);
4429 }
4430 }
4431 else
4432 {
4433 curwin->w_cursor = old_cursor;
4434 }
4435 oap->block_mode = FALSE;
4436 clearop(oap);
4437 motion_force = NUL;
4438 }
4439#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004440 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004441#endif
4442}
Christian Brabandtffb13672023-09-15 20:22:02 +02004443
4444// put byte 'c' at position 'lp', but
4445// verify, that the position to place
4446// is actually safe
4447 static void
4448pbyte(pos_T lp, int c)
4449{
4450 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4451 int len = curbuf->b_ml.ml_line_len;
4452
4453 // safety check
4454 if (lp.col >= len)
4455 lp.col = (len > 1 ? len - 2 : 0);
4456 *(p + lp.col) = c;
4457}