blob: 4adeb09453bdb53c7f4519d164b553c6890a6e66 [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);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020
Bram Moolenaarf2732452018-06-03 14:47:35 +020021// Flags for third item in "opchars".
22#define OPF_LINES 1 // operator always works on lines
23#define OPF_CHANGE 2 // operator changes text
24
Bram Moolenaar071d4272004-06-13 20:20:40 +000025/*
26 * The names of operators.
27 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020028 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 */
30static char opchars[][3] =
31{
Bram Moolenaarf2732452018-06-03 14:47:35 +020032 {NUL, NUL, 0}, // OP_NOP
33 {'d', NUL, OPF_CHANGE}, // OP_DELETE
34 {'y', NUL, 0}, // OP_YANK
35 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
36 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
37 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
38 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
39 {'g', '~', OPF_CHANGE}, // OP_TILDE
40 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
41 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
42 {':', NUL, OPF_LINES}, // OP_COLON
43 {'g', 'U', OPF_CHANGE}, // OP_UPPER
44 {'g', 'u', OPF_CHANGE}, // OP_LOWER
45 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
46 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
47 {'g', '?', OPF_CHANGE}, // OP_ROT13
48 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
49 {'I', NUL, OPF_CHANGE}, // OP_INSERT
50 {'A', NUL, OPF_CHANGE}, // OP_APPEND
51 {'z', 'f', OPF_LINES}, // OP_FOLD
52 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
53 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
54 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
55 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
56 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
57 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
58 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
59 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
60 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
61 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000062};
63
64/*
65 * Translate a command name into an operator type.
66 * Must only be called with a valid operator name!
67 */
68 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010069get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070{
71 int i;
72
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010073 if (char1 == 'r') // ignore second character
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 return OP_REPLACE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010075 if (char1 == '~') // when tilde is an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 return OP_TILDE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010077 if (char1 == 'g' && char2 == Ctrl_A) // add
Bram Moolenaard79e5502016-01-10 22:13:02 +010078 return OP_NR_ADD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010079 if (char1 == 'g' && char2 == Ctrl_X) // subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +010080 return OP_NR_SUB;
Christian Brabandt544a38e2021-06-10 19:39:11 +020081 if (char1 == 'z' && char2 == 'y') // OP_YANK
82 return OP_YANK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 if (opchars[i][0] == char1 && opchars[i][1] == char2)
86 break;
K.Takataeeec2542021-06-02 13:28:16 +020087 if (i == (int)ARRAY_LENGTH(opchars) - 1)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010088 {
89 internal_error("get_op_type()");
90 break;
91 }
92 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return i;
94}
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * Return TRUE if operator "op" always works on whole lines.
98 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020099 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100100op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200102 return opchars[op][2] & OPF_LINES;
103}
104
Bram Moolenaar113e1072019-01-20 15:30:40 +0100105#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200106/*
107 * Return TRUE if operator "op" changes text.
108 */
109 int
110op_is_change(int op)
111{
112 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
116/*
117 * Get first operator command character.
118 * Returns 'g' or 'z' if there is another command character.
119 */
120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100121get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return opchars[optype][0];
124}
125
126/*
127 * Get second operator command character.
128 */
129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100130get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 return opchars[optype][1];
133}
134
135/*
136 * op_shift - handle a shift operation
137 */
138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100139op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 long i;
142 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145 if (u_save((linenr_T)(oap->start.lnum - 1),
146 (linenr_T)(oap->end.lnum + 1)) == FAIL)
147 return;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 if (oap->block_mode)
150 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152 for (i = oap->line_count; --i >= 0; )
153 {
154 first_char = *ml_get_curline();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100155 if (first_char == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 else if (oap->block_mode)
158 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100160 // Move the line right if it doesn't start with '#', 'smartindent'
161 // isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
163 if (first_char != '#' || !preprocs_left())
164#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000165 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++curwin->w_cursor.lnum;
167 }
168
169 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (oap->block_mode)
171 {
172 curwin->w_cursor.lnum = oap->start.lnum;
173 curwin->w_cursor.col = block_col;
174 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100175 else if (curs_top) // put cursor on first line, for ">>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 {
177 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100178 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
180 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100181 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100184 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100185 foldOpenCursor();
186#endif
187
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (oap->line_count > p_report)
190 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200191 char *op;
192 char *msg_line_single;
193 char *msg_line_plural;
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200196 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200198 op = "<";
199 msg_line_single = NGETTEXT("%ld line %sed %d time",
200 "%ld line %sed %d times", amount);
201 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
202 "%ld lines %sed %d times", amount);
203 vim_snprintf((char *)IObuff, IOSIZE,
204 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
205 oap->line_count, op, amount);
Bram Moolenaare4fc7462020-03-15 19:17:50 +0100206 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
208
Bram Moolenaare1004402020-10-24 20:49:43 +0200209 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100210 {
211 // Set "'[" and "']" marks.
212 curbuf->b_op_start = oap->start;
213 curbuf->b_op_end.lnum = oap->end.lnum;
214 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
215 if (curbuf->b_op_end.col > 0)
216 --curbuf->b_op_end.col;
217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
220/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100221 * Shift the current line one shiftwidth left (if left != 0) or right
222 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 */
224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100225shift_line(
226 int left,
227 int round,
228 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200229 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 int count;
232 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200233 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200235 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200237 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200239 i = count / sw_val; // number of 'shiftwidth' rounded down
240 j = count % sw_val; // extra spaces
241 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 --amount;
243 if (left)
244 {
245 i -= amount;
246 if (i < 0)
247 i = 0;
248 }
249 else
250 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200251 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200253 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
255 if (left)
256 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200257 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 if (count < 0)
259 count = 0;
260 }
261 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200262 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200265 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000267 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000269 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270}
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272/*
273 * Shift one line of the current block one shiftwidth right or left.
274 * Leaves cursor on first character in block.
275 */
276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100277shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278{
279 int left = (oap->op_type == OP_LSHIFT);
280 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000281 int total;
282 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 int sw_val = (int)get_sw_value_indent(curbuf);
285 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000288 colnr_T ws_vcol;
LemonBoy4b936742022-05-13 21:56:28 +0100289 int added;
290 unsigned new_line_len; // the length of the line after the
291 // block shift
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifdef FEAT_RIGHTLEFT
293 int old_p_ri = p_ri;
294
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100295 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#endif
297
Bram Moolenaar24959102022-05-07 20:01:16 +0100298 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
300 if (bd.is_short)
301 return;
302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100303 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 total = (int)((unsigned)amount * (unsigned)sw_val);
305 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100306 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 oldp = ml_get_curline();
309
310 if (!left)
311 {
LemonBoy4b936742022-05-13 21:56:28 +0100312 int tabs = 0, spaces = 0;
313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 /*
315 * 1. Get start vcol
316 * 2. Total ws vcols
317 * 3. Divvy into TABs & spp
318 * 4. Construct new string
319 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100320 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 ws_vcol = bd.start_vcol - bd.pre_whitesp;
322 if (bd.startspaces)
323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100325 {
326 if ((*mb_ptr2len)(bd.textstart) == 1)
327 ++bd.textstart;
328 else
329 {
330 ws_vcol = 0;
331 bd.startspaces = 0;
332 }
333 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000334 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000335 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100337 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100339 // TODO: is passing bd.textstart for start of the line OK?
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000340 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, bd.start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 total += incr;
342 bd.start_vcol += incr;
343 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // OK, now total=all the VWS reqd, and textstart points at the 1st
345 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200346#ifdef FEAT_VARTABS
347 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200348 tabstop_fromto(ws_vcol, ws_vcol + total,
LemonBoy4b936742022-05-13 21:56:28 +0100349 ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200350 else
LemonBoy4b936742022-05-13 21:56:28 +0100351 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200352#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (!curbuf->b_p_et)
LemonBoy4b936742022-05-13 21:56:28 +0100354 tabs = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
355 if (tabs > 0)
356 spaces = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 else
LemonBoy4b936742022-05-13 21:56:28 +0100358 spaces = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200359#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100360 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
LemonBoy4b936742022-05-13 21:56:28 +0100362
363 new_line_len = bd.textcol + tabs + spaces + (int)STRLEN(bd.textstart);
364 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (newp == NULL)
366 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 mch_memmove(newp, oldp, (size_t)bd.textcol);
LemonBoy4b936742022-05-13 21:56:28 +0100368 vim_memset(newp + bd.textcol, TAB, (size_t)tabs);
369 vim_memset(newp + bd.textcol + tabs, ' ', (size_t)spaces);
370 // Note that STRMOVE() copies the trailing NUL.
371 STRMOVE(newp + bd.textcol + tabs + spaces, bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100373 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100375 colnr_T destination_col; // column to which text in block will
376 // be shifted
377 char_u *verbatim_copy_end; // end of the part of the line which is
378 // copied verbatim
379 colnr_T verbatim_copy_width;// the (displayed) width of this part
380 // of line
381 unsigned fill; // nr of spaces that replace a TAB
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000382 size_t block_space_width;
383 size_t shift_amount;
384 char_u *non_white = bd.textstart;
385 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000387 /*
388 * Firstly, let's find the first non-whitespace character that is
389 * displayed after the block's start column and the character's column
390 * number. Also, let's calculate the width of all the whitespace
391 * characters that are displayed in the block and precede the searched
392 * non-whitespace character.
393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100395 // If "bd.startspaces" is set, "bd.textstart" points to the character,
396 // the part of which is displayed at the block's beginning. Let's start
397 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000398 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100399 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100401 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000402 non_white_col = bd.start_vcol;
403
Bram Moolenaar1c465442017-03-12 20:10:05 +0100404 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000405 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200406 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000407 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000410 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100411 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000412 shift_amount = (block_space_width < (size_t)total
413 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100415 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000416 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Now let's find out how much of the beginning of the line we can
419 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000420 verbatim_copy_end = bd.textstart;
421 verbatim_copy_width = bd.start_vcol;
422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100423 // If "bd.startspaces" is set, "bd.textstart" points to the character
424 // preceding the block. We have to subtract its width to obtain its
425 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000426 if (bd.startspaces)
427 verbatim_copy_width -= bd.start_char_vcols;
428 while (verbatim_copy_width < destination_col)
429 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200430 char_u *line = verbatim_copy_end;
431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100432 // TODO: is passing verbatim_copy_end for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200433 incr = lbr_chartabsize(line, verbatim_copy_end,
434 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000435 if (verbatim_copy_width + incr > destination_col)
436 break;
437 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100438 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000439 }
440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100441 // If "destination_col" is different from the width of the initial
442 // part of the line that will be copied, it means we encountered a tab
443 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000444 fill = destination_col - verbatim_copy_width;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // The replacement line will consist of:
447 // - the beginning of the original line up to "verbatim_copy_end",
448 // - "fill" number of spaces,
449 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000450 new_line_len = (unsigned)(verbatim_copy_end - oldp)
451 + fill
LemonBoy4b936742022-05-13 21:56:28 +0100452 + (unsigned)STRLEN(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000453
LemonBoy4b936742022-05-13 21:56:28 +0100454 newp = alloc(new_line_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (newp == NULL)
456 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200458 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
LemonBoy4b936742022-05-13 21:56:28 +0100459 // Note that STRMOVE() copies the trailing NUL.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000460 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100462 // replace the line
LemonBoy4b936742022-05-13 21:56:28 +0100463 added = new_line_len - (int)STRLEN(oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
LemonBoy4b936742022-05-13 21:56:28 +0100465 inserted_bytes(curwin->w_cursor.lnum, bd.textcol, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 State = oldstate;
467 curwin->w_cursor.col = oldcol;
468#ifdef FEAT_RIGHTLEFT
469 p_ri = old_p_ri;
470#endif
471}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473/*
474 * Insert string "s" (b_insert ? before : after) block :AKelly
475 * Caller must prepare for undo.
476 */
477 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100478block_insert(
479 oparg_T *oap,
480 char_u *s,
481 int b_insert,
482 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
Bram Moolenaardac13472019-09-16 21:06:21 +0200484 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100485 int count = 0; // extra spaces to replace a cut TAB
486 int spaces = 0; // non-zero if cutting a TAB
487 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200488 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 unsigned s_len; // STRLEN(s)
490 char_u *newp, *oldp; // new, old lines
491 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int oldstate = State;
493
Bram Moolenaar24959102022-05-07 20:01:16 +0100494 State = MODE_INSERT; // don't want MODE_REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 s_len = (unsigned)STRLEN(s);
496
497 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
498 {
499 block_prep(oap, bdp, lnum, TRUE);
500 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100501 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
503 oldp = ml_get(lnum);
504
505 if (b_insert)
506 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200507 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 spaces = bdp->startspaces;
509 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100510 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 offset = bdp->textcol;
512 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100513 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200515 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100516 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200518 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100520 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 offset = bdp->textcol + bdp->textlen - (spaces != 0);
522 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100523 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (!bdp->is_MAX)
527 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
528 count = spaces;
529 offset = bdp->textcol + bdp->textlen;
530 }
531 }
532
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200533 if (has_mbyte && spaces > 0)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000534 // avoid copying part of a multi-byte character
535 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100536
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200537 if (spaces < 0) // can happen when the cursor was moved
538 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200539
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000540 // Make sure the allocated size matches what is actually copied below.
541 newp = alloc(STRLEN(oldp) + spaces + s_len
542 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
543 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (newp == NULL)
545 continue;
546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100547 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000548 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 oldp += offset;
550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100551 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200552 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200553 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100555 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200556 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 offset += s_len;
558
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000559 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000561 if (*oldp == TAB)
562 {
563 // insert post-padding
564 vim_memset(newp + offset + spaces, ' ',
565 (size_t)(ts_val - spaces));
566 // we're splitting a TAB, don't copy it
567 oldp++;
568 // We allowed for that TAB, remember this now
569 count++;
570 }
571 else
572 // Not a TAB, no extra spaces
573 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575
576 if (spaces > 0)
577 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000578 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
580 ml_replace(lnum, newp, FALSE);
581
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200582 if (b_insert)
583 // correct any text properties
584 inserted_bytes(lnum, startcol, s_len);
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (lnum == oap->end.lnum)
587 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 // Set "']" mark to the end of the block instead of the end of
589 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 curbuf->b_op_end.lnum = oap->end.lnum;
591 curbuf->b_op_end.col = offset;
592 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100593 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
595 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
596
597 State = oldstate;
598}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200601 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200603 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 */
605 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100606op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 int n;
609 linenr_T lnum;
610 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 char_u *newp, *oldp;
612 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
614 int did_yank = FALSE;
615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100616 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 return OK;
618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100619 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (oap->empty)
621 return u_save_cursor();
622
623 if (!curbuf->b_p_ma)
624 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200625 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 return FAIL;
627 }
628
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000629 if (VIsual_select && oap->is_VIsual)
630 // use register given with CTRL_R, defaults to zero
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100631 oap->regname = VIsual_select_reg;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633#ifdef FEAT_CLIPBOARD
634 adjust_clip_reg(&oap->regname);
635#endif
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (has_mbyte)
638 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaard04b7502010-07-08 22:27:55 +0200640 /*
641 * Imitate the strange Vi behaviour: If the delete spans more than one
642 * line and motion_type == MCHAR and the result is a blank line, make the
643 * delete linewise. Don't do this for the change command or Visual mode.
644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000647 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100649 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 && oap->op_type == OP_DELETE)
651 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200652 ptr = ml_get(oap->end.lnum) + oap->end.col;
653 if (*ptr != NUL)
654 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 ptr = skipwhite(ptr);
656 if (*ptr == NUL && inindent(0))
657 oap->motion_type = MLINE;
658 }
659
Bram Moolenaard04b7502010-07-08 22:27:55 +0200660 /*
661 * Check for trying to delete (e.g. "D") in an empty line.
662 * Note: For the change operator it is ok.
663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if ( oap->motion_type == MCHAR
665 && oap->line_count == 1
666 && oap->op_type == OP_DELETE
667 && *ml_get(oap->start.lnum) == NUL)
668 {
669 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000670 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 * 'cpoptions' (Vi compatible).
672 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000673 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100674 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
675 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000676 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
678 beep_flush();
679 return OK;
680 }
681
Bram Moolenaard04b7502010-07-08 22:27:55 +0200682 /*
683 * Do a yank of whatever we're about to delete.
684 * If a yank register was specified, put the deleted text into that
685 * register. For the black hole register '_' don't yank anything.
686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (oap->regname != '_')
688 {
689 if (oap->regname != 0)
690 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100691 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 if (!valid_yank_reg(oap->regname, TRUE))
693 {
694 beep_flush();
695 return OK;
696 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100697 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
698 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 did_yank = TRUE;
700 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200701 else
702 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
704 /*
705 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100706 * delete contains a line break, or when using a specific operator (Vi
707 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200708 * Use the register name from before adjust_clip_reg() may have
709 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100711 if (oap->motion_type == MLINE || oap->line_count > 1
712 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100714 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (op_yank(oap, TRUE, FALSE) == OK)
716 did_yank = TRUE;
717 }
718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 // Yank into small delete register when no named register specified
720 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200721 if ((
722#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200723 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
724 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200725#endif
726 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 && oap->line_count == 1)
728 {
729 oap->regname = '-';
730 get_yank_register(oap->regname, TRUE);
731 if (op_yank(oap, TRUE, FALSE) == OK)
732 did_yank = TRUE;
733 oap->regname = 0;
734 }
735
736 /*
737 * If there's too much stuff to fit in the yank register, then get a
738 * confirmation before doing the delete. This is crude, but simple.
739 * And it avoids doing a delete of something we can't put back if we
740 * want.
741 */
742 if (!did_yank)
743 {
744 int msg_silent_save = msg_silent;
745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100746 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
748 msg_silent = msg_silent_save;
749 if (n != 'y')
750 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000751 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return FAIL;
753 }
754 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100755
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100756#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100757 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200758 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761
Bram Moolenaard04b7502010-07-08 22:27:55 +0200762 /*
763 * block mode delete
764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (oap->block_mode)
766 {
767 if (u_save((linenr_T)(oap->start.lnum - 1),
768 (linenr_T)(oap->end.lnum + 1)) == FAIL)
769 return FAIL;
770
771 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
772 {
773 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100774 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 continue;
776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100777 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (lnum == curwin->w_cursor.lnum)
779 {
780 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783
Bram Moolenaar8055d172019-05-17 22:57:26 +0200784 // "n" == number of chars deleted
785 // If we delete a TAB, it may be replaced by several characters.
786 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 n = bd.textlen - bd.startspaces - bd.endspaces;
788 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200789 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (newp == NULL)
791 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100792 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100794 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200795 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000799 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100800 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200802
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100803#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200804 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200805 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
808
809 check_cursor_col();
810 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
811 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100812 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100814 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 if (oap->op_type == OP_CHANGE)
817 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // Delete the lines except the first one. Temporarily move the
819 // cursor to the next line. Save the current line number, if the
820 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (oap->line_count > 1)
822 {
823 lnum = curwin->w_cursor.lnum;
824 ++curwin->w_cursor.lnum;
825 del_lines((long)(oap->line_count - 1), TRUE);
826 curwin->w_cursor.lnum = lnum;
827 }
828 if (u_save_cursor() == FAIL)
829 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100832 beginline(BL_WHITE); // cursor on first non-white
833 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 ai_col = curwin->w_cursor.col;
835 }
836 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 beginline(0); // cursor in column 0
838 truncate_line(FALSE); // delete the rest of the line
839 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 else
844 {
845 del_lines(oap->line_count, TRUE);
846 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100847 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849 }
850 else
851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (virtual_op)
853 {
854 int endcol = 0;
855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (gchar_pos(&oap->start) == '\t')
858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 return FAIL;
861 if (oap->line_count == 1)
862 endcol = getviscol2(oap->end.col, oap->end.coladd);
863 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
864 oap->start = curwin->w_cursor;
865 if (oap->line_count == 1)
866 {
867 coladvance(endcol);
868 oap->end.col = curwin->w_cursor.col;
869 oap->end.coladd = curwin->w_cursor.coladd;
870 curwin->w_cursor = oap->start;
871 }
872 }
873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100874 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (gchar_pos(&oap->end) == '\t'
876 && (int)oap->end.coladd < oap->inclusive)
877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (u_save((linenr_T)(oap->end.lnum - 1),
880 (linenr_T)(oap->end.lnum + 1)) == FAIL)
881 return FAIL;
882 curwin->w_cursor = oap->end;
883 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
884 oap->end = curwin->w_cursor;
885 curwin->w_cursor = oap->start;
886 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100887 if (has_mbyte)
888 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100891 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 return FAIL;
895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100896 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100897 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 && oap->op_type == OP_CHANGE
899 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100900 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 display_dollar(oap->end.col - !oap->inclusive);
902
903 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (virtual_op)
906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100907 // fix up things for virtualedit-delete:
908 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 char_u *curline = ml_get_curline();
910 int len = (int)STRLEN(curline);
911
912 if (oap->end.coladd != 0
913 && (int)oap->end.col >= len - 1
914 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
915 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (n == 0 && oap->start.coladd != oap->end.coladd)
918 n = 1;
919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (gchar_cursor() != NUL)
922 curwin->w_cursor.coladd = 0;
923 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200924 (void)del_bytes((long)n, !virtual_op,
925 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100927 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
929 pos_T curpos;
930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
933 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
934 return FAIL;
935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100936 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100938 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 ++curwin->w_cursor.lnum;
940 del_lines((long)(oap->line_count - 2), FALSE);
941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100942 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200943 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200944 curwin->w_cursor.col = 0;
945 (void)del_bytes((long)n, !virtual_op,
946 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200948 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200950 if (oap->op_type == OP_DELETE)
951 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953
954 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
955
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000956setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200957 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100959 if (oap->block_mode)
960 {
961 curbuf->b_op_end.lnum = oap->end.lnum;
962 curbuf->b_op_end.col = oap->start.col;
963 }
964 else
965 curbuf->b_op_end = oap->start;
966 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 return OK;
970}
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972/*
973 * Adjust end of operating area for ending on a multi-byte character.
974 * Used for deletion.
975 */
976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100977mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 char_u *p;
980
981 if (oap->inclusive)
982 {
983 p = ml_get(oap->end.lnum);
984 oap->end.col += mb_tail_off(p, p + oap->end.col);
985 }
986}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar630afe82018-06-28 19:26:28 +0200988/*
989 * Replace the character under the cursor with "c".
990 * This takes care of multi-byte characters.
991 */
992 static void
993replace_character(int c)
994{
995 int n = State;
996
Bram Moolenaar24959102022-05-07 20:01:16 +0100997 State = MODE_REPLACE;
Bram Moolenaar630afe82018-06-28 19:26:28 +0200998 ins_char(c);
999 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001000 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001001 dec_cursor();
1002}
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004/*
1005 * Replace a whole area with one character.
1006 */
1007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001008op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 char_u *newp, *oldp;
1013 size_t oldlen;
1014 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001015 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001016 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001019 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001021 if (c == REPLACE_CR_NCHAR)
1022 {
1023 had_ctrl_v_cr = TRUE;
1024 c = CAR;
1025 }
1026 else if (c == REPLACE_NL_NCHAR)
1027 {
1028 had_ctrl_v_cr = TRUE;
1029 c = NL;
1030 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (has_mbyte)
1033 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 if (u_save((linenr_T)(oap->start.lnum - 1),
1036 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1037 return FAIL;
1038
1039 /*
1040 * block mode replace
1041 */
1042 if (oap->block_mode)
1043 {
1044 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1045 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1049 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001050 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001052 // n == number of extra chars required
1053 // If we split a TAB, it may be replaced by several characters.
1054 // Thus the number of characters may increase!
1055 // If the range starts in virtual space, count the initial
1056 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1058 {
1059 pos_T vpos;
1060
Bram Moolenaara1381de2009-11-03 15:44:21 +00001061 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 getvpos(&vpos, oap->start_vcol);
1063 bd.startspaces += vpos.coladd;
1064 n = bd.startspaces;
1065 }
1066 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001070 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001074 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 numc = oap->end_vcol - oap->start_vcol + 1;
1076 if (bd.is_short && (!virtual_op || bd.is_MAX))
1077 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001079 // A double-wide character can be replaced only up to half the
1080 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if ((*mb_char2cells)(c) > 1)
1082 {
1083 if ((numc & 1) && !bd.is_short)
1084 {
1085 ++bd.endspaces;
1086 ++n;
1087 }
1088 numc = numc / 2;
1089 }
1090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001091 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 num_chars = numc;
1093 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001094 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 n += numc - bd.textlen;
1096
1097 oldp = ml_get_curline();
1098 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001099 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (newp == NULL)
1101 continue;
1102 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001103 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 mch_memmove(newp, oldp, (size_t)bd.textcol);
1105 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001106 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001107 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001108 // insert replacement chars CHECK FOR ALLOCATED SPACE
1109 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1110 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001111 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001113 if (has_mbyte)
1114 {
1115 n = (int)STRLEN(newp);
1116 while (--num_chars >= 0)
1117 n += (*mb_char2bytes)(c, newp + n);
1118 }
1119 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001120 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001121 if (!bd.is_short)
1122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001123 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001124 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001125 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001126 STRMOVE(newp + STRLEN(newp), oldp);
1127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001131 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001132 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001133 if (after_p != NULL)
1134 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001138 if (after_p != NULL)
1139 {
1140 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1141 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1142 oap->end.lnum++;
1143 vim_free(after_p);
1144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146 }
1147 else
1148 {
1149 /*
1150 * MCHAR and MLINE motion replace.
1151 */
1152 if (oap->motion_type == MLINE)
1153 {
1154 oap->start.col = 0;
1155 curwin->w_cursor.col = 0;
1156 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1157 if (oap->end.col)
1158 --oap->end.col;
1159 }
1160 else if (!oap->inclusive)
1161 dec(&(oap->end));
1162
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001163 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
1165 n = gchar_cursor();
1166 if (n != NUL)
1167 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001168 int new_byte_len = (*mb_char2len)(c);
1169 int old_byte_len = mb_ptr2len(ml_get_cursor());
1170
1171 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001173 // This is slow, but it handles replacing a single-byte
1174 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001175 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001176 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001177 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (n == TAB)
1182 {
1183 int end_vcol = 0;
1184
1185 if (curwin->w_cursor.lnum == oap->end.lnum)
1186 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001187 // oap->end has to be recalculated when
1188 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 end_vcol = getviscol2(oap->end.col,
1190 oap->end.coladd);
1191 }
1192 coladvance_force(getviscol());
1193 if (curwin->w_cursor.lnum == oap->end.lnum)
1194 getvpos(&oap->end, end_vcol);
1195 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001196 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1200 {
1201 int virtcols = oap->end.coladd;
1202
1203 if (curwin->w_cursor.lnum == oap->start.lnum
1204 && oap->start.col == oap->end.col && oap->start.coladd)
1205 virtcols -= oap->start.coladd;
1206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001207 // oap->end has been trimmed so it's effectively inclusive;
1208 // as a result an extra +1 must be counted so we don't
1209 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1211 curwin->w_cursor.col -= (virtcols + 1);
1212 for (; virtcols >= 0; virtcols--)
1213 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001214 if ((*mb_char2len)(c) > 1)
Bram Moolenaar630afe82018-06-28 19:26:28 +02001215 replace_character(c);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001216 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001217 PBYTE(curwin->w_cursor, c);
1218 if (inc(&curwin->w_cursor) == -1)
1219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001223 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (inc_cursor() == -1)
1225 break;
1226 }
1227 }
1228
1229 curwin->w_cursor = oap->start;
1230 check_cursor();
1231 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1232
Bram Moolenaare1004402020-10-24 20:49:43 +02001233 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001234 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001236 curbuf->b_op_start = oap->start;
1237 curbuf->b_op_end = oap->end;
1238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 return OK;
1241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001243static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245/*
1246 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1247 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001249op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250{
1251 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001253 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
1255 if (u_save((linenr_T)(oap->start.lnum - 1),
1256 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1257 return;
1258
1259 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001260 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1263 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001264 int one_change;
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 block_prep(oap, &bd, pos.lnum, FALSE);
1267 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001268 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1269 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001270
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001271#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001272 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
1274 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1275
Bram Moolenaar009b2592004-10-24 19:18:58 +00001276 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1277 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001279 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 if (did_change)
1284 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1285 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001286 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 {
1288 if (oap->motion_type == MLINE)
1289 {
1290 oap->start.col = 0;
1291 pos.col = 0;
1292 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1293 if (oap->end.col)
1294 --oap->end.col;
1295 }
1296 else if (!oap->inclusive)
1297 dec(&(oap->end));
1298
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001299 if (pos.lnum == oap->end.lnum)
1300 did_change = swapchars(oap->op_type, &pos,
1301 oap->end.col - pos.col + 1);
1302 else
1303 for (;;)
1304 {
1305 did_change |= swapchars(oap->op_type, &pos,
1306 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1307 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001308 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001309 break;
1310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (did_change)
1312 {
1313 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1314 0L);
1315#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarfe154992022-03-22 20:42:12 +00001316 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
1318 char_u *ptr;
1319 int count;
1320
1321 pos = oap->start;
1322 while (pos.lnum < oap->end.lnum)
1323 {
1324 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001325 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001326 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001328 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 pos.col = 0;
1330 pos.lnum++;
1331 }
1332 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1333 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001334 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001336 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338#endif
1339 }
1340 }
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001343 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
Bram Moolenaare1004402020-10-24 20:49:43 +02001346 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001347 {
1348 // Set '[ and '] marks.
1349 curbuf->b_op_start = oap->start;
1350 curbuf->b_op_end = oap->end;
1351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001354 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001355 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356}
1357
1358/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001359 * Invoke swapchar() on "length" bytes at position "pos".
1360 * "pos" is advanced to just after the changed characters.
1361 * "length" is rounded up to include the whole last multi-byte character.
1362 * Also works correctly when the number of bytes changes.
1363 * Returns TRUE if some character was changed.
1364 */
1365 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001366swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001367{
1368 int todo;
1369 int did_change = 0;
1370
1371 for (todo = length; todo > 0; --todo)
1372 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001373 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001374 {
1375 int len = (*mb_ptr2len)(ml_get_pos(pos));
1376
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001377 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001378 if (len > 0)
1379 todo -= len - 1;
1380 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001381 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001382 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001383 break;
1384 }
1385 return did_change;
1386}
1387
1388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 * If op_type == OP_UPPER: make uppercase,
1390 * if op_type == OP_LOWER: make lowercase,
1391 * if op_type == OP_ROT13: do rot13 encoding,
1392 * else swap case of character at 'pos'
1393 * returns TRUE when something actually changed.
1394 */
1395 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001396swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
1398 int c;
1399 int nc;
1400
1401 c = gchar_pos(pos);
1402
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001403 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if (c >= 0x80 && op_type == OP_ROT13)
1405 return FALSE;
1406
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001407 if (op_type == OP_UPPER && c == 0xdf
1408 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001409 {
1410 pos_T sp = curwin->w_cursor;
1411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001412 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001413 curwin->w_cursor = *pos;
1414 del_char(FALSE);
1415 ins_char('S');
1416 ins_char('S');
1417 curwin->w_cursor = sp;
1418 inc(pos);
1419 }
1420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001421 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 nc = c;
1424 if (MB_ISLOWER(c))
1425 {
1426 if (op_type == OP_ROT13)
1427 nc = ROT13(c, 'a');
1428 else if (op_type != OP_LOWER)
1429 nc = MB_TOUPPER(c);
1430 }
1431 else if (MB_ISUPPER(c))
1432 {
1433 if (op_type == OP_ROT13)
1434 nc = ROT13(c, 'A');
1435 else if (op_type != OP_UPPER)
1436 nc = MB_TOLOWER(c);
1437 }
1438 if (nc != c)
1439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1441 {
1442 pos_T sp = curwin->w_cursor;
1443
1444 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001445 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001446 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 ins_char(nc);
1448 curwin->w_cursor = sp;
1449 }
1450 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001451 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 return TRUE;
1453 }
1454 return FALSE;
1455}
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457/*
1458 * op_insert - Insert and append operators for Visual mode.
1459 */
1460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001461op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 long ins_len, pre_textlen = 0;
1464 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001465 colnr_T ind_pre_col = 0, ind_post_col;
1466 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 struct block_def bd;
1468 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001469 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001470 pos_T start_insert;
1471 // offset when cursor was moved in insert mode
1472 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001474 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001477 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 curwin->w_cursor.lnum = oap->start.lnum;
1479 update_screen(INVERTED);
1480
1481 if (oap->block_mode)
1482 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001483 // When 'virtualedit' is used, need to insert the extra spaces before
1484 // doing block_prep(). When only "block" is used, virtual edit is
1485 // already disabled, but still need it when calling
1486 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001487 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001488 // state for the current window. To override that state, we need to
1489 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (curwin->w_cursor.coladd > 0)
1491 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001492 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if (u_save_cursor() == FAIL)
1495 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001496
Gary Johnson51ad8502021-08-03 18:33:08 +02001497 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 coladvance_force(oap->op_type == OP_APPEND
1499 ? oap->end_vcol + 1 : getviscol());
1500 if (oap->op_type == OP_APPEND)
1501 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001502 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001504 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001506 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001507 ind_pre_col = (colnr_T)getwhitecols_curline();
1508 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (oap->op_type == OP_APPEND)
1512 firstline += bd.textlen;
1513 pre_textlen = (long)STRLEN(firstline);
1514 }
1515
1516 if (oap->op_type == OP_APPEND)
1517 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001518 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001520 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 curwin->w_set_curswant = TRUE;
1522 while (*ml_get_cursor() != NUL
1523 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1524 ++curwin->w_cursor.col;
1525 if (bd.is_short && !bd.is_MAX)
1526 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001527 // First line was too short, make it longer and adjust the
1528 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (u_save_cursor() == FAIL)
1530 return;
1531 for (i = 0; i < bd.endspaces; ++i)
1532 ins_char(' ');
1533 bd.textlen += bd.endspaces;
1534 }
1535 }
1536 else
1537 {
1538 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001539 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001541 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001542 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 && oap->start_vcol != oap->end_vcol)
1544 inc_cursor();
1545 }
1546 }
1547
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001548 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001549 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001550 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001552 // When a tab was inserted, and the characters in front of the tab
1553 // have been converted to a tab as well, the column of the cursor
1554 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001555 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001556 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001557 oap->start = curbuf->b_op_start_orig;
1558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001559 // If user has moved off this line, we don't know what to do, so do
1560 // nothing.
1561 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001562 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return;
1564
1565 if (oap->block_mode)
1566 {
1567 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001568 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001569 size_t len;
1570 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001572 // If indent kicked in, the firstline might have changed
1573 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001574 ind_post_col = (colnr_T)getwhitecols_curline();
1575 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001576 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001577 bd.textcol += ind_post_col - ind_pre_col;
1578 ind_post_vcol = get_indent();
1579 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001580 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001581 }
1582
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001583 // The user may have moved the cursor before inserting something, try
1584 // to adjust the block for that. But only do it, if the difference
1585 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001586 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1587 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001588 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001589 int t = getviscol2(curbuf->b_op_start_orig.col,
1590 curbuf->b_op_start_orig.coladd);
1591
zeertzjq7745f142022-02-15 11:48:22 +00001592 if (oap->op_type == OP_INSERT
1593 && oap->start.col + oap->start.coladd
1594 != curbuf->b_op_start_orig.col
1595 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001596 {
zeertzjq7745f142022-02-15 11:48:22 +00001597 oap->start.col = curbuf->b_op_start_orig.col;
1598 pre_textlen -= t - oap->start_vcol;
1599 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001600 }
zeertzjq7745f142022-02-15 11:48:22 +00001601 else if (oap->op_type == OP_APPEND
1602 && oap->start.col + oap->start.coladd
1603 >= curbuf->b_op_start_orig.col
1604 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001605 {
zeertzjq7745f142022-02-15 11:48:22 +00001606 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001608 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001609 pre_textlen -= t - oap->start_vcol;
zeertzjq7745f142022-02-15 11:48:22 +00001610 oap->start_vcol = t;
1611 oap->op_type = OP_INSERT;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001612 }
1613 }
1614
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001615 // Spaces and tabs in the indent may have changed to other spaces and
1616 // tabs. Get the starting column again and correct the length.
1617 // Don't do this when "$" used, end-of-line will have changed.
1618 //
1619 // if indent was added and the inserted text was after the indent,
1620 // correct the selection for the new indent.
1621 if (did_indent && bd.textcol - ind_post_col > 0)
1622 {
1623 oap->start.col += ind_post_col - ind_pre_col;
1624 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1625 oap->end.col += ind_post_col - ind_pre_col;
1626 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001629 if (did_indent && bd.textcol - ind_post_col > 0)
1630 {
1631 // undo for where "oap" is used below
1632 oap->start.col -= ind_post_col - ind_pre_col;
1633 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1634 oap->end.col -= ind_post_col - ind_pre_col;
1635 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1638 {
1639 if (oap->op_type == OP_APPEND)
1640 {
1641 pre_textlen += bd2.textlen - bd.textlen;
1642 if (bd2.endspaces)
1643 --bd2.textlen;
1644 }
1645 bd.textcol = bd2.textcol;
1646 bd.textlen = bd2.textlen;
1647 }
1648
1649 /*
1650 * Subsequent calls to ml_get() flush the firstline data - take a
1651 * copy of the required string.
1652 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001653 firstline = ml_get(oap->start.lnum);
1654 len = STRLEN(firstline);
1655 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001657 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001658 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001659 // account for pressing cursor in insert mode when '$' was used
1660 if (bd.is_MAX
1661 && (start_insert.lnum == Insstart.lnum
1662 && start_insert.col > Insstart.col))
1663 {
1664 offset = (start_insert.col - Insstart.col);
1665 add -= offset;
1666 if (oap->end_vcol > offset)
1667 oap->end_vcol -= (offset + 1);
1668 else
1669 // moved outside of the visual block, what to do?
1670 return;
1671 }
1672 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001673 if ((size_t)add > len)
1674 firstline += len; // short line, point to the NUL
1675 else
1676 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001677 if (pre_textlen >= 0 && (ins_len =
1678 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001680 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (ins_text != NULL)
1682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001683 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (u_save(oap->start.lnum,
1685 (linenr_T)(oap->end.lnum + 1)) == OK)
1686 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1687 &bd);
1688
1689 curwin->w_cursor.col = oap->start.col;
1690 check_cursor();
1691 vim_free(ins_text);
1692 }
1693 }
1694 }
1695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
1697/*
1698 * op_change - handle a change operation
1699 *
1700 * return TRUE if edit() returns because of a CTRL-O command
1701 */
1702 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001703op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 colnr_T l;
1706 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 long offset;
1708 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001709 long ins_len;
1710 long pre_textlen = 0;
1711 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 char_u *firstline;
1713 char_u *ins_text, *newp, *oldp;
1714 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
1716 l = oap->start.col;
1717 if (oap->motion_type == MLINE)
1718 {
1719 l = 0;
1720#ifdef FEAT_SMARTINDENT
1721 if (!p_paste && curbuf->b_p_si
1722# ifdef FEAT_CINDENT
1723 && !curbuf->b_p_cin
1724# endif
1725 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001726 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
1728 }
1729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 // First delete the text in the region. In an empty buffer only need to
1731 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1733 {
1734 if (u_save_cursor() == FAIL)
1735 return FALSE;
1736 }
1737 else if (op_delete(oap) == FAIL)
1738 return FALSE;
1739
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001740 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 && !virtual_op)
1742 inc_cursor();
1743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001744 // check for still on same line (<CR> in inserted text meaningless)
1745 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (oap->block_mode)
1747 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001748 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (virtual_op && (curwin->w_cursor.coladd > 0
1750 || gchar_cursor() == NUL))
1751 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001752 firstline = ml_get(oap->start.lnum);
1753 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001754 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 bd.textcol = curwin->w_cursor.col;
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1759 if (oap->motion_type == MLINE)
1760 fix_indent();
1761#endif
1762
1763 retval = edit(NUL, FALSE, (linenr_T)1);
1764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001766 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001768 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001770 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001772 // Auto-indenting may have changed the indent. If the cursor was past
1773 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001775 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001777 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001778
1779 pre_textlen += new_indent - pre_indent;
1780 bd.textcol += new_indent - pre_indent;
1781 }
1782
1783 ins_len = (long)STRLEN(firstline) - pre_textlen;
1784 if (ins_len > 0)
1785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001786 // Subsequent calls to ml_get() flush the firstline data - take a
1787 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001788 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001790 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1792 linenr++)
1793 {
1794 block_prep(oap, &bd, linenr, TRUE);
1795 if (!bd.is_short || virtual_op)
1796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 pos_T vpos;
1798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 // If the block starts in virtual space, count the
1800 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (bd.is_short)
1802 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001803 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 else
1807 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001809 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (newp == NULL)
1811 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001812 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 mch_memmove(newp, oldp, (size_t)bd.textcol);
1814 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001815 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1818 offset += ins_len;
1819 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001820 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 ml_replace(linenr, newp, FALSE);
1822 }
1823 }
1824 check_cursor();
1825
1826 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1827 }
1828 vim_free(ins_text);
1829 }
1830 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001831 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 return retval;
1834}
1835
1836/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001837 * When the cursor is on the NUL past the end of the line and it should not be
1838 * there move it left.
1839 */
1840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001841adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001842{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001843 unsigned int cur_ve_flags = get_ve_flags();
1844
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001845 if (curwin->w_cursor.col > 0
1846 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001847 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001848 && !(restart_edit || (State & MODE_INSERT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001850 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001851 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001852
Gary Johnson53ba05b2021-07-26 22:19:10 +02001853 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001854 {
1855 colnr_T scol, ecol;
1856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001857 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001858 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1859 curwin->w_cursor.coladd = ecol - scol + 1;
1860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862}
1863
Bram Moolenaar81340392012-06-06 16:12:59 +02001864/*
1865 * If "process" is TRUE and the line begins with a comment leader (possibly
1866 * after some white space), return a pointer to the text after it. Put a boolean
1867 * value indicating whether the line ends with an unclosed comment in
1868 * "is_comment".
1869 * line - line to be processed,
1870 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001871 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001872 * include_space - whether to also skip space following the comment leader,
1873 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001874 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001875 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001877skip_comment(
1878 char_u *line,
1879 int process,
1880 int include_space,
1881 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001882{
1883 char_u *comment_flags = NULL;
1884 int lead_len;
1885 int leader_offset = get_last_leader_offset(line, &comment_flags);
1886
1887 *is_comment = FALSE;
1888 if (leader_offset != -1)
1889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001890 // Let's check whether the line ends with an unclosed comment.
1891 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001892 while (*comment_flags)
1893 {
1894 if (*comment_flags == COM_END
1895 || *comment_flags == ':')
1896 break;
1897 ++comment_flags;
1898 }
1899 if (*comment_flags != COM_END)
1900 *is_comment = TRUE;
1901 }
1902
1903 if (process == FALSE)
1904 return line;
1905
1906 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1907
1908 if (lead_len == 0)
1909 return line;
1910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001911 // Find:
1912 // - COM_END,
1913 // - colon,
1914 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001915 while (*comment_flags)
1916 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001917 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001918 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001919 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001920 ++comment_flags;
1921 }
1922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001923 // If we found a colon, it means that we are not processing a line
1924 // starting with a closing part of a three-part comment. That's good,
1925 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001926 if (*comment_flags == ':' || *comment_flags == NUL)
1927 line += lead_len;
1928
1929 return line;
1930}
Bram Moolenaar81340392012-06-06 16:12:59 +02001931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001933 * Join 'count' lines (minimal 2) at cursor position.
1934 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001935 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1936 * leaders should not be removed.
1937 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1938 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001940 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 */
1942 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001943do_join(
1944 long count,
1945 int insert_space,
1946 int save_undo,
1947 int use_formatoptions UNUSED,
1948 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001950 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001951 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001952 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001954 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001955 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001956 int endcurr1 = NUL;
1957 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001958 int currsize = 0; // size of the current line
1959 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001961 colnr_T col = 0;
1962 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001963 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001964 int remove_comments = (use_formatoptions == TRUE)
1965 && has_format_option(FO_REMOVE_COMS);
1966 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001967#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001968 int propcount = 0; // number of props over all joined lines
1969 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001972 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1973 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1974 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001976 // Allocate an array to store the number of spaces inserted before each
1977 // line. We will use it to pre-compute the length of the new line and the
1978 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001979 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001980 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001982 if (remove_comments)
1983 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001984 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001985 if (comments == NULL)
1986 {
1987 vim_free(spaces);
1988 return FAIL;
1989 }
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001993 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001994 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001995 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001997 for (t = 0; t < count; ++t)
1998 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001999 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002000#ifdef FEAT_PROP_POPUP
2001 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
2002#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002003 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002004 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002005 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002006 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2007 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2008 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002009 if (remove_comments)
2010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 // We don't want to remove the comment leader if the
2012 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002013 if (t > 0 && prev_was_comment)
2014 {
2015
2016 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2017 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002018 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002019 curr = new_curr;
2020 }
2021 else
2022 curr = skip_comment(curr, FALSE, insert_space,
2023 &prev_was_comment);
2024 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002025
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002026 if (insert_space && t > 0)
2027 {
2028 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002029 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002030 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002031 && (!has_format_option(FO_MBYTE_JOIN)
2032 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2033 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002034 || (mb_ptr2char(curr) < 0x100
2035 && !(enc_utf8 && utf_eat_space(endcurr1)))
2036 || (endcurr1 < 0x100
2037 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002038 )
2039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002040 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002041 if (endcurr1 == ' ')
2042 endcurr1 = endcurr2;
2043 else
2044 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002045 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002046 if ( p_js
2047 && (endcurr1 == '.'
2048 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2049 && (endcurr1 == '?' || endcurr1 == '!'))))
2050 ++spaces[t];
2051 }
2052 }
2053 currsize = (int)STRLEN(curr);
2054 sumsize += currsize + spaces[t];
2055 endcurr1 = endcurr2 = NUL;
2056 if (insert_space && currsize > 0)
2057 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002058 if (has_mbyte)
2059 {
2060 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002061 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002062 endcurr1 = (*mb_ptr2char)(cend);
2063 if (cend > curr)
2064 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002065 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002066 endcurr2 = (*mb_ptr2char)(cend);
2067 }
2068 }
2069 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002070 {
2071 endcurr1 = *(curr + currsize - 1);
2072 if (currsize > 1)
2073 endcurr2 = *(curr + currsize - 2);
2074 }
2075 }
2076 line_breakcheck();
2077 if (got_int)
2078 {
2079 ret = FAIL;
2080 goto theend;
2081 }
2082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002084 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002085 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002087 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002088 newp_len = sumsize + 1;
2089#ifdef FEAT_PROP_POPUP
2090 newp_len += propcount * sizeof(textprop_T);
2091#endif
2092 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002093 if (newp == NULL)
2094 {
2095 ret = FAIL;
2096 goto theend;
2097 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002098 cend = newp + sumsize;
2099 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002101 /*
2102 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002103 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 *
2105 * Move marks from each deleted line to the joined line, adjusting the
2106 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2107 * should not really be a problem.
2108 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002109#ifdef FEAT_PROP_POPUP
2110 props_remaining = propcount;
2111#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002112 for (t = count - 1; ; --t)
2113 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002114 int spaces_removed;
2115
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002116 cend -= currsize;
2117 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002118
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002119 if (spaces[t] > 0)
2120 {
2121 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002122 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002123 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002124
2125 // If deleting more spaces than adding, the cursor moves no more than
2126 // what is added if it is inside these spaces.
2127 spaces_removed = (curr - curr_start) - spaces[t];
2128
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002129 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, -t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002130 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002131#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002132 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2133 curwin->w_cursor.lnum + t, t == count - 1,
2134 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002135#endif
2136
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002137 if (t == 0)
2138 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002139 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002140 if (remove_comments)
2141 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002142 if (insert_space && t > 1)
2143 curr = skipwhite(curr);
2144 currsize = (int)STRLEN(curr);
2145 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002146
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002147 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
Bram Moolenaare1004402020-10-24 20:49:43 +02002149 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002151 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002152 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002153 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002154 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002156 // Only report the change in the first line here, del_lines() will report
2157 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 changed_lines(curwin->w_cursor.lnum, currsize,
2159 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002161 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * briefly, and then move it back. After del_lines() the cursor may
2163 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 */
2165 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002167 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 curwin->w_cursor.lnum = t;
2169
2170 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002171 * Set the cursor column:
2172 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002173 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002175 curwin->w_cursor.col =
2176 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 curwin->w_set_curswant = TRUE;
2181
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002182theend:
2183 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002184 if (remove_comments)
2185 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002186 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187}
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * prepare a few things for block mode yank/delete/tilde
2191 *
2192 * for delete:
2193 * - textlen includes the first/last char to be (partly) deleted
2194 * - start/endspaces is the number of columns that are taken by the
2195 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002196 * deleted.
2197 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 * - textlen includes the first/last char to be wholly yanked
2199 * - start/endspaces is the number of columns of the first/last yanked char
2200 * that are to be yanked.
2201 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002202 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002203block_prep(
2204 oparg_T *oap,
2205 struct block_def *bdp,
2206 linenr_T lnum,
2207 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 int incr = 0;
2210 char_u *pend;
2211 char_u *pstart;
2212 char_u *line;
2213 char_u *prev_pstart;
2214 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002215#ifdef FEAT_LINEBREAK
2216 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002218 // Avoid a problem with unwanted linebreaks in block mode.
2219 curwin->w_p_lbr = FALSE;
2220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 bdp->startspaces = 0;
2222 bdp->endspaces = 0;
2223 bdp->textlen = 0;
2224 bdp->start_vcol = 0;
2225 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 bdp->is_short = FALSE;
2227 bdp->is_oneChar = FALSE;
2228 bdp->pre_whitesp = 0;
2229 bdp->pre_whitesp_c = 0;
2230 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 bdp->start_char_vcols = 0;
2232
2233 line = ml_get(lnum);
2234 pstart = line;
2235 prev_pstart = line;
2236 while (bdp->start_vcol < oap->start_vcol && *pstart)
2237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 // Count a tab for what it's worth (if list mode not on)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002239 incr = lbr_chartabsize(line, pstart, bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002241 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 bdp->pre_whitesp += incr;
2244 bdp->pre_whitesp_c++;
2245 }
2246 else
2247 {
2248 bdp->pre_whitesp = 0;
2249 bdp->pre_whitesp_c = 0;
2250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002252 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
2257 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (!is_del || oap->op_type == OP_APPEND)
2260 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2261 }
2262 else
2263 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002264 // notice: this converts partly selected Multibyte characters to
2265 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2267 if (is_del && bdp->startspaces)
2268 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2269 pend = pstart;
2270 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002271 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (oap->op_type == OP_INSERT)
2275 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2276 else if (oap->op_type == OP_APPEND)
2277 {
2278 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2279 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2280 }
2281 else
2282 {
2283 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2284 if (is_del && oap->op_type != OP_LSHIFT)
2285 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002286 // just putting the sum of those two into
2287 // bdp->startspaces doesn't work for Visual replace,
2288 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 bdp->startspaces = bdp->start_char_vcols
2290 - (bdp->start_vcol - oap->start_vcol);
2291 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2292 }
2293 }
2294 }
2295 else
2296 {
2297 prev_pend = pend;
2298 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2299 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002300 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 prev_pend = pend;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002302 incr = lbr_chartabsize_adv(line, &pend, bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 bdp->end_vcol += incr;
2304 }
2305 if (bdp->end_vcol <= oap->end_vcol
2306 && (!is_del
2307 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002308 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002311 // Alternative: include spaces to fill up the block.
2312 // Disadvantage: can lead to trailing spaces when the line is
2313 // short where the text is put
2314 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (oap->op_type == OP_APPEND || virtual_op)
2316 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002317 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002319 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 else if (bdp->end_vcol > oap->end_vcol)
2322 {
2323 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2324 if (!is_del && bdp->endspaces)
2325 {
2326 bdp->endspaces = incr - bdp->endspaces;
2327 if (pend != pstart)
2328 pend = prev_pend;
2329 }
2330 }
2331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (is_del && bdp->startspaces)
2334 pstart = prev_pstart;
2335 bdp->textlen = (int)(pend - pstart);
2336 }
2337 bdp->textcol = (colnr_T) (pstart - line);
2338 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002339#ifdef FEAT_LINEBREAK
2340 curwin->w_p_lbr = lbr_saved;
2341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002345 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002347 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002348op_addsub(
2349 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002350 linenr_T Prenum1, // Amount of add/subtract
2351 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002353 pos_T pos;
2354 struct block_def bd;
2355 int change_cnt = 0;
2356 linenr_T amount = Prenum1;
2357
Bram Moolenaar6b731882018-11-16 20:54:47 +01002358 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002359 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002360 // the call to changed_lines().
2361#ifdef FEAT_FOLDING
2362 disable_fold_update++;
2363#endif
2364
Bram Moolenaard79e5502016-01-10 22:13:02 +01002365 if (!VIsual_active)
2366 {
2367 pos = curwin->w_cursor;
2368 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002369 {
2370#ifdef FEAT_FOLDING
2371 disable_fold_update--;
2372#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002373 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002374 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002375 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002376#ifdef FEAT_FOLDING
2377 disable_fold_update--;
2378#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002379 if (change_cnt)
2380 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2381 }
2382 else
2383 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002384 int one_change;
2385 int length;
2386 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002387
2388 if (u_save((linenr_T)(oap->start.lnum - 1),
2389 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002390 {
2391#ifdef FEAT_FOLDING
2392 disable_fold_update--;
2393#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002394 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002395 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002396
2397 pos = oap->start;
2398 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2399 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002400 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002401 {
2402 block_prep(oap, &bd, pos.lnum, FALSE);
2403 pos.col = bd.textcol;
2404 length = bd.textlen;
2405 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002406 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002407 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002408 curwin->w_cursor.col = 0;
2409 pos.col = 0;
2410 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2411 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002412 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002413 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002414 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002415 dec(&(oap->end));
2416 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2417 pos.col = 0;
2418 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002419 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002420 pos.col += oap->start.col;
2421 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002422 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002423 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002424 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002425 length = (int)STRLEN(ml_get(oap->end.lnum));
2426 if (oap->end.col >= length)
2427 oap->end.col = length - 1;
2428 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002429 }
2430 }
2431 one_change = do_addsub(oap->op_type, &pos, length, amount);
2432 if (one_change)
2433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002434 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002435 if (change_cnt == 0)
2436 startpos = curbuf->b_op_start;
2437 ++change_cnt;
2438 }
2439
2440#ifdef FEAT_NETBEANS_INTG
2441 if (netbeans_active() && one_change)
2442 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002443 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002444
2445 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002446 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002447 netbeans_inserted(curbuf, pos.lnum, pos.col,
2448 &ptr[pos.col], length);
2449 }
2450#endif
2451 if (g_cmd && one_change)
2452 amount += Prenum1;
2453 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002454
2455#ifdef FEAT_FOLDING
2456 disable_fold_update--;
2457#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002458 if (change_cnt)
2459 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2460
2461 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002462 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002463 redraw_curbuf_later(INVERTED);
2464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002465 // Set '[ mark if something changed. Keep the last end
2466 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002467 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002468 curbuf->b_op_start = startpos;
2469
2470 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002471 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002472 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002473 }
2474}
2475
2476/*
2477 * Add or subtract 'Prenum1' from a number in a line
2478 * op_type is OP_NR_ADD or OP_NR_SUB
2479 *
2480 * Returns TRUE if some character was changed.
2481 */
2482 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002483do_addsub(
2484 int op_type,
2485 pos_T *pos,
2486 int length,
2487 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002488{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 int col;
2490 char_u *buf1;
2491 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002492 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2493 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002494 uvarnumber_T n;
2495 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 char_u *ptr;
2497 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002499 int do_hex;
2500 int do_oct;
2501 int do_bin;
2502 int do_alpha;
2503 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002506 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002507 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002508 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002509 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002510 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002511 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002512 pos_T startpos;
2513 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002514 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002516 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2517 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2518 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2519 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2520 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002522 if (virtual_active())
2523 {
2524 save_coladd = pos->coladd;
2525 pos->coladd = 0;
2526 }
2527
Bram Moolenaard79e5502016-01-10 22:13:02 +01002528 curwin->w_cursor = *pos;
2529 ptr = ml_get(pos->lnum);
2530 col = pos->col;
2531
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002532 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002533 goto theend;
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 /*
2536 * First check if we are on a hexadecimal number, after the "0x".
2537 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002538 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002540 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002541 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002542 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002543 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002544 if (has_mbyte)
2545 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002546 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002547
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002548 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002549 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002550 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002551 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002552 if (has_mbyte)
2553 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002554 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002555
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002556 if ( do_bin
2557 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002558 && ! ((col > 0
2559 && (ptr[col] == 'X'
2560 || ptr[col] == 'x')
2561 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002562 && (!has_mbyte ||
2563 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002564 && vim_isxdigit(ptr[col + 1]))))
2565 {
2566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002567 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002568
Bram Moolenaard79e5502016-01-10 22:13:02 +01002569 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002570
2571 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002572 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002573 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002574 if (has_mbyte)
2575 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002576 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002577 }
2578
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002579 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002580 && col > 0
2581 && (ptr[col] == 'X'
2582 || ptr[col] == 'x')
2583 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002584 && (!has_mbyte ||
2585 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002586 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002587 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002588 && col > 0
2589 && (ptr[col] == 'B'
2590 || ptr[col] == 'b')
2591 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002592 && (!has_mbyte ||
2593 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002594 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002595 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002596 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002597 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002598 if (has_mbyte)
2599 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002600 }
2601 else
2602 {
2603 /*
2604 * Search forward and then backward to find the start of number.
2605 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002606 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002607
2608 while (ptr[col] != NUL
2609 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002610 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002611 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002612
2613 while (col > 0
2614 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002615 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002616 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002617 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002618 if (has_mbyte)
2619 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002620 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002621 }
2622 }
2623
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002624 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002625 {
2626 while (ptr[col] != NUL && length > 0
2627 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002628 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002629 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002630 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002631
2632 col += mb_len;
2633 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002634 }
2635
2636 if (length == 0)
2637 goto theend;
2638
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002639 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002640 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2641 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002642 {
2643 negative = TRUE;
2644 was_positive = FALSE;
2645 }
2646 }
2647
2648 /*
2649 * If a number was found, and saving for undo works, replace the number.
2650 */
2651 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002652 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002653 {
2654 beep_flush();
2655 goto theend;
2656 }
2657
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002658 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002659 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002660 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002661 if (op_type == OP_NR_SUB)
2662 {
2663 if (CharOrd(firstdigit) < Prenum1)
2664 {
2665 if (isupper(firstdigit))
2666 firstdigit = 'A';
2667 else
2668 firstdigit = 'a';
2669 }
2670 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002671 firstdigit -= Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002672 }
2673 else
2674 {
2675 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2676 {
2677 if (isupper(firstdigit))
2678 firstdigit = 'Z';
2679 else
2680 firstdigit = 'z';
2681 }
2682 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002683 firstdigit += Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002684 }
2685 curwin->w_cursor.col = col;
2686 if (!did_change)
2687 startpos = curwin->w_cursor;
2688 did_change = TRUE;
2689 (void)del_char(FALSE);
2690 ins_char(firstdigit);
2691 endpos = curwin->w_cursor;
2692 curwin->w_cursor.col = col;
2693 }
2694 else
2695 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002696 pos_T save_pos;
2697 int i;
2698
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002699 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002700 && (!has_mbyte ||
2701 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002702 && !visual
2703 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002706 --col;
2707 negative = TRUE;
2708 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002710 if (visual && VIsual_mode != 'V')
2711 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2712 ? (int)STRLEN(ptr) - col
2713 : length);
2714
2715 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002716 0 + (do_bin ? STR2NR_BIN : 0)
2717 + (do_oct ? STR2NR_OCT : 0)
2718 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002719 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002722 if (pre && negative)
2723 {
2724 ++col;
2725 --length;
2726 negative = FALSE;
2727 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002729 subtract = FALSE;
2730 if (op_type == OP_NR_SUB)
2731 subtract ^= TRUE;
2732 if (negative)
2733 subtract ^= TRUE;
2734
2735 oldn = n;
2736 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002737 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002738 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002739 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002741 if (!pre)
2742 {
2743 if (subtract)
2744 {
2745 if (n > oldn)
2746 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002747 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002748 negative ^= TRUE;
2749 }
2750 }
2751 else
2752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002754 if (n < oldn)
2755 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002756 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002757 negative ^= TRUE;
2758 }
2759 }
2760 if (n == 0)
2761 negative = FALSE;
2762 }
2763
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002764 if (do_unsigned && negative)
2765 {
2766 if (subtract)
2767 // sticking at zero.
2768 n = (uvarnumber_T)0;
2769 else
2770 // sticking at 2^64 - 1.
2771 n = (uvarnumber_T)(-1);
2772 negative = FALSE;
2773 }
2774
Bram Moolenaard79e5502016-01-10 22:13:02 +01002775 if (visual && !was_positive && !negative && col > 0)
2776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002778 col--;
2779 length++;
2780 }
2781
2782 /*
2783 * Delete the old number.
2784 */
2785 curwin->w_cursor.col = col;
2786 if (!did_change)
2787 startpos = curwin->w_cursor;
2788 did_change = TRUE;
2789 todel = length;
2790 c = gchar_cursor();
2791 /*
2792 * Don't include the '-' in the length, only the length of the
2793 * part after it is kept the same.
2794 */
2795 if (c == '-')
2796 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002797
2798 save_pos = curwin->w_cursor;
2799 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002800 {
2801 if (c < 0x100 && isalpha(c))
2802 {
2803 if (isupper(c))
2804 hexupper = TRUE;
2805 else
2806 hexupper = FALSE;
2807 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002808 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002809 c = gchar_cursor();
2810 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002811 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002812
2813 /*
2814 * Prepare the leading characters in buf1[].
2815 * When there are many leading zeros it could be very long.
2816 * Allocate a bit too much.
2817 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002818 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002819 if (buf1 == NULL)
2820 goto theend;
2821 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002822 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002823 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002824 if (pre)
2825 {
2826 *ptr++ = '0';
2827 --length;
2828 }
2829 if (pre == 'b' || pre == 'B' ||
2830 pre == 'x' || pre == 'X')
2831 {
2832 *ptr++ = pre;
2833 --length;
2834 }
2835
2836 /*
2837 * Put the number characters in buf2[].
2838 */
2839 if (pre == 'b' || pre == 'B')
2840 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002841 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002842 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002844 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002845 for (bit = bits; bit > 0; bit--)
2846 if ((n >> (bit - 1)) & 0x1) break;
2847
2848 for (i = 0; bit > 0; bit--)
2849 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2850
2851 buf2[i] = '\0';
2852 }
2853 else if (pre == 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002854 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002855 else if (pre == '0')
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002856 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002857 else if (pre && hexupper)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002858 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002859 else
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00002860 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002861 length -= (int)STRLEN(buf2);
2862
2863 /*
2864 * Adjust number of zeros to the new number of digits, so the
2865 * total length of the number remains the same.
2866 * Don't do this when
2867 * the result may look like an octal number.
2868 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002869 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002870 while (length-- > 0)
2871 *ptr++ = '0';
2872 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002873
Bram Moolenaard79e5502016-01-10 22:13:02 +01002874 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002875
2876 // Insert just after the first character to be removed, so that any
2877 // text properties will be adjusted. Then delete the old number
2878 // afterwards.
2879 save_pos = curwin->w_cursor;
2880 if (todel > 0)
2881 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002883 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002884
2885 // del_char() will also mark line needing displaying
2886 if (todel > 0)
2887 {
2888 int bytes_after = (int)STRLEN(ml_get_curline())
2889 - curwin->w_cursor.col;
2890
2891 // Delete the one character before the insert.
2892 curwin->w_cursor = save_pos;
2893 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002894 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2895 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002896 --todel;
2897 }
2898 while (todel-- > 0)
2899 (void)del_char(FALSE);
2900
Bram Moolenaard79e5502016-01-10 22:13:02 +01002901 endpos = curwin->w_cursor;
2902 if (did_change && curwin->w_cursor.col)
2903 --curwin->w_cursor.col;
2904 }
2905
Bram Moolenaare1004402020-10-24 20:49:43 +02002906 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002909 curbuf->b_op_start = startpos;
2910 curbuf->b_op_end = endpos;
2911 if (curbuf->b_op_end.col > 0)
2912 --curbuf->b_op_end.col;
2913 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002914
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002915theend:
2916 if (visual)
2917 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002918 else if (did_change)
2919 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002920 else if (virtual_active())
2921 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002922
Bram Moolenaard79e5502016-01-10 22:13:02 +01002923 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924}
2925
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002927clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002929 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930}
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002933 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 *
2935 * "Words" are counted by looking for boundaries between non-space and
2936 * space characters. (it seems to produce results that match 'wc'.)
2937 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002938 * Return value is byte count; word count for the line is added to "*wc".
2939 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 *
2941 * The function will only examine the first "limit" characters in the
2942 * line, stopping if it encounters an end-of-line (NUL byte). In that
2943 * case, eol_size will be added to the character count to account for
2944 * the size of the EOL character.
2945 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002946 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002947line_count_info(
2948 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002949 varnumber_T *wc,
2950 varnumber_T *cc,
2951 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002952 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002954 varnumber_T i;
2955 varnumber_T words = 0;
2956 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 int is_word = 0;
2958
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002959 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 if (is_word)
2962 {
2963 if (vim_isspace(line[i]))
2964 {
2965 words++;
2966 is_word = 0;
2967 }
2968 }
2969 else if (!vim_isspace(line[i]))
2970 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002971 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002972 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974
2975 if (is_word)
2976 words++;
2977 *wc += words;
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002980 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002983 chars += eol_size;
2984 }
2985 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 return i;
2987}
2988
2989/*
2990 * Give some info about the position of the cursor (for "g CTRL-G").
2991 * In Visual mode, give some info about the selected region. (In this case,
2992 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002993 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 */
2995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002996cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002999 char_u buf1[50];
3000 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003002 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003003 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003004 varnumber_T byte_count_cursor = 0;
3005 varnumber_T char_count = 0;
3006 varnumber_T char_count_cursor = 0;
3007 varnumber_T word_count = 0;
3008 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003009 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003010 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 long line_count_selected = 0;
3012 pos_T min_pos, max_pos;
3013 oparg_T oparg;
3014 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 /*
3017 * Compute the length of the file in characters.
3018 */
3019 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3020 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003021 if (dict == NULL)
3022 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003023 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003024 return;
3025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
3027 else
3028 {
3029 if (get_fileformat(curbuf) == EOL_DOS)
3030 eol_size = 2;
3031 else
3032 eol_size = 1;
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (VIsual_active)
3035 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003036 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
3038 min_pos = VIsual;
3039 max_pos = curwin->w_cursor;
3040 }
3041 else
3042 {
3043 min_pos = curwin->w_cursor;
3044 max_pos = VIsual;
3045 }
3046 if (*p_sel == 'e' && max_pos.col > 0)
3047 --max_pos.col;
3048
3049 if (VIsual_mode == Ctrl_V)
3050 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003051#ifdef FEAT_LINEBREAK
3052 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003053 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003056 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003057 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 oparg.is_VIsual = 1;
3060 oparg.block_mode = TRUE;
3061 oparg.op_type = OP_NOP;
3062 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003063 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003064#ifdef FEAT_LINEBREAK
3065 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003066 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003067#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003068 if (curwin->w_curswant == MAXCOL)
3069 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 if (oparg.end_vcol < oparg.start_vcol)
3072 {
3073 oparg.end_vcol += oparg.start_vcol;
3074 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3075 oparg.end_vcol -= oparg.start_vcol;
3076 }
3077 }
3078 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3082 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003084 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
3086 ui_breakcheck();
3087 if (got_int)
3088 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003089 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003092 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (VIsual_active
3094 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3095 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003096 char_u *s = NULL;
3097 long len = 0L;
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 switch (VIsual_mode)
3100 {
3101 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003105 s = bd.textstart;
3106 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 break;
3108 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003109 s = ml_get(lnum);
3110 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 break;
3112 case 'v':
3113 {
3114 colnr_T start_col = (lnum == min_pos.lnum)
3115 ? min_pos.col : 0;
3116 colnr_T end_col = (lnum == max_pos.lnum)
3117 ? max_pos.col - start_col + 1 : MAXCOL;
3118
Bram Moolenaardef9e822004-12-31 20:58:58 +00003119 s = ml_get(lnum) + start_col;
3120 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122 break;
3123 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003124 if (s != NULL)
3125 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003126 byte_count_cursor += line_count_info(s, &word_count_cursor,
3127 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003128 if (lnum == curbuf->b_ml.ml_line_count
3129 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003130 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003131 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003132 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 }
3135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003137 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (lnum == curwin->w_cursor.lnum)
3139 {
3140 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003141 char_count_cursor += char_count;
3142 byte_count_cursor = byte_count +
3143 line_count_info(ml_get(lnum),
3144 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003145 (varnumber_T)(curwin->w_cursor.col + 1),
3146 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
3148 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003150 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003151 &char_count, (varnumber_T)MAXCOL,
3152 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003156 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003157 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaared767a22016-01-03 22:49:16 +01003159 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003161 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003163 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3164 {
3165 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3166 &max_pos.col);
3167 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3168 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3169 }
3170 else
3171 buf1[0] = NUL;
3172
3173 if (char_count_cursor == byte_count_cursor
3174 && char_count == byte_count)
3175 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003176 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003177 buf1, line_count_selected,
3178 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003179 word_count_cursor,
3180 word_count,
3181 byte_count_cursor,
3182 byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003183 else
3184 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003185 _("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 +01003186 buf1, line_count_selected,
3187 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003188 word_count_cursor,
3189 word_count,
3190 char_count_cursor,
3191 char_count,
3192 byte_count_cursor,
3193 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003196 {
3197 p = ml_get_curline();
3198 validate_virtcol();
3199 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3200 (int)curwin->w_virtcol + 1);
3201 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3202 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaared767a22016-01-03 22:49:16 +01003204 if (char_count_cursor == byte_count_cursor
3205 && char_count == byte_count)
3206 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003207 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003208 (char *)buf1, (char *)buf2,
3209 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003211 word_count_cursor, word_count,
3212 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003213 else
3214 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003215 _("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 +01003216 (char *)buf1, (char *)buf2,
3217 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003218 (long)curbuf->b_ml.ml_line_count,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003219 word_count_cursor, word_count,
3220 char_count_cursor, char_count,
3221 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003222 }
3223 }
3224
Bram Moolenaared767a22016-01-03 22:49:16 +01003225 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003226 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003227 {
3228 size_t len = STRLEN(IObuff);
3229
3230 vim_snprintf((char *)IObuff + len, IOSIZE - len,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003231 _("(+%lld for BOM)"), bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003232 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003233 if (dict == NULL)
3234 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003235 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003236 p = p_shm;
3237 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003238 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003239 p_shm = p;
3240 }
3241 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003242#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003243 if (dict != NULL)
3244 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003245 dict_add_number(dict, "words", word_count);
3246 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003247 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003248 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3249 byte_count_cursor);
3250 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3251 char_count_cursor);
3252 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3253 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003257
3258/*
3259 * Handle indent and format operators and visual mode ":".
3260 */
3261 static void
3262op_colon(oparg_T *oap)
3263{
3264 stuffcharReadbuff(':');
3265 if (oap->is_VIsual)
3266 stuffReadbuff((char_u *)"'<,'>");
3267 else
3268 {
3269 // Make the range look nice, so it can be repeated.
3270 if (oap->start.lnum == curwin->w_cursor.lnum)
3271 stuffcharReadbuff('.');
3272 else
3273 stuffnumReadbuff((long)oap->start.lnum);
3274 if (oap->end.lnum != oap->start.lnum)
3275 {
3276 stuffcharReadbuff(',');
3277 if (oap->end.lnum == curwin->w_cursor.lnum)
3278 stuffcharReadbuff('.');
3279 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3280 stuffcharReadbuff('$');
3281 else if (oap->start.lnum == curwin->w_cursor.lnum)
3282 {
3283 stuffReadbuff((char_u *)".+");
3284 stuffnumReadbuff((long)oap->line_count - 1);
3285 }
3286 else
3287 stuffnumReadbuff((long)oap->end.lnum);
3288 }
3289 }
3290 if (oap->op_type != OP_COLON)
3291 stuffReadbuff((char_u *)"!");
3292 if (oap->op_type == OP_INDENT)
3293 {
3294#ifndef FEAT_CINDENT
3295 if (*get_equalprg() == NUL)
3296 stuffReadbuff((char_u *)"indent");
3297 else
3298#endif
3299 stuffReadbuff(get_equalprg());
3300 stuffReadbuff((char_u *)"\n");
3301 }
3302 else if (oap->op_type == OP_FORMAT)
3303 {
3304 if (*curbuf->b_p_fp != NUL)
3305 stuffReadbuff(curbuf->b_p_fp);
3306 else if (*p_fp != NUL)
3307 stuffReadbuff(p_fp);
3308 else
3309 stuffReadbuff((char_u *)"fmt");
3310 stuffReadbuff((char_u *)"\n']");
3311 }
3312
3313 // do_cmdline() does the rest
3314}
3315
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003316// callback function for 'operatorfunc'
3317static callback_T opfunc_cb;
3318
3319/*
3320 * Process the 'operatorfunc' option value.
3321 * Returns OK or FAIL.
3322 */
3323 int
3324set_operatorfunc_option(void)
3325{
3326 return option_set_callback_func(p_opfunc, &opfunc_cb);
3327}
3328
3329#if defined(EXITFREE) || defined(PROTO)
3330 void
3331free_operatorfunc_option(void)
3332{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003333# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003334 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003335# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003336}
3337#endif
3338
Dominique Pelle748b3082022-01-08 12:41:16 +00003339#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003340/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003341 * Mark the global 'operatorfunc' callback with 'copyID' so that it is not
3342 * garbage collected.
3343 */
3344 int
3345set_ref_in_opfunc(int copyID UNUSED)
3346{
3347 int abort = FALSE;
3348
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003349 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003350
3351 return abort;
3352}
Dominique Pelle748b3082022-01-08 12:41:16 +00003353#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003354
3355/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003356 * Handle the "g@" operator: call 'operatorfunc'.
3357 */
3358 static void
3359op_function(oparg_T *oap UNUSED)
3360{
3361#ifdef FEAT_EVAL
3362 typval_T argv[2];
3363 int save_virtual_op = virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003364 int save_finish_op = finish_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003365 pos_T orig_start = curbuf->b_op_start;
3366 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003367 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003368
3369 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003370 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003371 else
3372 {
3373 // Set '[ and '] marks to text to be operated on.
3374 curbuf->b_op_start = oap->start;
3375 curbuf->b_op_end = oap->end;
3376 if (oap->motion_type != MLINE && !oap->inclusive)
3377 // Exclude the end position.
3378 decl(&curbuf->b_op_end);
3379
3380 argv[0].v_type = VAR_STRING;
3381 if (oap->block_mode)
3382 argv[0].vval.v_string = (char_u *)"block";
3383 else if (oap->motion_type == MLINE)
3384 argv[0].vval.v_string = (char_u *)"line";
3385 else
3386 argv[0].vval.v_string = (char_u *)"char";
3387 argv[1].v_type = VAR_UNKNOWN;
3388
3389 // Reset virtual_op so that 'virtualedit' can be changed in the
3390 // function.
3391 virtual_op = MAYBE;
3392
naohiro ono75c30e92021-10-19 11:15:41 +01003393 // Reset finish_op so that mode() returns the right value.
3394 finish_op = FALSE;
3395
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003396 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3397 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003398
3399 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003400 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003401 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003402 {
3403 curbuf->b_op_start = orig_start;
3404 curbuf->b_op_end = orig_end;
3405 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003406 }
3407#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003408 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003409#endif
3410}
3411
3412/*
3413 * Calculate start/end virtual columns for operating in block mode.
3414 */
3415 static void
3416get_op_vcol(
3417 oparg_T *oap,
3418 colnr_T redo_VIsual_vcol,
3419 int initial) // when TRUE adjust position for 'selectmode'
3420{
3421 colnr_T start, end;
3422
3423 if (VIsual_mode != Ctrl_V
3424 || (!initial && oap->end.col < curwin->w_width))
3425 return;
3426
3427 oap->block_mode = TRUE;
3428
3429 // prevent from moving onto a trail byte
3430 if (has_mbyte)
3431 mb_adjustpos(curwin->w_buffer, &oap->end);
3432
3433 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3434
3435 if (!redo_VIsual_busy)
3436 {
3437 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3438
3439 if (start < oap->start_vcol)
3440 oap->start_vcol = start;
3441 if (end > oap->end_vcol)
3442 {
3443 if (initial && *p_sel == 'e' && start >= 1
3444 && start - 1 >= oap->end_vcol)
3445 oap->end_vcol = start - 1;
3446 else
3447 oap->end_vcol = end;
3448 }
3449 }
3450
3451 // if '$' was used, get oap->end_vcol from longest line
3452 if (curwin->w_curswant == MAXCOL)
3453 {
3454 curwin->w_cursor.col = MAXCOL;
3455 oap->end_vcol = 0;
3456 for (curwin->w_cursor.lnum = oap->start.lnum;
3457 curwin->w_cursor.lnum <= oap->end.lnum;
3458 ++curwin->w_cursor.lnum)
3459 {
3460 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3461 if (end > oap->end_vcol)
3462 oap->end_vcol = end;
3463 }
3464 }
3465 else if (redo_VIsual_busy)
3466 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3467 // Correct oap->end.col and oap->start.col to be the
3468 // upper-left and lower-right corner of the block area.
3469 //
3470 // (Actually, this does convert column positions into character
3471 // positions)
3472 curwin->w_cursor.lnum = oap->end.lnum;
3473 coladvance(oap->end_vcol);
3474 oap->end = curwin->w_cursor;
3475
3476 curwin->w_cursor = oap->start;
3477 coladvance(oap->start_vcol);
3478 oap->start = curwin->w_cursor;
3479}
3480
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003481// Information for redoing the previous Visual selection.
3482typedef struct {
3483 int rv_mode; // 'v', 'V', or Ctrl-V
3484 linenr_T rv_line_count; // number of lines
3485 colnr_T rv_vcol; // number of cols or end column
3486 long rv_count; // count for Visual operator
3487 int rv_arg; // extra argument
3488} redo_VIsual_T;
3489
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003490 static int
3491is_ex_cmdchar(cmdarg_T *cap)
3492{
3493 return cap->cmdchar == ':'
3494 || cap->cmdchar == K_COMMAND
3495 || cap->cmdchar == K_SCRIPT_COMMAND;
3496}
3497
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003498/*
3499 * Handle an operator after Visual mode or when the movement is finished.
3500 * "gui_yank" is true when yanking text for the clipboard.
3501 */
3502 void
3503do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3504{
3505 oparg_T *oap = cap->oap;
3506 pos_T old_cursor;
3507 int empty_region_error;
3508 int restart_edit_save;
3509#ifdef FEAT_LINEBREAK
3510 int lbr_saved = curwin->w_p_lbr;
3511#endif
3512
3513 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003514 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3515
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003516 int include_line_break = FALSE;
3517
3518#if defined(FEAT_CLIPBOARD)
3519 // Yank the visual area into the GUI selection register before we operate
3520 // on it and lose it forever.
3521 // Don't do it if a specific register was specified, so that ""x"*P works.
3522 // This could call do_pending_operator() recursively, but that's OK
3523 // because gui_yank will be TRUE for the nested call.
3524 if ((clip_star.available || clip_plus.available)
3525 && oap->op_type != OP_NOP
3526 && !gui_yank
3527 && VIsual_active
3528 && !redo_VIsual_busy
3529 && oap->regname == 0)
3530 clip_auto_select();
3531#endif
3532 old_cursor = curwin->w_cursor;
3533
3534 // If an operation is pending, handle it...
3535 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3536 {
3537 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3538 // for the clipboard.
3539 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3540
3541#ifdef FEAT_LINEBREAK
3542 // Avoid a problem with unwanted linebreaks in block mode.
3543 if (curwin->w_p_lbr)
3544 curwin->w_valid &= ~VALID_VIRTCOL;
3545 curwin->w_p_lbr = FALSE;
3546#endif
3547 oap->is_VIsual = VIsual_active;
3548 if (oap->motion_force == 'V')
3549 oap->motion_type = MLINE;
3550 else if (oap->motion_force == 'v')
3551 {
3552 // If the motion was linewise, "inclusive" will not have been set.
3553 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3554 if (oap->motion_type == MLINE)
3555 oap->inclusive = FALSE;
3556 // If the motion already was characterwise, toggle "inclusive"
3557 else if (oap->motion_type == MCHAR)
3558 oap->inclusive = !oap->inclusive;
3559 oap->motion_type = MCHAR;
3560 }
3561 else if (oap->motion_force == Ctrl_V)
3562 {
3563 // Change line- or characterwise motion into Visual block mode.
3564 if (!VIsual_active)
3565 {
3566 VIsual_active = TRUE;
3567 VIsual = oap->start;
3568 }
3569 VIsual_mode = Ctrl_V;
3570 VIsual_select = FALSE;
3571 VIsual_reselect = FALSE;
3572 }
3573
3574 // Only redo yank when 'y' flag is in 'cpoptions'.
3575 // Never redo "zf" (define fold).
3576 if ((redo_yank || oap->op_type != OP_YANK)
3577 && ((!VIsual_active || oap->motion_force)
3578 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003579 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003580 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003581 && cap->cmdchar != 'D'
3582#ifdef FEAT_FOLDING
3583 && oap->op_type != OP_FOLD
3584 && oap->op_type != OP_FOLDOPEN
3585 && oap->op_type != OP_FOLDOPENREC
3586 && oap->op_type != OP_FOLDCLOSE
3587 && oap->op_type != OP_FOLDCLOSEREC
3588 && oap->op_type != OP_FOLDDEL
3589 && oap->op_type != OP_FOLDDELREC
3590#endif
3591 )
3592 {
3593 prep_redo(oap->regname, cap->count0,
3594 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3595 oap->motion_force, cap->cmdchar, cap->nchar);
3596 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3597 {
3598 // If 'cpoptions' does not contain 'r', insert the search
3599 // pattern to really repeat the same command.
3600 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3601 AppendToRedobuffLit(cap->searchbuf, -1);
3602 AppendToRedobuff(NL_STR);
3603 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003604 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003605 {
3606 // do_cmdline() has stored the first typed line in
3607 // "repeat_cmdline". When several lines are typed repeating
3608 // won't be possible.
3609 if (repeat_cmdline == NULL)
3610 ResetRedobuff();
3611 else
3612 {
3613 AppendToRedobuffLit(repeat_cmdline, -1);
3614 AppendToRedobuff(NL_STR);
3615 VIM_CLEAR(repeat_cmdline);
3616 }
3617 }
3618 }
3619
3620 if (redo_VIsual_busy)
3621 {
3622 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003623 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003624 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003625 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003626 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3627 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003628 VIsual_mode = redo_VIsual.rv_mode;
3629 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003630 {
3631 if (VIsual_mode == 'v')
3632 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003633 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003634 {
3635 validate_virtcol();
3636 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003637 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003638 }
3639 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003640 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003641 }
3642 else
3643 {
3644 curwin->w_curswant = MAXCOL;
3645 }
3646 coladvance(curwin->w_curswant);
3647 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003648 cap->count0 = redo_VIsual.rv_count;
3649 if (redo_VIsual.rv_count != 0)
3650 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003651 else
3652 cap->count1 = 1;
3653 }
3654 else if (VIsual_active)
3655 {
3656 if (!gui_yank)
3657 {
3658 // Save the current VIsual area for '< and '> marks, and "gv"
3659 curbuf->b_visual.vi_start = VIsual;
3660 curbuf->b_visual.vi_end = curwin->w_cursor;
3661 curbuf->b_visual.vi_mode = VIsual_mode;
3662 restore_visual_mode();
3663 curbuf->b_visual.vi_curswant = curwin->w_curswant;
K.Takata6e1d31e2022-02-03 13:05:32 +00003664#ifdef FEAT_EVAL
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003665 curbuf->b_visual_mode_eval = VIsual_mode;
K.Takata6e1d31e2022-02-03 13:05:32 +00003666#endif
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003667 }
3668
3669 // In Select mode, a linewise selection is operated upon like a
3670 // characterwise selection.
3671 // Special case: gH<Del> deletes the last line.
3672 if (VIsual_select && VIsual_mode == 'V'
3673 && cap->oap->op_type != OP_DELETE)
3674 {
3675 if (LT_POS(VIsual, curwin->w_cursor))
3676 {
3677 VIsual.col = 0;
3678 curwin->w_cursor.col =
3679 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3680 }
3681 else
3682 {
3683 curwin->w_cursor.col = 0;
3684 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3685 }
3686 VIsual_mode = 'v';
3687 }
3688 // If 'selection' is "exclusive", backup one character for
3689 // charwise selections.
3690 else if (VIsual_mode == 'v')
3691 include_line_break = unadjust_for_sel();
3692
3693 oap->start = VIsual;
3694 if (VIsual_mode == 'V')
3695 {
3696 oap->start.col = 0;
3697 oap->start.coladd = 0;
3698 }
3699 }
3700
3701 // Set oap->start to the first position of the operated text, oap->end
3702 // to the end of the operated text. w_cursor is equal to oap->start.
3703 if (LT_POS(oap->start, curwin->w_cursor))
3704 {
3705#ifdef FEAT_FOLDING
3706 // Include folded lines completely.
3707 if (!VIsual_active)
3708 {
3709 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3710 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003711 if ((curwin->w_cursor.col > 0 || oap->inclusive
3712 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003713 && hasFolding(curwin->w_cursor.lnum, NULL,
3714 &curwin->w_cursor.lnum))
3715 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3716 }
3717#endif
3718 oap->end = curwin->w_cursor;
3719 curwin->w_cursor = oap->start;
3720
3721 // w_virtcol may have been updated; if the cursor goes back to its
3722 // previous position w_virtcol becomes invalid and isn't updated
3723 // automatically.
3724 curwin->w_valid &= ~VALID_VIRTCOL;
3725 }
3726 else
3727 {
3728#ifdef FEAT_FOLDING
3729 // Include folded lines completely.
3730 if (!VIsual_active && oap->motion_type == MLINE)
3731 {
3732 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3733 NULL))
3734 curwin->w_cursor.col = 0;
3735 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3736 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3737 }
3738#endif
3739 oap->end = oap->start;
3740 oap->start = curwin->w_cursor;
3741 }
3742
3743 // Just in case lines were deleted that make the position invalid.
3744 check_pos(curwin->w_buffer, &oap->end);
3745 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3746
3747 // Set "virtual_op" before resetting VIsual_active.
3748 virtual_op = virtual_active();
3749
3750 if (VIsual_active || redo_VIsual_busy)
3751 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003752 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003753
3754 if (!redo_VIsual_busy && !gui_yank)
3755 {
3756 // Prepare to reselect and redo Visual: this is based on the
3757 // size of the Visual text
3758 resel_VIsual_mode = VIsual_mode;
3759 if (curwin->w_curswant == MAXCOL)
3760 resel_VIsual_vcol = MAXCOL;
3761 else
3762 {
3763 if (VIsual_mode != Ctrl_V)
3764 getvvcol(curwin, &(oap->end),
3765 NULL, NULL, &oap->end_vcol);
3766 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3767 {
3768 if (VIsual_mode != Ctrl_V)
3769 getvvcol(curwin, &(oap->start),
3770 &oap->start_vcol, NULL, NULL);
3771 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3772 }
3773 else
3774 resel_VIsual_vcol = oap->end_vcol;
3775 }
3776 resel_VIsual_line_count = oap->line_count;
3777 }
3778
3779 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3780 if ((redo_yank || oap->op_type != OP_YANK)
3781 && oap->op_type != OP_COLON
3782#ifdef FEAT_FOLDING
3783 && oap->op_type != OP_FOLD
3784 && oap->op_type != OP_FOLDOPEN
3785 && oap->op_type != OP_FOLDOPENREC
3786 && oap->op_type != OP_FOLDCLOSE
3787 && oap->op_type != OP_FOLDCLOSEREC
3788 && oap->op_type != OP_FOLDDEL
3789 && oap->op_type != OP_FOLDDELREC
3790#endif
3791 && oap->motion_force == NUL
3792 )
3793 {
3794 // Prepare for redoing. Only use the nchar field for "r",
3795 // otherwise it might be the second char of the operator.
3796 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3797 || cap->nchar == 'N'))
3798 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003799 get_op_char(oap->op_type),
3800 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003801 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003802 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003803 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003804 int opchar = get_op_char(oap->op_type);
3805 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003806 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3807
3808 // reverse what nv_replace() did
3809 if (nchar == REPLACE_CR_NCHAR)
3810 nchar = CAR;
3811 else if (nchar == REPLACE_NL_NCHAR)
3812 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003813
3814 if (opchar == 'g' && extra_opchar == '@')
3815 // also repeat the count for 'operatorfunc'
3816 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3817 cap->count0, opchar, extra_opchar, nchar);
3818 else
3819 prep_redo(oap->regname, 0L, NUL, 'v',
3820 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003821 }
3822 if (!redo_VIsual_busy)
3823 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003824 redo_VIsual.rv_mode = resel_VIsual_mode;
3825 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3826 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3827 redo_VIsual.rv_count = cap->count0;
3828 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003829 }
3830 }
3831
3832 // oap->inclusive defaults to TRUE.
3833 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3834 // FALSE. This makes "d}P" and "v}dP" work the same.
3835 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3836 oap->inclusive = TRUE;
3837 if (VIsual_mode == 'V')
3838 oap->motion_type = MLINE;
3839 else
3840 {
3841 oap->motion_type = MCHAR;
3842 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3843 && (include_line_break || !virtual_op))
3844 {
3845 oap->inclusive = FALSE;
3846 // Try to include the newline, unless it's an operator
3847 // that works on lines only.
3848 if (*p_sel != 'o'
3849 && !op_on_lines(oap->op_type)
3850 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3851 {
3852 ++oap->end.lnum;
3853 oap->end.col = 0;
3854 oap->end.coladd = 0;
3855 ++oap->line_count;
3856 }
3857 }
3858 }
3859
3860 redo_VIsual_busy = FALSE;
3861
3862 // Switch Visual off now, so screen updating does
3863 // not show inverted text when the screen is redrawn.
3864 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3865 // no screen redraw, so it is done here to remove the inverted
3866 // part.
3867 if (!gui_yank)
3868 {
3869 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003870 setmouse();
3871 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003872 may_clear_cmdline();
3873 if ((oap->op_type == OP_YANK
3874 || oap->op_type == OP_COLON
3875 || oap->op_type == OP_FUNCTION
3876 || oap->op_type == OP_FILTER)
3877 && oap->motion_force == NUL)
3878 {
3879#ifdef FEAT_LINEBREAK
3880 // make sure redrawing is correct
3881 curwin->w_p_lbr = lbr_saved;
3882#endif
3883 redraw_curbuf_later(INVERTED);
3884 }
3885 }
3886 }
3887
3888 // Include the trailing byte of a multi-byte char.
3889 if (has_mbyte && oap->inclusive)
3890 {
3891 int l;
3892
3893 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3894 if (l > 1)
3895 oap->end.col += l - 1;
3896 }
3897 curwin->w_set_curswant = TRUE;
3898
3899 // oap->empty is set when start and end are the same. The inclusive
3900 // flag affects this too, unless yanking and the end is on a NUL.
3901 oap->empty = (oap->motion_type == MCHAR
3902 && (!oap->inclusive
3903 || (oap->op_type == OP_YANK
3904 && gchar_pos(&oap->end) == NUL))
3905 && EQUAL_POS(oap->start, oap->end)
3906 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3907 // For delete, change and yank, it's an error to operate on an
3908 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3909 empty_region_error = (oap->empty
3910 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3911
3912 // Force a redraw when operating on an empty Visual region, when
3913 // 'modifiable is off or creating a fold.
3914 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3915#ifdef FEAT_FOLDING
3916 || oap->op_type == OP_FOLD
3917#endif
3918 ))
3919 {
3920#ifdef FEAT_LINEBREAK
3921 curwin->w_p_lbr = lbr_saved;
3922#endif
3923 redraw_curbuf_later(INVERTED);
3924 }
3925
3926 // If the end of an operator is in column one while oap->motion_type
3927 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3928 // character in the previous line. If op_start is on or before the
3929 // first non-blank in the line, the operator becomes linewise
3930 // (strange, but that's the way vi does it).
3931 if ( oap->motion_type == MCHAR
3932 && oap->inclusive == FALSE
3933 && !(cap->retval & CA_NO_ADJ_OP_END)
3934 && oap->end.col == 0
3935 && (!oap->is_VIsual || *p_sel == 'o')
3936 && !oap->block_mode
3937 && oap->line_count > 1)
3938 {
3939 oap->end_adjusted = TRUE; // remember that we did this
3940 --oap->line_count;
3941 --oap->end.lnum;
3942 if (inindent(0))
3943 oap->motion_type = MLINE;
3944 else
3945 {
3946 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3947 if (oap->end.col)
3948 {
3949 --oap->end.col;
3950 oap->inclusive = TRUE;
3951 }
3952 }
3953 }
3954 else
3955 oap->end_adjusted = FALSE;
3956
3957 switch (oap->op_type)
3958 {
3959 case OP_LSHIFT:
3960 case OP_RSHIFT:
3961 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3962 auto_format(FALSE, TRUE);
3963 break;
3964
3965 case OP_JOIN_NS:
3966 case OP_JOIN:
3967 if (oap->line_count < 2)
3968 oap->line_count = 2;
3969 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3970 curbuf->b_ml.ml_line_count)
3971 beep_flush();
3972 else
3973 {
3974 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3975 TRUE, TRUE, TRUE);
3976 auto_format(FALSE, TRUE);
3977 }
3978 break;
3979
3980 case OP_DELETE:
3981 VIsual_reselect = FALSE; // don't reselect now
3982 if (empty_region_error)
3983 {
3984 vim_beep(BO_OPER);
3985 CancelRedo();
3986 }
3987 else
3988 {
3989 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01003990 // save cursor line for undo if it wasn't saved yet
3991 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3992 && u_save_cursor() == OK)
3993 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003994 }
3995 break;
3996
3997 case OP_YANK:
3998 if (empty_region_error)
3999 {
4000 if (!gui_yank)
4001 {
4002 vim_beep(BO_OPER);
4003 CancelRedo();
4004 }
4005 }
4006 else
4007 {
4008#ifdef FEAT_LINEBREAK
4009 curwin->w_p_lbr = lbr_saved;
4010#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004011 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004012 (void)op_yank(oap, FALSE, !gui_yank);
4013 }
4014 check_cursor_col();
4015 break;
4016
4017 case OP_CHANGE:
4018 VIsual_reselect = FALSE; // don't reselect now
4019 if (empty_region_error)
4020 {
4021 vim_beep(BO_OPER);
4022 CancelRedo();
4023 }
4024 else
4025 {
4026 // This is a new edit command, not a restart. Need to
4027 // remember it to make 'insertmode' work with mappings for
4028 // Visual mode. But do this only once and not when typed and
4029 // 'insertmode' isn't set.
4030 if (p_im || !KeyTyped)
4031 restart_edit_save = restart_edit;
4032 else
4033 restart_edit_save = 0;
4034 restart_edit = 0;
4035#ifdef FEAT_LINEBREAK
4036 // Restore linebreak, so that when the user edits it looks as
4037 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004038 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004039#endif
4040 // Reset finish_op now, don't want it set inside edit().
4041 finish_op = FALSE;
4042 if (op_change(oap)) // will call edit()
4043 cap->retval |= CA_COMMAND_BUSY;
4044 if (restart_edit == 0)
4045 restart_edit = restart_edit_save;
4046 }
4047 break;
4048
4049 case OP_FILTER:
4050 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4051 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4052 else
4053 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4054 // FALLTHROUGH
4055
4056 case OP_INDENT:
4057 case OP_COLON:
4058
4059#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4060 // If 'equalprg' is empty, do the indenting internally.
4061 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4062 {
4063# ifdef FEAT_LISP
4064 if (curbuf->b_p_lisp)
4065 {
4066 op_reindent(oap, get_lisp_indent);
4067 break;
4068 }
4069# endif
4070# ifdef FEAT_CINDENT
4071 op_reindent(oap,
4072# ifdef FEAT_EVAL
4073 *curbuf->b_p_inde != NUL ? get_expr_indent :
4074# endif
4075 get_c_indent);
4076 break;
4077# endif
4078 }
4079#endif
4080
4081 op_colon(oap);
4082 break;
4083
4084 case OP_TILDE:
4085 case OP_UPPER:
4086 case OP_LOWER:
4087 case OP_ROT13:
4088 if (empty_region_error)
4089 {
4090 vim_beep(BO_OPER);
4091 CancelRedo();
4092 }
4093 else
4094 op_tilde(oap);
4095 check_cursor_col();
4096 break;
4097
4098 case OP_FORMAT:
4099#if defined(FEAT_EVAL)
4100 if (*curbuf->b_p_fex != NUL)
4101 op_formatexpr(oap); // use expression
4102 else
4103#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004104 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004105 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004106 op_colon(oap); // use external command
4107 else
4108 op_format(oap, FALSE); // use internal function
4109 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004110 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004111 case OP_FORMAT2:
4112 op_format(oap, TRUE); // use internal function
4113 break;
4114
4115 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004116 {
4117 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4118
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004119#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004120 // Restore linebreak, so that when the user edits it looks as
4121 // before.
4122 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004123#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004124 // call 'operatorfunc'
4125 op_function(oap);
4126
4127 // Restore the info for redoing Visual mode, the function may
4128 // invoke another operator and unintentionally change it.
4129 redo_VIsual = save_redo_VIsual;
4130 break;
4131 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004132
4133 case OP_INSERT:
4134 case OP_APPEND:
4135 VIsual_reselect = FALSE; // don't reselect now
4136 if (empty_region_error)
4137 {
4138 vim_beep(BO_OPER);
4139 CancelRedo();
4140 }
4141 else
4142 {
4143 // This is a new edit command, not a restart. Need to
4144 // remember it to make 'insertmode' work with mappings for
4145 // Visual mode. But do this only once.
4146 restart_edit_save = restart_edit;
4147 restart_edit = 0;
4148#ifdef FEAT_LINEBREAK
4149 // Restore linebreak, so that when the user edits it looks as
4150 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004151 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004152#endif
4153 op_insert(oap, cap->count1);
4154#ifdef FEAT_LINEBREAK
4155 // Reset linebreak, so that formatting works correctly.
4156 curwin->w_p_lbr = FALSE;
4157#endif
4158
4159 // TODO: when inserting in several lines, should format all
4160 // the lines.
4161 auto_format(FALSE, TRUE);
4162
4163 if (restart_edit == 0)
4164 restart_edit = restart_edit_save;
4165 else
4166 cap->retval |= CA_COMMAND_BUSY;
4167 }
4168 break;
4169
4170 case OP_REPLACE:
4171 VIsual_reselect = FALSE; // don't reselect now
4172 if (empty_region_error)
4173 {
4174 vim_beep(BO_OPER);
4175 CancelRedo();
4176 }
4177 else
4178 {
4179#ifdef FEAT_LINEBREAK
4180 // Restore linebreak, so that when the user edits it looks as
4181 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004182 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004183#endif
4184 op_replace(oap, cap->nchar);
4185 }
4186 break;
4187
4188#ifdef FEAT_FOLDING
4189 case OP_FOLD:
4190 VIsual_reselect = FALSE; // don't reselect now
4191 foldCreate(oap->start.lnum, oap->end.lnum);
4192 break;
4193
4194 case OP_FOLDOPEN:
4195 case OP_FOLDOPENREC:
4196 case OP_FOLDCLOSE:
4197 case OP_FOLDCLOSEREC:
4198 VIsual_reselect = FALSE; // don't reselect now
4199 opFoldRange(oap->start.lnum, oap->end.lnum,
4200 oap->op_type == OP_FOLDOPEN
4201 || oap->op_type == OP_FOLDOPENREC,
4202 oap->op_type == OP_FOLDOPENREC
4203 || oap->op_type == OP_FOLDCLOSEREC,
4204 oap->is_VIsual);
4205 break;
4206
4207 case OP_FOLDDEL:
4208 case OP_FOLDDELREC:
4209 VIsual_reselect = FALSE; // don't reselect now
4210 deleteFold(oap->start.lnum, oap->end.lnum,
4211 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4212 break;
4213#endif
4214 case OP_NR_ADD:
4215 case OP_NR_SUB:
4216 if (empty_region_error)
4217 {
4218 vim_beep(BO_OPER);
4219 CancelRedo();
4220 }
4221 else
4222 {
4223 VIsual_active = TRUE;
4224#ifdef FEAT_LINEBREAK
4225 curwin->w_p_lbr = lbr_saved;
4226#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004227 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004228 VIsual_active = FALSE;
4229 }
4230 check_cursor_col();
4231 break;
4232 default:
4233 clearopbeep(oap);
4234 }
4235 virtual_op = MAYBE;
4236 if (!gui_yank)
4237 {
4238 // if 'sol' not set, go back to old column for some commands
4239 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4240 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4241 || oap->op_type == OP_DELETE))
4242 {
4243#ifdef FEAT_LINEBREAK
4244 curwin->w_p_lbr = FALSE;
4245#endif
4246 coladvance(curwin->w_curswant = old_col);
4247 }
4248 }
4249 else
4250 {
4251 curwin->w_cursor = old_cursor;
4252 }
4253 oap->block_mode = FALSE;
4254 clearop(oap);
4255 motion_force = NUL;
4256 }
4257#ifdef FEAT_LINEBREAK
4258 curwin->w_p_lbr = lbr_saved;
4259#endif
4260}