blob: ecd7fc2170c58bf958eeabe3b896d09d6136c6b4 [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;
215 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
216 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{
Christian Brabandt6bf13182023-11-14 22:42:59 +0100232 long long count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200234 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Christian Brabandt6bf13182023-11-14 22:42:59 +0100236 count = (long long)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;
Bram Moolenaardac13472019-09-16 21:06:21 +0200252 count = i * 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 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200258 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (count < 0)
260 count = 0;
261 }
262 else
Christian Brabandt6bf13182023-11-14 22:42:59 +0100263 {
264 if ((long long)sw_val * (long long)amount > INT_MAX - count)
265 count = INT_MAX;
266 else
267 count += (long long)sw_val * (long long)amount;
268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 }
270
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200271 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (State & VREPLACE_FLAG)
Christian Brabandt6bf13182023-11-14 22:42:59 +0100273 change_indent(INDENT_SET, (int)count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 else
Christian Brabandt6bf13182023-11-14 22:42:59 +0100275 (void)set_indent((int)count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276}
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278/*
279 * Shift one line of the current block one shiftwidth right or left.
280 * Leaves cursor on first character in block.
281 */
282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100283shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284{
285 int left = (oap->op_type == OP_LSHIFT);
286 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000287 int total;
288 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200290 int sw_val = (int)get_sw_value_indent(curbuf);
291 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000294 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100295 int added;
296 unsigned new_line_len; // the length of the line after the
297 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298#ifdef FEAT_RIGHTLEFT
299 int old_p_ri = p_ri;
300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100301 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302#endif
303
Bram Moolenaar24959102022-05-07 20:01:16 +0100304 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
306 if (bd.is_short)
307 return;
308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100309 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200310 total = (int)((unsigned)amount * (unsigned)sw_val);
311 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100312 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 oldp = ml_get_curline();
315
316 if (!left)
317 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100318 int tabs = 0, spaces = 0;
319 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100320
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 /*
322 * 1. Get start vcol
323 * 2. Total ws vcols
324 * 3. Divvy into TABs & spp
325 * 4. Construct new string
326 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100327 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 ws_vcol = bd.start_vcol - bd.pre_whitesp;
329 if (bd.startspaces)
330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100332 {
333 if ((*mb_ptr2len)(bd.textstart) == 1)
334 ++bd.textstart;
335 else
336 {
337 ws_vcol = 0;
338 bd.startspaces = 0;
339 }
340 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000341 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000342 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100344
345 // TODO: is passing bd.textstart for start of the line OK?
346 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
347 bd.start_vcol, bd.textstart, bd.textstart);
348 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100350 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100352 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100354 bd.textstart = cts.cts_ptr;
355 bd.start_vcol = cts.cts_vcol;
356 clear_chartabsize_arg(&cts);
357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100358 // OK, now total=all the VWS reqd, and textstart points at the 1st
359 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200360#ifdef FEAT_VARTABS
361 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100363 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200364 else
LemonBoy4b936742022-05-13 21:56:28 +0100365 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200366#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100368 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
369 if (tabs > 0)
370 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 else
LemonBoy4b936742022-05-13 21:56:28 +0100372 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200373#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100374 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100376
377 new_line_len = bd.textcol + tabs + spaces + (int)STRLEN(bd.textstart);
378 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (newp == NULL)
380 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 mch_memmove(newp, oldp, (size_t)bd.textcol);
LemonBoy4b936742022-05-13 21:56:28 +0100382 vim_memset(newp + bd.textcol, TAB, (size_t)tabs);
383 vim_memset(newp + bd.textcol + tabs, ' ', (size_t)spaces);
384 // Note that STRMOVE() copies the trailing NUL.
385 STRMOVE(newp + bd.textcol + tabs + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100389 colnr_T destination_col; // column to which text in block will
390 // be shifted
391 char_u *verbatim_copy_end; // end of the part of the line which is
392 // copied verbatim
393 colnr_T verbatim_copy_width;// the (displayed) width of this part
394 // of line
395 unsigned fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000396 size_t block_space_width;
397 size_t shift_amount;
398 char_u *non_white = bd.textstart;
399 colnr_T non_white_col;
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.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000471 new_line_len = (unsigned)(verbatim_copy_end - oldp)
472 + fill
LemonBoy4b936742022-05-13 21:56:28 +0100473 + (unsigned)STRLEN(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000474
LemonBoy4b936742022-05-13 21:56:28 +0100475 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (newp == NULL)
477 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000478 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200479 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
LemonBoy4b936742022-05-13 21:56:28 +0100480 // Note that STRMOVE() copies the trailing NUL.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000481 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100483 // replace the line
LemonBoy4b936742022-05-13 21:56:28 +0100484 added = new_line_len - (int)STRLEN(oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
LemonBoy4b936742022-05-13 21:56:28 +0100486 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 State = oldstate;
488 curwin->w_cursor.col = oldcol;
489#ifdef FEAT_RIGHTLEFT
490 p_ri = old_p_ri;
491#endif
492}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494/*
495 * Insert string "s" (b_insert ? before : after) block :AKelly
496 * Caller must prepare for undo.
497 */
498 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100499block_insert(
500 oparg_T *oap,
501 char_u *s,
502 int b_insert,
503 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
Bram Moolenaardac13472019-09-16 21:06:21 +0200505 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100506 int count = 0; // extra spaces to replace a cut TAB
507 int spaces = 0; // non-zero if cutting a TAB
508 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200509 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100510 unsigned s_len; // STRLEN(s)
511 char_u *newp, *oldp; // new, old lines
512 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 int oldstate = State;
514
Bram Moolenaar24959102022-05-07 20:01:16 +0100515 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 s_len = (unsigned)STRLEN(s);
517
518 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
519 {
520 block_prep(oap, bdp, lnum, TRUE);
521 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100522 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
524 oldp = ml_get(lnum);
525
526 if (b_insert)
527 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200528 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 spaces = bdp->startspaces;
530 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 offset = bdp->textcol;
533 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100534 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200536 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100537 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200539 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 offset = bdp->textcol + bdp->textlen - (spaces != 0);
543 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100544 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100546 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 if (!bdp->is_MAX)
548 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
549 count = spaces;
550 offset = bdp->textcol + bdp->textlen;
551 }
552 }
553
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200554 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000555 // avoid copying part of a multi-byte character
556 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100557
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200558 if (spaces < 0) // can happen when the cursor was moved
559 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200560
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000561 // Make sure the allocated size matches what is actually copied below.
562 newp = alloc(STRLEN(oldp) + spaces + s_len
563 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
564 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (newp == NULL)
566 continue;
567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100568 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000569 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 oldp += offset;
571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100572 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200573 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200574 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100576 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200577 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 offset += s_len;
579
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000580 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000582 if (*oldp == TAB)
583 {
584 // insert post-padding
585 vim_memset(newp + offset + spaces, ' ',
586 (size_t)(ts_val - spaces));
587 // we're splitting a TAB, don't copy it
588 oldp++;
589 // We allowed for that TAB, remember this now
590 count++;
591 }
592 else
593 // Not a TAB, no extra spaces
594 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
596
597 if (spaces > 0)
598 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000599 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 ml_replace(lnum, newp, FALSE);
602
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200603 if (b_insert)
604 // correct any text properties
605 inserted_bytes(lnum, startcol, s_len);
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (lnum == oap->end.lnum)
608 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 // Set "']" mark to the end of the block instead of the end of
610 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 curbuf->b_op_end.lnum = oap->end.lnum;
612 curbuf->b_op_end.col = offset;
613 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
617
618 State = oldstate;
619}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200622 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200624 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 */
626 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100627op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 int n;
630 linenr_T lnum;
631 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 char_u *newp, *oldp;
633 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
635 int did_yank = FALSE;
636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100637 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 return OK;
639
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100640 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (oap->empty)
642 return u_save_cursor();
643
644 if (!curbuf->b_p_ma)
645 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200646 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 return FAIL;
648 }
649
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000650 if (VIsual_select && oap->is_VIsual)
651 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100652 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#ifdef FEAT_CLIPBOARD
655 adjust_clip_reg(&oap->regname);
656#endif
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (has_mbyte)
659 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
Bram Moolenaard04b7502010-07-08 22:27:55 +0200661 /*
662 * Imitate the strange Vi behaviour: If the delete spans more than one
663 * line and motion_type == MCHAR and the result is a blank line, make the
664 * delete linewise. Don't do this for the change command or Visual mode.
665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000668 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100670 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 && oap->op_type == OP_DELETE)
672 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200673 ptr = ml_get(oap->end.lnum) + oap->end.col;
674 if (*ptr != NUL)
675 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 ptr = skipwhite(ptr);
677 if (*ptr == NUL && inindent(0))
678 oap->motion_type = MLINE;
679 }
680
Bram Moolenaard04b7502010-07-08 22:27:55 +0200681 /*
682 * Check for trying to delete (e.g. "D") in an empty line.
683 * Note: For the change operator it is ok.
684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if ( oap->motion_type == MCHAR
686 && oap->line_count == 1
687 && oap->op_type == OP_DELETE
688 && *ml_get(oap->start.lnum) == NUL)
689 {
690 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000691 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 * 'cpoptions' (Vi compatible).
693 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000694 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100695 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
696 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000697 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
699 beep_flush();
700 return OK;
701 }
702
Bram Moolenaard04b7502010-07-08 22:27:55 +0200703 /*
704 * Do a yank of whatever we're about to delete.
705 * If a yank register was specified, put the deleted text into that
706 * register. For the black hole register '_' don't yank anything.
707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (oap->regname != '_')
709 {
710 if (oap->regname != 0)
711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (!valid_yank_reg(oap->regname, TRUE))
714 {
715 beep_flush();
716 return OK;
717 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100718 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
719 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 did_yank = TRUE;
721 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200722 else
723 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
725 /*
726 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100727 * delete contains a line break, or when using a specific operator (Vi
728 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100730 if (oap->motion_type == MLINE || oap->line_count > 1
731 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100733 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 if (op_yank(oap, TRUE, FALSE) == OK)
735 did_yank = TRUE;
736 }
737
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100738 // Yank into small delete register when no named register specified
739 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200740 if ((
741#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200742 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
743 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200744#endif
745 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 && oap->line_count == 1)
747 {
748 oap->regname = '-';
749 get_yank_register(oap->regname, TRUE);
750 if (op_yank(oap, TRUE, FALSE) == OK)
751 did_yank = TRUE;
752 oap->regname = 0;
753 }
754
755 /*
756 * If there's too much stuff to fit in the yank register, then get a
757 * confirmation before doing the delete. This is crude, but simple.
758 * And it avoids doing a delete of something we can't put back if we
759 * want.
760 */
761 if (!did_yank)
762 {
763 int msg_silent_save = msg_silent;
764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100765 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
767 msg_silent = msg_silent_save;
768 if (n != 'y')
769 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000770 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 return FAIL;
772 }
773 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100774
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100775#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100776 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200777 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780
Bram Moolenaard04b7502010-07-08 22:27:55 +0200781 /*
782 * block mode delete
783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (oap->block_mode)
785 {
786 if (u_save((linenr_T)(oap->start.lnum - 1),
787 (linenr_T)(oap->end.lnum + 1)) == FAIL)
788 return FAIL;
789
790 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
791 {
792 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100793 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 continue;
795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100796 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 if (lnum == curwin->w_cursor.lnum)
798 {
799 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802
Bram Moolenaar8055d172019-05-17 22:57:26 +0200803 // "n" == number of chars deleted
804 // If we delete a TAB, it may be replaced by several characters.
805 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 n = bd.textlen - bd.startspaces - bd.endspaces;
807 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200808 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 if (newp == NULL)
810 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100811 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200814 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000818 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200821
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100822#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200823 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200824 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 }
827
828 check_cursor_col();
829 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
830 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100833 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 if (oap->op_type == OP_CHANGE)
836 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 // Delete the lines except the first one. Temporarily move the
838 // cursor to the next line. Save the current line number, if the
839 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (oap->line_count > 1)
841 {
842 lnum = curwin->w_cursor.lnum;
843 ++curwin->w_cursor.lnum;
844 del_lines((long)(oap->line_count - 1), TRUE);
845 curwin->w_cursor.lnum = lnum;
846 }
847 if (u_save_cursor() == FAIL)
848 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100849 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100851 beginline(BL_WHITE); // cursor on first non-white
852 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 ai_col = curwin->w_cursor.col;
854 }
855 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 beginline(0); // cursor in column 0
857 truncate_line(FALSE); // delete the rest of the line
858 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862 else
863 {
864 del_lines(oap->line_count, TRUE);
865 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100866 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868 }
869 else
870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (virtual_op)
872 {
873 int endcol = 0;
874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (gchar_pos(&oap->start) == '\t')
877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 return FAIL;
880 if (oap->line_count == 1)
881 endcol = getviscol2(oap->end.col, oap->end.coladd);
882 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
883 oap->start = curwin->w_cursor;
884 if (oap->line_count == 1)
885 {
886 coladvance(endcol);
887 oap->end.col = curwin->w_cursor.col;
888 oap->end.coladd = curwin->w_cursor.coladd;
889 curwin->w_cursor = oap->start;
890 }
891 }
892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (gchar_pos(&oap->end) == '\t'
895 && (int)oap->end.coladd < oap->inclusive)
896 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100897 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (u_save((linenr_T)(oap->end.lnum - 1),
899 (linenr_T)(oap->end.lnum + 1)) == FAIL)
900 return FAIL;
901 curwin->w_cursor = oap->end;
902 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
903 oap->end = curwin->w_cursor;
904 curwin->w_cursor = oap->start;
905 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100906 if (has_mbyte)
907 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100912 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 return FAIL;
914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100915 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100916 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 && oap->op_type == OP_CHANGE
918 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100919 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 display_dollar(oap->end.col - !oap->inclusive);
921
922 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 if (virtual_op)
925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100926 // fix up things for virtualedit-delete:
927 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 char_u *curline = ml_get_curline();
929 int len = (int)STRLEN(curline);
930
931 if (oap->end.coladd != 0
932 && (int)oap->end.col >= len - 1
933 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
934 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (n == 0 && oap->start.coladd != oap->end.coladd)
937 n = 1;
938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100939 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (gchar_cursor() != NUL)
941 curwin->w_cursor.coladd = 0;
942 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200943 (void)del_bytes((long)n, !virtual_op,
944 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 pos_T curpos;
949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100950 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
952 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
953 return FAIL;
954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100955 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 ++curwin->w_cursor.lnum;
959 del_lines((long)(oap->line_count - 2), FALSE);
960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200962 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200963 curwin->w_cursor.col = 0;
964 (void)del_bytes((long)n, !virtual_op,
965 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200967 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200969 if (oap->op_type == OP_DELETE)
970 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972
973 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
974
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000975setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200976 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100978 if (oap->block_mode)
979 {
980 curbuf->b_op_end.lnum = oap->end.lnum;
981 curbuf->b_op_end.col = oap->start.col;
982 }
983 else
984 curbuf->b_op_end = oap->start;
985 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 return OK;
989}
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991/*
992 * Adjust end of operating area for ending on a multi-byte character.
993 * Used for deletion.
994 */
995 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100996mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 char_u *p;
999
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001000 if (!oap->inclusive)
1001 return;
1002
1003 p = ml_get(oap->end.lnum);
1004 oap->end.col += mb_tail_off(p, p + oap->end.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaar630afe82018-06-28 19:26:28 +02001007/*
1008 * Replace the character under the cursor with "c".
1009 * This takes care of multi-byte characters.
1010 */
1011 static void
1012replace_character(int c)
1013{
1014 int n = State;
1015
Bram Moolenaar24959102022-05-07 20:01:16 +01001016 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001017 ins_char(c);
1018 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001019 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001020 dec_cursor();
1021}
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023/*
1024 * Replace a whole area with one character.
1025 */
1026 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001027op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 char_u *newp, *oldp;
1032 size_t oldlen;
1033 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001034 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001035 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001038 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001040 if (c == REPLACE_CR_NCHAR)
1041 {
1042 had_ctrl_v_cr = TRUE;
1043 c = CAR;
1044 }
1045 else if (c == REPLACE_NL_NCHAR)
1046 {
1047 had_ctrl_v_cr = TRUE;
1048 c = NL;
1049 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (has_mbyte)
1052 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
1054 if (u_save((linenr_T)(oap->start.lnum - 1),
1055 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1056 return FAIL;
1057
1058 /*
1059 * block mode replace
1060 */
1061 if (oap->block_mode)
1062 {
1063 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1064 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1065 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001066 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1068 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 // n == number of extra chars required
1072 // If we split a TAB, it may be replaced by several characters.
1073 // Thus the number of characters may increase!
1074 // If the range starts in virtual space, count the initial
1075 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1077 {
1078 pos_T vpos;
1079
Bram Moolenaara1381de2009-11-03 15:44:21 +00001080 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 getvpos(&vpos, oap->start_vcol);
1082 bd.startspaces += vpos.coladd;
1083 n = bd.startspaces;
1084 }
1085 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001086 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001089 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001093 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 numc = oap->end_vcol - oap->start_vcol + 1;
1095 if (bd.is_short && (!virtual_op || bd.is_MAX))
1096 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // A double-wide character can be replaced only up to half the
1099 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if ((*mb_char2cells)(c) > 1)
1101 {
1102 if ((numc & 1) && !bd.is_short)
1103 {
1104 ++bd.endspaces;
1105 ++n;
1106 }
1107 numc = numc / 2;
1108 }
1109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001110 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 num_chars = numc;
1112 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001113 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 n += numc - bd.textlen;
1115
1116 oldp = ml_get_curline();
1117 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001118 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (newp == NULL)
1120 continue;
1121 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001122 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 mch_memmove(newp, oldp, (size_t)bd.textcol);
1124 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001125 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001126 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001127 // insert replacement chars CHECK FOR ALLOCATED SPACE
1128 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1129 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001130 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001132 if (has_mbyte)
1133 {
1134 n = (int)STRLEN(newp);
1135 while (--num_chars >= 0)
1136 n += (*mb_char2bytes)(c, newp + n);
1137 }
1138 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001139 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001140 if (!bd.is_short)
1141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001142 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001143 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001144 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001145 STRMOVE(newp + STRLEN(newp), oldp);
1146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
1148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001150 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001151 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001152 if (after_p != NULL)
1153 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001155 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001157 if (after_p != NULL)
1158 {
1159 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1160 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1161 oap->end.lnum++;
1162 vim_free(after_p);
1163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 }
1166 else
1167 {
1168 /*
1169 * MCHAR and MLINE motion replace.
1170 */
1171 if (oap->motion_type == MLINE)
1172 {
1173 oap->start.col = 0;
1174 curwin->w_cursor.col = 0;
1175 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1176 if (oap->end.col)
1177 --oap->end.col;
1178 }
1179 else if (!oap->inclusive)
1180 dec(&(oap->end));
1181
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001182 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001184 int done = FALSE;
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 n = gchar_cursor();
1187 if (n != NUL)
1188 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001189 int new_byte_len = (*mb_char2len)(c);
1190 int old_byte_len = mb_ptr2len(ml_get_cursor());
1191
1192 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001194 // This is slow, but it handles replacing a single-byte
1195 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001196 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001197 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001198 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001199 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 }
1201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (n == TAB)
1204 {
1205 int end_vcol = 0;
1206
1207 if (curwin->w_cursor.lnum == oap->end.lnum)
1208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001209 // oap->end has to be recalculated when
1210 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 end_vcol = getviscol2(oap->end.col,
1212 oap->end.coladd);
1213 }
1214 coladvance_force(getviscol());
1215 if (curwin->w_cursor.lnum == oap->end.lnum)
1216 getvpos(&oap->end, end_vcol);
1217 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001218 // with "coladd" set may move to just after a TAB
1219 if (gchar_cursor() != NUL)
1220 {
1221 PBYTE(curwin->w_cursor, c);
1222 done = TRUE;
1223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001226 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
1228 int virtcols = oap->end.coladd;
1229
1230 if (curwin->w_cursor.lnum == oap->start.lnum
1231 && oap->start.col == oap->end.col && oap->start.coladd)
1232 virtcols -= oap->start.coladd;
1233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001234 // oap->end has been trimmed so it's effectively inclusive;
1235 // as a result an extra +1 must be counted so we don't
1236 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1238 curwin->w_cursor.col -= (virtcols + 1);
1239 for (; virtcols >= 0; virtcols--)
1240 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001241 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001242 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001243 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001244 PBYTE(curwin->w_cursor, c);
1245 if (inc(&curwin->w_cursor) == -1)
1246 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (inc_cursor() == -1)
1252 break;
1253 }
1254 }
1255
1256 curwin->w_cursor = oap->start;
1257 check_cursor();
1258 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1259
Bram Moolenaare1004402020-10-24 20:49:43 +02001260 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001261 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001262 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001263 curbuf->b_op_start = oap->start;
1264 curbuf->b_op_end = oap->end;
1265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267 return OK;
1268}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001270static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272/*
1273 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1274 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001276op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001280 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 if (u_save((linenr_T)(oap->start.lnum - 1),
1283 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1284 return;
1285
1286 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001287 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
1289 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1290 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001291 int one_change;
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 block_prep(oap, &bd, pos.lnum, FALSE);
1294 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001295 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1296 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001297
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001298#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001299 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001301 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302
Bram Moolenaar009b2592004-10-24 19:18:58 +00001303 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1304 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001305 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001306 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001308 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (did_change)
1313 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1314 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001315 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 if (oap->motion_type == MLINE)
1318 {
1319 oap->start.col = 0;
1320 pos.col = 0;
1321 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1322 if (oap->end.col)
1323 --oap->end.col;
1324 }
1325 else if (!oap->inclusive)
1326 dec(&(oap->end));
1327
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001328 if (pos.lnum == oap->end.lnum)
1329 did_change = swapchars(oap->op_type, &pos,
1330 oap->end.col - pos.col + 1);
1331 else
1332 for (;;)
1333 {
1334 did_change |= swapchars(oap->op_type, &pos,
1335 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1336 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001337 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001338 break;
1339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (did_change)
1341 {
1342 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1343 0L);
1344#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001345 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
1347 char_u *ptr;
1348 int count;
1349
1350 pos = oap->start;
1351 while (pos.lnum < oap->end.lnum)
1352 {
1353 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001354 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001355 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001356 // get the line again, it may have been flushed
1357 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001359 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 pos.col = 0;
1361 pos.lnum++;
1362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001364 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001365 // get the line again, it may have been flushed
1366 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001368 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370#endif
1371 }
1372 }
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001375 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001376 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
Bram Moolenaare1004402020-10-24 20:49:43 +02001378 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001379 {
1380 // Set '[ and '] marks.
1381 curbuf->b_op_start = oap->start;
1382 curbuf->b_op_end = oap->end;
1383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001386 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001387 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388}
1389
1390/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001391 * Invoke swapchar() on "length" bytes at position "pos".
1392 * "pos" is advanced to just after the changed characters.
1393 * "length" is rounded up to include the whole last multi-byte character.
1394 * Also works correctly when the number of bytes changes.
1395 * Returns TRUE if some character was changed.
1396 */
1397 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001398swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001399{
1400 int todo;
1401 int did_change = 0;
1402
1403 for (todo = length; todo > 0; --todo)
1404 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001405 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001406 {
1407 int len = (*mb_ptr2len)(ml_get_pos(pos));
1408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001409 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001410 if (len > 0)
1411 todo -= len - 1;
1412 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001413 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001414 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001415 break;
1416 }
1417 return did_change;
1418}
1419
1420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 * If op_type == OP_UPPER: make uppercase,
1422 * if op_type == OP_LOWER: make lowercase,
1423 * if op_type == OP_ROT13: do rot13 encoding,
1424 * else swap case of character at 'pos'
1425 * returns TRUE when something actually changed.
1426 */
1427 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001428swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430 int c;
1431 int nc;
1432
1433 c = gchar_pos(pos);
1434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001435 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (c >= 0x80 && op_type == OP_ROT13)
1437 return FALSE;
1438
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001439 if (op_type == OP_UPPER && c == 0xdf
1440 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001441 {
1442 pos_T sp = curwin->w_cursor;
1443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001444 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001445 curwin->w_cursor = *pos;
1446 del_char(FALSE);
1447 ins_char('S');
1448 ins_char('S');
1449 curwin->w_cursor = sp;
1450 inc(pos);
1451 }
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 nc = c;
1456 if (MB_ISLOWER(c))
1457 {
1458 if (op_type == OP_ROT13)
1459 nc = ROT13(c, 'a');
1460 else if (op_type != OP_LOWER)
1461 nc = MB_TOUPPER(c);
1462 }
1463 else if (MB_ISUPPER(c))
1464 {
1465 if (op_type == OP_ROT13)
1466 nc = ROT13(c, 'A');
1467 else if (op_type != OP_UPPER)
1468 nc = MB_TOLOWER(c);
1469 }
1470 if (nc != c)
1471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1473 {
1474 pos_T sp = curwin->w_cursor;
1475
1476 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001477 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001478 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ins_char(nc);
1480 curwin->w_cursor = sp;
1481 }
1482 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001483 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 return TRUE;
1485 }
1486 return FALSE;
1487}
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489/*
1490 * op_insert - Insert and append operators for Visual mode.
1491 */
1492 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001493op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 long ins_len, pre_textlen = 0;
1496 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001497 colnr_T ind_pre_col = 0, ind_post_col;
1498 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 struct block_def bd;
1500 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001501 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001502 pos_T start_insert;
1503 // offset when cursor was moved in insert mode
1504 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001506 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1508
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001509 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001511 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
1513 if (oap->block_mode)
1514 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001515 // When 'virtualedit' is used, need to insert the extra spaces before
1516 // doing block_prep(). When only "block" is used, virtual edit is
1517 // already disabled, but still need it when calling
1518 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001519 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001520 // state for the current window. To override that state, we need to
1521 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (curwin->w_cursor.coladd > 0)
1523 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001524 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (u_save_cursor() == FAIL)
1527 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001528
Gary Johnson51ad8502021-08-03 18:33:08 +02001529 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 coladvance_force(oap->op_type == OP_APPEND
1531 ? oap->end_vcol + 1 : getviscol());
1532 if (oap->op_type == OP_APPEND)
1533 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001534 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001536 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001538 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001539 ind_pre_col = (colnr_T)getwhitecols_curline();
1540 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if (oap->op_type == OP_APPEND)
1544 firstline += bd.textlen;
1545 pre_textlen = (long)STRLEN(firstline);
1546 }
1547
1548 if (oap->op_type == OP_APPEND)
1549 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001550 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001552 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 curwin->w_set_curswant = TRUE;
1554 while (*ml_get_cursor() != NUL
1555 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1556 ++curwin->w_cursor.col;
1557 if (bd.is_short && !bd.is_MAX)
1558 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001559 // First line was too short, make it longer and adjust the
1560 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (u_save_cursor() == FAIL)
1562 return;
1563 for (i = 0; i < bd.endspaces; ++i)
1564 ins_char(' ');
1565 bd.textlen += bd.endspaces;
1566 }
1567 }
1568 else
1569 {
1570 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001571 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001573 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001574 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 && oap->start_vcol != oap->end_vcol)
1576 inc_cursor();
1577 }
1578 }
1579
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001580 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001581 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001582 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001584 // When a tab was inserted, and the characters in front of the tab
1585 // have been converted to a tab as well, the column of the cursor
1586 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001587 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001588 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001589 oap->start = curbuf->b_op_start_orig;
1590
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591 // If user has moved off this line, we don't know what to do, so do
1592 // nothing.
1593 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001594 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 return;
1596
1597 if (oap->block_mode)
1598 {
1599 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001600 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001601 size_t len;
1602 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001604 // If indent kicked in, the firstline might have changed
1605 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001606 ind_post_col = (colnr_T)getwhitecols_curline();
1607 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001608 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001609 bd.textcol += ind_post_col - ind_pre_col;
1610 ind_post_vcol = get_indent();
1611 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001612 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001613 }
1614
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001615 // The user may have moved the cursor before inserting something, try
1616 // to adjust the block for that. But only do it, if the difference
1617 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001618 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1619 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001620 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001621 int t = getviscol2(curbuf->b_op_start_orig.col,
1622 curbuf->b_op_start_orig.coladd);
1623
zeertzjq7745f142022-02-15 11:48:22 +00001624 if (oap->op_type == OP_INSERT
1625 && oap->start.col + oap->start.coladd
1626 != curbuf->b_op_start_orig.col
1627 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001628 {
zeertzjq7745f142022-02-15 11:48:22 +00001629 oap->start.col = curbuf->b_op_start_orig.col;
1630 pre_textlen -= t - oap->start_vcol;
1631 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001632 }
zeertzjq7745f142022-02-15 11:48:22 +00001633 else if (oap->op_type == OP_APPEND
1634 && oap->start.col + oap->start.coladd
1635 >= curbuf->b_op_start_orig.col
1636 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001637 {
zeertzjq7745f142022-02-15 11:48:22 +00001638 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001639 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001640 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001641 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001642 oap->start_vcol = t;
1643 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001644 }
1645 }
1646
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001647 // Spaces and tabs in the indent may have changed to other spaces and
1648 // tabs. Get the starting column again and correct the length.
1649 // Don't do this when "$" used, end-of-line will have changed.
1650 //
1651 // if indent was added and the inserted text was after the indent,
1652 // correct the selection for the new indent.
1653 if (did_indent && bd.textcol - ind_post_col > 0)
1654 {
1655 oap->start.col += ind_post_col - ind_pre_col;
1656 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1657 oap->end.col += ind_post_col - ind_pre_col;
1658 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001661 if (did_indent && bd.textcol - ind_post_col > 0)
1662 {
1663 // undo for where "oap" is used below
1664 oap->start.col -= ind_post_col - ind_pre_col;
1665 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1666 oap->end.col -= ind_post_col - ind_pre_col;
1667 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1670 {
1671 if (oap->op_type == OP_APPEND)
1672 {
1673 pre_textlen += bd2.textlen - bd.textlen;
1674 if (bd2.endspaces)
1675 --bd2.textlen;
1676 }
1677 bd.textcol = bd2.textcol;
1678 bd.textlen = bd2.textlen;
1679 }
1680
1681 /*
1682 * Subsequent calls to ml_get() flush the firstline data - take a
1683 * copy of the required string.
1684 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001685 firstline = ml_get(oap->start.lnum);
1686 len = STRLEN(firstline);
1687 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001689 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001690 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001691 // account for pressing cursor in insert mode when '$' was used
1692 if (bd.is_MAX
1693 && (start_insert.lnum == Insstart.lnum
1694 && start_insert.col > Insstart.col))
1695 {
1696 offset = (start_insert.col - Insstart.col);
1697 add -= offset;
1698 if (oap->end_vcol > offset)
1699 oap->end_vcol -= (offset + 1);
1700 else
1701 // moved outside of the visual block, what to do?
1702 return;
1703 }
1704 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001705 if ((size_t)add > len)
1706 firstline += len; // short line, point to the NUL
1707 else
1708 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001709 if (pre_textlen >= 0 && (ins_len =
1710 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001712 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (ins_text != NULL)
1714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001715 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (u_save(oap->start.lnum,
1717 (linenr_T)(oap->end.lnum + 1)) == OK)
1718 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1719 &bd);
1720
1721 curwin->w_cursor.col = oap->start.col;
1722 check_cursor();
1723 vim_free(ins_text);
1724 }
1725 }
1726 }
1727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729/*
1730 * op_change - handle a change operation
1731 *
1732 * return TRUE if edit() returns because of a CTRL-O command
1733 */
1734 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001735op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 colnr_T l;
1738 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 long offset;
1740 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001741 long ins_len;
1742 long pre_textlen = 0;
1743 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 char_u *firstline;
1745 char_u *ins_text, *newp, *oldp;
1746 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748 l = oap->start.col;
1749 if (oap->motion_type == MLINE)
1750 {
1751 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001752 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001755 // First delete the text in the region. In an empty buffer only need to
1756 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1758 {
1759 if (u_save_cursor() == FAIL)
1760 return FALSE;
1761 }
1762 else if (op_delete(oap) == FAIL)
1763 return FALSE;
1764
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001765 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 && !virtual_op)
1767 inc_cursor();
1768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001769 // check for still on same line (<CR> in inserted text meaningless)
1770 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (oap->block_mode)
1772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001773 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (virtual_op && (curwin->w_cursor.coladd > 0
1775 || gchar_cursor() == NUL))
1776 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001777 firstline = ml_get(oap->start.lnum);
1778 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001779 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 bd.textcol = curwin->w_cursor.col;
1781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (oap->motion_type == MLINE)
1784 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
zeertzjqcdeb6572022-11-15 13:46:12 +00001786 // Reset finish_op now, don't want it set inside edit().
1787 int save_finish_op = finish_op;
1788 finish_op = FALSE;
1789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = edit(NUL, FALSE, (linenr_T)1);
1791
zeertzjqcdeb6572022-11-15 13:46:12 +00001792 finish_op = save_finish_op;
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001795 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001797 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001799 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001801 // Auto-indenting may have changed the indent. If the cursor was past
1802 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001804 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001806 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001807
1808 pre_textlen += new_indent - pre_indent;
1809 bd.textcol += new_indent - pre_indent;
1810 }
1811
1812 ins_len = (long)STRLEN(firstline) - pre_textlen;
1813 if (ins_len > 0)
1814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001815 // Subsequent calls to ml_get() flush the firstline data - take a
1816 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001817 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001819 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1821 linenr++)
1822 {
1823 block_prep(oap, &bd, linenr, TRUE);
1824 if (!bd.is_short || virtual_op)
1825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 pos_T vpos;
1827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001828 // If the block starts in virtual space, count the
1829 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if (bd.is_short)
1831 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001832 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
1835 else
1836 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001838 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (newp == NULL)
1840 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001841 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 mch_memmove(newp, oldp, (size_t)bd.textcol);
1843 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001844 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1847 offset += ins_len;
1848 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001849 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001851#ifdef FEAT_PROP_POPUP
1852 // Shift the properties for linenr as edit() would do.
1853 if (curbuf->b_has_textprop)
1854 adjust_prop_columns(linenr, bd.textcol,
1855 vpos.coladd + ins_len, 0);
1856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 }
1859 check_cursor();
1860
1861 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1862 }
1863 vim_free(ins_text);
1864 }
1865 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001866 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 return retval;
1869}
1870
1871/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001872 * When the cursor is on the NUL past the end of the line and it should not be
1873 * there move it left.
1874 */
1875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001876adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001877{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001878 unsigned int cur_ve_flags = get_ve_flags();
1879
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001880 int adj_cursor = (curwin->w_cursor.col > 0
1881 && gchar_cursor() == NUL
1882 && (cur_ve_flags & VE_ONEMORE) == 0
1883 && !(restart_edit || (State & MODE_INSERT)));
1884 if (!adj_cursor)
1885 return;
1886
1887 // Put the cursor on the last character in the line.
1888 dec_cursor();
1889
1890 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001892 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001893
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001894 // Coladd is set to the width of the last character.
1895 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1896 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898}
1899
Bram Moolenaar81340392012-06-06 16:12:59 +02001900/*
1901 * If "process" is TRUE and the line begins with a comment leader (possibly
1902 * after some white space), return a pointer to the text after it. Put a boolean
1903 * value indicating whether the line ends with an unclosed comment in
1904 * "is_comment".
1905 * line - line to be processed,
1906 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001907 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001908 * include_space - whether to also skip space following the comment leader,
1909 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001910 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001911 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001912 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001913skip_comment(
1914 char_u *line,
1915 int process,
1916 int include_space,
1917 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001918{
1919 char_u *comment_flags = NULL;
1920 int lead_len;
1921 int leader_offset = get_last_leader_offset(line, &comment_flags);
1922
1923 *is_comment = FALSE;
1924 if (leader_offset != -1)
1925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001926 // Let's check whether the line ends with an unclosed comment.
1927 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001928 while (*comment_flags)
1929 {
1930 if (*comment_flags == COM_END
1931 || *comment_flags == ':')
1932 break;
1933 ++comment_flags;
1934 }
1935 if (*comment_flags != COM_END)
1936 *is_comment = TRUE;
1937 }
1938
1939 if (process == FALSE)
1940 return line;
1941
1942 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1943
1944 if (lead_len == 0)
1945 return line;
1946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // Find:
1948 // - COM_END,
1949 // - colon,
1950 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001951 while (*comment_flags)
1952 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001953 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001954 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001955 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001956 ++comment_flags;
1957 }
1958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001959 // If we found a colon, it means that we are not processing a line
1960 // starting with a closing part of a three-part comment. That's good,
1961 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001962 if (*comment_flags == ':' || *comment_flags == NUL)
1963 line += lead_len;
1964
1965 return line;
1966}
Bram Moolenaar81340392012-06-06 16:12:59 +02001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001969 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001970 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001971 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1972 * leaders should not be removed.
1973 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1974 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001976 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
1978 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001979do_join(
1980 long count,
1981 int insert_space,
1982 int save_undo,
1983 int use_formatoptions UNUSED,
1984 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001986 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001987 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001988 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001990 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001991 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001992 int endcurr1 = NUL;
1993 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001994 int currsize = 0; // size of the current line
1995 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001997 colnr_T col = 0;
1998 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001999 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002000 int remove_comments = (use_formatoptions == TRUE)
2001 && has_format_option(FO_REMOVE_COMS);
2002 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002003#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002004 int propcount = 0; // number of props over all joined lines
2005 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002008 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2009 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2010 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002012 // Allocate an array to store the number of spaces inserted before each
2013 // line. We will use it to pre-compute the length of the new line and the
2014 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002015 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002016 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002018 if (remove_comments)
2019 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002020 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002021 if (comments == NULL)
2022 {
2023 vim_free(spaces);
2024 return FAIL;
2025 }
2026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002029 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002030 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002031 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002033 for (t = 0; t < count; ++t)
2034 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002035 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002036#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002037 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2038 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002039#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002040 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002042 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002043 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2044 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2045 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002046 if (remove_comments)
2047 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002048 // We don't want to remove the comment leader if the
2049 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002050 if (t > 0 && prev_was_comment)
2051 {
2052
2053 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2054 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002055 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002056 curr = new_curr;
2057 }
2058 else
2059 curr = skip_comment(curr, FALSE, insert_space,
2060 &prev_was_comment);
2061 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002062
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002063 if (insert_space && t > 0)
2064 {
2065 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002066 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002067 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002068 && (!has_format_option(FO_MBYTE_JOIN)
2069 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2070 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002071 || (mb_ptr2char(curr) < 0x100
2072 && !(enc_utf8 && utf_eat_space(endcurr1)))
2073 || (endcurr1 < 0x100
2074 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002075 )
2076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002077 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002078 if (endcurr1 == ' ')
2079 endcurr1 = endcurr2;
2080 else
2081 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002082 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002083 if ( p_js
2084 && (endcurr1 == '.'
2085 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2086 && (endcurr1 == '?' || endcurr1 == '!'))))
2087 ++spaces[t];
2088 }
2089 }
2090 currsize = (int)STRLEN(curr);
2091 sumsize += currsize + spaces[t];
2092 endcurr1 = endcurr2 = NUL;
2093 if (insert_space && currsize > 0)
2094 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002095 if (has_mbyte)
2096 {
2097 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002098 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002099 endcurr1 = (*mb_ptr2char)(cend);
2100 if (cend > curr)
2101 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002102 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002103 endcurr2 = (*mb_ptr2char)(cend);
2104 }
2105 }
2106 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002107 {
2108 endcurr1 = *(curr + currsize - 1);
2109 if (currsize > 1)
2110 endcurr2 = *(curr + currsize - 2);
2111 }
2112 }
2113 line_breakcheck();
2114 if (got_int)
2115 {
2116 ret = FAIL;
2117 goto theend;
2118 }
2119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002121 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002122 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002124 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002125 newp_len = sumsize + 1;
2126#ifdef FEAT_PROP_POPUP
2127 newp_len += propcount * sizeof(textprop_T);
2128#endif
2129 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002130 if (newp == NULL)
2131 {
2132 ret = FAIL;
2133 goto theend;
2134 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002135 cend = newp + sumsize;
2136 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002138 /*
2139 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002140 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002141 *
2142 * Move marks from each deleted line to the joined line, adjusting the
2143 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2144 * should not really be a problem.
2145 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002146#ifdef FEAT_PROP_POPUP
2147 props_remaining = propcount;
2148#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002149 for (t = count - 1; ; --t)
2150 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002151 int spaces_removed;
2152
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002153 cend -= currsize;
2154 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002155
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002156 if (spaces[t] > 0)
2157 {
2158 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002159 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002160 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002161
2162 // If deleting more spaces than adding, the cursor moves no more than
2163 // what is added if it is inside these spaces.
2164 spaces_removed = (curr - curr_start) - spaces[t];
2165
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002166 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002167 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002168#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002169 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2170 curwin->w_cursor.lnum + t, t == count - 1,
2171 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002172#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002173 if (t == 0)
2174 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002175 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002176 if (remove_comments)
2177 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002178 if (insert_space && t > 1)
2179 curr = skipwhite(curr);
2180 currsize = (int)STRLEN(curr);
2181 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002182
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002183 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
Bram Moolenaare1004402020-10-24 20:49:43 +02002185 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002186 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002187 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002188 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002189 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002190 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002192 // Only report the change in the first line here, del_lines() will report
2193 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 changed_lines(curwin->w_cursor.lnum, currsize,
2195 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002197 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 * briefly, and then move it back. After del_lines() the cursor may
2199 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 */
2201 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002203 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 curwin->w_cursor.lnum = t;
2205
2206 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002207 * Set the cursor column:
2208 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002209 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002211 curwin->w_cursor.col =
2212 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 curwin->w_set_curswant = TRUE;
2217
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002218theend:
2219 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002220 if (remove_comments)
2221 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002222 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002225#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002227 * Reset 'linebreak' and take care of side effects.
2228 * Returns the previous value, to be passed to restore_lbr().
2229 */
2230 static int
2231reset_lbr(void)
2232{
2233 if (!curwin->w_p_lbr)
2234 return FALSE;
2235 // changing 'linebreak' may require w_virtcol to be updated
2236 curwin->w_p_lbr = FALSE;
2237 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2238 return TRUE;
2239}
2240
2241/*
2242 * Restore 'linebreak' and take care of side effects.
2243 */
2244 static void
2245restore_lbr(int lbr_saved)
2246{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002247 if (curwin->w_p_lbr || !lbr_saved)
2248 return;
2249
2250 // changing 'linebreak' may require w_virtcol to be updated
2251 curwin->w_p_lbr = TRUE;
2252 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002253}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002254#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002255
2256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * prepare a few things for block mode yank/delete/tilde
2258 *
2259 * for delete:
2260 * - textlen includes the first/last char to be (partly) deleted
2261 * - start/endspaces is the number of columns that are taken by the
2262 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002263 * deleted.
2264 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 * - textlen includes the first/last char to be wholly yanked
2266 * - start/endspaces is the number of columns of the first/last yanked char
2267 * that are to be yanked.
2268 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002269 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002270block_prep(
2271 oparg_T *oap,
2272 struct block_def *bdp,
2273 linenr_T lnum,
2274 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
2276 int incr = 0;
2277 char_u *pend;
2278 char_u *pstart;
2279 char_u *line;
2280 char_u *prev_pstart;
2281 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002282 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002283#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002284 // Avoid a problem with unwanted linebreaks in block mode.
2285 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002286#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002287
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 bdp->startspaces = 0;
2289 bdp->endspaces = 0;
2290 bdp->textlen = 0;
2291 bdp->start_vcol = 0;
2292 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 bdp->is_short = FALSE;
2294 bdp->is_oneChar = FALSE;
2295 bdp->pre_whitesp = 0;
2296 bdp->pre_whitesp_c = 0;
2297 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 bdp->start_char_vcols = 0;
2299
2300 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002302 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2303 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002305 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002306 incr = lbr_chartabsize(&cts);
2307 cts.cts_vcol += incr;
2308 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 bdp->pre_whitesp += incr;
2311 bdp->pre_whitesp_c++;
2312 }
2313 else
2314 {
2315 bdp->pre_whitesp = 0;
2316 bdp->pre_whitesp_c = 0;
2317 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002318 prev_pstart = cts.cts_ptr;
2319 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002321 bdp->start_vcol = cts.cts_vcol;
2322 pstart = cts.cts_ptr;
2323 clear_chartabsize_arg(&cts);
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002326 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (!is_del || oap->op_type == OP_APPEND)
2331 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2332 }
2333 else
2334 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002335 // notice: this converts partly selected Multibyte characters to
2336 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2338 if (is_del && bdp->startspaces)
2339 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2340 pend = pstart;
2341 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (oap->op_type == OP_INSERT)
2346 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2347 else if (oap->op_type == OP_APPEND)
2348 {
2349 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2350 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2351 }
2352 else
2353 {
2354 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2355 if (is_del && oap->op_type != OP_LSHIFT)
2356 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002357 // just putting the sum of those two into
2358 // bdp->startspaces doesn't work for Visual replace,
2359 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 bdp->startspaces = bdp->start_char_vcols
2361 - (bdp->start_vcol - oap->start_vcol);
2362 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2363 }
2364 }
2365 }
2366 else
2367 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002368 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2369 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002371 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002373 // count a tab for what it's worth (if list mode not on)
2374 prev_pend = cts.cts_ptr;
2375 incr = lbr_chartabsize_adv(&cts);
2376 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002378 bdp->end_vcol = cts.cts_vcol;
2379 pend = cts.cts_ptr;
2380 clear_chartabsize_arg(&cts);
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (bdp->end_vcol <= oap->end_vcol
2383 && (!is_del
2384 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002385 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // Alternative: include spaces to fill up the block.
2389 // Disadvantage: can lead to trailing spaces when the line is
2390 // short where the text is put
2391 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (oap->op_type == OP_APPEND || virtual_op)
2393 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002394 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002396 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398 else if (bdp->end_vcol > oap->end_vcol)
2399 {
2400 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2401 if (!is_del && bdp->endspaces)
2402 {
2403 bdp->endspaces = incr - bdp->endspaces;
2404 if (pend != pstart)
2405 pend = prev_pend;
2406 }
2407 }
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (is_del && bdp->startspaces)
2411 pstart = prev_pstart;
2412 bdp->textlen = (int)(pend - pstart);
2413 }
2414 bdp->textcol = (colnr_T) (pstart - line);
2415 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002416#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002417 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002422 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002424 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002425op_addsub(
2426 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002427 linenr_T Prenum1, // Amount of add/subtract
2428 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002430 pos_T pos;
2431 struct block_def bd;
2432 int change_cnt = 0;
2433 linenr_T amount = Prenum1;
2434
Bram Moolenaar6b731882018-11-16 20:54:47 +01002435 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002436 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002437 // the call to changed_lines().
2438#ifdef FEAT_FOLDING
2439 disable_fold_update++;
2440#endif
2441
Bram Moolenaard79e5502016-01-10 22:13:02 +01002442 if (!VIsual_active)
2443 {
2444 pos = curwin->w_cursor;
2445 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002446 {
2447#ifdef FEAT_FOLDING
2448 disable_fold_update--;
2449#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002450 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002451 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002452 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002453#ifdef FEAT_FOLDING
2454 disable_fold_update--;
2455#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002456 if (change_cnt)
2457 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2458 }
2459 else
2460 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002461 int one_change;
2462 int length;
2463 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002464
2465 if (u_save((linenr_T)(oap->start.lnum - 1),
2466 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002467 {
2468#ifdef FEAT_FOLDING
2469 disable_fold_update--;
2470#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002471 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002472 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002473
2474 pos = oap->start;
2475 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002477 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002478 {
2479 block_prep(oap, &bd, pos.lnum, FALSE);
2480 pos.col = bd.textcol;
2481 length = bd.textlen;
2482 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002483 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002484 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002485 curwin->w_cursor.col = 0;
2486 pos.col = 0;
2487 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2488 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002489 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002490 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002491 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002492 dec(&(oap->end));
2493 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2494 pos.col = 0;
2495 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002496 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002497 pos.col += oap->start.col;
2498 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002499 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002500 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002501 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002502 length = (int)STRLEN(ml_get(oap->end.lnum));
2503 if (oap->end.col >= length)
2504 oap->end.col = length - 1;
2505 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002506 }
2507 }
2508 one_change = do_addsub(oap->op_type, &pos, length, amount);
2509 if (one_change)
2510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002511 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002512 if (change_cnt == 0)
2513 startpos = curbuf->b_op_start;
2514 ++change_cnt;
2515 }
2516
2517#ifdef FEAT_NETBEANS_INTG
2518 if (netbeans_active() && one_change)
2519 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002520 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002521
2522 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002523 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002524 netbeans_inserted(curbuf, pos.lnum, pos.col,
2525 &ptr[pos.col], length);
2526 }
2527#endif
2528 if (g_cmd && one_change)
2529 amount += Prenum1;
2530 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002531
2532#ifdef FEAT_FOLDING
2533 disable_fold_update--;
2534#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002535 if (change_cnt)
2536 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2537
2538 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002540 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002542 // Set '[ mark if something changed. Keep the last end
2543 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002544 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002545 curbuf->b_op_start = startpos;
2546
2547 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002548 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002549 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002550 }
2551}
2552
2553/*
2554 * Add or subtract 'Prenum1' from a number in a line
2555 * op_type is OP_NR_ADD or OP_NR_SUB
2556 *
2557 * Returns TRUE if some character was changed.
2558 */
2559 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002560do_addsub(
2561 int op_type,
2562 pos_T *pos,
2563 int length,
2564 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002565{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 int col;
2567 char_u *buf1;
2568 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002569 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2570 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002571 uvarnumber_T n;
2572 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 char_u *ptr;
2574 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002576 int do_hex;
2577 int do_oct;
2578 int do_bin;
2579 int do_alpha;
2580 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002583 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002584 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002585 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002586 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002587 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002588 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002589 pos_T startpos;
2590 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002591 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002593 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2594 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2595 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2596 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2597 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002599 if (virtual_active())
2600 {
2601 save_coladd = pos->coladd;
2602 pos->coladd = 0;
2603 }
2604
Bram Moolenaard79e5502016-01-10 22:13:02 +01002605 curwin->w_cursor = *pos;
2606 ptr = ml_get(pos->lnum);
2607 col = pos->col;
2608
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002609 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002610 goto theend;
2611
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 /*
2613 * First check if we are on a hexadecimal number, after the "0x".
2614 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002615 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002617 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002618 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002619 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002620 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002621 if (has_mbyte)
2622 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002623 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002624
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002625 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002626 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002627 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002628 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002629 if (has_mbyte)
2630 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002631 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002632
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002633 if ( do_bin
2634 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002635 && ! ((col > 0
2636 && (ptr[col] == 'X'
2637 || ptr[col] == 'x')
2638 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002639 && (!has_mbyte ||
2640 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002641 && vim_isxdigit(ptr[col + 1]))))
2642 {
2643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002644 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002645
Bram Moolenaard79e5502016-01-10 22:13:02 +01002646 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002647
2648 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002649 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002650 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002651 if (has_mbyte)
2652 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002653 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002654 }
2655
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002656 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002657 && col > 0
2658 && (ptr[col] == 'X'
2659 || ptr[col] == 'x')
2660 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002661 && (!has_mbyte ||
2662 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002663 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002664 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002665 && col > 0
2666 && (ptr[col] == 'B'
2667 || ptr[col] == 'b')
2668 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002669 && (!has_mbyte ||
2670 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002671 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002672 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002673 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002674 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002675 if (has_mbyte)
2676 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002677 }
2678 else
2679 {
2680 /*
2681 * Search forward and then backward to find the start of number.
2682 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002683 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002684
2685 while (ptr[col] != NUL
2686 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002687 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002688 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002689
2690 while (col > 0
2691 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002692 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002693 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002694 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002695 if (has_mbyte)
2696 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002697 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002698 }
2699 }
2700
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002701 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002702 {
2703 while (ptr[col] != NUL && length > 0
2704 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002705 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002706 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002707 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002708
2709 col += mb_len;
2710 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002711 }
2712
2713 if (length == 0)
2714 goto theend;
2715
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002716 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002717 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2718 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002719 {
2720 negative = TRUE;
2721 was_positive = FALSE;
2722 }
2723 }
2724
2725 /*
2726 * If a number was found, and saving for undo works, replace the number.
2727 */
2728 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002729 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002730 {
2731 beep_flush();
2732 goto theend;
2733 }
2734
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002735 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002738 if (op_type == OP_NR_SUB)
2739 {
2740 if (CharOrd(firstdigit) < Prenum1)
2741 {
2742 if (isupper(firstdigit))
2743 firstdigit = 'A';
2744 else
2745 firstdigit = 'a';
2746 }
2747 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002748 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002749 }
2750 else
2751 {
2752 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2753 {
2754 if (isupper(firstdigit))
2755 firstdigit = 'Z';
2756 else
2757 firstdigit = 'z';
2758 }
2759 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002760 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002761 }
2762 curwin->w_cursor.col = col;
2763 if (!did_change)
2764 startpos = curwin->w_cursor;
2765 did_change = TRUE;
2766 (void)del_char(FALSE);
2767 ins_char(firstdigit);
2768 endpos = curwin->w_cursor;
2769 curwin->w_cursor.col = col;
2770 }
2771 else
2772 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002773 pos_T save_pos;
2774 int i;
2775
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002776 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002777 && (!has_mbyte ||
2778 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002779 && !visual
2780 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002781 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002782 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002783 --col;
2784 negative = TRUE;
2785 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002787 if (visual && VIsual_mode != 'V')
2788 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2789 ? (int)STRLEN(ptr) - col
2790 : length);
2791
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002792 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002793 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002794 0 + (do_bin ? STR2NR_BIN : 0)
2795 + (do_oct ? STR2NR_OCT : 0)
2796 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002797 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002800 if (pre && negative)
2801 {
2802 ++col;
2803 --length;
2804 negative = FALSE;
2805 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002807 subtract = FALSE;
2808 if (op_type == OP_NR_SUB)
2809 subtract ^= TRUE;
2810 if (negative)
2811 subtract ^= TRUE;
2812
2813 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002814 if (!overflow) // if number is too big don't add/subtract
2815 {
2816 if (subtract)
2817 n -= (uvarnumber_T)Prenum1;
2818 else
2819 n += (uvarnumber_T)Prenum1;
2820 }
2821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002822 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002823 if (!pre)
2824 {
2825 if (subtract)
2826 {
2827 if (n > oldn)
2828 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002829 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002830 negative ^= TRUE;
2831 }
2832 }
2833 else
2834 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002836 if (n < oldn)
2837 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002838 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002839 negative ^= TRUE;
2840 }
2841 }
2842 if (n == 0)
2843 negative = FALSE;
2844 }
2845
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002846 if (do_unsigned && negative)
2847 {
2848 if (subtract)
2849 // sticking at zero.
2850 n = (uvarnumber_T)0;
2851 else
2852 // sticking at 2^64 - 1.
2853 n = (uvarnumber_T)(-1);
2854 negative = FALSE;
2855 }
2856
Bram Moolenaard79e5502016-01-10 22:13:02 +01002857 if (visual && !was_positive && !negative && col > 0)
2858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002860 col--;
2861 length++;
2862 }
2863
2864 /*
2865 * Delete the old number.
2866 */
2867 curwin->w_cursor.col = col;
2868 if (!did_change)
2869 startpos = curwin->w_cursor;
2870 did_change = TRUE;
2871 todel = length;
2872 c = gchar_cursor();
2873 /*
2874 * Don't include the '-' in the length, only the length of the
2875 * part after it is kept the same.
2876 */
2877 if (c == '-')
2878 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002879
2880 save_pos = curwin->w_cursor;
2881 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002882 {
2883 if (c < 0x100 && isalpha(c))
2884 {
2885 if (isupper(c))
2886 hexupper = TRUE;
2887 else
2888 hexupper = FALSE;
2889 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002890 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002891 c = gchar_cursor();
2892 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002893 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002894
2895 /*
2896 * Prepare the leading characters in buf1[].
2897 * When there are many leading zeros it could be very long.
2898 * Allocate a bit too much.
2899 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002900 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002901 if (buf1 == NULL)
2902 goto theend;
2903 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002904 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002905 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002906 if (pre)
2907 {
2908 *ptr++ = '0';
2909 --length;
2910 }
2911 if (pre == 'b' || pre == 'B' ||
2912 pre == 'x' || pre == 'X')
2913 {
2914 *ptr++ = pre;
2915 --length;
2916 }
2917
2918 /*
2919 * Put the number characters in buf2[].
2920 */
2921 if (pre == 'b' || pre == 'B')
2922 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002923 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002924 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002927 for (bit = bits; bit > 0; bit--)
2928 if ((n >> (bit - 1)) & 0x1) break;
2929
Christian Brabandt889f6af2023-09-02 19:43:33 +02002930 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002931 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2932
2933 buf2[i] = '\0';
2934 }
2935 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002936 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002937 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002938 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002939 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002940 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002941 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002942 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002943 length -= (int)STRLEN(buf2);
2944
2945 /*
2946 * Adjust number of zeros to the new number of digits, so the
2947 * total length of the number remains the same.
2948 * Don't do this when
2949 * the result may look like an octal number.
2950 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002951 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002952 while (length-- > 0)
2953 *ptr++ = '0';
2954 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002955
Bram Moolenaard79e5502016-01-10 22:13:02 +01002956 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002957
2958 // Insert just after the first character to be removed, so that any
2959 // text properties will be adjusted. Then delete the old number
2960 // afterwards.
2961 save_pos = curwin->w_cursor;
2962 if (todel > 0)
2963 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002965 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002966
2967 // del_char() will also mark line needing displaying
2968 if (todel > 0)
2969 {
2970 int bytes_after = (int)STRLEN(ml_get_curline())
2971 - curwin->w_cursor.col;
2972
2973 // Delete the one character before the insert.
2974 curwin->w_cursor = save_pos;
2975 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002976 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2977 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002978 --todel;
2979 }
2980 while (todel-- > 0)
2981 (void)del_char(FALSE);
2982
Bram Moolenaard79e5502016-01-10 22:13:02 +01002983 endpos = curwin->w_cursor;
2984 if (did_change && curwin->w_cursor.col)
2985 --curwin->w_cursor.col;
2986 }
2987
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002991 curbuf->b_op_start = startpos;
2992 curbuf->b_op_end = endpos;
2993 if (curbuf->b_op_end.col > 0)
2994 --curbuf->b_op_end.col;
2995 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002996
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002997theend:
2998 if (visual)
2999 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003000 else if (did_change)
3001 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003002 else if (virtual_active())
3003 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003004
Bram Moolenaard79e5502016-01-10 22:13:02 +01003005 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003009clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003011 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012}
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003015 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 *
3017 * "Words" are counted by looking for boundaries between non-space and
3018 * space characters. (it seems to produce results that match 'wc'.)
3019 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003020 * Return value is byte count; word count for the line is added to "*wc".
3021 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 *
3023 * The function will only examine the first "limit" characters in the
3024 * line, stopping if it encounters an end-of-line (NUL byte). In that
3025 * case, eol_size will be added to the character count to account for
3026 * the size of the EOL character.
3027 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003028 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003029line_count_info(
3030 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003031 varnumber_T *wc,
3032 varnumber_T *cc,
3033 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003034 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003036 varnumber_T i;
3037 varnumber_T words = 0;
3038 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 int is_word = 0;
3040
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003041 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 {
3043 if (is_word)
3044 {
3045 if (vim_isspace(line[i]))
3046 {
3047 words++;
3048 is_word = 0;
3049 }
3050 }
3051 else if (!vim_isspace(line[i]))
3052 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003053 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003054 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056
3057 if (is_word)
3058 words++;
3059 *wc += words;
3060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003061 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003062 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003065 chars += eol_size;
3066 }
3067 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 return i;
3069}
3070
3071/*
3072 * Give some info about the position of the cursor (for "g CTRL-G").
3073 * In Visual mode, give some info about the selected region. (In this case,
3074 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003075 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 */
3077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003078cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003081 char_u buf1[50];
3082 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003084 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003085 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003086 varnumber_T byte_count_cursor = 0;
3087 varnumber_T char_count = 0;
3088 varnumber_T char_count_cursor = 0;
3089 varnumber_T word_count = 0;
3090 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003091 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003092 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 long line_count_selected = 0;
3094 pos_T min_pos, max_pos;
3095 oparg_T oparg;
3096 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 /*
3099 * Compute the length of the file in characters.
3100 */
3101 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3102 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003103 if (dict == NULL)
3104 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003105 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003106 return;
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
3109 else
3110 {
3111 if (get_fileformat(curbuf) == EOL_DOS)
3112 eol_size = 2;
3113 else
3114 eol_size = 1;
3115
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 if (VIsual_active)
3117 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003118 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
3120 min_pos = VIsual;
3121 max_pos = curwin->w_cursor;
3122 }
3123 else
3124 {
3125 min_pos = curwin->w_cursor;
3126 max_pos = VIsual;
3127 }
3128 if (*p_sel == 'e' && max_pos.col > 0)
3129 --max_pos.col;
3130
3131 if (VIsual_mode == Ctrl_V)
3132 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003133#ifdef FEAT_LINEBREAK
3134 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003135 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003136
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003137 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003138 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003139 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 oparg.is_VIsual = 1;
3142 oparg.block_mode = TRUE;
3143 oparg.op_type = OP_NOP;
3144 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003145 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003146#ifdef FEAT_LINEBREAK
3147 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003148 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003149#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003150 if (curwin->w_curswant == MAXCOL)
3151 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (oparg.end_vcol < oparg.start_vcol)
3154 {
3155 oparg.end_vcol += oparg.start_vcol;
3156 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3157 oparg.end_vcol -= oparg.start_vcol;
3158 }
3159 }
3160 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003166 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 ui_breakcheck();
3169 if (got_int)
3170 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003171 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 }
3173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (VIsual_active
3176 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3177 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003178 char_u *s = NULL;
3179 long len = 0L;
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 switch (VIsual_mode)
3182 {
3183 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003187 s = bd.textstart;
3188 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 break;
3190 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003191 s = ml_get(lnum);
3192 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 break;
3194 case 'v':
3195 {
3196 colnr_T start_col = (lnum == min_pos.lnum)
3197 ? min_pos.col : 0;
3198 colnr_T end_col = (lnum == max_pos.lnum)
3199 ? max_pos.col - start_col + 1 : MAXCOL;
3200
Bram Moolenaardef9e822004-12-31 20:58:58 +00003201 s = ml_get(lnum) + start_col;
3202 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 break;
3205 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003206 if (s != NULL)
3207 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003208 byte_count_cursor += line_count_info(s, &word_count_cursor,
3209 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003210 if (lnum == curbuf->b_ml.ml_line_count
3211 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003212 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003213 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003214 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003219 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (lnum == curwin->w_cursor.lnum)
3221 {
3222 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003223 char_count_cursor += char_count;
3224 byte_count_cursor = byte_count +
3225 line_count_info(ml_get(lnum),
3226 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003227 (varnumber_T)(curwin->w_cursor.col + 1),
3228 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
3230 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003231 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003232 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003233 &char_count, (varnumber_T)MAXCOL,
3234 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
3236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003237 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003238 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003239 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaared767a22016-01-03 22:49:16 +01003241 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003243 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003245 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3246 {
3247 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3248 &max_pos.col);
3249 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3250 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3251 }
3252 else
3253 buf1[0] = NUL;
3254
3255 if (char_count_cursor == byte_count_cursor
3256 && char_count == byte_count)
3257 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003258 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003259 buf1, line_count_selected,
3260 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003261 word_count_cursor,
3262 word_count,
3263 byte_count_cursor,
3264 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003265 else
3266 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003267 _("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 +01003268 buf1, line_count_selected,
3269 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003270 word_count_cursor,
3271 word_count,
3272 char_count_cursor,
3273 char_count,
3274 byte_count_cursor,
3275 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003278 {
3279 p = ml_get_curline();
3280 validate_virtcol();
3281 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3282 (int)curwin->w_virtcol + 1);
3283 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003284 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaared767a22016-01-03 22:49:16 +01003286 if (char_count_cursor == byte_count_cursor
3287 && char_count == byte_count)
3288 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003289 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003290 (char *)buf1, (char *)buf2,
3291 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003293 word_count_cursor, word_count,
3294 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003295 else
3296 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003297 _("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 +01003298 (char *)buf1, (char *)buf2,
3299 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003300 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003301 word_count_cursor, word_count,
3302 char_count_cursor, char_count,
3303 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003304 }
3305 }
3306
Bram Moolenaared767a22016-01-03 22:49:16 +01003307 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003308 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003309 {
3310 size_t len = STRLEN(IObuff);
3311
3312 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003313 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003314 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003315 if (dict == NULL)
3316 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003317 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003318 p = p_shm;
3319 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003320 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003321 p_shm = p;
3322 }
3323 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003324#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003325 if (dict != NULL)
3326 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003327 dict_add_number(dict, "words", word_count);
3328 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003329 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003330 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3331 byte_count_cursor);
3332 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3333 char_count_cursor);
3334 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3335 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003339
3340/*
3341 * Handle indent and format operators and visual mode ":".
3342 */
3343 static void
3344op_colon(oparg_T *oap)
3345{
3346 stuffcharReadbuff(':');
3347 if (oap->is_VIsual)
3348 stuffReadbuff((char_u *)"'<,'>");
3349 else
3350 {
3351 // Make the range look nice, so it can be repeated.
3352 if (oap->start.lnum == curwin->w_cursor.lnum)
3353 stuffcharReadbuff('.');
3354 else
3355 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003356
3357#ifdef FEAT_FOLDING
3358 // When using !! on a closed fold the range ".!" works best to operate
3359 // on, it will be made the whole closed fold later.
3360 linenr_T endOfStartFold = oap->start.lnum;
3361 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3362#endif
3363 if (oap->end.lnum != oap->start.lnum
3364#ifdef FEAT_FOLDING
3365 && oap->end.lnum != endOfStartFold
3366#endif
3367 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003368 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003369 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003370 stuffcharReadbuff(',');
3371 if (oap->end.lnum == curwin->w_cursor.lnum)
3372 stuffcharReadbuff('.');
3373 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3374 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003375 else if (oap->start.lnum == curwin->w_cursor.lnum
3376#ifdef FEAT_FOLDING
3377 // do not use ".+number" for a closed fold, it would count
3378 // folded lines twice
3379 && !hasFolding(oap->end.lnum, NULL, NULL)
3380#endif
3381 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003382 {
3383 stuffReadbuff((char_u *)".+");
3384 stuffnumReadbuff((long)oap->line_count - 1);
3385 }
3386 else
3387 stuffnumReadbuff((long)oap->end.lnum);
3388 }
3389 }
3390 if (oap->op_type != OP_COLON)
3391 stuffReadbuff((char_u *)"!");
3392 if (oap->op_type == OP_INDENT)
3393 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003394 if (*get_equalprg() == NUL)
3395 stuffReadbuff((char_u *)"indent");
3396 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003397 stuffReadbuff(get_equalprg());
3398 stuffReadbuff((char_u *)"\n");
3399 }
3400 else if (oap->op_type == OP_FORMAT)
3401 {
3402 if (*curbuf->b_p_fp != NUL)
3403 stuffReadbuff(curbuf->b_p_fp);
3404 else if (*p_fp != NUL)
3405 stuffReadbuff(p_fp);
3406 else
3407 stuffReadbuff((char_u *)"fmt");
3408 stuffReadbuff((char_u *)"\n']");
3409 }
3410
3411 // do_cmdline() does the rest
3412}
3413
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003414// callback function for 'operatorfunc'
3415static callback_T opfunc_cb;
3416
3417/*
3418 * Process the 'operatorfunc' option value.
3419 * Returns OK or FAIL.
3420 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003421 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003422did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003423{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003424 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3425 return e_invalid_argument;
3426
3427 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003428}
3429
3430#if defined(EXITFREE) || defined(PROTO)
3431 void
3432free_operatorfunc_option(void)
3433{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003434# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003435 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003436# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003437}
3438#endif
3439
Dominique Pelle748b3082022-01-08 12:41:16 +00003440#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003441/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003442 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003443 * garbage collected.
3444 */
3445 int
3446set_ref_in_opfunc(int copyID UNUSED)
3447{
3448 int abort = FALSE;
3449
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003450 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003451
3452 return abort;
3453}
Dominique Pelle748b3082022-01-08 12:41:16 +00003454#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003455
3456/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003457 * Handle the "g@" operator: call 'operatorfunc'.
3458 */
3459 static void
3460op_function(oparg_T *oap UNUSED)
3461{
3462#ifdef FEAT_EVAL
3463 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003464 pos_T orig_start = curbuf->b_op_start;
3465 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003466 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003467
3468 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003469 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003470 else
3471 {
3472 // Set '[ and '] marks to text to be operated on.
3473 curbuf->b_op_start = oap->start;
3474 curbuf->b_op_end = oap->end;
3475 if (oap->motion_type != MLINE && !oap->inclusive)
3476 // Exclude the end position.
3477 decl(&curbuf->b_op_end);
3478
3479 argv[0].v_type = VAR_STRING;
3480 if (oap->block_mode)
3481 argv[0].vval.v_string = (char_u *)"block";
3482 else if (oap->motion_type == MLINE)
3483 argv[0].vval.v_string = (char_u *)"line";
3484 else
3485 argv[0].vval.v_string = (char_u *)"char";
3486 argv[1].v_type = VAR_UNKNOWN;
3487
3488 // Reset virtual_op so that 'virtualedit' can be changed in the
3489 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003490 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003491 virtual_op = MAYBE;
3492
naohiro ono75c30e92021-10-19 11:15:41 +01003493 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003494 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003495 finish_op = FALSE;
3496
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003497 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3498 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003499
3500 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003501 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003502 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003503 {
3504 curbuf->b_op_start = orig_start;
3505 curbuf->b_op_end = orig_end;
3506 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003507 }
3508#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003509 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003510#endif
3511}
3512
3513/*
3514 * Calculate start/end virtual columns for operating in block mode.
3515 */
3516 static void
3517get_op_vcol(
3518 oparg_T *oap,
3519 colnr_T redo_VIsual_vcol,
3520 int initial) // when TRUE adjust position for 'selectmode'
3521{
3522 colnr_T start, end;
3523
3524 if (VIsual_mode != Ctrl_V
3525 || (!initial && oap->end.col < curwin->w_width))
3526 return;
3527
3528 oap->block_mode = TRUE;
3529
3530 // prevent from moving onto a trail byte
3531 if (has_mbyte)
3532 mb_adjustpos(curwin->w_buffer, &oap->end);
3533
3534 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3535
3536 if (!redo_VIsual_busy)
3537 {
3538 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3539
3540 if (start < oap->start_vcol)
3541 oap->start_vcol = start;
3542 if (end > oap->end_vcol)
3543 {
3544 if (initial && *p_sel == 'e' && start >= 1
3545 && start - 1 >= oap->end_vcol)
3546 oap->end_vcol = start - 1;
3547 else
3548 oap->end_vcol = end;
3549 }
3550 }
3551
3552 // if '$' was used, get oap->end_vcol from longest line
3553 if (curwin->w_curswant == MAXCOL)
3554 {
3555 curwin->w_cursor.col = MAXCOL;
3556 oap->end_vcol = 0;
3557 for (curwin->w_cursor.lnum = oap->start.lnum;
3558 curwin->w_cursor.lnum <= oap->end.lnum;
3559 ++curwin->w_cursor.lnum)
3560 {
3561 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3562 if (end > oap->end_vcol)
3563 oap->end_vcol = end;
3564 }
3565 }
3566 else if (redo_VIsual_busy)
3567 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3568 // Correct oap->end.col and oap->start.col to be the
3569 // upper-left and lower-right corner of the block area.
3570 //
3571 // (Actually, this does convert column positions into character
3572 // positions)
3573 curwin->w_cursor.lnum = oap->end.lnum;
3574 coladvance(oap->end_vcol);
3575 oap->end = curwin->w_cursor;
3576
3577 curwin->w_cursor = oap->start;
3578 coladvance(oap->start_vcol);
3579 oap->start = curwin->w_cursor;
3580}
3581
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003582// Information for redoing the previous Visual selection.
3583typedef struct {
3584 int rv_mode; // 'v', 'V', or Ctrl-V
3585 linenr_T rv_line_count; // number of lines
3586 colnr_T rv_vcol; // number of cols or end column
3587 long rv_count; // count for Visual operator
3588 int rv_arg; // extra argument
3589} redo_VIsual_T;
3590
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003591 static int
3592is_ex_cmdchar(cmdarg_T *cap)
3593{
3594 return cap->cmdchar == ':'
3595 || cap->cmdchar == K_COMMAND
3596 || cap->cmdchar == K_SCRIPT_COMMAND;
3597}
3598
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003599/*
3600 * Handle an operator after Visual mode or when the movement is finished.
3601 * "gui_yank" is true when yanking text for the clipboard.
3602 */
3603 void
3604do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3605{
3606 oparg_T *oap = cap->oap;
3607 pos_T old_cursor;
3608 int empty_region_error;
3609 int restart_edit_save;
3610#ifdef FEAT_LINEBREAK
3611 int lbr_saved = curwin->w_p_lbr;
3612#endif
3613
3614 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003615 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3616
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003617 int include_line_break = FALSE;
3618
3619#if defined(FEAT_CLIPBOARD)
3620 // Yank the visual area into the GUI selection register before we operate
3621 // on it and lose it forever.
3622 // Don't do it if a specific register was specified, so that ""x"*P works.
3623 // This could call do_pending_operator() recursively, but that's OK
3624 // because gui_yank will be TRUE for the nested call.
3625 if ((clip_star.available || clip_plus.available)
3626 && oap->op_type != OP_NOP
3627 && !gui_yank
3628 && VIsual_active
3629 && !redo_VIsual_busy
3630 && oap->regname == 0)
3631 clip_auto_select();
3632#endif
3633 old_cursor = curwin->w_cursor;
3634
3635 // If an operation is pending, handle it...
3636 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3637 {
3638 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3639 // for the clipboard.
3640 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3641
3642#ifdef FEAT_LINEBREAK
3643 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003644 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003645#endif
3646 oap->is_VIsual = VIsual_active;
3647 if (oap->motion_force == 'V')
3648 oap->motion_type = MLINE;
3649 else if (oap->motion_force == 'v')
3650 {
3651 // If the motion was linewise, "inclusive" will not have been set.
3652 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3653 if (oap->motion_type == MLINE)
3654 oap->inclusive = FALSE;
3655 // If the motion already was characterwise, toggle "inclusive"
3656 else if (oap->motion_type == MCHAR)
3657 oap->inclusive = !oap->inclusive;
3658 oap->motion_type = MCHAR;
3659 }
3660 else if (oap->motion_force == Ctrl_V)
3661 {
3662 // Change line- or characterwise motion into Visual block mode.
3663 if (!VIsual_active)
3664 {
3665 VIsual_active = TRUE;
3666 VIsual = oap->start;
3667 }
3668 VIsual_mode = Ctrl_V;
3669 VIsual_select = FALSE;
3670 VIsual_reselect = FALSE;
3671 }
3672
3673 // Only redo yank when 'y' flag is in 'cpoptions'.
3674 // Never redo "zf" (define fold).
3675 if ((redo_yank || oap->op_type != OP_YANK)
3676 && ((!VIsual_active || oap->motion_force)
3677 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003678 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003679 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003680 && cap->cmdchar != 'D'
3681#ifdef FEAT_FOLDING
3682 && oap->op_type != OP_FOLD
3683 && oap->op_type != OP_FOLDOPEN
3684 && oap->op_type != OP_FOLDOPENREC
3685 && oap->op_type != OP_FOLDCLOSE
3686 && oap->op_type != OP_FOLDCLOSEREC
3687 && oap->op_type != OP_FOLDDEL
3688 && oap->op_type != OP_FOLDDELREC
3689#endif
3690 )
3691 {
3692 prep_redo(oap->regname, cap->count0,
3693 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3694 oap->motion_force, cap->cmdchar, cap->nchar);
3695 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3696 {
3697 // If 'cpoptions' does not contain 'r', insert the search
3698 // pattern to really repeat the same command.
3699 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3700 AppendToRedobuffLit(cap->searchbuf, -1);
3701 AppendToRedobuff(NL_STR);
3702 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003703 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003704 {
3705 // do_cmdline() has stored the first typed line in
3706 // "repeat_cmdline". When several lines are typed repeating
3707 // won't be possible.
3708 if (repeat_cmdline == NULL)
3709 ResetRedobuff();
3710 else
3711 {
zeertzjq30b6d612023-05-07 17:39:23 +01003712 if (cap->cmdchar == ':')
3713 AppendToRedobuffLit(repeat_cmdline, -1);
3714 else
3715 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003716 AppendToRedobuff(NL_STR);
3717 VIM_CLEAR(repeat_cmdline);
3718 }
3719 }
3720 }
3721
3722 if (redo_VIsual_busy)
3723 {
3724 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003725 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003726 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003727 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003728 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3729 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003730 VIsual_mode = redo_VIsual.rv_mode;
3731 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003732 {
3733 if (VIsual_mode == 'v')
3734 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003735 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003736 {
3737 validate_virtcol();
3738 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003739 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003740 }
3741 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003742 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003743 }
3744 else
3745 {
3746 curwin->w_curswant = MAXCOL;
3747 }
3748 coladvance(curwin->w_curswant);
3749 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003750 cap->count0 = redo_VIsual.rv_count;
3751 if (redo_VIsual.rv_count != 0)
3752 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003753 else
3754 cap->count1 = 1;
3755 }
3756 else if (VIsual_active)
3757 {
3758 if (!gui_yank)
3759 {
3760 // Save the current VIsual area for '< and '> marks, and "gv"
3761 curbuf->b_visual.vi_start = VIsual;
3762 curbuf->b_visual.vi_end = curwin->w_cursor;
3763 curbuf->b_visual.vi_mode = VIsual_mode;
3764 restore_visual_mode();
3765 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003766#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003767 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003768#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003769 }
3770
3771 // In Select mode, a linewise selection is operated upon like a
3772 // characterwise selection.
3773 // Special case: gH<Del> deletes the last line.
3774 if (VIsual_select && VIsual_mode == 'V'
3775 && cap->oap->op_type != OP_DELETE)
3776 {
3777 if (LT_POS(VIsual, curwin->w_cursor))
3778 {
3779 VIsual.col = 0;
3780 curwin->w_cursor.col =
3781 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3782 }
3783 else
3784 {
3785 curwin->w_cursor.col = 0;
3786 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3787 }
3788 VIsual_mode = 'v';
3789 }
3790 // If 'selection' is "exclusive", backup one character for
3791 // charwise selections.
3792 else if (VIsual_mode == 'v')
3793 include_line_break = unadjust_for_sel();
3794
3795 oap->start = VIsual;
3796 if (VIsual_mode == 'V')
3797 {
3798 oap->start.col = 0;
3799 oap->start.coladd = 0;
3800 }
3801 }
3802
3803 // Set oap->start to the first position of the operated text, oap->end
3804 // to the end of the operated text. w_cursor is equal to oap->start.
3805 if (LT_POS(oap->start, curwin->w_cursor))
3806 {
3807#ifdef FEAT_FOLDING
3808 // Include folded lines completely.
3809 if (!VIsual_active)
3810 {
3811 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3812 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003813 if ((curwin->w_cursor.col > 0 || oap->inclusive
3814 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003815 && hasFolding(curwin->w_cursor.lnum, NULL,
3816 &curwin->w_cursor.lnum))
3817 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3818 }
3819#endif
3820 oap->end = curwin->w_cursor;
3821 curwin->w_cursor = oap->start;
3822
3823 // w_virtcol may have been updated; if the cursor goes back to its
3824 // previous position w_virtcol becomes invalid and isn't updated
3825 // automatically.
3826 curwin->w_valid &= ~VALID_VIRTCOL;
3827 }
3828 else
3829 {
3830#ifdef FEAT_FOLDING
3831 // Include folded lines completely.
3832 if (!VIsual_active && oap->motion_type == MLINE)
3833 {
3834 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3835 NULL))
3836 curwin->w_cursor.col = 0;
3837 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3838 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3839 }
3840#endif
3841 oap->end = oap->start;
3842 oap->start = curwin->w_cursor;
3843 }
3844
3845 // Just in case lines were deleted that make the position invalid.
3846 check_pos(curwin->w_buffer, &oap->end);
3847 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3848
3849 // Set "virtual_op" before resetting VIsual_active.
3850 virtual_op = virtual_active();
3851
3852 if (VIsual_active || redo_VIsual_busy)
3853 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003854 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003855
3856 if (!redo_VIsual_busy && !gui_yank)
3857 {
3858 // Prepare to reselect and redo Visual: this is based on the
3859 // size of the Visual text
3860 resel_VIsual_mode = VIsual_mode;
3861 if (curwin->w_curswant == MAXCOL)
3862 resel_VIsual_vcol = MAXCOL;
3863 else
3864 {
3865 if (VIsual_mode != Ctrl_V)
3866 getvvcol(curwin, &(oap->end),
3867 NULL, NULL, &oap->end_vcol);
3868 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3869 {
3870 if (VIsual_mode != Ctrl_V)
3871 getvvcol(curwin, &(oap->start),
3872 &oap->start_vcol, NULL, NULL);
3873 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3874 }
3875 else
3876 resel_VIsual_vcol = oap->end_vcol;
3877 }
3878 resel_VIsual_line_count = oap->line_count;
3879 }
3880
3881 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3882 if ((redo_yank || oap->op_type != OP_YANK)
3883 && oap->op_type != OP_COLON
3884#ifdef FEAT_FOLDING
3885 && oap->op_type != OP_FOLD
3886 && oap->op_type != OP_FOLDOPEN
3887 && oap->op_type != OP_FOLDOPENREC
3888 && oap->op_type != OP_FOLDCLOSE
3889 && oap->op_type != OP_FOLDCLOSEREC
3890 && oap->op_type != OP_FOLDDEL
3891 && oap->op_type != OP_FOLDDELREC
3892#endif
3893 && oap->motion_force == NUL
3894 )
3895 {
3896 // Prepare for redoing. Only use the nchar field for "r",
3897 // otherwise it might be the second char of the operator.
3898 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3899 || cap->nchar == 'N'))
3900 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003901 get_op_char(oap->op_type),
3902 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003903 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003904 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003905 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003906 int opchar = get_op_char(oap->op_type);
3907 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003908 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3909
3910 // reverse what nv_replace() did
3911 if (nchar == REPLACE_CR_NCHAR)
3912 nchar = CAR;
3913 else if (nchar == REPLACE_NL_NCHAR)
3914 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003915
3916 if (opchar == 'g' && extra_opchar == '@')
3917 // also repeat the count for 'operatorfunc'
3918 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3919 cap->count0, opchar, extra_opchar, nchar);
3920 else
3921 prep_redo(oap->regname, 0L, NUL, 'v',
3922 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003923 }
3924 if (!redo_VIsual_busy)
3925 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003926 redo_VIsual.rv_mode = resel_VIsual_mode;
3927 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3928 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3929 redo_VIsual.rv_count = cap->count0;
3930 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003931 }
3932 }
3933
3934 // oap->inclusive defaults to TRUE.
3935 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3936 // FALSE. This makes "d}P" and "v}dP" work the same.
3937 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3938 oap->inclusive = TRUE;
3939 if (VIsual_mode == 'V')
3940 oap->motion_type = MLINE;
3941 else
3942 {
3943 oap->motion_type = MCHAR;
3944 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3945 && (include_line_break || !virtual_op))
3946 {
3947 oap->inclusive = FALSE;
3948 // Try to include the newline, unless it's an operator
3949 // that works on lines only.
3950 if (*p_sel != 'o'
3951 && !op_on_lines(oap->op_type)
3952 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3953 {
3954 ++oap->end.lnum;
3955 oap->end.col = 0;
3956 oap->end.coladd = 0;
3957 ++oap->line_count;
3958 }
3959 }
3960 }
3961
3962 redo_VIsual_busy = FALSE;
3963
3964 // Switch Visual off now, so screen updating does
3965 // not show inverted text when the screen is redrawn.
3966 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3967 // no screen redraw, so it is done here to remove the inverted
3968 // part.
3969 if (!gui_yank)
3970 {
3971 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003972 setmouse();
3973 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003974 may_clear_cmdline();
3975 if ((oap->op_type == OP_YANK
3976 || oap->op_type == OP_COLON
3977 || oap->op_type == OP_FUNCTION
3978 || oap->op_type == OP_FILTER)
3979 && oap->motion_force == NUL)
3980 {
3981#ifdef FEAT_LINEBREAK
3982 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01003983 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003984#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003985 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003986 }
3987 }
3988 }
3989
3990 // Include the trailing byte of a multi-byte char.
3991 if (has_mbyte && oap->inclusive)
3992 {
3993 int l;
3994
3995 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3996 if (l > 1)
3997 oap->end.col += l - 1;
3998 }
3999 curwin->w_set_curswant = TRUE;
4000
4001 // oap->empty is set when start and end are the same. The inclusive
4002 // flag affects this too, unless yanking and the end is on a NUL.
4003 oap->empty = (oap->motion_type == MCHAR
4004 && (!oap->inclusive
4005 || (oap->op_type == OP_YANK
4006 && gchar_pos(&oap->end) == NUL))
4007 && EQUAL_POS(oap->start, oap->end)
4008 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4009 // For delete, change and yank, it's an error to operate on an
4010 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4011 empty_region_error = (oap->empty
4012 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4013
4014 // Force a redraw when operating on an empty Visual region, when
4015 // 'modifiable is off or creating a fold.
4016 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4017#ifdef FEAT_FOLDING
4018 || oap->op_type == OP_FOLD
4019#endif
4020 ))
4021 {
4022#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004023 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004024#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004025 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004026 }
4027
4028 // If the end of an operator is in column one while oap->motion_type
4029 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4030 // character in the previous line. If op_start is on or before the
4031 // first non-blank in the line, the operator becomes linewise
4032 // (strange, but that's the way vi does it).
4033 if ( oap->motion_type == MCHAR
4034 && oap->inclusive == FALSE
4035 && !(cap->retval & CA_NO_ADJ_OP_END)
4036 && oap->end.col == 0
4037 && (!oap->is_VIsual || *p_sel == 'o')
4038 && !oap->block_mode
4039 && oap->line_count > 1)
4040 {
4041 oap->end_adjusted = TRUE; // remember that we did this
4042 --oap->line_count;
4043 --oap->end.lnum;
4044 if (inindent(0))
4045 oap->motion_type = MLINE;
4046 else
4047 {
4048 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
4049 if (oap->end.col)
4050 {
4051 --oap->end.col;
4052 oap->inclusive = TRUE;
4053 }
4054 }
4055 }
4056 else
4057 oap->end_adjusted = FALSE;
4058
4059 switch (oap->op_type)
4060 {
4061 case OP_LSHIFT:
4062 case OP_RSHIFT:
4063 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4064 auto_format(FALSE, TRUE);
4065 break;
4066
4067 case OP_JOIN_NS:
4068 case OP_JOIN:
4069 if (oap->line_count < 2)
4070 oap->line_count = 2;
4071 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4072 curbuf->b_ml.ml_line_count)
4073 beep_flush();
4074 else
4075 {
4076 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4077 TRUE, TRUE, TRUE);
4078 auto_format(FALSE, TRUE);
4079 }
4080 break;
4081
4082 case OP_DELETE:
4083 VIsual_reselect = FALSE; // don't reselect now
4084 if (empty_region_error)
4085 {
4086 vim_beep(BO_OPER);
4087 CancelRedo();
4088 }
4089 else
4090 {
4091 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004092 // save cursor line for undo if it wasn't saved yet
4093 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4094 && u_save_cursor() == OK)
4095 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004096 }
4097 break;
4098
4099 case OP_YANK:
4100 if (empty_region_error)
4101 {
4102 if (!gui_yank)
4103 {
4104 vim_beep(BO_OPER);
4105 CancelRedo();
4106 }
4107 }
4108 else
4109 {
4110#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004111 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004112#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004113 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004114 (void)op_yank(oap, FALSE, !gui_yank);
4115 }
4116 check_cursor_col();
4117 break;
4118
4119 case OP_CHANGE:
4120 VIsual_reselect = FALSE; // don't reselect now
4121 if (empty_region_error)
4122 {
4123 vim_beep(BO_OPER);
4124 CancelRedo();
4125 }
4126 else
4127 {
4128 // This is a new edit command, not a restart. Need to
4129 // remember it to make 'insertmode' work with mappings for
4130 // Visual mode. But do this only once and not when typed and
4131 // 'insertmode' isn't set.
4132 if (p_im || !KeyTyped)
4133 restart_edit_save = restart_edit;
4134 else
4135 restart_edit_save = 0;
4136 restart_edit = 0;
4137#ifdef FEAT_LINEBREAK
4138 // Restore linebreak, so that when the user edits it looks as
4139 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004140 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004141#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004142 // trigger TextChangedI
4143 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4144
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004145 if (op_change(oap)) // will call edit()
4146 cap->retval |= CA_COMMAND_BUSY;
4147 if (restart_edit == 0)
4148 restart_edit = restart_edit_save;
4149 }
4150 break;
4151
4152 case OP_FILTER:
4153 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4154 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4155 else
4156 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4157 // FALLTHROUGH
4158
4159 case OP_INDENT:
4160 case OP_COLON:
4161
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004162 // If 'equalprg' is empty, do the indenting internally.
4163 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4164 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004165 if (curbuf->b_p_lisp)
4166 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004167#ifdef FEAT_EVAL
4168 if (use_indentexpr_for_lisp())
4169 op_reindent(oap, get_expr_indent);
4170 else
4171#endif
4172 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004173 break;
4174 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004175 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004176#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004177 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004178#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004179 get_c_indent);
4180 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004181 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004182
4183 op_colon(oap);
4184 break;
4185
4186 case OP_TILDE:
4187 case OP_UPPER:
4188 case OP_LOWER:
4189 case OP_ROT13:
4190 if (empty_region_error)
4191 {
4192 vim_beep(BO_OPER);
4193 CancelRedo();
4194 }
4195 else
4196 op_tilde(oap);
4197 check_cursor_col();
4198 break;
4199
4200 case OP_FORMAT:
4201#if defined(FEAT_EVAL)
4202 if (*curbuf->b_p_fex != NUL)
4203 op_formatexpr(oap); // use expression
4204 else
4205#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004206 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004207 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004208 op_colon(oap); // use external command
4209 else
4210 op_format(oap, FALSE); // use internal function
4211 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004212 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004213 case OP_FORMAT2:
4214 op_format(oap, TRUE); // use internal function
4215 break;
4216
4217 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004218 {
4219 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4220
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004221#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004222 // Restore linebreak, so that when the user edits it looks as
4223 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004224 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004225#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004226 // call 'operatorfunc'
4227 op_function(oap);
4228
4229 // Restore the info for redoing Visual mode, the function may
4230 // invoke another operator and unintentionally change it.
4231 redo_VIsual = save_redo_VIsual;
4232 break;
4233 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004234
4235 case OP_INSERT:
4236 case OP_APPEND:
4237 VIsual_reselect = FALSE; // don't reselect now
4238 if (empty_region_error)
4239 {
4240 vim_beep(BO_OPER);
4241 CancelRedo();
4242 }
4243 else
4244 {
4245 // This is a new edit command, not a restart. Need to
4246 // remember it to make 'insertmode' work with mappings for
4247 // Visual mode. But do this only once.
4248 restart_edit_save = restart_edit;
4249 restart_edit = 0;
4250#ifdef FEAT_LINEBREAK
4251 // Restore linebreak, so that when the user edits it looks as
4252 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004253 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004254#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004255 // trigger TextChangedI
4256 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4257
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004258 op_insert(oap, cap->count1);
4259#ifdef FEAT_LINEBREAK
4260 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004261 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004262#endif
4263
4264 // TODO: when inserting in several lines, should format all
4265 // the lines.
4266 auto_format(FALSE, TRUE);
4267
4268 if (restart_edit == 0)
4269 restart_edit = restart_edit_save;
4270 else
4271 cap->retval |= CA_COMMAND_BUSY;
4272 }
4273 break;
4274
4275 case OP_REPLACE:
4276 VIsual_reselect = FALSE; // don't reselect now
4277 if (empty_region_error)
4278 {
4279 vim_beep(BO_OPER);
4280 CancelRedo();
4281 }
4282 else
4283 {
4284#ifdef FEAT_LINEBREAK
4285 // Restore linebreak, so that when the user edits it looks as
4286 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004287 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004288#endif
4289 op_replace(oap, cap->nchar);
4290 }
4291 break;
4292
4293#ifdef FEAT_FOLDING
4294 case OP_FOLD:
4295 VIsual_reselect = FALSE; // don't reselect now
4296 foldCreate(oap->start.lnum, oap->end.lnum);
4297 break;
4298
4299 case OP_FOLDOPEN:
4300 case OP_FOLDOPENREC:
4301 case OP_FOLDCLOSE:
4302 case OP_FOLDCLOSEREC:
4303 VIsual_reselect = FALSE; // don't reselect now
4304 opFoldRange(oap->start.lnum, oap->end.lnum,
4305 oap->op_type == OP_FOLDOPEN
4306 || oap->op_type == OP_FOLDOPENREC,
4307 oap->op_type == OP_FOLDOPENREC
4308 || oap->op_type == OP_FOLDCLOSEREC,
4309 oap->is_VIsual);
4310 break;
4311
4312 case OP_FOLDDEL:
4313 case OP_FOLDDELREC:
4314 VIsual_reselect = FALSE; // don't reselect now
4315 deleteFold(oap->start.lnum, oap->end.lnum,
4316 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4317 break;
4318#endif
4319 case OP_NR_ADD:
4320 case OP_NR_SUB:
4321 if (empty_region_error)
4322 {
4323 vim_beep(BO_OPER);
4324 CancelRedo();
4325 }
4326 else
4327 {
4328 VIsual_active = TRUE;
4329#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004330 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004331#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004332 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004333 VIsual_active = FALSE;
4334 }
4335 check_cursor_col();
4336 break;
4337 default:
4338 clearopbeep(oap);
4339 }
4340 virtual_op = MAYBE;
4341 if (!gui_yank)
4342 {
4343 // if 'sol' not set, go back to old column for some commands
4344 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4345 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4346 || oap->op_type == OP_DELETE))
4347 {
4348#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004349 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004350#endif
4351 coladvance(curwin->w_curswant = old_col);
4352 }
4353 }
4354 else
4355 {
4356 curwin->w_cursor = old_cursor;
4357 }
4358 oap->block_mode = FALSE;
4359 clearop(oap);
4360 motion_force = NUL;
4361 }
4362#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004363 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004364#endif
4365}
Christian Brabandtffb13672023-09-15 20:22:02 +02004366
4367// put byte 'c' at position 'lp', but
4368// verify, that the position to place
4369// is actually safe
4370 static void
4371pbyte(pos_T lp, int c)
4372{
4373 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4374 int len = curbuf->b_ml.ml_line_len;
4375
4376 // safety check
4377 if (lp.col >= len)
4378 lp.col = (len > 1 ? len - 2 : 0);
4379 *(p + lp.col) = c;
4380}