blob: 75619c570cd6e4d2dea637f90629201eed6bf19e [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 Moolenaarb5cf6c32014-08-16 18:36:43 +0200539 }
540 else
541 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100542 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200543 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200544 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100545 spaces -= off;
546 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200547 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200548 if (spaces < 0) // can happen when the cursor was moved
549 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200550
Bram Moolenaar964b3742019-05-24 18:54:09 +0200551 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (newp == NULL)
553 continue;
554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100555 // copy up to shifted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 mch_memmove(newp, oldp, (size_t)(offset));
557 oldp += offset;
558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200560 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200561 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100563 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200564 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 offset += s_len;
566
567 if (spaces && !bdp->is_short)
568 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 // insert post-padding
Bram Moolenaardac13472019-09-16 21:06:21 +0200570 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100571 // We're splitting a TAB, don't copy it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 oldp++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100573 // We allowed for that TAB, remember this now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 count++;
575 }
576
577 if (spaces > 0)
578 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000579 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
581 ml_replace(lnum, newp, FALSE);
582
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200583 if (b_insert)
584 // correct any text properties
585 inserted_bytes(lnum, startcol, s_len);
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (lnum == oap->end.lnum)
588 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100589 // Set "']" mark to the end of the block instead of the end of
590 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 curbuf->b_op_end.lnum = oap->end.lnum;
592 curbuf->b_op_end.col = offset;
593 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
597
598 State = oldstate;
599}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200602 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200604 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 */
606 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100607op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 int n;
610 linenr_T lnum;
611 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 char_u *newp, *oldp;
613 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
615 int did_yank = FALSE;
616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 return OK;
619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100620 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 if (oap->empty)
622 return u_save_cursor();
623
624 if (!curbuf->b_p_ma)
625 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200626 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 return FAIL;
628 }
629
630#ifdef FEAT_CLIPBOARD
631 adjust_clip_reg(&oap->regname);
632#endif
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (has_mbyte)
635 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
Bram Moolenaard04b7502010-07-08 22:27:55 +0200637 /*
638 * Imitate the strange Vi behaviour: If the delete spans more than one
639 * line and motion_type == MCHAR and the result is a blank line, make the
640 * delete linewise. Don't do this for the change command or Visual mode.
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000644 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100646 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 && oap->op_type == OP_DELETE)
648 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200649 ptr = ml_get(oap->end.lnum) + oap->end.col;
650 if (*ptr != NUL)
651 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 ptr = skipwhite(ptr);
653 if (*ptr == NUL && inindent(0))
654 oap->motion_type = MLINE;
655 }
656
Bram Moolenaard04b7502010-07-08 22:27:55 +0200657 /*
658 * Check for trying to delete (e.g. "D") in an empty line.
659 * Note: For the change operator it is ok.
660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if ( oap->motion_type == MCHAR
662 && oap->line_count == 1
663 && oap->op_type == OP_DELETE
664 && *ml_get(oap->start.lnum) == NUL)
665 {
666 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 * 'cpoptions' (Vi compatible).
669 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000670 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100671 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
672 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000673 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
675 beep_flush();
676 return OK;
677 }
678
Bram Moolenaard04b7502010-07-08 22:27:55 +0200679 /*
680 * Do a yank of whatever we're about to delete.
681 * If a yank register was specified, put the deleted text into that
682 * register. For the black hole register '_' don't yank anything.
683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (oap->regname != '_')
685 {
686 if (oap->regname != 0)
687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100688 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (!valid_yank_reg(oap->regname, TRUE))
690 {
691 beep_flush();
692 return OK;
693 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100694 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
695 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 did_yank = TRUE;
697 }
698
699 /*
700 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100701 * delete contains a line break, or when using a specific operator (Vi
702 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200703 * Use the register name from before adjust_clip_reg() may have
704 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100706 if (oap->motion_type == MLINE || oap->line_count > 1
707 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100709 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 if (op_yank(oap, TRUE, FALSE) == OK)
711 did_yank = TRUE;
712 }
713
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100714 // Yank into small delete register when no named register specified
715 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200716 if ((
717#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200718 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
719 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200720#endif
721 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 && oap->line_count == 1)
723 {
724 oap->regname = '-';
725 get_yank_register(oap->regname, TRUE);
726 if (op_yank(oap, TRUE, FALSE) == OK)
727 did_yank = TRUE;
728 oap->regname = 0;
729 }
730
731 /*
732 * If there's too much stuff to fit in the yank register, then get a
733 * confirmation before doing the delete. This is crude, but simple.
734 * And it avoids doing a delete of something we can't put back if we
735 * want.
736 */
737 if (!did_yank)
738 {
739 int msg_silent_save = msg_silent;
740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100741 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
743 msg_silent = msg_silent_save;
744 if (n != 'y')
745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100746 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 return FAIL;
748 }
749 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100750
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100751#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100752 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200753 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756
Bram Moolenaard04b7502010-07-08 22:27:55 +0200757 /*
758 * block mode delete
759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (oap->block_mode)
761 {
762 if (u_save((linenr_T)(oap->start.lnum - 1),
763 (linenr_T)(oap->end.lnum + 1)) == FAIL)
764 return FAIL;
765
766 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
767 {
768 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 continue;
771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100772 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (lnum == curwin->w_cursor.lnum)
774 {
775 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778
Bram Moolenaar8055d172019-05-17 22:57:26 +0200779 // "n" == number of chars deleted
780 // If we delete a TAB, it may be replaced by several characters.
781 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 n = bd.textlen - bd.startspaces - bd.endspaces;
783 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200784 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (newp == NULL)
786 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100787 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200790 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100792 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000794 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100795 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200797
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100798#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200799 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200800 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803
804 check_cursor_col();
805 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
806 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100809 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
811 if (oap->op_type == OP_CHANGE)
812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 // Delete the lines except the first one. Temporarily move the
814 // cursor to the next line. Save the current line number, if the
815 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (oap->line_count > 1)
817 {
818 lnum = curwin->w_cursor.lnum;
819 ++curwin->w_cursor.lnum;
820 del_lines((long)(oap->line_count - 1), TRUE);
821 curwin->w_cursor.lnum = lnum;
822 }
823 if (u_save_cursor() == FAIL)
824 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100825 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 beginline(BL_WHITE); // cursor on first non-white
828 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ai_col = curwin->w_cursor.col;
830 }
831 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100832 beginline(0); // cursor in column 0
833 truncate_line(FALSE); // delete the rest of the line
834 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 else
839 {
840 del_lines(oap->line_count, TRUE);
841 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 }
845 else
846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (virtual_op)
848 {
849 int endcol = 0;
850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100851 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (gchar_pos(&oap->start) == '\t')
853 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 return FAIL;
856 if (oap->line_count == 1)
857 endcol = getviscol2(oap->end.col, oap->end.coladd);
858 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
859 oap->start = curwin->w_cursor;
860 if (oap->line_count == 1)
861 {
862 coladvance(endcol);
863 oap->end.col = curwin->w_cursor.col;
864 oap->end.coladd = curwin->w_cursor.coladd;
865 curwin->w_cursor = oap->start;
866 }
867 }
868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (gchar_pos(&oap->end) == '\t'
871 && (int)oap->end.coladd < oap->inclusive)
872 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100873 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (u_save((linenr_T)(oap->end.lnum - 1),
875 (linenr_T)(oap->end.lnum + 1)) == FAIL)
876 return FAIL;
877 curwin->w_cursor = oap->end;
878 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
879 oap->end = curwin->w_cursor;
880 curwin->w_cursor = oap->start;
881 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100882 if (has_mbyte)
883 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100888 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 return FAIL;
890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100891 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100892 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 && oap->op_type == OP_CHANGE
894 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100895 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 display_dollar(oap->end.col - !oap->inclusive);
897
898 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (virtual_op)
901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 // fix up things for virtualedit-delete:
903 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 char_u *curline = ml_get_curline();
905 int len = (int)STRLEN(curline);
906
907 if (oap->end.coladd != 0
908 && (int)oap->end.col >= len - 1
909 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
910 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100911 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (n == 0 && oap->start.coladd != oap->end.coladd)
913 n = 1;
914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100915 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if (gchar_cursor() != NUL)
917 curwin->w_cursor.coladd = 0;
918 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200919 (void)del_bytes((long)n, !virtual_op,
920 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100922 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
924 pos_T curpos;
925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100926 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
928 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
929 return FAIL;
930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100933 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 ++curwin->w_cursor.lnum;
935 del_lines((long)(oap->line_count - 2), FALSE);
936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100937 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200938 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200939 curwin->w_cursor.col = 0;
940 (void)del_bytes((long)n, !virtual_op,
941 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100942 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200943 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
Bram Moolenaard0a1dee2020-12-20 13:07:48 +0100945 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947
948 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
949
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000950setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200951 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100953 if (oap->block_mode)
954 {
955 curbuf->b_op_end.lnum = oap->end.lnum;
956 curbuf->b_op_end.col = oap->start.col;
957 }
958 else
959 curbuf->b_op_end = oap->start;
960 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
963 return OK;
964}
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966/*
967 * Adjust end of operating area for ending on a multi-byte character.
968 * Used for deletion.
969 */
970 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100971mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 char_u *p;
974
975 if (oap->inclusive)
976 {
977 p = ml_get(oap->end.lnum);
978 oap->end.col += mb_tail_off(p, p + oap->end.col);
979 }
980}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar630afe82018-06-28 19:26:28 +0200982/*
983 * Replace the character under the cursor with "c".
984 * This takes care of multi-byte characters.
985 */
986 static void
987replace_character(int c)
988{
989 int n = State;
990
991 State = REPLACE;
992 ins_char(c);
993 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100994 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200995 dec_cursor();
996}
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998/*
999 * Replace a whole area with one character.
1000 */
1001 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001002op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 char_u *newp, *oldp;
1007 size_t oldlen;
1008 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001009 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001010 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001013 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001015 if (c == REPLACE_CR_NCHAR)
1016 {
1017 had_ctrl_v_cr = TRUE;
1018 c = CAR;
1019 }
1020 else if (c == REPLACE_NL_NCHAR)
1021 {
1022 had_ctrl_v_cr = TRUE;
1023 c = NL;
1024 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (has_mbyte)
1027 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 if (u_save((linenr_T)(oap->start.lnum - 1),
1030 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1031 return FAIL;
1032
1033 /*
1034 * block mode replace
1035 */
1036 if (oap->block_mode)
1037 {
1038 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1039 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1040 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001041 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1043 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001046 // n == number of extra chars required
1047 // If we split a TAB, it may be replaced by several characters.
1048 // Thus the number of characters may increase!
1049 // If the range starts in virtual space, count the initial
1050 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1052 {
1053 pos_T vpos;
1054
Bram Moolenaara1381de2009-11-03 15:44:21 +00001055 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 getvpos(&vpos, oap->start_vcol);
1057 bd.startspaces += vpos.coladd;
1058 n = bd.startspaces;
1059 }
1060 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001061 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 numc = oap->end_vcol - oap->start_vcol + 1;
1070 if (bd.is_short && (!virtual_op || bd.is_MAX))
1071 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001073 // A double-wide character can be replaced only up to half the
1074 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if ((*mb_char2cells)(c) > 1)
1076 {
1077 if ((numc & 1) && !bd.is_short)
1078 {
1079 ++bd.endspaces;
1080 ++n;
1081 }
1082 numc = numc / 2;
1083 }
1084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001085 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 num_chars = numc;
1087 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001088 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 n += numc - bd.textlen;
1090
1091 oldp = ml_get_curline();
1092 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001093 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (newp == NULL)
1095 continue;
1096 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001097 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 mch_memmove(newp, oldp, (size_t)bd.textcol);
1099 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001101 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001102 // insert replacement chars CHECK FOR ALLOCATED SPACE
1103 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1104 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001105 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001107 if (has_mbyte)
1108 {
1109 n = (int)STRLEN(newp);
1110 while (--num_chars >= 0)
1111 n += (*mb_char2bytes)(c, newp + n);
1112 }
1113 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001114 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001115 if (!bd.is_short)
1116 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001117 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001118 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001119 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001120 STRMOVE(newp + STRLEN(newp), oldp);
1121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001125 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001126 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001127 if (after_p != NULL)
1128 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001130 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001132 if (after_p != NULL)
1133 {
1134 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1135 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1136 oap->end.lnum++;
1137 vim_free(after_p);
1138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 }
1141 else
1142 {
1143 /*
1144 * MCHAR and MLINE motion replace.
1145 */
1146 if (oap->motion_type == MLINE)
1147 {
1148 oap->start.col = 0;
1149 curwin->w_cursor.col = 0;
1150 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1151 if (oap->end.col)
1152 --oap->end.col;
1153 }
1154 else if (!oap->inclusive)
1155 dec(&(oap->end));
1156
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001157 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 n = gchar_cursor();
1160 if (n != NUL)
1161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001164 // This is slow, but it handles replacing a single-byte
1165 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001166 if (curwin->w_cursor.lnum == oap->end.lnum)
1167 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001168 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
1170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (n == TAB)
1173 {
1174 int end_vcol = 0;
1175
1176 if (curwin->w_cursor.lnum == oap->end.lnum)
1177 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001178 // oap->end has to be recalculated when
1179 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 end_vcol = getviscol2(oap->end.col,
1181 oap->end.coladd);
1182 }
1183 coladvance_force(getviscol());
1184 if (curwin->w_cursor.lnum == oap->end.lnum)
1185 getvpos(&oap->end, end_vcol);
1186 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001187 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1191 {
1192 int virtcols = oap->end.coladd;
1193
1194 if (curwin->w_cursor.lnum == oap->start.lnum
1195 && oap->start.col == oap->end.col && oap->start.coladd)
1196 virtcols -= oap->start.coladd;
1197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001198 // oap->end has been trimmed so it's effectively inclusive;
1199 // as a result an extra +1 must be counted so we don't
1200 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1202 curwin->w_cursor.col -= (virtcols + 1);
1203 for (; virtcols >= 0; virtcols--)
1204 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001205 if ((*mb_char2len)(c) > 1)
1206 replace_character(c);
1207 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001208 PBYTE(curwin->w_cursor, c);
1209 if (inc(&curwin->w_cursor) == -1)
1210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001214 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (inc_cursor() == -1)
1216 break;
1217 }
1218 }
1219
1220 curwin->w_cursor = oap->start;
1221 check_cursor();
1222 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1223
Bram Moolenaare1004402020-10-24 20:49:43 +02001224 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001225 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001226 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001227 curbuf->b_op_start = oap->start;
1228 curbuf->b_op_end = oap->end;
1229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231 return OK;
1232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001234static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236/*
1237 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1238 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001240op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
1242 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001244 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 if (u_save((linenr_T)(oap->start.lnum - 1),
1247 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1248 return;
1249
1250 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001251 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
1253 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1254 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001255 int one_change;
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 block_prep(oap, &bd, pos.lnum, FALSE);
1258 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001259 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1260 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001261
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001262#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001263 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1266
Bram Moolenaar009b2592004-10-24 19:18:58 +00001267 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1268 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001270 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 if (did_change)
1275 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1276 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001277 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 if (oap->motion_type == MLINE)
1280 {
1281 oap->start.col = 0;
1282 pos.col = 0;
1283 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1284 if (oap->end.col)
1285 --oap->end.col;
1286 }
1287 else if (!oap->inclusive)
1288 dec(&(oap->end));
1289
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001290 if (pos.lnum == oap->end.lnum)
1291 did_change = swapchars(oap->op_type, &pos,
1292 oap->end.col - pos.col + 1);
1293 else
1294 for (;;)
1295 {
1296 did_change |= swapchars(oap->op_type, &pos,
1297 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1298 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001299 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001300 break;
1301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 if (did_change)
1303 {
1304 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1305 0L);
1306#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001307 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
1309 char_u *ptr;
1310 int count;
1311
1312 pos = oap->start;
1313 while (pos.lnum < oap->end.lnum)
1314 {
1315 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001316 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001317 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001319 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 pos.col = 0;
1321 pos.lnum++;
1322 }
1323 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1324 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001325 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001327 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329#endif
1330 }
1331 }
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001334 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaare1004402020-10-24 20:49:43 +02001337 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001338 {
1339 // Set '[ and '] marks.
1340 curbuf->b_op_start = oap->start;
1341 curbuf->b_op_end = oap->end;
1342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
1344 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001345 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001346 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347}
1348
1349/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001350 * Invoke swapchar() on "length" bytes at position "pos".
1351 * "pos" is advanced to just after the changed characters.
1352 * "length" is rounded up to include the whole last multi-byte character.
1353 * Also works correctly when the number of bytes changes.
1354 * Returns TRUE if some character was changed.
1355 */
1356 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001357swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001358{
1359 int todo;
1360 int did_change = 0;
1361
1362 for (todo = length; todo > 0; --todo)
1363 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001364 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001365 {
1366 int len = (*mb_ptr2len)(ml_get_pos(pos));
1367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001368 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001369 if (len > 0)
1370 todo -= len - 1;
1371 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001372 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001373 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001374 break;
1375 }
1376 return did_change;
1377}
1378
1379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 * If op_type == OP_UPPER: make uppercase,
1381 * if op_type == OP_LOWER: make lowercase,
1382 * if op_type == OP_ROT13: do rot13 encoding,
1383 * else swap case of character at 'pos'
1384 * returns TRUE when something actually changed.
1385 */
1386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001387swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 int c;
1390 int nc;
1391
1392 c = gchar_pos(pos);
1393
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001394 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (c >= 0x80 && op_type == OP_ROT13)
1396 return FALSE;
1397
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001398 if (op_type == OP_UPPER && c == 0xdf
1399 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001400 {
1401 pos_T sp = curwin->w_cursor;
1402
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001403 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001404 curwin->w_cursor = *pos;
1405 del_char(FALSE);
1406 ins_char('S');
1407 ins_char('S');
1408 curwin->w_cursor = sp;
1409 inc(pos);
1410 }
1411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001412 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 nc = c;
1415 if (MB_ISLOWER(c))
1416 {
1417 if (op_type == OP_ROT13)
1418 nc = ROT13(c, 'a');
1419 else if (op_type != OP_LOWER)
1420 nc = MB_TOUPPER(c);
1421 }
1422 else if (MB_ISUPPER(c))
1423 {
1424 if (op_type == OP_ROT13)
1425 nc = ROT13(c, 'A');
1426 else if (op_type != OP_UPPER)
1427 nc = MB_TOLOWER(c);
1428 }
1429 if (nc != c)
1430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1432 {
1433 pos_T sp = curwin->w_cursor;
1434
1435 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001437 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 ins_char(nc);
1439 curwin->w_cursor = sp;
1440 }
1441 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001442 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 return TRUE;
1444 }
1445 return FALSE;
1446}
1447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448/*
1449 * op_insert - Insert and append operators for Visual mode.
1450 */
1451 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001452op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 long ins_len, pre_textlen = 0;
1455 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001456 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 struct block_def bd;
1458 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001459 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001460 pos_T start_insert;
1461 // offset when cursor was moved in insert mode
1462 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001464 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 curwin->w_cursor.lnum = oap->start.lnum;
1469 update_screen(INVERTED);
1470
1471 if (oap->block_mode)
1472 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001473 // When 'virtualedit' is used, need to insert the extra spaces before
1474 // doing block_prep(). When only "block" is used, virtual edit is
1475 // already disabled, but still need it when calling
1476 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001477 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
1478 // state for the current buffer. To override that state, we need to
1479 // set the buffer-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (curwin->w_cursor.coladd > 0)
1481 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02001482 int old_ve_flags = curbuf->b_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (u_save_cursor() == FAIL)
1485 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001486
1487 curbuf->b_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 coladvance_force(oap->op_type == OP_APPEND
1489 ? oap->end_vcol + 1 : getviscol());
1490 if (oap->op_type == OP_APPEND)
1491 --curwin->w_cursor.col;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001492 curbuf->b_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001494 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001496 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001497 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (oap->op_type == OP_APPEND)
1501 firstline += bd.textlen;
1502 pre_textlen = (long)STRLEN(firstline);
1503 }
1504
1505 if (oap->op_type == OP_APPEND)
1506 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001507 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001509 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 curwin->w_set_curswant = TRUE;
1511 while (*ml_get_cursor() != NUL
1512 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1513 ++curwin->w_cursor.col;
1514 if (bd.is_short && !bd.is_MAX)
1515 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001516 // First line was too short, make it longer and adjust the
1517 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if (u_save_cursor() == FAIL)
1519 return;
1520 for (i = 0; i < bd.endspaces; ++i)
1521 ins_char(' ');
1522 bd.textlen += bd.endspaces;
1523 }
1524 }
1525 else
1526 {
1527 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001528 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001530 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001531 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 && oap->start_vcol != oap->end_vcol)
1533 inc_cursor();
1534 }
1535 }
1536
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001537 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001538 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001539 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001541 // When a tab was inserted, and the characters in front of the tab
1542 // have been converted to a tab as well, the column of the cursor
1543 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001544 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001545 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001546 oap->start = curbuf->b_op_start_orig;
1547
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001548 // If user has moved off this line, we don't know what to do, so do
1549 // nothing.
1550 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001551 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return;
1553
1554 if (oap->block_mode)
1555 {
1556 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001557 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001558 size_t len;
1559 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001561 // If indent kicked in, the firstline might have changed
1562 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001563 ind_post = (colnr_T)getwhitecols_curline();
1564 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1565 {
1566 bd.textcol += ind_post - ind_pre;
1567 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001568 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001569 }
1570
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001571 // The user may have moved the cursor before inserting something, try
1572 // to adjust the block for that. But only do it, if the difference
1573 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001574 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1575 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001576 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001577 int t = getviscol2(curbuf->b_op_start_orig.col,
1578 curbuf->b_op_start_orig.coladd);
1579
1580 if (!bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001581 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001582 if (oap->op_type == OP_INSERT
1583 && oap->start.col + oap->start.coladd
1584 != curbuf->b_op_start_orig.col
1585 + curbuf->b_op_start_orig.coladd)
1586 {
1587 oap->start.col = curbuf->b_op_start_orig.col;
1588 pre_textlen -= t - oap->start_vcol;
1589 oap->start_vcol = t;
1590 }
1591 else if (oap->op_type == OP_APPEND
1592 && oap->end.col + oap->end.coladd
1593 >= curbuf->b_op_start_orig.col
1594 + curbuf->b_op_start_orig.coladd)
1595 {
1596 oap->start.col = curbuf->b_op_start_orig.col;
1597 // reset pre_textlen to the value of OP_INSERT
1598 pre_textlen += bd.textlen;
1599 pre_textlen -= t - oap->start_vcol;
1600 oap->start_vcol = t;
1601 oap->op_type = OP_INSERT;
1602 }
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001603 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001604 else if (bd.is_MAX && oap->op_type == OP_APPEND)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001606 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001607 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001608 pre_textlen -= t - oap->start_vcol;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001609 }
1610 }
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 /*
1613 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001614 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 * Don't do this when "$" used, end-of-line will have changed.
1616 */
1617 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1618 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1619 {
1620 if (oap->op_type == OP_APPEND)
1621 {
1622 pre_textlen += bd2.textlen - bd.textlen;
1623 if (bd2.endspaces)
1624 --bd2.textlen;
1625 }
1626 bd.textcol = bd2.textcol;
1627 bd.textlen = bd2.textlen;
1628 }
1629
1630 /*
1631 * Subsequent calls to ml_get() flush the firstline data - take a
1632 * copy of the required string.
1633 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001634 firstline = ml_get(oap->start.lnum);
1635 len = STRLEN(firstline);
1636 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001638 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001639 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001640 // account for pressing cursor in insert mode when '$' was used
1641 if (bd.is_MAX
1642 && (start_insert.lnum == Insstart.lnum
1643 && start_insert.col > Insstart.col))
1644 {
1645 offset = (start_insert.col - Insstart.col);
1646 add -= offset;
1647 if (oap->end_vcol > offset)
1648 oap->end_vcol -= (offset + 1);
1649 else
1650 // moved outside of the visual block, what to do?
1651 return;
1652 }
1653 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001654 if ((size_t)add > len)
1655 firstline += len; // short line, point to the NUL
1656 else
1657 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001658 if (pre_textlen >= 0 && (ins_len =
1659 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001661 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (ins_text != NULL)
1663 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001664 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (u_save(oap->start.lnum,
1666 (linenr_T)(oap->end.lnum + 1)) == OK)
1667 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1668 &bd);
1669
1670 curwin->w_cursor.col = oap->start.col;
1671 check_cursor();
1672 vim_free(ins_text);
1673 }
1674 }
1675 }
1676}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678/*
1679 * op_change - handle a change operation
1680 *
1681 * return TRUE if edit() returns because of a CTRL-O command
1682 */
1683 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001684op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 colnr_T l;
1687 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 long offset;
1689 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001690 long ins_len;
1691 long pre_textlen = 0;
1692 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 char_u *firstline;
1694 char_u *ins_text, *newp, *oldp;
1695 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
1697 l = oap->start.col;
1698 if (oap->motion_type == MLINE)
1699 {
1700 l = 0;
1701#ifdef FEAT_SMARTINDENT
1702 if (!p_paste && curbuf->b_p_si
1703# ifdef FEAT_CINDENT
1704 && !curbuf->b_p_cin
1705# endif
1706 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001707 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709 }
1710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001711 // First delete the text in the region. In an empty buffer only need to
1712 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1714 {
1715 if (u_save_cursor() == FAIL)
1716 return FALSE;
1717 }
1718 else if (op_delete(oap) == FAIL)
1719 return FALSE;
1720
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001721 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 && !virtual_op)
1723 inc_cursor();
1724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001725 // check for still on same line (<CR> in inserted text meaningless)
1726 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (oap->block_mode)
1728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (virtual_op && (curwin->w_cursor.coladd > 0
1731 || gchar_cursor() == NUL))
1732 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001733 firstline = ml_get(oap->start.lnum);
1734 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001735 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 bd.textcol = curwin->w_cursor.col;
1737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
1739#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1740 if (oap->motion_type == MLINE)
1741 fix_indent();
1742#endif
1743
1744 retval = edit(NUL, FALSE, (linenr_T)1);
1745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001747 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001749 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001751 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001753 // Auto-indenting may have changed the indent. If the cursor was past
1754 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001756 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001758 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001759
1760 pre_textlen += new_indent - pre_indent;
1761 bd.textcol += new_indent - pre_indent;
1762 }
1763
1764 ins_len = (long)STRLEN(firstline) - pre_textlen;
1765 if (ins_len > 0)
1766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001767 // Subsequent calls to ml_get() flush the firstline data - take a
1768 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001769 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001771 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1773 linenr++)
1774 {
1775 block_prep(oap, &bd, linenr, TRUE);
1776 if (!bd.is_short || virtual_op)
1777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 pos_T vpos;
1779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001780 // If the block starts in virtual space, count the
1781 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (bd.is_short)
1783 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001784 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
1787 else
1788 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001790 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (newp == NULL)
1792 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001793 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 mch_memmove(newp, oldp, (size_t)bd.textcol);
1795 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001796 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1799 offset += ins_len;
1800 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001801 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 ml_replace(linenr, newp, FALSE);
1803 }
1804 }
1805 check_cursor();
1806
1807 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1808 }
1809 vim_free(ins_text);
1810 }
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 return retval;
1814}
1815
1816/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001817 * When the cursor is on the NUL past the end of the line and it should not be
1818 * there move it left.
1819 */
1820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001821adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001822{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001823 unsigned int cur_ve_flags = get_ve_flags();
1824
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001825 if (curwin->w_cursor.col > 0
1826 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001827 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 && !(restart_edit || (State & INSERT)))
1829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001831 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001832
Gary Johnson53ba05b2021-07-26 22:19:10 +02001833 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001834 {
1835 colnr_T scol, ecol;
1836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001837 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001838 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1839 curwin->w_cursor.coladd = ecol - scol + 1;
1840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842}
1843
Bram Moolenaar81340392012-06-06 16:12:59 +02001844/*
1845 * If "process" is TRUE and the line begins with a comment leader (possibly
1846 * after some white space), return a pointer to the text after it. Put a boolean
1847 * value indicating whether the line ends with an unclosed comment in
1848 * "is_comment".
1849 * line - line to be processed,
1850 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001851 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001852 * include_space - whether to also skip space following the comment leader,
1853 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001854 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001855 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001856 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001857skip_comment(
1858 char_u *line,
1859 int process,
1860 int include_space,
1861 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001862{
1863 char_u *comment_flags = NULL;
1864 int lead_len;
1865 int leader_offset = get_last_leader_offset(line, &comment_flags);
1866
1867 *is_comment = FALSE;
1868 if (leader_offset != -1)
1869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001870 // Let's check whether the line ends with an unclosed comment.
1871 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001872 while (*comment_flags)
1873 {
1874 if (*comment_flags == COM_END
1875 || *comment_flags == ':')
1876 break;
1877 ++comment_flags;
1878 }
1879 if (*comment_flags != COM_END)
1880 *is_comment = TRUE;
1881 }
1882
1883 if (process == FALSE)
1884 return line;
1885
1886 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1887
1888 if (lead_len == 0)
1889 return line;
1890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001891 // Find:
1892 // - COM_END,
1893 // - colon,
1894 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001895 while (*comment_flags)
1896 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001897 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001898 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001899 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001900 ++comment_flags;
1901 }
1902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001903 // If we found a colon, it means that we are not processing a line
1904 // starting with a closing part of a three-part comment. That's good,
1905 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001906 if (*comment_flags == ':' || *comment_flags == NUL)
1907 line += lead_len;
1908
1909 return line;
1910}
Bram Moolenaar81340392012-06-06 16:12:59 +02001911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001913 * Join 'count' lines (minimal 2) at cursor position.
1914 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001915 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1916 * leaders should not be removed.
1917 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1918 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001920 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 */
1922 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001923do_join(
1924 long count,
1925 int insert_space,
1926 int save_undo,
1927 int use_formatoptions UNUSED,
1928 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001930 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001931 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001932 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001934 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001935 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001936 int endcurr1 = NUL;
1937 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001938 int currsize = 0; // size of the current line
1939 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001941 colnr_T col = 0;
1942 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001943 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001944 int remove_comments = (use_formatoptions == TRUE)
1945 && has_format_option(FO_REMOVE_COMS);
1946 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001947#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001948 int propcount = 0; // number of props over all joined lines
1949 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001952 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1953 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001956 // Allocate an array to store the number of spaces inserted before each
1957 // line. We will use it to pre-compute the length of the new line and the
1958 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001959 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001960 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001962 if (remove_comments)
1963 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001964 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001965 if (comments == NULL)
1966 {
1967 vim_free(spaces);
1968 return FAIL;
1969 }
1970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001973 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001974 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001975 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001977 for (t = 0; t < count; ++t)
1978 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001979 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001980#ifdef FEAT_PROP_POPUP
1981 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1982#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001983 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001984 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001985 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001986 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1987 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1988 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001989 if (remove_comments)
1990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001991 // We don't want to remove the comment leader if the
1992 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001993 if (t > 0 && prev_was_comment)
1994 {
1995
1996 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1997 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001998 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001999 curr = new_curr;
2000 }
2001 else
2002 curr = skip_comment(curr, FALSE, insert_space,
2003 &prev_was_comment);
2004 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002005
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002006 if (insert_space && t > 0)
2007 {
2008 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002009 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002010 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002011 && (!has_format_option(FO_MBYTE_JOIN)
2012 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2013 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002014 || (mb_ptr2char(curr) < 0x100
2015 && !(enc_utf8 && utf_eat_space(endcurr1)))
2016 || (endcurr1 < 0x100
2017 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002018 )
2019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002020 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002021 if (endcurr1 == ' ')
2022 endcurr1 = endcurr2;
2023 else
2024 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002025 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002026 if ( p_js
2027 && (endcurr1 == '.'
2028 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2029 && (endcurr1 == '?' || endcurr1 == '!'))))
2030 ++spaces[t];
2031 }
2032 }
2033 currsize = (int)STRLEN(curr);
2034 sumsize += currsize + spaces[t];
2035 endcurr1 = endcurr2 = NUL;
2036 if (insert_space && currsize > 0)
2037 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002038 if (has_mbyte)
2039 {
2040 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002041 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002042 endcurr1 = (*mb_ptr2char)(cend);
2043 if (cend > curr)
2044 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002045 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002046 endcurr2 = (*mb_ptr2char)(cend);
2047 }
2048 }
2049 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002050 {
2051 endcurr1 = *(curr + currsize - 1);
2052 if (currsize > 1)
2053 endcurr2 = *(curr + currsize - 2);
2054 }
2055 }
2056 line_breakcheck();
2057 if (got_int)
2058 {
2059 ret = FAIL;
2060 goto theend;
2061 }
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002064 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002067 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002068 newp_len = sumsize + 1;
2069#ifdef FEAT_PROP_POPUP
2070 newp_len += propcount * sizeof(textprop_T);
2071#endif
2072 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002073 if (newp == NULL)
2074 {
2075 ret = FAIL;
2076 goto theend;
2077 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002078 cend = newp + sumsize;
2079 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002081 /*
2082 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002083 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002084 *
2085 * Move marks from each deleted line to the joined line, adjusting the
2086 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2087 * should not really be a problem.
2088 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002089#ifdef FEAT_PROP_POPUP
2090 props_remaining = propcount;
2091#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002092 for (t = count - 1; ; --t)
2093 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002094 int spaces_removed;
2095
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002096 cend -= currsize;
2097 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002098
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002099 if (spaces[t] > 0)
2100 {
2101 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002102 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002103 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002104
2105 // If deleting more spaces than adding, the cursor moves no more than
2106 // what is added if it is inside these spaces.
2107 spaces_removed = (curr - curr_start) - spaces[t];
2108
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002109 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002110 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002111#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002112 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2113 curwin->w_cursor.lnum + t, t == count - 1,
2114 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002115#endif
2116
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002117 if (t == 0)
2118 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002119 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002120 if (remove_comments)
2121 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002122 if (insert_space && t > 1)
2123 curr = skipwhite(curr);
2124 currsize = (int)STRLEN(curr);
2125 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002126
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002127 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
Bram Moolenaare1004402020-10-24 20:49:43 +02002129 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002130 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002131 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002132 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002133 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002134 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002136 // Only report the change in the first line here, del_lines() will report
2137 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 changed_lines(curwin->w_cursor.lnum, currsize,
2139 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002141 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 * briefly, and then move it back. After del_lines() the cursor may
2143 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 */
2145 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002147 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 curwin->w_cursor.lnum = t;
2149
2150 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002151 * Set the cursor column:
2152 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002153 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002155 curwin->w_cursor.col =
2156 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 curwin->w_set_curswant = TRUE;
2161
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002162theend:
2163 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002164 if (remove_comments)
2165 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002166 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 * prepare a few things for block mode yank/delete/tilde
2171 *
2172 * for delete:
2173 * - textlen includes the first/last char to be (partly) deleted
2174 * - start/endspaces is the number of columns that are taken by the
2175 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002176 * deleted.
2177 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 * - textlen includes the first/last char to be wholly yanked
2179 * - start/endspaces is the number of columns of the first/last yanked char
2180 * that are to be yanked.
2181 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002183block_prep(
2184 oparg_T *oap,
2185 struct block_def *bdp,
2186 linenr_T lnum,
2187 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
2189 int incr = 0;
2190 char_u *pend;
2191 char_u *pstart;
2192 char_u *line;
2193 char_u *prev_pstart;
2194 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002195#ifdef FEAT_LINEBREAK
2196 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002198 // Avoid a problem with unwanted linebreaks in block mode.
2199 curwin->w_p_lbr = FALSE;
2200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 bdp->startspaces = 0;
2202 bdp->endspaces = 0;
2203 bdp->textlen = 0;
2204 bdp->start_vcol = 0;
2205 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 bdp->is_short = FALSE;
2207 bdp->is_oneChar = FALSE;
2208 bdp->pre_whitesp = 0;
2209 bdp->pre_whitesp_c = 0;
2210 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 bdp->start_char_vcols = 0;
2212
2213 line = ml_get(lnum);
2214 pstart = line;
2215 prev_pstart = line;
2216 while (bdp->start_vcol < oap->start_vcol && *pstart)
2217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002219 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002221 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 bdp->pre_whitesp += incr;
2224 bdp->pre_whitesp_c++;
2225 }
2226 else
2227 {
2228 bdp->pre_whitesp = 0;
2229 bdp->pre_whitesp_c = 0;
2230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002232 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002235 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (!is_del || oap->op_type == OP_APPEND)
2240 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2241 }
2242 else
2243 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // notice: this converts partly selected Multibyte characters to
2245 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2247 if (is_del && bdp->startspaces)
2248 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2249 pend = pstart;
2250 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (oap->op_type == OP_INSERT)
2255 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2256 else if (oap->op_type == OP_APPEND)
2257 {
2258 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2259 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2260 }
2261 else
2262 {
2263 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2264 if (is_del && oap->op_type != OP_LSHIFT)
2265 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002266 // just putting the sum of those two into
2267 // bdp->startspaces doesn't work for Visual replace,
2268 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 bdp->startspaces = bdp->start_char_vcols
2270 - (bdp->start_vcol - oap->start_vcol);
2271 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2272 }
2273 }
2274 }
2275 else
2276 {
2277 prev_pend = pend;
2278 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2279 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002280 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002282 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 bdp->end_vcol += incr;
2284 }
2285 if (bdp->end_vcol <= oap->end_vcol
2286 && (!is_del
2287 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002288 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002291 // Alternative: include spaces to fill up the block.
2292 // Disadvantage: can lead to trailing spaces when the line is
2293 // short where the text is put
2294 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (oap->op_type == OP_APPEND || virtual_op)
2296 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002297 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002299 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 else if (bdp->end_vcol > oap->end_vcol)
2302 {
2303 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2304 if (!is_del && bdp->endspaces)
2305 {
2306 bdp->endspaces = incr - bdp->endspaces;
2307 if (pend != pstart)
2308 pend = prev_pend;
2309 }
2310 }
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (is_del && bdp->startspaces)
2314 pstart = prev_pstart;
2315 bdp->textlen = (int)(pend - pstart);
2316 }
2317 bdp->textcol = (colnr_T) (pstart - line);
2318 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002319#ifdef FEAT_LINEBREAK
2320 curwin->w_p_lbr = lbr_saved;
2321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002325 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002327 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002328op_addsub(
2329 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002330 linenr_T Prenum1, // Amount of add/subtract
2331 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002333 pos_T pos;
2334 struct block_def bd;
2335 int change_cnt = 0;
2336 linenr_T amount = Prenum1;
2337
Bram Moolenaar6b731882018-11-16 20:54:47 +01002338 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002339 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002340 // the call to changed_lines().
2341#ifdef FEAT_FOLDING
2342 disable_fold_update++;
2343#endif
2344
Bram Moolenaard79e5502016-01-10 22:13:02 +01002345 if (!VIsual_active)
2346 {
2347 pos = curwin->w_cursor;
2348 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002349 {
2350#ifdef FEAT_FOLDING
2351 disable_fold_update--;
2352#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002353 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002354 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002355 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002356#ifdef FEAT_FOLDING
2357 disable_fold_update--;
2358#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002359 if (change_cnt)
2360 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2361 }
2362 else
2363 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002364 int one_change;
2365 int length;
2366 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002367
2368 if (u_save((linenr_T)(oap->start.lnum - 1),
2369 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002370 {
2371#ifdef FEAT_FOLDING
2372 disable_fold_update--;
2373#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002374 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002375 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002376
2377 pos = oap->start;
2378 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2379 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002381 {
2382 block_prep(oap, &bd, pos.lnum, FALSE);
2383 pos.col = bd.textcol;
2384 length = bd.textlen;
2385 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002386 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002387 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002388 curwin->w_cursor.col = 0;
2389 pos.col = 0;
2390 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2391 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002392 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002393 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002394 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002395 dec(&(oap->end));
2396 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2397 pos.col = 0;
2398 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002399 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002400 pos.col += oap->start.col;
2401 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002402 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002403 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002404 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002405 length = (int)STRLEN(ml_get(oap->end.lnum));
2406 if (oap->end.col >= length)
2407 oap->end.col = length - 1;
2408 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002409 }
2410 }
2411 one_change = do_addsub(oap->op_type, &pos, length, amount);
2412 if (one_change)
2413 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002414 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002415 if (change_cnt == 0)
2416 startpos = curbuf->b_op_start;
2417 ++change_cnt;
2418 }
2419
2420#ifdef FEAT_NETBEANS_INTG
2421 if (netbeans_active() && one_change)
2422 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002423 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002424
2425 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002426 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002427 netbeans_inserted(curbuf, pos.lnum, pos.col,
2428 &ptr[pos.col], length);
2429 }
2430#endif
2431 if (g_cmd && one_change)
2432 amount += Prenum1;
2433 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002434
2435#ifdef FEAT_FOLDING
2436 disable_fold_update--;
2437#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002438 if (change_cnt)
2439 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2440
2441 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002442 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002443 redraw_curbuf_later(INVERTED);
2444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // Set '[ mark if something changed. Keep the last end
2446 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002447 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002448 curbuf->b_op_start = startpos;
2449
2450 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002451 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002452 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002453 }
2454}
2455
2456/*
2457 * Add or subtract 'Prenum1' from a number in a line
2458 * op_type is OP_NR_ADD or OP_NR_SUB
2459 *
2460 * Returns TRUE if some character was changed.
2461 */
2462 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463do_addsub(
2464 int op_type,
2465 pos_T *pos,
2466 int length,
2467 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002468{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 int col;
2470 char_u *buf1;
2471 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002472 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2473 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002474 uvarnumber_T n;
2475 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 char_u *ptr;
2477 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002479 int do_hex;
2480 int do_oct;
2481 int do_bin;
2482 int do_alpha;
2483 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002486 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002487 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002488 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002489 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002490 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002491 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002492 pos_T startpos;
2493 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002494 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002496 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2497 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2498 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2499 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2500 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002502 if (virtual_active())
2503 {
2504 save_coladd = pos->coladd;
2505 pos->coladd = 0;
2506 }
2507
Bram Moolenaard79e5502016-01-10 22:13:02 +01002508 curwin->w_cursor = *pos;
2509 ptr = ml_get(pos->lnum);
2510 col = pos->col;
2511
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002512 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002513 goto theend;
2514
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 /*
2516 * First check if we are on a hexadecimal number, after the "0x".
2517 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002518 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002520 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002521 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002522 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002523 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002524 if (has_mbyte)
2525 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002526 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002527
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002528 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002529 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002530 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002531 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002532 if (has_mbyte)
2533 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002534 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002535
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002536 if ( do_bin
2537 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002538 && ! ((col > 0
2539 && (ptr[col] == 'X'
2540 || ptr[col] == 'x')
2541 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002542 && (!has_mbyte ||
2543 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002544 && vim_isxdigit(ptr[col + 1]))))
2545 {
2546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002547 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002548
Bram Moolenaard79e5502016-01-10 22:13:02 +01002549 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002550
2551 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002552 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002553 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002554 if (has_mbyte)
2555 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002556 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002557 }
2558
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002559 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002560 && col > 0
2561 && (ptr[col] == 'X'
2562 || ptr[col] == 'x')
2563 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002564 && (!has_mbyte ||
2565 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002566 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002567 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002568 && col > 0
2569 && (ptr[col] == 'B'
2570 || ptr[col] == 'b')
2571 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002572 && (!has_mbyte ||
2573 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002574 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002575 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002576 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002577 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002578 if (has_mbyte)
2579 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002580 }
2581 else
2582 {
2583 /*
2584 * Search forward and then backward to find the start of number.
2585 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002586 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002587
2588 while (ptr[col] != NUL
2589 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002590 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002591 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002592
2593 while (col > 0
2594 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002595 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002596 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002597 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002598 if (has_mbyte)
2599 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002600 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002601 }
2602 }
2603
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002604 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002605 {
2606 while (ptr[col] != NUL && length > 0
2607 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002608 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002609 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002610 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002611
2612 col += mb_len;
2613 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002614 }
2615
2616 if (length == 0)
2617 goto theend;
2618
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002619 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002620 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2621 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002622 {
2623 negative = TRUE;
2624 was_positive = FALSE;
2625 }
2626 }
2627
2628 /*
2629 * If a number was found, and saving for undo works, replace the number.
2630 */
2631 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002632 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002633 {
2634 beep_flush();
2635 goto theend;
2636 }
2637
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002638 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002639 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002640 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002641 if (op_type == OP_NR_SUB)
2642 {
2643 if (CharOrd(firstdigit) < Prenum1)
2644 {
2645 if (isupper(firstdigit))
2646 firstdigit = 'A';
2647 else
2648 firstdigit = 'a';
2649 }
2650 else
2651#ifdef EBCDIC
2652 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2653#else
2654 firstdigit -= Prenum1;
2655#endif
2656 }
2657 else
2658 {
2659 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2660 {
2661 if (isupper(firstdigit))
2662 firstdigit = 'Z';
2663 else
2664 firstdigit = 'z';
2665 }
2666 else
2667#ifdef EBCDIC
2668 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2669#else
2670 firstdigit += Prenum1;
2671#endif
2672 }
2673 curwin->w_cursor.col = col;
2674 if (!did_change)
2675 startpos = curwin->w_cursor;
2676 did_change = TRUE;
2677 (void)del_char(FALSE);
2678 ins_char(firstdigit);
2679 endpos = curwin->w_cursor;
2680 curwin->w_cursor.col = col;
2681 }
2682 else
2683 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002684 pos_T save_pos;
2685 int i;
2686
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002687 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002688 && (!has_mbyte ||
2689 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002690 && !visual
2691 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002694 --col;
2695 negative = TRUE;
2696 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002698 if (visual && VIsual_mode != 'V')
2699 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2700 ? (int)STRLEN(ptr) - col
2701 : length);
2702
2703 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002704 0 + (do_bin ? STR2NR_BIN : 0)
2705 + (do_oct ? STR2NR_OCT : 0)
2706 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002707 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002708
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002710 if (pre && negative)
2711 {
2712 ++col;
2713 --length;
2714 negative = FALSE;
2715 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002717 subtract = FALSE;
2718 if (op_type == OP_NR_SUB)
2719 subtract ^= TRUE;
2720 if (negative)
2721 subtract ^= TRUE;
2722
2723 oldn = n;
2724 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002725 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002726 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002727 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002729 if (!pre)
2730 {
2731 if (subtract)
2732 {
2733 if (n > oldn)
2734 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002735 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002736 negative ^= TRUE;
2737 }
2738 }
2739 else
2740 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002742 if (n < oldn)
2743 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002744 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002745 negative ^= TRUE;
2746 }
2747 }
2748 if (n == 0)
2749 negative = FALSE;
2750 }
2751
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002752 if (do_unsigned && negative)
2753 {
2754 if (subtract)
2755 // sticking at zero.
2756 n = (uvarnumber_T)0;
2757 else
2758 // sticking at 2^64 - 1.
2759 n = (uvarnumber_T)(-1);
2760 negative = FALSE;
2761 }
2762
Bram Moolenaard79e5502016-01-10 22:13:02 +01002763 if (visual && !was_positive && !negative && col > 0)
2764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002766 col--;
2767 length++;
2768 }
2769
2770 /*
2771 * Delete the old number.
2772 */
2773 curwin->w_cursor.col = col;
2774 if (!did_change)
2775 startpos = curwin->w_cursor;
2776 did_change = TRUE;
2777 todel = length;
2778 c = gchar_cursor();
2779 /*
2780 * Don't include the '-' in the length, only the length of the
2781 * part after it is kept the same.
2782 */
2783 if (c == '-')
2784 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002785
2786 save_pos = curwin->w_cursor;
2787 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002788 {
2789 if (c < 0x100 && isalpha(c))
2790 {
2791 if (isupper(c))
2792 hexupper = TRUE;
2793 else
2794 hexupper = FALSE;
2795 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002796 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002797 c = gchar_cursor();
2798 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002799 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002800
2801 /*
2802 * Prepare the leading characters in buf1[].
2803 * When there are many leading zeros it could be very long.
2804 * Allocate a bit too much.
2805 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002806 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002807 if (buf1 == NULL)
2808 goto theend;
2809 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002810 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002811 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002812 if (pre)
2813 {
2814 *ptr++ = '0';
2815 --length;
2816 }
2817 if (pre == 'b' || pre == 'B' ||
2818 pre == 'x' || pre == 'X')
2819 {
2820 *ptr++ = pre;
2821 --length;
2822 }
2823
2824 /*
2825 * Put the number characters in buf2[].
2826 */
2827 if (pre == 'b' || pre == 'B')
2828 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002829 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002830 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002833 for (bit = bits; bit > 0; bit--)
2834 if ((n >> (bit - 1)) & 0x1) break;
2835
2836 for (i = 0; bit > 0; bit--)
2837 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2838
2839 buf2[i] = '\0';
2840 }
2841 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002842 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002843 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002844 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002845 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002846 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002847 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002848 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002849 length -= (int)STRLEN(buf2);
2850
2851 /*
2852 * Adjust number of zeros to the new number of digits, so the
2853 * total length of the number remains the same.
2854 * Don't do this when
2855 * the result may look like an octal number.
2856 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002857 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002858 while (length-- > 0)
2859 *ptr++ = '0';
2860 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002861
Bram Moolenaard79e5502016-01-10 22:13:02 +01002862 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002863
2864 // Insert just after the first character to be removed, so that any
2865 // text properties will be adjusted. Then delete the old number
2866 // afterwards.
2867 save_pos = curwin->w_cursor;
2868 if (todel > 0)
2869 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002871 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002872
2873 // del_char() will also mark line needing displaying
2874 if (todel > 0)
2875 {
2876 int bytes_after = (int)STRLEN(ml_get_curline())
2877 - curwin->w_cursor.col;
2878
2879 // Delete the one character before the insert.
2880 curwin->w_cursor = save_pos;
2881 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002882 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2883 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002884 --todel;
2885 }
2886 while (todel-- > 0)
2887 (void)del_char(FALSE);
2888
Bram Moolenaard79e5502016-01-10 22:13:02 +01002889 endpos = curwin->w_cursor;
2890 if (did_change && curwin->w_cursor.col)
2891 --curwin->w_cursor.col;
2892 }
2893
Bram Moolenaare1004402020-10-24 20:49:43 +02002894 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002897 curbuf->b_op_start = startpos;
2898 curbuf->b_op_end = endpos;
2899 if (curbuf->b_op_end.col > 0)
2900 --curbuf->b_op_end.col;
2901 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002902
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002903theend:
2904 if (visual)
2905 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002906 else if (did_change)
2907 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002908 else if (virtual_active())
2909 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002910
Bram Moolenaard79e5502016-01-10 22:13:02 +01002911 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912}
2913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002915clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002917 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002921 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 *
2923 * "Words" are counted by looking for boundaries between non-space and
2924 * space characters. (it seems to produce results that match 'wc'.)
2925 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002926 * Return value is byte count; word count for the line is added to "*wc".
2927 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 *
2929 * The function will only examine the first "limit" characters in the
2930 * line, stopping if it encounters an end-of-line (NUL byte). In that
2931 * case, eol_size will be added to the character count to account for
2932 * the size of the EOL character.
2933 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002934 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002935line_count_info(
2936 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002937 varnumber_T *wc,
2938 varnumber_T *cc,
2939 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002940 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002942 varnumber_T i;
2943 varnumber_T words = 0;
2944 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 int is_word = 0;
2946
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002947 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 if (is_word)
2950 {
2951 if (vim_isspace(line[i]))
2952 {
2953 words++;
2954 is_word = 0;
2955 }
2956 }
2957 else if (!vim_isspace(line[i]))
2958 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002959 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002960 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
2962
2963 if (is_word)
2964 words++;
2965 *wc += words;
2966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002968 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002969 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002971 chars += eol_size;
2972 }
2973 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 return i;
2975}
2976
2977/*
2978 * Give some info about the position of the cursor (for "g CTRL-G").
2979 * In Visual mode, give some info about the selected region. (In this case,
2980 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002981 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 */
2983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002984cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002987 char_u buf1[50];
2988 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002990 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002991 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002992 varnumber_T byte_count_cursor = 0;
2993 varnumber_T char_count = 0;
2994 varnumber_T char_count_cursor = 0;
2995 varnumber_T word_count = 0;
2996 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002997 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002998 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 long line_count_selected = 0;
3000 pos_T min_pos, max_pos;
3001 oparg_T oparg;
3002 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 /*
3005 * Compute the length of the file in characters.
3006 */
3007 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3008 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003009 if (dict == NULL)
3010 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003011 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003012 return;
3013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015 else
3016 {
3017 if (get_fileformat(curbuf) == EOL_DOS)
3018 eol_size = 2;
3019 else
3020 eol_size = 1;
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (VIsual_active)
3023 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003024 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 {
3026 min_pos = VIsual;
3027 max_pos = curwin->w_cursor;
3028 }
3029 else
3030 {
3031 min_pos = curwin->w_cursor;
3032 max_pos = VIsual;
3033 }
3034 if (*p_sel == 'e' && max_pos.col > 0)
3035 --max_pos.col;
3036
3037 if (VIsual_mode == Ctrl_V)
3038 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003039#ifdef FEAT_LINEBREAK
3040 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003041 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003043 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003044 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003045 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 oparg.is_VIsual = 1;
3048 oparg.block_mode = TRUE;
3049 oparg.op_type = OP_NOP;
3050 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003051 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003052#ifdef FEAT_LINEBREAK
3053 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003054 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003055#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003056 if (curwin->w_curswant == MAXCOL)
3057 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if (oparg.end_vcol < oparg.start_vcol)
3060 {
3061 oparg.end_vcol += oparg.start_vcol;
3062 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3063 oparg.end_vcol -= oparg.start_vcol;
3064 }
3065 }
3066 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3070 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003071 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003072 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
3074 ui_breakcheck();
3075 if (got_int)
3076 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003077 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
3079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 if (VIsual_active
3082 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3083 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003084 char_u *s = NULL;
3085 long len = 0L;
3086
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 switch (VIsual_mode)
3088 {
3089 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003093 s = bd.textstart;
3094 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 break;
3096 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003097 s = ml_get(lnum);
3098 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 break;
3100 case 'v':
3101 {
3102 colnr_T start_col = (lnum == min_pos.lnum)
3103 ? min_pos.col : 0;
3104 colnr_T end_col = (lnum == max_pos.lnum)
3105 ? max_pos.col - start_col + 1 : MAXCOL;
3106
Bram Moolenaardef9e822004-12-31 20:58:58 +00003107 s = ml_get(lnum) + start_col;
3108 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110 break;
3111 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003112 if (s != NULL)
3113 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003114 byte_count_cursor += line_count_info(s, &word_count_cursor,
3115 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003116 if (lnum == curbuf->b_ml.ml_line_count
3117 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003118 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003119 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003120 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
3123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003125 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (lnum == curwin->w_cursor.lnum)
3127 {
3128 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003129 char_count_cursor += char_count;
3130 byte_count_cursor = byte_count +
3131 line_count_info(ml_get(lnum),
3132 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003133 (varnumber_T)(curwin->w_cursor.col + 1),
3134 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003137 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003138 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003139 &char_count, (varnumber_T)MAXCOL,
3140 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
3142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003143 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003144 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003145 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaared767a22016-01-03 22:49:16 +01003147 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003149 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003151 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3152 {
3153 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3154 &max_pos.col);
3155 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3156 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3157 }
3158 else
3159 buf1[0] = NUL;
3160
3161 if (char_count_cursor == byte_count_cursor
3162 && char_count == byte_count)
3163 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003164 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003165 buf1, line_count_selected,
3166 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003167 (varnumber_T)word_count_cursor,
3168 (varnumber_T)word_count,
3169 (varnumber_T)byte_count_cursor,
3170 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003171 else
3172 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003173 _("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 +01003174 buf1, line_count_selected,
3175 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003176 (varnumber_T)word_count_cursor,
3177 (varnumber_T)word_count,
3178 (varnumber_T)char_count_cursor,
3179 (varnumber_T)char_count,
3180 (varnumber_T)byte_count_cursor,
3181 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003184 {
3185 p = ml_get_curline();
3186 validate_virtcol();
3187 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3188 (int)curwin->w_virtcol + 1);
3189 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3190 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaared767a22016-01-03 22:49:16 +01003192 if (char_count_cursor == byte_count_cursor
3193 && char_count == byte_count)
3194 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003195 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003196 (char *)buf1, (char *)buf2,
3197 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003199 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3200 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003201 else
3202 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003203 _("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 +01003204 (char *)buf1, (char *)buf2,
3205 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003206 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003207 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3208 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3209 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003210 }
3211 }
3212
Bram Moolenaared767a22016-01-03 22:49:16 +01003213 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003214 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003215 {
3216 size_t len = STRLEN(IObuff);
3217
3218 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003219 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003220 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003221 if (dict == NULL)
3222 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003223 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003224 p = p_shm;
3225 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003226 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003227 p_shm = p;
3228 }
3229 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003230#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003231 if (dict != NULL)
3232 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003233 dict_add_number(dict, "words", word_count);
3234 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003235 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003236 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3237 byte_count_cursor);
3238 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3239 char_count_cursor);
3240 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3241 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003245
3246/*
3247 * Handle indent and format operators and visual mode ":".
3248 */
3249 static void
3250op_colon(oparg_T *oap)
3251{
3252 stuffcharReadbuff(':');
3253 if (oap->is_VIsual)
3254 stuffReadbuff((char_u *)"'<,'>");
3255 else
3256 {
3257 // Make the range look nice, so it can be repeated.
3258 if (oap->start.lnum == curwin->w_cursor.lnum)
3259 stuffcharReadbuff('.');
3260 else
3261 stuffnumReadbuff((long)oap->start.lnum);
3262 if (oap->end.lnum != oap->start.lnum)
3263 {
3264 stuffcharReadbuff(',');
3265 if (oap->end.lnum == curwin->w_cursor.lnum)
3266 stuffcharReadbuff('.');
3267 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3268 stuffcharReadbuff('$');
3269 else if (oap->start.lnum == curwin->w_cursor.lnum)
3270 {
3271 stuffReadbuff((char_u *)".+");
3272 stuffnumReadbuff((long)oap->line_count - 1);
3273 }
3274 else
3275 stuffnumReadbuff((long)oap->end.lnum);
3276 }
3277 }
3278 if (oap->op_type != OP_COLON)
3279 stuffReadbuff((char_u *)"!");
3280 if (oap->op_type == OP_INDENT)
3281 {
3282#ifndef FEAT_CINDENT
3283 if (*get_equalprg() == NUL)
3284 stuffReadbuff((char_u *)"indent");
3285 else
3286#endif
3287 stuffReadbuff(get_equalprg());
3288 stuffReadbuff((char_u *)"\n");
3289 }
3290 else if (oap->op_type == OP_FORMAT)
3291 {
3292 if (*curbuf->b_p_fp != NUL)
3293 stuffReadbuff(curbuf->b_p_fp);
3294 else if (*p_fp != NUL)
3295 stuffReadbuff(p_fp);
3296 else
3297 stuffReadbuff((char_u *)"fmt");
3298 stuffReadbuff((char_u *)"\n']");
3299 }
3300
3301 // do_cmdline() does the rest
3302}
3303
3304/*
3305 * Handle the "g@" operator: call 'operatorfunc'.
3306 */
3307 static void
3308op_function(oparg_T *oap UNUSED)
3309{
3310#ifdef FEAT_EVAL
3311 typval_T argv[2];
3312 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003313 pos_T orig_start = curbuf->b_op_start;
3314 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003315
3316 if (*p_opfunc == NUL)
3317 emsg(_("E774: 'operatorfunc' is empty"));
3318 else
3319 {
3320 // Set '[ and '] marks to text to be operated on.
3321 curbuf->b_op_start = oap->start;
3322 curbuf->b_op_end = oap->end;
3323 if (oap->motion_type != MLINE && !oap->inclusive)
3324 // Exclude the end position.
3325 decl(&curbuf->b_op_end);
3326
3327 argv[0].v_type = VAR_STRING;
3328 if (oap->block_mode)
3329 argv[0].vval.v_string = (char_u *)"block";
3330 else if (oap->motion_type == MLINE)
3331 argv[0].vval.v_string = (char_u *)"line";
3332 else
3333 argv[0].vval.v_string = (char_u *)"char";
3334 argv[1].v_type = VAR_UNKNOWN;
3335
3336 // Reset virtual_op so that 'virtualedit' can be changed in the
3337 // function.
3338 virtual_op = MAYBE;
3339
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003340 (void)call_func_noret(p_opfunc, 1, argv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003341
3342 virtual_op = save_virtual_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003343 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003344 {
3345 curbuf->b_op_start = orig_start;
3346 curbuf->b_op_end = orig_end;
3347 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003348 }
3349#else
3350 emsg(_("E775: Eval feature not available"));
3351#endif
3352}
3353
3354/*
3355 * Calculate start/end virtual columns for operating in block mode.
3356 */
3357 static void
3358get_op_vcol(
3359 oparg_T *oap,
3360 colnr_T redo_VIsual_vcol,
3361 int initial) // when TRUE adjust position for 'selectmode'
3362{
3363 colnr_T start, end;
3364
3365 if (VIsual_mode != Ctrl_V
3366 || (!initial && oap->end.col < curwin->w_width))
3367 return;
3368
3369 oap->block_mode = TRUE;
3370
3371 // prevent from moving onto a trail byte
3372 if (has_mbyte)
3373 mb_adjustpos(curwin->w_buffer, &oap->end);
3374
3375 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3376
3377 if (!redo_VIsual_busy)
3378 {
3379 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3380
3381 if (start < oap->start_vcol)
3382 oap->start_vcol = start;
3383 if (end > oap->end_vcol)
3384 {
3385 if (initial && *p_sel == 'e' && start >= 1
3386 && start - 1 >= oap->end_vcol)
3387 oap->end_vcol = start - 1;
3388 else
3389 oap->end_vcol = end;
3390 }
3391 }
3392
3393 // if '$' was used, get oap->end_vcol from longest line
3394 if (curwin->w_curswant == MAXCOL)
3395 {
3396 curwin->w_cursor.col = MAXCOL;
3397 oap->end_vcol = 0;
3398 for (curwin->w_cursor.lnum = oap->start.lnum;
3399 curwin->w_cursor.lnum <= oap->end.lnum;
3400 ++curwin->w_cursor.lnum)
3401 {
3402 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3403 if (end > oap->end_vcol)
3404 oap->end_vcol = end;
3405 }
3406 }
3407 else if (redo_VIsual_busy)
3408 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3409 // Correct oap->end.col and oap->start.col to be the
3410 // upper-left and lower-right corner of the block area.
3411 //
3412 // (Actually, this does convert column positions into character
3413 // positions)
3414 curwin->w_cursor.lnum = oap->end.lnum;
3415 coladvance(oap->end_vcol);
3416 oap->end = curwin->w_cursor;
3417
3418 curwin->w_cursor = oap->start;
3419 coladvance(oap->start_vcol);
3420 oap->start = curwin->w_cursor;
3421}
3422
3423/*
3424 * Handle an operator after Visual mode or when the movement is finished.
3425 * "gui_yank" is true when yanking text for the clipboard.
3426 */
3427 void
3428do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3429{
3430 oparg_T *oap = cap->oap;
3431 pos_T old_cursor;
3432 int empty_region_error;
3433 int restart_edit_save;
3434#ifdef FEAT_LINEBREAK
3435 int lbr_saved = curwin->w_p_lbr;
3436#endif
3437
3438 // The visual area is remembered for redo
3439 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3440 static linenr_T redo_VIsual_line_count; // number of lines
3441 static colnr_T redo_VIsual_vcol; // number of cols or end column
3442 static long redo_VIsual_count; // count for Visual operator
3443 static int redo_VIsual_arg; // extra argument
3444 int include_line_break = FALSE;
3445
3446#if defined(FEAT_CLIPBOARD)
3447 // Yank the visual area into the GUI selection register before we operate
3448 // on it and lose it forever.
3449 // Don't do it if a specific register was specified, so that ""x"*P works.
3450 // This could call do_pending_operator() recursively, but that's OK
3451 // because gui_yank will be TRUE for the nested call.
3452 if ((clip_star.available || clip_plus.available)
3453 && oap->op_type != OP_NOP
3454 && !gui_yank
3455 && VIsual_active
3456 && !redo_VIsual_busy
3457 && oap->regname == 0)
3458 clip_auto_select();
3459#endif
3460 old_cursor = curwin->w_cursor;
3461
3462 // If an operation is pending, handle it...
3463 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3464 {
3465 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3466 // for the clipboard.
3467 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3468
3469#ifdef FEAT_LINEBREAK
3470 // Avoid a problem with unwanted linebreaks in block mode.
3471 if (curwin->w_p_lbr)
3472 curwin->w_valid &= ~VALID_VIRTCOL;
3473 curwin->w_p_lbr = FALSE;
3474#endif
3475 oap->is_VIsual = VIsual_active;
3476 if (oap->motion_force == 'V')
3477 oap->motion_type = MLINE;
3478 else if (oap->motion_force == 'v')
3479 {
3480 // If the motion was linewise, "inclusive" will not have been set.
3481 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3482 if (oap->motion_type == MLINE)
3483 oap->inclusive = FALSE;
3484 // If the motion already was characterwise, toggle "inclusive"
3485 else if (oap->motion_type == MCHAR)
3486 oap->inclusive = !oap->inclusive;
3487 oap->motion_type = MCHAR;
3488 }
3489 else if (oap->motion_force == Ctrl_V)
3490 {
3491 // Change line- or characterwise motion into Visual block mode.
3492 if (!VIsual_active)
3493 {
3494 VIsual_active = TRUE;
3495 VIsual = oap->start;
3496 }
3497 VIsual_mode = Ctrl_V;
3498 VIsual_select = FALSE;
3499 VIsual_reselect = FALSE;
3500 }
3501
3502 // Only redo yank when 'y' flag is in 'cpoptions'.
3503 // Never redo "zf" (define fold).
3504 if ((redo_yank || oap->op_type != OP_YANK)
3505 && ((!VIsual_active || oap->motion_force)
3506 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003507 || (VIsual_active
3508 && (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3509 && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003510 && cap->cmdchar != 'D'
3511#ifdef FEAT_FOLDING
3512 && oap->op_type != OP_FOLD
3513 && oap->op_type != OP_FOLDOPEN
3514 && oap->op_type != OP_FOLDOPENREC
3515 && oap->op_type != OP_FOLDCLOSE
3516 && oap->op_type != OP_FOLDCLOSEREC
3517 && oap->op_type != OP_FOLDDEL
3518 && oap->op_type != OP_FOLDDELREC
3519#endif
3520 )
3521 {
3522 prep_redo(oap->regname, cap->count0,
3523 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3524 oap->motion_force, cap->cmdchar, cap->nchar);
3525 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3526 {
3527 // If 'cpoptions' does not contain 'r', insert the search
3528 // pattern to really repeat the same command.
3529 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3530 AppendToRedobuffLit(cap->searchbuf, -1);
3531 AppendToRedobuff(NL_STR);
3532 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003533 else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003534 {
3535 // do_cmdline() has stored the first typed line in
3536 // "repeat_cmdline". When several lines are typed repeating
3537 // won't be possible.
3538 if (repeat_cmdline == NULL)
3539 ResetRedobuff();
3540 else
3541 {
3542 AppendToRedobuffLit(repeat_cmdline, -1);
3543 AppendToRedobuff(NL_STR);
3544 VIM_CLEAR(repeat_cmdline);
3545 }
3546 }
3547 }
3548
3549 if (redo_VIsual_busy)
3550 {
3551 // Redo of an operation on a Visual area. Use the same size from
3552 // redo_VIsual_line_count and redo_VIsual_vcol.
3553 oap->start = curwin->w_cursor;
3554 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3555 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3556 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3557 VIsual_mode = redo_VIsual_mode;
3558 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3559 {
3560 if (VIsual_mode == 'v')
3561 {
3562 if (redo_VIsual_line_count <= 1)
3563 {
3564 validate_virtcol();
3565 curwin->w_curswant =
3566 curwin->w_virtcol + redo_VIsual_vcol - 1;
3567 }
3568 else
3569 curwin->w_curswant = redo_VIsual_vcol;
3570 }
3571 else
3572 {
3573 curwin->w_curswant = MAXCOL;
3574 }
3575 coladvance(curwin->w_curswant);
3576 }
3577 cap->count0 = redo_VIsual_count;
3578 if (redo_VIsual_count != 0)
3579 cap->count1 = redo_VIsual_count;
3580 else
3581 cap->count1 = 1;
3582 }
3583 else if (VIsual_active)
3584 {
3585 if (!gui_yank)
3586 {
3587 // Save the current VIsual area for '< and '> marks, and "gv"
3588 curbuf->b_visual.vi_start = VIsual;
3589 curbuf->b_visual.vi_end = curwin->w_cursor;
3590 curbuf->b_visual.vi_mode = VIsual_mode;
3591 restore_visual_mode();
3592 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3593# ifdef FEAT_EVAL
3594 curbuf->b_visual_mode_eval = VIsual_mode;
3595# endif
3596 }
3597
3598 // In Select mode, a linewise selection is operated upon like a
3599 // characterwise selection.
3600 // Special case: gH<Del> deletes the last line.
3601 if (VIsual_select && VIsual_mode == 'V'
3602 && cap->oap->op_type != OP_DELETE)
3603 {
3604 if (LT_POS(VIsual, curwin->w_cursor))
3605 {
3606 VIsual.col = 0;
3607 curwin->w_cursor.col =
3608 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3609 }
3610 else
3611 {
3612 curwin->w_cursor.col = 0;
3613 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3614 }
3615 VIsual_mode = 'v';
3616 }
3617 // If 'selection' is "exclusive", backup one character for
3618 // charwise selections.
3619 else if (VIsual_mode == 'v')
3620 include_line_break = unadjust_for_sel();
3621
3622 oap->start = VIsual;
3623 if (VIsual_mode == 'V')
3624 {
3625 oap->start.col = 0;
3626 oap->start.coladd = 0;
3627 }
3628 }
3629
3630 // Set oap->start to the first position of the operated text, oap->end
3631 // to the end of the operated text. w_cursor is equal to oap->start.
3632 if (LT_POS(oap->start, curwin->w_cursor))
3633 {
3634#ifdef FEAT_FOLDING
3635 // Include folded lines completely.
3636 if (!VIsual_active)
3637 {
3638 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3639 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003640 if ((curwin->w_cursor.col > 0 || oap->inclusive
3641 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003642 && hasFolding(curwin->w_cursor.lnum, NULL,
3643 &curwin->w_cursor.lnum))
3644 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3645 }
3646#endif
3647 oap->end = curwin->w_cursor;
3648 curwin->w_cursor = oap->start;
3649
3650 // w_virtcol may have been updated; if the cursor goes back to its
3651 // previous position w_virtcol becomes invalid and isn't updated
3652 // automatically.
3653 curwin->w_valid &= ~VALID_VIRTCOL;
3654 }
3655 else
3656 {
3657#ifdef FEAT_FOLDING
3658 // Include folded lines completely.
3659 if (!VIsual_active && oap->motion_type == MLINE)
3660 {
3661 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3662 NULL))
3663 curwin->w_cursor.col = 0;
3664 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3665 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3666 }
3667#endif
3668 oap->end = oap->start;
3669 oap->start = curwin->w_cursor;
3670 }
3671
3672 // Just in case lines were deleted that make the position invalid.
3673 check_pos(curwin->w_buffer, &oap->end);
3674 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3675
3676 // Set "virtual_op" before resetting VIsual_active.
3677 virtual_op = virtual_active();
3678
3679 if (VIsual_active || redo_VIsual_busy)
3680 {
3681 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3682
3683 if (!redo_VIsual_busy && !gui_yank)
3684 {
3685 // Prepare to reselect and redo Visual: this is based on the
3686 // size of the Visual text
3687 resel_VIsual_mode = VIsual_mode;
3688 if (curwin->w_curswant == MAXCOL)
3689 resel_VIsual_vcol = MAXCOL;
3690 else
3691 {
3692 if (VIsual_mode != Ctrl_V)
3693 getvvcol(curwin, &(oap->end),
3694 NULL, NULL, &oap->end_vcol);
3695 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3696 {
3697 if (VIsual_mode != Ctrl_V)
3698 getvvcol(curwin, &(oap->start),
3699 &oap->start_vcol, NULL, NULL);
3700 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3701 }
3702 else
3703 resel_VIsual_vcol = oap->end_vcol;
3704 }
3705 resel_VIsual_line_count = oap->line_count;
3706 }
3707
3708 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3709 if ((redo_yank || oap->op_type != OP_YANK)
3710 && oap->op_type != OP_COLON
3711#ifdef FEAT_FOLDING
3712 && oap->op_type != OP_FOLD
3713 && oap->op_type != OP_FOLDOPEN
3714 && oap->op_type != OP_FOLDOPENREC
3715 && oap->op_type != OP_FOLDCLOSE
3716 && oap->op_type != OP_FOLDCLOSEREC
3717 && oap->op_type != OP_FOLDDEL
3718 && oap->op_type != OP_FOLDDELREC
3719#endif
3720 && oap->motion_force == NUL
3721 )
3722 {
3723 // Prepare for redoing. Only use the nchar field for "r",
3724 // otherwise it might be the second char of the operator.
3725 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3726 || cap->nchar == 'N'))
3727 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003728 get_op_char(oap->op_type),
3729 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003730 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003731 else if (cap->cmdchar != ':' && cap->cmdchar != K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003732 {
3733 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3734
3735 // reverse what nv_replace() did
3736 if (nchar == REPLACE_CR_NCHAR)
3737 nchar = CAR;
3738 else if (nchar == REPLACE_NL_NCHAR)
3739 nchar = NL;
3740 prep_redo(oap->regname, 0L, NUL, 'v',
3741 get_op_char(oap->op_type),
3742 get_extra_op_char(oap->op_type),
3743 nchar);
3744 }
3745 if (!redo_VIsual_busy)
3746 {
3747 redo_VIsual_mode = resel_VIsual_mode;
3748 redo_VIsual_vcol = resel_VIsual_vcol;
3749 redo_VIsual_line_count = resel_VIsual_line_count;
3750 redo_VIsual_count = cap->count0;
3751 redo_VIsual_arg = cap->arg;
3752 }
3753 }
3754
3755 // oap->inclusive defaults to TRUE.
3756 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3757 // FALSE. This makes "d}P" and "v}dP" work the same.
3758 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3759 oap->inclusive = TRUE;
3760 if (VIsual_mode == 'V')
3761 oap->motion_type = MLINE;
3762 else
3763 {
3764 oap->motion_type = MCHAR;
3765 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3766 && (include_line_break || !virtual_op))
3767 {
3768 oap->inclusive = FALSE;
3769 // Try to include the newline, unless it's an operator
3770 // that works on lines only.
3771 if (*p_sel != 'o'
3772 && !op_on_lines(oap->op_type)
3773 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3774 {
3775 ++oap->end.lnum;
3776 oap->end.col = 0;
3777 oap->end.coladd = 0;
3778 ++oap->line_count;
3779 }
3780 }
3781 }
3782
3783 redo_VIsual_busy = FALSE;
3784
3785 // Switch Visual off now, so screen updating does
3786 // not show inverted text when the screen is redrawn.
3787 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3788 // no screen redraw, so it is done here to remove the inverted
3789 // part.
3790 if (!gui_yank)
3791 {
3792 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003793 setmouse();
3794 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003795 may_clear_cmdline();
3796 if ((oap->op_type == OP_YANK
3797 || oap->op_type == OP_COLON
3798 || oap->op_type == OP_FUNCTION
3799 || oap->op_type == OP_FILTER)
3800 && oap->motion_force == NUL)
3801 {
3802#ifdef FEAT_LINEBREAK
3803 // make sure redrawing is correct
3804 curwin->w_p_lbr = lbr_saved;
3805#endif
3806 redraw_curbuf_later(INVERTED);
3807 }
3808 }
3809 }
3810
3811 // Include the trailing byte of a multi-byte char.
3812 if (has_mbyte && oap->inclusive)
3813 {
3814 int l;
3815
3816 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3817 if (l > 1)
3818 oap->end.col += l - 1;
3819 }
3820 curwin->w_set_curswant = TRUE;
3821
3822 // oap->empty is set when start and end are the same. The inclusive
3823 // flag affects this too, unless yanking and the end is on a NUL.
3824 oap->empty = (oap->motion_type == MCHAR
3825 && (!oap->inclusive
3826 || (oap->op_type == OP_YANK
3827 && gchar_pos(&oap->end) == NUL))
3828 && EQUAL_POS(oap->start, oap->end)
3829 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3830 // For delete, change and yank, it's an error to operate on an
3831 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3832 empty_region_error = (oap->empty
3833 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3834
3835 // Force a redraw when operating on an empty Visual region, when
3836 // 'modifiable is off or creating a fold.
3837 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3838#ifdef FEAT_FOLDING
3839 || oap->op_type == OP_FOLD
3840#endif
3841 ))
3842 {
3843#ifdef FEAT_LINEBREAK
3844 curwin->w_p_lbr = lbr_saved;
3845#endif
3846 redraw_curbuf_later(INVERTED);
3847 }
3848
3849 // If the end of an operator is in column one while oap->motion_type
3850 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3851 // character in the previous line. If op_start is on or before the
3852 // first non-blank in the line, the operator becomes linewise
3853 // (strange, but that's the way vi does it).
3854 if ( oap->motion_type == MCHAR
3855 && oap->inclusive == FALSE
3856 && !(cap->retval & CA_NO_ADJ_OP_END)
3857 && oap->end.col == 0
3858 && (!oap->is_VIsual || *p_sel == 'o')
3859 && !oap->block_mode
3860 && oap->line_count > 1)
3861 {
3862 oap->end_adjusted = TRUE; // remember that we did this
3863 --oap->line_count;
3864 --oap->end.lnum;
3865 if (inindent(0))
3866 oap->motion_type = MLINE;
3867 else
3868 {
3869 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3870 if (oap->end.col)
3871 {
3872 --oap->end.col;
3873 oap->inclusive = TRUE;
3874 }
3875 }
3876 }
3877 else
3878 oap->end_adjusted = FALSE;
3879
3880 switch (oap->op_type)
3881 {
3882 case OP_LSHIFT:
3883 case OP_RSHIFT:
3884 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3885 auto_format(FALSE, TRUE);
3886 break;
3887
3888 case OP_JOIN_NS:
3889 case OP_JOIN:
3890 if (oap->line_count < 2)
3891 oap->line_count = 2;
3892 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3893 curbuf->b_ml.ml_line_count)
3894 beep_flush();
3895 else
3896 {
3897 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3898 TRUE, TRUE, TRUE);
3899 auto_format(FALSE, TRUE);
3900 }
3901 break;
3902
3903 case OP_DELETE:
3904 VIsual_reselect = FALSE; // don't reselect now
3905 if (empty_region_error)
3906 {
3907 vim_beep(BO_OPER);
3908 CancelRedo();
3909 }
3910 else
3911 {
3912 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01003913 // save cursor line for undo if it wasn't saved yet
3914 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3915 && u_save_cursor() == OK)
3916 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003917 }
3918 break;
3919
3920 case OP_YANK:
3921 if (empty_region_error)
3922 {
3923 if (!gui_yank)
3924 {
3925 vim_beep(BO_OPER);
3926 CancelRedo();
3927 }
3928 }
3929 else
3930 {
3931#ifdef FEAT_LINEBREAK
3932 curwin->w_p_lbr = lbr_saved;
3933#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02003934 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003935 (void)op_yank(oap, FALSE, !gui_yank);
3936 }
3937 check_cursor_col();
3938 break;
3939
3940 case OP_CHANGE:
3941 VIsual_reselect = FALSE; // don't reselect now
3942 if (empty_region_error)
3943 {
3944 vim_beep(BO_OPER);
3945 CancelRedo();
3946 }
3947 else
3948 {
3949 // This is a new edit command, not a restart. Need to
3950 // remember it to make 'insertmode' work with mappings for
3951 // Visual mode. But do this only once and not when typed and
3952 // 'insertmode' isn't set.
3953 if (p_im || !KeyTyped)
3954 restart_edit_save = restart_edit;
3955 else
3956 restart_edit_save = 0;
3957 restart_edit = 0;
3958#ifdef FEAT_LINEBREAK
3959 // Restore linebreak, so that when the user edits it looks as
3960 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01003961 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003962#endif
3963 // Reset finish_op now, don't want it set inside edit().
3964 finish_op = FALSE;
3965 if (op_change(oap)) // will call edit()
3966 cap->retval |= CA_COMMAND_BUSY;
3967 if (restart_edit == 0)
3968 restart_edit = restart_edit_save;
3969 }
3970 break;
3971
3972 case OP_FILTER:
3973 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3974 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
3975 else
3976 bangredo = TRUE; // do_bang() will put cmd in redo buffer
3977 // FALLTHROUGH
3978
3979 case OP_INDENT:
3980 case OP_COLON:
3981
3982#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3983 // If 'equalprg' is empty, do the indenting internally.
3984 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3985 {
3986# ifdef FEAT_LISP
3987 if (curbuf->b_p_lisp)
3988 {
3989 op_reindent(oap, get_lisp_indent);
3990 break;
3991 }
3992# endif
3993# ifdef FEAT_CINDENT
3994 op_reindent(oap,
3995# ifdef FEAT_EVAL
3996 *curbuf->b_p_inde != NUL ? get_expr_indent :
3997# endif
3998 get_c_indent);
3999 break;
4000# endif
4001 }
4002#endif
4003
4004 op_colon(oap);
4005 break;
4006
4007 case OP_TILDE:
4008 case OP_UPPER:
4009 case OP_LOWER:
4010 case OP_ROT13:
4011 if (empty_region_error)
4012 {
4013 vim_beep(BO_OPER);
4014 CancelRedo();
4015 }
4016 else
4017 op_tilde(oap);
4018 check_cursor_col();
4019 break;
4020
4021 case OP_FORMAT:
4022#if defined(FEAT_EVAL)
4023 if (*curbuf->b_p_fex != NUL)
4024 op_formatexpr(oap); // use expression
4025 else
4026#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004027 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004028 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004029 op_colon(oap); // use external command
4030 else
4031 op_format(oap, FALSE); // use internal function
4032 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004033 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004034 case OP_FORMAT2:
4035 op_format(oap, TRUE); // use internal function
4036 break;
4037
4038 case OP_FUNCTION:
4039#ifdef FEAT_LINEBREAK
4040 // Restore linebreak, so that when the user edits it looks as
4041 // before.
4042 curwin->w_p_lbr = lbr_saved;
4043#endif
4044 op_function(oap); // call 'operatorfunc'
4045 break;
4046
4047 case OP_INSERT:
4048 case OP_APPEND:
4049 VIsual_reselect = FALSE; // don't reselect now
4050 if (empty_region_error)
4051 {
4052 vim_beep(BO_OPER);
4053 CancelRedo();
4054 }
4055 else
4056 {
4057 // This is a new edit command, not a restart. Need to
4058 // remember it to make 'insertmode' work with mappings for
4059 // Visual mode. But do this only once.
4060 restart_edit_save = restart_edit;
4061 restart_edit = 0;
4062#ifdef FEAT_LINEBREAK
4063 // Restore linebreak, so that when the user edits it looks as
4064 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004065 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004066#endif
4067 op_insert(oap, cap->count1);
4068#ifdef FEAT_LINEBREAK
4069 // Reset linebreak, so that formatting works correctly.
4070 curwin->w_p_lbr = FALSE;
4071#endif
4072
4073 // TODO: when inserting in several lines, should format all
4074 // the lines.
4075 auto_format(FALSE, TRUE);
4076
4077 if (restart_edit == 0)
4078 restart_edit = restart_edit_save;
4079 else
4080 cap->retval |= CA_COMMAND_BUSY;
4081 }
4082 break;
4083
4084 case OP_REPLACE:
4085 VIsual_reselect = FALSE; // don't reselect now
4086 if (empty_region_error)
4087 {
4088 vim_beep(BO_OPER);
4089 CancelRedo();
4090 }
4091 else
4092 {
4093#ifdef FEAT_LINEBREAK
4094 // Restore linebreak, so that when the user edits it looks as
4095 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004096 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004097#endif
4098 op_replace(oap, cap->nchar);
4099 }
4100 break;
4101
4102#ifdef FEAT_FOLDING
4103 case OP_FOLD:
4104 VIsual_reselect = FALSE; // don't reselect now
4105 foldCreate(oap->start.lnum, oap->end.lnum);
4106 break;
4107
4108 case OP_FOLDOPEN:
4109 case OP_FOLDOPENREC:
4110 case OP_FOLDCLOSE:
4111 case OP_FOLDCLOSEREC:
4112 VIsual_reselect = FALSE; // don't reselect now
4113 opFoldRange(oap->start.lnum, oap->end.lnum,
4114 oap->op_type == OP_FOLDOPEN
4115 || oap->op_type == OP_FOLDOPENREC,
4116 oap->op_type == OP_FOLDOPENREC
4117 || oap->op_type == OP_FOLDCLOSEREC,
4118 oap->is_VIsual);
4119 break;
4120
4121 case OP_FOLDDEL:
4122 case OP_FOLDDELREC:
4123 VIsual_reselect = FALSE; // don't reselect now
4124 deleteFold(oap->start.lnum, oap->end.lnum,
4125 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4126 break;
4127#endif
4128 case OP_NR_ADD:
4129 case OP_NR_SUB:
4130 if (empty_region_error)
4131 {
4132 vim_beep(BO_OPER);
4133 CancelRedo();
4134 }
4135 else
4136 {
4137 VIsual_active = TRUE;
4138#ifdef FEAT_LINEBREAK
4139 curwin->w_p_lbr = lbr_saved;
4140#endif
4141 op_addsub(oap, cap->count1, redo_VIsual_arg);
4142 VIsual_active = FALSE;
4143 }
4144 check_cursor_col();
4145 break;
4146 default:
4147 clearopbeep(oap);
4148 }
4149 virtual_op = MAYBE;
4150 if (!gui_yank)
4151 {
4152 // if 'sol' not set, go back to old column for some commands
4153 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4154 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4155 || oap->op_type == OP_DELETE))
4156 {
4157#ifdef FEAT_LINEBREAK
4158 curwin->w_p_lbr = FALSE;
4159#endif
4160 coladvance(curwin->w_curswant = old_col);
4161 }
4162 }
4163 else
4164 {
4165 curwin->w_cursor = old_cursor;
4166 }
4167 oap->block_mode = FALSE;
4168 clearop(oap);
4169 motion_force = NUL;
4170 }
4171#ifdef FEAT_LINEBREAK
4172 curwin->w_p_lbr = lbr_saved;
4173#endif
4174}