blob: a9968024901ec1dc8ff2e5cf7eeaeb7c2e1edb0b [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;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int i = 0, j = 0;
290 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifdef FEAT_RIGHTLEFT
292 int old_p_ri = p_ri;
293
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100294 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100297 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
299 if (bd.is_short)
300 return;
301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100302 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 total = (int)((unsigned)amount * (unsigned)sw_val);
304 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100305 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 oldp = ml_get_curline();
308
309 if (!left)
310 {
311 /*
312 * 1. Get start vcol
313 * 2. Total ws vcols
314 * 3. Divvy into TABs & spp
315 * 4. Construct new string
316 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100317 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 ws_vcol = bd.start_vcol - bd.pre_whitesp;
319 if (bd.startspaces)
320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100322 {
323 if ((*mb_ptr2len)(bd.textstart) == 1)
324 ++bd.textstart;
325 else
326 {
327 ws_vcol = 0;
328 bd.startspaces = 0;
329 }
330 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000331 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000332 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100334 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100336 // TODO: is passing bd.textstart for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200337 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
338 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 total += incr;
340 bd.start_vcol += incr;
341 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100342 // OK, now total=all the VWS reqd, and textstart points at the 1st
343 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200344#ifdef FEAT_VARTABS
345 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200346 tabstop_fromto(ws_vcol, ws_vcol + total,
347 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348 else
349 j = total;
350#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 if (!curbuf->b_p_et)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100352 i = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (i)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100354 j = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 else
356 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100358 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
360 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200361 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (newp == NULL)
363 return;
364 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
365 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200366 vim_memset(newp + bd.textcol, TAB, (size_t)i);
367 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100368 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
370 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100371 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100373 colnr_T destination_col; // column to which text in block will
374 // be shifted
375 char_u *verbatim_copy_end; // end of the part of the line which is
376 // copied verbatim
377 colnr_T verbatim_copy_width;// the (displayed) width of this part
378 // of line
379 unsigned fill; // nr of spaces that replace a TAB
380 unsigned new_line_len; // the length of the line after the
381 // block shift
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
452 + (unsigned)STRLEN(non_white) + 1;
453
Bram Moolenaar964b3742019-05-24 18:54:09 +0200454 newp = alloc(new_line_len);
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);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000459 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100461 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
463 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
464 State = oldstate;
465 curwin->w_cursor.col = oldcol;
466#ifdef FEAT_RIGHTLEFT
467 p_ri = old_p_ri;
468#endif
469}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * Insert string "s" (b_insert ? before : after) block :AKelly
473 * Caller must prepare for undo.
474 */
475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100476block_insert(
477 oparg_T *oap,
478 char_u *s,
479 int b_insert,
480 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaardac13472019-09-16 21:06:21 +0200482 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100483 int count = 0; // extra spaces to replace a cut TAB
484 int spaces = 0; // non-zero if cutting a TAB
485 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200486 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 unsigned s_len; // STRLEN(s)
488 char_u *newp, *oldp; // new, old lines
489 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int oldstate = State;
491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100492 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 s_len = (unsigned)STRLEN(s);
494
495 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
496 {
497 block_prep(oap, bdp, lnum, TRUE);
498 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100499 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
501 oldp = ml_get(lnum);
502
503 if (b_insert)
504 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200505 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 spaces = bdp->startspaces;
507 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 offset = bdp->textcol;
510 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100511 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200513 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100514 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200516 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100518 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 offset = bdp->textcol + bdp->textlen - (spaces != 0);
520 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100523 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 if (!bdp->is_MAX)
525 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
526 count = spaces;
527 offset = bdp->textcol + bdp->textlen;
528 }
529 }
530
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200531 if (has_mbyte && spaces > 0)
532 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100533 int off;
534
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100535 // Avoid starting halfway a multi-byte character.
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200536 if (b_insert)
537 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100538 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000539 spaces -= off;
540 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200541 }
542 else
543 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000544 // spaces fill the gap, the character that's at the edge moves
545 // right
546 off = (*mb_head_off)(oldp, oldp + offset);
547 offset -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200548 }
549 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200550 if (spaces < 0) // can happen when the cursor was moved
551 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200552
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000553 // Make sure the allocated size matches what is actually copied below.
554 newp = alloc(STRLEN(oldp) + spaces + s_len
555 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
556 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (newp == NULL)
558 continue;
559
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100560 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000561 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 oldp += offset;
563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100564 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200565 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200566 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100568 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200569 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 offset += s_len;
571
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000572 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000574 if (*oldp == TAB)
575 {
576 // insert post-padding
577 vim_memset(newp + offset + spaces, ' ',
578 (size_t)(ts_val - spaces));
579 // we're splitting a TAB, don't copy it
580 oldp++;
581 // We allowed for that TAB, remember this now
582 count++;
583 }
584 else
585 // Not a TAB, no extra spaces
586 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588
589 if (spaces > 0)
590 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000591 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ml_replace(lnum, newp, FALSE);
594
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200595 if (b_insert)
596 // correct any text properties
597 inserted_bytes(lnum, startcol, s_len);
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (lnum == oap->end.lnum)
600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100601 // Set "']" mark to the end of the block instead of the end of
602 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 curbuf->b_op_end.lnum = oap->end.lnum;
604 curbuf->b_op_end.col = offset;
605 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100606 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
609
610 State = oldstate;
611}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200614 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200616 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
618 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int n;
622 linenr_T lnum;
623 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 char_u *newp, *oldp;
625 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
627 int did_yank = FALSE;
628
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100629 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 return OK;
631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100632 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (oap->empty)
634 return u_save_cursor();
635
636 if (!curbuf->b_p_ma)
637 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200638 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 return FAIL;
640 }
641
642#ifdef FEAT_CLIPBOARD
643 adjust_clip_reg(&oap->regname);
644#endif
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if (has_mbyte)
647 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaard04b7502010-07-08 22:27:55 +0200649 /*
650 * Imitate the strange Vi behaviour: If the delete spans more than one
651 * line and motion_type == MCHAR and the result is a blank line, make the
652 * delete linewise. Don't do this for the change command or Visual mode.
653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000656 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100658 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 && oap->op_type == OP_DELETE)
660 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200661 ptr = ml_get(oap->end.lnum) + oap->end.col;
662 if (*ptr != NUL)
663 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 ptr = skipwhite(ptr);
665 if (*ptr == NUL && inindent(0))
666 oap->motion_type = MLINE;
667 }
668
Bram Moolenaard04b7502010-07-08 22:27:55 +0200669 /*
670 * Check for trying to delete (e.g. "D") in an empty line.
671 * Note: For the change operator it is ok.
672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if ( oap->motion_type == MCHAR
674 && oap->line_count == 1
675 && oap->op_type == OP_DELETE
676 && *ml_get(oap->start.lnum) == NUL)
677 {
678 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000679 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * 'cpoptions' (Vi compatible).
681 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000682 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
684 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000685 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
687 beep_flush();
688 return OK;
689 }
690
Bram Moolenaard04b7502010-07-08 22:27:55 +0200691 /*
692 * Do a yank of whatever we're about to delete.
693 * If a yank register was specified, put the deleted text into that
694 * register. For the black hole register '_' don't yank anything.
695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (oap->regname != '_')
697 {
698 if (oap->regname != 0)
699 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100700 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 if (!valid_yank_reg(oap->regname, TRUE))
702 {
703 beep_flush();
704 return OK;
705 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100706 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
707 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 did_yank = TRUE;
709 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200710 else
711 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712
713 /*
714 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100715 * delete contains a line break, or when using a specific operator (Vi
716 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200717 * Use the register name from before adjust_clip_reg() may have
718 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100720 if (oap->motion_type == MLINE || oap->line_count > 1
721 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100723 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (op_yank(oap, TRUE, FALSE) == OK)
725 did_yank = TRUE;
726 }
727
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100728 // Yank into small delete register when no named register specified
729 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200730 if ((
731#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200732 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
733 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200734#endif
735 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 && oap->line_count == 1)
737 {
738 oap->regname = '-';
739 get_yank_register(oap->regname, TRUE);
740 if (op_yank(oap, TRUE, FALSE) == OK)
741 did_yank = TRUE;
742 oap->regname = 0;
743 }
744
745 /*
746 * If there's too much stuff to fit in the yank register, then get a
747 * confirmation before doing the delete. This is crude, but simple.
748 * And it avoids doing a delete of something we can't put back if we
749 * want.
750 */
751 if (!did_yank)
752 {
753 int msg_silent_save = msg_silent;
754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100755 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
757 msg_silent = msg_silent_save;
758 if (n != 'y')
759 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000760 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 return FAIL;
762 }
763 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100764
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100765#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100766 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200767 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 }
770
Bram Moolenaard04b7502010-07-08 22:27:55 +0200771 /*
772 * block mode delete
773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (oap->block_mode)
775 {
776 if (u_save((linenr_T)(oap->start.lnum - 1),
777 (linenr_T)(oap->end.lnum + 1)) == FAIL)
778 return FAIL;
779
780 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
781 {
782 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 continue;
785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100786 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (lnum == curwin->w_cursor.lnum)
788 {
789 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
792
Bram Moolenaar8055d172019-05-17 22:57:26 +0200793 // "n" == number of chars deleted
794 // If we delete a TAB, it may be replaced by several characters.
795 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 n = bd.textlen - bd.startspaces - bd.endspaces;
797 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200798 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (newp == NULL)
800 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100803 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200804 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100806 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000808 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200811
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100812#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200813 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200814 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817
818 check_cursor_col();
819 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
820 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100821 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100823 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
825 if (oap->op_type == OP_CHANGE)
826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // Delete the lines except the first one. Temporarily move the
828 // cursor to the next line. Save the current line number, if the
829 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (oap->line_count > 1)
831 {
832 lnum = curwin->w_cursor.lnum;
833 ++curwin->w_cursor.lnum;
834 del_lines((long)(oap->line_count - 1), TRUE);
835 curwin->w_cursor.lnum = lnum;
836 }
837 if (u_save_cursor() == FAIL)
838 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100839 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 beginline(BL_WHITE); // cursor on first non-white
842 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 ai_col = curwin->w_cursor.col;
844 }
845 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 beginline(0); // cursor in column 0
847 truncate_line(FALSE); // delete the rest of the line
848 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100850 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 else
853 {
854 del_lines(oap->line_count, TRUE);
855 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858 }
859 else
860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (virtual_op)
862 {
863 int endcol = 0;
864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (gchar_pos(&oap->start) == '\t')
867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100868 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 return FAIL;
870 if (oap->line_count == 1)
871 endcol = getviscol2(oap->end.col, oap->end.coladd);
872 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
873 oap->start = curwin->w_cursor;
874 if (oap->line_count == 1)
875 {
876 coladvance(endcol);
877 oap->end.col = curwin->w_cursor.col;
878 oap->end.coladd = curwin->w_cursor.coladd;
879 curwin->w_cursor = oap->start;
880 }
881 }
882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (gchar_pos(&oap->end) == '\t'
885 && (int)oap->end.coladd < oap->inclusive)
886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100887 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (u_save((linenr_T)(oap->end.lnum - 1),
889 (linenr_T)(oap->end.lnum + 1)) == FAIL)
890 return FAIL;
891 curwin->w_cursor = oap->end;
892 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
893 oap->end = curwin->w_cursor;
894 curwin->w_cursor = oap->start;
895 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100896 if (has_mbyte)
897 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100900 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 return FAIL;
904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100905 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100906 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 && oap->op_type == OP_CHANGE
908 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100909 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 display_dollar(oap->end.col - !oap->inclusive);
911
912 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
913
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (virtual_op)
915 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 // fix up things for virtualedit-delete:
917 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 char_u *curline = ml_get_curline();
919 int len = (int)STRLEN(curline);
920
921 if (oap->end.coladd != 0
922 && (int)oap->end.col >= len - 1
923 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
924 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100925 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (n == 0 && oap->start.coladd != oap->end.coladd)
927 n = 1;
928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100929 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (gchar_cursor() != NUL)
931 curwin->w_cursor.coladd = 0;
932 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200933 (void)del_bytes((long)n, !virtual_op,
934 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100936 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 pos_T curpos;
939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100940 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
942 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
943 return FAIL;
944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100945 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 ++curwin->w_cursor.lnum;
949 del_lines((long)(oap->line_count - 2), FALSE);
950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100951 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200952 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200953 curwin->w_cursor.col = 0;
954 (void)del_bytes((long)n, !virtual_op,
955 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100956 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200957 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200959 if (oap->op_type == OP_DELETE)
960 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
962
963 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
964
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000965setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200966 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100968 if (oap->block_mode)
969 {
970 curbuf->b_op_end.lnum = oap->end.lnum;
971 curbuf->b_op_end.col = oap->start.col;
972 }
973 else
974 curbuf->b_op_end = oap->start;
975 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 return OK;
979}
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981/*
982 * Adjust end of operating area for ending on a multi-byte character.
983 * Used for deletion.
984 */
985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100986mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988 char_u *p;
989
990 if (oap->inclusive)
991 {
992 p = ml_get(oap->end.lnum);
993 oap->end.col += mb_tail_off(p, p + oap->end.col);
994 }
995}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
Bram Moolenaar630afe82018-06-28 19:26:28 +0200997/*
998 * Replace the character under the cursor with "c".
999 * This takes care of multi-byte characters.
1000 */
1001 static void
1002replace_character(int c)
1003{
1004 int n = State;
1005
1006 State = REPLACE;
1007 ins_char(c);
1008 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001009 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001010 dec_cursor();
1011}
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013/*
1014 * Replace a whole area with one character.
1015 */
1016 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001017op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
1019 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 char_u *newp, *oldp;
1022 size_t oldlen;
1023 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001024 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001025 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001028 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001030 if (c == REPLACE_CR_NCHAR)
1031 {
1032 had_ctrl_v_cr = TRUE;
1033 c = CAR;
1034 }
1035 else if (c == REPLACE_NL_NCHAR)
1036 {
1037 had_ctrl_v_cr = TRUE;
1038 c = NL;
1039 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (has_mbyte)
1042 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044 if (u_save((linenr_T)(oap->start.lnum - 1),
1045 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1046 return FAIL;
1047
1048 /*
1049 * block mode replace
1050 */
1051 if (oap->block_mode)
1052 {
1053 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1054 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1055 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001056 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1058 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001059 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001061 // n == number of extra chars required
1062 // If we split a TAB, it may be replaced by several characters.
1063 // Thus the number of characters may increase!
1064 // If the range starts in virtual space, count the initial
1065 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1067 {
1068 pos_T vpos;
1069
Bram Moolenaara1381de2009-11-03 15:44:21 +00001070 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 getvpos(&vpos, oap->start_vcol);
1072 bd.startspaces += vpos.coladd;
1073 n = bd.startspaces;
1074 }
1075 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001076 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001079 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001083 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 numc = oap->end_vcol - oap->start_vcol + 1;
1085 if (bd.is_short && (!virtual_op || bd.is_MAX))
1086 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001088 // A double-wide character can be replaced only up to half the
1089 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if ((*mb_char2cells)(c) > 1)
1091 {
1092 if ((numc & 1) && !bd.is_short)
1093 {
1094 ++bd.endspaces;
1095 ++n;
1096 }
1097 numc = numc / 2;
1098 }
1099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 num_chars = numc;
1102 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001103 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 n += numc - bd.textlen;
1105
1106 oldp = ml_get_curline();
1107 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001108 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (newp == NULL)
1110 continue;
1111 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001112 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 mch_memmove(newp, oldp, (size_t)bd.textcol);
1114 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001115 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001116 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001117 // insert replacement chars CHECK FOR ALLOCATED SPACE
1118 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1119 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001120 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001122 if (has_mbyte)
1123 {
1124 n = (int)STRLEN(newp);
1125 while (--num_chars >= 0)
1126 n += (*mb_char2bytes)(c, newp + n);
1127 }
1128 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001129 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001130 if (!bd.is_short)
1131 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001132 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001133 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001134 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001135 STRMOVE(newp + STRLEN(newp), oldp);
1136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
1138 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001140 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001141 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001142 if (after_p != NULL)
1143 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001145 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001147 if (after_p != NULL)
1148 {
1149 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1150 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1151 oap->end.lnum++;
1152 vim_free(after_p);
1153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 }
1156 else
1157 {
1158 /*
1159 * MCHAR and MLINE motion replace.
1160 */
1161 if (oap->motion_type == MLINE)
1162 {
1163 oap->start.col = 0;
1164 curwin->w_cursor.col = 0;
1165 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1166 if (oap->end.col)
1167 --oap->end.col;
1168 }
1169 else if (!oap->inclusive)
1170 dec(&(oap->end));
1171
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001172 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 n = gchar_cursor();
1175 if (n != NUL)
1176 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001177 int new_byte_len = (*mb_char2len)(c);
1178 int old_byte_len = mb_ptr2len(ml_get_cursor());
1179
1180 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001182 // This is slow, but it handles replacing a single-byte
1183 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001184 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001185 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001186 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 }
1188 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (n == TAB)
1191 {
1192 int end_vcol = 0;
1193
1194 if (curwin->w_cursor.lnum == oap->end.lnum)
1195 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001196 // oap->end has to be recalculated when
1197 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 end_vcol = getviscol2(oap->end.col,
1199 oap->end.coladd);
1200 }
1201 coladvance_force(getviscol());
1202 if (curwin->w_cursor.lnum == oap->end.lnum)
1203 getvpos(&oap->end, end_vcol);
1204 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001205 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1209 {
1210 int virtcols = oap->end.coladd;
1211
1212 if (curwin->w_cursor.lnum == oap->start.lnum
1213 && oap->start.col == oap->end.col && oap->start.coladd)
1214 virtcols -= oap->start.coladd;
1215
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001216 // oap->end has been trimmed so it's effectively inclusive;
1217 // as a result an extra +1 must be counted so we don't
1218 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1220 curwin->w_cursor.col -= (virtcols + 1);
1221 for (; virtcols >= 0; virtcols--)
1222 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001223 if ((*mb_char2len)(c) > 1)
1224 replace_character(c);
1225 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001226 PBYTE(curwin->w_cursor, c);
1227 if (inc(&curwin->w_cursor) == -1)
1228 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001232 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (inc_cursor() == -1)
1234 break;
1235 }
1236 }
1237
1238 curwin->w_cursor = oap->start;
1239 check_cursor();
1240 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1241
Bram Moolenaare1004402020-10-24 20:49:43 +02001242 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001243 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001244 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001245 curbuf->b_op_start = oap->start;
1246 curbuf->b_op_end = oap->end;
1247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
1249 return OK;
1250}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001252static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254/*
1255 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1256 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001258op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
1260 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001262 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 if (u_save((linenr_T)(oap->start.lnum - 1),
1265 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1266 return;
1267
1268 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001269 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
1271 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1272 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001273 int one_change;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 block_prep(oap, &bd, pos.lnum, FALSE);
1276 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001277 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1278 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001279
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001280#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001281 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
1283 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1284
Bram Moolenaar009b2592004-10-24 19:18:58 +00001285 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1286 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001288 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 if (did_change)
1293 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1294 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001295 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
1297 if (oap->motion_type == MLINE)
1298 {
1299 oap->start.col = 0;
1300 pos.col = 0;
1301 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1302 if (oap->end.col)
1303 --oap->end.col;
1304 }
1305 else if (!oap->inclusive)
1306 dec(&(oap->end));
1307
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001308 if (pos.lnum == oap->end.lnum)
1309 did_change = swapchars(oap->op_type, &pos,
1310 oap->end.col - pos.col + 1);
1311 else
1312 for (;;)
1313 {
1314 did_change |= swapchars(oap->op_type, &pos,
1315 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1316 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001317 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001318 break;
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (did_change)
1321 {
1322 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1323 0L);
1324#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001325 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
1327 char_u *ptr;
1328 int count;
1329
1330 pos = oap->start;
1331 while (pos.lnum < oap->end.lnum)
1332 {
1333 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001334 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001335 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001337 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 pos.col = 0;
1339 pos.lnum++;
1340 }
1341 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1342 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001343 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001345 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
1347#endif
1348 }
1349 }
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001352 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaare1004402020-10-24 20:49:43 +02001355 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001356 {
1357 // Set '[ and '] marks.
1358 curbuf->b_op_start = oap->start;
1359 curbuf->b_op_end = oap->end;
1360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001363 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001364 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365}
1366
1367/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001368 * Invoke swapchar() on "length" bytes at position "pos".
1369 * "pos" is advanced to just after the changed characters.
1370 * "length" is rounded up to include the whole last multi-byte character.
1371 * Also works correctly when the number of bytes changes.
1372 * Returns TRUE if some character was changed.
1373 */
1374 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001375swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001376{
1377 int todo;
1378 int did_change = 0;
1379
1380 for (todo = length; todo > 0; --todo)
1381 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001382 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001383 {
1384 int len = (*mb_ptr2len)(ml_get_pos(pos));
1385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001386 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001387 if (len > 0)
1388 todo -= len - 1;
1389 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001390 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001391 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001392 break;
1393 }
1394 return did_change;
1395}
1396
1397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * If op_type == OP_UPPER: make uppercase,
1399 * if op_type == OP_LOWER: make lowercase,
1400 * if op_type == OP_ROT13: do rot13 encoding,
1401 * else swap case of character at 'pos'
1402 * returns TRUE when something actually changed.
1403 */
1404 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001405swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
1407 int c;
1408 int nc;
1409
1410 c = gchar_pos(pos);
1411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001412 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (c >= 0x80 && op_type == OP_ROT13)
1414 return FALSE;
1415
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001416 if (op_type == OP_UPPER && c == 0xdf
1417 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001418 {
1419 pos_T sp = curwin->w_cursor;
1420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001421 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001422 curwin->w_cursor = *pos;
1423 del_char(FALSE);
1424 ins_char('S');
1425 ins_char('S');
1426 curwin->w_cursor = sp;
1427 inc(pos);
1428 }
1429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001430 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 nc = c;
1433 if (MB_ISLOWER(c))
1434 {
1435 if (op_type == OP_ROT13)
1436 nc = ROT13(c, 'a');
1437 else if (op_type != OP_LOWER)
1438 nc = MB_TOUPPER(c);
1439 }
1440 else if (MB_ISUPPER(c))
1441 {
1442 if (op_type == OP_ROT13)
1443 nc = ROT13(c, 'A');
1444 else if (op_type != OP_UPPER)
1445 nc = MB_TOLOWER(c);
1446 }
1447 if (nc != c)
1448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1450 {
1451 pos_T sp = curwin->w_cursor;
1452
1453 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001454 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001455 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 ins_char(nc);
1457 curwin->w_cursor = sp;
1458 }
1459 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001460 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 return TRUE;
1462 }
1463 return FALSE;
1464}
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466/*
1467 * op_insert - Insert and append operators for Visual mode.
1468 */
1469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001470op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 long ins_len, pre_textlen = 0;
1473 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001474 colnr_T ind_pre_col = 0, ind_post_col;
1475 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 struct block_def bd;
1477 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001478 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001479 pos_T start_insert;
1480 // offset when cursor was moved in insert mode
1481 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001483 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001486 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 curwin->w_cursor.lnum = oap->start.lnum;
1488 update_screen(INVERTED);
1489
1490 if (oap->block_mode)
1491 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001492 // When 'virtualedit' is used, need to insert the extra spaces before
1493 // doing block_prep(). When only "block" is used, virtual edit is
1494 // already disabled, but still need it when calling
1495 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001496 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001497 // state for the current window. To override that state, we need to
1498 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (curwin->w_cursor.coladd > 0)
1500 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001501 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (u_save_cursor() == FAIL)
1504 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001505
Gary Johnson51ad8502021-08-03 18:33:08 +02001506 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 coladvance_force(oap->op_type == OP_APPEND
1508 ? oap->end_vcol + 1 : getviscol());
1509 if (oap->op_type == OP_APPEND)
1510 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001511 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001513 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001515 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001516 ind_pre_col = (colnr_T)getwhitecols_curline();
1517 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (oap->op_type == OP_APPEND)
1521 firstline += bd.textlen;
1522 pre_textlen = (long)STRLEN(firstline);
1523 }
1524
1525 if (oap->op_type == OP_APPEND)
1526 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001527 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 curwin->w_set_curswant = TRUE;
1531 while (*ml_get_cursor() != NUL
1532 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1533 ++curwin->w_cursor.col;
1534 if (bd.is_short && !bd.is_MAX)
1535 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001536 // First line was too short, make it longer and adjust the
1537 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (u_save_cursor() == FAIL)
1539 return;
1540 for (i = 0; i < bd.endspaces; ++i)
1541 ins_char(' ');
1542 bd.textlen += bd.endspaces;
1543 }
1544 }
1545 else
1546 {
1547 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001548 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001550 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001551 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 && oap->start_vcol != oap->end_vcol)
1553 inc_cursor();
1554 }
1555 }
1556
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001557 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001558 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001559 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001561 // When a tab was inserted, and the characters in front of the tab
1562 // have been converted to a tab as well, the column of the cursor
1563 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001564 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001565 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001566 oap->start = curbuf->b_op_start_orig;
1567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001568 // If user has moved off this line, we don't know what to do, so do
1569 // nothing.
1570 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001571 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 return;
1573
1574 if (oap->block_mode)
1575 {
1576 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001577 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001578 size_t len;
1579 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581 // If indent kicked in, the firstline might have changed
1582 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001583 ind_post_col = (colnr_T)getwhitecols_curline();
1584 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001585 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001586 bd.textcol += ind_post_col - ind_pre_col;
1587 ind_post_vcol = get_indent();
1588 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001589 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001590 }
1591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001592 // The user may have moved the cursor before inserting something, try
1593 // to adjust the block for that. But only do it, if the difference
1594 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001595 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1596 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001597 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001598 int t = getviscol2(curbuf->b_op_start_orig.col,
1599 curbuf->b_op_start_orig.coladd);
1600
1601 if (!bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001602 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001603 if (oap->op_type == OP_INSERT
1604 && oap->start.col + oap->start.coladd
1605 != curbuf->b_op_start_orig.col
1606 + curbuf->b_op_start_orig.coladd)
1607 {
1608 oap->start.col = curbuf->b_op_start_orig.col;
1609 pre_textlen -= t - oap->start_vcol;
1610 oap->start_vcol = t;
1611 }
1612 else if (oap->op_type == OP_APPEND
Bram Moolenaar9f8c3042022-01-17 17:30:21 +00001613 && oap->start.col + oap->start.coladd
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001614 >= curbuf->b_op_start_orig.col
1615 + curbuf->b_op_start_orig.coladd)
1616 {
1617 oap->start.col = curbuf->b_op_start_orig.col;
1618 // reset pre_textlen to the value of OP_INSERT
1619 pre_textlen += bd.textlen;
1620 pre_textlen -= t - oap->start_vcol;
1621 oap->start_vcol = t;
1622 oap->op_type = OP_INSERT;
1623 }
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001624 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001625 else if (bd.is_MAX && oap->op_type == OP_APPEND)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001628 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001629 pre_textlen -= t - oap->start_vcol;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001630 }
1631 }
1632
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001633 // Spaces and tabs in the indent may have changed to other spaces and
1634 // tabs. Get the starting column again and correct the length.
1635 // Don't do this when "$" used, end-of-line will have changed.
1636 //
1637 // if indent was added and the inserted text was after the indent,
1638 // correct the selection for the new indent.
1639 if (did_indent && bd.textcol - ind_post_col > 0)
1640 {
1641 oap->start.col += ind_post_col - ind_pre_col;
1642 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1643 oap->end.col += ind_post_col - ind_pre_col;
1644 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001647 if (did_indent && bd.textcol - ind_post_col > 0)
1648 {
1649 // undo for where "oap" is used below
1650 oap->start.col -= ind_post_col - ind_pre_col;
1651 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1652 oap->end.col -= ind_post_col - ind_pre_col;
1653 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1656 {
1657 if (oap->op_type == OP_APPEND)
1658 {
1659 pre_textlen += bd2.textlen - bd.textlen;
1660 if (bd2.endspaces)
1661 --bd2.textlen;
1662 }
1663 bd.textcol = bd2.textcol;
1664 bd.textlen = bd2.textlen;
1665 }
1666
1667 /*
1668 * Subsequent calls to ml_get() flush the firstline data - take a
1669 * copy of the required string.
1670 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001671 firstline = ml_get(oap->start.lnum);
1672 len = STRLEN(firstline);
1673 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001675 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001676 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001677 // account for pressing cursor in insert mode when '$' was used
1678 if (bd.is_MAX
1679 && (start_insert.lnum == Insstart.lnum
1680 && start_insert.col > Insstart.col))
1681 {
1682 offset = (start_insert.col - Insstart.col);
1683 add -= offset;
1684 if (oap->end_vcol > offset)
1685 oap->end_vcol -= (offset + 1);
1686 else
1687 // moved outside of the visual block, what to do?
1688 return;
1689 }
1690 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001691 if ((size_t)add > len)
1692 firstline += len; // short line, point to the NUL
1693 else
1694 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001695 if (pre_textlen >= 0 && (ins_len =
1696 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001698 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if (ins_text != NULL)
1700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001701 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if (u_save(oap->start.lnum,
1703 (linenr_T)(oap->end.lnum + 1)) == OK)
1704 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1705 &bd);
1706
1707 curwin->w_cursor.col = oap->start.col;
1708 check_cursor();
1709 vim_free(ins_text);
1710 }
1711 }
1712 }
1713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715/*
1716 * op_change - handle a change operation
1717 *
1718 * return TRUE if edit() returns because of a CTRL-O command
1719 */
1720 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001721op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 colnr_T l;
1724 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 long offset;
1726 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001727 long ins_len;
1728 long pre_textlen = 0;
1729 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 char_u *firstline;
1731 char_u *ins_text, *newp, *oldp;
1732 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
1734 l = oap->start.col;
1735 if (oap->motion_type == MLINE)
1736 {
1737 l = 0;
1738#ifdef FEAT_SMARTINDENT
1739 if (!p_paste && curbuf->b_p_si
1740# ifdef FEAT_CINDENT
1741 && !curbuf->b_p_cin
1742# endif
1743 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001744 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746 }
1747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001748 // First delete the text in the region. In an empty buffer only need to
1749 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1751 {
1752 if (u_save_cursor() == FAIL)
1753 return FALSE;
1754 }
1755 else if (op_delete(oap) == FAIL)
1756 return FALSE;
1757
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001758 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 && !virtual_op)
1760 inc_cursor();
1761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001762 // check for still on same line (<CR> in inserted text meaningless)
1763 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (oap->block_mode)
1765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001766 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (virtual_op && (curwin->w_cursor.coladd > 0
1768 || gchar_cursor() == NUL))
1769 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001770 firstline = ml_get(oap->start.lnum);
1771 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001772 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 bd.textcol = curwin->w_cursor.col;
1774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
1776#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1777 if (oap->motion_type == MLINE)
1778 fix_indent();
1779#endif
1780
1781 retval = edit(NUL, FALSE, (linenr_T)1);
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001784 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001786 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001788 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001790 // Auto-indenting may have changed the indent. If the cursor was past
1791 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001793 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001795 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001796
1797 pre_textlen += new_indent - pre_indent;
1798 bd.textcol += new_indent - pre_indent;
1799 }
1800
1801 ins_len = (long)STRLEN(firstline) - pre_textlen;
1802 if (ins_len > 0)
1803 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001804 // Subsequent calls to ml_get() flush the firstline data - take a
1805 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001806 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001808 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1810 linenr++)
1811 {
1812 block_prep(oap, &bd, linenr, TRUE);
1813 if (!bd.is_short || virtual_op)
1814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 pos_T vpos;
1816
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001817 // If the block starts in virtual space, count the
1818 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (bd.is_short)
1820 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001821 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824 else
1825 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001827 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (newp == NULL)
1829 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 mch_memmove(newp, oldp, (size_t)bd.textcol);
1832 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001833 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1836 offset += ins_len;
1837 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001838 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 ml_replace(linenr, newp, FALSE);
1840 }
1841 }
1842 check_cursor();
1843
1844 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1845 }
1846 vim_free(ins_text);
1847 }
1848 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001849 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 return retval;
1852}
1853
1854/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001855 * When the cursor is on the NUL past the end of the line and it should not be
1856 * there move it left.
1857 */
1858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001859adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001860{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001861 unsigned int cur_ve_flags = get_ve_flags();
1862
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001863 if (curwin->w_cursor.col > 0
1864 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001865 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 && !(restart_edit || (State & INSERT)))
1867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001868 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001869 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001870
Gary Johnson53ba05b2021-07-26 22:19:10 +02001871 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001872 {
1873 colnr_T scol, ecol;
1874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001875 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001876 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1877 curwin->w_cursor.coladd = ecol - scol + 1;
1878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880}
1881
Bram Moolenaar81340392012-06-06 16:12:59 +02001882/*
1883 * If "process" is TRUE and the line begins with a comment leader (possibly
1884 * after some white space), return a pointer to the text after it. Put a boolean
1885 * value indicating whether the line ends with an unclosed comment in
1886 * "is_comment".
1887 * line - line to be processed,
1888 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001889 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001890 * include_space - whether to also skip space following the comment leader,
1891 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001892 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001893 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001894 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001895skip_comment(
1896 char_u *line,
1897 int process,
1898 int include_space,
1899 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001900{
1901 char_u *comment_flags = NULL;
1902 int lead_len;
1903 int leader_offset = get_last_leader_offset(line, &comment_flags);
1904
1905 *is_comment = FALSE;
1906 if (leader_offset != -1)
1907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001908 // Let's check whether the line ends with an unclosed comment.
1909 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001910 while (*comment_flags)
1911 {
1912 if (*comment_flags == COM_END
1913 || *comment_flags == ':')
1914 break;
1915 ++comment_flags;
1916 }
1917 if (*comment_flags != COM_END)
1918 *is_comment = TRUE;
1919 }
1920
1921 if (process == FALSE)
1922 return line;
1923
1924 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1925
1926 if (lead_len == 0)
1927 return line;
1928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001929 // Find:
1930 // - COM_END,
1931 // - colon,
1932 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001933 while (*comment_flags)
1934 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001935 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001936 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001937 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001938 ++comment_flags;
1939 }
1940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001941 // If we found a colon, it means that we are not processing a line
1942 // starting with a closing part of a three-part comment. That's good,
1943 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001944 if (*comment_flags == ':' || *comment_flags == NUL)
1945 line += lead_len;
1946
1947 return line;
1948}
Bram Moolenaar81340392012-06-06 16:12:59 +02001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001951 * Join 'count' lines (minimal 2) at cursor position.
1952 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001953 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1954 * leaders should not be removed.
1955 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1956 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001958 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 */
1960 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001961do_join(
1962 long count,
1963 int insert_space,
1964 int save_undo,
1965 int use_formatoptions UNUSED,
1966 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001968 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001969 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001970 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001972 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001973 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001974 int endcurr1 = NUL;
1975 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001976 int currsize = 0; // size of the current line
1977 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001979 colnr_T col = 0;
1980 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001981 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001982 int remove_comments = (use_formatoptions == TRUE)
1983 && has_format_option(FO_REMOVE_COMS);
1984 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001985#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001986 int propcount = 0; // number of props over all joined lines
1987 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001990 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1991 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001994 // Allocate an array to store the number of spaces inserted before each
1995 // line. We will use it to pre-compute the length of the new line and the
1996 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001997 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001998 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002000 if (remove_comments)
2001 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002002 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002003 if (comments == NULL)
2004 {
2005 vim_free(spaces);
2006 return FAIL;
2007 }
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002011 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002012 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002013 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002015 for (t = 0; t < count; ++t)
2016 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002017 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002018#ifdef FEAT_PROP_POPUP
2019 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
2020#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002021 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002022 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002023 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002024 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2025 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2026 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002027 if (remove_comments)
2028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // We don't want to remove the comment leader if the
2030 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002031 if (t > 0 && prev_was_comment)
2032 {
2033
2034 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2035 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002036 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002037 curr = new_curr;
2038 }
2039 else
2040 curr = skip_comment(curr, FALSE, insert_space,
2041 &prev_was_comment);
2042 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002043
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 if (insert_space && t > 0)
2045 {
2046 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002047 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002048 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002049 && (!has_format_option(FO_MBYTE_JOIN)
2050 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2051 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002052 || (mb_ptr2char(curr) < 0x100
2053 && !(enc_utf8 && utf_eat_space(endcurr1)))
2054 || (endcurr1 < 0x100
2055 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002056 )
2057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002059 if (endcurr1 == ' ')
2060 endcurr1 = endcurr2;
2061 else
2062 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002063 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002064 if ( p_js
2065 && (endcurr1 == '.'
2066 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2067 && (endcurr1 == '?' || endcurr1 == '!'))))
2068 ++spaces[t];
2069 }
2070 }
2071 currsize = (int)STRLEN(curr);
2072 sumsize += currsize + spaces[t];
2073 endcurr1 = endcurr2 = NUL;
2074 if (insert_space && currsize > 0)
2075 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002076 if (has_mbyte)
2077 {
2078 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002079 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002080 endcurr1 = (*mb_ptr2char)(cend);
2081 if (cend > curr)
2082 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002083 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002084 endcurr2 = (*mb_ptr2char)(cend);
2085 }
2086 }
2087 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002088 {
2089 endcurr1 = *(curr + currsize - 1);
2090 if (currsize > 1)
2091 endcurr2 = *(curr + currsize - 2);
2092 }
2093 }
2094 line_breakcheck();
2095 if (got_int)
2096 {
2097 ret = FAIL;
2098 goto theend;
2099 }
2100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002102 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002103 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002105 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002106 newp_len = sumsize + 1;
2107#ifdef FEAT_PROP_POPUP
2108 newp_len += propcount * sizeof(textprop_T);
2109#endif
2110 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002111 if (newp == NULL)
2112 {
2113 ret = FAIL;
2114 goto theend;
2115 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002116 cend = newp + sumsize;
2117 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002119 /*
2120 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002121 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002122 *
2123 * Move marks from each deleted line to the joined line, adjusting the
2124 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2125 * should not really be a problem.
2126 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002127#ifdef FEAT_PROP_POPUP
2128 props_remaining = propcount;
2129#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002130 for (t = count - 1; ; --t)
2131 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002132 int spaces_removed;
2133
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002134 cend -= currsize;
2135 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002136
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002137 if (spaces[t] > 0)
2138 {
2139 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002140 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002141 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002142
2143 // If deleting more spaces than adding, the cursor moves no more than
2144 // what is added if it is inside these spaces.
2145 spaces_removed = (curr - curr_start) - spaces[t];
2146
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002147 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002148 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002149#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002150 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2151 curwin->w_cursor.lnum + t, t == count - 1,
2152 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002153#endif
2154
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002155 if (t == 0)
2156 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002157 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002158 if (remove_comments)
2159 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002160 if (insert_space && t > 1)
2161 curr = skipwhite(curr);
2162 currsize = (int)STRLEN(curr);
2163 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002164
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002165 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaare1004402020-10-24 20:49:43 +02002167 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002169 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002170 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002171 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002172 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002174 // Only report the change in the first line here, del_lines() will report
2175 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 changed_lines(curwin->w_cursor.lnum, currsize,
2177 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002179 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * briefly, and then move it back. After del_lines() the cursor may
2181 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
2183 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002185 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 curwin->w_cursor.lnum = t;
2187
2188 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002189 * Set the cursor column:
2190 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002191 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002193 curwin->w_cursor.col =
2194 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 curwin->w_set_curswant = TRUE;
2199
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002200theend:
2201 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002202 if (remove_comments)
2203 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002204 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205}
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 * prepare a few things for block mode yank/delete/tilde
2209 *
2210 * for delete:
2211 * - textlen includes the first/last char to be (partly) deleted
2212 * - start/endspaces is the number of columns that are taken by the
2213 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002214 * deleted.
2215 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 * - textlen includes the first/last char to be wholly yanked
2217 * - start/endspaces is the number of columns of the first/last yanked char
2218 * that are to be yanked.
2219 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002221block_prep(
2222 oparg_T *oap,
2223 struct block_def *bdp,
2224 linenr_T lnum,
2225 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 int incr = 0;
2228 char_u *pend;
2229 char_u *pstart;
2230 char_u *line;
2231 char_u *prev_pstart;
2232 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002233#ifdef FEAT_LINEBREAK
2234 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002236 // Avoid a problem with unwanted linebreaks in block mode.
2237 curwin->w_p_lbr = FALSE;
2238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 bdp->startspaces = 0;
2240 bdp->endspaces = 0;
2241 bdp->textlen = 0;
2242 bdp->start_vcol = 0;
2243 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 bdp->is_short = FALSE;
2245 bdp->is_oneChar = FALSE;
2246 bdp->pre_whitesp = 0;
2247 bdp->pre_whitesp_c = 0;
2248 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 bdp->start_char_vcols = 0;
2250
2251 line = ml_get(lnum);
2252 pstart = line;
2253 prev_pstart = line;
2254 while (bdp->start_vcol < oap->start_vcol && *pstart)
2255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002256 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002257 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002259 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 bdp->pre_whitesp += incr;
2262 bdp->pre_whitesp_c++;
2263 }
2264 else
2265 {
2266 bdp->pre_whitesp = 0;
2267 bdp->pre_whitesp_c = 0;
2268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002270 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (!is_del || oap->op_type == OP_APPEND)
2278 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2279 }
2280 else
2281 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002282 // notice: this converts partly selected Multibyte characters to
2283 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2285 if (is_del && bdp->startspaces)
2286 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2287 pend = pstart;
2288 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (oap->op_type == OP_INSERT)
2293 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2294 else if (oap->op_type == OP_APPEND)
2295 {
2296 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2297 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2298 }
2299 else
2300 {
2301 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2302 if (is_del && oap->op_type != OP_LSHIFT)
2303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002304 // just putting the sum of those two into
2305 // bdp->startspaces doesn't work for Visual replace,
2306 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 bdp->startspaces = bdp->start_char_vcols
2308 - (bdp->start_vcol - oap->start_vcol);
2309 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2310 }
2311 }
2312 }
2313 else
2314 {
2315 prev_pend = pend;
2316 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002318 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002320 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 bdp->end_vcol += incr;
2322 }
2323 if (bdp->end_vcol <= oap->end_vcol
2324 && (!is_del
2325 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002326 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002329 // Alternative: include spaces to fill up the block.
2330 // Disadvantage: can lead to trailing spaces when the line is
2331 // short where the text is put
2332 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (oap->op_type == OP_APPEND || virtual_op)
2334 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002335 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339 else if (bdp->end_vcol > oap->end_vcol)
2340 {
2341 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2342 if (!is_del && bdp->endspaces)
2343 {
2344 bdp->endspaces = incr - bdp->endspaces;
2345 if (pend != pstart)
2346 pend = prev_pend;
2347 }
2348 }
2349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (is_del && bdp->startspaces)
2352 pstart = prev_pstart;
2353 bdp->textlen = (int)(pend - pstart);
2354 }
2355 bdp->textcol = (colnr_T) (pstart - line);
2356 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002357#ifdef FEAT_LINEBREAK
2358 curwin->w_p_lbr = lbr_saved;
2359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002363 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002365 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002366op_addsub(
2367 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002368 linenr_T Prenum1, // Amount of add/subtract
2369 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002371 pos_T pos;
2372 struct block_def bd;
2373 int change_cnt = 0;
2374 linenr_T amount = Prenum1;
2375
Bram Moolenaar6b731882018-11-16 20:54:47 +01002376 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002377 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002378 // the call to changed_lines().
2379#ifdef FEAT_FOLDING
2380 disable_fold_update++;
2381#endif
2382
Bram Moolenaard79e5502016-01-10 22:13:02 +01002383 if (!VIsual_active)
2384 {
2385 pos = curwin->w_cursor;
2386 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002387 {
2388#ifdef FEAT_FOLDING
2389 disable_fold_update--;
2390#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002391 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002392 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002393 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002394#ifdef FEAT_FOLDING
2395 disable_fold_update--;
2396#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002397 if (change_cnt)
2398 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2399 }
2400 else
2401 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002402 int one_change;
2403 int length;
2404 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002405
2406 if (u_save((linenr_T)(oap->start.lnum - 1),
2407 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002408 {
2409#ifdef FEAT_FOLDING
2410 disable_fold_update--;
2411#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002412 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002413 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002414
2415 pos = oap->start;
2416 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2417 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002418 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002419 {
2420 block_prep(oap, &bd, pos.lnum, FALSE);
2421 pos.col = bd.textcol;
2422 length = bd.textlen;
2423 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002424 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002425 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002426 curwin->w_cursor.col = 0;
2427 pos.col = 0;
2428 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2429 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002430 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002431 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002432 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002433 dec(&(oap->end));
2434 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2435 pos.col = 0;
2436 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002437 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002438 pos.col += oap->start.col;
2439 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002440 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002441 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002442 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002443 length = (int)STRLEN(ml_get(oap->end.lnum));
2444 if (oap->end.col >= length)
2445 oap->end.col = length - 1;
2446 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002447 }
2448 }
2449 one_change = do_addsub(oap->op_type, &pos, length, amount);
2450 if (one_change)
2451 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002452 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002453 if (change_cnt == 0)
2454 startpos = curbuf->b_op_start;
2455 ++change_cnt;
2456 }
2457
2458#ifdef FEAT_NETBEANS_INTG
2459 if (netbeans_active() && one_change)
2460 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002461 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002462
2463 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002464 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002465 netbeans_inserted(curbuf, pos.lnum, pos.col,
2466 &ptr[pos.col], length);
2467 }
2468#endif
2469 if (g_cmd && one_change)
2470 amount += Prenum1;
2471 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002472
2473#ifdef FEAT_FOLDING
2474 disable_fold_update--;
2475#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002476 if (change_cnt)
2477 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2478
2479 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002481 redraw_curbuf_later(INVERTED);
2482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002483 // Set '[ mark if something changed. Keep the last end
2484 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002485 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002486 curbuf->b_op_start = startpos;
2487
2488 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002489 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002490 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002491 }
2492}
2493
2494/*
2495 * Add or subtract 'Prenum1' from a number in a line
2496 * op_type is OP_NR_ADD or OP_NR_SUB
2497 *
2498 * Returns TRUE if some character was changed.
2499 */
2500 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002501do_addsub(
2502 int op_type,
2503 pos_T *pos,
2504 int length,
2505 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002506{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 int col;
2508 char_u *buf1;
2509 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002510 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2511 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002512 uvarnumber_T n;
2513 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 char_u *ptr;
2515 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002517 int do_hex;
2518 int do_oct;
2519 int do_bin;
2520 int do_alpha;
2521 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002524 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002525 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002526 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002527 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002528 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002529 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002530 pos_T startpos;
2531 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002532 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002534 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2535 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2536 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2537 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2538 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002540 if (virtual_active())
2541 {
2542 save_coladd = pos->coladd;
2543 pos->coladd = 0;
2544 }
2545
Bram Moolenaard79e5502016-01-10 22:13:02 +01002546 curwin->w_cursor = *pos;
2547 ptr = ml_get(pos->lnum);
2548 col = pos->col;
2549
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002550 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002551 goto theend;
2552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 /*
2554 * First check if we are on a hexadecimal number, after the "0x".
2555 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002556 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002558 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002559 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002560 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002561 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002562 if (has_mbyte)
2563 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002564 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002565
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002566 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002567 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002568 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002569 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002570 if (has_mbyte)
2571 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002572 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002573
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002574 if ( do_bin
2575 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002576 && ! ((col > 0
2577 && (ptr[col] == 'X'
2578 || ptr[col] == 'x')
2579 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002580 && (!has_mbyte ||
2581 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002582 && vim_isxdigit(ptr[col + 1]))))
2583 {
2584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002585 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002586
Bram Moolenaard79e5502016-01-10 22:13:02 +01002587 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002588
2589 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002590 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002591 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002592 if (has_mbyte)
2593 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002594 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002595 }
2596
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002597 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002598 && col > 0
2599 && (ptr[col] == 'X'
2600 || ptr[col] == 'x')
2601 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002602 && (!has_mbyte ||
2603 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002604 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002605 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002606 && col > 0
2607 && (ptr[col] == 'B'
2608 || ptr[col] == 'b')
2609 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002610 && (!has_mbyte ||
2611 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002612 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002614 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002615 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002616 if (has_mbyte)
2617 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002618 }
2619 else
2620 {
2621 /*
2622 * Search forward and then backward to find the start of number.
2623 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002624 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002625
2626 while (ptr[col] != NUL
2627 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002628 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002629 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002630
2631 while (col > 0
2632 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002633 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002634 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002635 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002636 if (has_mbyte)
2637 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002638 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002639 }
2640 }
2641
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002642 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002643 {
2644 while (ptr[col] != NUL && length > 0
2645 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002646 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002647 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002648 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002649
2650 col += mb_len;
2651 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002652 }
2653
2654 if (length == 0)
2655 goto theend;
2656
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002657 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002658 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2659 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002660 {
2661 negative = TRUE;
2662 was_positive = FALSE;
2663 }
2664 }
2665
2666 /*
2667 * If a number was found, and saving for undo works, replace the number.
2668 */
2669 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002670 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002671 {
2672 beep_flush();
2673 goto theend;
2674 }
2675
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002676 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002679 if (op_type == OP_NR_SUB)
2680 {
2681 if (CharOrd(firstdigit) < Prenum1)
2682 {
2683 if (isupper(firstdigit))
2684 firstdigit = 'A';
2685 else
2686 firstdigit = 'a';
2687 }
2688 else
2689#ifdef EBCDIC
2690 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2691#else
2692 firstdigit -= Prenum1;
2693#endif
2694 }
2695 else
2696 {
2697 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2698 {
2699 if (isupper(firstdigit))
2700 firstdigit = 'Z';
2701 else
2702 firstdigit = 'z';
2703 }
2704 else
2705#ifdef EBCDIC
2706 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2707#else
2708 firstdigit += Prenum1;
2709#endif
2710 }
2711 curwin->w_cursor.col = col;
2712 if (!did_change)
2713 startpos = curwin->w_cursor;
2714 did_change = TRUE;
2715 (void)del_char(FALSE);
2716 ins_char(firstdigit);
2717 endpos = curwin->w_cursor;
2718 curwin->w_cursor.col = col;
2719 }
2720 else
2721 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002722 pos_T save_pos;
2723 int i;
2724
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002725 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002726 && (!has_mbyte ||
2727 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002728 && !visual
2729 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002732 --col;
2733 negative = TRUE;
2734 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002736 if (visual && VIsual_mode != 'V')
2737 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2738 ? (int)STRLEN(ptr) - col
2739 : length);
2740
2741 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002742 0 + (do_bin ? STR2NR_BIN : 0)
2743 + (do_oct ? STR2NR_OCT : 0)
2744 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002745 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002747 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002748 if (pre && negative)
2749 {
2750 ++col;
2751 --length;
2752 negative = FALSE;
2753 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002755 subtract = FALSE;
2756 if (op_type == OP_NR_SUB)
2757 subtract ^= TRUE;
2758 if (negative)
2759 subtract ^= TRUE;
2760
2761 oldn = n;
2762 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002763 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002764 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002765 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002767 if (!pre)
2768 {
2769 if (subtract)
2770 {
2771 if (n > oldn)
2772 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002773 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002774 negative ^= TRUE;
2775 }
2776 }
2777 else
2778 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002780 if (n < oldn)
2781 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002782 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002783 negative ^= TRUE;
2784 }
2785 }
2786 if (n == 0)
2787 negative = FALSE;
2788 }
2789
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002790 if (do_unsigned && negative)
2791 {
2792 if (subtract)
2793 // sticking at zero.
2794 n = (uvarnumber_T)0;
2795 else
2796 // sticking at 2^64 - 1.
2797 n = (uvarnumber_T)(-1);
2798 negative = FALSE;
2799 }
2800
Bram Moolenaard79e5502016-01-10 22:13:02 +01002801 if (visual && !was_positive && !negative && col > 0)
2802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002804 col--;
2805 length++;
2806 }
2807
2808 /*
2809 * Delete the old number.
2810 */
2811 curwin->w_cursor.col = col;
2812 if (!did_change)
2813 startpos = curwin->w_cursor;
2814 did_change = TRUE;
2815 todel = length;
2816 c = gchar_cursor();
2817 /*
2818 * Don't include the '-' in the length, only the length of the
2819 * part after it is kept the same.
2820 */
2821 if (c == '-')
2822 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002823
2824 save_pos = curwin->w_cursor;
2825 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002826 {
2827 if (c < 0x100 && isalpha(c))
2828 {
2829 if (isupper(c))
2830 hexupper = TRUE;
2831 else
2832 hexupper = FALSE;
2833 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002834 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002835 c = gchar_cursor();
2836 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002837 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002838
2839 /*
2840 * Prepare the leading characters in buf1[].
2841 * When there are many leading zeros it could be very long.
2842 * Allocate a bit too much.
2843 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002844 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002845 if (buf1 == NULL)
2846 goto theend;
2847 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002848 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002849 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002850 if (pre)
2851 {
2852 *ptr++ = '0';
2853 --length;
2854 }
2855 if (pre == 'b' || pre == 'B' ||
2856 pre == 'x' || pre == 'X')
2857 {
2858 *ptr++ = pre;
2859 --length;
2860 }
2861
2862 /*
2863 * Put the number characters in buf2[].
2864 */
2865 if (pre == 'b' || pre == 'B')
2866 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002867 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002868 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002871 for (bit = bits; bit > 0; bit--)
2872 if ((n >> (bit - 1)) & 0x1) break;
2873
2874 for (i = 0; bit > 0; bit--)
2875 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2876
2877 buf2[i] = '\0';
2878 }
2879 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002880 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002881 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002882 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002883 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002884 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002885 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002886 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002887 length -= (int)STRLEN(buf2);
2888
2889 /*
2890 * Adjust number of zeros to the new number of digits, so the
2891 * total length of the number remains the same.
2892 * Don't do this when
2893 * the result may look like an octal number.
2894 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002895 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002896 while (length-- > 0)
2897 *ptr++ = '0';
2898 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002899
Bram Moolenaard79e5502016-01-10 22:13:02 +01002900 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002901
2902 // Insert just after the first character to be removed, so that any
2903 // text properties will be adjusted. Then delete the old number
2904 // afterwards.
2905 save_pos = curwin->w_cursor;
2906 if (todel > 0)
2907 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002909 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002910
2911 // del_char() will also mark line needing displaying
2912 if (todel > 0)
2913 {
2914 int bytes_after = (int)STRLEN(ml_get_curline())
2915 - curwin->w_cursor.col;
2916
2917 // Delete the one character before the insert.
2918 curwin->w_cursor = save_pos;
2919 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002920 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2921 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002922 --todel;
2923 }
2924 while (todel-- > 0)
2925 (void)del_char(FALSE);
2926
Bram Moolenaard79e5502016-01-10 22:13:02 +01002927 endpos = curwin->w_cursor;
2928 if (did_change && curwin->w_cursor.col)
2929 --curwin->w_cursor.col;
2930 }
2931
Bram Moolenaare1004402020-10-24 20:49:43 +02002932 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002935 curbuf->b_op_start = startpos;
2936 curbuf->b_op_end = endpos;
2937 if (curbuf->b_op_end.col > 0)
2938 --curbuf->b_op_end.col;
2939 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002940
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002941theend:
2942 if (visual)
2943 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002944 else if (did_change)
2945 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002946 else if (virtual_active())
2947 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002948
Bram Moolenaard79e5502016-01-10 22:13:02 +01002949 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002953clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002955 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956}
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002959 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 *
2961 * "Words" are counted by looking for boundaries between non-space and
2962 * space characters. (it seems to produce results that match 'wc'.)
2963 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002964 * Return value is byte count; word count for the line is added to "*wc".
2965 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 *
2967 * The function will only examine the first "limit" characters in the
2968 * line, stopping if it encounters an end-of-line (NUL byte). In that
2969 * case, eol_size will be added to the character count to account for
2970 * the size of the EOL character.
2971 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002972 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002973line_count_info(
2974 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002975 varnumber_T *wc,
2976 varnumber_T *cc,
2977 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002978 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002980 varnumber_T i;
2981 varnumber_T words = 0;
2982 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 int is_word = 0;
2984
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002985 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
2987 if (is_word)
2988 {
2989 if (vim_isspace(line[i]))
2990 {
2991 words++;
2992 is_word = 0;
2993 }
2994 }
2995 else if (!vim_isspace(line[i]))
2996 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002997 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002998 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
3000
3001 if (is_word)
3002 words++;
3003 *wc += words;
3004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003006 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003009 chars += eol_size;
3010 }
3011 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 return i;
3013}
3014
3015/*
3016 * Give some info about the position of the cursor (for "g CTRL-G").
3017 * In Visual mode, give some info about the selected region. (In this case,
3018 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003019 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
3021 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003022cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003025 char_u buf1[50];
3026 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003028 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003029 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003030 varnumber_T byte_count_cursor = 0;
3031 varnumber_T char_count = 0;
3032 varnumber_T char_count_cursor = 0;
3033 varnumber_T word_count = 0;
3034 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003035 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003036 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 long line_count_selected = 0;
3038 pos_T min_pos, max_pos;
3039 oparg_T oparg;
3040 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042 /*
3043 * Compute the length of the file in characters.
3044 */
3045 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3046 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003047 if (dict == NULL)
3048 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003049 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003050 return;
3051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053 else
3054 {
3055 if (get_fileformat(curbuf) == EOL_DOS)
3056 eol_size = 2;
3057 else
3058 eol_size = 1;
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (VIsual_active)
3061 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003062 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
3064 min_pos = VIsual;
3065 max_pos = curwin->w_cursor;
3066 }
3067 else
3068 {
3069 min_pos = curwin->w_cursor;
3070 max_pos = VIsual;
3071 }
3072 if (*p_sel == 'e' && max_pos.col > 0)
3073 --max_pos.col;
3074
3075 if (VIsual_mode == Ctrl_V)
3076 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003077#ifdef FEAT_LINEBREAK
3078 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003079 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003082 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003083 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 oparg.is_VIsual = 1;
3086 oparg.block_mode = TRUE;
3087 oparg.op_type = OP_NOP;
3088 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003089 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003090#ifdef FEAT_LINEBREAK
3091 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003092 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003093#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003094 if (curwin->w_curswant == MAXCOL)
3095 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (oparg.end_vcol < oparg.start_vcol)
3098 {
3099 oparg.end_vcol += oparg.start_vcol;
3100 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3101 oparg.end_vcol -= oparg.start_vcol;
3102 }
3103 }
3104 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
3107 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3108 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003110 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 ui_breakcheck();
3113 if (got_int)
3114 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003115 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (VIsual_active
3120 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3121 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003122 char_u *s = NULL;
3123 long len = 0L;
3124
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 switch (VIsual_mode)
3126 {
3127 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003131 s = bd.textstart;
3132 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 break;
3134 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003135 s = ml_get(lnum);
3136 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 break;
3138 case 'v':
3139 {
3140 colnr_T start_col = (lnum == min_pos.lnum)
3141 ? min_pos.col : 0;
3142 colnr_T end_col = (lnum == max_pos.lnum)
3143 ? max_pos.col - start_col + 1 : MAXCOL;
3144
Bram Moolenaardef9e822004-12-31 20:58:58 +00003145 s = ml_get(lnum) + start_col;
3146 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
3148 break;
3149 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003150 if (s != NULL)
3151 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003152 byte_count_cursor += line_count_info(s, &word_count_cursor,
3153 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003154 if (lnum == curbuf->b_ml.ml_line_count
3155 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003156 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003157 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003158 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003163 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (lnum == curwin->w_cursor.lnum)
3165 {
3166 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003167 char_count_cursor += char_count;
3168 byte_count_cursor = byte_count +
3169 line_count_info(ml_get(lnum),
3170 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003171 (varnumber_T)(curwin->w_cursor.col + 1),
3172 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003176 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003177 &char_count, (varnumber_T)MAXCOL,
3178 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003182 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003183 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaared767a22016-01-03 22:49:16 +01003185 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003187 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003189 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3190 {
3191 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3192 &max_pos.col);
3193 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3194 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3195 }
3196 else
3197 buf1[0] = NUL;
3198
3199 if (char_count_cursor == byte_count_cursor
3200 && char_count == byte_count)
3201 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003202 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003203 buf1, line_count_selected,
3204 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003205 (varnumber_T)word_count_cursor,
3206 (varnumber_T)word_count,
3207 (varnumber_T)byte_count_cursor,
3208 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003209 else
3210 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003211 _("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 +01003212 buf1, line_count_selected,
3213 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003214 (varnumber_T)word_count_cursor,
3215 (varnumber_T)word_count,
3216 (varnumber_T)char_count_cursor,
3217 (varnumber_T)char_count,
3218 (varnumber_T)byte_count_cursor,
3219 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
3221 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003222 {
3223 p = ml_get_curline();
3224 validate_virtcol();
3225 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3226 (int)curwin->w_virtcol + 1);
3227 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3228 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaared767a22016-01-03 22:49:16 +01003230 if (char_count_cursor == byte_count_cursor
3231 && char_count == byte_count)
3232 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003233 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003234 (char *)buf1, (char *)buf2,
3235 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003237 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3238 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003239 else
3240 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003241 _("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 +01003242 (char *)buf1, (char *)buf2,
3243 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003244 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003245 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3246 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3247 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003248 }
3249 }
3250
Bram Moolenaared767a22016-01-03 22:49:16 +01003251 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003252 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003253 {
3254 size_t len = STRLEN(IObuff);
3255
3256 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003257 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003258 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003259 if (dict == NULL)
3260 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003261 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003262 p = p_shm;
3263 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003264 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003265 p_shm = p;
3266 }
3267 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003268#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003269 if (dict != NULL)
3270 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003271 dict_add_number(dict, "words", word_count);
3272 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003273 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003274 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3275 byte_count_cursor);
3276 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3277 char_count_cursor);
3278 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3279 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003283
3284/*
3285 * Handle indent and format operators and visual mode ":".
3286 */
3287 static void
3288op_colon(oparg_T *oap)
3289{
3290 stuffcharReadbuff(':');
3291 if (oap->is_VIsual)
3292 stuffReadbuff((char_u *)"'<,'>");
3293 else
3294 {
3295 // Make the range look nice, so it can be repeated.
3296 if (oap->start.lnum == curwin->w_cursor.lnum)
3297 stuffcharReadbuff('.');
3298 else
3299 stuffnumReadbuff((long)oap->start.lnum);
3300 if (oap->end.lnum != oap->start.lnum)
3301 {
3302 stuffcharReadbuff(',');
3303 if (oap->end.lnum == curwin->w_cursor.lnum)
3304 stuffcharReadbuff('.');
3305 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3306 stuffcharReadbuff('$');
3307 else if (oap->start.lnum == curwin->w_cursor.lnum)
3308 {
3309 stuffReadbuff((char_u *)".+");
3310 stuffnumReadbuff((long)oap->line_count - 1);
3311 }
3312 else
3313 stuffnumReadbuff((long)oap->end.lnum);
3314 }
3315 }
3316 if (oap->op_type != OP_COLON)
3317 stuffReadbuff((char_u *)"!");
3318 if (oap->op_type == OP_INDENT)
3319 {
3320#ifndef FEAT_CINDENT
3321 if (*get_equalprg() == NUL)
3322 stuffReadbuff((char_u *)"indent");
3323 else
3324#endif
3325 stuffReadbuff(get_equalprg());
3326 stuffReadbuff((char_u *)"\n");
3327 }
3328 else if (oap->op_type == OP_FORMAT)
3329 {
3330 if (*curbuf->b_p_fp != NUL)
3331 stuffReadbuff(curbuf->b_p_fp);
3332 else if (*p_fp != NUL)
3333 stuffReadbuff(p_fp);
3334 else
3335 stuffReadbuff((char_u *)"fmt");
3336 stuffReadbuff((char_u *)"\n']");
3337 }
3338
3339 // do_cmdline() does the rest
3340}
3341
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003342// callback function for 'operatorfunc'
3343static callback_T opfunc_cb;
3344
3345/*
3346 * Process the 'operatorfunc' option value.
3347 * Returns OK or FAIL.
3348 */
3349 int
3350set_operatorfunc_option(void)
3351{
3352 return option_set_callback_func(p_opfunc, &opfunc_cb);
3353}
3354
3355#if defined(EXITFREE) || defined(PROTO)
3356 void
3357free_operatorfunc_option(void)
3358{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003359# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003360 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003361# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003362}
3363#endif
3364
Dominique Pelle748b3082022-01-08 12:41:16 +00003365#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003366/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003367 * Mark the global 'operatorfunc' callback with 'copyID' so that it is not
3368 * garbage collected.
3369 */
3370 int
3371set_ref_in_opfunc(int copyID UNUSED)
3372{
3373 int abort = FALSE;
3374
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003375 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003376
3377 return abort;
3378}
Dominique Pelle748b3082022-01-08 12:41:16 +00003379#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003380
3381/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003382 * Handle the "g@" operator: call 'operatorfunc'.
3383 */
3384 static void
3385op_function(oparg_T *oap UNUSED)
3386{
3387#ifdef FEAT_EVAL
3388 typval_T argv[2];
3389 int save_virtual_op = virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003390 int save_finish_op = finish_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003391 pos_T orig_start = curbuf->b_op_start;
3392 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003393 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003394
3395 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003396 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003397 else
3398 {
3399 // Set '[ and '] marks to text to be operated on.
3400 curbuf->b_op_start = oap->start;
3401 curbuf->b_op_end = oap->end;
3402 if (oap->motion_type != MLINE && !oap->inclusive)
3403 // Exclude the end position.
3404 decl(&curbuf->b_op_end);
3405
3406 argv[0].v_type = VAR_STRING;
3407 if (oap->block_mode)
3408 argv[0].vval.v_string = (char_u *)"block";
3409 else if (oap->motion_type == MLINE)
3410 argv[0].vval.v_string = (char_u *)"line";
3411 else
3412 argv[0].vval.v_string = (char_u *)"char";
3413 argv[1].v_type = VAR_UNKNOWN;
3414
3415 // Reset virtual_op so that 'virtualedit' can be changed in the
3416 // function.
3417 virtual_op = MAYBE;
3418
naohiro ono75c30e92021-10-19 11:15:41 +01003419 // Reset finish_op so that mode() returns the right value.
3420 finish_op = FALSE;
3421
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003422 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3423 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003424
3425 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003426 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003427 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003428 {
3429 curbuf->b_op_start = orig_start;
3430 curbuf->b_op_end = orig_end;
3431 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003432 }
3433#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003434 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003435#endif
3436}
3437
3438/*
3439 * Calculate start/end virtual columns for operating in block mode.
3440 */
3441 static void
3442get_op_vcol(
3443 oparg_T *oap,
3444 colnr_T redo_VIsual_vcol,
3445 int initial) // when TRUE adjust position for 'selectmode'
3446{
3447 colnr_T start, end;
3448
3449 if (VIsual_mode != Ctrl_V
3450 || (!initial && oap->end.col < curwin->w_width))
3451 return;
3452
3453 oap->block_mode = TRUE;
3454
3455 // prevent from moving onto a trail byte
3456 if (has_mbyte)
3457 mb_adjustpos(curwin->w_buffer, &oap->end);
3458
3459 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3460
3461 if (!redo_VIsual_busy)
3462 {
3463 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3464
3465 if (start < oap->start_vcol)
3466 oap->start_vcol = start;
3467 if (end > oap->end_vcol)
3468 {
3469 if (initial && *p_sel == 'e' && start >= 1
3470 && start - 1 >= oap->end_vcol)
3471 oap->end_vcol = start - 1;
3472 else
3473 oap->end_vcol = end;
3474 }
3475 }
3476
3477 // if '$' was used, get oap->end_vcol from longest line
3478 if (curwin->w_curswant == MAXCOL)
3479 {
3480 curwin->w_cursor.col = MAXCOL;
3481 oap->end_vcol = 0;
3482 for (curwin->w_cursor.lnum = oap->start.lnum;
3483 curwin->w_cursor.lnum <= oap->end.lnum;
3484 ++curwin->w_cursor.lnum)
3485 {
3486 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3487 if (end > oap->end_vcol)
3488 oap->end_vcol = end;
3489 }
3490 }
3491 else if (redo_VIsual_busy)
3492 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3493 // Correct oap->end.col and oap->start.col to be the
3494 // upper-left and lower-right corner of the block area.
3495 //
3496 // (Actually, this does convert column positions into character
3497 // positions)
3498 curwin->w_cursor.lnum = oap->end.lnum;
3499 coladvance(oap->end_vcol);
3500 oap->end = curwin->w_cursor;
3501
3502 curwin->w_cursor = oap->start;
3503 coladvance(oap->start_vcol);
3504 oap->start = curwin->w_cursor;
3505}
3506
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003507// Information for redoing the previous Visual selection.
3508typedef struct {
3509 int rv_mode; // 'v', 'V', or Ctrl-V
3510 linenr_T rv_line_count; // number of lines
3511 colnr_T rv_vcol; // number of cols or end column
3512 long rv_count; // count for Visual operator
3513 int rv_arg; // extra argument
3514} redo_VIsual_T;
3515
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003516 static int
3517is_ex_cmdchar(cmdarg_T *cap)
3518{
3519 return cap->cmdchar == ':'
3520 || cap->cmdchar == K_COMMAND
3521 || cap->cmdchar == K_SCRIPT_COMMAND;
3522}
3523
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003524/*
3525 * Handle an operator after Visual mode or when the movement is finished.
3526 * "gui_yank" is true when yanking text for the clipboard.
3527 */
3528 void
3529do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3530{
3531 oparg_T *oap = cap->oap;
3532 pos_T old_cursor;
3533 int empty_region_error;
3534 int restart_edit_save;
3535#ifdef FEAT_LINEBREAK
3536 int lbr_saved = curwin->w_p_lbr;
3537#endif
3538
3539 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003540 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3541
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003542 int include_line_break = FALSE;
3543
3544#if defined(FEAT_CLIPBOARD)
3545 // Yank the visual area into the GUI selection register before we operate
3546 // on it and lose it forever.
3547 // Don't do it if a specific register was specified, so that ""x"*P works.
3548 // This could call do_pending_operator() recursively, but that's OK
3549 // because gui_yank will be TRUE for the nested call.
3550 if ((clip_star.available || clip_plus.available)
3551 && oap->op_type != OP_NOP
3552 && !gui_yank
3553 && VIsual_active
3554 && !redo_VIsual_busy
3555 && oap->regname == 0)
3556 clip_auto_select();
3557#endif
3558 old_cursor = curwin->w_cursor;
3559
3560 // If an operation is pending, handle it...
3561 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3562 {
3563 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3564 // for the clipboard.
3565 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3566
3567#ifdef FEAT_LINEBREAK
3568 // Avoid a problem with unwanted linebreaks in block mode.
3569 if (curwin->w_p_lbr)
3570 curwin->w_valid &= ~VALID_VIRTCOL;
3571 curwin->w_p_lbr = FALSE;
3572#endif
3573 oap->is_VIsual = VIsual_active;
3574 if (oap->motion_force == 'V')
3575 oap->motion_type = MLINE;
3576 else if (oap->motion_force == 'v')
3577 {
3578 // If the motion was linewise, "inclusive" will not have been set.
3579 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3580 if (oap->motion_type == MLINE)
3581 oap->inclusive = FALSE;
3582 // If the motion already was characterwise, toggle "inclusive"
3583 else if (oap->motion_type == MCHAR)
3584 oap->inclusive = !oap->inclusive;
3585 oap->motion_type = MCHAR;
3586 }
3587 else if (oap->motion_force == Ctrl_V)
3588 {
3589 // Change line- or characterwise motion into Visual block mode.
3590 if (!VIsual_active)
3591 {
3592 VIsual_active = TRUE;
3593 VIsual = oap->start;
3594 }
3595 VIsual_mode = Ctrl_V;
3596 VIsual_select = FALSE;
3597 VIsual_reselect = FALSE;
3598 }
3599
3600 // Only redo yank when 'y' flag is in 'cpoptions'.
3601 // Never redo "zf" (define fold).
3602 if ((redo_yank || oap->op_type != OP_YANK)
3603 && ((!VIsual_active || oap->motion_force)
3604 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003605 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003606 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003607 && cap->cmdchar != 'D'
3608#ifdef FEAT_FOLDING
3609 && oap->op_type != OP_FOLD
3610 && oap->op_type != OP_FOLDOPEN
3611 && oap->op_type != OP_FOLDOPENREC
3612 && oap->op_type != OP_FOLDCLOSE
3613 && oap->op_type != OP_FOLDCLOSEREC
3614 && oap->op_type != OP_FOLDDEL
3615 && oap->op_type != OP_FOLDDELREC
3616#endif
3617 )
3618 {
3619 prep_redo(oap->regname, cap->count0,
3620 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3621 oap->motion_force, cap->cmdchar, cap->nchar);
3622 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3623 {
3624 // If 'cpoptions' does not contain 'r', insert the search
3625 // pattern to really repeat the same command.
3626 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3627 AppendToRedobuffLit(cap->searchbuf, -1);
3628 AppendToRedobuff(NL_STR);
3629 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003630 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003631 {
3632 // do_cmdline() has stored the first typed line in
3633 // "repeat_cmdline". When several lines are typed repeating
3634 // won't be possible.
3635 if (repeat_cmdline == NULL)
3636 ResetRedobuff();
3637 else
3638 {
3639 AppendToRedobuffLit(repeat_cmdline, -1);
3640 AppendToRedobuff(NL_STR);
3641 VIM_CLEAR(repeat_cmdline);
3642 }
3643 }
3644 }
3645
3646 if (redo_VIsual_busy)
3647 {
3648 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003649 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003650 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003651 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003652 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3653 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003654 VIsual_mode = redo_VIsual.rv_mode;
3655 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003656 {
3657 if (VIsual_mode == 'v')
3658 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003659 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003660 {
3661 validate_virtcol();
3662 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003663 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003664 }
3665 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003666 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003667 }
3668 else
3669 {
3670 curwin->w_curswant = MAXCOL;
3671 }
3672 coladvance(curwin->w_curswant);
3673 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003674 cap->count0 = redo_VIsual.rv_count;
3675 if (redo_VIsual.rv_count != 0)
3676 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003677 else
3678 cap->count1 = 1;
3679 }
3680 else if (VIsual_active)
3681 {
3682 if (!gui_yank)
3683 {
3684 // Save the current VIsual area for '< and '> marks, and "gv"
3685 curbuf->b_visual.vi_start = VIsual;
3686 curbuf->b_visual.vi_end = curwin->w_cursor;
3687 curbuf->b_visual.vi_mode = VIsual_mode;
3688 restore_visual_mode();
3689 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3690# ifdef FEAT_EVAL
3691 curbuf->b_visual_mode_eval = VIsual_mode;
3692# endif
3693 }
3694
3695 // In Select mode, a linewise selection is operated upon like a
3696 // characterwise selection.
3697 // Special case: gH<Del> deletes the last line.
3698 if (VIsual_select && VIsual_mode == 'V'
3699 && cap->oap->op_type != OP_DELETE)
3700 {
3701 if (LT_POS(VIsual, curwin->w_cursor))
3702 {
3703 VIsual.col = 0;
3704 curwin->w_cursor.col =
3705 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3706 }
3707 else
3708 {
3709 curwin->w_cursor.col = 0;
3710 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3711 }
3712 VIsual_mode = 'v';
3713 }
3714 // If 'selection' is "exclusive", backup one character for
3715 // charwise selections.
3716 else if (VIsual_mode == 'v')
3717 include_line_break = unadjust_for_sel();
3718
3719 oap->start = VIsual;
3720 if (VIsual_mode == 'V')
3721 {
3722 oap->start.col = 0;
3723 oap->start.coladd = 0;
3724 }
3725 }
3726
3727 // Set oap->start to the first position of the operated text, oap->end
3728 // to the end of the operated text. w_cursor is equal to oap->start.
3729 if (LT_POS(oap->start, curwin->w_cursor))
3730 {
3731#ifdef FEAT_FOLDING
3732 // Include folded lines completely.
3733 if (!VIsual_active)
3734 {
3735 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3736 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003737 if ((curwin->w_cursor.col > 0 || oap->inclusive
3738 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003739 && hasFolding(curwin->w_cursor.lnum, NULL,
3740 &curwin->w_cursor.lnum))
3741 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3742 }
3743#endif
3744 oap->end = curwin->w_cursor;
3745 curwin->w_cursor = oap->start;
3746
3747 // w_virtcol may have been updated; if the cursor goes back to its
3748 // previous position w_virtcol becomes invalid and isn't updated
3749 // automatically.
3750 curwin->w_valid &= ~VALID_VIRTCOL;
3751 }
3752 else
3753 {
3754#ifdef FEAT_FOLDING
3755 // Include folded lines completely.
3756 if (!VIsual_active && oap->motion_type == MLINE)
3757 {
3758 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3759 NULL))
3760 curwin->w_cursor.col = 0;
3761 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3762 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3763 }
3764#endif
3765 oap->end = oap->start;
3766 oap->start = curwin->w_cursor;
3767 }
3768
3769 // Just in case lines were deleted that make the position invalid.
3770 check_pos(curwin->w_buffer, &oap->end);
3771 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3772
3773 // Set "virtual_op" before resetting VIsual_active.
3774 virtual_op = virtual_active();
3775
3776 if (VIsual_active || redo_VIsual_busy)
3777 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003778 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003779
3780 if (!redo_VIsual_busy && !gui_yank)
3781 {
3782 // Prepare to reselect and redo Visual: this is based on the
3783 // size of the Visual text
3784 resel_VIsual_mode = VIsual_mode;
3785 if (curwin->w_curswant == MAXCOL)
3786 resel_VIsual_vcol = MAXCOL;
3787 else
3788 {
3789 if (VIsual_mode != Ctrl_V)
3790 getvvcol(curwin, &(oap->end),
3791 NULL, NULL, &oap->end_vcol);
3792 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3793 {
3794 if (VIsual_mode != Ctrl_V)
3795 getvvcol(curwin, &(oap->start),
3796 &oap->start_vcol, NULL, NULL);
3797 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3798 }
3799 else
3800 resel_VIsual_vcol = oap->end_vcol;
3801 }
3802 resel_VIsual_line_count = oap->line_count;
3803 }
3804
3805 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3806 if ((redo_yank || oap->op_type != OP_YANK)
3807 && oap->op_type != OP_COLON
3808#ifdef FEAT_FOLDING
3809 && oap->op_type != OP_FOLD
3810 && oap->op_type != OP_FOLDOPEN
3811 && oap->op_type != OP_FOLDOPENREC
3812 && oap->op_type != OP_FOLDCLOSE
3813 && oap->op_type != OP_FOLDCLOSEREC
3814 && oap->op_type != OP_FOLDDEL
3815 && oap->op_type != OP_FOLDDELREC
3816#endif
3817 && oap->motion_force == NUL
3818 )
3819 {
3820 // Prepare for redoing. Only use the nchar field for "r",
3821 // otherwise it might be the second char of the operator.
3822 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3823 || cap->nchar == 'N'))
3824 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003825 get_op_char(oap->op_type),
3826 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003827 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003828 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003829 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003830 int opchar = get_op_char(oap->op_type);
3831 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003832 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3833
3834 // reverse what nv_replace() did
3835 if (nchar == REPLACE_CR_NCHAR)
3836 nchar = CAR;
3837 else if (nchar == REPLACE_NL_NCHAR)
3838 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003839
3840 if (opchar == 'g' && extra_opchar == '@')
3841 // also repeat the count for 'operatorfunc'
3842 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3843 cap->count0, opchar, extra_opchar, nchar);
3844 else
3845 prep_redo(oap->regname, 0L, NUL, 'v',
3846 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003847 }
3848 if (!redo_VIsual_busy)
3849 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003850 redo_VIsual.rv_mode = resel_VIsual_mode;
3851 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3852 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3853 redo_VIsual.rv_count = cap->count0;
3854 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003855 }
3856 }
3857
3858 // oap->inclusive defaults to TRUE.
3859 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3860 // FALSE. This makes "d}P" and "v}dP" work the same.
3861 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3862 oap->inclusive = TRUE;
3863 if (VIsual_mode == 'V')
3864 oap->motion_type = MLINE;
3865 else
3866 {
3867 oap->motion_type = MCHAR;
3868 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3869 && (include_line_break || !virtual_op))
3870 {
3871 oap->inclusive = FALSE;
3872 // Try to include the newline, unless it's an operator
3873 // that works on lines only.
3874 if (*p_sel != 'o'
3875 && !op_on_lines(oap->op_type)
3876 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3877 {
3878 ++oap->end.lnum;
3879 oap->end.col = 0;
3880 oap->end.coladd = 0;
3881 ++oap->line_count;
3882 }
3883 }
3884 }
3885
3886 redo_VIsual_busy = FALSE;
3887
3888 // Switch Visual off now, so screen updating does
3889 // not show inverted text when the screen is redrawn.
3890 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3891 // no screen redraw, so it is done here to remove the inverted
3892 // part.
3893 if (!gui_yank)
3894 {
3895 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003896 setmouse();
3897 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003898 may_clear_cmdline();
3899 if ((oap->op_type == OP_YANK
3900 || oap->op_type == OP_COLON
3901 || oap->op_type == OP_FUNCTION
3902 || oap->op_type == OP_FILTER)
3903 && oap->motion_force == NUL)
3904 {
3905#ifdef FEAT_LINEBREAK
3906 // make sure redrawing is correct
3907 curwin->w_p_lbr = lbr_saved;
3908#endif
3909 redraw_curbuf_later(INVERTED);
3910 }
3911 }
3912 }
3913
3914 // Include the trailing byte of a multi-byte char.
3915 if (has_mbyte && oap->inclusive)
3916 {
3917 int l;
3918
3919 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3920 if (l > 1)
3921 oap->end.col += l - 1;
3922 }
3923 curwin->w_set_curswant = TRUE;
3924
3925 // oap->empty is set when start and end are the same. The inclusive
3926 // flag affects this too, unless yanking and the end is on a NUL.
3927 oap->empty = (oap->motion_type == MCHAR
3928 && (!oap->inclusive
3929 || (oap->op_type == OP_YANK
3930 && gchar_pos(&oap->end) == NUL))
3931 && EQUAL_POS(oap->start, oap->end)
3932 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3933 // For delete, change and yank, it's an error to operate on an
3934 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3935 empty_region_error = (oap->empty
3936 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3937
3938 // Force a redraw when operating on an empty Visual region, when
3939 // 'modifiable is off or creating a fold.
3940 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3941#ifdef FEAT_FOLDING
3942 || oap->op_type == OP_FOLD
3943#endif
3944 ))
3945 {
3946#ifdef FEAT_LINEBREAK
3947 curwin->w_p_lbr = lbr_saved;
3948#endif
3949 redraw_curbuf_later(INVERTED);
3950 }
3951
3952 // If the end of an operator is in column one while oap->motion_type
3953 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3954 // character in the previous line. If op_start is on or before the
3955 // first non-blank in the line, the operator becomes linewise
3956 // (strange, but that's the way vi does it).
3957 if ( oap->motion_type == MCHAR
3958 && oap->inclusive == FALSE
3959 && !(cap->retval & CA_NO_ADJ_OP_END)
3960 && oap->end.col == 0
3961 && (!oap->is_VIsual || *p_sel == 'o')
3962 && !oap->block_mode
3963 && oap->line_count > 1)
3964 {
3965 oap->end_adjusted = TRUE; // remember that we did this
3966 --oap->line_count;
3967 --oap->end.lnum;
3968 if (inindent(0))
3969 oap->motion_type = MLINE;
3970 else
3971 {
3972 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3973 if (oap->end.col)
3974 {
3975 --oap->end.col;
3976 oap->inclusive = TRUE;
3977 }
3978 }
3979 }
3980 else
3981 oap->end_adjusted = FALSE;
3982
3983 switch (oap->op_type)
3984 {
3985 case OP_LSHIFT:
3986 case OP_RSHIFT:
3987 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3988 auto_format(FALSE, TRUE);
3989 break;
3990
3991 case OP_JOIN_NS:
3992 case OP_JOIN:
3993 if (oap->line_count < 2)
3994 oap->line_count = 2;
3995 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3996 curbuf->b_ml.ml_line_count)
3997 beep_flush();
3998 else
3999 {
4000 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4001 TRUE, TRUE, TRUE);
4002 auto_format(FALSE, TRUE);
4003 }
4004 break;
4005
4006 case OP_DELETE:
4007 VIsual_reselect = FALSE; // don't reselect now
4008 if (empty_region_error)
4009 {
4010 vim_beep(BO_OPER);
4011 CancelRedo();
4012 }
4013 else
4014 {
4015 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004016 // save cursor line for undo if it wasn't saved yet
4017 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4018 && u_save_cursor() == OK)
4019 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004020 }
4021 break;
4022
4023 case OP_YANK:
4024 if (empty_region_error)
4025 {
4026 if (!gui_yank)
4027 {
4028 vim_beep(BO_OPER);
4029 CancelRedo();
4030 }
4031 }
4032 else
4033 {
4034#ifdef FEAT_LINEBREAK
4035 curwin->w_p_lbr = lbr_saved;
4036#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004037 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004038 (void)op_yank(oap, FALSE, !gui_yank);
4039 }
4040 check_cursor_col();
4041 break;
4042
4043 case OP_CHANGE:
4044 VIsual_reselect = FALSE; // don't reselect now
4045 if (empty_region_error)
4046 {
4047 vim_beep(BO_OPER);
4048 CancelRedo();
4049 }
4050 else
4051 {
4052 // This is a new edit command, not a restart. Need to
4053 // remember it to make 'insertmode' work with mappings for
4054 // Visual mode. But do this only once and not when typed and
4055 // 'insertmode' isn't set.
4056 if (p_im || !KeyTyped)
4057 restart_edit_save = restart_edit;
4058 else
4059 restart_edit_save = 0;
4060 restart_edit = 0;
4061#ifdef FEAT_LINEBREAK
4062 // Restore linebreak, so that when the user edits it looks as
4063 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004064 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004065#endif
4066 // Reset finish_op now, don't want it set inside edit().
4067 finish_op = FALSE;
4068 if (op_change(oap)) // will call edit()
4069 cap->retval |= CA_COMMAND_BUSY;
4070 if (restart_edit == 0)
4071 restart_edit = restart_edit_save;
4072 }
4073 break;
4074
4075 case OP_FILTER:
4076 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4077 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4078 else
4079 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4080 // FALLTHROUGH
4081
4082 case OP_INDENT:
4083 case OP_COLON:
4084
4085#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4086 // If 'equalprg' is empty, do the indenting internally.
4087 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4088 {
4089# ifdef FEAT_LISP
4090 if (curbuf->b_p_lisp)
4091 {
4092 op_reindent(oap, get_lisp_indent);
4093 break;
4094 }
4095# endif
4096# ifdef FEAT_CINDENT
4097 op_reindent(oap,
4098# ifdef FEAT_EVAL
4099 *curbuf->b_p_inde != NUL ? get_expr_indent :
4100# endif
4101 get_c_indent);
4102 break;
4103# endif
4104 }
4105#endif
4106
4107 op_colon(oap);
4108 break;
4109
4110 case OP_TILDE:
4111 case OP_UPPER:
4112 case OP_LOWER:
4113 case OP_ROT13:
4114 if (empty_region_error)
4115 {
4116 vim_beep(BO_OPER);
4117 CancelRedo();
4118 }
4119 else
4120 op_tilde(oap);
4121 check_cursor_col();
4122 break;
4123
4124 case OP_FORMAT:
4125#if defined(FEAT_EVAL)
4126 if (*curbuf->b_p_fex != NUL)
4127 op_formatexpr(oap); // use expression
4128 else
4129#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004130 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004131 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004132 op_colon(oap); // use external command
4133 else
4134 op_format(oap, FALSE); // use internal function
4135 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004136 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004137 case OP_FORMAT2:
4138 op_format(oap, TRUE); // use internal function
4139 break;
4140
4141 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004142 {
4143 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4144
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004145#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004146 // Restore linebreak, so that when the user edits it looks as
4147 // before.
4148 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004149#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004150 // call 'operatorfunc'
4151 op_function(oap);
4152
4153 // Restore the info for redoing Visual mode, the function may
4154 // invoke another operator and unintentionally change it.
4155 redo_VIsual = save_redo_VIsual;
4156 break;
4157 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004158
4159 case OP_INSERT:
4160 case OP_APPEND:
4161 VIsual_reselect = FALSE; // don't reselect now
4162 if (empty_region_error)
4163 {
4164 vim_beep(BO_OPER);
4165 CancelRedo();
4166 }
4167 else
4168 {
4169 // This is a new edit command, not a restart. Need to
4170 // remember it to make 'insertmode' work with mappings for
4171 // Visual mode. But do this only once.
4172 restart_edit_save = restart_edit;
4173 restart_edit = 0;
4174#ifdef FEAT_LINEBREAK
4175 // Restore linebreak, so that when the user edits it looks as
4176 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004177 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004178#endif
4179 op_insert(oap, cap->count1);
4180#ifdef FEAT_LINEBREAK
4181 // Reset linebreak, so that formatting works correctly.
4182 curwin->w_p_lbr = FALSE;
4183#endif
4184
4185 // TODO: when inserting in several lines, should format all
4186 // the lines.
4187 auto_format(FALSE, TRUE);
4188
4189 if (restart_edit == 0)
4190 restart_edit = restart_edit_save;
4191 else
4192 cap->retval |= CA_COMMAND_BUSY;
4193 }
4194 break;
4195
4196 case OP_REPLACE:
4197 VIsual_reselect = FALSE; // don't reselect now
4198 if (empty_region_error)
4199 {
4200 vim_beep(BO_OPER);
4201 CancelRedo();
4202 }
4203 else
4204 {
4205#ifdef FEAT_LINEBREAK
4206 // Restore linebreak, so that when the user edits it looks as
4207 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004208 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004209#endif
4210 op_replace(oap, cap->nchar);
4211 }
4212 break;
4213
4214#ifdef FEAT_FOLDING
4215 case OP_FOLD:
4216 VIsual_reselect = FALSE; // don't reselect now
4217 foldCreate(oap->start.lnum, oap->end.lnum);
4218 break;
4219
4220 case OP_FOLDOPEN:
4221 case OP_FOLDOPENREC:
4222 case OP_FOLDCLOSE:
4223 case OP_FOLDCLOSEREC:
4224 VIsual_reselect = FALSE; // don't reselect now
4225 opFoldRange(oap->start.lnum, oap->end.lnum,
4226 oap->op_type == OP_FOLDOPEN
4227 || oap->op_type == OP_FOLDOPENREC,
4228 oap->op_type == OP_FOLDOPENREC
4229 || oap->op_type == OP_FOLDCLOSEREC,
4230 oap->is_VIsual);
4231 break;
4232
4233 case OP_FOLDDEL:
4234 case OP_FOLDDELREC:
4235 VIsual_reselect = FALSE; // don't reselect now
4236 deleteFold(oap->start.lnum, oap->end.lnum,
4237 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4238 break;
4239#endif
4240 case OP_NR_ADD:
4241 case OP_NR_SUB:
4242 if (empty_region_error)
4243 {
4244 vim_beep(BO_OPER);
4245 CancelRedo();
4246 }
4247 else
4248 {
4249 VIsual_active = TRUE;
4250#ifdef FEAT_LINEBREAK
4251 curwin->w_p_lbr = lbr_saved;
4252#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004253 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004254 VIsual_active = FALSE;
4255 }
4256 check_cursor_col();
4257 break;
4258 default:
4259 clearopbeep(oap);
4260 }
4261 virtual_op = MAYBE;
4262 if (!gui_yank)
4263 {
4264 // if 'sol' not set, go back to old column for some commands
4265 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4266 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4267 || oap->op_type == OP_DELETE))
4268 {
4269#ifdef FEAT_LINEBREAK
4270 curwin->w_p_lbr = FALSE;
4271#endif
4272 coladvance(curwin->w_curswant = old_col);
4273 }
4274 }
4275 else
4276 {
4277 curwin->w_cursor = old_cursor;
4278 }
4279 oap->block_mode = FALSE;
4280 clearop(oap);
4281 motion_force = NUL;
4282 }
4283#ifdef FEAT_LINEBREAK
4284 curwin->w_p_lbr = lbr_saved;
4285#endif
4286}