blob: fa6a4c4d1f8c53874afacea3b38a7dee69788165 [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 Moolenaar6b36d2a2021-08-23 21:19:01 +0200945 if (oap->op_type == OP_DELETE)
946 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
948
949 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
950
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000951setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200952 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100954 if (oap->block_mode)
955 {
956 curbuf->b_op_end.lnum = oap->end.lnum;
957 curbuf->b_op_end.col = oap->start.col;
958 }
959 else
960 curbuf->b_op_end = oap->start;
961 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
964 return OK;
965}
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967/*
968 * Adjust end of operating area for ending on a multi-byte character.
969 * Used for deletion.
970 */
971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100972mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 char_u *p;
975
976 if (oap->inclusive)
977 {
978 p = ml_get(oap->end.lnum);
979 oap->end.col += mb_tail_off(p, p + oap->end.col);
980 }
981}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar630afe82018-06-28 19:26:28 +0200983/*
984 * Replace the character under the cursor with "c".
985 * This takes care of multi-byte characters.
986 */
987 static void
988replace_character(int c)
989{
990 int n = State;
991
992 State = REPLACE;
993 ins_char(c);
994 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100995 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200996 dec_cursor();
997}
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999/*
1000 * Replace a whole area with one character.
1001 */
1002 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001003op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 char_u *newp, *oldp;
1008 size_t oldlen;
1009 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001010 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001011 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001016 if (c == REPLACE_CR_NCHAR)
1017 {
1018 had_ctrl_v_cr = TRUE;
1019 c = CAR;
1020 }
1021 else if (c == REPLACE_NL_NCHAR)
1022 {
1023 had_ctrl_v_cr = TRUE;
1024 c = NL;
1025 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (has_mbyte)
1028 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 if (u_save((linenr_T)(oap->start.lnum - 1),
1031 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1032 return FAIL;
1033
1034 /*
1035 * block mode replace
1036 */
1037 if (oap->block_mode)
1038 {
1039 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1040 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001042 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1044 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001045 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 // n == number of extra chars required
1048 // If we split a TAB, it may be replaced by several characters.
1049 // Thus the number of characters may increase!
1050 // If the range starts in virtual space, count the initial
1051 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1053 {
1054 pos_T vpos;
1055
Bram Moolenaara1381de2009-11-03 15:44:21 +00001056 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 getvpos(&vpos, oap->start_vcol);
1058 bd.startspaces += vpos.coladd;
1059 n = bd.startspaces;
1060 }
1061 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001062 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001065 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 numc = oap->end_vcol - oap->start_vcol + 1;
1071 if (bd.is_short && (!virtual_op || bd.is_MAX))
1072 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001074 // A double-wide character can be replaced only up to half the
1075 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if ((*mb_char2cells)(c) > 1)
1077 {
1078 if ((numc & 1) && !bd.is_short)
1079 {
1080 ++bd.endspaces;
1081 ++n;
1082 }
1083 numc = numc / 2;
1084 }
1085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001086 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 num_chars = numc;
1088 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001089 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 n += numc - bd.textlen;
1091
1092 oldp = ml_get_curline();
1093 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001094 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (newp == NULL)
1096 continue;
1097 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 mch_memmove(newp, oldp, (size_t)bd.textcol);
1100 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001101 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001102 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001103 // insert replacement chars CHECK FOR ALLOCATED SPACE
1104 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1105 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001106 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001108 if (has_mbyte)
1109 {
1110 n = (int)STRLEN(newp);
1111 while (--num_chars >= 0)
1112 n += (*mb_char2bytes)(c, newp + n);
1113 }
1114 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001115 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001116 if (!bd.is_short)
1117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001118 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001119 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001120 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001121 STRMOVE(newp + STRLEN(newp), oldp);
1122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001126 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001127 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001128 if (after_p != NULL)
1129 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001131 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001133 if (after_p != NULL)
1134 {
1135 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1136 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1137 oap->end.lnum++;
1138 vim_free(after_p);
1139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
1141 }
1142 else
1143 {
1144 /*
1145 * MCHAR and MLINE motion replace.
1146 */
1147 if (oap->motion_type == MLINE)
1148 {
1149 oap->start.col = 0;
1150 curwin->w_cursor.col = 0;
1151 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1152 if (oap->end.col)
1153 --oap->end.col;
1154 }
1155 else if (!oap->inclusive)
1156 dec(&(oap->end));
1157
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001158 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 n = gchar_cursor();
1161 if (n != NUL)
1162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001165 // This is slow, but it handles replacing a single-byte
1166 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001167 if (curwin->w_cursor.lnum == oap->end.lnum)
1168 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001169 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (n == TAB)
1174 {
1175 int end_vcol = 0;
1176
1177 if (curwin->w_cursor.lnum == oap->end.lnum)
1178 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001179 // oap->end has to be recalculated when
1180 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 end_vcol = getviscol2(oap->end.col,
1182 oap->end.coladd);
1183 }
1184 coladvance_force(getviscol());
1185 if (curwin->w_cursor.lnum == oap->end.lnum)
1186 getvpos(&oap->end, end_vcol);
1187 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001188 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1192 {
1193 int virtcols = oap->end.coladd;
1194
1195 if (curwin->w_cursor.lnum == oap->start.lnum
1196 && oap->start.col == oap->end.col && oap->start.coladd)
1197 virtcols -= oap->start.coladd;
1198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001199 // oap->end has been trimmed so it's effectively inclusive;
1200 // as a result an extra +1 must be counted so we don't
1201 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1203 curwin->w_cursor.col -= (virtcols + 1);
1204 for (; virtcols >= 0; virtcols--)
1205 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001206 if ((*mb_char2len)(c) > 1)
1207 replace_character(c);
1208 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001209 PBYTE(curwin->w_cursor, c);
1210 if (inc(&curwin->w_cursor) == -1)
1211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001215 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (inc_cursor() == -1)
1217 break;
1218 }
1219 }
1220
1221 curwin->w_cursor = oap->start;
1222 check_cursor();
1223 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1224
Bram Moolenaare1004402020-10-24 20:49:43 +02001225 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001228 curbuf->b_op_start = oap->start;
1229 curbuf->b_op_end = oap->end;
1230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
1232 return OK;
1233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001235static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237/*
1238 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1239 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001241op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001245 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 if (u_save((linenr_T)(oap->start.lnum - 1),
1248 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1249 return;
1250
1251 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001252 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
1254 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1255 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001256 int one_change;
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 block_prep(oap, &bd, pos.lnum, FALSE);
1259 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001260 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1261 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001262
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001263#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001264 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
1266 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1267
Bram Moolenaar009b2592004-10-24 19:18:58 +00001268 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1269 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001271 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 if (did_change)
1276 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1277 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001278 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
1280 if (oap->motion_type == MLINE)
1281 {
1282 oap->start.col = 0;
1283 pos.col = 0;
1284 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1285 if (oap->end.col)
1286 --oap->end.col;
1287 }
1288 else if (!oap->inclusive)
1289 dec(&(oap->end));
1290
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001291 if (pos.lnum == oap->end.lnum)
1292 did_change = swapchars(oap->op_type, &pos,
1293 oap->end.col - pos.col + 1);
1294 else
1295 for (;;)
1296 {
1297 did_change |= swapchars(oap->op_type, &pos,
1298 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1299 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001300 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001301 break;
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (did_change)
1304 {
1305 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1306 0L);
1307#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001308 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
1310 char_u *ptr;
1311 int count;
1312
1313 pos = oap->start;
1314 while (pos.lnum < oap->end.lnum)
1315 {
1316 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001317 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001318 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001320 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 pos.col = 0;
1322 pos.lnum++;
1323 }
1324 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1325 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001326 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001328 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330#endif
1331 }
1332 }
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001335 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaare1004402020-10-24 20:49:43 +02001338 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001339 {
1340 // Set '[ and '] marks.
1341 curbuf->b_op_start = oap->start;
1342 curbuf->b_op_end = oap->end;
1343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001346 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001347 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348}
1349
1350/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001351 * Invoke swapchar() on "length" bytes at position "pos".
1352 * "pos" is advanced to just after the changed characters.
1353 * "length" is rounded up to include the whole last multi-byte character.
1354 * Also works correctly when the number of bytes changes.
1355 * Returns TRUE if some character was changed.
1356 */
1357 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001358swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001359{
1360 int todo;
1361 int did_change = 0;
1362
1363 for (todo = length; todo > 0; --todo)
1364 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001365 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001366 {
1367 int len = (*mb_ptr2len)(ml_get_pos(pos));
1368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001369 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001370 if (len > 0)
1371 todo -= len - 1;
1372 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001373 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001374 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001375 break;
1376 }
1377 return did_change;
1378}
1379
1380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 * If op_type == OP_UPPER: make uppercase,
1382 * if op_type == OP_LOWER: make lowercase,
1383 * if op_type == OP_ROT13: do rot13 encoding,
1384 * else swap case of character at 'pos'
1385 * returns TRUE when something actually changed.
1386 */
1387 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001388swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 int c;
1391 int nc;
1392
1393 c = gchar_pos(pos);
1394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001395 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (c >= 0x80 && op_type == OP_ROT13)
1397 return FALSE;
1398
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001399 if (op_type == OP_UPPER && c == 0xdf
1400 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001401 {
1402 pos_T sp = curwin->w_cursor;
1403
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001404 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001405 curwin->w_cursor = *pos;
1406 del_char(FALSE);
1407 ins_char('S');
1408 ins_char('S');
1409 curwin->w_cursor = sp;
1410 inc(pos);
1411 }
1412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001413 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 nc = c;
1416 if (MB_ISLOWER(c))
1417 {
1418 if (op_type == OP_ROT13)
1419 nc = ROT13(c, 'a');
1420 else if (op_type != OP_LOWER)
1421 nc = MB_TOUPPER(c);
1422 }
1423 else if (MB_ISUPPER(c))
1424 {
1425 if (op_type == OP_ROT13)
1426 nc = ROT13(c, 'A');
1427 else if (op_type != OP_UPPER)
1428 nc = MB_TOLOWER(c);
1429 }
1430 if (nc != c)
1431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1433 {
1434 pos_T sp = curwin->w_cursor;
1435
1436 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001437 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001438 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ins_char(nc);
1440 curwin->w_cursor = sp;
1441 }
1442 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001443 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return TRUE;
1445 }
1446 return FALSE;
1447}
1448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449/*
1450 * op_insert - Insert and append operators for Visual mode.
1451 */
1452 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001453op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 long ins_len, pre_textlen = 0;
1456 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001457 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 struct block_def bd;
1459 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001460 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001461 pos_T start_insert;
1462 // offset when cursor was moved in insert mode
1463 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001465 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001468 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 curwin->w_cursor.lnum = oap->start.lnum;
1470 update_screen(INVERTED);
1471
1472 if (oap->block_mode)
1473 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001474 // When 'virtualedit' is used, need to insert the extra spaces before
1475 // doing block_prep(). When only "block" is used, virtual edit is
1476 // already disabled, but still need it when calling
1477 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001478 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001479 // state for the current window. To override that state, we need to
1480 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (curwin->w_cursor.coladd > 0)
1482 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001483 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (u_save_cursor() == FAIL)
1486 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001487
Gary Johnson51ad8502021-08-03 18:33:08 +02001488 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 coladvance_force(oap->op_type == OP_APPEND
1490 ? oap->end_vcol + 1 : getviscol());
1491 if (oap->op_type == OP_APPEND)
1492 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001493 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001495 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001497 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001498 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (oap->op_type == OP_APPEND)
1502 firstline += bd.textlen;
1503 pre_textlen = (long)STRLEN(firstline);
1504 }
1505
1506 if (oap->op_type == OP_APPEND)
1507 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001508 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001510 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 curwin->w_set_curswant = TRUE;
1512 while (*ml_get_cursor() != NUL
1513 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1514 ++curwin->w_cursor.col;
1515 if (bd.is_short && !bd.is_MAX)
1516 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001517 // First line was too short, make it longer and adjust the
1518 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (u_save_cursor() == FAIL)
1520 return;
1521 for (i = 0; i < bd.endspaces; ++i)
1522 ins_char(' ');
1523 bd.textlen += bd.endspaces;
1524 }
1525 }
1526 else
1527 {
1528 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001529 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001532 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 && oap->start_vcol != oap->end_vcol)
1534 inc_cursor();
1535 }
1536 }
1537
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001538 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001539 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001540 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001542 // When a tab was inserted, and the characters in front of the tab
1543 // have been converted to a tab as well, the column of the cursor
1544 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001545 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001546 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001547 oap->start = curbuf->b_op_start_orig;
1548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001549 // If user has moved off this line, we don't know what to do, so do
1550 // nothing.
1551 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001552 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 return;
1554
1555 if (oap->block_mode)
1556 {
1557 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001558 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001559 size_t len;
1560 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001562 // If indent kicked in, the firstline might have changed
1563 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001564 ind_post = (colnr_T)getwhitecols_curline();
1565 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1566 {
1567 bd.textcol += ind_post - ind_pre;
1568 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001569 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001570 }
1571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001572 // The user may have moved the cursor before inserting something, try
1573 // to adjust the block for that. But only do it, if the difference
1574 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001575 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1576 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001577 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001578 int t = getviscol2(curbuf->b_op_start_orig.col,
1579 curbuf->b_op_start_orig.coladd);
1580
1581 if (!bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001582 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001583 if (oap->op_type == OP_INSERT
1584 && oap->start.col + oap->start.coladd
1585 != curbuf->b_op_start_orig.col
1586 + curbuf->b_op_start_orig.coladd)
1587 {
1588 oap->start.col = curbuf->b_op_start_orig.col;
1589 pre_textlen -= t - oap->start_vcol;
1590 oap->start_vcol = t;
1591 }
1592 else if (oap->op_type == OP_APPEND
1593 && oap->end.col + oap->end.coladd
1594 >= curbuf->b_op_start_orig.col
1595 + curbuf->b_op_start_orig.coladd)
1596 {
1597 oap->start.col = curbuf->b_op_start_orig.col;
1598 // reset pre_textlen to the value of OP_INSERT
1599 pre_textlen += bd.textlen;
1600 pre_textlen -= t - oap->start_vcol;
1601 oap->start_vcol = t;
1602 oap->op_type = OP_INSERT;
1603 }
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001604 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001605 else if (bd.is_MAX && oap->op_type == OP_APPEND)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001608 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001609 pre_textlen -= t - oap->start_vcol;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001610 }
1611 }
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /*
1614 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001615 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 * Don't do this when "$" used, end-of-line will have changed.
1617 */
1618 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1619 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1620 {
1621 if (oap->op_type == OP_APPEND)
1622 {
1623 pre_textlen += bd2.textlen - bd.textlen;
1624 if (bd2.endspaces)
1625 --bd2.textlen;
1626 }
1627 bd.textcol = bd2.textcol;
1628 bd.textlen = bd2.textlen;
1629 }
1630
1631 /*
1632 * Subsequent calls to ml_get() flush the firstline data - take a
1633 * copy of the required string.
1634 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001635 firstline = ml_get(oap->start.lnum);
1636 len = STRLEN(firstline);
1637 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001639 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001640 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001641 // account for pressing cursor in insert mode when '$' was used
1642 if (bd.is_MAX
1643 && (start_insert.lnum == Insstart.lnum
1644 && start_insert.col > Insstart.col))
1645 {
1646 offset = (start_insert.col - Insstart.col);
1647 add -= offset;
1648 if (oap->end_vcol > offset)
1649 oap->end_vcol -= (offset + 1);
1650 else
1651 // moved outside of the visual block, what to do?
1652 return;
1653 }
1654 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001655 if ((size_t)add > len)
1656 firstline += len; // short line, point to the NUL
1657 else
1658 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001659 if (pre_textlen >= 0 && (ins_len =
1660 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001662 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (ins_text != NULL)
1664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001665 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (u_save(oap->start.lnum,
1667 (linenr_T)(oap->end.lnum + 1)) == OK)
1668 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1669 &bd);
1670
1671 curwin->w_cursor.col = oap->start.col;
1672 check_cursor();
1673 vim_free(ins_text);
1674 }
1675 }
1676 }
1677}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679/*
1680 * op_change - handle a change operation
1681 *
1682 * return TRUE if edit() returns because of a CTRL-O command
1683 */
1684 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001685op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 colnr_T l;
1688 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 long offset;
1690 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001691 long ins_len;
1692 long pre_textlen = 0;
1693 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 char_u *firstline;
1695 char_u *ins_text, *newp, *oldp;
1696 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698 l = oap->start.col;
1699 if (oap->motion_type == MLINE)
1700 {
1701 l = 0;
1702#ifdef FEAT_SMARTINDENT
1703 if (!p_paste && curbuf->b_p_si
1704# ifdef FEAT_CINDENT
1705 && !curbuf->b_p_cin
1706# endif
1707 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001708 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#endif
1710 }
1711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001712 // First delete the text in the region. In an empty buffer only need to
1713 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1715 {
1716 if (u_save_cursor() == FAIL)
1717 return FALSE;
1718 }
1719 else if (op_delete(oap) == FAIL)
1720 return FALSE;
1721
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001722 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 && !virtual_op)
1724 inc_cursor();
1725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001726 // check for still on same line (<CR> in inserted text meaningless)
1727 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (oap->block_mode)
1729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (virtual_op && (curwin->w_cursor.coladd > 0
1732 || gchar_cursor() == NUL))
1733 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001734 firstline = ml_get(oap->start.lnum);
1735 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001736 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 bd.textcol = curwin->w_cursor.col;
1738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1741 if (oap->motion_type == MLINE)
1742 fix_indent();
1743#endif
1744
1745 retval = edit(NUL, FALSE, (linenr_T)1);
1746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001748 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001750 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001752 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001754 // Auto-indenting may have changed the indent. If the cursor was past
1755 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001757 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001759 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001760
1761 pre_textlen += new_indent - pre_indent;
1762 bd.textcol += new_indent - pre_indent;
1763 }
1764
1765 ins_len = (long)STRLEN(firstline) - pre_textlen;
1766 if (ins_len > 0)
1767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001768 // Subsequent calls to ml_get() flush the firstline data - take a
1769 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001770 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001772 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1774 linenr++)
1775 {
1776 block_prep(oap, &bd, linenr, TRUE);
1777 if (!bd.is_short || virtual_op)
1778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 pos_T vpos;
1780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001781 // If the block starts in virtual space, count the
1782 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (bd.is_short)
1784 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001785 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 else
1789 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001791 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (newp == NULL)
1793 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001794 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 mch_memmove(newp, oldp, (size_t)bd.textcol);
1796 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001797 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1800 offset += ins_len;
1801 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001802 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 ml_replace(linenr, newp, FALSE);
1804 }
1805 }
1806 check_cursor();
1807
1808 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1809 }
1810 vim_free(ins_text);
1811 }
1812 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001813 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 return retval;
1816}
1817
1818/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001819 * When the cursor is on the NUL past the end of the line and it should not be
1820 * there move it left.
1821 */
1822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001823adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001824{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001825 unsigned int cur_ve_flags = get_ve_flags();
1826
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001827 if (curwin->w_cursor.col > 0
1828 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001829 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 && !(restart_edit || (State & INSERT)))
1831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001832 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001833 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001834
Gary Johnson53ba05b2021-07-26 22:19:10 +02001835 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001836 {
1837 colnr_T scol, ecol;
1838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001839 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001840 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1841 curwin->w_cursor.coladd = ecol - scol + 1;
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844}
1845
Bram Moolenaar81340392012-06-06 16:12:59 +02001846/*
1847 * If "process" is TRUE and the line begins with a comment leader (possibly
1848 * after some white space), return a pointer to the text after it. Put a boolean
1849 * value indicating whether the line ends with an unclosed comment in
1850 * "is_comment".
1851 * line - line to be processed,
1852 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001853 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001854 * include_space - whether to also skip space following the comment leader,
1855 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001856 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001857 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001858 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001859skip_comment(
1860 char_u *line,
1861 int process,
1862 int include_space,
1863 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001864{
1865 char_u *comment_flags = NULL;
1866 int lead_len;
1867 int leader_offset = get_last_leader_offset(line, &comment_flags);
1868
1869 *is_comment = FALSE;
1870 if (leader_offset != -1)
1871 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001872 // Let's check whether the line ends with an unclosed comment.
1873 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001874 while (*comment_flags)
1875 {
1876 if (*comment_flags == COM_END
1877 || *comment_flags == ':')
1878 break;
1879 ++comment_flags;
1880 }
1881 if (*comment_flags != COM_END)
1882 *is_comment = TRUE;
1883 }
1884
1885 if (process == FALSE)
1886 return line;
1887
1888 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1889
1890 if (lead_len == 0)
1891 return line;
1892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 // Find:
1894 // - COM_END,
1895 // - colon,
1896 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001897 while (*comment_flags)
1898 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001899 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001900 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001901 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001902 ++comment_flags;
1903 }
1904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001905 // If we found a colon, it means that we are not processing a line
1906 // starting with a closing part of a three-part comment. That's good,
1907 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001908 if (*comment_flags == ':' || *comment_flags == NUL)
1909 line += lead_len;
1910
1911 return line;
1912}
Bram Moolenaar81340392012-06-06 16:12:59 +02001913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001915 * Join 'count' lines (minimal 2) at cursor position.
1916 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001917 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1918 * leaders should not be removed.
1919 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1920 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001922 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
1924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001925do_join(
1926 long count,
1927 int insert_space,
1928 int save_undo,
1929 int use_formatoptions UNUSED,
1930 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001932 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001933 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001934 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001936 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001937 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001938 int endcurr1 = NUL;
1939 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001940 int currsize = 0; // size of the current line
1941 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001943 colnr_T col = 0;
1944 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001945 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001946 int remove_comments = (use_formatoptions == TRUE)
1947 && has_format_option(FO_REMOVE_COMS);
1948 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001949#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001950 int propcount = 0; // number of props over all joined lines
1951 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001954 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1955 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1956 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001958 // Allocate an array to store the number of spaces inserted before each
1959 // line. We will use it to pre-compute the length of the new line and the
1960 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001961 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001962 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001964 if (remove_comments)
1965 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001966 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001967 if (comments == NULL)
1968 {
1969 vim_free(spaces);
1970 return FAIL;
1971 }
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
1974 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001975 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001976 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001977 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001979 for (t = 0; t < count; ++t)
1980 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001981 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001982#ifdef FEAT_PROP_POPUP
1983 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1984#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001985 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001987 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001988 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1989 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1990 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001991 if (remove_comments)
1992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001993 // We don't want to remove the comment leader if the
1994 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001995 if (t > 0 && prev_was_comment)
1996 {
1997
1998 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1999 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002000 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002001 curr = new_curr;
2002 }
2003 else
2004 curr = skip_comment(curr, FALSE, insert_space,
2005 &prev_was_comment);
2006 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002007
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002008 if (insert_space && t > 0)
2009 {
2010 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002011 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002012 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002013 && (!has_format_option(FO_MBYTE_JOIN)
2014 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2015 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002016 || (mb_ptr2char(curr) < 0x100
2017 && !(enc_utf8 && utf_eat_space(endcurr1)))
2018 || (endcurr1 < 0x100
2019 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002020 )
2021 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002022 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002023 if (endcurr1 == ' ')
2024 endcurr1 = endcurr2;
2025 else
2026 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002027 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002028 if ( p_js
2029 && (endcurr1 == '.'
2030 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2031 && (endcurr1 == '?' || endcurr1 == '!'))))
2032 ++spaces[t];
2033 }
2034 }
2035 currsize = (int)STRLEN(curr);
2036 sumsize += currsize + spaces[t];
2037 endcurr1 = endcurr2 = NUL;
2038 if (insert_space && currsize > 0)
2039 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002040 if (has_mbyte)
2041 {
2042 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002043 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 endcurr1 = (*mb_ptr2char)(cend);
2045 if (cend > curr)
2046 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002047 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002048 endcurr2 = (*mb_ptr2char)(cend);
2049 }
2050 }
2051 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002052 {
2053 endcurr1 = *(curr + currsize - 1);
2054 if (currsize > 1)
2055 endcurr2 = *(curr + currsize - 2);
2056 }
2057 }
2058 line_breakcheck();
2059 if (got_int)
2060 {
2061 ret = FAIL;
2062 goto theend;
2063 }
2064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002066 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002067 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002069 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002070 newp_len = sumsize + 1;
2071#ifdef FEAT_PROP_POPUP
2072 newp_len += propcount * sizeof(textprop_T);
2073#endif
2074 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002075 if (newp == NULL)
2076 {
2077 ret = FAIL;
2078 goto theend;
2079 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002080 cend = newp + sumsize;
2081 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002083 /*
2084 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002085 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002086 *
2087 * Move marks from each deleted line to the joined line, adjusting the
2088 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2089 * should not really be a problem.
2090 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002091#ifdef FEAT_PROP_POPUP
2092 props_remaining = propcount;
2093#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002094 for (t = count - 1; ; --t)
2095 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002096 int spaces_removed;
2097
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002098 cend -= currsize;
2099 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002100
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002101 if (spaces[t] > 0)
2102 {
2103 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002104 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002105 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002106
2107 // If deleting more spaces than adding, the cursor moves no more than
2108 // what is added if it is inside these spaces.
2109 spaces_removed = (curr - curr_start) - spaces[t];
2110
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002111 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002112 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002113#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002114 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2115 curwin->w_cursor.lnum + t, t == count - 1,
2116 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002117#endif
2118
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002119 if (t == 0)
2120 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002121 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002122 if (remove_comments)
2123 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002124 if (insert_space && t > 1)
2125 curr = skipwhite(curr);
2126 currsize = (int)STRLEN(curr);
2127 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002128
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002129 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
Bram Moolenaare1004402020-10-24 20:49:43 +02002131 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002132 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002133 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002134 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002135 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002136 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002138 // Only report the change in the first line here, del_lines() will report
2139 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 changed_lines(curwin->w_cursor.lnum, currsize,
2141 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002143 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 * briefly, and then move it back. After del_lines() the cursor may
2145 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 */
2147 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002149 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 curwin->w_cursor.lnum = t;
2151
2152 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002153 * Set the cursor column:
2154 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002155 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002157 curwin->w_cursor.col =
2158 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 curwin->w_set_curswant = TRUE;
2163
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002164theend:
2165 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002166 if (remove_comments)
2167 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002168 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169}
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 * prepare a few things for block mode yank/delete/tilde
2173 *
2174 * for delete:
2175 * - textlen includes the first/last char to be (partly) deleted
2176 * - start/endspaces is the number of columns that are taken by the
2177 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002178 * deleted.
2179 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * - textlen includes the first/last char to be wholly yanked
2181 * - start/endspaces is the number of columns of the first/last yanked char
2182 * that are to be yanked.
2183 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002185block_prep(
2186 oparg_T *oap,
2187 struct block_def *bdp,
2188 linenr_T lnum,
2189 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 int incr = 0;
2192 char_u *pend;
2193 char_u *pstart;
2194 char_u *line;
2195 char_u *prev_pstart;
2196 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002197#ifdef FEAT_LINEBREAK
2198 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002200 // Avoid a problem with unwanted linebreaks in block mode.
2201 curwin->w_p_lbr = FALSE;
2202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 bdp->startspaces = 0;
2204 bdp->endspaces = 0;
2205 bdp->textlen = 0;
2206 bdp->start_vcol = 0;
2207 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 bdp->is_short = FALSE;
2209 bdp->is_oneChar = FALSE;
2210 bdp->pre_whitesp = 0;
2211 bdp->pre_whitesp_c = 0;
2212 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 bdp->start_char_vcols = 0;
2214
2215 line = ml_get(lnum);
2216 pstart = line;
2217 prev_pstart = line;
2218 while (bdp->start_vcol < oap->start_vcol && *pstart)
2219 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002220 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002221 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002223 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 bdp->pre_whitesp += incr;
2226 bdp->pre_whitesp_c++;
2227 }
2228 else
2229 {
2230 bdp->pre_whitesp = 0;
2231 bdp->pre_whitesp_c = 0;
2232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002234 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
2236 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (!is_del || oap->op_type == OP_APPEND)
2242 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2243 }
2244 else
2245 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002246 // notice: this converts partly selected Multibyte characters to
2247 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2249 if (is_del && bdp->startspaces)
2250 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2251 pend = pstart;
2252 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002253 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (oap->op_type == OP_INSERT)
2257 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2258 else if (oap->op_type == OP_APPEND)
2259 {
2260 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2261 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2262 }
2263 else
2264 {
2265 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2266 if (is_del && oap->op_type != OP_LSHIFT)
2267 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002268 // just putting the sum of those two into
2269 // bdp->startspaces doesn't work for Visual replace,
2270 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 bdp->startspaces = bdp->start_char_vcols
2272 - (bdp->start_vcol - oap->start_vcol);
2273 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2274 }
2275 }
2276 }
2277 else
2278 {
2279 prev_pend = pend;
2280 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2281 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002282 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002284 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 bdp->end_vcol += incr;
2286 }
2287 if (bdp->end_vcol <= oap->end_vcol
2288 && (!is_del
2289 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002290 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 // Alternative: include spaces to fill up the block.
2294 // Disadvantage: can lead to trailing spaces when the line is
2295 // short where the text is put
2296 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (oap->op_type == OP_APPEND || virtual_op)
2298 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002299 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002301 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 else if (bdp->end_vcol > oap->end_vcol)
2304 {
2305 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2306 if (!is_del && bdp->endspaces)
2307 {
2308 bdp->endspaces = incr - bdp->endspaces;
2309 if (pend != pstart)
2310 pend = prev_pend;
2311 }
2312 }
2313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (is_del && bdp->startspaces)
2316 pstart = prev_pstart;
2317 bdp->textlen = (int)(pend - pstart);
2318 }
2319 bdp->textcol = (colnr_T) (pstart - line);
2320 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002321#ifdef FEAT_LINEBREAK
2322 curwin->w_p_lbr = lbr_saved;
2323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002327 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002329 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002330op_addsub(
2331 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002332 linenr_T Prenum1, // Amount of add/subtract
2333 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002335 pos_T pos;
2336 struct block_def bd;
2337 int change_cnt = 0;
2338 linenr_T amount = Prenum1;
2339
Bram Moolenaar6b731882018-11-16 20:54:47 +01002340 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002341 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002342 // the call to changed_lines().
2343#ifdef FEAT_FOLDING
2344 disable_fold_update++;
2345#endif
2346
Bram Moolenaard79e5502016-01-10 22:13:02 +01002347 if (!VIsual_active)
2348 {
2349 pos = curwin->w_cursor;
2350 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002351 {
2352#ifdef FEAT_FOLDING
2353 disable_fold_update--;
2354#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002355 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002356 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002357 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002358#ifdef FEAT_FOLDING
2359 disable_fold_update--;
2360#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002361 if (change_cnt)
2362 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2363 }
2364 else
2365 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002366 int one_change;
2367 int length;
2368 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002369
2370 if (u_save((linenr_T)(oap->start.lnum - 1),
2371 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002372 {
2373#ifdef FEAT_FOLDING
2374 disable_fold_update--;
2375#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002376 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002377 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002378
2379 pos = oap->start;
2380 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2381 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002382 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002383 {
2384 block_prep(oap, &bd, pos.lnum, FALSE);
2385 pos.col = bd.textcol;
2386 length = bd.textlen;
2387 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002388 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002389 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002390 curwin->w_cursor.col = 0;
2391 pos.col = 0;
2392 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2393 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002394 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002395 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002396 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002397 dec(&(oap->end));
2398 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2399 pos.col = 0;
2400 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002401 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002402 pos.col += oap->start.col;
2403 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002404 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002405 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002406 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002407 length = (int)STRLEN(ml_get(oap->end.lnum));
2408 if (oap->end.col >= length)
2409 oap->end.col = length - 1;
2410 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002411 }
2412 }
2413 one_change = do_addsub(oap->op_type, &pos, length, amount);
2414 if (one_change)
2415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002416 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002417 if (change_cnt == 0)
2418 startpos = curbuf->b_op_start;
2419 ++change_cnt;
2420 }
2421
2422#ifdef FEAT_NETBEANS_INTG
2423 if (netbeans_active() && one_change)
2424 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002425 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002426
2427 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002428 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002429 netbeans_inserted(curbuf, pos.lnum, pos.col,
2430 &ptr[pos.col], length);
2431 }
2432#endif
2433 if (g_cmd && one_change)
2434 amount += Prenum1;
2435 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002436
2437#ifdef FEAT_FOLDING
2438 disable_fold_update--;
2439#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002440 if (change_cnt)
2441 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2442
2443 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002444 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002445 redraw_curbuf_later(INVERTED);
2446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 // Set '[ mark if something changed. Keep the last end
2448 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002449 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002450 curbuf->b_op_start = startpos;
2451
2452 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002453 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002454 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002455 }
2456}
2457
2458/*
2459 * Add or subtract 'Prenum1' from a number in a line
2460 * op_type is OP_NR_ADD or OP_NR_SUB
2461 *
2462 * Returns TRUE if some character was changed.
2463 */
2464 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465do_addsub(
2466 int op_type,
2467 pos_T *pos,
2468 int length,
2469 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002470{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 int col;
2472 char_u *buf1;
2473 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002474 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2475 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002476 uvarnumber_T n;
2477 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 char_u *ptr;
2479 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002481 int do_hex;
2482 int do_oct;
2483 int do_bin;
2484 int do_alpha;
2485 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002488 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002489 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002490 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002491 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002492 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002493 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002494 pos_T startpos;
2495 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002496 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002498 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2499 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2500 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2501 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2502 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002504 if (virtual_active())
2505 {
2506 save_coladd = pos->coladd;
2507 pos->coladd = 0;
2508 }
2509
Bram Moolenaard79e5502016-01-10 22:13:02 +01002510 curwin->w_cursor = *pos;
2511 ptr = ml_get(pos->lnum);
2512 col = pos->col;
2513
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002514 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002515 goto theend;
2516
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 /*
2518 * First check if we are on a hexadecimal number, after the "0x".
2519 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002520 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002522 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002523 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002524 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002525 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002526 if (has_mbyte)
2527 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002528 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002529
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002530 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002531 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002532 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002533 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002534 if (has_mbyte)
2535 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002536 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002537
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002538 if ( do_bin
2539 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002540 && ! ((col > 0
2541 && (ptr[col] == 'X'
2542 || ptr[col] == 'x')
2543 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002544 && (!has_mbyte ||
2545 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002546 && vim_isxdigit(ptr[col + 1]))))
2547 {
2548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002549 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002550
Bram Moolenaard79e5502016-01-10 22:13:02 +01002551 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002552
2553 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002554 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002555 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002556 if (has_mbyte)
2557 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002558 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002559 }
2560
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002561 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002562 && col > 0
2563 && (ptr[col] == 'X'
2564 || ptr[col] == 'x')
2565 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002566 && (!has_mbyte ||
2567 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002568 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002569 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002570 && col > 0
2571 && (ptr[col] == 'B'
2572 || ptr[col] == 'b')
2573 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002574 && (!has_mbyte ||
2575 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002576 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002577 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002578 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002579 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002580 if (has_mbyte)
2581 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002582 }
2583 else
2584 {
2585 /*
2586 * Search forward and then backward to find the start of number.
2587 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002588 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002589
2590 while (ptr[col] != NUL
2591 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002592 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002593 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002594
2595 while (col > 0
2596 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002597 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002598 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002599 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002600 if (has_mbyte)
2601 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002602 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002603 }
2604 }
2605
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002606 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002607 {
2608 while (ptr[col] != NUL && length > 0
2609 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002610 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002611 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002612 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002613
2614 col += mb_len;
2615 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002616 }
2617
2618 if (length == 0)
2619 goto theend;
2620
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002621 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002622 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2623 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002624 {
2625 negative = TRUE;
2626 was_positive = FALSE;
2627 }
2628 }
2629
2630 /*
2631 * If a number was found, and saving for undo works, replace the number.
2632 */
2633 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002634 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002635 {
2636 beep_flush();
2637 goto theend;
2638 }
2639
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002640 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002641 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002642 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002643 if (op_type == OP_NR_SUB)
2644 {
2645 if (CharOrd(firstdigit) < Prenum1)
2646 {
2647 if (isupper(firstdigit))
2648 firstdigit = 'A';
2649 else
2650 firstdigit = 'a';
2651 }
2652 else
2653#ifdef EBCDIC
2654 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2655#else
2656 firstdigit -= Prenum1;
2657#endif
2658 }
2659 else
2660 {
2661 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2662 {
2663 if (isupper(firstdigit))
2664 firstdigit = 'Z';
2665 else
2666 firstdigit = 'z';
2667 }
2668 else
2669#ifdef EBCDIC
2670 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2671#else
2672 firstdigit += Prenum1;
2673#endif
2674 }
2675 curwin->w_cursor.col = col;
2676 if (!did_change)
2677 startpos = curwin->w_cursor;
2678 did_change = TRUE;
2679 (void)del_char(FALSE);
2680 ins_char(firstdigit);
2681 endpos = curwin->w_cursor;
2682 curwin->w_cursor.col = col;
2683 }
2684 else
2685 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002686 pos_T save_pos;
2687 int i;
2688
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002689 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002690 && (!has_mbyte ||
2691 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002692 && !visual
2693 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002695 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002696 --col;
2697 negative = TRUE;
2698 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002700 if (visual && VIsual_mode != 'V')
2701 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2702 ? (int)STRLEN(ptr) - col
2703 : length);
2704
2705 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002706 0 + (do_bin ? STR2NR_BIN : 0)
2707 + (do_oct ? STR2NR_OCT : 0)
2708 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002709 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002712 if (pre && negative)
2713 {
2714 ++col;
2715 --length;
2716 negative = FALSE;
2717 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002719 subtract = FALSE;
2720 if (op_type == OP_NR_SUB)
2721 subtract ^= TRUE;
2722 if (negative)
2723 subtract ^= TRUE;
2724
2725 oldn = n;
2726 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002727 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002728 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002729 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002731 if (!pre)
2732 {
2733 if (subtract)
2734 {
2735 if (n > oldn)
2736 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002737 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002738 negative ^= TRUE;
2739 }
2740 }
2741 else
2742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002744 if (n < oldn)
2745 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002746 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002747 negative ^= TRUE;
2748 }
2749 }
2750 if (n == 0)
2751 negative = FALSE;
2752 }
2753
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002754 if (do_unsigned && negative)
2755 {
2756 if (subtract)
2757 // sticking at zero.
2758 n = (uvarnumber_T)0;
2759 else
2760 // sticking at 2^64 - 1.
2761 n = (uvarnumber_T)(-1);
2762 negative = FALSE;
2763 }
2764
Bram Moolenaard79e5502016-01-10 22:13:02 +01002765 if (visual && !was_positive && !negative && col > 0)
2766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002768 col--;
2769 length++;
2770 }
2771
2772 /*
2773 * Delete the old number.
2774 */
2775 curwin->w_cursor.col = col;
2776 if (!did_change)
2777 startpos = curwin->w_cursor;
2778 did_change = TRUE;
2779 todel = length;
2780 c = gchar_cursor();
2781 /*
2782 * Don't include the '-' in the length, only the length of the
2783 * part after it is kept the same.
2784 */
2785 if (c == '-')
2786 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002787
2788 save_pos = curwin->w_cursor;
2789 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002790 {
2791 if (c < 0x100 && isalpha(c))
2792 {
2793 if (isupper(c))
2794 hexupper = TRUE;
2795 else
2796 hexupper = FALSE;
2797 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002798 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002799 c = gchar_cursor();
2800 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002801 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002802
2803 /*
2804 * Prepare the leading characters in buf1[].
2805 * When there are many leading zeros it could be very long.
2806 * Allocate a bit too much.
2807 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002808 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002809 if (buf1 == NULL)
2810 goto theend;
2811 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002812 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002813 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002814 if (pre)
2815 {
2816 *ptr++ = '0';
2817 --length;
2818 }
2819 if (pre == 'b' || pre == 'B' ||
2820 pre == 'x' || pre == 'X')
2821 {
2822 *ptr++ = pre;
2823 --length;
2824 }
2825
2826 /*
2827 * Put the number characters in buf2[].
2828 */
2829 if (pre == 'b' || pre == 'B')
2830 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002831 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002832 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002833
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002834 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002835 for (bit = bits; bit > 0; bit--)
2836 if ((n >> (bit - 1)) & 0x1) break;
2837
2838 for (i = 0; bit > 0; bit--)
2839 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2840
2841 buf2[i] = '\0';
2842 }
2843 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002844 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002845 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002846 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002847 else if (pre && hexupper)
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 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002850 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002851 length -= (int)STRLEN(buf2);
2852
2853 /*
2854 * Adjust number of zeros to the new number of digits, so the
2855 * total length of the number remains the same.
2856 * Don't do this when
2857 * the result may look like an octal number.
2858 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002859 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002860 while (length-- > 0)
2861 *ptr++ = '0';
2862 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002863
Bram Moolenaard79e5502016-01-10 22:13:02 +01002864 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002865
2866 // Insert just after the first character to be removed, so that any
2867 // text properties will be adjusted. Then delete the old number
2868 // afterwards.
2869 save_pos = curwin->w_cursor;
2870 if (todel > 0)
2871 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002873 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002874
2875 // del_char() will also mark line needing displaying
2876 if (todel > 0)
2877 {
2878 int bytes_after = (int)STRLEN(ml_get_curline())
2879 - curwin->w_cursor.col;
2880
2881 // Delete the one character before the insert.
2882 curwin->w_cursor = save_pos;
2883 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002884 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2885 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002886 --todel;
2887 }
2888 while (todel-- > 0)
2889 (void)del_char(FALSE);
2890
Bram Moolenaard79e5502016-01-10 22:13:02 +01002891 endpos = curwin->w_cursor;
2892 if (did_change && curwin->w_cursor.col)
2893 --curwin->w_cursor.col;
2894 }
2895
Bram Moolenaare1004402020-10-24 20:49:43 +02002896 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002899 curbuf->b_op_start = startpos;
2900 curbuf->b_op_end = endpos;
2901 if (curbuf->b_op_end.col > 0)
2902 --curbuf->b_op_end.col;
2903 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002904
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002905theend:
2906 if (visual)
2907 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002908 else if (did_change)
2909 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002910 else if (virtual_active())
2911 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002912
Bram Moolenaard79e5502016-01-10 22:13:02 +01002913 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914}
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002917clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002919 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002923 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 *
2925 * "Words" are counted by looking for boundaries between non-space and
2926 * space characters. (it seems to produce results that match 'wc'.)
2927 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002928 * Return value is byte count; word count for the line is added to "*wc".
2929 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 *
2931 * The function will only examine the first "limit" characters in the
2932 * line, stopping if it encounters an end-of-line (NUL byte). In that
2933 * case, eol_size will be added to the character count to account for
2934 * the size of the EOL character.
2935 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002936 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002937line_count_info(
2938 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002939 varnumber_T *wc,
2940 varnumber_T *cc,
2941 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002942 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002944 varnumber_T i;
2945 varnumber_T words = 0;
2946 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 int is_word = 0;
2948
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002949 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
2951 if (is_word)
2952 {
2953 if (vim_isspace(line[i]))
2954 {
2955 words++;
2956 is_word = 0;
2957 }
2958 }
2959 else if (!vim_isspace(line[i]))
2960 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002961 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002962 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964
2965 if (is_word)
2966 words++;
2967 *wc += words;
2968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002970 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002973 chars += eol_size;
2974 }
2975 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 return i;
2977}
2978
2979/*
2980 * Give some info about the position of the cursor (for "g CTRL-G").
2981 * In Visual mode, give some info about the selected region. (In this case,
2982 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002983 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 */
2985 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002986cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002989 char_u buf1[50];
2990 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002992 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002993 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002994 varnumber_T byte_count_cursor = 0;
2995 varnumber_T char_count = 0;
2996 varnumber_T char_count_cursor = 0;
2997 varnumber_T word_count = 0;
2998 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002999 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003000 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 long line_count_selected = 0;
3002 pos_T min_pos, max_pos;
3003 oparg_T oparg;
3004 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
3006 /*
3007 * Compute the length of the file in characters.
3008 */
3009 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3010 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003011 if (dict == NULL)
3012 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003013 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003014 return;
3015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017 else
3018 {
3019 if (get_fileformat(curbuf) == EOL_DOS)
3020 eol_size = 2;
3021 else
3022 eol_size = 1;
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (VIsual_active)
3025 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003026 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
3028 min_pos = VIsual;
3029 max_pos = curwin->w_cursor;
3030 }
3031 else
3032 {
3033 min_pos = curwin->w_cursor;
3034 max_pos = VIsual;
3035 }
3036 if (*p_sel == 'e' && max_pos.col > 0)
3037 --max_pos.col;
3038
3039 if (VIsual_mode == Ctrl_V)
3040 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003041#ifdef FEAT_LINEBREAK
3042 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003043 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003046 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003047 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 oparg.is_VIsual = 1;
3050 oparg.block_mode = TRUE;
3051 oparg.op_type = OP_NOP;
3052 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003053 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003054#ifdef FEAT_LINEBREAK
3055 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003056 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003057#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003058 if (curwin->w_curswant == MAXCOL)
3059 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003060 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 if (oparg.end_vcol < oparg.start_vcol)
3062 {
3063 oparg.end_vcol += oparg.start_vcol;
3064 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3065 oparg.end_vcol -= oparg.start_vcol;
3066 }
3067 }
3068 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003073 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003074 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 ui_breakcheck();
3077 if (got_int)
3078 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003079 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 }
3081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 if (VIsual_active
3084 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3085 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003086 char_u *s = NULL;
3087 long len = 0L;
3088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 switch (VIsual_mode)
3090 {
3091 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003095 s = bd.textstart;
3096 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 break;
3098 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003099 s = ml_get(lnum);
3100 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 break;
3102 case 'v':
3103 {
3104 colnr_T start_col = (lnum == min_pos.lnum)
3105 ? min_pos.col : 0;
3106 colnr_T end_col = (lnum == max_pos.lnum)
3107 ? max_pos.col - start_col + 1 : MAXCOL;
3108
Bram Moolenaardef9e822004-12-31 20:58:58 +00003109 s = ml_get(lnum) + start_col;
3110 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112 break;
3113 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003114 if (s != NULL)
3115 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003116 byte_count_cursor += line_count_info(s, &word_count_cursor,
3117 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003118 if (lnum == curbuf->b_ml.ml_line_count
3119 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003120 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003121 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003122 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
3125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (lnum == curwin->w_cursor.lnum)
3129 {
3130 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003131 char_count_cursor += char_count;
3132 byte_count_cursor = byte_count +
3133 line_count_info(ml_get(lnum),
3134 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003135 (varnumber_T)(curwin->w_cursor.col + 1),
3136 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
3138 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003139 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003140 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003141 &char_count, (varnumber_T)MAXCOL,
3142 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
3144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003146 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003147 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
Bram Moolenaared767a22016-01-03 22:49:16 +01003149 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003151 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003153 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3154 {
3155 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3156 &max_pos.col);
3157 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3158 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3159 }
3160 else
3161 buf1[0] = NUL;
3162
3163 if (char_count_cursor == byte_count_cursor
3164 && char_count == byte_count)
3165 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003166 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003167 buf1, line_count_selected,
3168 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003169 (varnumber_T)word_count_cursor,
3170 (varnumber_T)word_count,
3171 (varnumber_T)byte_count_cursor,
3172 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003173 else
3174 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003175 _("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 +01003176 buf1, line_count_selected,
3177 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003178 (varnumber_T)word_count_cursor,
3179 (varnumber_T)word_count,
3180 (varnumber_T)char_count_cursor,
3181 (varnumber_T)char_count,
3182 (varnumber_T)byte_count_cursor,
3183 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003186 {
3187 p = ml_get_curline();
3188 validate_virtcol();
3189 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3190 (int)curwin->w_virtcol + 1);
3191 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3192 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaared767a22016-01-03 22:49:16 +01003194 if (char_count_cursor == byte_count_cursor
3195 && char_count == byte_count)
3196 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003197 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003198 (char *)buf1, (char *)buf2,
3199 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003201 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3202 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003203 else
3204 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003205 _("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 +01003206 (char *)buf1, (char *)buf2,
3207 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003208 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003209 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3210 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3211 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003212 }
3213 }
3214
Bram Moolenaared767a22016-01-03 22:49:16 +01003215 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003216 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003217 {
3218 size_t len = STRLEN(IObuff);
3219
3220 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003221 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003222 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003223 if (dict == NULL)
3224 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003225 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003226 p = p_shm;
3227 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003228 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003229 p_shm = p;
3230 }
3231 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003232#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003233 if (dict != NULL)
3234 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003235 dict_add_number(dict, "words", word_count);
3236 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003237 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003238 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3239 byte_count_cursor);
3240 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3241 char_count_cursor);
3242 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3243 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003247
3248/*
3249 * Handle indent and format operators and visual mode ":".
3250 */
3251 static void
3252op_colon(oparg_T *oap)
3253{
3254 stuffcharReadbuff(':');
3255 if (oap->is_VIsual)
3256 stuffReadbuff((char_u *)"'<,'>");
3257 else
3258 {
3259 // Make the range look nice, so it can be repeated.
3260 if (oap->start.lnum == curwin->w_cursor.lnum)
3261 stuffcharReadbuff('.');
3262 else
3263 stuffnumReadbuff((long)oap->start.lnum);
3264 if (oap->end.lnum != oap->start.lnum)
3265 {
3266 stuffcharReadbuff(',');
3267 if (oap->end.lnum == curwin->w_cursor.lnum)
3268 stuffcharReadbuff('.');
3269 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3270 stuffcharReadbuff('$');
3271 else if (oap->start.lnum == curwin->w_cursor.lnum)
3272 {
3273 stuffReadbuff((char_u *)".+");
3274 stuffnumReadbuff((long)oap->line_count - 1);
3275 }
3276 else
3277 stuffnumReadbuff((long)oap->end.lnum);
3278 }
3279 }
3280 if (oap->op_type != OP_COLON)
3281 stuffReadbuff((char_u *)"!");
3282 if (oap->op_type == OP_INDENT)
3283 {
3284#ifndef FEAT_CINDENT
3285 if (*get_equalprg() == NUL)
3286 stuffReadbuff((char_u *)"indent");
3287 else
3288#endif
3289 stuffReadbuff(get_equalprg());
3290 stuffReadbuff((char_u *)"\n");
3291 }
3292 else if (oap->op_type == OP_FORMAT)
3293 {
3294 if (*curbuf->b_p_fp != NUL)
3295 stuffReadbuff(curbuf->b_p_fp);
3296 else if (*p_fp != NUL)
3297 stuffReadbuff(p_fp);
3298 else
3299 stuffReadbuff((char_u *)"fmt");
3300 stuffReadbuff((char_u *)"\n']");
3301 }
3302
3303 // do_cmdline() does the rest
3304}
3305
3306/*
3307 * Handle the "g@" operator: call 'operatorfunc'.
3308 */
3309 static void
3310op_function(oparg_T *oap UNUSED)
3311{
3312#ifdef FEAT_EVAL
3313 typval_T argv[2];
3314 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003315 pos_T orig_start = curbuf->b_op_start;
3316 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003317
3318 if (*p_opfunc == NUL)
3319 emsg(_("E774: 'operatorfunc' is empty"));
3320 else
3321 {
3322 // Set '[ and '] marks to text to be operated on.
3323 curbuf->b_op_start = oap->start;
3324 curbuf->b_op_end = oap->end;
3325 if (oap->motion_type != MLINE && !oap->inclusive)
3326 // Exclude the end position.
3327 decl(&curbuf->b_op_end);
3328
3329 argv[0].v_type = VAR_STRING;
3330 if (oap->block_mode)
3331 argv[0].vval.v_string = (char_u *)"block";
3332 else if (oap->motion_type == MLINE)
3333 argv[0].vval.v_string = (char_u *)"line";
3334 else
3335 argv[0].vval.v_string = (char_u *)"char";
3336 argv[1].v_type = VAR_UNKNOWN;
3337
3338 // Reset virtual_op so that 'virtualedit' can be changed in the
3339 // function.
3340 virtual_op = MAYBE;
3341
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003342 (void)call_func_noret(p_opfunc, 1, argv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003343
3344 virtual_op = save_virtual_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003345 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003346 {
3347 curbuf->b_op_start = orig_start;
3348 curbuf->b_op_end = orig_end;
3349 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003350 }
3351#else
3352 emsg(_("E775: Eval feature not available"));
3353#endif
3354}
3355
3356/*
3357 * Calculate start/end virtual columns for operating in block mode.
3358 */
3359 static void
3360get_op_vcol(
3361 oparg_T *oap,
3362 colnr_T redo_VIsual_vcol,
3363 int initial) // when TRUE adjust position for 'selectmode'
3364{
3365 colnr_T start, end;
3366
3367 if (VIsual_mode != Ctrl_V
3368 || (!initial && oap->end.col < curwin->w_width))
3369 return;
3370
3371 oap->block_mode = TRUE;
3372
3373 // prevent from moving onto a trail byte
3374 if (has_mbyte)
3375 mb_adjustpos(curwin->w_buffer, &oap->end);
3376
3377 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3378
3379 if (!redo_VIsual_busy)
3380 {
3381 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3382
3383 if (start < oap->start_vcol)
3384 oap->start_vcol = start;
3385 if (end > oap->end_vcol)
3386 {
3387 if (initial && *p_sel == 'e' && start >= 1
3388 && start - 1 >= oap->end_vcol)
3389 oap->end_vcol = start - 1;
3390 else
3391 oap->end_vcol = end;
3392 }
3393 }
3394
3395 // if '$' was used, get oap->end_vcol from longest line
3396 if (curwin->w_curswant == MAXCOL)
3397 {
3398 curwin->w_cursor.col = MAXCOL;
3399 oap->end_vcol = 0;
3400 for (curwin->w_cursor.lnum = oap->start.lnum;
3401 curwin->w_cursor.lnum <= oap->end.lnum;
3402 ++curwin->w_cursor.lnum)
3403 {
3404 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3405 if (end > oap->end_vcol)
3406 oap->end_vcol = end;
3407 }
3408 }
3409 else if (redo_VIsual_busy)
3410 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3411 // Correct oap->end.col and oap->start.col to be the
3412 // upper-left and lower-right corner of the block area.
3413 //
3414 // (Actually, this does convert column positions into character
3415 // positions)
3416 curwin->w_cursor.lnum = oap->end.lnum;
3417 coladvance(oap->end_vcol);
3418 oap->end = curwin->w_cursor;
3419
3420 curwin->w_cursor = oap->start;
3421 coladvance(oap->start_vcol);
3422 oap->start = curwin->w_cursor;
3423}
3424
3425/*
3426 * Handle an operator after Visual mode or when the movement is finished.
3427 * "gui_yank" is true when yanking text for the clipboard.
3428 */
3429 void
3430do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3431{
3432 oparg_T *oap = cap->oap;
3433 pos_T old_cursor;
3434 int empty_region_error;
3435 int restart_edit_save;
3436#ifdef FEAT_LINEBREAK
3437 int lbr_saved = curwin->w_p_lbr;
3438#endif
3439
3440 // The visual area is remembered for redo
3441 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3442 static linenr_T redo_VIsual_line_count; // number of lines
3443 static colnr_T redo_VIsual_vcol; // number of cols or end column
3444 static long redo_VIsual_count; // count for Visual operator
3445 static int redo_VIsual_arg; // extra argument
3446 int include_line_break = FALSE;
3447
3448#if defined(FEAT_CLIPBOARD)
3449 // Yank the visual area into the GUI selection register before we operate
3450 // on it and lose it forever.
3451 // Don't do it if a specific register was specified, so that ""x"*P works.
3452 // This could call do_pending_operator() recursively, but that's OK
3453 // because gui_yank will be TRUE for the nested call.
3454 if ((clip_star.available || clip_plus.available)
3455 && oap->op_type != OP_NOP
3456 && !gui_yank
3457 && VIsual_active
3458 && !redo_VIsual_busy
3459 && oap->regname == 0)
3460 clip_auto_select();
3461#endif
3462 old_cursor = curwin->w_cursor;
3463
3464 // If an operation is pending, handle it...
3465 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3466 {
3467 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3468 // for the clipboard.
3469 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3470
3471#ifdef FEAT_LINEBREAK
3472 // Avoid a problem with unwanted linebreaks in block mode.
3473 if (curwin->w_p_lbr)
3474 curwin->w_valid &= ~VALID_VIRTCOL;
3475 curwin->w_p_lbr = FALSE;
3476#endif
3477 oap->is_VIsual = VIsual_active;
3478 if (oap->motion_force == 'V')
3479 oap->motion_type = MLINE;
3480 else if (oap->motion_force == 'v')
3481 {
3482 // If the motion was linewise, "inclusive" will not have been set.
3483 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3484 if (oap->motion_type == MLINE)
3485 oap->inclusive = FALSE;
3486 // If the motion already was characterwise, toggle "inclusive"
3487 else if (oap->motion_type == MCHAR)
3488 oap->inclusive = !oap->inclusive;
3489 oap->motion_type = MCHAR;
3490 }
3491 else if (oap->motion_force == Ctrl_V)
3492 {
3493 // Change line- or characterwise motion into Visual block mode.
3494 if (!VIsual_active)
3495 {
3496 VIsual_active = TRUE;
3497 VIsual = oap->start;
3498 }
3499 VIsual_mode = Ctrl_V;
3500 VIsual_select = FALSE;
3501 VIsual_reselect = FALSE;
3502 }
3503
3504 // Only redo yank when 'y' flag is in 'cpoptions'.
3505 // Never redo "zf" (define fold).
3506 if ((redo_yank || oap->op_type != OP_YANK)
3507 && ((!VIsual_active || oap->motion_force)
3508 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003509 || (VIsual_active
3510 && (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3511 && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003512 && cap->cmdchar != 'D'
3513#ifdef FEAT_FOLDING
3514 && oap->op_type != OP_FOLD
3515 && oap->op_type != OP_FOLDOPEN
3516 && oap->op_type != OP_FOLDOPENREC
3517 && oap->op_type != OP_FOLDCLOSE
3518 && oap->op_type != OP_FOLDCLOSEREC
3519 && oap->op_type != OP_FOLDDEL
3520 && oap->op_type != OP_FOLDDELREC
3521#endif
3522 )
3523 {
3524 prep_redo(oap->regname, cap->count0,
3525 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3526 oap->motion_force, cap->cmdchar, cap->nchar);
3527 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3528 {
3529 // If 'cpoptions' does not contain 'r', insert the search
3530 // pattern to really repeat the same command.
3531 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3532 AppendToRedobuffLit(cap->searchbuf, -1);
3533 AppendToRedobuff(NL_STR);
3534 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003535 else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003536 {
3537 // do_cmdline() has stored the first typed line in
3538 // "repeat_cmdline". When several lines are typed repeating
3539 // won't be possible.
3540 if (repeat_cmdline == NULL)
3541 ResetRedobuff();
3542 else
3543 {
3544 AppendToRedobuffLit(repeat_cmdline, -1);
3545 AppendToRedobuff(NL_STR);
3546 VIM_CLEAR(repeat_cmdline);
3547 }
3548 }
3549 }
3550
3551 if (redo_VIsual_busy)
3552 {
3553 // Redo of an operation on a Visual area. Use the same size from
3554 // redo_VIsual_line_count and redo_VIsual_vcol.
3555 oap->start = curwin->w_cursor;
3556 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3557 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3558 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3559 VIsual_mode = redo_VIsual_mode;
3560 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3561 {
3562 if (VIsual_mode == 'v')
3563 {
3564 if (redo_VIsual_line_count <= 1)
3565 {
3566 validate_virtcol();
3567 curwin->w_curswant =
3568 curwin->w_virtcol + redo_VIsual_vcol - 1;
3569 }
3570 else
3571 curwin->w_curswant = redo_VIsual_vcol;
3572 }
3573 else
3574 {
3575 curwin->w_curswant = MAXCOL;
3576 }
3577 coladvance(curwin->w_curswant);
3578 }
3579 cap->count0 = redo_VIsual_count;
3580 if (redo_VIsual_count != 0)
3581 cap->count1 = redo_VIsual_count;
3582 else
3583 cap->count1 = 1;
3584 }
3585 else if (VIsual_active)
3586 {
3587 if (!gui_yank)
3588 {
3589 // Save the current VIsual area for '< and '> marks, and "gv"
3590 curbuf->b_visual.vi_start = VIsual;
3591 curbuf->b_visual.vi_end = curwin->w_cursor;
3592 curbuf->b_visual.vi_mode = VIsual_mode;
3593 restore_visual_mode();
3594 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3595# ifdef FEAT_EVAL
3596 curbuf->b_visual_mode_eval = VIsual_mode;
3597# endif
3598 }
3599
3600 // In Select mode, a linewise selection is operated upon like a
3601 // characterwise selection.
3602 // Special case: gH<Del> deletes the last line.
3603 if (VIsual_select && VIsual_mode == 'V'
3604 && cap->oap->op_type != OP_DELETE)
3605 {
3606 if (LT_POS(VIsual, curwin->w_cursor))
3607 {
3608 VIsual.col = 0;
3609 curwin->w_cursor.col =
3610 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3611 }
3612 else
3613 {
3614 curwin->w_cursor.col = 0;
3615 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3616 }
3617 VIsual_mode = 'v';
3618 }
3619 // If 'selection' is "exclusive", backup one character for
3620 // charwise selections.
3621 else if (VIsual_mode == 'v')
3622 include_line_break = unadjust_for_sel();
3623
3624 oap->start = VIsual;
3625 if (VIsual_mode == 'V')
3626 {
3627 oap->start.col = 0;
3628 oap->start.coladd = 0;
3629 }
3630 }
3631
3632 // Set oap->start to the first position of the operated text, oap->end
3633 // to the end of the operated text. w_cursor is equal to oap->start.
3634 if (LT_POS(oap->start, curwin->w_cursor))
3635 {
3636#ifdef FEAT_FOLDING
3637 // Include folded lines completely.
3638 if (!VIsual_active)
3639 {
3640 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3641 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003642 if ((curwin->w_cursor.col > 0 || oap->inclusive
3643 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003644 && hasFolding(curwin->w_cursor.lnum, NULL,
3645 &curwin->w_cursor.lnum))
3646 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3647 }
3648#endif
3649 oap->end = curwin->w_cursor;
3650 curwin->w_cursor = oap->start;
3651
3652 // w_virtcol may have been updated; if the cursor goes back to its
3653 // previous position w_virtcol becomes invalid and isn't updated
3654 // automatically.
3655 curwin->w_valid &= ~VALID_VIRTCOL;
3656 }
3657 else
3658 {
3659#ifdef FEAT_FOLDING
3660 // Include folded lines completely.
3661 if (!VIsual_active && oap->motion_type == MLINE)
3662 {
3663 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3664 NULL))
3665 curwin->w_cursor.col = 0;
3666 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3667 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3668 }
3669#endif
3670 oap->end = oap->start;
3671 oap->start = curwin->w_cursor;
3672 }
3673
3674 // Just in case lines were deleted that make the position invalid.
3675 check_pos(curwin->w_buffer, &oap->end);
3676 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3677
3678 // Set "virtual_op" before resetting VIsual_active.
3679 virtual_op = virtual_active();
3680
3681 if (VIsual_active || redo_VIsual_busy)
3682 {
3683 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3684
3685 if (!redo_VIsual_busy && !gui_yank)
3686 {
3687 // Prepare to reselect and redo Visual: this is based on the
3688 // size of the Visual text
3689 resel_VIsual_mode = VIsual_mode;
3690 if (curwin->w_curswant == MAXCOL)
3691 resel_VIsual_vcol = MAXCOL;
3692 else
3693 {
3694 if (VIsual_mode != Ctrl_V)
3695 getvvcol(curwin, &(oap->end),
3696 NULL, NULL, &oap->end_vcol);
3697 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3698 {
3699 if (VIsual_mode != Ctrl_V)
3700 getvvcol(curwin, &(oap->start),
3701 &oap->start_vcol, NULL, NULL);
3702 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3703 }
3704 else
3705 resel_VIsual_vcol = oap->end_vcol;
3706 }
3707 resel_VIsual_line_count = oap->line_count;
3708 }
3709
3710 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3711 if ((redo_yank || oap->op_type != OP_YANK)
3712 && oap->op_type != OP_COLON
3713#ifdef FEAT_FOLDING
3714 && oap->op_type != OP_FOLD
3715 && oap->op_type != OP_FOLDOPEN
3716 && oap->op_type != OP_FOLDOPENREC
3717 && oap->op_type != OP_FOLDCLOSE
3718 && oap->op_type != OP_FOLDCLOSEREC
3719 && oap->op_type != OP_FOLDDEL
3720 && oap->op_type != OP_FOLDDELREC
3721#endif
3722 && oap->motion_force == NUL
3723 )
3724 {
3725 // Prepare for redoing. Only use the nchar field for "r",
3726 // otherwise it might be the second char of the operator.
3727 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3728 || cap->nchar == 'N'))
3729 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003730 get_op_char(oap->op_type),
3731 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003732 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003733 else if (cap->cmdchar != ':' && cap->cmdchar != K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003734 {
3735 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3736
3737 // reverse what nv_replace() did
3738 if (nchar == REPLACE_CR_NCHAR)
3739 nchar = CAR;
3740 else if (nchar == REPLACE_NL_NCHAR)
3741 nchar = NL;
3742 prep_redo(oap->regname, 0L, NUL, 'v',
3743 get_op_char(oap->op_type),
3744 get_extra_op_char(oap->op_type),
3745 nchar);
3746 }
3747 if (!redo_VIsual_busy)
3748 {
3749 redo_VIsual_mode = resel_VIsual_mode;
3750 redo_VIsual_vcol = resel_VIsual_vcol;
3751 redo_VIsual_line_count = resel_VIsual_line_count;
3752 redo_VIsual_count = cap->count0;
3753 redo_VIsual_arg = cap->arg;
3754 }
3755 }
3756
3757 // oap->inclusive defaults to TRUE.
3758 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3759 // FALSE. This makes "d}P" and "v}dP" work the same.
3760 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3761 oap->inclusive = TRUE;
3762 if (VIsual_mode == 'V')
3763 oap->motion_type = MLINE;
3764 else
3765 {
3766 oap->motion_type = MCHAR;
3767 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3768 && (include_line_break || !virtual_op))
3769 {
3770 oap->inclusive = FALSE;
3771 // Try to include the newline, unless it's an operator
3772 // that works on lines only.
3773 if (*p_sel != 'o'
3774 && !op_on_lines(oap->op_type)
3775 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3776 {
3777 ++oap->end.lnum;
3778 oap->end.col = 0;
3779 oap->end.coladd = 0;
3780 ++oap->line_count;
3781 }
3782 }
3783 }
3784
3785 redo_VIsual_busy = FALSE;
3786
3787 // Switch Visual off now, so screen updating does
3788 // not show inverted text when the screen is redrawn.
3789 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3790 // no screen redraw, so it is done here to remove the inverted
3791 // part.
3792 if (!gui_yank)
3793 {
3794 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003795 setmouse();
3796 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003797 may_clear_cmdline();
3798 if ((oap->op_type == OP_YANK
3799 || oap->op_type == OP_COLON
3800 || oap->op_type == OP_FUNCTION
3801 || oap->op_type == OP_FILTER)
3802 && oap->motion_force == NUL)
3803 {
3804#ifdef FEAT_LINEBREAK
3805 // make sure redrawing is correct
3806 curwin->w_p_lbr = lbr_saved;
3807#endif
3808 redraw_curbuf_later(INVERTED);
3809 }
3810 }
3811 }
3812
3813 // Include the trailing byte of a multi-byte char.
3814 if (has_mbyte && oap->inclusive)
3815 {
3816 int l;
3817
3818 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3819 if (l > 1)
3820 oap->end.col += l - 1;
3821 }
3822 curwin->w_set_curswant = TRUE;
3823
3824 // oap->empty is set when start and end are the same. The inclusive
3825 // flag affects this too, unless yanking and the end is on a NUL.
3826 oap->empty = (oap->motion_type == MCHAR
3827 && (!oap->inclusive
3828 || (oap->op_type == OP_YANK
3829 && gchar_pos(&oap->end) == NUL))
3830 && EQUAL_POS(oap->start, oap->end)
3831 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3832 // For delete, change and yank, it's an error to operate on an
3833 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3834 empty_region_error = (oap->empty
3835 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3836
3837 // Force a redraw when operating on an empty Visual region, when
3838 // 'modifiable is off or creating a fold.
3839 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3840#ifdef FEAT_FOLDING
3841 || oap->op_type == OP_FOLD
3842#endif
3843 ))
3844 {
3845#ifdef FEAT_LINEBREAK
3846 curwin->w_p_lbr = lbr_saved;
3847#endif
3848 redraw_curbuf_later(INVERTED);
3849 }
3850
3851 // If the end of an operator is in column one while oap->motion_type
3852 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3853 // character in the previous line. If op_start is on or before the
3854 // first non-blank in the line, the operator becomes linewise
3855 // (strange, but that's the way vi does it).
3856 if ( oap->motion_type == MCHAR
3857 && oap->inclusive == FALSE
3858 && !(cap->retval & CA_NO_ADJ_OP_END)
3859 && oap->end.col == 0
3860 && (!oap->is_VIsual || *p_sel == 'o')
3861 && !oap->block_mode
3862 && oap->line_count > 1)
3863 {
3864 oap->end_adjusted = TRUE; // remember that we did this
3865 --oap->line_count;
3866 --oap->end.lnum;
3867 if (inindent(0))
3868 oap->motion_type = MLINE;
3869 else
3870 {
3871 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3872 if (oap->end.col)
3873 {
3874 --oap->end.col;
3875 oap->inclusive = TRUE;
3876 }
3877 }
3878 }
3879 else
3880 oap->end_adjusted = FALSE;
3881
3882 switch (oap->op_type)
3883 {
3884 case OP_LSHIFT:
3885 case OP_RSHIFT:
3886 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3887 auto_format(FALSE, TRUE);
3888 break;
3889
3890 case OP_JOIN_NS:
3891 case OP_JOIN:
3892 if (oap->line_count < 2)
3893 oap->line_count = 2;
3894 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3895 curbuf->b_ml.ml_line_count)
3896 beep_flush();
3897 else
3898 {
3899 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3900 TRUE, TRUE, TRUE);
3901 auto_format(FALSE, TRUE);
3902 }
3903 break;
3904
3905 case OP_DELETE:
3906 VIsual_reselect = FALSE; // don't reselect now
3907 if (empty_region_error)
3908 {
3909 vim_beep(BO_OPER);
3910 CancelRedo();
3911 }
3912 else
3913 {
3914 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01003915 // save cursor line for undo if it wasn't saved yet
3916 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3917 && u_save_cursor() == OK)
3918 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003919 }
3920 break;
3921
3922 case OP_YANK:
3923 if (empty_region_error)
3924 {
3925 if (!gui_yank)
3926 {
3927 vim_beep(BO_OPER);
3928 CancelRedo();
3929 }
3930 }
3931 else
3932 {
3933#ifdef FEAT_LINEBREAK
3934 curwin->w_p_lbr = lbr_saved;
3935#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02003936 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003937 (void)op_yank(oap, FALSE, !gui_yank);
3938 }
3939 check_cursor_col();
3940 break;
3941
3942 case OP_CHANGE:
3943 VIsual_reselect = FALSE; // don't reselect now
3944 if (empty_region_error)
3945 {
3946 vim_beep(BO_OPER);
3947 CancelRedo();
3948 }
3949 else
3950 {
3951 // This is a new edit command, not a restart. Need to
3952 // remember it to make 'insertmode' work with mappings for
3953 // Visual mode. But do this only once and not when typed and
3954 // 'insertmode' isn't set.
3955 if (p_im || !KeyTyped)
3956 restart_edit_save = restart_edit;
3957 else
3958 restart_edit_save = 0;
3959 restart_edit = 0;
3960#ifdef FEAT_LINEBREAK
3961 // Restore linebreak, so that when the user edits it looks as
3962 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01003963 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003964#endif
3965 // Reset finish_op now, don't want it set inside edit().
3966 finish_op = FALSE;
3967 if (op_change(oap)) // will call edit()
3968 cap->retval |= CA_COMMAND_BUSY;
3969 if (restart_edit == 0)
3970 restart_edit = restart_edit_save;
3971 }
3972 break;
3973
3974 case OP_FILTER:
3975 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3976 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
3977 else
3978 bangredo = TRUE; // do_bang() will put cmd in redo buffer
3979 // FALLTHROUGH
3980
3981 case OP_INDENT:
3982 case OP_COLON:
3983
3984#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3985 // If 'equalprg' is empty, do the indenting internally.
3986 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3987 {
3988# ifdef FEAT_LISP
3989 if (curbuf->b_p_lisp)
3990 {
3991 op_reindent(oap, get_lisp_indent);
3992 break;
3993 }
3994# endif
3995# ifdef FEAT_CINDENT
3996 op_reindent(oap,
3997# ifdef FEAT_EVAL
3998 *curbuf->b_p_inde != NUL ? get_expr_indent :
3999# endif
4000 get_c_indent);
4001 break;
4002# endif
4003 }
4004#endif
4005
4006 op_colon(oap);
4007 break;
4008
4009 case OP_TILDE:
4010 case OP_UPPER:
4011 case OP_LOWER:
4012 case OP_ROT13:
4013 if (empty_region_error)
4014 {
4015 vim_beep(BO_OPER);
4016 CancelRedo();
4017 }
4018 else
4019 op_tilde(oap);
4020 check_cursor_col();
4021 break;
4022
4023 case OP_FORMAT:
4024#if defined(FEAT_EVAL)
4025 if (*curbuf->b_p_fex != NUL)
4026 op_formatexpr(oap); // use expression
4027 else
4028#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004029 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004030 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004031 op_colon(oap); // use external command
4032 else
4033 op_format(oap, FALSE); // use internal function
4034 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004035 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004036 case OP_FORMAT2:
4037 op_format(oap, TRUE); // use internal function
4038 break;
4039
4040 case OP_FUNCTION:
4041#ifdef FEAT_LINEBREAK
4042 // Restore linebreak, so that when the user edits it looks as
4043 // before.
4044 curwin->w_p_lbr = lbr_saved;
4045#endif
4046 op_function(oap); // call 'operatorfunc'
4047 break;
4048
4049 case OP_INSERT:
4050 case OP_APPEND:
4051 VIsual_reselect = FALSE; // don't reselect now
4052 if (empty_region_error)
4053 {
4054 vim_beep(BO_OPER);
4055 CancelRedo();
4056 }
4057 else
4058 {
4059 // This is a new edit command, not a restart. Need to
4060 // remember it to make 'insertmode' work with mappings for
4061 // Visual mode. But do this only once.
4062 restart_edit_save = restart_edit;
4063 restart_edit = 0;
4064#ifdef FEAT_LINEBREAK
4065 // Restore linebreak, so that when the user edits it looks as
4066 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004067 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004068#endif
4069 op_insert(oap, cap->count1);
4070#ifdef FEAT_LINEBREAK
4071 // Reset linebreak, so that formatting works correctly.
4072 curwin->w_p_lbr = FALSE;
4073#endif
4074
4075 // TODO: when inserting in several lines, should format all
4076 // the lines.
4077 auto_format(FALSE, TRUE);
4078
4079 if (restart_edit == 0)
4080 restart_edit = restart_edit_save;
4081 else
4082 cap->retval |= CA_COMMAND_BUSY;
4083 }
4084 break;
4085
4086 case OP_REPLACE:
4087 VIsual_reselect = FALSE; // don't reselect now
4088 if (empty_region_error)
4089 {
4090 vim_beep(BO_OPER);
4091 CancelRedo();
4092 }
4093 else
4094 {
4095#ifdef FEAT_LINEBREAK
4096 // Restore linebreak, so that when the user edits it looks as
4097 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004098 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004099#endif
4100 op_replace(oap, cap->nchar);
4101 }
4102 break;
4103
4104#ifdef FEAT_FOLDING
4105 case OP_FOLD:
4106 VIsual_reselect = FALSE; // don't reselect now
4107 foldCreate(oap->start.lnum, oap->end.lnum);
4108 break;
4109
4110 case OP_FOLDOPEN:
4111 case OP_FOLDOPENREC:
4112 case OP_FOLDCLOSE:
4113 case OP_FOLDCLOSEREC:
4114 VIsual_reselect = FALSE; // don't reselect now
4115 opFoldRange(oap->start.lnum, oap->end.lnum,
4116 oap->op_type == OP_FOLDOPEN
4117 || oap->op_type == OP_FOLDOPENREC,
4118 oap->op_type == OP_FOLDOPENREC
4119 || oap->op_type == OP_FOLDCLOSEREC,
4120 oap->is_VIsual);
4121 break;
4122
4123 case OP_FOLDDEL:
4124 case OP_FOLDDELREC:
4125 VIsual_reselect = FALSE; // don't reselect now
4126 deleteFold(oap->start.lnum, oap->end.lnum,
4127 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4128 break;
4129#endif
4130 case OP_NR_ADD:
4131 case OP_NR_SUB:
4132 if (empty_region_error)
4133 {
4134 vim_beep(BO_OPER);
4135 CancelRedo();
4136 }
4137 else
4138 {
4139 VIsual_active = TRUE;
4140#ifdef FEAT_LINEBREAK
4141 curwin->w_p_lbr = lbr_saved;
4142#endif
4143 op_addsub(oap, cap->count1, redo_VIsual_arg);
4144 VIsual_active = FALSE;
4145 }
4146 check_cursor_col();
4147 break;
4148 default:
4149 clearopbeep(oap);
4150 }
4151 virtual_op = MAYBE;
4152 if (!gui_yank)
4153 {
4154 // if 'sol' not set, go back to old column for some commands
4155 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4156 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4157 || oap->op_type == OP_DELETE))
4158 {
4159#ifdef FEAT_LINEBREAK
4160 curwin->w_p_lbr = FALSE;
4161#endif
4162 coladvance(curwin->w_curswant = old_col);
4163 }
4164 }
4165 else
4166 {
4167 curwin->w_cursor = old_cursor;
4168 }
4169 oap->block_mode = FALSE;
4170 clearop(oap);
4171 motion_force = NUL;
4172 }
4173#ifdef FEAT_LINEBREAK
4174 curwin->w_p_lbr = lbr_saved;
4175#endif
4176}