blob: 9e8ea86160c30d781cadea33e1f3df80357bda52 [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;
Christian Brabandt22a97fc2023-11-19 10:45:24 +0100252 count = (long long)i * (long long)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 {
Christian Brabandt22a97fc2023-11-19 10:45:24 +0100258 count -= (long long)sw_val * (long long)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (count < 0)
260 count = 0;
261 }
262 else
Christian Brabandt22a97fc2023-11-19 10:45:24 +0100263 count += (long long)sw_val * (long long)amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265
Christian Brabandt22a97fc2023-11-19 10:45:24 +0100266 if (count > INT_MAX)
267 count = INT_MAX;
268
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200269 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 if (State & VREPLACE_FLAG)
Christian Brabandt6bf13182023-11-14 22:42:59 +0100271 change_indent(INDENT_SET, (int)count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 else
Christian Brabandt6bf13182023-11-14 22:42:59 +0100273 (void)set_indent((int)count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274}
275
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276/*
277 * Shift one line of the current block one shiftwidth right or left.
278 * Leaves cursor on first character in block.
279 */
280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100281shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
283 int left = (oap->op_type == OP_LSHIFT);
284 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000285 int total;
286 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200288 int sw_val = (int)get_sw_value_indent(curbuf);
289 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000292 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100293 int added;
294 unsigned new_line_len; // the length of the line after the
295 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#ifdef FEAT_RIGHTLEFT
297 int old_p_ri = p_ri;
298
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100299 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300#endif
301
Bram Moolenaar24959102022-05-07 20:01:16 +0100302 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
304 if (bd.is_short)
305 return;
306
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100307 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200308 total = (int)((unsigned)amount * (unsigned)sw_val);
309 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100310 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 oldp = ml_get_curline();
313
314 if (!left)
315 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100316 int tabs = 0, spaces = 0;
317 chartabsize_T cts;
LemonBoy4b936742022-05-13 21:56:28 +0100318
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 /*
320 * 1. Get start vcol
321 * 2. Total ws vcols
322 * 3. Divvy into TABs & spp
323 * 4. Construct new string
324 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100325 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 ws_vcol = bd.start_vcol - bd.pre_whitesp;
327 if (bd.startspaces)
328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100330 {
331 if ((*mb_ptr2len)(bd.textstart) == 1)
332 ++bd.textstart;
333 else
334 {
335 ws_vcol = 0;
336 bd.startspaces = 0;
337 }
338 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000339 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000340 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100342
343 // TODO: is passing bd.textstart for start of the line OK?
344 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
345 bd.start_vcol, bd.textstart, bd.textstart);
346 for ( ; VIM_ISWHITE(*cts.cts_ptr); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100348 incr = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 total += incr;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100350 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100352 bd.textstart = cts.cts_ptr;
353 bd.start_vcol = cts.cts_vcol;
354 clear_chartabsize_arg(&cts);
355
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100356 // OK, now total=all the VWS reqd, and textstart points at the 1st
357 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200358#ifdef FEAT_VARTABS
359 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200360 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100361 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200362 else
LemonBoy4b936742022-05-13 21:56:28 +0100363 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200364#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100366 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
367 if (tabs > 0)
368 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 else
LemonBoy4b936742022-05-13 21:56:28 +0100370 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200371#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100374
375 new_line_len = bd.textcol + tabs + spaces + (int)STRLEN(bd.textstart);
376 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 if (newp == NULL)
378 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 mch_memmove(newp, oldp, (size_t)bd.textcol);
LemonBoy4b936742022-05-13 21:56:28 +0100380 vim_memset(newp + bd.textcol, TAB, (size_t)tabs);
381 vim_memset(newp + bd.textcol + tabs, ' ', (size_t)spaces);
382 // Note that STRMOVE() copies the trailing NUL.
383 STRMOVE(newp + bd.textcol + tabs + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100385 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387 colnr_T destination_col; // column to which text in block will
388 // be shifted
389 char_u *verbatim_copy_end; // end of the part of the line which is
390 // copied verbatim
391 colnr_T verbatim_copy_width;// the (displayed) width of this part
392 // of line
393 unsigned fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000394 size_t block_space_width;
395 size_t shift_amount;
396 char_u *non_white = bd.textstart;
397 colnr_T non_white_col;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100398 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000400 /*
401 * Firstly, let's find the first non-whitespace character that is
402 * displayed after the block's start column and the character's column
403 * number. Also, let's calculate the width of all the whitespace
404 * characters that are displayed in the block and precede the searched
405 * non-whitespace character.
406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100408 // If "bd.startspaces" is set, "bd.textstart" points to the character,
409 // the part of which is displayed at the block's beginning. Let's start
410 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000411 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100412 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000413
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100414 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000415 non_white_col = bd.start_vcol;
416
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100417 init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
418 non_white_col, bd.textstart, non_white);
419 while (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000420 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100421 incr = lbr_chartabsize_adv(&cts);
422 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100424 non_white_col = cts.cts_vcol;
425 non_white = cts.cts_ptr;
426 clear_chartabsize_arg(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000428 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100429 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000430 shift_amount = (block_space_width < (size_t)total
431 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100433 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000434 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100436 // Now let's find out how much of the beginning of the line we can
437 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000438 verbatim_copy_end = bd.textstart;
439 verbatim_copy_width = bd.start_vcol;
440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100441 // If "bd.startspaces" is set, "bd.textstart" points to the character
442 // preceding the block. We have to subtract its width to obtain its
443 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000444 if (bd.startspaces)
445 verbatim_copy_width -= bd.start_char_vcols;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100446 init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
447 bd.textstart, verbatim_copy_end);
448 while (cts.cts_vcol < destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000449 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100450 incr = lbr_chartabsize(&cts);
451 if (cts.cts_vcol + incr > destination_col)
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000452 break;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100453 cts.cts_vcol += incr;
454 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000455 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100456 verbatim_copy_width = cts.cts_vcol;
457 verbatim_copy_end = cts.cts_ptr;
458 clear_chartabsize_arg(&cts);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100460 // If "destination_col" is different from the width of the initial
461 // part of the line that will be copied, it means we encountered a tab
462 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000463 fill = destination_col - verbatim_copy_width;
464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100465 // The replacement line will consist of:
466 // - the beginning of the original line up to "verbatim_copy_end",
467 // - "fill" number of spaces,
468 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000469 new_line_len = (unsigned)(verbatim_copy_end - oldp)
470 + fill
LemonBoy4b936742022-05-13 21:56:28 +0100471 + (unsigned)STRLEN(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000472
LemonBoy4b936742022-05-13 21:56:28 +0100473 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (newp == NULL)
475 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000476 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200477 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
LemonBoy4b936742022-05-13 21:56:28 +0100478 // Note that STRMOVE() copies the trailing NUL.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000479 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100481 // replace the line
LemonBoy4b936742022-05-13 21:56:28 +0100482 added = new_line_len - (int)STRLEN(oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
LemonBoy4b936742022-05-13 21:56:28 +0100484 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 State = oldstate;
486 curwin->w_cursor.col = oldcol;
487#ifdef FEAT_RIGHTLEFT
488 p_ri = old_p_ri;
489#endif
490}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Insert string "s" (b_insert ? before : after) block :AKelly
494 * Caller must prepare for undo.
495 */
496 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100497block_insert(
498 oparg_T *oap,
499 char_u *s,
500 int b_insert,
501 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
Bram Moolenaardac13472019-09-16 21:06:21 +0200503 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100504 int count = 0; // extra spaces to replace a cut TAB
505 int spaces = 0; // non-zero if cutting a TAB
506 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200507 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 unsigned s_len; // STRLEN(s)
509 char_u *newp, *oldp; // new, old lines
510 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 int oldstate = State;
512
Bram Moolenaar24959102022-05-07 20:01:16 +0100513 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 s_len = (unsigned)STRLEN(s);
515
516 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
517 {
518 block_prep(oap, bdp, lnum, TRUE);
519 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100520 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
522 oldp = ml_get(lnum);
523
524 if (b_insert)
525 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200526 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 spaces = bdp->startspaces;
528 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100529 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 offset = bdp->textcol;
531 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200534 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100535 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200537 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100539 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 offset = bdp->textcol + bdp->textlen - (spaces != 0);
541 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100544 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (!bdp->is_MAX)
546 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
547 count = spaces;
548 offset = bdp->textcol + bdp->textlen;
549 }
550 }
551
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200552 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000553 // avoid copying part of a multi-byte character
554 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100555
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200556 if (spaces < 0) // can happen when the cursor was moved
557 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200558
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000559 // Make sure the allocated size matches what is actually copied below.
560 newp = alloc(STRLEN(oldp) + spaces + s_len
561 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
562 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 if (newp == NULL)
564 continue;
565
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100566 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000567 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 oldp += offset;
569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100570 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200571 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200572 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100574 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200575 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 offset += s_len;
577
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000578 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000580 if (*oldp == TAB)
581 {
582 // insert post-padding
583 vim_memset(newp + offset + spaces, ' ',
584 (size_t)(ts_val - spaces));
585 // we're splitting a TAB, don't copy it
586 oldp++;
587 // We allowed for that TAB, remember this now
588 count++;
589 }
590 else
591 // Not a TAB, no extra spaces
592 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594
595 if (spaces > 0)
596 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000597 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 ml_replace(lnum, newp, FALSE);
600
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200601 if (b_insert)
602 // correct any text properties
603 inserted_bytes(lnum, startcol, s_len);
604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (lnum == oap->end.lnum)
606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100607 // Set "']" mark to the end of the block instead of the end of
608 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 curbuf->b_op_end.lnum = oap->end.lnum;
610 curbuf->b_op_end.col = offset;
611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100612 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
615
616 State = oldstate;
617}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200620 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200622 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 */
624 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100625op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 int n;
628 linenr_T lnum;
629 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 char_u *newp, *oldp;
631 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
633 int did_yank = FALSE;
634
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100635 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 return OK;
637
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100638 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if (oap->empty)
640 return u_save_cursor();
641
642 if (!curbuf->b_p_ma)
643 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200644 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 return FAIL;
646 }
647
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000648 if (VIsual_select && oap->is_VIsual)
649 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100650 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000651
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#ifdef FEAT_CLIPBOARD
653 adjust_clip_reg(&oap->regname);
654#endif
655
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (has_mbyte)
657 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
Bram Moolenaard04b7502010-07-08 22:27:55 +0200659 /*
660 * Imitate the strange Vi behaviour: If the delete spans more than one
661 * line and motion_type == MCHAR and the result is a blank line, make the
662 * delete linewise. Don't do this for the change command or Visual mode.
663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000666 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100668 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 && oap->op_type == OP_DELETE)
670 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200671 ptr = ml_get(oap->end.lnum) + oap->end.col;
672 if (*ptr != NUL)
673 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 ptr = skipwhite(ptr);
675 if (*ptr == NUL && inindent(0))
676 oap->motion_type = MLINE;
677 }
678
Bram Moolenaard04b7502010-07-08 22:27:55 +0200679 /*
680 * Check for trying to delete (e.g. "D") in an empty line.
681 * Note: For the change operator it is ok.
682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 if ( oap->motion_type == MCHAR
684 && oap->line_count == 1
685 && oap->op_type == OP_DELETE
686 && *ml_get(oap->start.lnum) == NUL)
687 {
688 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000689 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 * 'cpoptions' (Vi compatible).
691 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000692 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100693 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
694 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000695 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
697 beep_flush();
698 return OK;
699 }
700
Bram Moolenaard04b7502010-07-08 22:27:55 +0200701 /*
702 * Do a yank of whatever we're about to delete.
703 * If a yank register was specified, put the deleted text into that
704 * register. For the black hole register '_' don't yank anything.
705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (oap->regname != '_')
707 {
708 if (oap->regname != 0)
709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100710 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 if (!valid_yank_reg(oap->regname, TRUE))
712 {
713 beep_flush();
714 return OK;
715 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100716 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
717 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 did_yank = TRUE;
719 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200720 else
721 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
723 /*
724 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100725 * delete contains a line break, or when using a specific operator (Vi
726 * compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100728 if (oap->motion_type == MLINE || oap->line_count > 1
729 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100731 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (op_yank(oap, TRUE, FALSE) == OK)
733 did_yank = TRUE;
734 }
735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100736 // Yank into small delete register when no named register specified
737 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200738 if ((
739#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200740 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
741 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200742#endif
743 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 && oap->line_count == 1)
745 {
746 oap->regname = '-';
747 get_yank_register(oap->regname, TRUE);
748 if (op_yank(oap, TRUE, FALSE) == OK)
749 did_yank = TRUE;
750 oap->regname = 0;
751 }
752
753 /*
754 * If there's too much stuff to fit in the yank register, then get a
755 * confirmation before doing the delete. This is crude, but simple.
756 * And it avoids doing a delete of something we can't put back if we
757 * want.
758 */
759 if (!did_yank)
760 {
761 int msg_silent_save = msg_silent;
762
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100763 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
765 msg_silent = msg_silent_save;
766 if (n != 'y')
767 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000768 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 return FAIL;
770 }
771 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100772
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100773#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100774 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200775 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778
Bram Moolenaard04b7502010-07-08 22:27:55 +0200779 /*
780 * block mode delete
781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (oap->block_mode)
783 {
784 if (u_save((linenr_T)(oap->start.lnum - 1),
785 (linenr_T)(oap->end.lnum + 1)) == FAIL)
786 return FAIL;
787
788 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
789 {
790 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 continue;
793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100794 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 if (lnum == curwin->w_cursor.lnum)
796 {
797 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
800
Bram Moolenaar8055d172019-05-17 22:57:26 +0200801 // "n" == number of chars deleted
802 // If we delete a TAB, it may be replaced by several characters.
803 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 n = bd.textlen - bd.startspaces - bd.endspaces;
805 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200806 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (newp == NULL)
808 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100811 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200812 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100814 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000816 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100817 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200819
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100820#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200821 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200822 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825
826 check_cursor_col();
827 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
828 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100831 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
833 if (oap->op_type == OP_CHANGE)
834 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100835 // Delete the lines except the first one. Temporarily move the
836 // cursor to the next line. Save the current line number, if the
837 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (oap->line_count > 1)
839 {
840 lnum = curwin->w_cursor.lnum;
841 ++curwin->w_cursor.lnum;
842 del_lines((long)(oap->line_count - 1), TRUE);
843 curwin->w_cursor.lnum = lnum;
844 }
845 if (u_save_cursor() == FAIL)
846 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100847 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100849 beginline(BL_WHITE); // cursor on first non-white
850 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 ai_col = curwin->w_cursor.col;
852 }
853 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 beginline(0); // cursor in column 0
855 truncate_line(FALSE); // delete the rest of the line
856 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860 else
861 {
862 del_lines(oap->line_count, TRUE);
863 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 }
867 else
868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (virtual_op)
870 {
871 int endcol = 0;
872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100873 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (gchar_pos(&oap->start) == '\t')
875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100876 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 return FAIL;
878 if (oap->line_count == 1)
879 endcol = getviscol2(oap->end.col, oap->end.coladd);
880 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
881 oap->start = curwin->w_cursor;
882 if (oap->line_count == 1)
883 {
884 coladvance(endcol);
885 oap->end.col = curwin->w_cursor.col;
886 oap->end.coladd = curwin->w_cursor.coladd;
887 curwin->w_cursor = oap->start;
888 }
889 }
890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100891 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 if (gchar_pos(&oap->end) == '\t'
893 && (int)oap->end.coladd < oap->inclusive)
894 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100895 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (u_save((linenr_T)(oap->end.lnum - 1),
897 (linenr_T)(oap->end.lnum + 1)) == FAIL)
898 return FAIL;
899 curwin->w_cursor = oap->end;
900 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
901 oap->end = curwin->w_cursor;
902 curwin->w_cursor = oap->start;
903 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100904 if (has_mbyte)
905 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100908 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100910 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 return FAIL;
912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100913 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100914 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 && oap->op_type == OP_CHANGE
916 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100917 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 display_dollar(oap->end.col - !oap->inclusive);
919
920 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (virtual_op)
923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 // fix up things for virtualedit-delete:
925 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 char_u *curline = ml_get_curline();
927 int len = (int)STRLEN(curline);
928
929 if (oap->end.coladd != 0
930 && (int)oap->end.col >= len - 1
931 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
932 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100933 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (n == 0 && oap->start.coladd != oap->end.coladd)
935 n = 1;
936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100937 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 if (gchar_cursor() != NUL)
939 curwin->w_cursor.coladd = 0;
940 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200941 (void)del_bytes((long)n, !virtual_op,
942 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100944 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
946 pos_T curpos;
947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
950 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
951 return FAIL;
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100955 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 ++curwin->w_cursor.lnum;
957 del_lines((long)(oap->line_count - 2), FALSE);
958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200960 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200961 curwin->w_cursor.col = 0;
962 (void)del_bytes((long)n, !virtual_op,
963 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100964 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200965 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200967 if (oap->op_type == OP_DELETE)
968 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970
971 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
972
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000973setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200974 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100976 if (oap->block_mode)
977 {
978 curbuf->b_op_end.lnum = oap->end.lnum;
979 curbuf->b_op_end.col = oap->start.col;
980 }
981 else
982 curbuf->b_op_end = oap->start;
983 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 return OK;
987}
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Adjust end of operating area for ending on a multi-byte character.
991 * Used for deletion.
992 */
993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100994mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 char_u *p;
997
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000998 if (!oap->inclusive)
999 return;
1000
1001 p = ml_get(oap->end.lnum);
1002 oap->end.col += mb_tail_off(p, p + oap->end.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar630afe82018-06-28 19:26:28 +02001005/*
1006 * Replace the character under the cursor with "c".
1007 * This takes care of multi-byte characters.
1008 */
1009 static void
1010replace_character(int c)
1011{
1012 int n = State;
1013
Bram Moolenaar24959102022-05-07 20:01:16 +01001014 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001015 ins_char(c);
1016 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001017 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001018 dec_cursor();
1019}
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021/*
1022 * Replace a whole area with one character.
1023 */
1024 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001025op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
1027 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 char_u *newp, *oldp;
1030 size_t oldlen;
1031 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001032 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001033 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001036 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001038 if (c == REPLACE_CR_NCHAR)
1039 {
1040 had_ctrl_v_cr = TRUE;
1041 c = CAR;
1042 }
1043 else if (c == REPLACE_NL_NCHAR)
1044 {
1045 had_ctrl_v_cr = TRUE;
1046 c = NL;
1047 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (has_mbyte)
1050 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
1052 if (u_save((linenr_T)(oap->start.lnum - 1),
1053 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1054 return FAIL;
1055
1056 /*
1057 * block mode replace
1058 */
1059 if (oap->block_mode)
1060 {
1061 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1062 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1063 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1066 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 // n == number of extra chars required
1070 // If we split a TAB, it may be replaced by several characters.
1071 // Thus the number of characters may increase!
1072 // If the range starts in virtual space, count the initial
1073 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1075 {
1076 pos_T vpos;
1077
Bram Moolenaara1381de2009-11-03 15:44:21 +00001078 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 getvpos(&vpos, oap->start_vcol);
1080 bd.startspaces += vpos.coladd;
1081 n = bd.startspaces;
1082 }
1083 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001084 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001087 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001091 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 numc = oap->end_vcol - oap->start_vcol + 1;
1093 if (bd.is_short && (!virtual_op || bd.is_MAX))
1094 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001096 // A double-wide character can be replaced only up to half the
1097 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if ((*mb_char2cells)(c) > 1)
1099 {
1100 if ((numc & 1) && !bd.is_short)
1101 {
1102 ++bd.endspaces;
1103 ++n;
1104 }
1105 numc = numc / 2;
1106 }
1107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001108 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 num_chars = numc;
1110 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001111 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 n += numc - bd.textlen;
1113
1114 oldp = ml_get_curline();
1115 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001116 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (newp == NULL)
1118 continue;
1119 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001120 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 mch_memmove(newp, oldp, (size_t)bd.textcol);
1122 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001123 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001124 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001125 // insert replacement chars CHECK FOR ALLOCATED SPACE
1126 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1127 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001128 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001130 if (has_mbyte)
1131 {
1132 n = (int)STRLEN(newp);
1133 while (--num_chars >= 0)
1134 n += (*mb_char2bytes)(c, newp + n);
1135 }
1136 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001137 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001138 if (!bd.is_short)
1139 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001140 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001141 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001142 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001143 STRMOVE(newp + STRLEN(newp), oldp);
1144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001148 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001149 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001150 if (after_p != NULL)
1151 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001153 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001155 if (after_p != NULL)
1156 {
1157 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1158 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1159 oap->end.lnum++;
1160 vim_free(after_p);
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 }
1164 else
1165 {
1166 /*
1167 * MCHAR and MLINE motion replace.
1168 */
1169 if (oap->motion_type == MLINE)
1170 {
1171 oap->start.col = 0;
1172 curwin->w_cursor.col = 0;
1173 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1174 if (oap->end.col)
1175 --oap->end.col;
1176 }
1177 else if (!oap->inclusive)
1178 dec(&(oap->end));
1179
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001180 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarc2499132022-09-16 22:16:59 +01001182 int done = FALSE;
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 n = gchar_cursor();
1185 if (n != NUL)
1186 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001187 int new_byte_len = (*mb_char2len)(c);
1188 int old_byte_len = mb_ptr2len(ml_get_cursor());
1189
1190 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001192 // This is slow, but it handles replacing a single-byte
1193 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001194 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001195 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001196 replace_character(c);
Bram Moolenaarc2499132022-09-16 22:16:59 +01001197 done = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (n == TAB)
1202 {
1203 int end_vcol = 0;
1204
1205 if (curwin->w_cursor.lnum == oap->end.lnum)
1206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001207 // oap->end has to be recalculated when
1208 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 end_vcol = getviscol2(oap->end.col,
1210 oap->end.coladd);
1211 }
1212 coladvance_force(getviscol());
1213 if (curwin->w_cursor.lnum == oap->end.lnum)
1214 getvpos(&oap->end, end_vcol);
1215 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001216 // with "coladd" set may move to just after a TAB
1217 if (gchar_cursor() != NUL)
1218 {
1219 PBYTE(curwin->w_cursor, c);
1220 done = TRUE;
1221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 }
Bram Moolenaarc2499132022-09-16 22:16:59 +01001224 if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 int virtcols = oap->end.coladd;
1227
1228 if (curwin->w_cursor.lnum == oap->start.lnum
1229 && oap->start.col == oap->end.col && oap->start.coladd)
1230 virtcols -= oap->start.coladd;
1231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001232 // oap->end has been trimmed so it's effectively inclusive;
1233 // as a result an extra +1 must be counted so we don't
1234 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1236 curwin->w_cursor.col -= (virtcols + 1);
1237 for (; virtcols >= 0; virtcols--)
1238 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001239 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001240 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001241 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001242 PBYTE(curwin->w_cursor, c);
1243 if (inc(&curwin->w_cursor) == -1)
1244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001248 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (inc_cursor() == -1)
1250 break;
1251 }
1252 }
1253
1254 curwin->w_cursor = oap->start;
1255 check_cursor();
1256 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1257
Bram Moolenaare1004402020-10-24 20:49:43 +02001258 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001259 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001260 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001261 curbuf->b_op_start = oap->start;
1262 curbuf->b_op_end = oap->end;
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 return OK;
1266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001268static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/*
1271 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1272 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001274op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001278 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280 if (u_save((linenr_T)(oap->start.lnum - 1),
1281 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1282 return;
1283
1284 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001285 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
1287 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1288 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001289 int one_change;
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 block_prep(oap, &bd, pos.lnum, FALSE);
1292 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001293 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1294 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001295
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001296#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001297 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001299 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
Bram Moolenaar009b2592004-10-24 19:18:58 +00001301 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1302 (long)bd.textlen);
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01001303 // get the line now, it may have been flushed
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001304 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001306 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 if (did_change)
1311 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1312 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001313 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
1315 if (oap->motion_type == MLINE)
1316 {
1317 oap->start.col = 0;
1318 pos.col = 0;
1319 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1320 if (oap->end.col)
1321 --oap->end.col;
1322 }
1323 else if (!oap->inclusive)
1324 dec(&(oap->end));
1325
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001326 if (pos.lnum == oap->end.lnum)
1327 did_change = swapchars(oap->op_type, &pos,
1328 oap->end.col - pos.col + 1);
1329 else
1330 for (;;)
1331 {
1332 did_change |= swapchars(oap->op_type, &pos,
1333 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1334 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001335 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001336 break;
1337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (did_change)
1339 {
1340 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1341 0L);
1342#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001343 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
1345 char_u *ptr;
1346 int count;
1347
1348 pos = oap->start;
1349 while (pos.lnum < oap->end.lnum)
1350 {
1351 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001352 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001353 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001354 // get the line again, it may have been flushed
1355 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001357 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 pos.col = 0;
1359 pos.lnum++;
1360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001362 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001363 // get the line again, it may have been flushed
1364 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001366 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368#endif
1369 }
1370 }
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001373 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001374 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
Bram Moolenaare1004402020-10-24 20:49:43 +02001376 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001377 {
1378 // Set '[ and '] marks.
1379 curbuf->b_op_start = oap->start;
1380 curbuf->b_op_end = oap->end;
1381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
1383 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001384 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001385 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386}
1387
1388/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001389 * Invoke swapchar() on "length" bytes at position "pos".
1390 * "pos" is advanced to just after the changed characters.
1391 * "length" is rounded up to include the whole last multi-byte character.
1392 * Also works correctly when the number of bytes changes.
1393 * Returns TRUE if some character was changed.
1394 */
1395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001396swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001397{
1398 int todo;
1399 int did_change = 0;
1400
1401 for (todo = length; todo > 0; --todo)
1402 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001403 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001404 {
1405 int len = (*mb_ptr2len)(ml_get_pos(pos));
1406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001407 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001408 if (len > 0)
1409 todo -= len - 1;
1410 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001411 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001412 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001413 break;
1414 }
1415 return did_change;
1416}
1417
1418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 * If op_type == OP_UPPER: make uppercase,
1420 * if op_type == OP_LOWER: make lowercase,
1421 * if op_type == OP_ROT13: do rot13 encoding,
1422 * else swap case of character at 'pos'
1423 * returns TRUE when something actually changed.
1424 */
1425 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001426swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 int c;
1429 int nc;
1430
1431 c = gchar_pos(pos);
1432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001433 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (c >= 0x80 && op_type == OP_ROT13)
1435 return FALSE;
1436
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001437 if (op_type == OP_UPPER && c == 0xdf
1438 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001439 {
1440 pos_T sp = curwin->w_cursor;
1441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001442 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001443 curwin->w_cursor = *pos;
1444 del_char(FALSE);
1445 ins_char('S');
1446 ins_char('S');
1447 curwin->w_cursor = sp;
1448 inc(pos);
1449 }
1450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001451 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 nc = c;
1454 if (MB_ISLOWER(c))
1455 {
1456 if (op_type == OP_ROT13)
1457 nc = ROT13(c, 'a');
1458 else if (op_type != OP_LOWER)
1459 nc = MB_TOUPPER(c);
1460 }
1461 else if (MB_ISUPPER(c))
1462 {
1463 if (op_type == OP_ROT13)
1464 nc = ROT13(c, 'A');
1465 else if (op_type != OP_UPPER)
1466 nc = MB_TOLOWER(c);
1467 }
1468 if (nc != c)
1469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1471 {
1472 pos_T sp = curwin->w_cursor;
1473
1474 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001476 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 ins_char(nc);
1478 curwin->w_cursor = sp;
1479 }
1480 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001481 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 return TRUE;
1483 }
1484 return FALSE;
1485}
1486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487/*
1488 * op_insert - Insert and append operators for Visual mode.
1489 */
1490 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001491op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 long ins_len, pre_textlen = 0;
1494 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001495 colnr_T ind_pre_col = 0, ind_post_col;
1496 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 struct block_def bd;
1498 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001499 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001500 pos_T start_insert;
1501 // offset when cursor was moved in insert mode
1502 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001504 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001507 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001509 update_screen(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
1511 if (oap->block_mode)
1512 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001513 // When 'virtualedit' is used, need to insert the extra spaces before
1514 // doing block_prep(). When only "block" is used, virtual edit is
1515 // already disabled, but still need it when calling
1516 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001517 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001518 // state for the current window. To override that state, we need to
1519 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (curwin->w_cursor.coladd > 0)
1521 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001522 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (u_save_cursor() == FAIL)
1525 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001526
Gary Johnson51ad8502021-08-03 18:33:08 +02001527 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 coladvance_force(oap->op_type == OP_APPEND
1529 ? oap->end_vcol + 1 : getviscol());
1530 if (oap->op_type == OP_APPEND)
1531 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001532 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001534 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001536 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001537 ind_pre_col = (colnr_T)getwhitecols_curline();
1538 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (oap->op_type == OP_APPEND)
1542 firstline += bd.textlen;
1543 pre_textlen = (long)STRLEN(firstline);
1544 }
1545
1546 if (oap->op_type == OP_APPEND)
1547 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001548 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001550 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 curwin->w_set_curswant = TRUE;
1552 while (*ml_get_cursor() != NUL
1553 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1554 ++curwin->w_cursor.col;
1555 if (bd.is_short && !bd.is_MAX)
1556 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001557 // First line was too short, make it longer and adjust the
1558 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (u_save_cursor() == FAIL)
1560 return;
1561 for (i = 0; i < bd.endspaces; ++i)
1562 ins_char(' ');
1563 bd.textlen += bd.endspaces;
1564 }
1565 }
1566 else
1567 {
1568 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001569 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001571 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001572 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 && oap->start_vcol != oap->end_vcol)
1574 inc_cursor();
1575 }
1576 }
1577
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001578 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001579 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001580 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001582 // When a tab was inserted, and the characters in front of the tab
1583 // have been converted to a tab as well, the column of the cursor
1584 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001585 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001586 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001587 oap->start = curbuf->b_op_start_orig;
1588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001589 // If user has moved off this line, we don't know what to do, so do
1590 // nothing.
1591 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001592 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 return;
1594
1595 if (oap->block_mode)
1596 {
1597 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001598 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001599 size_t len;
1600 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001602 // If indent kicked in, the firstline might have changed
1603 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001604 ind_post_col = (colnr_T)getwhitecols_curline();
1605 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001606 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001607 bd.textcol += ind_post_col - ind_pre_col;
1608 ind_post_vcol = get_indent();
1609 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001610 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001611 }
1612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001613 // The user may have moved the cursor before inserting something, try
1614 // to adjust the block for that. But only do it, if the difference
1615 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001616 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1617 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001618 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001619 int t = getviscol2(curbuf->b_op_start_orig.col,
1620 curbuf->b_op_start_orig.coladd);
1621
zeertzjq7745f142022-02-15 11:48:22 +00001622 if (oap->op_type == OP_INSERT
1623 && oap->start.col + oap->start.coladd
1624 != curbuf->b_op_start_orig.col
1625 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001626 {
zeertzjq7745f142022-02-15 11:48:22 +00001627 oap->start.col = curbuf->b_op_start_orig.col;
1628 pre_textlen -= t - oap->start_vcol;
1629 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001630 }
zeertzjq7745f142022-02-15 11:48:22 +00001631 else if (oap->op_type == OP_APPEND
1632 && oap->start.col + oap->start.coladd
1633 >= curbuf->b_op_start_orig.col
1634 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001635 {
zeertzjq7745f142022-02-15 11:48:22 +00001636 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001637 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001638 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001639 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001640 oap->start_vcol = t;
1641 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001642 }
1643 }
1644
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001645 // Spaces and tabs in the indent may have changed to other spaces and
1646 // tabs. Get the starting column again and correct the length.
1647 // Don't do this when "$" used, end-of-line will have changed.
1648 //
1649 // if indent was added and the inserted text was after the indent,
1650 // correct the selection for the new indent.
1651 if (did_indent && bd.textcol - ind_post_col > 0)
1652 {
1653 oap->start.col += ind_post_col - ind_pre_col;
1654 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1655 oap->end.col += ind_post_col - ind_pre_col;
1656 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001659 if (did_indent && bd.textcol - ind_post_col > 0)
1660 {
1661 // undo for where "oap" is used below
1662 oap->start.col -= ind_post_col - ind_pre_col;
1663 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1664 oap->end.col -= ind_post_col - ind_pre_col;
1665 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1668 {
1669 if (oap->op_type == OP_APPEND)
1670 {
1671 pre_textlen += bd2.textlen - bd.textlen;
1672 if (bd2.endspaces)
1673 --bd2.textlen;
1674 }
1675 bd.textcol = bd2.textcol;
1676 bd.textlen = bd2.textlen;
1677 }
1678
1679 /*
1680 * Subsequent calls to ml_get() flush the firstline data - take a
1681 * copy of the required string.
1682 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001683 firstline = ml_get(oap->start.lnum);
1684 len = STRLEN(firstline);
1685 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001687 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001688 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001689 // account for pressing cursor in insert mode when '$' was used
1690 if (bd.is_MAX
1691 && (start_insert.lnum == Insstart.lnum
1692 && start_insert.col > Insstart.col))
1693 {
1694 offset = (start_insert.col - Insstart.col);
1695 add -= offset;
1696 if (oap->end_vcol > offset)
1697 oap->end_vcol -= (offset + 1);
1698 else
1699 // moved outside of the visual block, what to do?
1700 return;
1701 }
1702 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001703 if ((size_t)add > len)
1704 firstline += len; // short line, point to the NUL
1705 else
1706 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001707 if (pre_textlen >= 0 && (ins_len =
1708 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001710 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if (ins_text != NULL)
1712 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001713 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (u_save(oap->start.lnum,
1715 (linenr_T)(oap->end.lnum + 1)) == OK)
1716 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1717 &bd);
1718
1719 curwin->w_cursor.col = oap->start.col;
1720 check_cursor();
1721 vim_free(ins_text);
1722 }
1723 }
1724 }
1725}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727/*
1728 * op_change - handle a change operation
1729 *
1730 * return TRUE if edit() returns because of a CTRL-O command
1731 */
1732 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001733op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 colnr_T l;
1736 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 long offset;
1738 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001739 long ins_len;
1740 long pre_textlen = 0;
1741 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 char_u *firstline;
1743 char_u *ins_text, *newp, *oldp;
1744 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746 l = oap->start.col;
1747 if (oap->motion_type == MLINE)
1748 {
1749 l = 0;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001750 can_si = may_do_si(); // Like opening a new line, do smart indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001753 // First delete the text in the region. In an empty buffer only need to
1754 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1756 {
1757 if (u_save_cursor() == FAIL)
1758 return FALSE;
1759 }
1760 else if (op_delete(oap) == FAIL)
1761 return FALSE;
1762
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001763 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 && !virtual_op)
1765 inc_cursor();
1766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001767 // check for still on same line (<CR> in inserted text meaningless)
1768 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (oap->block_mode)
1770 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001771 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 if (virtual_op && (curwin->w_cursor.coladd > 0
1773 || gchar_cursor() == NUL))
1774 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001775 firstline = ml_get(oap->start.lnum);
1776 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001777 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 bd.textcol = curwin->w_cursor.col;
1779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (oap->motion_type == MLINE)
1782 fix_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
zeertzjqcdeb6572022-11-15 13:46:12 +00001784 // Reset finish_op now, don't want it set inside edit().
1785 int save_finish_op = finish_op;
1786 finish_op = FALSE;
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 retval = edit(NUL, FALSE, (linenr_T)1);
1789
zeertzjqcdeb6572022-11-15 13:46:12 +00001790 finish_op = save_finish_op;
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001793 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001795 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001797 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 // Auto-indenting may have changed the indent. If the cursor was past
1800 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001802 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001804 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001805
1806 pre_textlen += new_indent - pre_indent;
1807 bd.textcol += new_indent - pre_indent;
1808 }
1809
1810 ins_len = (long)STRLEN(firstline) - pre_textlen;
1811 if (ins_len > 0)
1812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001813 // Subsequent calls to ml_get() flush the firstline data - take a
1814 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001815 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001817 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1819 linenr++)
1820 {
1821 block_prep(oap, &bd, linenr, TRUE);
1822 if (!bd.is_short || virtual_op)
1823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 pos_T vpos;
1825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001826 // If the block starts in virtual space, count the
1827 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (bd.is_short)
1829 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001830 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 else
1834 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001836 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (newp == NULL)
1838 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001839 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 mch_memmove(newp, oldp, (size_t)bd.textcol);
1841 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001842 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1845 offset += ins_len;
1846 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001847 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 ml_replace(linenr, newp, FALSE);
LemonBoyb559b302022-05-15 13:08:02 +01001849#ifdef FEAT_PROP_POPUP
1850 // Shift the properties for linenr as edit() would do.
1851 if (curbuf->b_has_textprop)
1852 adjust_prop_columns(linenr, bd.textcol,
1853 vpos.coladd + ins_len, 0);
1854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856 }
1857 check_cursor();
1858
1859 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1860 }
1861 vim_free(ins_text);
1862 }
1863 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001864 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
1866 return retval;
1867}
1868
1869/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001870 * When the cursor is on the NUL past the end of the line and it should not be
1871 * there move it left.
1872 */
1873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001874adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001875{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001876 unsigned int cur_ve_flags = get_ve_flags();
1877
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001878 int adj_cursor = (curwin->w_cursor.col > 0
1879 && gchar_cursor() == NUL
1880 && (cur_ve_flags & VE_ONEMORE) == 0
1881 && !(restart_edit || (State & MODE_INSERT)));
1882 if (!adj_cursor)
1883 return;
1884
1885 // Put the cursor on the last character in the line.
1886 dec_cursor();
1887
1888 if (cur_ve_flags == VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001890 colnr_T scol, ecol;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001891
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001892 // Coladd is set to the width of the last character.
1893 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1894 curwin->w_cursor.coladd = ecol - scol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896}
1897
Bram Moolenaar81340392012-06-06 16:12:59 +02001898/*
1899 * If "process" is TRUE and the line begins with a comment leader (possibly
1900 * after some white space), return a pointer to the text after it. Put a boolean
1901 * value indicating whether the line ends with an unclosed comment in
1902 * "is_comment".
1903 * line - line to be processed,
1904 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001905 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001906 * include_space - whether to also skip space following the comment leader,
1907 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001908 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001909 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001910 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001911skip_comment(
1912 char_u *line,
1913 int process,
1914 int include_space,
1915 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001916{
1917 char_u *comment_flags = NULL;
1918 int lead_len;
1919 int leader_offset = get_last_leader_offset(line, &comment_flags);
1920
1921 *is_comment = FALSE;
1922 if (leader_offset != -1)
1923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001924 // Let's check whether the line ends with an unclosed comment.
1925 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001926 while (*comment_flags)
1927 {
1928 if (*comment_flags == COM_END
1929 || *comment_flags == ':')
1930 break;
1931 ++comment_flags;
1932 }
1933 if (*comment_flags != COM_END)
1934 *is_comment = TRUE;
1935 }
1936
1937 if (process == FALSE)
1938 return line;
1939
1940 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1941
1942 if (lead_len == 0)
1943 return line;
1944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 // Find:
1946 // - COM_END,
1947 // - colon,
1948 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001949 while (*comment_flags)
1950 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001951 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001953 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001954 ++comment_flags;
1955 }
1956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001957 // If we found a colon, it means that we are not processing a line
1958 // starting with a closing part of a three-part comment. That's good,
1959 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001960 if (*comment_flags == ':' || *comment_flags == NUL)
1961 line += lead_len;
1962
1963 return line;
1964}
Bram Moolenaar81340392012-06-06 16:12:59 +02001965
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966/*
Bram Moolenaar7d0f7e92022-08-06 17:10:57 +01001967 * Join 'count' lines (minimal 2) at the cursor position.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001968 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001969 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1970 * leaders should not be removed.
1971 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1972 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001974 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 */
1976 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001977do_join(
1978 long count,
1979 int insert_space,
1980 int save_undo,
1981 int use_formatoptions UNUSED,
1982 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001984 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001985 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001986 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001988 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001989 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001990 int endcurr1 = NUL;
1991 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001992 int currsize = 0; // size of the current line
1993 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001995 colnr_T col = 0;
1996 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001997 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001998 int remove_comments = (use_formatoptions == TRUE)
1999 && has_format_option(FO_REMOVE_COMS);
2000 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002001#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002002 int propcount = 0; // number of props over all joined lines
2003 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002006 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2007 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2008 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002010 // Allocate an array to store the number of spaces inserted before each
2011 // line. We will use it to pre-compute the length of the new line and the
2012 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002013 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002014 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002016 if (remove_comments)
2017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002018 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002019 if (comments == NULL)
2020 {
2021 vim_free(spaces);
2022 return FAIL;
2023 }
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002027 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002028 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002029 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002031 for (t = 0; t < count; ++t)
2032 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002033 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002034#ifdef FEAT_PROP_POPUP
Bram Moolenaare175dc62022-08-01 22:18:50 +01002035 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t),
2036 t > 0, t + 1 == count);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002037#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002038 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002040 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002041 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2042 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2043 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002044 if (remove_comments)
2045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002046 // We don't want to remove the comment leader if the
2047 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002048 if (t > 0 && prev_was_comment)
2049 {
2050
2051 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2052 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002053 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002054 curr = new_curr;
2055 }
2056 else
2057 curr = skip_comment(curr, FALSE, insert_space,
2058 &prev_was_comment);
2059 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002060
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002061 if (insert_space && t > 0)
2062 {
2063 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002064 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002065 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002066 && (!has_format_option(FO_MBYTE_JOIN)
2067 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2068 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002069 || (mb_ptr2char(curr) < 0x100
2070 && !(enc_utf8 && utf_eat_space(endcurr1)))
2071 || (endcurr1 < 0x100
2072 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002073 )
2074 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002075 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002076 if (endcurr1 == ' ')
2077 endcurr1 = endcurr2;
2078 else
2079 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002080 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002081 if ( p_js
2082 && (endcurr1 == '.'
2083 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2084 && (endcurr1 == '?' || endcurr1 == '!'))))
2085 ++spaces[t];
2086 }
2087 }
2088 currsize = (int)STRLEN(curr);
2089 sumsize += currsize + spaces[t];
2090 endcurr1 = endcurr2 = NUL;
2091 if (insert_space && currsize > 0)
2092 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002093 if (has_mbyte)
2094 {
2095 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002096 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002097 endcurr1 = (*mb_ptr2char)(cend);
2098 if (cend > curr)
2099 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002100 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002101 endcurr2 = (*mb_ptr2char)(cend);
2102 }
2103 }
2104 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002105 {
2106 endcurr1 = *(curr + currsize - 1);
2107 if (currsize > 1)
2108 endcurr2 = *(curr + currsize - 2);
2109 }
2110 }
2111 line_breakcheck();
2112 if (got_int)
2113 {
2114 ret = FAIL;
2115 goto theend;
2116 }
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002119 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002120 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002122 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002123 newp_len = sumsize + 1;
2124#ifdef FEAT_PROP_POPUP
2125 newp_len += propcount * sizeof(textprop_T);
2126#endif
2127 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002128 if (newp == NULL)
2129 {
2130 ret = FAIL;
2131 goto theend;
2132 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002133 cend = newp + sumsize;
2134 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002136 /*
2137 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002138 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002139 *
2140 * Move marks from each deleted line to the joined line, adjusting the
2141 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2142 * should not really be a problem.
2143 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002144#ifdef FEAT_PROP_POPUP
2145 props_remaining = propcount;
2146#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002147 for (t = count - 1; ; --t)
2148 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002149 int spaces_removed;
2150
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002151 cend -= currsize;
2152 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002153
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002154 if (spaces[t] > 0)
2155 {
2156 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002157 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002158 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002159
2160 // If deleting more spaces than adding, the cursor moves no more than
2161 // what is added if it is inside these spaces.
2162 spaces_removed = (curr - curr_start) - spaces[t];
2163
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002164 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002165 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002166#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002167 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2168 curwin->w_cursor.lnum + t, t == count - 1,
2169 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002170#endif
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002171 if (t == 0)
2172 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002173 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002174 if (remove_comments)
2175 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002176 if (insert_space && t > 1)
2177 curr = skipwhite(curr);
2178 currsize = (int)STRLEN(curr);
2179 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002180
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002181 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaare1004402020-10-24 20:49:43 +02002183 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002184 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002185 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002186 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002187 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002188 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002190 // Only report the change in the first line here, del_lines() will report
2191 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 changed_lines(curwin->w_cursor.lnum, currsize,
2193 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002195 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * briefly, and then move it back. After del_lines() the cursor may
2197 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 */
2199 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002201 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 curwin->w_cursor.lnum = t;
2203
2204 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002205 * Set the cursor column:
2206 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002207 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002209 curwin->w_cursor.col =
2210 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 curwin->w_set_curswant = TRUE;
2215
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002216theend:
2217 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002218 if (remove_comments)
2219 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002220 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221}
2222
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002223#ifdef FEAT_LINEBREAK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224/*
Bram Moolenaar16dab412022-10-08 16:41:32 +01002225 * Reset 'linebreak' and take care of side effects.
2226 * Returns the previous value, to be passed to restore_lbr().
2227 */
2228 static int
2229reset_lbr(void)
2230{
2231 if (!curwin->w_p_lbr)
2232 return FALSE;
2233 // changing 'linebreak' may require w_virtcol to be updated
2234 curwin->w_p_lbr = FALSE;
2235 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
2236 return TRUE;
2237}
2238
2239/*
2240 * Restore 'linebreak' and take care of side effects.
2241 */
2242 static void
2243restore_lbr(int lbr_saved)
2244{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002245 if (curwin->w_p_lbr || !lbr_saved)
2246 return;
2247
2248 // changing 'linebreak' may require w_virtcol to be updated
2249 curwin->w_p_lbr = TRUE;
2250 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
Bram Moolenaar16dab412022-10-08 16:41:32 +01002251}
Bram Moolenaarbf499c02022-10-08 17:55:32 +01002252#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002253
2254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 * prepare a few things for block mode yank/delete/tilde
2256 *
2257 * for delete:
2258 * - textlen includes the first/last char to be (partly) deleted
2259 * - start/endspaces is the number of columns that are taken by the
2260 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002261 * deleted.
2262 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 * - textlen includes the first/last char to be wholly yanked
2264 * - start/endspaces is the number of columns of the first/last yanked char
2265 * that are to be yanked.
2266 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002267 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002268block_prep(
2269 oparg_T *oap,
2270 struct block_def *bdp,
2271 linenr_T lnum,
2272 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 int incr = 0;
2275 char_u *pend;
2276 char_u *pstart;
2277 char_u *line;
2278 char_u *prev_pstart;
2279 char_u *prev_pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002280 chartabsize_T cts;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002281#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002282 // Avoid a problem with unwanted linebreaks in block mode.
2283 int lbr_saved = reset_lbr();
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002284#endif
Bram Moolenaar16dab412022-10-08 16:41:32 +01002285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 bdp->startspaces = 0;
2287 bdp->endspaces = 0;
2288 bdp->textlen = 0;
2289 bdp->start_vcol = 0;
2290 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 bdp->is_short = FALSE;
2292 bdp->is_oneChar = FALSE;
2293 bdp->pre_whitesp = 0;
2294 bdp->pre_whitesp_c = 0;
2295 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 bdp->start_char_vcols = 0;
2297
2298 line = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 prev_pstart = line;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002300 init_chartabsize_arg(&cts, curwin, lnum, bdp->start_vcol, line, line);
2301 while (cts.cts_vcol < oap->start_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002304 incr = lbr_chartabsize(&cts);
2305 cts.cts_vcol += incr;
2306 if (VIM_ISWHITE(*cts.cts_ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 bdp->pre_whitesp += incr;
2309 bdp->pre_whitesp_c++;
2310 }
2311 else
2312 {
2313 bdp->pre_whitesp = 0;
2314 bdp->pre_whitesp_c = 0;
2315 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002316 prev_pstart = cts.cts_ptr;
2317 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002319 bdp->start_vcol = cts.cts_vcol;
2320 pstart = cts.cts_ptr;
2321 clear_chartabsize_arg(&cts);
2322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002324 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
2326 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (!is_del || oap->op_type == OP_APPEND)
2329 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2330 }
2331 else
2332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 // notice: this converts partly selected Multibyte characters to
2334 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2336 if (is_del && bdp->startspaces)
2337 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2338 pend = pstart;
2339 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (oap->op_type == OP_INSERT)
2344 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2345 else if (oap->op_type == OP_APPEND)
2346 {
2347 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2348 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2349 }
2350 else
2351 {
2352 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2353 if (is_del && oap->op_type != OP_LSHIFT)
2354 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002355 // just putting the sum of those two into
2356 // bdp->startspaces doesn't work for Visual replace,
2357 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 bdp->startspaces = bdp->start_char_vcols
2359 - (bdp->start_vcol - oap->start_vcol);
2360 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2361 }
2362 }
2363 }
2364 else
2365 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002366 init_chartabsize_arg(&cts, curwin, lnum, bdp->end_vcol,
2367 line, pend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 prev_pend = pend;
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002369 while (cts.cts_vcol <= oap->end_vcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002371 // count a tab for what it's worth (if list mode not on)
2372 prev_pend = cts.cts_ptr;
2373 incr = lbr_chartabsize_adv(&cts);
2374 cts.cts_vcol += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01002376 bdp->end_vcol = cts.cts_vcol;
2377 pend = cts.cts_ptr;
2378 clear_chartabsize_arg(&cts);
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (bdp->end_vcol <= oap->end_vcol
2381 && (!is_del
2382 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002383 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // Alternative: include spaces to fill up the block.
2387 // Disadvantage: can lead to trailing spaces when the line is
2388 // short where the text is put
2389 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 if (oap->op_type == OP_APPEND || virtual_op)
2391 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002392 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002394 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396 else if (bdp->end_vcol > oap->end_vcol)
2397 {
2398 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2399 if (!is_del && bdp->endspaces)
2400 {
2401 bdp->endspaces = incr - bdp->endspaces;
2402 if (pend != pstart)
2403 pend = prev_pend;
2404 }
2405 }
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (is_del && bdp->startspaces)
2409 pstart = prev_pstart;
2410 bdp->textlen = (int)(pend - pstart);
2411 }
2412 bdp->textcol = (colnr_T) (pstart - line);
2413 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002414#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01002415 restore_lbr(lbr_saved);
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002420 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002422 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002423op_addsub(
2424 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002425 linenr_T Prenum1, // Amount of add/subtract
2426 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002428 pos_T pos;
2429 struct block_def bd;
2430 int change_cnt = 0;
2431 linenr_T amount = Prenum1;
2432
Bram Moolenaar6b731882018-11-16 20:54:47 +01002433 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002434 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002435 // the call to changed_lines().
2436#ifdef FEAT_FOLDING
2437 disable_fold_update++;
2438#endif
2439
Bram Moolenaard79e5502016-01-10 22:13:02 +01002440 if (!VIsual_active)
2441 {
2442 pos = curwin->w_cursor;
2443 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002444 {
2445#ifdef FEAT_FOLDING
2446 disable_fold_update--;
2447#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002448 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002449 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002450 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002451#ifdef FEAT_FOLDING
2452 disable_fold_update--;
2453#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002454 if (change_cnt)
2455 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2456 }
2457 else
2458 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002459 int one_change;
2460 int length;
2461 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002462
2463 if (u_save((linenr_T)(oap->start.lnum - 1),
2464 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002465 {
2466#ifdef FEAT_FOLDING
2467 disable_fold_update--;
2468#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002469 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002470 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002471
2472 pos = oap->start;
2473 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2474 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002475 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002476 {
2477 block_prep(oap, &bd, pos.lnum, FALSE);
2478 pos.col = bd.textcol;
2479 length = bd.textlen;
2480 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002481 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002482 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002483 curwin->w_cursor.col = 0;
2484 pos.col = 0;
2485 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2486 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002487 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002488 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002489 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002490 dec(&(oap->end));
2491 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2492 pos.col = 0;
2493 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002494 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002495 pos.col += oap->start.col;
2496 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002497 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002498 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002499 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002500 length = (int)STRLEN(ml_get(oap->end.lnum));
2501 if (oap->end.col >= length)
2502 oap->end.col = length - 1;
2503 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002504 }
2505 }
2506 one_change = do_addsub(oap->op_type, &pos, length, amount);
2507 if (one_change)
2508 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002509 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002510 if (change_cnt == 0)
2511 startpos = curbuf->b_op_start;
2512 ++change_cnt;
2513 }
2514
2515#ifdef FEAT_NETBEANS_INTG
2516 if (netbeans_active() && one_change)
2517 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002518 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002519
2520 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002521 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002522 netbeans_inserted(curbuf, pos.lnum, pos.col,
2523 &ptr[pos.col], length);
2524 }
2525#endif
2526 if (g_cmd && one_change)
2527 amount += Prenum1;
2528 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002529
2530#ifdef FEAT_FOLDING
2531 disable_fold_update--;
2532#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002533 if (change_cnt)
2534 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2535
2536 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002537 // No change: need to remove the Visual selection
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002538 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 // Set '[ mark if something changed. Keep the last end
2541 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002542 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002543 curbuf->b_op_start = startpos;
2544
2545 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002546 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002547 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002548 }
2549}
2550
2551/*
2552 * Add or subtract 'Prenum1' from a number in a line
2553 * op_type is OP_NR_ADD or OP_NR_SUB
2554 *
2555 * Returns TRUE if some character was changed.
2556 */
2557 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002558do_addsub(
2559 int op_type,
2560 pos_T *pos,
2561 int length,
2562 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 int col;
2565 char_u *buf1;
2566 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002567 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2568 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002569 uvarnumber_T n;
2570 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 char_u *ptr;
2572 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002574 int do_hex;
2575 int do_oct;
2576 int do_bin;
2577 int do_alpha;
2578 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002581 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002582 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002583 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002584 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002585 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002586 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002587 pos_T startpos;
2588 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002589 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002591 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2592 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2593 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2594 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2595 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002597 if (virtual_active())
2598 {
2599 save_coladd = pos->coladd;
2600 pos->coladd = 0;
2601 }
2602
Bram Moolenaard79e5502016-01-10 22:13:02 +01002603 curwin->w_cursor = *pos;
2604 ptr = ml_get(pos->lnum);
2605 col = pos->col;
2606
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002607 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002608 goto theend;
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 /*
2611 * First check if we are on a hexadecimal number, after the "0x".
2612 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002613 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002615 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002616 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002617 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002618 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002619 if (has_mbyte)
2620 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002621 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002622
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002623 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002624 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002625 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002626 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002627 if (has_mbyte)
2628 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002629 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002630
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002631 if ( do_bin
2632 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002633 && ! ((col > 0
2634 && (ptr[col] == 'X'
2635 || ptr[col] == 'x')
2636 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002637 && (!has_mbyte ||
2638 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002639 && vim_isxdigit(ptr[col + 1]))))
2640 {
2641
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002642 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002643
Bram Moolenaard79e5502016-01-10 22:13:02 +01002644 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002645
2646 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002647 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002648 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002649 if (has_mbyte)
2650 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002651 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002652 }
2653
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002654 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002655 && col > 0
2656 && (ptr[col] == 'X'
2657 || ptr[col] == 'x')
2658 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002659 && (!has_mbyte ||
2660 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002661 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002662 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002663 && col > 0
2664 && (ptr[col] == 'B'
2665 || ptr[col] == 'b')
2666 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002667 && (!has_mbyte ||
2668 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002669 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002670 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002672 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002673 if (has_mbyte)
2674 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002675 }
2676 else
2677 {
2678 /*
2679 * Search forward and then backward to find the start of number.
2680 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002681 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002682
2683 while (ptr[col] != NUL
2684 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002685 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002686 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002687
2688 while (col > 0
2689 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002690 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002691 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002692 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002693 if (has_mbyte)
2694 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002695 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002696 }
2697 }
2698
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002699 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002700 {
2701 while (ptr[col] != NUL && length > 0
2702 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002703 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002704 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002705 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002706
2707 col += mb_len;
2708 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002709 }
2710
2711 if (length == 0)
2712 goto theend;
2713
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002714 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002715 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2716 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002717 {
2718 negative = TRUE;
2719 was_positive = FALSE;
2720 }
2721 }
2722
2723 /*
2724 * If a number was found, and saving for undo works, replace the number.
2725 */
2726 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002727 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002728 {
2729 beep_flush();
2730 goto theend;
2731 }
2732
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002733 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002736 if (op_type == OP_NR_SUB)
2737 {
2738 if (CharOrd(firstdigit) < Prenum1)
2739 {
2740 if (isupper(firstdigit))
2741 firstdigit = 'A';
2742 else
2743 firstdigit = 'a';
2744 }
2745 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002746 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002747 }
2748 else
2749 {
2750 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2751 {
2752 if (isupper(firstdigit))
2753 firstdigit = 'Z';
2754 else
2755 firstdigit = 'z';
2756 }
2757 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002758 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002759 }
2760 curwin->w_cursor.col = col;
2761 if (!did_change)
2762 startpos = curwin->w_cursor;
2763 did_change = TRUE;
2764 (void)del_char(FALSE);
2765 ins_char(firstdigit);
2766 endpos = curwin->w_cursor;
2767 curwin->w_cursor.col = col;
2768 }
2769 else
2770 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002771 pos_T save_pos;
2772 int i;
2773
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002774 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002775 && (!has_mbyte ||
2776 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002777 && !visual
2778 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002781 --col;
2782 negative = TRUE;
2783 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002784 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002785 if (visual && VIsual_mode != 'V')
2786 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2787 ? (int)STRLEN(ptr) - col
2788 : length);
2789
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002790 int overflow = FALSE;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002791 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002792 0 + (do_bin ? STR2NR_BIN : 0)
2793 + (do_oct ? STR2NR_OCT : 0)
2794 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002795 NULL, &n, maxlen, FALSE, &overflow);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002798 if (pre && negative)
2799 {
2800 ++col;
2801 --length;
2802 negative = FALSE;
2803 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002805 subtract = FALSE;
2806 if (op_type == OP_NR_SUB)
2807 subtract ^= TRUE;
2808 if (negative)
2809 subtract ^= TRUE;
2810
2811 oldn = n;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002812 if (!overflow) // if number is too big don't add/subtract
2813 {
2814 if (subtract)
2815 n -= (uvarnumber_T)Prenum1;
2816 else
2817 n += (uvarnumber_T)Prenum1;
2818 }
2819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002821 if (!pre)
2822 {
2823 if (subtract)
2824 {
2825 if (n > oldn)
2826 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002827 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002828 negative ^= TRUE;
2829 }
2830 }
2831 else
2832 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002834 if (n < oldn)
2835 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002836 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002837 negative ^= TRUE;
2838 }
2839 }
2840 if (n == 0)
2841 negative = FALSE;
2842 }
2843
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002844 if (do_unsigned && negative)
2845 {
2846 if (subtract)
2847 // sticking at zero.
2848 n = (uvarnumber_T)0;
2849 else
2850 // sticking at 2^64 - 1.
2851 n = (uvarnumber_T)(-1);
2852 negative = FALSE;
2853 }
2854
Bram Moolenaard79e5502016-01-10 22:13:02 +01002855 if (visual && !was_positive && !negative && col > 0)
2856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002858 col--;
2859 length++;
2860 }
2861
2862 /*
2863 * Delete the old number.
2864 */
2865 curwin->w_cursor.col = col;
2866 if (!did_change)
2867 startpos = curwin->w_cursor;
2868 did_change = TRUE;
2869 todel = length;
2870 c = gchar_cursor();
2871 /*
2872 * Don't include the '-' in the length, only the length of the
2873 * part after it is kept the same.
2874 */
2875 if (c == '-')
2876 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002877
2878 save_pos = curwin->w_cursor;
2879 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002880 {
2881 if (c < 0x100 && isalpha(c))
2882 {
2883 if (isupper(c))
2884 hexupper = TRUE;
2885 else
2886 hexupper = FALSE;
2887 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002888 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002889 c = gchar_cursor();
2890 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002891 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002892
2893 /*
2894 * Prepare the leading characters in buf1[].
2895 * When there are many leading zeros it could be very long.
2896 * Allocate a bit too much.
2897 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002898 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002899 if (buf1 == NULL)
2900 goto theend;
2901 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002902 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002903 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002904 if (pre)
2905 {
2906 *ptr++ = '0';
2907 --length;
2908 }
2909 if (pre == 'b' || pre == 'B' ||
2910 pre == 'x' || pre == 'X')
2911 {
2912 *ptr++ = pre;
2913 --length;
2914 }
2915
2916 /*
2917 * Put the number characters in buf2[].
2918 */
2919 if (pre == 'b' || pre == 'B')
2920 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002921 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002922 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002925 for (bit = bits; bit > 0; bit--)
2926 if ((n >> (bit - 1)) & 0x1) break;
2927
Christian Brabandt889f6af2023-09-02 19:43:33 +02002928 for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002929 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2930
2931 buf2[i] = '\0';
2932 }
2933 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002934 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002935 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002936 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002937 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002938 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002939 else
=?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 length -= (int)STRLEN(buf2);
2942
2943 /*
2944 * Adjust number of zeros to the new number of digits, so the
2945 * total length of the number remains the same.
2946 * Don't do this when
2947 * the result may look like an octal number.
2948 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002949 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002950 while (length-- > 0)
2951 *ptr++ = '0';
2952 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002953
Bram Moolenaard79e5502016-01-10 22:13:02 +01002954 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002955
2956 // Insert just after the first character to be removed, so that any
2957 // text properties will be adjusted. Then delete the old number
2958 // afterwards.
2959 save_pos = curwin->w_cursor;
2960 if (todel > 0)
2961 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002963 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002964
2965 // del_char() will also mark line needing displaying
2966 if (todel > 0)
2967 {
2968 int bytes_after = (int)STRLEN(ml_get_curline())
2969 - curwin->w_cursor.col;
2970
2971 // Delete the one character before the insert.
2972 curwin->w_cursor = save_pos;
2973 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002974 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2975 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002976 --todel;
2977 }
2978 while (todel-- > 0)
2979 (void)del_char(FALSE);
2980
Bram Moolenaard79e5502016-01-10 22:13:02 +01002981 endpos = curwin->w_cursor;
2982 if (did_change && curwin->w_cursor.col)
2983 --curwin->w_cursor.col;
2984 }
2985
Bram Moolenaare1004402020-10-24 20:49:43 +02002986 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002989 curbuf->b_op_start = startpos;
2990 curbuf->b_op_end = endpos;
2991 if (curbuf->b_op_end.col > 0)
2992 --curbuf->b_op_end.col;
2993 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002994
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002995theend:
2996 if (visual)
2997 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002998 else if (did_change)
2999 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02003000 else if (virtual_active())
3001 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003002
Bram Moolenaard79e5502016-01-10 22:13:02 +01003003 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004}
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003007clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003009 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010}
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003013 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 *
3015 * "Words" are counted by looking for boundaries between non-space and
3016 * space characters. (it seems to produce results that match 'wc'.)
3017 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003018 * Return value is byte count; word count for the line is added to "*wc".
3019 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 *
3021 * The function will only examine the first "limit" characters in the
3022 * line, stopping if it encounters an end-of-line (NUL byte). In that
3023 * case, eol_size will be added to the character count to account for
3024 * the size of the EOL character.
3025 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003026 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003027line_count_info(
3028 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003029 varnumber_T *wc,
3030 varnumber_T *cc,
3031 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003032 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003034 varnumber_T i;
3035 varnumber_T words = 0;
3036 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 int is_word = 0;
3038
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003039 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
3041 if (is_word)
3042 {
3043 if (vim_isspace(line[i]))
3044 {
3045 words++;
3046 is_word = 0;
3047 }
3048 }
3049 else if (!vim_isspace(line[i]))
3050 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003051 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003052 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054
3055 if (is_word)
3056 words++;
3057 *wc += words;
3058
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003059 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003060 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003063 chars += eol_size;
3064 }
3065 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 return i;
3067}
3068
3069/*
3070 * Give some info about the position of the cursor (for "g CTRL-G").
3071 * In Visual mode, give some info about the selected region. (In this case,
3072 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003073 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
3075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003076cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003079 char_u buf1[50];
3080 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003082 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003083 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003084 varnumber_T byte_count_cursor = 0;
3085 varnumber_T char_count = 0;
3086 varnumber_T char_count_cursor = 0;
3087 varnumber_T word_count = 0;
3088 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003089 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003090 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 long line_count_selected = 0;
3092 pos_T min_pos, max_pos;
3093 oparg_T oparg;
3094 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 /*
3097 * Compute the length of the file in characters.
3098 */
3099 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3100 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003101 if (dict == NULL)
3102 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003103 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003104 return;
3105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 }
3107 else
3108 {
3109 if (get_fileformat(curbuf) == EOL_DOS)
3110 eol_size = 2;
3111 else
3112 eol_size = 1;
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (VIsual_active)
3115 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003116 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
3118 min_pos = VIsual;
3119 max_pos = curwin->w_cursor;
3120 }
3121 else
3122 {
3123 min_pos = curwin->w_cursor;
3124 max_pos = VIsual;
3125 }
3126 if (*p_sel == 'e' && max_pos.col > 0)
3127 --max_pos.col;
3128
3129 if (VIsual_mode == Ctrl_V)
3130 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003131#ifdef FEAT_LINEBREAK
3132 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003133 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003135 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003136 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003137 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 oparg.is_VIsual = 1;
3140 oparg.block_mode = TRUE;
3141 oparg.op_type = OP_NOP;
3142 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003143 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003144#ifdef FEAT_LINEBREAK
3145 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003146 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003147#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003148 if (curwin->w_curswant == MAXCOL)
3149 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003150 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (oparg.end_vcol < oparg.start_vcol)
3152 {
3153 oparg.end_vcol += oparg.start_vcol;
3154 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3155 oparg.end_vcol -= oparg.start_vcol;
3156 }
3157 }
3158 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003163 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003164 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 ui_breakcheck();
3167 if (got_int)
3168 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003169 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (VIsual_active
3174 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3175 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003176 char_u *s = NULL;
3177 long len = 0L;
3178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 switch (VIsual_mode)
3180 {
3181 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003185 s = bd.textstart;
3186 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 break;
3188 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003189 s = ml_get(lnum);
3190 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 break;
3192 case 'v':
3193 {
3194 colnr_T start_col = (lnum == min_pos.lnum)
3195 ? min_pos.col : 0;
3196 colnr_T end_col = (lnum == max_pos.lnum)
3197 ? max_pos.col - start_col + 1 : MAXCOL;
3198
Bram Moolenaardef9e822004-12-31 20:58:58 +00003199 s = ml_get(lnum) + start_col;
3200 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 break;
3203 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003204 if (s != NULL)
3205 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003206 byte_count_cursor += line_count_info(s, &word_count_cursor,
3207 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003208 if (lnum == curbuf->b_ml.ml_line_count
3209 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003210 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003211 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003212 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
3215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003217 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (lnum == curwin->w_cursor.lnum)
3219 {
3220 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003221 char_count_cursor += char_count;
3222 byte_count_cursor = byte_count +
3223 line_count_info(ml_get(lnum),
3224 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003225 (varnumber_T)(curwin->w_cursor.col + 1),
3226 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003230 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003231 &char_count, (varnumber_T)MAXCOL,
3232 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003235 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003236 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003237 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaared767a22016-01-03 22:49:16 +01003239 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003241 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003243 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3244 {
3245 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3246 &max_pos.col);
3247 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3248 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3249 }
3250 else
3251 buf1[0] = NUL;
3252
3253 if (char_count_cursor == byte_count_cursor
3254 && char_count == byte_count)
3255 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003256 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003257 buf1, line_count_selected,
3258 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003259 word_count_cursor,
3260 word_count,
3261 byte_count_cursor,
3262 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003263 else
3264 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003265 _("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 +01003266 buf1, line_count_selected,
3267 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003268 word_count_cursor,
3269 word_count,
3270 char_count_cursor,
3271 char_count,
3272 byte_count_cursor,
3273 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003276 {
3277 p = ml_get_curline();
3278 validate_virtcol();
3279 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3280 (int)curwin->w_virtcol + 1);
3281 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
Bram Moolenaarc9121f72022-10-14 20:09:04 +01003282 linetabsize_str(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaared767a22016-01-03 22:49:16 +01003284 if (char_count_cursor == byte_count_cursor
3285 && char_count == byte_count)
3286 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003287 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003288 (char *)buf1, (char *)buf2,
3289 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003291 word_count_cursor, word_count,
3292 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003293 else
3294 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003295 _("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 +01003296 (char *)buf1, (char *)buf2,
3297 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003298 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003299 word_count_cursor, word_count,
3300 char_count_cursor, char_count,
3301 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003302 }
3303 }
3304
Bram Moolenaared767a22016-01-03 22:49:16 +01003305 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003306 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003307 {
3308 size_t len = STRLEN(IObuff);
3309
3310 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003311 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003312 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003313 if (dict == NULL)
3314 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003315 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003316 p = p_shm;
3317 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003318 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003319 p_shm = p;
3320 }
3321 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003322#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003323 if (dict != NULL)
3324 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003325 dict_add_number(dict, "words", word_count);
3326 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003327 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003328 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3329 byte_count_cursor);
3330 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3331 char_count_cursor);
3332 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3333 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003337
3338/*
3339 * Handle indent and format operators and visual mode ":".
3340 */
3341 static void
3342op_colon(oparg_T *oap)
3343{
3344 stuffcharReadbuff(':');
3345 if (oap->is_VIsual)
3346 stuffReadbuff((char_u *)"'<,'>");
3347 else
3348 {
3349 // Make the range look nice, so it can be repeated.
3350 if (oap->start.lnum == curwin->w_cursor.lnum)
3351 stuffcharReadbuff('.');
3352 else
3353 stuffnumReadbuff((long)oap->start.lnum);
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003354
3355#ifdef FEAT_FOLDING
3356 // When using !! on a closed fold the range ".!" works best to operate
3357 // on, it will be made the whole closed fold later.
3358 linenr_T endOfStartFold = oap->start.lnum;
3359 (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold);
3360#endif
3361 if (oap->end.lnum != oap->start.lnum
3362#ifdef FEAT_FOLDING
3363 && oap->end.lnum != endOfStartFold
3364#endif
3365 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003366 {
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003367 // Make it a range with the end line.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003368 stuffcharReadbuff(',');
3369 if (oap->end.lnum == curwin->w_cursor.lnum)
3370 stuffcharReadbuff('.');
3371 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3372 stuffcharReadbuff('$');
Bram Moolenaar9954dc32022-11-11 22:58:36 +00003373 else if (oap->start.lnum == curwin->w_cursor.lnum
3374#ifdef FEAT_FOLDING
3375 // do not use ".+number" for a closed fold, it would count
3376 // folded lines twice
3377 && !hasFolding(oap->end.lnum, NULL, NULL)
3378#endif
3379 )
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003380 {
3381 stuffReadbuff((char_u *)".+");
3382 stuffnumReadbuff((long)oap->line_count - 1);
3383 }
3384 else
3385 stuffnumReadbuff((long)oap->end.lnum);
3386 }
3387 }
3388 if (oap->op_type != OP_COLON)
3389 stuffReadbuff((char_u *)"!");
3390 if (oap->op_type == OP_INDENT)
3391 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003392 if (*get_equalprg() == NUL)
3393 stuffReadbuff((char_u *)"indent");
3394 else
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003395 stuffReadbuff(get_equalprg());
3396 stuffReadbuff((char_u *)"\n");
3397 }
3398 else if (oap->op_type == OP_FORMAT)
3399 {
3400 if (*curbuf->b_p_fp != NUL)
3401 stuffReadbuff(curbuf->b_p_fp);
3402 else if (*p_fp != NUL)
3403 stuffReadbuff(p_fp);
3404 else
3405 stuffReadbuff((char_u *)"fmt");
3406 stuffReadbuff((char_u *)"\n']");
3407 }
3408
3409 // do_cmdline() does the rest
3410}
3411
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003412// callback function for 'operatorfunc'
3413static callback_T opfunc_cb;
3414
3415/*
3416 * Process the 'operatorfunc' option value.
3417 * Returns OK or FAIL.
3418 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003419 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003420did_set_operatorfunc(optset_T *args UNUSED)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003421{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003422 if (option_set_callback_func(p_opfunc, &opfunc_cb) == FAIL)
3423 return e_invalid_argument;
3424
3425 return NULL;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003426}
3427
3428#if defined(EXITFREE) || defined(PROTO)
3429 void
3430free_operatorfunc_option(void)
3431{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003432# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003433 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003434# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003435}
3436#endif
3437
Dominique Pelle748b3082022-01-08 12:41:16 +00003438#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003439/*
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00003440 * Mark the global 'operatorfunc' callback with "copyID" so that it is not
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003441 * garbage collected.
3442 */
3443 int
3444set_ref_in_opfunc(int copyID UNUSED)
3445{
3446 int abort = FALSE;
3447
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003448 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003449
3450 return abort;
3451}
Dominique Pelle748b3082022-01-08 12:41:16 +00003452#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003453
3454/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003455 * Handle the "g@" operator: call 'operatorfunc'.
3456 */
3457 static void
3458op_function(oparg_T *oap UNUSED)
3459{
3460#ifdef FEAT_EVAL
3461 typval_T argv[2];
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003462 pos_T orig_start = curbuf->b_op_start;
3463 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003464 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003465
3466 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003467 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003468 else
3469 {
3470 // Set '[ and '] marks to text to be operated on.
3471 curbuf->b_op_start = oap->start;
3472 curbuf->b_op_end = oap->end;
3473 if (oap->motion_type != MLINE && !oap->inclusive)
3474 // Exclude the end position.
3475 decl(&curbuf->b_op_end);
3476
3477 argv[0].v_type = VAR_STRING;
3478 if (oap->block_mode)
3479 argv[0].vval.v_string = (char_u *)"block";
3480 else if (oap->motion_type == MLINE)
3481 argv[0].vval.v_string = (char_u *)"line";
3482 else
3483 argv[0].vval.v_string = (char_u *)"char";
3484 argv[1].v_type = VAR_UNKNOWN;
3485
3486 // Reset virtual_op so that 'virtualedit' can be changed in the
3487 // function.
zeertzjqcdeb6572022-11-15 13:46:12 +00003488 int save_virtual_op = virtual_op;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003489 virtual_op = MAYBE;
3490
naohiro ono75c30e92021-10-19 11:15:41 +01003491 // Reset finish_op so that mode() returns the right value.
zeertzjqcdeb6572022-11-15 13:46:12 +00003492 int save_finish_op = finish_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003493 finish_op = FALSE;
3494
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003495 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3496 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003497
3498 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003499 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003500 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003501 {
3502 curbuf->b_op_start = orig_start;
3503 curbuf->b_op_end = orig_end;
3504 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003505 }
3506#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003507 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003508#endif
3509}
3510
3511/*
3512 * Calculate start/end virtual columns for operating in block mode.
3513 */
3514 static void
3515get_op_vcol(
3516 oparg_T *oap,
3517 colnr_T redo_VIsual_vcol,
3518 int initial) // when TRUE adjust position for 'selectmode'
3519{
3520 colnr_T start, end;
3521
3522 if (VIsual_mode != Ctrl_V
3523 || (!initial && oap->end.col < curwin->w_width))
3524 return;
3525
3526 oap->block_mode = TRUE;
3527
3528 // prevent from moving onto a trail byte
3529 if (has_mbyte)
3530 mb_adjustpos(curwin->w_buffer, &oap->end);
3531
3532 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3533
3534 if (!redo_VIsual_busy)
3535 {
3536 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3537
3538 if (start < oap->start_vcol)
3539 oap->start_vcol = start;
3540 if (end > oap->end_vcol)
3541 {
3542 if (initial && *p_sel == 'e' && start >= 1
3543 && start - 1 >= oap->end_vcol)
3544 oap->end_vcol = start - 1;
3545 else
3546 oap->end_vcol = end;
3547 }
3548 }
3549
3550 // if '$' was used, get oap->end_vcol from longest line
3551 if (curwin->w_curswant == MAXCOL)
3552 {
3553 curwin->w_cursor.col = MAXCOL;
3554 oap->end_vcol = 0;
3555 for (curwin->w_cursor.lnum = oap->start.lnum;
3556 curwin->w_cursor.lnum <= oap->end.lnum;
3557 ++curwin->w_cursor.lnum)
3558 {
3559 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3560 if (end > oap->end_vcol)
3561 oap->end_vcol = end;
3562 }
3563 }
3564 else if (redo_VIsual_busy)
3565 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3566 // Correct oap->end.col and oap->start.col to be the
3567 // upper-left and lower-right corner of the block area.
3568 //
3569 // (Actually, this does convert column positions into character
3570 // positions)
3571 curwin->w_cursor.lnum = oap->end.lnum;
3572 coladvance(oap->end_vcol);
3573 oap->end = curwin->w_cursor;
3574
3575 curwin->w_cursor = oap->start;
3576 coladvance(oap->start_vcol);
3577 oap->start = curwin->w_cursor;
3578}
3579
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003580// Information for redoing the previous Visual selection.
3581typedef struct {
3582 int rv_mode; // 'v', 'V', or Ctrl-V
3583 linenr_T rv_line_count; // number of lines
3584 colnr_T rv_vcol; // number of cols or end column
3585 long rv_count; // count for Visual operator
3586 int rv_arg; // extra argument
3587} redo_VIsual_T;
3588
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003589 static int
3590is_ex_cmdchar(cmdarg_T *cap)
3591{
3592 return cap->cmdchar == ':'
3593 || cap->cmdchar == K_COMMAND
3594 || cap->cmdchar == K_SCRIPT_COMMAND;
3595}
3596
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003597/*
3598 * Handle an operator after Visual mode or when the movement is finished.
3599 * "gui_yank" is true when yanking text for the clipboard.
3600 */
3601 void
3602do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3603{
3604 oparg_T *oap = cap->oap;
3605 pos_T old_cursor;
3606 int empty_region_error;
3607 int restart_edit_save;
3608#ifdef FEAT_LINEBREAK
3609 int lbr_saved = curwin->w_p_lbr;
3610#endif
3611
3612 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003613 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3614
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003615 int include_line_break = FALSE;
3616
3617#if defined(FEAT_CLIPBOARD)
3618 // Yank the visual area into the GUI selection register before we operate
3619 // on it and lose it forever.
3620 // Don't do it if a specific register was specified, so that ""x"*P works.
3621 // This could call do_pending_operator() recursively, but that's OK
3622 // because gui_yank will be TRUE for the nested call.
3623 if ((clip_star.available || clip_plus.available)
3624 && oap->op_type != OP_NOP
3625 && !gui_yank
3626 && VIsual_active
3627 && !redo_VIsual_busy
3628 && oap->regname == 0)
3629 clip_auto_select();
3630#endif
3631 old_cursor = curwin->w_cursor;
3632
3633 // If an operation is pending, handle it...
3634 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3635 {
3636 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3637 // for the clipboard.
3638 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3639
3640#ifdef FEAT_LINEBREAK
3641 // Avoid a problem with unwanted linebreaks in block mode.
Bram Moolenaar16dab412022-10-08 16:41:32 +01003642 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003643#endif
3644 oap->is_VIsual = VIsual_active;
3645 if (oap->motion_force == 'V')
3646 oap->motion_type = MLINE;
3647 else if (oap->motion_force == 'v')
3648 {
3649 // If the motion was linewise, "inclusive" will not have been set.
3650 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3651 if (oap->motion_type == MLINE)
3652 oap->inclusive = FALSE;
3653 // If the motion already was characterwise, toggle "inclusive"
3654 else if (oap->motion_type == MCHAR)
3655 oap->inclusive = !oap->inclusive;
3656 oap->motion_type = MCHAR;
3657 }
3658 else if (oap->motion_force == Ctrl_V)
3659 {
3660 // Change line- or characterwise motion into Visual block mode.
3661 if (!VIsual_active)
3662 {
3663 VIsual_active = TRUE;
3664 VIsual = oap->start;
3665 }
3666 VIsual_mode = Ctrl_V;
3667 VIsual_select = FALSE;
3668 VIsual_reselect = FALSE;
3669 }
3670
3671 // Only redo yank when 'y' flag is in 'cpoptions'.
3672 // Never redo "zf" (define fold).
3673 if ((redo_yank || oap->op_type != OP_YANK)
3674 && ((!VIsual_active || oap->motion_force)
3675 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003676 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003677 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003678 && cap->cmdchar != 'D'
3679#ifdef FEAT_FOLDING
3680 && oap->op_type != OP_FOLD
3681 && oap->op_type != OP_FOLDOPEN
3682 && oap->op_type != OP_FOLDOPENREC
3683 && oap->op_type != OP_FOLDCLOSE
3684 && oap->op_type != OP_FOLDCLOSEREC
3685 && oap->op_type != OP_FOLDDEL
3686 && oap->op_type != OP_FOLDDELREC
3687#endif
3688 )
3689 {
3690 prep_redo(oap->regname, cap->count0,
3691 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3692 oap->motion_force, cap->cmdchar, cap->nchar);
3693 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3694 {
3695 // If 'cpoptions' does not contain 'r', insert the search
3696 // pattern to really repeat the same command.
3697 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3698 AppendToRedobuffLit(cap->searchbuf, -1);
3699 AppendToRedobuff(NL_STR);
3700 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003701 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003702 {
3703 // do_cmdline() has stored the first typed line in
3704 // "repeat_cmdline". When several lines are typed repeating
3705 // won't be possible.
3706 if (repeat_cmdline == NULL)
3707 ResetRedobuff();
3708 else
3709 {
zeertzjq30b6d612023-05-07 17:39:23 +01003710 if (cap->cmdchar == ':')
3711 AppendToRedobuffLit(repeat_cmdline, -1);
3712 else
3713 AppendToRedobuffSpec(repeat_cmdline);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003714 AppendToRedobuff(NL_STR);
3715 VIM_CLEAR(repeat_cmdline);
3716 }
3717 }
3718 }
3719
3720 if (redo_VIsual_busy)
3721 {
3722 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003723 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003724 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003725 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003726 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3727 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003728 VIsual_mode = redo_VIsual.rv_mode;
3729 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003730 {
3731 if (VIsual_mode == 'v')
3732 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003733 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003734 {
3735 validate_virtcol();
3736 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003737 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003738 }
3739 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003740 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003741 }
3742 else
3743 {
3744 curwin->w_curswant = MAXCOL;
3745 }
3746 coladvance(curwin->w_curswant);
3747 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003748 cap->count0 = redo_VIsual.rv_count;
3749 if (redo_VIsual.rv_count != 0)
3750 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003751 else
3752 cap->count1 = 1;
3753 }
3754 else if (VIsual_active)
3755 {
3756 if (!gui_yank)
3757 {
3758 // Save the current VIsual area for '< and '> marks, and "gv"
3759 curbuf->b_visual.vi_start = VIsual;
3760 curbuf->b_visual.vi_end = curwin->w_cursor;
3761 curbuf->b_visual.vi_mode = VIsual_mode;
3762 restore_visual_mode();
3763 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003764#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003765 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003766#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003767 }
3768
3769 // In Select mode, a linewise selection is operated upon like a
3770 // characterwise selection.
3771 // Special case: gH<Del> deletes the last line.
3772 if (VIsual_select && VIsual_mode == 'V'
3773 && cap->oap->op_type != OP_DELETE)
3774 {
3775 if (LT_POS(VIsual, curwin->w_cursor))
3776 {
3777 VIsual.col = 0;
3778 curwin->w_cursor.col =
3779 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3780 }
3781 else
3782 {
3783 curwin->w_cursor.col = 0;
3784 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3785 }
3786 VIsual_mode = 'v';
3787 }
3788 // If 'selection' is "exclusive", backup one character for
3789 // charwise selections.
3790 else if (VIsual_mode == 'v')
3791 include_line_break = unadjust_for_sel();
3792
3793 oap->start = VIsual;
3794 if (VIsual_mode == 'V')
3795 {
3796 oap->start.col = 0;
3797 oap->start.coladd = 0;
3798 }
3799 }
3800
3801 // Set oap->start to the first position of the operated text, oap->end
3802 // to the end of the operated text. w_cursor is equal to oap->start.
3803 if (LT_POS(oap->start, curwin->w_cursor))
3804 {
3805#ifdef FEAT_FOLDING
3806 // Include folded lines completely.
3807 if (!VIsual_active)
3808 {
3809 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3810 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003811 if ((curwin->w_cursor.col > 0 || oap->inclusive
3812 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003813 && hasFolding(curwin->w_cursor.lnum, NULL,
3814 &curwin->w_cursor.lnum))
3815 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3816 }
3817#endif
3818 oap->end = curwin->w_cursor;
3819 curwin->w_cursor = oap->start;
3820
3821 // w_virtcol may have been updated; if the cursor goes back to its
3822 // previous position w_virtcol becomes invalid and isn't updated
3823 // automatically.
3824 curwin->w_valid &= ~VALID_VIRTCOL;
3825 }
3826 else
3827 {
3828#ifdef FEAT_FOLDING
3829 // Include folded lines completely.
3830 if (!VIsual_active && oap->motion_type == MLINE)
3831 {
3832 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3833 NULL))
3834 curwin->w_cursor.col = 0;
3835 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3836 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3837 }
3838#endif
3839 oap->end = oap->start;
3840 oap->start = curwin->w_cursor;
3841 }
3842
3843 // Just in case lines were deleted that make the position invalid.
3844 check_pos(curwin->w_buffer, &oap->end);
3845 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3846
3847 // Set "virtual_op" before resetting VIsual_active.
3848 virtual_op = virtual_active();
3849
3850 if (VIsual_active || redo_VIsual_busy)
3851 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003852 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003853
3854 if (!redo_VIsual_busy && !gui_yank)
3855 {
3856 // Prepare to reselect and redo Visual: this is based on the
3857 // size of the Visual text
3858 resel_VIsual_mode = VIsual_mode;
3859 if (curwin->w_curswant == MAXCOL)
3860 resel_VIsual_vcol = MAXCOL;
3861 else
3862 {
3863 if (VIsual_mode != Ctrl_V)
3864 getvvcol(curwin, &(oap->end),
3865 NULL, NULL, &oap->end_vcol);
3866 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3867 {
3868 if (VIsual_mode != Ctrl_V)
3869 getvvcol(curwin, &(oap->start),
3870 &oap->start_vcol, NULL, NULL);
3871 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3872 }
3873 else
3874 resel_VIsual_vcol = oap->end_vcol;
3875 }
3876 resel_VIsual_line_count = oap->line_count;
3877 }
3878
3879 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3880 if ((redo_yank || oap->op_type != OP_YANK)
3881 && oap->op_type != OP_COLON
3882#ifdef FEAT_FOLDING
3883 && oap->op_type != OP_FOLD
3884 && oap->op_type != OP_FOLDOPEN
3885 && oap->op_type != OP_FOLDOPENREC
3886 && oap->op_type != OP_FOLDCLOSE
3887 && oap->op_type != OP_FOLDCLOSEREC
3888 && oap->op_type != OP_FOLDDEL
3889 && oap->op_type != OP_FOLDDELREC
3890#endif
3891 && oap->motion_force == NUL
3892 )
3893 {
3894 // Prepare for redoing. Only use the nchar field for "r",
3895 // otherwise it might be the second char of the operator.
3896 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3897 || cap->nchar == 'N'))
3898 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003899 get_op_char(oap->op_type),
3900 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003901 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003902 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003903 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003904 int opchar = get_op_char(oap->op_type);
3905 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003906 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3907
3908 // reverse what nv_replace() did
3909 if (nchar == REPLACE_CR_NCHAR)
3910 nchar = CAR;
3911 else if (nchar == REPLACE_NL_NCHAR)
3912 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003913
3914 if (opchar == 'g' && extra_opchar == '@')
3915 // also repeat the count for 'operatorfunc'
3916 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3917 cap->count0, opchar, extra_opchar, nchar);
3918 else
3919 prep_redo(oap->regname, 0L, NUL, 'v',
3920 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003921 }
3922 if (!redo_VIsual_busy)
3923 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003924 redo_VIsual.rv_mode = resel_VIsual_mode;
3925 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3926 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3927 redo_VIsual.rv_count = cap->count0;
3928 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003929 }
3930 }
3931
3932 // oap->inclusive defaults to TRUE.
3933 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3934 // FALSE. This makes "d}P" and "v}dP" work the same.
3935 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3936 oap->inclusive = TRUE;
3937 if (VIsual_mode == 'V')
3938 oap->motion_type = MLINE;
3939 else
3940 {
3941 oap->motion_type = MCHAR;
3942 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3943 && (include_line_break || !virtual_op))
3944 {
3945 oap->inclusive = FALSE;
3946 // Try to include the newline, unless it's an operator
3947 // that works on lines only.
3948 if (*p_sel != 'o'
3949 && !op_on_lines(oap->op_type)
3950 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3951 {
3952 ++oap->end.lnum;
3953 oap->end.col = 0;
3954 oap->end.coladd = 0;
3955 ++oap->line_count;
3956 }
3957 }
3958 }
3959
3960 redo_VIsual_busy = FALSE;
3961
3962 // Switch Visual off now, so screen updating does
3963 // not show inverted text when the screen is redrawn.
3964 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3965 // no screen redraw, so it is done here to remove the inverted
3966 // part.
3967 if (!gui_yank)
3968 {
3969 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003970 setmouse();
3971 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003972 may_clear_cmdline();
3973 if ((oap->op_type == OP_YANK
3974 || oap->op_type == OP_COLON
3975 || oap->op_type == OP_FUNCTION
3976 || oap->op_type == OP_FILTER)
3977 && oap->motion_force == NUL)
3978 {
3979#ifdef FEAT_LINEBREAK
3980 // make sure redrawing is correct
Bram Moolenaar16dab412022-10-08 16:41:32 +01003981 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003982#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003983 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003984 }
3985 }
3986 }
3987
3988 // Include the trailing byte of a multi-byte char.
3989 if (has_mbyte && oap->inclusive)
3990 {
3991 int l;
3992
3993 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3994 if (l > 1)
3995 oap->end.col += l - 1;
3996 }
3997 curwin->w_set_curswant = TRUE;
3998
3999 // oap->empty is set when start and end are the same. The inclusive
4000 // flag affects this too, unless yanking and the end is on a NUL.
4001 oap->empty = (oap->motion_type == MCHAR
4002 && (!oap->inclusive
4003 || (oap->op_type == OP_YANK
4004 && gchar_pos(&oap->end) == NUL))
4005 && EQUAL_POS(oap->start, oap->end)
4006 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4007 // For delete, change and yank, it's an error to operate on an
4008 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4009 empty_region_error = (oap->empty
4010 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4011
4012 // Force a redraw when operating on an empty Visual region, when
4013 // 'modifiable is off or creating a fold.
4014 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4015#ifdef FEAT_FOLDING
4016 || oap->op_type == OP_FOLD
4017#endif
4018 ))
4019 {
4020#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004021 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004022#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004023 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004024 }
4025
4026 // If the end of an operator is in column one while oap->motion_type
4027 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4028 // character in the previous line. If op_start is on or before the
4029 // first non-blank in the line, the operator becomes linewise
4030 // (strange, but that's the way vi does it).
4031 if ( oap->motion_type == MCHAR
4032 && oap->inclusive == FALSE
4033 && !(cap->retval & CA_NO_ADJ_OP_END)
4034 && oap->end.col == 0
4035 && (!oap->is_VIsual || *p_sel == 'o')
4036 && !oap->block_mode
4037 && oap->line_count > 1)
4038 {
4039 oap->end_adjusted = TRUE; // remember that we did this
4040 --oap->line_count;
4041 --oap->end.lnum;
4042 if (inindent(0))
4043 oap->motion_type = MLINE;
4044 else
4045 {
4046 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
4047 if (oap->end.col)
4048 {
4049 --oap->end.col;
4050 oap->inclusive = TRUE;
4051 }
4052 }
4053 }
4054 else
4055 oap->end_adjusted = FALSE;
4056
4057 switch (oap->op_type)
4058 {
4059 case OP_LSHIFT:
4060 case OP_RSHIFT:
4061 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4062 auto_format(FALSE, TRUE);
4063 break;
4064
4065 case OP_JOIN_NS:
4066 case OP_JOIN:
4067 if (oap->line_count < 2)
4068 oap->line_count = 2;
4069 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4070 curbuf->b_ml.ml_line_count)
4071 beep_flush();
4072 else
4073 {
4074 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4075 TRUE, TRUE, TRUE);
4076 auto_format(FALSE, TRUE);
4077 }
4078 break;
4079
4080 case OP_DELETE:
4081 VIsual_reselect = FALSE; // don't reselect now
4082 if (empty_region_error)
4083 {
4084 vim_beep(BO_OPER);
4085 CancelRedo();
4086 }
4087 else
4088 {
4089 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004090 // save cursor line for undo if it wasn't saved yet
4091 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4092 && u_save_cursor() == OK)
4093 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004094 }
4095 break;
4096
4097 case OP_YANK:
4098 if (empty_region_error)
4099 {
4100 if (!gui_yank)
4101 {
4102 vim_beep(BO_OPER);
4103 CancelRedo();
4104 }
4105 }
4106 else
4107 {
4108#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004109 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004110#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004111 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004112 (void)op_yank(oap, FALSE, !gui_yank);
4113 }
4114 check_cursor_col();
4115 break;
4116
4117 case OP_CHANGE:
4118 VIsual_reselect = FALSE; // don't reselect now
4119 if (empty_region_error)
4120 {
4121 vim_beep(BO_OPER);
4122 CancelRedo();
4123 }
4124 else
4125 {
4126 // This is a new edit command, not a restart. Need to
4127 // remember it to make 'insertmode' work with mappings for
4128 // Visual mode. But do this only once and not when typed and
4129 // 'insertmode' isn't set.
4130 if (p_im || !KeyTyped)
4131 restart_edit_save = restart_edit;
4132 else
4133 restart_edit_save = 0;
4134 restart_edit = 0;
4135#ifdef FEAT_LINEBREAK
4136 // Restore linebreak, so that when the user edits it looks as
4137 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004138 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004139#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004140 // trigger TextChangedI
4141 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4142
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004143 if (op_change(oap)) // will call edit()
4144 cap->retval |= CA_COMMAND_BUSY;
4145 if (restart_edit == 0)
4146 restart_edit = restart_edit_save;
4147 }
4148 break;
4149
4150 case OP_FILTER:
4151 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4152 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4153 else
4154 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4155 // FALLTHROUGH
4156
4157 case OP_INDENT:
4158 case OP_COLON:
4159
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004160 // If 'equalprg' is empty, do the indenting internally.
4161 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4162 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004163 if (curbuf->b_p_lisp)
4164 {
Bram Moolenaarc8b67352022-10-15 16:41:53 +01004165#ifdef FEAT_EVAL
4166 if (use_indentexpr_for_lisp())
4167 op_reindent(oap, get_expr_indent);
4168 else
4169#endif
4170 op_reindent(oap, get_lisp_indent);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004171 break;
4172 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004173 op_reindent(oap,
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004174#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004175 *curbuf->b_p_inde != NUL ? get_expr_indent :
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004176#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004177 get_c_indent);
4178 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004179 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004180
4181 op_colon(oap);
4182 break;
4183
4184 case OP_TILDE:
4185 case OP_UPPER:
4186 case OP_LOWER:
4187 case OP_ROT13:
4188 if (empty_region_error)
4189 {
4190 vim_beep(BO_OPER);
4191 CancelRedo();
4192 }
4193 else
4194 op_tilde(oap);
4195 check_cursor_col();
4196 break;
4197
4198 case OP_FORMAT:
4199#if defined(FEAT_EVAL)
4200 if (*curbuf->b_p_fex != NUL)
4201 op_formatexpr(oap); // use expression
4202 else
4203#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004204 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004205 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004206 op_colon(oap); // use external command
4207 else
4208 op_format(oap, FALSE); // use internal function
4209 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004210 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004211 case OP_FORMAT2:
4212 op_format(oap, TRUE); // use internal function
4213 break;
4214
4215 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004216 {
4217 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4218
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004219#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004220 // Restore linebreak, so that when the user edits it looks as
4221 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004222 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004223#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004224 // call 'operatorfunc'
4225 op_function(oap);
4226
4227 // Restore the info for redoing Visual mode, the function may
4228 // invoke another operator and unintentionally change it.
4229 redo_VIsual = save_redo_VIsual;
4230 break;
4231 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004232
4233 case OP_INSERT:
4234 case OP_APPEND:
4235 VIsual_reselect = FALSE; // don't reselect now
4236 if (empty_region_error)
4237 {
4238 vim_beep(BO_OPER);
4239 CancelRedo();
4240 }
4241 else
4242 {
4243 // This is a new edit command, not a restart. Need to
4244 // remember it to make 'insertmode' work with mappings for
4245 // Visual mode. But do this only once.
4246 restart_edit_save = restart_edit;
4247 restart_edit = 0;
4248#ifdef FEAT_LINEBREAK
4249 // Restore linebreak, so that when the user edits it looks as
4250 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004251 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004252#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02004253 // trigger TextChangedI
4254 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
4255
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004256 op_insert(oap, cap->count1);
4257#ifdef FEAT_LINEBREAK
4258 // Reset linebreak, so that formatting works correctly.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004259 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004260#endif
4261
4262 // TODO: when inserting in several lines, should format all
4263 // the lines.
4264 auto_format(FALSE, TRUE);
4265
4266 if (restart_edit == 0)
4267 restart_edit = restart_edit_save;
4268 else
4269 cap->retval |= CA_COMMAND_BUSY;
4270 }
4271 break;
4272
4273 case OP_REPLACE:
4274 VIsual_reselect = FALSE; // don't reselect now
4275 if (empty_region_error)
4276 {
4277 vim_beep(BO_OPER);
4278 CancelRedo();
4279 }
4280 else
4281 {
4282#ifdef FEAT_LINEBREAK
4283 // Restore linebreak, so that when the user edits it looks as
4284 // before.
Bram Moolenaar16dab412022-10-08 16:41:32 +01004285 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004286#endif
4287 op_replace(oap, cap->nchar);
4288 }
4289 break;
4290
4291#ifdef FEAT_FOLDING
4292 case OP_FOLD:
4293 VIsual_reselect = FALSE; // don't reselect now
4294 foldCreate(oap->start.lnum, oap->end.lnum);
4295 break;
4296
4297 case OP_FOLDOPEN:
4298 case OP_FOLDOPENREC:
4299 case OP_FOLDCLOSE:
4300 case OP_FOLDCLOSEREC:
4301 VIsual_reselect = FALSE; // don't reselect now
4302 opFoldRange(oap->start.lnum, oap->end.lnum,
4303 oap->op_type == OP_FOLDOPEN
4304 || oap->op_type == OP_FOLDOPENREC,
4305 oap->op_type == OP_FOLDOPENREC
4306 || oap->op_type == OP_FOLDCLOSEREC,
4307 oap->is_VIsual);
4308 break;
4309
4310 case OP_FOLDDEL:
4311 case OP_FOLDDELREC:
4312 VIsual_reselect = FALSE; // don't reselect now
4313 deleteFold(oap->start.lnum, oap->end.lnum,
4314 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4315 break;
4316#endif
4317 case OP_NR_ADD:
4318 case OP_NR_SUB:
4319 if (empty_region_error)
4320 {
4321 vim_beep(BO_OPER);
4322 CancelRedo();
4323 }
4324 else
4325 {
4326 VIsual_active = TRUE;
4327#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004328 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004329#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004330 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004331 VIsual_active = FALSE;
4332 }
4333 check_cursor_col();
4334 break;
4335 default:
4336 clearopbeep(oap);
4337 }
4338 virtual_op = MAYBE;
4339 if (!gui_yank)
4340 {
4341 // if 'sol' not set, go back to old column for some commands
4342 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4343 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4344 || oap->op_type == OP_DELETE))
4345 {
4346#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004347 (void)reset_lbr();
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004348#endif
4349 coladvance(curwin->w_curswant = old_col);
4350 }
4351 }
4352 else
4353 {
4354 curwin->w_cursor = old_cursor;
4355 }
4356 oap->block_mode = FALSE;
4357 clearop(oap);
4358 motion_force = NUL;
4359 }
4360#ifdef FEAT_LINEBREAK
Bram Moolenaar16dab412022-10-08 16:41:32 +01004361 restore_lbr(lbr_saved);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004362#endif
4363}
Christian Brabandtffb13672023-09-15 20:22:02 +02004364
4365// put byte 'c' at position 'lp', but
4366// verify, that the position to place
4367// is actually safe
4368 static void
4369pbyte(pos_T lp, int c)
4370{
4371 char_u *p = ml_get_buf(curbuf, lp.lnum, TRUE);
4372 int len = curbuf->b_ml.ml_line_len;
4373
4374 // safety check
4375 if (lp.col >= len)
4376 lp.col = (len > 1 ? len - 2 : 0);
4377 *(p + lp.col) = c;
4378}