blob: 1dd36ab2867d0a0a96820f7ebdb8f349d9899527 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
Bram Moolenaar032a2d02020-12-22 17:59:35 +010012 * op_change, op_yank, do_join
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010017static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Christian Brabandtffb13672023-09-15 20:22:02 +020020static void pbyte(pos_T lp, int c);
21#define PBYTE(lp, c) pbyte(lp, c)
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaarf2732452018-06-03 14:47:35 +020024// Flags for third item in "opchars".
25#define OPF_LINES 1 // operator always works on lines
26#define OPF_CHANGE 2 // operator changes text
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028/*
29 * The names of operators.
30 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020031 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 */
33static char opchars[][3] =
34{
Bram Moolenaarf2732452018-06-03 14:47:35 +020035 {NUL, NUL, 0}, // OP_NOP
36 {'d', NUL, OPF_CHANGE}, // OP_DELETE
37 {'y', NUL, 0}, // OP_YANK
38 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
39 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
40 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
41 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
42 {'g', '~', OPF_CHANGE}, // OP_TILDE
43 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
44 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
45 {':', NUL, OPF_LINES}, // OP_COLON
46 {'g', 'U', OPF_CHANGE}, // OP_UPPER
47 {'g', 'u', OPF_CHANGE}, // OP_LOWER
48 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
49 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
50 {'g', '?', OPF_CHANGE}, // OP_ROT13
51 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
52 {'I', NUL, OPF_CHANGE}, // OP_INSERT
53 {'A', NUL, OPF_CHANGE}, // OP_APPEND
54 {'z', 'f', OPF_LINES}, // OP_FOLD
55 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
56 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
57 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
58 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
59 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
60 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
61 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
62 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
63 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
64 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000065};
66
67/*
68 * Translate a command name into an operator type.
69 * Must only be called with a valid operator name!
70 */
71 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010072get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
74 int i;
75
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010076 if (char1 == 'r') // ignore second character
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 return OP_REPLACE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010078 if (char1 == '~') // when tilde is an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 return OP_TILDE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010080 if (char1 == 'g' && char2 == Ctrl_A) // add
Bram Moolenaard79e5502016-01-10 22:13:02 +010081 return OP_NR_ADD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010082 if (char1 == 'g' && char2 == Ctrl_X) // subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +010083 return OP_NR_SUB;
Christian Brabandt544a38e2021-06-10 19:39:11 +020084 if (char1 == 'z' && char2 == 'y') // OP_YANK
85 return OP_YANK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 if (opchars[i][0] == char1 && opchars[i][1] == char2)
89 break;
K.Takataeeec2542021-06-02 13:28:16 +020090 if (i == (int)ARRAY_LENGTH(opchars) - 1)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010091 {
92 internal_error("get_op_type()");
93 break;
94 }
95 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 return i;
97}
98
Bram Moolenaar071d4272004-06-13 20:20:40 +000099/*
100 * Return TRUE if operator "op" always works on whole lines.
101 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +0200102 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100103op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200105 return opchars[op][2] & OPF_LINES;
106}
107
Bram Moolenaar113e1072019-01-20 15:30:40 +0100108#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200109/*
110 * Return TRUE if operator "op" changes text.
111 */
112 int
113op_is_change(int op)
114{
115 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
120 * Get first operator command character.
121 * Returns 'g' or 'z' if there is another command character.
122 */
123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100124get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 return opchars[optype][0];
127}
128
129/*
130 * Get second operator command character.
131 */
132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100133get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134{
135 return opchars[optype][1];
136}
137
138/*
139 * op_shift - handle a shift operation
140 */
141 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100142op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143{
144 long i;
145 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
148 if (u_save((linenr_T)(oap->start.lnum - 1),
149 (linenr_T)(oap->end.lnum + 1)) == FAIL)
150 return;
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 if (oap->block_mode)
153 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155 for (i = oap->line_count; --i >= 0; )
156 {
157 first_char = *ml_get_curline();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100158 if (first_char == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 else if (oap->block_mode)
161 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // Move the line right if it doesn't start with '#', 'smartindent'
164 // isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 if (first_char != '#' || !preprocs_left())
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100166 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 ++curwin->w_cursor.lnum;
168 }
169
170 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (oap->block_mode)
172 {
173 curwin->w_cursor.lnum = oap->start.lnum;
174 curwin->w_cursor.col = block_col;
175 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100176 else if (curs_top) // put cursor on first line, for ">>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 {
178 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100179 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 }
181 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100182 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100184#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100185 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100186 foldOpenCursor();
187#endif
188
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 if (oap->line_count > p_report)
191 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200192 char *op;
193 char *msg_line_single;
194 char *msg_line_plural;
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200197 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200199 op = "<";
200 msg_line_single = NGETTEXT("%ld line %sed %d time",
201 "%ld line %sed %d times", amount);
202 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
203 "%ld lines %sed %d times", amount);
204 vim_snprintf((char *)IObuff, IOSIZE,
205 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
206 oap->line_count, op, amount);
Bram Moolenaare4fc7462020-03-15 19:17:50 +0100207 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 }
209
Bram Moolenaare1004402020-10-24 20:49:43 +0200210 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100211 {
212 // Set "'[" and "']" marks.
213 curbuf->b_op_start = oap->start;
214 curbuf->b_op_end.lnum = oap->end.lnum;
zeertzjq94b7c322024-03-12 21:50:32 +0100215 curbuf->b_op_end.col = ml_get_len(oap->end.lnum);
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100216 if (curbuf->b_op_end.col > 0)
217 --curbuf->b_op_end.col;
218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219}
220
221/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100222 * Shift the current line one shiftwidth left (if left != 0) or right
223 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 */
225 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100226shift_line(
227 int left,
228 int round,
229 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200230 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
Ernie Raelfda700c2023-11-30 18:20:00 +0100232 vimlong_T count;
Ernie Rael2b0882f2023-11-23 20:21:45 +0100233 int i, j;
234 int sw_val = trim_to_int(get_sw_value_indent(curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Ernie Raelfda700c2023-11-30 18:20:00 +0100236 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200238 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200240 i = count / sw_val; // number of 'shiftwidth' rounded down
241 j = count % sw_val; // extra spaces
242 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 --amount;
244 if (left)
245 {
246 i -= amount;
247 if (i < 0)
248 i = 0;
249 }
250 else
251 i += amount;
Ernie Raelfda700c2023-11-30 18:20:00 +0100252 count = (vimlong_T)i * (vimlong_T)sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200254 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {
256 if (left)
257 {
Ernie Raelfda700c2023-11-30 18:20:00 +0100258 count -= (vimlong_T)sw_val * (vimlong_T)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (count < 0)
260 count = 0;
261 }
262 else
Ernie Raelfda700c2023-11-30 18:20:00 +0100263 count += (vimlong_T)sw_val * (vimlong_T)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200266 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 if (State & VREPLACE_FLAG)
Ernie Rael2b0882f2023-11-23 20:21:45 +0100268 change_indent(INDENT_SET, trim_to_int(count), FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 else
Ernie Rael2b0882f2023-11-23 20:21:45 +0100270 (void)set_indent(trim_to_int(count), call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271}
272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273/*
274 * Shift one line of the current block one shiftwidth right or left.
275 * Leaves cursor on first character in block.
276 */
277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100278shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280 int left = (oap->op_type == OP_LSHIFT);
281 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000282 int total;
283 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +0200284 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200286 int sw_val = (int)get_sw_value_indent(curbuf);
287 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000290 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100291 int added;
John Marriott38b9f452024-04-25 21:39:18 +0200292 size_t new_line_len; // the length of the line after the
LemonBoy4b936742022-05-13 21:56:28 +0100293 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#ifdef FEAT_RIGHTLEFT
295 int old_p_ri = p_ri;
296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100297 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298#endif
299
Bram Moolenaar24959102022-05-07 20:01:16 +0100300 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
302 if (bd.is_short)
303 return;
304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100305 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 total = (int)((unsigned)amount * (unsigned)sw_val);
307 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100308 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200309
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 oldp = ml_get_curline();
John Marriott38b9f452024-04-25 21:39:18 +0200311 oldlen = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313 if (!left)
314 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100315 int tabs = 0, spaces = 0;
316 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100317
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 /*
319 * 1. Get start vcol
320 * 2. Total ws vcols
321 * 3. Divvy into TABs & spp
322 * 4. Construct new string
323 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100324 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 ws_vcol = bd.start_vcol - bd.pre_whitesp;
326 if (bd.startspaces)
327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100329 {
330 if ((*mb_ptr2len)(bd.textstart) == 1)
331 ++bd.textstart;
332 else
333 {
334 ws_vcol = 0;
335 bd.startspaces = 0;
336 }
337 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000338 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000339 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100341
342 // TODO: is passing bd.textstart for start of the line OK?
343 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
344 bd.start_vcol, bd.textstart, bd.textstart);
345 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100347 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100349 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100351 bd.textstart = cts.cts_ptr;
352 bd.start_vcol = cts.cts_vcol;
353 clear_chartabsize_arg(&cts);
354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100355 // OK, now total=all the VWS reqd, and textstart points at the 1st
356 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#ifdef FEAT_VARTABS
358 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200359 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100360 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200361 else
LemonBoy4b936742022-05-13 21:56:28 +0100362 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200363#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100365 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
366 if (tabs > 0)
367 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else
LemonBoy4b936742022-05-13 21:56:28 +0100369 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200370#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100371 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100373
John Marriott38b9f452024-04-25 21:39:18 +0200374 new_line_len = bd.textcol + tabs + spaces + (oldlen - (bd.textstart - oldp));
LemonBoy4b936742022-05-13 21:56:28 +0100375 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (newp == NULL)
377 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +0200379 newlen = bd.textcol;
380 vim_memset(newp + newlen, TAB, (size_t)tabs);
381 newlen += tabs;
382 vim_memset(newp + newlen, ' ', (size_t)spaces);
383 STRCPY(newp + newlen + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100385 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387 colnr_T destination_col; // column to which text in block will
388 // be shifted
389 char_u *verbatim_copy_end; // end of the part of the line which is
390 // copied verbatim
391 colnr_T verbatim_copy_width;// the (displayed) width of this part
392 // of line
John Marriott38b9f452024-04-25 21:39:18 +0200393 size_t fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000394 size_t block_space_width;
395 size_t shift_amount;
396 char_u *non_white = bd.textstart;
397 colnr_T non_white_col;
John Marriott38b9f452024-04-25 21:39:18 +0200398 size_t fixedlen; // length of string left of the shift
399 // position (ie the string not being shifted)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100400 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000402 /*
403 * Firstly, let's find the first non-whitespace character that is
404 * displayed after the block's start column and the character's column
405 * number. Also, let's calculate the width of all the whitespace
406 * characters that are displayed in the block and precede the searched
407 * non-whitespace character.
408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100410 // If "bd.startspaces" is set, "bd.textstart" points to the character,
411 // the part of which is displayed at the block's beginning. Let's start
412 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000413 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100414 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100416 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417 non_white_col = bd.start_vcol;
418
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100419 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
420 non_white_col, bd.textstart, non_white);
421 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000422 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100423 incr = lbr_chartabsize_adv(&cts);
424 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100426 non_white_col = cts.cts_vcol;
427 non_white = cts.cts_ptr;
428 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000430 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100431 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000432 shift_amount = (block_space_width < (size_t)total
433 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100435 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000436 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // Now let's find out how much of the beginning of the line we can
439 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000440 verbatim_copy_end = bd.textstart;
441 verbatim_copy_width = bd.start_vcol;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // If "bd.startspaces" is set, "bd.textstart" points to the character
444 // preceding the block. We have to subtract its width to obtain its
445 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000446 if (bd.startspaces)
447 verbatim_copy_width -= bd.start_char_vcols;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100448 init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
449 bd.textstart, verbatim_copy_end);
450 while (cts.cts_vcol < destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000451 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100452 incr = lbr_chartabsize(&cts);
453 if (cts.cts_vcol + incr > destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000454 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100455 cts.cts_vcol += incr;
456 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100458 verbatim_copy_width = cts.cts_vcol;
459 verbatim_copy_end = cts.cts_ptr;
460 clear_chartabsize_arg(&cts);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100462 // If "destination_col" is different from the width of the initial
463 // part of the line that will be copied, it means we encountered a tab
464 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000465 fill = destination_col - verbatim_copy_width;
466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100467 // The replacement line will consist of:
468 // - the beginning of the original line up to "verbatim_copy_end",
469 // - "fill" number of spaces,
470 // - the rest of the line, pointed to by non_white.
John Marriott38b9f452024-04-25 21:39:18 +0200471 fixedlen = verbatim_copy_end - oldp;
472 new_line_len = fixedlen + fill + (oldlen - (non_white - oldp));
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000473
LemonBoy4b936742022-05-13 21:56:28 +0100474 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 if (newp == NULL)
476 return;
John Marriott38b9f452024-04-25 21:39:18 +0200477 mch_memmove(newp, oldp, fixedlen);
478 newlen = fixedlen;
479 vim_memset(newp + newlen, ' ', (size_t)fill);
480 STRCPY(newp + newlen + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100482 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
John Marriott38b9f452024-04-25 21:39:18 +0200484
485 // compute the number of bytes added or subtracted.
486 // note new_line_len and oldlen are unsigned so we have
487 // to be careful about how we calculate this.
488 if (new_line_len >= oldlen)
489 added = (int)(new_line_len - oldlen);
490 else
491 added = 0 - (int)(oldlen - new_line_len);
LemonBoy4b936742022-05-13 21:56:28 +0100492 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 State = oldstate;
494 curwin->w_cursor.col = oldcol;
495#ifdef FEAT_RIGHTLEFT
496 p_ri = old_p_ri;
497#endif
498}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500/*
501 * Insert string "s" (b_insert ? before : after) block :AKelly
502 * Caller must prepare for undo.
503 */
504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100505block_insert(
506 oparg_T *oap,
507 char_u *s,
John Marriott38b9f452024-04-25 21:39:18 +0200508 size_t slen,
Bram Moolenaar9b578142016-01-30 19:39:49 +0100509 int b_insert,
510 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
Bram Moolenaardac13472019-09-16 21:06:21 +0200512 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100513 int count = 0; // extra spaces to replace a cut TAB
514 int spaces = 0; // non-zero if cutting a TAB
515 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200516 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100517 char_u *newp, *oldp; // new, old lines
518 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 int oldstate = State;
520
Bram Moolenaar24959102022-05-07 20:01:16 +0100521 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
523 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
524 {
525 block_prep(oap, bdp, lnum, TRUE);
526 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100527 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
529 oldp = ml_get(lnum);
530
531 if (b_insert)
532 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200533 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 spaces = bdp->startspaces;
535 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100536 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 offset = bdp->textcol;
538 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100539 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200541 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200544 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100546 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 offset = bdp->textcol + bdp->textlen - (spaces != 0);
548 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100551 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (!bdp->is_MAX)
553 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
554 count = spaces;
555 offset = bdp->textcol + bdp->textlen;
556 }
557 }
558
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200559 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000560 // avoid copying part of a multi-byte character
561 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100562
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200563 if (spaces < 0) // can happen when the cursor was moved
564 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200565
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000566 // Make sure the allocated size matches what is actually copied below.
John Marriott38b9f452024-04-25 21:39:18 +0200567 newp = alloc(ml_get_len(lnum) + spaces + slen
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000568 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
569 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (newp == NULL)
571 continue;
572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100573 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000574 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 oldp += offset;
576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100577 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200578 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200579 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100581 // copy the new text
John Marriott38b9f452024-04-25 21:39:18 +0200582 mch_memmove(newp + startcol, s, slen);
583 offset += slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000585 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000587 if (*oldp == TAB)
588 {
589 // insert post-padding
590 vim_memset(newp + offset + spaces, ' ',
591 (size_t)(ts_val - spaces));
592 // we're splitting a TAB, don't copy it
593 oldp++;
594 // We allowed for that TAB, remember this now
595 count++;
596 }
597 else
598 // Not a TAB, no extra spaces
599 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601
602 if (spaces > 0)
603 offset += count;
John Marriott38b9f452024-04-25 21:39:18 +0200604 STRCPY(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 ml_replace(lnum, newp, FALSE);
607
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200608 if (b_insert)
609 // correct any text properties
John Marriott38b9f452024-04-25 21:39:18 +0200610 inserted_bytes(lnum, startcol, slen);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (lnum == oap->end.lnum)
613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 // Set "']" mark to the end of the block instead of the end of
615 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 curbuf->b_op_end.lnum = oap->end.lnum;
617 curbuf->b_op_end.col = offset;
618 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100619 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
622
623 State = oldstate;
624}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200627 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200629 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 */
631 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100632op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 int n;
635 linenr_T lnum;
636 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 char_u *newp, *oldp;
638 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
640 int did_yank = FALSE;
641
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100642 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 return OK;
644
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if (oap->empty)
647 return u_save_cursor();
648
649 if (!curbuf->b_p_ma)
650 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200651 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 return FAIL;
653 }
654
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000655 if (VIsual_select && oap->is_VIsual)
656 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100657 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000658
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#ifdef FEAT_CLIPBOARD
660 adjust_clip_reg(&oap->regname);
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (has_mbyte)
664 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
Bram Moolenaard04b7502010-07-08 22:27:55 +0200666 /*
667 * Imitate the strange Vi behaviour: If the delete spans more than one
668 * line and motion_type == MCHAR and the result is a blank line, make the
669 * delete linewise. Don't do this for the change command or Visual mode.
670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000673 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100675 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && oap->op_type == OP_DELETE)
677 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200678 ptr = ml_get(oap->end.lnum) + oap->end.col;
679 if (*ptr != NUL)
680 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 ptr = skipwhite(ptr);
682 if (*ptr == NUL && inindent(0))
683 oap->motion_type = MLINE;
684 }
685
Bram Moolenaard04b7502010-07-08 22:27:55 +0200686 /*
687 * Check for trying to delete (e.g. "D") in an empty line.
688 * Note: For the change operator it is ok.
689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if ( oap->motion_type == MCHAR
691 && oap->line_count == 1
692 && oap->op_type == OP_DELETE
693 && *ml_get(oap->start.lnum) == NUL)
694 {
695 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000696 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 * 'cpoptions' (Vi compatible).
698 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000699 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100700 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
701 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000702 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
704 beep_flush();
705 return OK;
706 }
707
Bram Moolenaard04b7502010-07-08 22:27:55 +0200708 /*
709 * Do a yank of whatever we're about to delete.
710 * If a yank register was specified, put the deleted text into that
711 * register. For the black hole register '_' don't yank anything.
712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (oap->regname != '_')
714 {
715 if (oap->regname != 0)
716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100717 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (!valid_yank_reg(oap->regname, TRUE))
719 {
720 beep_flush();
721 return OK;
722 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100723 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
724 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 did_yank = TRUE;
726 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200727 else
728 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 /*
731 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100732 * delete contains a line break, or when using a specific operator (Vi
733 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100735 if (oap->motion_type == MLINE || oap->line_count > 1
736 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100738 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (op_yank(oap, TRUE, FALSE) == OK)
740 did_yank = TRUE;
741 }
742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100743 // Yank into small delete register when no named register specified
744 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200745 if ((
746#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200747 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
748 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200749#endif
750 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 && oap->line_count == 1)
752 {
753 oap->regname = '-';
754 get_yank_register(oap->regname, TRUE);
755 if (op_yank(oap, TRUE, FALSE) == OK)
756 did_yank = TRUE;
757 oap->regname = 0;
758 }
759
760 /*
761 * If there's too much stuff to fit in the yank register, then get a
762 * confirmation before doing the delete. This is crude, but simple.
763 * And it avoids doing a delete of something we can't put back if we
764 * want.
765 */
766 if (!did_yank)
767 {
768 int msg_silent_save = msg_silent;
769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100770 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
772 msg_silent = msg_silent_save;
773 if (n != 'y')
774 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000775 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 return FAIL;
777 }
778 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100779
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100780#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100781 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200782 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785
Bram Moolenaard04b7502010-07-08 22:27:55 +0200786 /*
787 * block mode delete
788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 if (oap->block_mode)
790 {
791 if (u_save((linenr_T)(oap->start.lnum - 1),
792 (linenr_T)(oap->end.lnum + 1)) == FAIL)
793 return FAIL;
794
795 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
796 {
797 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100798 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 continue;
800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (lnum == curwin->w_cursor.lnum)
803 {
804 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807
Bram Moolenaar8055d172019-05-17 22:57:26 +0200808 // "n" == number of chars deleted
809 // If we delete a TAB, it may be replaced by several characters.
810 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 n = bd.textlen - bd.startspaces - bd.endspaces;
812 oldp = ml_get(lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100813 newp = alloc(ml_get_len(lnum) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (newp == NULL)
815 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200819 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100821 // copy the part after the deleted part
John Marriott38b9f452024-04-25 21:39:18 +0200822 STRCPY(newp + bd.textcol + bd.startspaces + bd.endspaces,
823 oldp + bd.textcol + bd.textlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100824 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200826
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100827#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200828 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200829 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832
833 check_cursor_col();
834 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
835 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100838 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
840 if (oap->op_type == OP_CHANGE)
841 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // Delete the lines except the first one. Temporarily move the
843 // cursor to the next line. Save the current line number, if the
844 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (oap->line_count > 1)
846 {
847 lnum = curwin->w_cursor.lnum;
848 ++curwin->w_cursor.lnum;
849 del_lines((long)(oap->line_count - 1), TRUE);
850 curwin->w_cursor.lnum = lnum;
851 }
852 if (u_save_cursor() == FAIL)
853 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 beginline(BL_WHITE); // cursor on first non-white
857 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 ai_col = curwin->w_cursor.col;
859 }
860 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 beginline(0); // cursor in column 0
zeertzjq8c55d602024-03-13 20:42:26 +0100862 truncate_line(FALSE); // delete the rest of the line,
863 // leaving cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 else
868 {
869 del_lines(oap->line_count, TRUE);
870 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 }
874 else
875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (virtual_op)
877 {
878 int endcol = 0;
879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100880 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (gchar_pos(&oap->start) == '\t')
882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 return FAIL;
885 if (oap->line_count == 1)
886 endcol = getviscol2(oap->end.col, oap->end.coladd);
887 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
888 oap->start = curwin->w_cursor;
889 if (oap->line_count == 1)
890 {
891 coladvance(endcol);
892 oap->end.col = curwin->w_cursor.col;
893 oap->end.coladd = curwin->w_cursor.coladd;
894 curwin->w_cursor = oap->start;
895 }
896 }
897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100898 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (gchar_pos(&oap->end) == '\t'
900 && (int)oap->end.coladd < oap->inclusive)
901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (u_save((linenr_T)(oap->end.lnum - 1),
904 (linenr_T)(oap->end.lnum + 1)) == FAIL)
905 return FAIL;
906 curwin->w_cursor = oap->end;
907 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
908 oap->end = curwin->w_cursor;
909 curwin->w_cursor = oap->start;
910 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100911 if (has_mbyte)
912 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100915 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 return FAIL;
919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100921 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 && oap->op_type == OP_CHANGE
923 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100924 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 display_dollar(oap->end.col - !oap->inclusive);
926
927 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (virtual_op)
930 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 // fix up things for virtualedit-delete:
932 // break the tabs which are going to get in our way
zeertzjq94b7c322024-03-12 21:50:32 +0100933 int len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935 if (oap->end.coladd != 0
936 && (int)oap->end.col >= len - 1
937 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
938 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100939 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (n == 0 && oap->start.coladd != oap->end.coladd)
941 n = 1;
942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100943 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (gchar_cursor() != NUL)
945 curwin->w_cursor.coladd = 0;
946 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200947 (void)del_bytes((long)n, !virtual_op,
948 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100950 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 {
952 pos_T curpos;
953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100954 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
956 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
957 return FAIL;
958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++curwin->w_cursor.lnum;
963 del_lines((long)(oap->line_count - 2), FALSE);
964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100965 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200966 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200967 curwin->w_cursor.col = 0;
968 (void)del_bytes((long)n, !virtual_op,
969 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100970 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200971 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200973 if (oap->op_type == OP_DELETE)
974 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976
977 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
978
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000979setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200980 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100982 if (oap->block_mode)
983 {
984 curbuf->b_op_end.lnum = oap->end.lnum;
985 curbuf->b_op_end.col = oap->start.col;
986 }
987 else
988 curbuf->b_op_end = oap->start;
989 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 return OK;
993}
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Adjust end of operating area for ending on a multi-byte character.
997 * Used for deletion.
998 */
999 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
zeertzjq048761b2024-02-24 14:21:39 +01001002 char_u *line;
1003 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001005 if (!oap->inclusive)
1006 return;
1007
zeertzjq048761b2024-02-24 14:21:39 +01001008 line = ml_get(oap->end.lnum);
1009 ptr = line + oap->end.col;
1010 if (*ptr != NUL)
1011 {
1012 ptr -= (*mb_head_off)(line, ptr);
1013 ptr += (*mb_ptr2len)(ptr) - 1;
1014 oap->end.col = ptr - line;
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaar630afe82018-06-28 19:26:28 +02001018/*
1019 * Replace the character under the cursor with "c".
1020 * This takes care of multi-byte characters.
1021 */
1022 static void
1023replace_character(int c)
1024{
1025 int n = State;
1026
Bram Moolenaar24959102022-05-07 20:01:16 +01001027 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001028 ins_char(c);
1029 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001030 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001031 dec_cursor();
1032}
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/*
1035 * Replace a whole area with one character.
1036 */
1037 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001038op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
1040 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 char_u *newp, *oldp;
John Marriott38b9f452024-04-25 21:39:18 +02001043 size_t newlen, oldlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001045 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001046 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
1048 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001051 if (c == REPLACE_CR_NCHAR)
1052 {
1053 had_ctrl_v_cr = TRUE;
1054 c = CAR;
1055 }
1056 else if (c == REPLACE_NL_NCHAR)
1057 {
1058 had_ctrl_v_cr = TRUE;
1059 c = NL;
1060 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (has_mbyte)
1063 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 if (u_save((linenr_T)(oap->start.lnum - 1),
1066 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1067 return FAIL;
1068
1069 /*
1070 * block mode replace
1071 */
1072 if (oap->block_mode)
1073 {
1074 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1075 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1079 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001080 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001082 // n == number of extra chars required
1083 // If we split a TAB, it may be replaced by several characters.
1084 // Thus the number of characters may increase!
1085 // If the range starts in virtual space, count the initial
1086 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1088 {
1089 pos_T vpos;
1090
Bram Moolenaara1381de2009-11-03 15:44:21 +00001091 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 getvpos(&vpos, oap->start_vcol);
1093 bd.startspaces += vpos.coladd;
1094 n = bd.startspaces;
1095 }
1096 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001097 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001104 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 numc = oap->end_vcol - oap->start_vcol + 1;
1106 if (bd.is_short && (!virtual_op || bd.is_MAX))
1107 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001109 // A double-wide character can be replaced only up to half the
1110 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if ((*mb_char2cells)(c) > 1)
1112 {
1113 if ((numc & 1) && !bd.is_short)
1114 {
1115 ++bd.endspaces;
1116 ++n;
1117 }
1118 numc = numc / 2;
1119 }
1120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001121 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 num_chars = numc;
1123 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001124 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 n += numc - bd.textlen;
1126
1127 oldp = ml_get_curline();
zeertzjq94b7c322024-03-12 21:50:32 +01001128 oldlen = ml_get_curline_len();
Bram Moolenaar964b3742019-05-24 18:54:09 +02001129 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (newp == NULL)
1131 continue;
1132 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001133 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001135 newlen = bd.textcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // insert pre-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001137 vim_memset(newp + newlen, ' ', (size_t)bd.startspaces);
1138 newlen += bd.startspaces;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001139 // insert replacement chars CHECK FOR ALLOCATED SPACE
1140 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1141 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001142 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001144 if (has_mbyte)
1145 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001146 while (--num_chars >= 0)
John Marriott38b9f452024-04-25 21:39:18 +02001147 newlen += (*mb_char2bytes)(c, newp + newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001148 }
1149 else
John Marriott38b9f452024-04-25 21:39:18 +02001150 {
1151 vim_memset(newp + newlen, c, (size_t)numc);
1152 newlen += numc;
1153 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001154 if (!bd.is_short)
1155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // insert post-spaces
John Marriott38b9f452024-04-25 21:39:18 +02001157 vim_memset(newp + newlen, ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001158 // copy the part after the changed part
John Marriott38b9f452024-04-25 21:39:18 +02001159 STRCPY(newp + newlen + bd.endspaces,
1160 oldp + bd.textcol + bd.textlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001165 // Replacing with \r or \n means splitting the line.
John Marriott38b9f452024-04-25 21:39:18 +02001166 after_p = alloc(oldlen + 1 + n - newlen);
Bram Moolenaard9820532013-11-04 01:41:17 +01001167 if (after_p != NULL)
John Marriott38b9f452024-04-25 21:39:18 +02001168 STRCPY(after_p, oldp + bd.textcol + bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
John Marriott38b9f452024-04-25 21:39:18 +02001170
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001171 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001173 if (after_p != NULL)
1174 {
1175 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1176 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1177 oap->end.lnum++;
1178 vim_free(after_p);
1179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
1181 }
1182 else
1183 {
1184 /*
1185 * MCHAR and MLINE motion replace.
1186 */
1187 if (oap->motion_type == MLINE)
1188 {
1189 oap->start.col = 0;
1190 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001191 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (oap->end.col)
1193 --oap->end.col;
1194 }
1195 else if (!oap->inclusive)
1196 dec(&(oap->end));
1197
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001198 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001200 int done = FALSE;
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 n = gchar_cursor();
1203 if (n != NUL)
1204 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001205 int new_byte_len = (*mb_char2len)(c);
1206 int old_byte_len = mb_ptr2len(ml_get_cursor());
1207
1208 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 // This is slow, but it handles replacing a single-byte
1211 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001212 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001213 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001214 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001215 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (n == TAB)
1220 {
1221 int end_vcol = 0;
1222
1223 if (curwin->w_cursor.lnum == oap->end.lnum)
1224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001225 // oap->end has to be recalculated when
1226 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 end_vcol = getviscol2(oap->end.col,
1228 oap->end.coladd);
1229 }
1230 coladvance_force(getviscol());
1231 if (curwin->w_cursor.lnum == oap->end.lnum)
1232 getvpos(&oap->end, end_vcol);
1233 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001234 // with "coladd" set may move to just after a TAB
1235 if (gchar_cursor() != NUL)
1236 {
1237 PBYTE(curwin->w_cursor, c);
1238 done = TRUE;
1239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001242 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 int virtcols = oap->end.coladd;
1245
1246 if (curwin->w_cursor.lnum == oap->start.lnum
1247 && oap->start.col == oap->end.col && oap->start.coladd)
1248 virtcols -= oap->start.coladd;
1249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 // oap->end has been trimmed so it's effectively inclusive;
1251 // as a result an extra +1 must be counted so we don't
1252 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1254 curwin->w_cursor.col -= (virtcols + 1);
1255 for (; virtcols >= 0; virtcols--)
1256 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001257 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001258 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001259 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001260 PBYTE(curwin->w_cursor, c);
1261 if (inc(&curwin->w_cursor) == -1)
1262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001266 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (inc_cursor() == -1)
1268 break;
1269 }
1270 }
1271
1272 curwin->w_cursor = oap->start;
1273 check_cursor();
1274 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1275
Bram Moolenaare1004402020-10-24 20:49:43 +02001276 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001277 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001278 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001279 curbuf->b_op_start = oap->start;
1280 curbuf->b_op_end = oap->end;
1281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
1283 return OK;
1284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001286static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288/*
1289 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1290 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001296 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298 if (u_save((linenr_T)(oap->start.lnum - 1),
1299 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1300 return;
1301
1302 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001303 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
1305 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1306 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001307 int one_change;
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 block_prep(oap, &bd, pos.lnum, FALSE);
1310 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001311 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1312 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001313
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001314#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001315 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001317 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
Bram Moolenaar009b2592004-10-24 19:18:58 +00001319 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1320 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001321 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001322 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001324 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 if (did_change)
1329 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1330 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
1333 if (oap->motion_type == MLINE)
1334 {
1335 oap->start.col = 0;
1336 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01001337 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (oap->end.col)
1339 --oap->end.col;
1340 }
1341 else if (!oap->inclusive)
1342 dec(&(oap->end));
1343
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001344 if (pos.lnum == oap->end.lnum)
1345 did_change = swapchars(oap->op_type, &pos,
1346 oap->end.col - pos.col + 1);
1347 else
1348 for (;;)
1349 {
1350 did_change |= swapchars(oap->op_type, &pos,
zeertzjq94b7c322024-03-12 21:50:32 +01001351 pos.lnum == oap->end.lnum ? oap->end.col + 1
1352 : ml_get_pos_len(&pos));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001353 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001354 break;
1355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (did_change)
1357 {
1358 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1359 0L);
1360#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001361 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
1363 char_u *ptr;
1364 int count;
1365
1366 pos = oap->start;
1367 while (pos.lnum < oap->end.lnum)
1368 {
1369 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01001370 count = ml_get_buf_len(curbuf, pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001371 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001372 // get the line again, it may have been flushed
1373 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001375 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 pos.col = 0;
1377 pos.lnum++;
1378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001380 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001381 // get the line again, it may have been flushed
1382 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001384 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386#endif
1387 }
1388 }
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001391 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001392 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaare1004402020-10-24 20:49:43 +02001394 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001395 {
1396 // Set '[ and '] marks.
1397 curbuf->b_op_start = oap->start;
1398 curbuf->b_op_end = oap->end;
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
1401 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001403 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404}
1405
1406/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001407 * Invoke swapchar() on "length" bytes at position "pos".
1408 * "pos" is advanced to just after the changed characters.
1409 * "length" is rounded up to include the whole last multi-byte character.
1410 * Also works correctly when the number of bytes changes.
1411 * Returns TRUE if some character was changed.
1412 */
1413 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001414swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001415{
1416 int todo;
1417 int did_change = 0;
1418
1419 for (todo = length; todo > 0; --todo)
1420 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001421 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001422 {
1423 int len = (*mb_ptr2len)(ml_get_pos(pos));
1424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001426 if (len > 0)
1427 todo -= len - 1;
1428 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001429 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001430 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001431 break;
1432 }
1433 return did_change;
1434}
1435
1436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 * If op_type == OP_UPPER: make uppercase,
1438 * if op_type == OP_LOWER: make lowercase,
1439 * if op_type == OP_ROT13: do rot13 encoding,
1440 * else swap case of character at 'pos'
1441 * returns TRUE when something actually changed.
1442 */
1443 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001444swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 int c;
1447 int nc;
1448
1449 c = gchar_pos(pos);
1450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001451 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (c >= 0x80 && op_type == OP_ROT13)
1453 return FALSE;
1454
glepnirbd1232a2024-02-12 22:14:53 +01001455 // ~ is OP_NOP, g~ is OP_TILDE, gU is OP_UPPER
1456 if ((op_type == OP_UPPER || op_type == OP_NOP || op_type == OP_TILDE)
1457 && c == 0xdf
1458 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001459 {
1460 pos_T sp = curwin->w_cursor;
1461
glepnirbd1232a2024-02-12 22:14:53 +01001462 // Special handling for lowercase German sharp s (ß): convert to uppercase (ẞ).
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001463 curwin->w_cursor = *pos;
1464 del_char(FALSE);
glepnirbd1232a2024-02-12 22:14:53 +01001465 ins_char(0x1E9E);
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001466 curwin->w_cursor = sp;
glepnirbd1232a2024-02-12 22:14:53 +01001467 return TRUE;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001468 }
1469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001470 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 nc = c;
1473 if (MB_ISLOWER(c))
1474 {
1475 if (op_type == OP_ROT13)
1476 nc = ROT13(c, 'a');
1477 else if (op_type != OP_LOWER)
1478 nc = MB_TOUPPER(c);
1479 }
1480 else if (MB_ISUPPER(c))
1481 {
1482 if (op_type == OP_ROT13)
1483 nc = ROT13(c, 'A');
1484 else if (op_type != OP_UPPER)
1485 nc = MB_TOLOWER(c);
1486 }
1487 if (nc != c)
1488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1490 {
1491 pos_T sp = curwin->w_cursor;
1492
1493 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001494 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001495 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ins_char(nc);
1497 curwin->w_cursor = sp;
1498 }
1499 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001500 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 return TRUE;
1502 }
1503 return FALSE;
1504}
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506/*
1507 * op_insert - Insert and append operators for Visual mode.
1508 */
1509 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001510op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
zeertzjq8c55d602024-03-13 20:42:26 +01001512 long pre_textlen = 0;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001513 colnr_T ind_pre_col = 0, ind_post_col;
1514 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 struct block_def bd;
1516 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001517 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001518 pos_T start_insert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001520 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1522
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001523 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001525 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 if (oap->block_mode)
1528 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 // When 'virtualedit' is used, need to insert the extra spaces before
1530 // doing block_prep(). When only "block" is used, virtual edit is
1531 // already disabled, but still need it when calling
1532 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001533 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001534 // state for the current window. To override that state, we need to
1535 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (curwin->w_cursor.coladd > 0)
1537 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001538 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (u_save_cursor() == FAIL)
1541 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001542
Gary Johnson51ad8502021-08-03 18:33:08 +02001543 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 coladvance_force(oap->op_type == OP_APPEND
1545 ? oap->end_vcol + 1 : getviscol());
1546 if (oap->op_type == OP_APPEND)
1547 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001548 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001550 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001552 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001553 ind_pre_col = (colnr_T)getwhitecols_curline();
1554 ind_pre_vcol = get_indent();
zeertzjq94b7c322024-03-12 21:50:32 +01001555 pre_textlen = ml_get_len(oap->start.lnum) - bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (oap->op_type == OP_APPEND)
zeertzjq94b7c322024-03-12 21:50:32 +01001557 pre_textlen -= bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559
1560 if (oap->op_type == OP_APPEND)
1561 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001562 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001564 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 curwin->w_set_curswant = TRUE;
1566 while (*ml_get_cursor() != NUL
1567 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1568 ++curwin->w_cursor.col;
1569 if (bd.is_short && !bd.is_MAX)
1570 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001571 // First line was too short, make it longer and adjust the
1572 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (u_save_cursor() == FAIL)
1574 return;
1575 for (i = 0; i < bd.endspaces; ++i)
1576 ins_char(' ');
1577 bd.textlen += bd.endspaces;
1578 }
1579 }
1580 else
1581 {
1582 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001583 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001585 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001586 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 && oap->start_vcol != oap->end_vcol)
1588 inc_cursor();
1589 }
1590 }
1591
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001592 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001593 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001594 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // When a tab was inserted, and the characters in front of the tab
1597 // have been converted to a tab as well, the column of the cursor
1598 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001599 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001600 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001601 oap->start = curbuf->b_op_start_orig;
1602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001603 // If user has moved off this line, we don't know what to do, so do
1604 // nothing.
1605 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001606 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 return;
1608
1609 if (oap->block_mode)
1610 {
Christian Brabandtd5c8c092024-05-08 22:17:19 +02001611 int ins_len;
zeertzjq8c55d602024-03-13 20:42:26 +01001612 char_u *firstline, *ins_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001614 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001615 size_t len;
Mike Williamsaca8f552024-04-07 18:26:22 +02001616 size_t add;
zeertzjq8c55d602024-03-13 20:42:26 +01001617 // offset when cursor was moved in insert mode
1618 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001620 // If indent kicked in, the firstline might have changed
1621 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001622 ind_post_col = (colnr_T)getwhitecols_curline();
1623 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001624 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001625 bd.textcol += ind_post_col - ind_pre_col;
1626 ind_post_vcol = get_indent();
1627 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001628 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001629 }
1630
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001631 // The user may have moved the cursor before inserting something, try
1632 // to adjust the block for that. But only do it, if the difference
1633 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001634 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1635 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001636 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001637 int t = getviscol2(curbuf->b_op_start_orig.col,
1638 curbuf->b_op_start_orig.coladd);
1639
zeertzjq7745f142022-02-15 11:48:22 +00001640 if (oap->op_type == OP_INSERT
1641 && oap->start.col + oap->start.coladd
1642 != curbuf->b_op_start_orig.col
1643 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001644 {
zeertzjq7745f142022-02-15 11:48:22 +00001645 oap->start.col = curbuf->b_op_start_orig.col;
1646 pre_textlen -= t - oap->start_vcol;
1647 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001648 }
zeertzjq7745f142022-02-15 11:48:22 +00001649 else if (oap->op_type == OP_APPEND
1650 && oap->start.col + oap->start.coladd
1651 >= curbuf->b_op_start_orig.col
1652 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001653 {
zeertzjq7745f142022-02-15 11:48:22 +00001654 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001655 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001656 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001657 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001658 oap->start_vcol = t;
1659 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001660 }
1661 }
1662
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001663 // Spaces and tabs in the indent may have changed to other spaces and
1664 // tabs. Get the starting column again and correct the length.
1665 // Don't do this when "$" used, end-of-line will have changed.
1666 //
1667 // if indent was added and the inserted text was after the indent,
1668 // correct the selection for the new indent.
1669 if (did_indent && bd.textcol - ind_post_col > 0)
1670 {
1671 oap->start.col += ind_post_col - ind_pre_col;
1672 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1673 oap->end.col += ind_post_col - ind_pre_col;
1674 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001677 if (did_indent && bd.textcol - ind_post_col > 0)
1678 {
1679 // undo for where "oap" is used below
1680 oap->start.col -= ind_post_col - ind_pre_col;
1681 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1682 oap->end.col -= ind_post_col - ind_pre_col;
1683 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1686 {
1687 if (oap->op_type == OP_APPEND)
1688 {
1689 pre_textlen += bd2.textlen - bd.textlen;
1690 if (bd2.endspaces)
1691 --bd2.textlen;
1692 }
1693 bd.textcol = bd2.textcol;
1694 bd.textlen = bd2.textlen;
1695 }
1696
1697 /*
1698 * Subsequent calls to ml_get() flush the firstline data - take a
1699 * copy of the required string.
1700 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001701 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001702 len = ml_get_len(oap->start.lnum);
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001703 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001705 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001706 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001707 // account for pressing cursor in insert mode when '$' was used
1708 if (bd.is_MAX
1709 && (start_insert.lnum == Insstart.lnum
1710 && start_insert.col > Insstart.col))
1711 {
1712 offset = (start_insert.col - Insstart.col);
1713 add -= offset;
1714 if (oap->end_vcol > offset)
1715 oap->end_vcol -= (offset + 1);
1716 else
1717 // moved outside of the visual block, what to do?
1718 return;
1719 }
1720 }
Mike Williamsaca8f552024-04-07 18:26:22 +02001721 if (add > len)
zeertzjq94b7c322024-03-12 21:50:32 +01001722 add = len; // short line, point to the NUL
1723 firstline += add;
1724 len -= add;
1725 if (pre_textlen >= 0 && (ins_len = len - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001727 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (ins_text != NULL)
1729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (u_save(oap->start.lnum,
1732 (linenr_T)(oap->end.lnum + 1)) == OK)
John Marriott38b9f452024-04-25 21:39:18 +02001733 block_insert(oap, ins_text, ins_len, (oap->op_type == OP_INSERT),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 &bd);
1735
1736 curwin->w_cursor.col = oap->start.col;
1737 check_cursor();
1738 vim_free(ins_text);
1739 }
1740 }
1741 }
1742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744/*
1745 * op_change - handle a change operation
1746 *
1747 * return TRUE if edit() returns because of a CTRL-O command
1748 */
1749 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 colnr_T l;
1753 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001755 long pre_textlen = 0;
1756 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 char_u *firstline;
1758 char_u *ins_text, *newp, *oldp;
1759 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761 l = oap->start.col;
1762 if (oap->motion_type == MLINE)
1763 {
1764 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001765 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001768 // First delete the text in the region. In an empty buffer only need to
1769 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1771 {
1772 if (u_save_cursor() == FAIL)
1773 return FALSE;
1774 }
1775 else if (op_delete(oap) == FAIL)
1776 return FALSE;
1777
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001778 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 && !virtual_op)
1780 inc_cursor();
1781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001782 // check for still on same line (<CR> in inserted text meaningless)
1783 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (oap->block_mode)
1785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001786 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (virtual_op && (curwin->w_cursor.coladd > 0
1788 || gchar_cursor() == NUL))
1789 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001790 firstline = ml_get(oap->start.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01001791 pre_textlen = ml_get_len(oap->start.lnum);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001792 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 bd.textcol = curwin->w_cursor.col;
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (oap->motion_type == MLINE)
1797 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
zeertzjqcdeb6572022-11-15 13:46:12 +00001799 // Reset finish_op now, don't want it set inside edit().
1800 int save_finish_op = finish_op;
1801 finish_op = FALSE;
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 retval = edit(NUL, FALSE, (linenr_T)1);
1804
zeertzjqcdeb6572022-11-15 13:46:12 +00001805 finish_op = save_finish_op;
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001808 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001810 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001812 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
John Marriott38b9f452024-04-25 21:39:18 +02001814 size_t ins_len;
1815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001816 // Auto-indenting may have changed the indent. If the cursor was past
1817 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001819 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001821 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001822
1823 pre_textlen += new_indent - pre_indent;
1824 bd.textcol += new_indent - pre_indent;
1825 }
1826
zeertzjq94b7c322024-03-12 21:50:32 +01001827 ins_len = ml_get_len(oap->start.lnum) - pre_textlen;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001828 if (ins_len > 0)
1829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 // Subsequent calls to ml_get() flush the firstline data - take a
1831 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001832 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
John Marriott38b9f452024-04-25 21:39:18 +02001834 vim_strncpy(ins_text, firstline + bd.textcol, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1836 linenr++)
1837 {
1838 block_prep(oap, &bd, linenr, TRUE);
1839 if (!bd.is_short || virtual_op)
1840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 pos_T vpos;
John Marriott38b9f452024-04-25 21:39:18 +02001842 size_t newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001844 // If the block starts in virtual space, count the
1845 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (bd.is_short)
1847 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001848 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851 else
1852 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 oldp = ml_get(linenr);
John Marriott38b9f452024-04-25 21:39:18 +02001854 newp = alloc(ml_get_len(linenr) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (newp == NULL)
1856 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001857 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 mch_memmove(newp, oldp, (size_t)bd.textcol);
John Marriott38b9f452024-04-25 21:39:18 +02001859 newlen = bd.textcol;
1860 vim_memset(newp + newlen, ' ', (size_t)vpos.coladd);
1861 newlen += vpos.coladd;
1862 mch_memmove(newp + newlen, ins_text, ins_len);
1863 STRCPY(newp + newlen + ins_len, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001865#ifdef FEAT_PROP_POPUP
1866 // Shift the properties for linenr as edit() would do.
1867 if (curbuf->b_has_textprop)
1868 adjust_prop_columns(linenr, bd.textcol,
1869 vpos.coladd + ins_len, 0);
1870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 }
1873 check_cursor();
1874
1875 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1876 }
1877 vim_free(ins_text);
1878 }
1879 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001880 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
1882 return retval;
1883}
1884
1885/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001886 * When the cursor is on the NUL past the end of the line and it should not be
1887 * there move it left.
1888 */
1889 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001890adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001891{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001892 unsigned int cur_ve_flags = get_ve_flags();
1893
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001894 int adj_cursor = (curwin->w_cursor.col > 0
1895 && gchar_cursor() == NUL
1896 && (cur_ve_flags & VE_ONEMORE) == 0
1897 && !(restart_edit || (State & MODE_INSERT)));
1898 if (!adj_cursor)
1899 return;
1900
1901 // Put the cursor on the last character in the line.
1902 dec_cursor();
1903
1904 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001906 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001907
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001908 // Coladd is set to the width of the last character.
1909 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1910 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912}
1913
Bram Moolenaar81340392012-06-06 16:12:59 +02001914/*
1915 * If "process" is TRUE and the line begins with a comment leader (possibly
1916 * after some white space), return a pointer to the text after it. Put a boolean
1917 * value indicating whether the line ends with an unclosed comment in
1918 * "is_comment".
1919 * line - line to be processed,
1920 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001921 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001922 * include_space - whether to also skip space following the comment leader,
1923 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001924 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001926 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001927skip_comment(
1928 char_u *line,
1929 int process,
1930 int include_space,
1931 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001932{
1933 char_u *comment_flags = NULL;
1934 int lead_len;
1935 int leader_offset = get_last_leader_offset(line, &comment_flags);
1936
1937 *is_comment = FALSE;
1938 if (leader_offset != -1)
1939 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001940 // Let's check whether the line ends with an unclosed comment.
1941 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001942 while (*comment_flags)
1943 {
1944 if (*comment_flags == COM_END
1945 || *comment_flags == ':')
1946 break;
1947 ++comment_flags;
1948 }
1949 if (*comment_flags != COM_END)
1950 *is_comment = TRUE;
1951 }
1952
1953 if (process == FALSE)
1954 return line;
1955
1956 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1957
1958 if (lead_len == 0)
1959 return line;
1960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 // Find:
1962 // - COM_END,
1963 // - colon,
1964 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001965 while (*comment_flags)
1966 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001967 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001968 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001969 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001970 ++comment_flags;
1971 }
1972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001973 // If we found a colon, it means that we are not processing a line
1974 // starting with a closing part of a three-part comment. That's good,
1975 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001976 if (*comment_flags == ':' || *comment_flags == NUL)
1977 line += lead_len;
1978
1979 return line;
1980}
Bram Moolenaar81340392012-06-06 16:12:59 +02001981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001983 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001984 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001985 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1986 * leaders should not be removed.
1987 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1988 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001990 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 */
1992 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001993do_join(
1994 long count,
1995 int insert_space,
1996 int save_undo,
1997 int use_formatoptions UNUSED,
1998 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002000 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002001 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002002 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002004 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002005 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02002006 int endcurr1 = NUL;
2007 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002008 int currsize = 0; // size of the current line
2009 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002011 colnr_T col = 0;
2012 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02002013 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002014 int remove_comments = (use_formatoptions == TRUE)
2015 && has_format_option(FO_REMOVE_COMS);
2016 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002017#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002018 int propcount = 0; // number of props over all joined lines
2019 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002022 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2023 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2024 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002026 // Allocate an array to store the number of spaces inserted before each
2027 // line. We will use it to pre-compute the length of the new line and the
2028 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002029 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002030 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002032 if (remove_comments)
2033 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002034 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002035 if (comments == NULL)
2036 {
2037 vim_free(spaces);
2038 return FAIL;
2039 }
2040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002043 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002045 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002047 for (t = 0; t < count; ++t)
2048 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002049 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002050#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002051 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2052 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002053#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002054 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002055 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002056 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002057 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2058 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2059 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002060 if (remove_comments)
2061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002062 // We don't want to remove the comment leader if the
2063 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002064 if (t > 0 && prev_was_comment)
2065 {
2066
2067 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2068 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002069 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002070 curr = new_curr;
2071 }
2072 else
2073 curr = skip_comment(curr, FALSE, insert_space,
2074 &prev_was_comment);
2075 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002076
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002077 if (insert_space && t > 0)
2078 {
2079 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002080 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002081 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002082 && (!has_format_option(FO_MBYTE_JOIN)
2083 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2084 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002085 || (mb_ptr2char(curr) < 0x100
2086 && !(enc_utf8 && utf_eat_space(endcurr1)))
2087 || (endcurr1 < 0x100
2088 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002089 )
2090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002091 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002092 if (endcurr1 == ' ')
2093 endcurr1 = endcurr2;
2094 else
2095 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002096 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002097 if ( p_js
2098 && (endcurr1 == '.'
2099 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2100 && (endcurr1 == '?' || endcurr1 == '!'))))
2101 ++spaces[t];
2102 }
2103 }
2104 currsize = (int)STRLEN(curr);
2105 sumsize += currsize + spaces[t];
2106 endcurr1 = endcurr2 = NUL;
2107 if (insert_space && currsize > 0)
2108 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002109 if (has_mbyte)
2110 {
2111 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002112 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002113 endcurr1 = (*mb_ptr2char)(cend);
2114 if (cend > curr)
2115 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002116 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002117 endcurr2 = (*mb_ptr2char)(cend);
2118 }
2119 }
2120 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002121 {
2122 endcurr1 = *(curr + currsize - 1);
2123 if (currsize > 1)
2124 endcurr2 = *(curr + currsize - 2);
2125 }
2126 }
2127 line_breakcheck();
2128 if (got_int)
2129 {
2130 ret = FAIL;
2131 goto theend;
2132 }
2133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002136 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002138 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002139 newp_len = sumsize + 1;
2140#ifdef FEAT_PROP_POPUP
2141 newp_len += propcount * sizeof(textprop_T);
2142#endif
2143 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002144 if (newp == NULL)
2145 {
2146 ret = FAIL;
2147 goto theend;
2148 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002149 cend = newp + sumsize;
2150 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002152 /*
2153 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002154 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002155 *
2156 * Move marks from each deleted line to the joined line, adjusting the
2157 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2158 * should not really be a problem.
2159 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002160#ifdef FEAT_PROP_POPUP
2161 props_remaining = propcount;
2162#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002163 for (t = count - 1; ; --t)
2164 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002165 int spaces_removed;
2166
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002167 cend -= currsize;
2168 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002169
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 if (spaces[t] > 0)
2171 {
2172 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002173 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002174 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002175
2176 // If deleting more spaces than adding, the cursor moves no more than
2177 // what is added if it is inside these spaces.
2178 spaces_removed = (curr - curr_start) - spaces[t];
2179
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002180 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002181 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002182#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002183 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2184 curwin->w_cursor.lnum + t, t == count - 1,
2185 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002186#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002187 if (t == 0)
2188 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002189 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002190 if (remove_comments)
2191 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002192 if (insert_space && t > 1)
2193 curr = skipwhite(curr);
2194 currsize = (int)STRLEN(curr);
2195 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002196
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002197 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
Bram Moolenaare1004402020-10-24 20:49:43 +02002199 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002202 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002203 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002204 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 // Only report the change in the first line here, del_lines() will report
2207 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 changed_lines(curwin->w_cursor.lnum, currsize,
2209 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002211 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * briefly, and then move it back. After del_lines() the cursor may
2213 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 */
2215 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002217 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 curwin->w_cursor.lnum = t;
2219
2220 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002221 * Set the cursor column:
2222 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002223 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002225 curwin->w_cursor.col =
2226 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 curwin->w_set_curswant = TRUE;
2231
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002232theend:
2233 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002234 if (remove_comments)
2235 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002236 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237}
2238
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002239#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002241 * Reset 'linebreak' and take care of side effects.
2242 * Returns the previous value, to be passed to restore_lbr().
2243 */
2244 static int
2245reset_lbr(void)
2246{
2247 if (!curwin->w_p_lbr)
2248 return FALSE;
2249 // changing 'linebreak' may require w_virtcol to be updated
2250 curwin->w_p_lbr = FALSE;
2251 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2252 return TRUE;
2253}
2254
2255/*
2256 * Restore 'linebreak' and take care of side effects.
2257 */
2258 static void
2259restore_lbr(int lbr_saved)
2260{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002261 if (curwin->w_p_lbr || !lbr_saved)
2262 return;
2263
2264 // changing 'linebreak' may require w_virtcol to be updated
2265 curwin->w_p_lbr = TRUE;
2266 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002267}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002268#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002269
2270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * prepare a few things for block mode yank/delete/tilde
2272 *
2273 * for delete:
2274 * - textlen includes the first/last char to be (partly) deleted
2275 * - start/endspaces is the number of columns that are taken by the
2276 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002277 * deleted.
2278 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 * - textlen includes the first/last char to be wholly yanked
2280 * - start/endspaces is the number of columns of the first/last yanked char
2281 * that are to be yanked.
2282 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002283 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284block_prep(
2285 oparg_T *oap,
2286 struct block_def *bdp,
2287 linenr_T lnum,
2288 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 int incr = 0;
2291 char_u *pend;
2292 char_u *pstart;
2293 char_u *line;
2294 char_u *prev_pstart;
2295 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002296 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002297#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002298 // Avoid a problem with unwanted linebreaks in block mode.
2299 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002300#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 bdp->startspaces = 0;
2303 bdp->endspaces = 0;
2304 bdp->textlen = 0;
2305 bdp->start_vcol = 0;
2306 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 bdp->is_short = FALSE;
2308 bdp->is_oneChar = FALSE;
2309 bdp->pre_whitesp = 0;
2310 bdp->pre_whitesp_c = 0;
2311 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 bdp->start_char_vcols = 0;
2313
2314 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002316 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2317 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002319 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002320 incr = lbr_chartabsize(&cts);
2321 cts.cts_vcol += incr;
2322 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 bdp->pre_whitesp += incr;
2325 bdp->pre_whitesp_c++;
2326 }
2327 else
2328 {
2329 bdp->pre_whitesp = 0;
2330 bdp->pre_whitesp_c = 0;
2331 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002332 prev_pstart = cts.cts_ptr;
2333 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002335 bdp->start_vcol = cts.cts_vcol;
2336 pstart = cts.cts_ptr;
2337 clear_chartabsize_arg(&cts);
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (!is_del || oap->op_type == OP_APPEND)
2345 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2346 }
2347 else
2348 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 // notice: this converts partly selected Multibyte characters to
2350 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2352 if (is_del && bdp->startspaces)
2353 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2354 pend = pstart;
2355 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002356 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (oap->op_type == OP_INSERT)
2360 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2361 else if (oap->op_type == OP_APPEND)
2362 {
2363 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2364 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2365 }
2366 else
2367 {
2368 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2369 if (is_del && oap->op_type != OP_LSHIFT)
2370 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 // just putting the sum of those two into
2372 // bdp->startspaces doesn't work for Visual replace,
2373 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 bdp->startspaces = bdp->start_char_vcols
2375 - (bdp->start_vcol - oap->start_vcol);
2376 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2377 }
2378 }
2379 }
2380 else
2381 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002382 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2383 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002385 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002387 // count a tab for what it's worth (if list mode not on)
2388 prev_pend = cts.cts_ptr;
2389 incr = lbr_chartabsize_adv(&cts);
2390 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002392 bdp->end_vcol = cts.cts_vcol;
2393 pend = cts.cts_ptr;
2394 clear_chartabsize_arg(&cts);
2395
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (bdp->end_vcol <= oap->end_vcol
2397 && (!is_del
2398 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002399 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 // Alternative: include spaces to fill up the block.
2403 // Disadvantage: can lead to trailing spaces when the line is
2404 // short where the text is put
2405 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (oap->op_type == OP_APPEND || virtual_op)
2407 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002408 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002410 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412 else if (bdp->end_vcol > oap->end_vcol)
2413 {
2414 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2415 if (!is_del && bdp->endspaces)
2416 {
2417 bdp->endspaces = incr - bdp->endspaces;
2418 if (pend != pstart)
2419 pend = prev_pend;
2420 }
2421 }
2422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (is_del && bdp->startspaces)
2425 pstart = prev_pstart;
2426 bdp->textlen = (int)(pend - pstart);
2427 }
2428 bdp->textcol = (colnr_T) (pstart - line);
2429 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002430#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002431 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435/*
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002436 * Get block text from "start" to "end"
2437 */
2438 void
2439charwise_block_prep(
2440 pos_T start,
2441 pos_T end,
2442 struct block_def *bdp,
2443 linenr_T lnum,
2444 int inclusive)
2445{
2446 colnr_T startcol = 0, endcol = MAXCOL;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002447 colnr_T cs, ce;
2448 char_u *p;
2449
2450 p = ml_get(lnum);
2451 bdp->startspaces = 0;
2452 bdp->endspaces = 0;
zeertzjq52a6f342024-05-22 16:42:44 +02002453 bdp->is_oneChar = FALSE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002454 bdp->start_char_vcols = 0;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002455
2456 if (lnum == start.lnum)
2457 {
2458 startcol = start.col;
2459 if (virtual_op)
2460 {
2461 getvcol(curwin, &start, &cs, NULL, &ce);
2462 if (ce != cs && start.coladd > 0)
2463 {
2464 // Part of a tab selected -- but don't
2465 // double-count it.
zeertzjqc95e64f2024-05-20 14:00:31 +02002466 bdp->start_char_vcols = ce - cs + 1;
2467 bdp->startspaces = bdp->start_char_vcols - start.coladd;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002468 if (bdp->startspaces < 0)
2469 bdp->startspaces = 0;
2470 startcol++;
2471 }
2472 }
2473 }
2474
2475 if (lnum == end.lnum)
2476 {
2477 endcol = end.col;
2478 if (virtual_op)
2479 {
2480 getvcol(curwin, &end, &cs, NULL, &ce);
2481 if (p[endcol] == NUL || (cs + end.coladd < ce
2482 // Don't add space for double-wide
2483 // char; endcol will be on last byte
2484 // of multi-byte char.
2485 && (*mb_head_off)(p, p + endcol) == 0))
2486 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002487 if (start.lnum == end.lnum && start.col == end.col)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002488 {
2489 // Special case: inside a single char
zeertzjq52a6f342024-05-22 16:42:44 +02002490 bdp->is_oneChar = TRUE;
zeertzjqc95e64f2024-05-20 14:00:31 +02002491 bdp->startspaces = end.coladd - start.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002492 endcol = startcol;
2493 }
2494 else
2495 {
zeertzjqc95e64f2024-05-20 14:00:31 +02002496 bdp->endspaces = end.coladd + inclusive;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002497 endcol -= inclusive;
2498 }
2499 }
2500 }
2501 }
2502 if (endcol == MAXCOL)
zeertzjq94b7c322024-03-12 21:50:32 +01002503 endcol = ml_get_len(lnum);
zeertzjq52a6f342024-05-22 16:42:44 +02002504 if (startcol > endcol || bdp->is_oneChar)
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002505 bdp->textlen = 0;
2506 else
2507 bdp->textlen = endcol - startcol + inclusive;
zeertzjqc95e64f2024-05-20 14:00:31 +02002508 bdp->textcol = startcol;
Shougo Matsushita3f905ab2024-02-21 00:02:45 +01002509 bdp->textstart = p + startcol;
2510}
2511
2512/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002513 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002515 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002516op_addsub(
2517 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002518 linenr_T Prenum1, // Amount of add/subtract
2519 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002521 pos_T pos;
2522 struct block_def bd;
2523 int change_cnt = 0;
2524 linenr_T amount = Prenum1;
2525
Bram Moolenaar6b731882018-11-16 20:54:47 +01002526 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002527 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002528 // the call to changed_lines().
2529#ifdef FEAT_FOLDING
2530 disable_fold_update++;
2531#endif
2532
Bram Moolenaard79e5502016-01-10 22:13:02 +01002533 if (!VIsual_active)
2534 {
2535 pos = curwin->w_cursor;
2536 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002537 {
2538#ifdef FEAT_FOLDING
2539 disable_fold_update--;
2540#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002541 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002542 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002543 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002544#ifdef FEAT_FOLDING
2545 disable_fold_update--;
2546#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002547 if (change_cnt)
2548 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2549 }
2550 else
2551 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002552 int one_change;
2553 int length;
2554 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002555
2556 if (u_save((linenr_T)(oap->start.lnum - 1),
2557 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002558 {
2559#ifdef FEAT_FOLDING
2560 disable_fold_update--;
2561#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002562 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002563 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002564
2565 pos = oap->start;
2566 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2567 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002568 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002569 {
2570 block_prep(oap, &bd, pos.lnum, FALSE);
2571 pos.col = bd.textcol;
2572 length = bd.textlen;
2573 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002574 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002575 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002576 curwin->w_cursor.col = 0;
2577 pos.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01002578 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002579 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002580 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002581 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002582 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002583 dec(&(oap->end));
zeertzjq94b7c322024-03-12 21:50:32 +01002584 length = ml_get_len(pos.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002585 pos.col = 0;
2586 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002587 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002588 pos.col += oap->start.col;
2589 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002590 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002591 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002592 {
zeertzjq94b7c322024-03-12 21:50:32 +01002593 length = ml_get_len(oap->end.lnum);
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002594 if (oap->end.col >= length)
2595 oap->end.col = length - 1;
2596 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002597 }
2598 }
2599 one_change = do_addsub(oap->op_type, &pos, length, amount);
2600 if (one_change)
2601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002602 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002603 if (change_cnt == 0)
2604 startpos = curbuf->b_op_start;
2605 ++change_cnt;
2606 }
2607
2608#ifdef FEAT_NETBEANS_INTG
2609 if (netbeans_active() && one_change)
2610 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002611 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002612
2613 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002614 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002615 netbeans_inserted(curbuf, pos.lnum, pos.col,
2616 &ptr[pos.col], length);
2617 }
2618#endif
2619 if (g_cmd && one_change)
2620 amount += Prenum1;
2621 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002622
2623#ifdef FEAT_FOLDING
2624 disable_fold_update--;
2625#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002626 if (change_cnt)
2627 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2628
2629 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002630 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002631 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002632
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002633 // Set '[ mark if something changed. Keep the last end
2634 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002635 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002636 curbuf->b_op_start = startpos;
2637
2638 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002639 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002640 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002641 }
2642}
2643
2644/*
2645 * Add or subtract 'Prenum1' from a number in a line
2646 * op_type is OP_NR_ADD or OP_NR_SUB
2647 *
2648 * Returns TRUE if some character was changed.
2649 */
2650 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002651do_addsub(
2652 int op_type,
2653 pos_T *pos,
2654 int length,
2655 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002656{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 int col;
2658 char_u *buf1;
2659 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002660 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2661 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002662 uvarnumber_T n;
2663 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 char_u *ptr;
zeertzjq8c55d602024-03-13 20:42:26 +01002665 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002668 int do_hex;
2669 int do_oct;
2670 int do_bin;
2671 int do_alpha;
2672 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002675 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002676 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002677 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002678 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002679 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002680 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002681 pos_T startpos;
2682 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002683 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002685 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2686 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2687 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2688 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2689 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002691 if (virtual_active())
2692 {
2693 save_coladd = pos->coladd;
2694 pos->coladd = 0;
2695 }
2696
Bram Moolenaard79e5502016-01-10 22:13:02 +01002697 curwin->w_cursor = *pos;
2698 ptr = ml_get(pos->lnum);
zeertzjq8c55d602024-03-13 20:42:26 +01002699 linelen = ml_get_len(pos->lnum);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002700 col = pos->col;
2701
zeertzjq8c55d602024-03-13 20:42:26 +01002702 if (col + !!save_coladd >= linelen)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002703 goto theend;
2704
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /*
2706 * First check if we are on a hexadecimal number, after the "0x".
2707 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002708 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002710 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002711 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002712 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002713 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002714 if (has_mbyte)
2715 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002716 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002717
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002718 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002719 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002720 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002721 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002722 if (has_mbyte)
2723 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002724 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002725
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002726 if ( do_bin
2727 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002728 && ! ((col > 0
2729 && (ptr[col] == 'X'
2730 || ptr[col] == 'x')
2731 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002732 && (!has_mbyte ||
2733 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002734 && vim_isxdigit(ptr[col + 1]))))
2735 {
2736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002738
Bram Moolenaard79e5502016-01-10 22:13:02 +01002739 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002740
2741 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002742 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002743 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002744 if (has_mbyte)
2745 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002746 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002747 }
2748
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002749 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002750 && col > 0
2751 && (ptr[col] == 'X'
2752 || ptr[col] == 'x')
2753 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002754 && (!has_mbyte ||
2755 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002756 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002757 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002758 && col > 0
2759 && (ptr[col] == 'B'
2760 || ptr[col] == 'b')
2761 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002762 && (!has_mbyte ||
2763 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002764 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002767 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002768 if (has_mbyte)
2769 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002770 }
2771 else
2772 {
2773 /*
2774 * Search forward and then backward to find the start of number.
2775 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002776 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002777
2778 while (ptr[col] != NUL
2779 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002780 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002781 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002782
2783 while (col > 0
2784 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002785 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002786 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002787 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002788 if (has_mbyte)
2789 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002790 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002791 }
2792 }
2793
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002794 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002795 {
2796 while (ptr[col] != NUL && length > 0
2797 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002798 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002799 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002800 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002801
2802 col += mb_len;
2803 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002804 }
2805
2806 if (length == 0)
2807 goto theend;
2808
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002809 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002810 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2811 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002812 {
2813 negative = TRUE;
2814 was_positive = FALSE;
2815 }
2816 }
2817
2818 /*
2819 * If a number was found, and saving for undo works, replace the number.
2820 */
2821 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002822 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002823 {
2824 beep_flush();
2825 goto theend;
2826 }
2827
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002828 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002830 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002831 if (op_type == OP_NR_SUB)
2832 {
2833 if (CharOrd(firstdigit) < Prenum1)
2834 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002835 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002836 firstdigit = 'A';
2837 else
2838 firstdigit = 'a';
2839 }
2840 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002841 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002842 }
2843 else
2844 {
2845 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2846 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002847 if (SAFE_isupper(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002848 firstdigit = 'Z';
2849 else
2850 firstdigit = 'z';
2851 }
2852 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002853 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002854 }
2855 curwin->w_cursor.col = col;
2856 if (!did_change)
2857 startpos = curwin->w_cursor;
2858 did_change = TRUE;
2859 (void)del_char(FALSE);
2860 ins_char(firstdigit);
2861 endpos = curwin->w_cursor;
2862 curwin->w_cursor.col = col;
2863 }
2864 else
2865 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002866 pos_T save_pos;
2867 int i;
2868
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002869 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002870 && (!has_mbyte ||
2871 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002872 && !visual
2873 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002876 --col;
2877 negative = TRUE;
2878 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002880 if (visual && VIsual_mode != 'V')
2881 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
zeertzjq8c55d602024-03-13 20:42:26 +01002882 ? linelen - col : length);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002883
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002884 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002885 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002886 0 + (do_bin ? STR2NR_BIN : 0)
2887 + (do_oct ? STR2NR_OCT : 0)
2888 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002889 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002892 if (pre && negative)
2893 {
2894 ++col;
2895 --length;
2896 negative = FALSE;
2897 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002899 subtract = FALSE;
2900 if (op_type == OP_NR_SUB)
2901 subtract ^= TRUE;
2902 if (negative)
2903 subtract ^= TRUE;
2904
2905 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002906 if (!overflow) // if number is too big don't add/subtract
2907 {
2908 if (subtract)
2909 n -= (uvarnumber_T)Prenum1;
2910 else
2911 n += (uvarnumber_T)Prenum1;
2912 }
2913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002915 if (!pre)
2916 {
2917 if (subtract)
2918 {
2919 if (n > oldn)
2920 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002921 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002922 negative ^= TRUE;
2923 }
2924 }
2925 else
2926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002928 if (n < oldn)
2929 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002930 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002931 negative ^= TRUE;
2932 }
2933 }
2934 if (n == 0)
2935 negative = FALSE;
2936 }
2937
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002938 if (do_unsigned && negative)
2939 {
2940 if (subtract)
2941 // sticking at zero.
2942 n = (uvarnumber_T)0;
2943 else
2944 // sticking at 2^64 - 1.
2945 n = (uvarnumber_T)(-1);
2946 negative = FALSE;
2947 }
2948
Bram Moolenaard79e5502016-01-10 22:13:02 +01002949 if (visual && !was_positive && !negative && col > 0)
2950 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002951 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002952 col--;
2953 length++;
2954 }
2955
2956 /*
2957 * Delete the old number.
2958 */
2959 curwin->w_cursor.col = col;
2960 if (!did_change)
2961 startpos = curwin->w_cursor;
2962 did_change = TRUE;
2963 todel = length;
2964 c = gchar_cursor();
2965 /*
2966 * Don't include the '-' in the length, only the length of the
2967 * part after it is kept the same.
2968 */
2969 if (c == '-')
2970 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002971
2972 save_pos = curwin->w_cursor;
2973 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002974 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002975 if (c < 0x100 && SAFE_isalpha(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002976 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002977 if (SAFE_isupper(c))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002978 hexupper = TRUE;
2979 else
2980 hexupper = FALSE;
2981 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002982 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002983 c = gchar_cursor();
2984 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002985 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002986
2987 /*
2988 * Prepare the leading characters in buf1[].
2989 * When there are many leading zeros it could be very long.
2990 * Allocate a bit too much.
2991 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002992 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002993 if (buf1 == NULL)
2994 goto theend;
2995 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002996 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002997 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002998 if (pre)
2999 {
3000 *ptr++ = '0';
3001 --length;
3002 }
3003 if (pre == 'b' || pre == 'B' ||
3004 pre == 'x' || pre == 'X')
3005 {
3006 *ptr++ = pre;
3007 --length;
3008 }
3009
3010 /*
3011 * Put the number characters in buf2[].
3012 */
3013 if (pre == 'b' || pre == 'B')
3014 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01003015 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003016 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003019 for (bit = bits; bit > 0; bit--)
3020 if ((n >> (bit - 1)) & 0x1) break;
3021
Christian Brabandt889f6af2023-09-02 19:43:33 +02003022 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003023 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3024
3025 buf2[i] = '\0';
3026 }
3027 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003028 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003029 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003030 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003031 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003032 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003033 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003034 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003035 length -= (int)STRLEN(buf2);
3036
3037 /*
3038 * Adjust number of zeros to the new number of digits, so the
3039 * total length of the number remains the same.
3040 * Don't do this when
3041 * the result may look like an octal number.
3042 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02003043 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003044 while (length-- > 0)
3045 *ptr++ = '0';
3046 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003047
Bram Moolenaard79e5502016-01-10 22:13:02 +01003048 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003049
3050 // Insert just after the first character to be removed, so that any
3051 // text properties will be adjusted. Then delete the old number
3052 // afterwards.
3053 save_pos = curwin->w_cursor;
3054 if (todel > 0)
3055 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003057 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003058
3059 // del_char() will also mark line needing displaying
3060 if (todel > 0)
3061 {
zeertzjq94b7c322024-03-12 21:50:32 +01003062 int bytes_after = ml_get_curline_len() - curwin->w_cursor.col;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003063
3064 // Delete the one character before the insert.
3065 curwin->w_cursor = save_pos;
3066 (void)del_char(FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +01003067 curwin->w_cursor.col = ml_get_curline_len() - bytes_after;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02003068 --todel;
3069 }
3070 while (todel-- > 0)
3071 (void)del_char(FALSE);
3072
Bram Moolenaard79e5502016-01-10 22:13:02 +01003073 endpos = curwin->w_cursor;
3074 if (did_change && curwin->w_cursor.col)
3075 --curwin->w_cursor.col;
3076 }
3077
Bram Moolenaare1004402020-10-24 20:49:43 +02003078 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003079 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003081 curbuf->b_op_start = startpos;
3082 curbuf->b_op_end = endpos;
3083 if (curbuf->b_op_end.col > 0)
3084 --curbuf->b_op_end.col;
3085 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003086
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003087theend:
3088 if (visual)
3089 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003090 else if (did_change)
3091 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003092 else if (virtual_active())
3093 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003094
Bram Moolenaard79e5502016-01-10 22:13:02 +01003095 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096}
3097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003099clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003101 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102}
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003105 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 *
3107 * "Words" are counted by looking for boundaries between non-space and
3108 * space characters. (it seems to produce results that match 'wc'.)
3109 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003110 * Return value is byte count; word count for the line is added to "*wc".
3111 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 *
3113 * The function will only examine the first "limit" characters in the
3114 * line, stopping if it encounters an end-of-line (NUL byte). In that
3115 * case, eol_size will be added to the character count to account for
3116 * the size of the EOL character.
3117 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003118 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003119line_count_info(
3120 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003121 varnumber_T *wc,
3122 varnumber_T *cc,
3123 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003124 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003126 varnumber_T i;
3127 varnumber_T words = 0;
3128 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 int is_word = 0;
3130
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003131 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 if (is_word)
3134 {
3135 if (vim_isspace(line[i]))
3136 {
3137 words++;
3138 is_word = 0;
3139 }
3140 }
3141 else if (!vim_isspace(line[i]))
3142 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003143 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003144 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
3146
3147 if (is_word)
3148 words++;
3149 *wc += words;
3150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003151 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003152 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003155 chars += eol_size;
3156 }
3157 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return i;
3159}
3160
3161/*
3162 * Give some info about the position of the cursor (for "g CTRL-G").
3163 * In Visual mode, give some info about the selected region. (In this case,
3164 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003165 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 */
3167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003168cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003171 char_u buf1[50];
3172 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003174 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003175 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003176 varnumber_T byte_count_cursor = 0;
3177 varnumber_T char_count = 0;
3178 varnumber_T char_count_cursor = 0;
3179 varnumber_T word_count = 0;
3180 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003181 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003182 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 long line_count_selected = 0;
3184 pos_T min_pos, max_pos;
3185 oparg_T oparg;
3186 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 /*
3189 * Compute the length of the file in characters.
3190 */
3191 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3192 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003193 if (dict == NULL)
3194 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003195 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003196 return;
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 else
3200 {
3201 if (get_fileformat(curbuf) == EOL_DOS)
3202 eol_size = 2;
3203 else
3204 eol_size = 1;
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (VIsual_active)
3207 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003208 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
3210 min_pos = VIsual;
3211 max_pos = curwin->w_cursor;
3212 }
3213 else
3214 {
3215 min_pos = curwin->w_cursor;
3216 max_pos = VIsual;
3217 }
3218 if (*p_sel == 'e' && max_pos.col > 0)
3219 --max_pos.col;
3220
3221 if (VIsual_mode == Ctrl_V)
3222 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003223#ifdef FEAT_LINEBREAK
3224 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003225 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003227 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003228 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003229 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 oparg.is_VIsual = 1;
3232 oparg.block_mode = TRUE;
3233 oparg.op_type = OP_NOP;
3234 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003235 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003236#ifdef FEAT_LINEBREAK
3237 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003238 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003239#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003240 if (curwin->w_curswant == MAXCOL)
3241 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003242 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (oparg.end_vcol < oparg.start_vcol)
3244 {
3245 oparg.end_vcol += oparg.start_vcol;
3246 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3247 oparg.end_vcol -= oparg.start_vcol;
3248 }
3249 }
3250 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003255 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003256 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
3258 ui_breakcheck();
3259 if (got_int)
3260 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003261 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003264 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (VIsual_active
3266 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3267 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003268 char_u *s = NULL;
3269 long len = 0L;
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 switch (VIsual_mode)
3272 {
3273 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003277 s = bd.textstart;
3278 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 break;
3280 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003281 s = ml_get(lnum);
3282 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 break;
3284 case 'v':
3285 {
3286 colnr_T start_col = (lnum == min_pos.lnum)
3287 ? min_pos.col : 0;
3288 colnr_T end_col = (lnum == max_pos.lnum)
3289 ? max_pos.col - start_col + 1 : MAXCOL;
3290
Bram Moolenaardef9e822004-12-31 20:58:58 +00003291 s = ml_get(lnum) + start_col;
3292 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294 break;
3295 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003296 if (s != NULL)
3297 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003298 byte_count_cursor += line_count_info(s, &word_count_cursor,
3299 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003300 if (lnum == curbuf->b_ml.ml_line_count
3301 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003302 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003303 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003304 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003309 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (lnum == curwin->w_cursor.lnum)
3311 {
3312 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003313 char_count_cursor += char_count;
3314 byte_count_cursor = byte_count +
3315 line_count_info(ml_get(lnum),
3316 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003317 (varnumber_T)(curwin->w_cursor.col + 1),
3318 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003321 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003322 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003323 &char_count, (varnumber_T)MAXCOL,
3324 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003327 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003328 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003329 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
Bram Moolenaared767a22016-01-03 22:49:16 +01003331 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003333 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003335 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3336 {
3337 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3338 &max_pos.col);
3339 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3340 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3341 }
3342 else
3343 buf1[0] = NUL;
3344
3345 if (char_count_cursor == byte_count_cursor
3346 && char_count == byte_count)
3347 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003348 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003349 buf1, line_count_selected,
3350 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003351 word_count_cursor,
3352 word_count,
3353 byte_count_cursor,
3354 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003355 else
3356 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003357 _("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 +01003358 buf1, line_count_selected,
3359 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003360 word_count_cursor,
3361 word_count,
3362 char_count_cursor,
3363 char_count,
3364 byte_count_cursor,
3365 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003368 {
3369 p = ml_get_curline();
3370 validate_virtcol();
3371 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3372 (int)curwin->w_virtcol + 1);
zeertzjq94b7c322024-03-12 21:50:32 +01003373 col_print(buf2, sizeof(buf2), ml_get_curline_len(),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003374 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
Bram Moolenaared767a22016-01-03 22:49:16 +01003376 if (char_count_cursor == byte_count_cursor
3377 && char_count == byte_count)
3378 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003379 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003380 (char *)buf1, (char *)buf2,
3381 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003383 word_count_cursor, word_count,
3384 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003385 else
3386 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003387 _("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 +01003388 (char *)buf1, (char *)buf2,
3389 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003390 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003391 word_count_cursor, word_count,
3392 char_count_cursor, char_count,
3393 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003394 }
3395 }
3396
Bram Moolenaared767a22016-01-03 22:49:16 +01003397 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003398 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003399 {
3400 size_t len = STRLEN(IObuff);
3401
3402 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003403 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003404 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003405 if (dict == NULL)
3406 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003407 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003408 p = p_shm;
3409 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003410 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003411 p_shm = p;
3412 }
3413 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003414#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003415 if (dict != NULL)
3416 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003417 dict_add_number(dict, "words", word_count);
3418 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003419 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003420 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3421 byte_count_cursor);
3422 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3423 char_count_cursor);
3424 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3425 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003429
3430/*
3431 * Handle indent and format operators and visual mode ":".
3432 */
3433 static void
3434op_colon(oparg_T *oap)
3435{
3436 stuffcharReadbuff(':');
3437 if (oap->is_VIsual)
3438 stuffReadbuff((char_u *)"'<,'>");
3439 else
3440 {
3441 // Make the range look nice, so it can be repeated.
3442 if (oap->start.lnum == curwin->w_cursor.lnum)
3443 stuffcharReadbuff('.');
3444 else
3445 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003446
3447#ifdef FEAT_FOLDING
3448 // When using !! on a closed fold the range ".!" works best to operate
3449 // on, it will be made the whole closed fold later.
3450 linenr_T endOfStartFold = oap->start.lnum;
3451 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3452#endif
3453 if (oap->end.lnum != oap->start.lnum
3454#ifdef FEAT_FOLDING
3455 && oap->end.lnum != endOfStartFold
3456#endif
3457 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003458 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003459 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003460 stuffcharReadbuff(',');
3461 if (oap->end.lnum == curwin->w_cursor.lnum)
3462 stuffcharReadbuff('.');
3463 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3464 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003465 else if (oap->start.lnum == curwin->w_cursor.lnum
3466#ifdef FEAT_FOLDING
3467 // do not use ".+number" for a closed fold, it would count
3468 // folded lines twice
3469 && !hasFolding(oap->end.lnum, NULL, NULL)
3470#endif
3471 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003472 {
3473 stuffReadbuff((char_u *)".+");
3474 stuffnumReadbuff((long)oap->line_count - 1);
3475 }
3476 else
3477 stuffnumReadbuff((long)oap->end.lnum);
3478 }
3479 }
3480 if (oap->op_type != OP_COLON)
3481 stuffReadbuff((char_u *)"!");
3482 if (oap->op_type == OP_INDENT)
3483 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003484 if (*get_equalprg() == NUL)
3485 stuffReadbuff((char_u *)"indent");
3486 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003487 stuffReadbuff(get_equalprg());
3488 stuffReadbuff((char_u *)"\n");
3489 }
3490 else if (oap->op_type == OP_FORMAT)
3491 {
3492 if (*curbuf->b_p_fp != NUL)
3493 stuffReadbuff(curbuf->b_p_fp);
3494 else if (*p_fp != NUL)
3495 stuffReadbuff(p_fp);
3496 else
3497 stuffReadbuff((char_u *)"fmt");
3498 stuffReadbuff((char_u *)"\n']");
3499 }
3500
3501 // do_cmdline() does the rest
3502}
3503
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003504// callback function for 'operatorfunc'
3505static callback_T opfunc_cb;
3506
3507/*
3508 * Process the 'operatorfunc' option value.
3509 * Returns OK or FAIL.
3510 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003511 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003512did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003513{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003514 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3515 return e_invalid_argument;
3516
3517 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003518}
3519
3520#if defined(EXITFREE) || defined(PROTO)
3521 void
3522free_operatorfunc_option(void)
3523{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003524# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003525 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003526# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003527}
3528#endif
3529
Dominique Pelle748b3082022-01-08 12:41:16 +00003530#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003531/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003532 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003533 * garbage collected.
3534 */
3535 int
3536set_ref_in_opfunc(int copyID UNUSED)
3537{
3538 int abort = FALSE;
3539
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003540 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003541
3542 return abort;
3543}
Dominique Pelle748b3082022-01-08 12:41:16 +00003544#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003545
3546/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003547 * Handle the "g@" operator: call 'operatorfunc'.
3548 */
3549 static void
3550op_function(oparg_T *oap UNUSED)
3551{
3552#ifdef FEAT_EVAL
3553 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003554 pos_T orig_start = curbuf->b_op_start;
3555 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003556 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003557
3558 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003559 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003560 else
3561 {
3562 // Set '[ and '] marks to text to be operated on.
3563 curbuf->b_op_start = oap->start;
3564 curbuf->b_op_end = oap->end;
3565 if (oap->motion_type != MLINE && !oap->inclusive)
3566 // Exclude the end position.
3567 decl(&curbuf->b_op_end);
3568
3569 argv[0].v_type = VAR_STRING;
3570 if (oap->block_mode)
3571 argv[0].vval.v_string = (char_u *)"block";
3572 else if (oap->motion_type == MLINE)
3573 argv[0].vval.v_string = (char_u *)"line";
3574 else
3575 argv[0].vval.v_string = (char_u *)"char";
3576 argv[1].v_type = VAR_UNKNOWN;
3577
3578 // Reset virtual_op so that 'virtualedit' can be changed in the
3579 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003580 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003581 virtual_op = MAYBE;
3582
naohiro ono75c30e92021-10-19 11:15:41 +01003583 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003584 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003585 finish_op = FALSE;
3586
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003587 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3588 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003589
3590 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003591 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003592 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003593 {
3594 curbuf->b_op_start = orig_start;
3595 curbuf->b_op_end = orig_end;
3596 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003597 }
3598#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003599 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003600#endif
3601}
3602
3603/*
3604 * Calculate start/end virtual columns for operating in block mode.
3605 */
3606 static void
3607get_op_vcol(
3608 oparg_T *oap,
3609 colnr_T redo_VIsual_vcol,
3610 int initial) // when TRUE adjust position for 'selectmode'
3611{
3612 colnr_T start, end;
3613
3614 if (VIsual_mode != Ctrl_V
3615 || (!initial && oap->end.col < curwin->w_width))
3616 return;
3617
3618 oap->block_mode = TRUE;
3619
3620 // prevent from moving onto a trail byte
3621 if (has_mbyte)
3622 mb_adjustpos(curwin->w_buffer, &oap->end);
3623
3624 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3625
3626 if (!redo_VIsual_busy)
3627 {
3628 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3629
3630 if (start < oap->start_vcol)
3631 oap->start_vcol = start;
3632 if (end > oap->end_vcol)
3633 {
3634 if (initial && *p_sel == 'e' && start >= 1
3635 && start - 1 >= oap->end_vcol)
3636 oap->end_vcol = start - 1;
3637 else
3638 oap->end_vcol = end;
3639 }
3640 }
3641
3642 // if '$' was used, get oap->end_vcol from longest line
3643 if (curwin->w_curswant == MAXCOL)
3644 {
3645 curwin->w_cursor.col = MAXCOL;
3646 oap->end_vcol = 0;
3647 for (curwin->w_cursor.lnum = oap->start.lnum;
3648 curwin->w_cursor.lnum <= oap->end.lnum;
3649 ++curwin->w_cursor.lnum)
3650 {
3651 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3652 if (end > oap->end_vcol)
3653 oap->end_vcol = end;
3654 }
3655 }
3656 else if (redo_VIsual_busy)
3657 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3658 // Correct oap->end.col and oap->start.col to be the
3659 // upper-left and lower-right corner of the block area.
3660 //
3661 // (Actually, this does convert column positions into character
3662 // positions)
3663 curwin->w_cursor.lnum = oap->end.lnum;
3664 coladvance(oap->end_vcol);
3665 oap->end = curwin->w_cursor;
3666
3667 curwin->w_cursor = oap->start;
3668 coladvance(oap->start_vcol);
3669 oap->start = curwin->w_cursor;
3670}
3671
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003672// Information for redoing the previous Visual selection.
3673typedef struct {
3674 int rv_mode; // 'v', 'V', or Ctrl-V
3675 linenr_T rv_line_count; // number of lines
3676 colnr_T rv_vcol; // number of cols or end column
3677 long rv_count; // count for Visual operator
3678 int rv_arg; // extra argument
3679} redo_VIsual_T;
3680
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003681 static int
3682is_ex_cmdchar(cmdarg_T *cap)
3683{
3684 return cap->cmdchar == ':'
3685 || cap->cmdchar == K_COMMAND
3686 || cap->cmdchar == K_SCRIPT_COMMAND;
3687}
3688
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003689/*
3690 * Handle an operator after Visual mode or when the movement is finished.
3691 * "gui_yank" is true when yanking text for the clipboard.
3692 */
3693 void
3694do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3695{
3696 oparg_T *oap = cap->oap;
3697 pos_T old_cursor;
3698 int empty_region_error;
3699 int restart_edit_save;
3700#ifdef FEAT_LINEBREAK
3701 int lbr_saved = curwin->w_p_lbr;
3702#endif
3703
3704 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003705 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3706
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003707 int include_line_break = FALSE;
3708
3709#if defined(FEAT_CLIPBOARD)
3710 // Yank the visual area into the GUI selection register before we operate
3711 // on it and lose it forever.
3712 // Don't do it if a specific register was specified, so that ""x"*P works.
3713 // This could call do_pending_operator() recursively, but that's OK
3714 // because gui_yank will be TRUE for the nested call.
3715 if ((clip_star.available || clip_plus.available)
3716 && oap->op_type != OP_NOP
3717 && !gui_yank
3718 && VIsual_active
3719 && !redo_VIsual_busy
3720 && oap->regname == 0)
3721 clip_auto_select();
3722#endif
3723 old_cursor = curwin->w_cursor;
3724
3725 // If an operation is pending, handle it...
3726 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3727 {
3728 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3729 // for the clipboard.
3730 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3731
3732#ifdef FEAT_LINEBREAK
3733 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003734 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003735#endif
3736 oap->is_VIsual = VIsual_active;
3737 if (oap->motion_force == 'V')
3738 oap->motion_type = MLINE;
3739 else if (oap->motion_force == 'v')
3740 {
3741 // If the motion was linewise, "inclusive" will not have been set.
3742 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3743 if (oap->motion_type == MLINE)
3744 oap->inclusive = FALSE;
3745 // If the motion already was characterwise, toggle "inclusive"
3746 else if (oap->motion_type == MCHAR)
3747 oap->inclusive = !oap->inclusive;
3748 oap->motion_type = MCHAR;
3749 }
3750 else if (oap->motion_force == Ctrl_V)
3751 {
3752 // Change line- or characterwise motion into Visual block mode.
3753 if (!VIsual_active)
3754 {
3755 VIsual_active = TRUE;
3756 VIsual = oap->start;
3757 }
3758 VIsual_mode = Ctrl_V;
3759 VIsual_select = FALSE;
3760 VIsual_reselect = FALSE;
3761 }
3762
3763 // Only redo yank when 'y' flag is in 'cpoptions'.
3764 // Never redo "zf" (define fold).
3765 if ((redo_yank || oap->op_type != OP_YANK)
3766 && ((!VIsual_active || oap->motion_force)
3767 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003768 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003769 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003770 && cap->cmdchar != 'D'
3771#ifdef FEAT_FOLDING
3772 && oap->op_type != OP_FOLD
3773 && oap->op_type != OP_FOLDOPEN
3774 && oap->op_type != OP_FOLDOPENREC
3775 && oap->op_type != OP_FOLDCLOSE
3776 && oap->op_type != OP_FOLDCLOSEREC
3777 && oap->op_type != OP_FOLDDEL
3778 && oap->op_type != OP_FOLDDELREC
3779#endif
3780 )
3781 {
3782 prep_redo(oap->regname, cap->count0,
3783 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3784 oap->motion_force, cap->cmdchar, cap->nchar);
3785 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3786 {
3787 // If 'cpoptions' does not contain 'r', insert the search
3788 // pattern to really repeat the same command.
3789 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3790 AppendToRedobuffLit(cap->searchbuf, -1);
3791 AppendToRedobuff(NL_STR);
3792 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003793 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003794 {
3795 // do_cmdline() has stored the first typed line in
3796 // "repeat_cmdline". When several lines are typed repeating
3797 // won't be possible.
3798 if (repeat_cmdline == NULL)
3799 ResetRedobuff();
3800 else
3801 {
zeertzjq30b6d612023-05-07 17:39:23 +01003802 if (cap->cmdchar == ':')
3803 AppendToRedobuffLit(repeat_cmdline, -1);
3804 else
3805 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003806 AppendToRedobuff(NL_STR);
3807 VIM_CLEAR(repeat_cmdline);
3808 }
3809 }
3810 }
3811
3812 if (redo_VIsual_busy)
3813 {
3814 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003815 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003816 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003817 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003818 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3819 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003820 VIsual_mode = redo_VIsual.rv_mode;
3821 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003822 {
3823 if (VIsual_mode == 'v')
3824 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003825 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003826 {
3827 validate_virtcol();
3828 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003829 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003830 }
3831 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003832 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003833 }
3834 else
3835 {
3836 curwin->w_curswant = MAXCOL;
3837 }
3838 coladvance(curwin->w_curswant);
3839 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003840 cap->count0 = redo_VIsual.rv_count;
3841 if (redo_VIsual.rv_count != 0)
3842 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003843 else
3844 cap->count1 = 1;
3845 }
3846 else if (VIsual_active)
3847 {
3848 if (!gui_yank)
3849 {
3850 // Save the current VIsual area for '< and '> marks, and "gv"
3851 curbuf->b_visual.vi_start = VIsual;
3852 curbuf->b_visual.vi_end = curwin->w_cursor;
3853 curbuf->b_visual.vi_mode = VIsual_mode;
3854 restore_visual_mode();
3855 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003856#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003857 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003858#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003859 }
3860
3861 // In Select mode, a linewise selection is operated upon like a
3862 // characterwise selection.
3863 // Special case: gH<Del> deletes the last line.
3864 if (VIsual_select && VIsual_mode == 'V'
3865 && cap->oap->op_type != OP_DELETE)
3866 {
3867 if (LT_POS(VIsual, curwin->w_cursor))
3868 {
3869 VIsual.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003870 curwin->w_cursor.col = ml_get_len(curwin->w_cursor.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003871 }
3872 else
3873 {
3874 curwin->w_cursor.col = 0;
zeertzjq94b7c322024-03-12 21:50:32 +01003875 VIsual.col = ml_get_len(VIsual.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003876 }
3877 VIsual_mode = 'v';
3878 }
3879 // If 'selection' is "exclusive", backup one character for
3880 // charwise selections.
3881 else if (VIsual_mode == 'v')
3882 include_line_break = unadjust_for_sel();
3883
3884 oap->start = VIsual;
3885 if (VIsual_mode == 'V')
3886 {
3887 oap->start.col = 0;
3888 oap->start.coladd = 0;
3889 }
3890 }
3891
3892 // Set oap->start to the first position of the operated text, oap->end
3893 // to the end of the operated text. w_cursor is equal to oap->start.
3894 if (LT_POS(oap->start, curwin->w_cursor))
3895 {
3896#ifdef FEAT_FOLDING
3897 // Include folded lines completely.
3898 if (!VIsual_active)
3899 {
3900 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3901 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003902 if ((curwin->w_cursor.col > 0 || oap->inclusive
3903 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003904 && hasFolding(curwin->w_cursor.lnum, NULL,
3905 &curwin->w_cursor.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003906 curwin->w_cursor.col = ml_get_curline_len();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003907 }
3908#endif
3909 oap->end = curwin->w_cursor;
3910 curwin->w_cursor = oap->start;
3911
3912 // w_virtcol may have been updated; if the cursor goes back to its
3913 // previous position w_virtcol becomes invalid and isn't updated
3914 // automatically.
3915 curwin->w_valid &= ~VALID_VIRTCOL;
3916 }
3917 else
3918 {
3919#ifdef FEAT_FOLDING
3920 // Include folded lines completely.
3921 if (!VIsual_active && oap->motion_type == MLINE)
3922 {
3923 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3924 NULL))
3925 curwin->w_cursor.col = 0;
3926 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
zeertzjq94b7c322024-03-12 21:50:32 +01003927 oap->start.col = ml_get_len(oap->start.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003928 }
3929#endif
3930 oap->end = oap->start;
3931 oap->start = curwin->w_cursor;
3932 }
3933
3934 // Just in case lines were deleted that make the position invalid.
3935 check_pos(curwin->w_buffer, &oap->end);
3936 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3937
3938 // Set "virtual_op" before resetting VIsual_active.
3939 virtual_op = virtual_active();
3940
3941 if (VIsual_active || redo_VIsual_busy)
3942 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003943 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003944
3945 if (!redo_VIsual_busy && !gui_yank)
3946 {
3947 // Prepare to reselect and redo Visual: this is based on the
3948 // size of the Visual text
3949 resel_VIsual_mode = VIsual_mode;
3950 if (curwin->w_curswant == MAXCOL)
3951 resel_VIsual_vcol = MAXCOL;
3952 else
3953 {
3954 if (VIsual_mode != Ctrl_V)
3955 getvvcol(curwin, &(oap->end),
3956 NULL, NULL, &oap->end_vcol);
3957 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3958 {
3959 if (VIsual_mode != Ctrl_V)
3960 getvvcol(curwin, &(oap->start),
3961 &oap->start_vcol, NULL, NULL);
3962 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3963 }
3964 else
3965 resel_VIsual_vcol = oap->end_vcol;
3966 }
3967 resel_VIsual_line_count = oap->line_count;
3968 }
3969
3970 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3971 if ((redo_yank || oap->op_type != OP_YANK)
3972 && oap->op_type != OP_COLON
3973#ifdef FEAT_FOLDING
3974 && oap->op_type != OP_FOLD
3975 && oap->op_type != OP_FOLDOPEN
3976 && oap->op_type != OP_FOLDOPENREC
3977 && oap->op_type != OP_FOLDCLOSE
3978 && oap->op_type != OP_FOLDCLOSEREC
3979 && oap->op_type != OP_FOLDDEL
3980 && oap->op_type != OP_FOLDDELREC
3981#endif
3982 && oap->motion_force == NUL
3983 )
3984 {
3985 // Prepare for redoing. Only use the nchar field for "r",
3986 // otherwise it might be the second char of the operator.
3987 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3988 || cap->nchar == 'N'))
3989 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003990 get_op_char(oap->op_type),
3991 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003992 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003993 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003994 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003995 int opchar = get_op_char(oap->op_type);
3996 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003997 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3998
3999 // reverse what nv_replace() did
4000 if (nchar == REPLACE_CR_NCHAR)
4001 nchar = CAR;
4002 else if (nchar == REPLACE_NL_NCHAR)
4003 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00004004
4005 if (opchar == 'g' && extra_opchar == '@')
4006 // also repeat the count for 'operatorfunc'
4007 prep_redo_num2(oap->regname, 0L, NUL, 'v',
4008 cap->count0, opchar, extra_opchar, nchar);
4009 else
4010 prep_redo(oap->regname, 0L, NUL, 'v',
4011 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004012 }
4013 if (!redo_VIsual_busy)
4014 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004015 redo_VIsual.rv_mode = resel_VIsual_mode;
4016 redo_VIsual.rv_vcol = resel_VIsual_vcol;
4017 redo_VIsual.rv_line_count = resel_VIsual_line_count;
4018 redo_VIsual.rv_count = cap->count0;
4019 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004020 }
4021 }
4022
4023 // oap->inclusive defaults to TRUE.
4024 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4025 // FALSE. This makes "d}P" and "v}dP" work the same.
4026 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4027 oap->inclusive = TRUE;
4028 if (VIsual_mode == 'V')
4029 oap->motion_type = MLINE;
4030 else
4031 {
4032 oap->motion_type = MCHAR;
4033 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4034 && (include_line_break || !virtual_op))
4035 {
4036 oap->inclusive = FALSE;
4037 // Try to include the newline, unless it's an operator
4038 // that works on lines only.
4039 if (*p_sel != 'o'
4040 && !op_on_lines(oap->op_type)
4041 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4042 {
4043 ++oap->end.lnum;
4044 oap->end.col = 0;
4045 oap->end.coladd = 0;
4046 ++oap->line_count;
4047 }
4048 }
4049 }
4050
4051 redo_VIsual_busy = FALSE;
4052
4053 // Switch Visual off now, so screen updating does
4054 // not show inverted text when the screen is redrawn.
4055 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4056 // no screen redraw, so it is done here to remove the inverted
4057 // part.
4058 if (!gui_yank)
4059 {
4060 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004061 setmouse();
4062 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004063 may_clear_cmdline();
4064 if ((oap->op_type == OP_YANK
4065 || oap->op_type == OP_COLON
4066 || oap->op_type == OP_FUNCTION
4067 || oap->op_type == OP_FILTER)
4068 && oap->motion_force == NUL)
4069 {
4070#ifdef FEAT_LINEBREAK
4071 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01004072 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004073#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004074 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004075 }
4076 }
4077 }
4078
4079 // Include the trailing byte of a multi-byte char.
4080 if (has_mbyte && oap->inclusive)
4081 {
4082 int l;
4083
4084 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4085 if (l > 1)
4086 oap->end.col += l - 1;
4087 }
4088 curwin->w_set_curswant = TRUE;
4089
4090 // oap->empty is set when start and end are the same. The inclusive
4091 // flag affects this too, unless yanking and the end is on a NUL.
4092 oap->empty = (oap->motion_type == MCHAR
4093 && (!oap->inclusive
4094 || (oap->op_type == OP_YANK
4095 && gchar_pos(&oap->end) == NUL))
4096 && EQUAL_POS(oap->start, oap->end)
4097 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4098 // For delete, change and yank, it's an error to operate on an
4099 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4100 empty_region_error = (oap->empty
4101 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4102
4103 // Force a redraw when operating on an empty Visual region, when
4104 // 'modifiable is off or creating a fold.
4105 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4106#ifdef FEAT_FOLDING
4107 || oap->op_type == OP_FOLD
4108#endif
4109 ))
4110 {
4111#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004112 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004113#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004114 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004115 }
4116
4117 // If the end of an operator is in column one while oap->motion_type
4118 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4119 // character in the previous line. If op_start is on or before the
4120 // first non-blank in the line, the operator becomes linewise
4121 // (strange, but that's the way vi does it).
4122 if ( oap->motion_type == MCHAR
4123 && oap->inclusive == FALSE
4124 && !(cap->retval & CA_NO_ADJ_OP_END)
4125 && oap->end.col == 0
4126 && (!oap->is_VIsual || *p_sel == 'o')
4127 && !oap->block_mode
4128 && oap->line_count > 1)
4129 {
4130 oap->end_adjusted = TRUE; // remember that we did this
4131 --oap->line_count;
4132 --oap->end.lnum;
4133 if (inindent(0))
4134 oap->motion_type = MLINE;
4135 else
4136 {
zeertzjq94b7c322024-03-12 21:50:32 +01004137 oap->end.col = ml_get_len(oap->end.lnum);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004138 if (oap->end.col)
4139 {
4140 --oap->end.col;
4141 oap->inclusive = TRUE;
4142 }
4143 }
4144 }
4145 else
4146 oap->end_adjusted = FALSE;
4147
4148 switch (oap->op_type)
4149 {
4150 case OP_LSHIFT:
4151 case OP_RSHIFT:
4152 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4153 auto_format(FALSE, TRUE);
4154 break;
4155
4156 case OP_JOIN_NS:
4157 case OP_JOIN:
4158 if (oap->line_count < 2)
4159 oap->line_count = 2;
4160 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4161 curbuf->b_ml.ml_line_count)
4162 beep_flush();
4163 else
4164 {
4165 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4166 TRUE, TRUE, TRUE);
4167 auto_format(FALSE, TRUE);
4168 }
4169 break;
4170
4171 case OP_DELETE:
4172 VIsual_reselect = FALSE; // don't reselect now
4173 if (empty_region_error)
4174 {
4175 vim_beep(BO_OPER);
4176 CancelRedo();
4177 }
4178 else
4179 {
4180 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004181 // save cursor line for undo if it wasn't saved yet
4182 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4183 && u_save_cursor() == OK)
4184 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004185 }
4186 break;
4187
4188 case OP_YANK:
4189 if (empty_region_error)
4190 {
4191 if (!gui_yank)
4192 {
4193 vim_beep(BO_OPER);
4194 CancelRedo();
4195 }
4196 }
4197 else
4198 {
4199#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004200 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004201#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004202 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004203 (void)op_yank(oap, FALSE, !gui_yank);
4204 }
4205 check_cursor_col();
4206 break;
4207
4208 case OP_CHANGE:
4209 VIsual_reselect = FALSE; // don't reselect now
4210 if (empty_region_error)
4211 {
4212 vim_beep(BO_OPER);
4213 CancelRedo();
4214 }
4215 else
4216 {
4217 // This is a new edit command, not a restart. Need to
4218 // remember it to make 'insertmode' work with mappings for
4219 // Visual mode. But do this only once and not when typed and
4220 // 'insertmode' isn't set.
4221 if (p_im || !KeyTyped)
4222 restart_edit_save = restart_edit;
4223 else
4224 restart_edit_save = 0;
4225 restart_edit = 0;
4226#ifdef FEAT_LINEBREAK
4227 // Restore linebreak, so that when the user edits it looks as
4228 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004229 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004230#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004231 // trigger TextChangedI
4232 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4233
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004234 if (op_change(oap)) // will call edit()
4235 cap->retval |= CA_COMMAND_BUSY;
4236 if (restart_edit == 0)
4237 restart_edit = restart_edit_save;
4238 }
4239 break;
4240
4241 case OP_FILTER:
4242 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4243 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4244 else
4245 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4246 // FALLTHROUGH
4247
4248 case OP_INDENT:
4249 case OP_COLON:
4250
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004251 // If 'equalprg' is empty, do the indenting internally.
4252 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4253 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004254 if (curbuf->b_p_lisp)
4255 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004256#ifdef FEAT_EVAL
4257 if (use_indentexpr_for_lisp())
4258 op_reindent(oap, get_expr_indent);
4259 else
4260#endif
4261 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004262 break;
4263 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004264 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004265#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004266 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004267#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004268 get_c_indent);
4269 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004270 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004271
4272 op_colon(oap);
4273 break;
4274
4275 case OP_TILDE:
4276 case OP_UPPER:
4277 case OP_LOWER:
4278 case OP_ROT13:
4279 if (empty_region_error)
4280 {
4281 vim_beep(BO_OPER);
4282 CancelRedo();
4283 }
4284 else
4285 op_tilde(oap);
4286 check_cursor_col();
4287 break;
4288
4289 case OP_FORMAT:
4290#if defined(FEAT_EVAL)
4291 if (*curbuf->b_p_fex != NUL)
4292 op_formatexpr(oap); // use expression
4293 else
4294#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004295 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004296 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004297 op_colon(oap); // use external command
4298 else
4299 op_format(oap, FALSE); // use internal function
4300 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004301 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004302 case OP_FORMAT2:
4303 op_format(oap, TRUE); // use internal function
4304 break;
4305
4306 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004307 {
4308 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4309
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004310#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004311 // Restore linebreak, so that when the user edits it looks as
4312 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004313 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004314#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004315 // call 'operatorfunc'
4316 op_function(oap);
4317
4318 // Restore the info for redoing Visual mode, the function may
4319 // invoke another operator and unintentionally change it.
4320 redo_VIsual = save_redo_VIsual;
4321 break;
4322 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004323
4324 case OP_INSERT:
4325 case OP_APPEND:
4326 VIsual_reselect = FALSE; // don't reselect now
4327 if (empty_region_error)
4328 {
4329 vim_beep(BO_OPER);
4330 CancelRedo();
4331 }
4332 else
4333 {
4334 // This is a new edit command, not a restart. Need to
4335 // remember it to make 'insertmode' work with mappings for
4336 // Visual mode. But do this only once.
4337 restart_edit_save = restart_edit;
4338 restart_edit = 0;
4339#ifdef FEAT_LINEBREAK
4340 // Restore linebreak, so that when the user edits it looks as
4341 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004342 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004343#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004344 // trigger TextChangedI
4345 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4346
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004347 op_insert(oap, cap->count1);
4348#ifdef FEAT_LINEBREAK
4349 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004350 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004351#endif
4352
4353 // TODO: when inserting in several lines, should format all
4354 // the lines.
4355 auto_format(FALSE, TRUE);
4356
4357 if (restart_edit == 0)
4358 restart_edit = restart_edit_save;
4359 else
4360 cap->retval |= CA_COMMAND_BUSY;
4361 }
4362 break;
4363
4364 case OP_REPLACE:
4365 VIsual_reselect = FALSE; // don't reselect now
4366 if (empty_region_error)
4367 {
4368 vim_beep(BO_OPER);
4369 CancelRedo();
4370 }
4371 else
4372 {
4373#ifdef FEAT_LINEBREAK
4374 // Restore linebreak, so that when the user edits it looks as
4375 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004376 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004377#endif
4378 op_replace(oap, cap->nchar);
4379 }
4380 break;
4381
4382#ifdef FEAT_FOLDING
4383 case OP_FOLD:
4384 VIsual_reselect = FALSE; // don't reselect now
4385 foldCreate(oap->start.lnum, oap->end.lnum);
4386 break;
4387
4388 case OP_FOLDOPEN:
4389 case OP_FOLDOPENREC:
4390 case OP_FOLDCLOSE:
4391 case OP_FOLDCLOSEREC:
4392 VIsual_reselect = FALSE; // don't reselect now
4393 opFoldRange(oap->start.lnum, oap->end.lnum,
4394 oap->op_type == OP_FOLDOPEN
4395 || oap->op_type == OP_FOLDOPENREC,
4396 oap->op_type == OP_FOLDOPENREC
4397 || oap->op_type == OP_FOLDCLOSEREC,
4398 oap->is_VIsual);
4399 break;
4400
4401 case OP_FOLDDEL:
4402 case OP_FOLDDELREC:
4403 VIsual_reselect = FALSE; // don't reselect now
4404 deleteFold(oap->start.lnum, oap->end.lnum,
4405 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4406 break;
4407#endif
4408 case OP_NR_ADD:
4409 case OP_NR_SUB:
4410 if (empty_region_error)
4411 {
4412 vim_beep(BO_OPER);
4413 CancelRedo();
4414 }
4415 else
4416 {
4417 VIsual_active = TRUE;
4418#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004419 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004420#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004421 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004422 VIsual_active = FALSE;
4423 }
4424 check_cursor_col();
4425 break;
4426 default:
4427 clearopbeep(oap);
4428 }
4429 virtual_op = MAYBE;
4430 if (!gui_yank)
4431 {
4432 // if 'sol' not set, go back to old column for some commands
4433 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4434 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4435 || oap->op_type == OP_DELETE))
4436 {
4437#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004438 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004439#endif
4440 coladvance(curwin->w_curswant = old_col);
4441 }
4442 }
4443 else
4444 {
4445 curwin->w_cursor = old_cursor;
4446 }
4447 oap->block_mode = FALSE;
4448 clearop(oap);
4449 motion_force = NUL;
4450 }
4451#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004452 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004453#endif
4454}
Christian Brabandtffb13672023-09-15 20:22:02 +02004455
4456// put byte 'c' at position 'lp', but
4457// verify, that the position to place
4458// is actually safe
4459 static void
4460pbyte(pos_T lp, int c)
4461{
4462 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4463 int len = curbuf->b_ml.ml_line_len;
4464
4465 // safety check
4466 if (lp.col >= len)
4467 lp.col = (len > 1 ? len - 2 : 0);
4468 *(p + lp.col) = c;
4469}