blob: 5a48550146bdfefb886ff78dae5834083f08aa39 [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 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200698 else
699 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
701 /*
702 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100703 * delete contains a line break, or when using a specific operator (Vi
704 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200705 * Use the register name from before adjust_clip_reg() may have
706 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100708 if (oap->motion_type == MLINE || oap->line_count > 1
709 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100711 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (op_yank(oap, TRUE, FALSE) == OK)
713 did_yank = TRUE;
714 }
715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100716 // Yank into small delete register when no named register specified
717 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200718 if ((
719#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200720 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
721 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200722#endif
723 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 && oap->line_count == 1)
725 {
726 oap->regname = '-';
727 get_yank_register(oap->regname, TRUE);
728 if (op_yank(oap, TRUE, FALSE) == OK)
729 did_yank = TRUE;
730 oap->regname = 0;
731 }
732
733 /*
734 * If there's too much stuff to fit in the yank register, then get a
735 * confirmation before doing the delete. This is crude, but simple.
736 * And it avoids doing a delete of something we can't put back if we
737 * want.
738 */
739 if (!did_yank)
740 {
741 int msg_silent_save = msg_silent;
742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100743 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
745 msg_silent = msg_silent_save;
746 if (n != 'y')
747 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100748 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 return FAIL;
750 }
751 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100752
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100753#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100754 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200755 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 }
758
Bram Moolenaard04b7502010-07-08 22:27:55 +0200759 /*
760 * block mode delete
761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (oap->block_mode)
763 {
764 if (u_save((linenr_T)(oap->start.lnum - 1),
765 (linenr_T)(oap->end.lnum + 1)) == FAIL)
766 return FAIL;
767
768 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
769 {
770 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 continue;
773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100774 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (lnum == curwin->w_cursor.lnum)
776 {
777 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780
Bram Moolenaar8055d172019-05-17 22:57:26 +0200781 // "n" == number of chars deleted
782 // If we delete a TAB, it may be replaced by several characters.
783 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 n = bd.textlen - bd.startspaces - bd.endspaces;
785 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200786 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (newp == NULL)
788 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200792 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100794 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000796 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200799
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100800#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200801 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200802 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805
806 check_cursor_col();
807 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
808 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100811 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {
813 if (oap->op_type == OP_CHANGE)
814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100815 // Delete the lines except the first one. Temporarily move the
816 // cursor to the next line. Save the current line number, if the
817 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (oap->line_count > 1)
819 {
820 lnum = curwin->w_cursor.lnum;
821 ++curwin->w_cursor.lnum;
822 del_lines((long)(oap->line_count - 1), TRUE);
823 curwin->w_cursor.lnum = lnum;
824 }
825 if (u_save_cursor() == FAIL)
826 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 beginline(BL_WHITE); // cursor on first non-white
830 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ai_col = curwin->w_cursor.col;
832 }
833 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100834 beginline(0); // cursor in column 0
835 truncate_line(FALSE); // delete the rest of the line
836 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 else
841 {
842 del_lines(oap->line_count, TRUE);
843 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 }
847 else
848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (virtual_op)
850 {
851 int endcol = 0;
852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (gchar_pos(&oap->start) == '\t')
855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return FAIL;
858 if (oap->line_count == 1)
859 endcol = getviscol2(oap->end.col, oap->end.coladd);
860 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
861 oap->start = curwin->w_cursor;
862 if (oap->line_count == 1)
863 {
864 coladvance(endcol);
865 oap->end.col = curwin->w_cursor.col;
866 oap->end.coladd = curwin->w_cursor.coladd;
867 curwin->w_cursor = oap->start;
868 }
869 }
870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (gchar_pos(&oap->end) == '\t'
873 && (int)oap->end.coladd < oap->inclusive)
874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (u_save((linenr_T)(oap->end.lnum - 1),
877 (linenr_T)(oap->end.lnum + 1)) == FAIL)
878 return FAIL;
879 curwin->w_cursor = oap->end;
880 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
881 oap->end = curwin->w_cursor;
882 curwin->w_cursor = oap->start;
883 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100884 if (has_mbyte)
885 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100888 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100890 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return FAIL;
892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100894 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 && oap->op_type == OP_CHANGE
896 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100897 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 display_dollar(oap->end.col - !oap->inclusive);
899
900 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (virtual_op)
903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100904 // fix up things for virtualedit-delete:
905 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 char_u *curline = ml_get_curline();
907 int len = (int)STRLEN(curline);
908
909 if (oap->end.coladd != 0
910 && (int)oap->end.col >= len - 1
911 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
912 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100913 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (n == 0 && oap->start.coladd != oap->end.coladd)
915 n = 1;
916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (gchar_cursor() != NUL)
919 curwin->w_cursor.coladd = 0;
920 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200921 (void)del_bytes((long)n, !virtual_op,
922 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
926 pos_T curpos;
927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100928 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
930 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
931 return FAIL;
932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100933 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 ++curwin->w_cursor.lnum;
937 del_lines((long)(oap->line_count - 2), FALSE);
938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100939 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200940 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200941 curwin->w_cursor.col = 0;
942 (void)del_bytes((long)n, !virtual_op,
943 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100944 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200945 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200947 if (oap->op_type == OP_DELETE)
948 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950
951 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
952
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000953setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200954 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100956 if (oap->block_mode)
957 {
958 curbuf->b_op_end.lnum = oap->end.lnum;
959 curbuf->b_op_end.col = oap->start.col;
960 }
961 else
962 curbuf->b_op_end = oap->start;
963 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 return OK;
967}
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969/*
970 * Adjust end of operating area for ending on a multi-byte character.
971 * Used for deletion.
972 */
973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100974mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 char_u *p;
977
978 if (oap->inclusive)
979 {
980 p = ml_get(oap->end.lnum);
981 oap->end.col += mb_tail_off(p, p + oap->end.col);
982 }
983}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar630afe82018-06-28 19:26:28 +0200985/*
986 * Replace the character under the cursor with "c".
987 * This takes care of multi-byte characters.
988 */
989 static void
990replace_character(int c)
991{
992 int n = State;
993
994 State = REPLACE;
995 ins_char(c);
996 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100997 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200998 dec_cursor();
999}
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/*
1002 * Replace a whole area with one character.
1003 */
1004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001005op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 char_u *newp, *oldp;
1010 size_t oldlen;
1011 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001012 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001013 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001016 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001018 if (c == REPLACE_CR_NCHAR)
1019 {
1020 had_ctrl_v_cr = TRUE;
1021 c = CAR;
1022 }
1023 else if (c == REPLACE_NL_NCHAR)
1024 {
1025 had_ctrl_v_cr = TRUE;
1026 c = NL;
1027 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (has_mbyte)
1030 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 if (u_save((linenr_T)(oap->start.lnum - 1),
1033 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1034 return FAIL;
1035
1036 /*
1037 * block mode replace
1038 */
1039 if (oap->block_mode)
1040 {
1041 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1042 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1043 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1046 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001049 // n == number of extra chars required
1050 // If we split a TAB, it may be replaced by several characters.
1051 // Thus the number of characters may increase!
1052 // If the range starts in virtual space, count the initial
1053 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1055 {
1056 pos_T vpos;
1057
Bram Moolenaara1381de2009-11-03 15:44:21 +00001058 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 getvpos(&vpos, oap->start_vcol);
1060 bd.startspaces += vpos.coladd;
1061 n = bd.startspaces;
1062 }
1063 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 numc = oap->end_vcol - oap->start_vcol + 1;
1073 if (bd.is_short && (!virtual_op || bd.is_MAX))
1074 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001076 // A double-wide character can be replaced only up to half the
1077 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if ((*mb_char2cells)(c) > 1)
1079 {
1080 if ((numc & 1) && !bd.is_short)
1081 {
1082 ++bd.endspaces;
1083 ++n;
1084 }
1085 numc = numc / 2;
1086 }
1087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001088 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 num_chars = numc;
1090 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001091 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 n += numc - bd.textlen;
1093
1094 oldp = ml_get_curline();
1095 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001096 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (newp == NULL)
1098 continue;
1099 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 mch_memmove(newp, oldp, (size_t)bd.textcol);
1102 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001103 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001104 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001105 // insert replacement chars CHECK FOR ALLOCATED SPACE
1106 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1107 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001108 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001110 if (has_mbyte)
1111 {
1112 n = (int)STRLEN(newp);
1113 while (--num_chars >= 0)
1114 n += (*mb_char2bytes)(c, newp + n);
1115 }
1116 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001117 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001118 if (!bd.is_short)
1119 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001120 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001121 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001122 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001123 STRMOVE(newp + STRLEN(newp), oldp);
1124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001128 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001129 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001130 if (after_p != NULL)
1131 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001133 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001135 if (after_p != NULL)
1136 {
1137 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1138 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1139 oap->end.lnum++;
1140 vim_free(after_p);
1141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 }
1144 else
1145 {
1146 /*
1147 * MCHAR and MLINE motion replace.
1148 */
1149 if (oap->motion_type == MLINE)
1150 {
1151 oap->start.col = 0;
1152 curwin->w_cursor.col = 0;
1153 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1154 if (oap->end.col)
1155 --oap->end.col;
1156 }
1157 else if (!oap->inclusive)
1158 dec(&(oap->end));
1159
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001160 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
1162 n = gchar_cursor();
1163 if (n != NUL)
1164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001167 // This is slow, but it handles replacing a single-byte
1168 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001169 if (curwin->w_cursor.lnum == oap->end.lnum)
1170 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001171 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (n == TAB)
1176 {
1177 int end_vcol = 0;
1178
1179 if (curwin->w_cursor.lnum == oap->end.lnum)
1180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001181 // oap->end has to be recalculated when
1182 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 end_vcol = getviscol2(oap->end.col,
1184 oap->end.coladd);
1185 }
1186 coladvance_force(getviscol());
1187 if (curwin->w_cursor.lnum == oap->end.lnum)
1188 getvpos(&oap->end, end_vcol);
1189 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001190 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1194 {
1195 int virtcols = oap->end.coladd;
1196
1197 if (curwin->w_cursor.lnum == oap->start.lnum
1198 && oap->start.col == oap->end.col && oap->start.coladd)
1199 virtcols -= oap->start.coladd;
1200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 // oap->end has been trimmed so it's effectively inclusive;
1202 // as a result an extra +1 must be counted so we don't
1203 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1205 curwin->w_cursor.col -= (virtcols + 1);
1206 for (; virtcols >= 0; virtcols--)
1207 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001208 if ((*mb_char2len)(c) > 1)
1209 replace_character(c);
1210 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001211 PBYTE(curwin->w_cursor, c);
1212 if (inc(&curwin->w_cursor) == -1)
1213 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (inc_cursor() == -1)
1219 break;
1220 }
1221 }
1222
1223 curwin->w_cursor = oap->start;
1224 check_cursor();
1225 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1226
Bram Moolenaare1004402020-10-24 20:49:43 +02001227 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001230 curbuf->b_op_start = oap->start;
1231 curbuf->b_op_end = oap->end;
1232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234 return OK;
1235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001237static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239/*
1240 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1241 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001242 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001243op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001247 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
1249 if (u_save((linenr_T)(oap->start.lnum - 1),
1250 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1251 return;
1252
1253 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
1256 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1257 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001258 int one_change;
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 block_prep(oap, &bd, pos.lnum, FALSE);
1261 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001262 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1263 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001264
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001265#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001266 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1269
Bram Moolenaar009b2592004-10-24 19:18:58 +00001270 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1271 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001273 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 if (did_change)
1278 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1279 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001280 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 {
1282 if (oap->motion_type == MLINE)
1283 {
1284 oap->start.col = 0;
1285 pos.col = 0;
1286 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1287 if (oap->end.col)
1288 --oap->end.col;
1289 }
1290 else if (!oap->inclusive)
1291 dec(&(oap->end));
1292
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001293 if (pos.lnum == oap->end.lnum)
1294 did_change = swapchars(oap->op_type, &pos,
1295 oap->end.col - pos.col + 1);
1296 else
1297 for (;;)
1298 {
1299 did_change |= swapchars(oap->op_type, &pos,
1300 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1301 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001302 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001303 break;
1304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (did_change)
1306 {
1307 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1308 0L);
1309#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001310 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
1312 char_u *ptr;
1313 int count;
1314
1315 pos = oap->start;
1316 while (pos.lnum < oap->end.lnum)
1317 {
1318 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001319 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001320 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001322 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 pos.col = 0;
1324 pos.lnum++;
1325 }
1326 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1327 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001328 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001330 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332#endif
1333 }
1334 }
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001337 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
Bram Moolenaare1004402020-10-24 20:49:43 +02001340 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001341 {
1342 // Set '[ and '] marks.
1343 curbuf->b_op_start = oap->start;
1344 curbuf->b_op_end = oap->end;
1345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001348 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001349 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350}
1351
1352/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001353 * Invoke swapchar() on "length" bytes at position "pos".
1354 * "pos" is advanced to just after the changed characters.
1355 * "length" is rounded up to include the whole last multi-byte character.
1356 * Also works correctly when the number of bytes changes.
1357 * Returns TRUE if some character was changed.
1358 */
1359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001361{
1362 int todo;
1363 int did_change = 0;
1364
1365 for (todo = length; todo > 0; --todo)
1366 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001367 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001368 {
1369 int len = (*mb_ptr2len)(ml_get_pos(pos));
1370
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001371 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001372 if (len > 0)
1373 todo -= len - 1;
1374 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001375 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001376 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001377 break;
1378 }
1379 return did_change;
1380}
1381
1382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 * If op_type == OP_UPPER: make uppercase,
1384 * if op_type == OP_LOWER: make lowercase,
1385 * if op_type == OP_ROT13: do rot13 encoding,
1386 * else swap case of character at 'pos'
1387 * returns TRUE when something actually changed.
1388 */
1389 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001390swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 int c;
1393 int nc;
1394
1395 c = gchar_pos(pos);
1396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001397 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (c >= 0x80 && op_type == OP_ROT13)
1399 return FALSE;
1400
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001401 if (op_type == OP_UPPER && c == 0xdf
1402 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001403 {
1404 pos_T sp = curwin->w_cursor;
1405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001407 curwin->w_cursor = *pos;
1408 del_char(FALSE);
1409 ins_char('S');
1410 ins_char('S');
1411 curwin->w_cursor = sp;
1412 inc(pos);
1413 }
1414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 nc = c;
1418 if (MB_ISLOWER(c))
1419 {
1420 if (op_type == OP_ROT13)
1421 nc = ROT13(c, 'a');
1422 else if (op_type != OP_LOWER)
1423 nc = MB_TOUPPER(c);
1424 }
1425 else if (MB_ISUPPER(c))
1426 {
1427 if (op_type == OP_ROT13)
1428 nc = ROT13(c, 'A');
1429 else if (op_type != OP_UPPER)
1430 nc = MB_TOLOWER(c);
1431 }
1432 if (nc != c)
1433 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1435 {
1436 pos_T sp = curwin->w_cursor;
1437
1438 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001439 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001440 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 ins_char(nc);
1442 curwin->w_cursor = sp;
1443 }
1444 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001445 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return TRUE;
1447 }
1448 return FALSE;
1449}
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
1452 * op_insert - Insert and append operators for Visual mode.
1453 */
1454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001455op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 long ins_len, pre_textlen = 0;
1458 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001459 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 struct block_def bd;
1461 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001462 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001463 pos_T start_insert;
1464 // offset when cursor was moved in insert mode
1465 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001470 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 curwin->w_cursor.lnum = oap->start.lnum;
1472 update_screen(INVERTED);
1473
1474 if (oap->block_mode)
1475 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 // When 'virtualedit' is used, need to insert the extra spaces before
1477 // doing block_prep(). When only "block" is used, virtual edit is
1478 // already disabled, but still need it when calling
1479 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001480 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001481 // state for the current window. To override that state, we need to
1482 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (curwin->w_cursor.coladd > 0)
1484 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001485 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (u_save_cursor() == FAIL)
1488 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001489
Gary Johnson51ad8502021-08-03 18:33:08 +02001490 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 coladvance_force(oap->op_type == OP_APPEND
1492 ? oap->end_vcol + 1 : getviscol());
1493 if (oap->op_type == OP_APPEND)
1494 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001495 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001497 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001499 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001500 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (oap->op_type == OP_APPEND)
1504 firstline += bd.textlen;
1505 pre_textlen = (long)STRLEN(firstline);
1506 }
1507
1508 if (oap->op_type == OP_APPEND)
1509 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001510 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001512 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 curwin->w_set_curswant = TRUE;
1514 while (*ml_get_cursor() != NUL
1515 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1516 ++curwin->w_cursor.col;
1517 if (bd.is_short && !bd.is_MAX)
1518 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001519 // First line was too short, make it longer and adjust the
1520 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (u_save_cursor() == FAIL)
1522 return;
1523 for (i = 0; i < bd.endspaces; ++i)
1524 ins_char(' ');
1525 bd.textlen += bd.endspaces;
1526 }
1527 }
1528 else
1529 {
1530 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001531 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001533 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001534 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 && oap->start_vcol != oap->end_vcol)
1536 inc_cursor();
1537 }
1538 }
1539
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001540 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001541 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001542 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001544 // When a tab was inserted, and the characters in front of the tab
1545 // have been converted to a tab as well, the column of the cursor
1546 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001547 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001548 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001549 oap->start = curbuf->b_op_start_orig;
1550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001551 // If user has moved off this line, we don't know what to do, so do
1552 // nothing.
1553 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001554 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return;
1556
1557 if (oap->block_mode)
1558 {
1559 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001560 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001561 size_t len;
1562 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001564 // If indent kicked in, the firstline might have changed
1565 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001566 ind_post = (colnr_T)getwhitecols_curline();
1567 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1568 {
1569 bd.textcol += ind_post - ind_pre;
1570 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001571 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001572 }
1573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001574 // The user may have moved the cursor before inserting something, try
1575 // to adjust the block for that. But only do it, if the difference
1576 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001577 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1578 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001579 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001580 int t = getviscol2(curbuf->b_op_start_orig.col,
1581 curbuf->b_op_start_orig.coladd);
1582
1583 if (!bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001584 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001585 if (oap->op_type == OP_INSERT
1586 && oap->start.col + oap->start.coladd
1587 != curbuf->b_op_start_orig.col
1588 + curbuf->b_op_start_orig.coladd)
1589 {
1590 oap->start.col = curbuf->b_op_start_orig.col;
1591 pre_textlen -= t - oap->start_vcol;
1592 oap->start_vcol = t;
1593 }
1594 else if (oap->op_type == OP_APPEND
1595 && oap->end.col + oap->end.coladd
1596 >= curbuf->b_op_start_orig.col
1597 + curbuf->b_op_start_orig.coladd)
1598 {
1599 oap->start.col = curbuf->b_op_start_orig.col;
1600 // reset pre_textlen to the value of OP_INSERT
1601 pre_textlen += bd.textlen;
1602 pre_textlen -= t - oap->start_vcol;
1603 oap->start_vcol = t;
1604 oap->op_type = OP_INSERT;
1605 }
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001606 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001607 else if (bd.is_MAX && oap->op_type == OP_APPEND)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001608 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001609 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001610 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001611 pre_textlen -= t - oap->start_vcol;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001612 }
1613 }
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 /*
1616 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001617 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 * Don't do this when "$" used, end-of-line will have changed.
1619 */
1620 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1621 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1622 {
1623 if (oap->op_type == OP_APPEND)
1624 {
1625 pre_textlen += bd2.textlen - bd.textlen;
1626 if (bd2.endspaces)
1627 --bd2.textlen;
1628 }
1629 bd.textcol = bd2.textcol;
1630 bd.textlen = bd2.textlen;
1631 }
1632
1633 /*
1634 * Subsequent calls to ml_get() flush the firstline data - take a
1635 * copy of the required string.
1636 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001637 firstline = ml_get(oap->start.lnum);
1638 len = STRLEN(firstline);
1639 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001641 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001642 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001643 // account for pressing cursor in insert mode when '$' was used
1644 if (bd.is_MAX
1645 && (start_insert.lnum == Insstart.lnum
1646 && start_insert.col > Insstart.col))
1647 {
1648 offset = (start_insert.col - Insstart.col);
1649 add -= offset;
1650 if (oap->end_vcol > offset)
1651 oap->end_vcol -= (offset + 1);
1652 else
1653 // moved outside of the visual block, what to do?
1654 return;
1655 }
1656 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001657 if ((size_t)add > len)
1658 firstline += len; // short line, point to the NUL
1659 else
1660 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001661 if (pre_textlen >= 0 && (ins_len =
1662 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001664 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (ins_text != NULL)
1666 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001667 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (u_save(oap->start.lnum,
1669 (linenr_T)(oap->end.lnum + 1)) == OK)
1670 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1671 &bd);
1672
1673 curwin->w_cursor.col = oap->start.col;
1674 check_cursor();
1675 vim_free(ins_text);
1676 }
1677 }
1678 }
1679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681/*
1682 * op_change - handle a change operation
1683 *
1684 * return TRUE if edit() returns because of a CTRL-O command
1685 */
1686 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001687op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 colnr_T l;
1690 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 long offset;
1692 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001693 long ins_len;
1694 long pre_textlen = 0;
1695 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 char_u *firstline;
1697 char_u *ins_text, *newp, *oldp;
1698 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700 l = oap->start.col;
1701 if (oap->motion_type == MLINE)
1702 {
1703 l = 0;
1704#ifdef FEAT_SMARTINDENT
1705 if (!p_paste && curbuf->b_p_si
1706# ifdef FEAT_CINDENT
1707 && !curbuf->b_p_cin
1708# endif
1709 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001710 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#endif
1712 }
1713
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001714 // First delete the text in the region. In an empty buffer only need to
1715 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1717 {
1718 if (u_save_cursor() == FAIL)
1719 return FALSE;
1720 }
1721 else if (op_delete(oap) == FAIL)
1722 return FALSE;
1723
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001724 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 && !virtual_op)
1726 inc_cursor();
1727
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001728 // check for still on same line (<CR> in inserted text meaningless)
1729 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (oap->block_mode)
1731 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001732 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (virtual_op && (curwin->w_cursor.coladd > 0
1734 || gchar_cursor() == NUL))
1735 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001736 firstline = ml_get(oap->start.lnum);
1737 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001738 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 bd.textcol = curwin->w_cursor.col;
1740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1743 if (oap->motion_type == MLINE)
1744 fix_indent();
1745#endif
1746
1747 retval = edit(NUL, FALSE, (linenr_T)1);
1748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001750 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001752 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001754 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001756 // Auto-indenting may have changed the indent. If the cursor was past
1757 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001759 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001761 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001762
1763 pre_textlen += new_indent - pre_indent;
1764 bd.textcol += new_indent - pre_indent;
1765 }
1766
1767 ins_len = (long)STRLEN(firstline) - pre_textlen;
1768 if (ins_len > 0)
1769 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001770 // Subsequent calls to ml_get() flush the firstline data - take a
1771 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001772 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001774 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1776 linenr++)
1777 {
1778 block_prep(oap, &bd, linenr, TRUE);
1779 if (!bd.is_short || virtual_op)
1780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 pos_T vpos;
1782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001783 // If the block starts in virtual space, count the
1784 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (bd.is_short)
1786 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001787 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 }
1790 else
1791 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001793 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (newp == NULL)
1795 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001796 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 mch_memmove(newp, oldp, (size_t)bd.textcol);
1798 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001799 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1802 offset += ins_len;
1803 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001804 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 ml_replace(linenr, newp, FALSE);
1806 }
1807 }
1808 check_cursor();
1809
1810 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1811 }
1812 vim_free(ins_text);
1813 }
1814 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001815 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 return retval;
1818}
1819
1820/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001821 * When the cursor is on the NUL past the end of the line and it should not be
1822 * there move it left.
1823 */
1824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001825adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001826{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001827 unsigned int cur_ve_flags = get_ve_flags();
1828
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001829 if (curwin->w_cursor.col > 0
1830 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001831 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 && !(restart_edit || (State & INSERT)))
1833 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001834 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001835 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001836
Gary Johnson53ba05b2021-07-26 22:19:10 +02001837 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001838 {
1839 colnr_T scol, ecol;
1840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001841 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001842 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1843 curwin->w_cursor.coladd = ecol - scol + 1;
1844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846}
1847
Bram Moolenaar81340392012-06-06 16:12:59 +02001848/*
1849 * If "process" is TRUE and the line begins with a comment leader (possibly
1850 * after some white space), return a pointer to the text after it. Put a boolean
1851 * value indicating whether the line ends with an unclosed comment in
1852 * "is_comment".
1853 * line - line to be processed,
1854 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001855 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001856 * include_space - whether to also skip space following the comment leader,
1857 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001858 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001859 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001860 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001861skip_comment(
1862 char_u *line,
1863 int process,
1864 int include_space,
1865 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001866{
1867 char_u *comment_flags = NULL;
1868 int lead_len;
1869 int leader_offset = get_last_leader_offset(line, &comment_flags);
1870
1871 *is_comment = FALSE;
1872 if (leader_offset != -1)
1873 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001874 // Let's check whether the line ends with an unclosed comment.
1875 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001876 while (*comment_flags)
1877 {
1878 if (*comment_flags == COM_END
1879 || *comment_flags == ':')
1880 break;
1881 ++comment_flags;
1882 }
1883 if (*comment_flags != COM_END)
1884 *is_comment = TRUE;
1885 }
1886
1887 if (process == FALSE)
1888 return line;
1889
1890 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1891
1892 if (lead_len == 0)
1893 return line;
1894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001895 // Find:
1896 // - COM_END,
1897 // - colon,
1898 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001899 while (*comment_flags)
1900 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001901 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001902 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001903 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001904 ++comment_flags;
1905 }
1906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001907 // If we found a colon, it means that we are not processing a line
1908 // starting with a closing part of a three-part comment. That's good,
1909 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001910 if (*comment_flags == ':' || *comment_flags == NUL)
1911 line += lead_len;
1912
1913 return line;
1914}
Bram Moolenaar81340392012-06-06 16:12:59 +02001915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001917 * Join 'count' lines (minimal 2) at cursor position.
1918 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001919 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1920 * leaders should not be removed.
1921 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1922 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001924 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001927do_join(
1928 long count,
1929 int insert_space,
1930 int save_undo,
1931 int use_formatoptions UNUSED,
1932 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001934 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001935 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001936 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001938 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001939 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001940 int endcurr1 = NUL;
1941 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001942 int currsize = 0; // size of the current line
1943 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001945 colnr_T col = 0;
1946 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001947 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001948 int remove_comments = (use_formatoptions == TRUE)
1949 && has_format_option(FO_REMOVE_COMS);
1950 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001951#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001952 int propcount = 0; // number of props over all joined lines
1953 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001956 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1957 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001960 // Allocate an array to store the number of spaces inserted before each
1961 // line. We will use it to pre-compute the length of the new line and the
1962 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001963 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001964 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001966 if (remove_comments)
1967 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001968 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001969 if (comments == NULL)
1970 {
1971 vim_free(spaces);
1972 return FAIL;
1973 }
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001977 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001978 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001979 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001981 for (t = 0; t < count; ++t)
1982 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001983 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001984#ifdef FEAT_PROP_POPUP
1985 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1986#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001987 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001989 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001990 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1991 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1992 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001993 if (remove_comments)
1994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001995 // We don't want to remove the comment leader if the
1996 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001997 if (t > 0 && prev_was_comment)
1998 {
1999
2000 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2001 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002002 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002003 curr = new_curr;
2004 }
2005 else
2006 curr = skip_comment(curr, FALSE, insert_space,
2007 &prev_was_comment);
2008 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002009
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002010 if (insert_space && t > 0)
2011 {
2012 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002013 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002014 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002015 && (!has_format_option(FO_MBYTE_JOIN)
2016 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2017 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002018 || (mb_ptr2char(curr) < 0x100
2019 && !(enc_utf8 && utf_eat_space(endcurr1)))
2020 || (endcurr1 < 0x100
2021 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002022 )
2023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002024 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002025 if (endcurr1 == ' ')
2026 endcurr1 = endcurr2;
2027 else
2028 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002030 if ( p_js
2031 && (endcurr1 == '.'
2032 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2033 && (endcurr1 == '?' || endcurr1 == '!'))))
2034 ++spaces[t];
2035 }
2036 }
2037 currsize = (int)STRLEN(curr);
2038 sumsize += currsize + spaces[t];
2039 endcurr1 = endcurr2 = NUL;
2040 if (insert_space && currsize > 0)
2041 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002042 if (has_mbyte)
2043 {
2044 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002045 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002046 endcurr1 = (*mb_ptr2char)(cend);
2047 if (cend > curr)
2048 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002049 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002050 endcurr2 = (*mb_ptr2char)(cend);
2051 }
2052 }
2053 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002054 {
2055 endcurr1 = *(curr + currsize - 1);
2056 if (currsize > 1)
2057 endcurr2 = *(curr + currsize - 2);
2058 }
2059 }
2060 line_breakcheck();
2061 if (got_int)
2062 {
2063 ret = FAIL;
2064 goto theend;
2065 }
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002068 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002069 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002071 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002072 newp_len = sumsize + 1;
2073#ifdef FEAT_PROP_POPUP
2074 newp_len += propcount * sizeof(textprop_T);
2075#endif
2076 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002077 if (newp == NULL)
2078 {
2079 ret = FAIL;
2080 goto theend;
2081 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002082 cend = newp + sumsize;
2083 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002085 /*
2086 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002087 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002088 *
2089 * Move marks from each deleted line to the joined line, adjusting the
2090 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2091 * should not really be a problem.
2092 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002093#ifdef FEAT_PROP_POPUP
2094 props_remaining = propcount;
2095#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002096 for (t = count - 1; ; --t)
2097 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002098 int spaces_removed;
2099
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002100 cend -= currsize;
2101 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002102
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002103 if (spaces[t] > 0)
2104 {
2105 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002106 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002107 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002108
2109 // If deleting more spaces than adding, the cursor moves no more than
2110 // what is added if it is inside these spaces.
2111 spaces_removed = (curr - curr_start) - spaces[t];
2112
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002113 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002114 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002115#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002116 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2117 curwin->w_cursor.lnum + t, t == count - 1,
2118 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002119#endif
2120
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002121 if (t == 0)
2122 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002123 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002124 if (remove_comments)
2125 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002126 if (insert_space && t > 1)
2127 curr = skipwhite(curr);
2128 currsize = (int)STRLEN(curr);
2129 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002130
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002131 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
Bram Moolenaare1004402020-10-24 20:49:43 +02002133 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002134 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002136 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002137 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002138 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002140 // Only report the change in the first line here, del_lines() will report
2141 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 changed_lines(curwin->w_cursor.lnum, currsize,
2143 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002145 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 * briefly, and then move it back. After del_lines() the cursor may
2147 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 */
2149 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002151 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 curwin->w_cursor.lnum = t;
2153
2154 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002155 * Set the cursor column:
2156 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002157 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002159 curwin->w_cursor.col =
2160 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 curwin->w_set_curswant = TRUE;
2165
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002166theend:
2167 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002168 if (remove_comments)
2169 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171}
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 * prepare a few things for block mode yank/delete/tilde
2175 *
2176 * for delete:
2177 * - textlen includes the first/last char to be (partly) deleted
2178 * - start/endspaces is the number of columns that are taken by the
2179 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002180 * deleted.
2181 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 * - textlen includes the first/last char to be wholly yanked
2183 * - start/endspaces is the number of columns of the first/last yanked char
2184 * that are to be yanked.
2185 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002186 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002187block_prep(
2188 oparg_T *oap,
2189 struct block_def *bdp,
2190 linenr_T lnum,
2191 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 int incr = 0;
2194 char_u *pend;
2195 char_u *pstart;
2196 char_u *line;
2197 char_u *prev_pstart;
2198 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002199#ifdef FEAT_LINEBREAK
2200 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002202 // Avoid a problem with unwanted linebreaks in block mode.
2203 curwin->w_p_lbr = FALSE;
2204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 bdp->startspaces = 0;
2206 bdp->endspaces = 0;
2207 bdp->textlen = 0;
2208 bdp->start_vcol = 0;
2209 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 bdp->is_short = FALSE;
2211 bdp->is_oneChar = FALSE;
2212 bdp->pre_whitesp = 0;
2213 bdp->pre_whitesp_c = 0;
2214 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 bdp->start_char_vcols = 0;
2216
2217 line = ml_get(lnum);
2218 pstart = line;
2219 prev_pstart = line;
2220 while (bdp->start_vcol < oap->start_vcol && *pstart)
2221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002223 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002225 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 bdp->pre_whitesp += incr;
2228 bdp->pre_whitesp_c++;
2229 }
2230 else
2231 {
2232 bdp->pre_whitesp = 0;
2233 bdp->pre_whitesp_c = 0;
2234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002236 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
2241 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (!is_del || oap->op_type == OP_APPEND)
2244 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2245 }
2246 else
2247 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002248 // notice: this converts partly selected Multibyte characters to
2249 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2251 if (is_del && bdp->startspaces)
2252 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2253 pend = pstart;
2254 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (oap->op_type == OP_INSERT)
2259 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2260 else if (oap->op_type == OP_APPEND)
2261 {
2262 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2263 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2264 }
2265 else
2266 {
2267 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2268 if (is_del && oap->op_type != OP_LSHIFT)
2269 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002270 // just putting the sum of those two into
2271 // bdp->startspaces doesn't work for Visual replace,
2272 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 bdp->startspaces = bdp->start_char_vcols
2274 - (bdp->start_vcol - oap->start_vcol);
2275 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2276 }
2277 }
2278 }
2279 else
2280 {
2281 prev_pend = pend;
2282 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2283 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002284 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002286 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 bdp->end_vcol += incr;
2288 }
2289 if (bdp->end_vcol <= oap->end_vcol
2290 && (!is_del
2291 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002292 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002295 // Alternative: include spaces to fill up the block.
2296 // Disadvantage: can lead to trailing spaces when the line is
2297 // short where the text is put
2298 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (oap->op_type == OP_APPEND || virtual_op)
2300 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002301 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 else if (bdp->end_vcol > oap->end_vcol)
2306 {
2307 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2308 if (!is_del && bdp->endspaces)
2309 {
2310 bdp->endspaces = incr - bdp->endspaces;
2311 if (pend != pstart)
2312 pend = prev_pend;
2313 }
2314 }
2315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (is_del && bdp->startspaces)
2318 pstart = prev_pstart;
2319 bdp->textlen = (int)(pend - pstart);
2320 }
2321 bdp->textcol = (colnr_T) (pstart - line);
2322 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002323#ifdef FEAT_LINEBREAK
2324 curwin->w_p_lbr = lbr_saved;
2325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002329 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002331 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002332op_addsub(
2333 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002334 linenr_T Prenum1, // Amount of add/subtract
2335 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002337 pos_T pos;
2338 struct block_def bd;
2339 int change_cnt = 0;
2340 linenr_T amount = Prenum1;
2341
Bram Moolenaar6b731882018-11-16 20:54:47 +01002342 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002343 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002344 // the call to changed_lines().
2345#ifdef FEAT_FOLDING
2346 disable_fold_update++;
2347#endif
2348
Bram Moolenaard79e5502016-01-10 22:13:02 +01002349 if (!VIsual_active)
2350 {
2351 pos = curwin->w_cursor;
2352 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002353 {
2354#ifdef FEAT_FOLDING
2355 disable_fold_update--;
2356#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002357 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002358 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002359 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002360#ifdef FEAT_FOLDING
2361 disable_fold_update--;
2362#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002363 if (change_cnt)
2364 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2365 }
2366 else
2367 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002368 int one_change;
2369 int length;
2370 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002371
2372 if (u_save((linenr_T)(oap->start.lnum - 1),
2373 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002374 {
2375#ifdef FEAT_FOLDING
2376 disable_fold_update--;
2377#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002378 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002379 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002380
2381 pos = oap->start;
2382 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2383 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002384 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002385 {
2386 block_prep(oap, &bd, pos.lnum, FALSE);
2387 pos.col = bd.textcol;
2388 length = bd.textlen;
2389 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002390 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002391 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002392 curwin->w_cursor.col = 0;
2393 pos.col = 0;
2394 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2395 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002396 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002397 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002398 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002399 dec(&(oap->end));
2400 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2401 pos.col = 0;
2402 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002403 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002404 pos.col += oap->start.col;
2405 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002406 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002407 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002408 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002409 length = (int)STRLEN(ml_get(oap->end.lnum));
2410 if (oap->end.col >= length)
2411 oap->end.col = length - 1;
2412 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002413 }
2414 }
2415 one_change = do_addsub(oap->op_type, &pos, length, amount);
2416 if (one_change)
2417 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002418 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002419 if (change_cnt == 0)
2420 startpos = curbuf->b_op_start;
2421 ++change_cnt;
2422 }
2423
2424#ifdef FEAT_NETBEANS_INTG
2425 if (netbeans_active() && one_change)
2426 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002427 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002428
2429 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002430 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002431 netbeans_inserted(curbuf, pos.lnum, pos.col,
2432 &ptr[pos.col], length);
2433 }
2434#endif
2435 if (g_cmd && one_change)
2436 amount += Prenum1;
2437 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002438
2439#ifdef FEAT_FOLDING
2440 disable_fold_update--;
2441#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002442 if (change_cnt)
2443 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2444
2445 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002446 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002447 redraw_curbuf_later(INVERTED);
2448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002449 // Set '[ mark if something changed. Keep the last end
2450 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002451 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002452 curbuf->b_op_start = startpos;
2453
2454 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002455 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002456 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002457 }
2458}
2459
2460/*
2461 * Add or subtract 'Prenum1' from a number in a line
2462 * op_type is OP_NR_ADD or OP_NR_SUB
2463 *
2464 * Returns TRUE if some character was changed.
2465 */
2466 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002467do_addsub(
2468 int op_type,
2469 pos_T *pos,
2470 int length,
2471 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002472{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 int col;
2474 char_u *buf1;
2475 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002476 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2477 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002478 uvarnumber_T n;
2479 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 char_u *ptr;
2481 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002483 int do_hex;
2484 int do_oct;
2485 int do_bin;
2486 int do_alpha;
2487 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002490 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002491 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002492 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002493 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002494 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002495 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002496 pos_T startpos;
2497 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002498 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002500 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2501 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2502 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2503 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2504 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002506 if (virtual_active())
2507 {
2508 save_coladd = pos->coladd;
2509 pos->coladd = 0;
2510 }
2511
Bram Moolenaard79e5502016-01-10 22:13:02 +01002512 curwin->w_cursor = *pos;
2513 ptr = ml_get(pos->lnum);
2514 col = pos->col;
2515
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002516 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002517 goto theend;
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /*
2520 * First check if we are on a hexadecimal number, after the "0x".
2521 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002522 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002524 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002525 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002526 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002527 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002528 if (has_mbyte)
2529 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002530 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002531
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002532 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002533 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002534 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002535 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002536 if (has_mbyte)
2537 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002538 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002539
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002540 if ( do_bin
2541 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002542 && ! ((col > 0
2543 && (ptr[col] == 'X'
2544 || ptr[col] == 'x')
2545 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002546 && (!has_mbyte ||
2547 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002548 && vim_isxdigit(ptr[col + 1]))))
2549 {
2550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002551 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002552
Bram Moolenaard79e5502016-01-10 22:13:02 +01002553 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002554
2555 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002556 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002557 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002558 if (has_mbyte)
2559 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002560 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002561 }
2562
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002563 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002564 && col > 0
2565 && (ptr[col] == 'X'
2566 || ptr[col] == 'x')
2567 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002568 && (!has_mbyte ||
2569 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002570 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002571 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002572 && col > 0
2573 && (ptr[col] == 'B'
2574 || ptr[col] == 'b')
2575 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002576 && (!has_mbyte ||
2577 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002578 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002579 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002580 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002581 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002582 if (has_mbyte)
2583 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002584 }
2585 else
2586 {
2587 /*
2588 * Search forward and then backward to find the start of number.
2589 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002590 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002591
2592 while (ptr[col] != NUL
2593 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002594 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002595 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002596
2597 while (col > 0
2598 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002599 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002600 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002601 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002602 if (has_mbyte)
2603 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002604 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002605 }
2606 }
2607
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002608 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002609 {
2610 while (ptr[col] != NUL && length > 0
2611 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002612 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002613 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002614 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002615
2616 col += mb_len;
2617 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002618 }
2619
2620 if (length == 0)
2621 goto theend;
2622
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002623 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002624 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2625 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002626 {
2627 negative = TRUE;
2628 was_positive = FALSE;
2629 }
2630 }
2631
2632 /*
2633 * If a number was found, and saving for undo works, replace the number.
2634 */
2635 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002636 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002637 {
2638 beep_flush();
2639 goto theend;
2640 }
2641
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002642 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002643 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002644 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002645 if (op_type == OP_NR_SUB)
2646 {
2647 if (CharOrd(firstdigit) < Prenum1)
2648 {
2649 if (isupper(firstdigit))
2650 firstdigit = 'A';
2651 else
2652 firstdigit = 'a';
2653 }
2654 else
2655#ifdef EBCDIC
2656 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2657#else
2658 firstdigit -= Prenum1;
2659#endif
2660 }
2661 else
2662 {
2663 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2664 {
2665 if (isupper(firstdigit))
2666 firstdigit = 'Z';
2667 else
2668 firstdigit = 'z';
2669 }
2670 else
2671#ifdef EBCDIC
2672 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2673#else
2674 firstdigit += Prenum1;
2675#endif
2676 }
2677 curwin->w_cursor.col = col;
2678 if (!did_change)
2679 startpos = curwin->w_cursor;
2680 did_change = TRUE;
2681 (void)del_char(FALSE);
2682 ins_char(firstdigit);
2683 endpos = curwin->w_cursor;
2684 curwin->w_cursor.col = col;
2685 }
2686 else
2687 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002688 pos_T save_pos;
2689 int i;
2690
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002691 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002692 && (!has_mbyte ||
2693 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002694 && !visual
2695 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002698 --col;
2699 negative = TRUE;
2700 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002702 if (visual && VIsual_mode != 'V')
2703 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2704 ? (int)STRLEN(ptr) - col
2705 : length);
2706
2707 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002708 0 + (do_bin ? STR2NR_BIN : 0)
2709 + (do_oct ? STR2NR_OCT : 0)
2710 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002711 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002714 if (pre && negative)
2715 {
2716 ++col;
2717 --length;
2718 negative = FALSE;
2719 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002721 subtract = FALSE;
2722 if (op_type == OP_NR_SUB)
2723 subtract ^= TRUE;
2724 if (negative)
2725 subtract ^= TRUE;
2726
2727 oldn = n;
2728 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002729 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002730 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002731 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002733 if (!pre)
2734 {
2735 if (subtract)
2736 {
2737 if (n > oldn)
2738 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002739 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002740 negative ^= TRUE;
2741 }
2742 }
2743 else
2744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002746 if (n < oldn)
2747 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002748 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002749 negative ^= TRUE;
2750 }
2751 }
2752 if (n == 0)
2753 negative = FALSE;
2754 }
2755
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002756 if (do_unsigned && negative)
2757 {
2758 if (subtract)
2759 // sticking at zero.
2760 n = (uvarnumber_T)0;
2761 else
2762 // sticking at 2^64 - 1.
2763 n = (uvarnumber_T)(-1);
2764 negative = FALSE;
2765 }
2766
Bram Moolenaard79e5502016-01-10 22:13:02 +01002767 if (visual && !was_positive && !negative && col > 0)
2768 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002770 col--;
2771 length++;
2772 }
2773
2774 /*
2775 * Delete the old number.
2776 */
2777 curwin->w_cursor.col = col;
2778 if (!did_change)
2779 startpos = curwin->w_cursor;
2780 did_change = TRUE;
2781 todel = length;
2782 c = gchar_cursor();
2783 /*
2784 * Don't include the '-' in the length, only the length of the
2785 * part after it is kept the same.
2786 */
2787 if (c == '-')
2788 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002789
2790 save_pos = curwin->w_cursor;
2791 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002792 {
2793 if (c < 0x100 && isalpha(c))
2794 {
2795 if (isupper(c))
2796 hexupper = TRUE;
2797 else
2798 hexupper = FALSE;
2799 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002800 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002801 c = gchar_cursor();
2802 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002803 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002804
2805 /*
2806 * Prepare the leading characters in buf1[].
2807 * When there are many leading zeros it could be very long.
2808 * Allocate a bit too much.
2809 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002810 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002811 if (buf1 == NULL)
2812 goto theend;
2813 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002814 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002815 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002816 if (pre)
2817 {
2818 *ptr++ = '0';
2819 --length;
2820 }
2821 if (pre == 'b' || pre == 'B' ||
2822 pre == 'x' || pre == 'X')
2823 {
2824 *ptr++ = pre;
2825 --length;
2826 }
2827
2828 /*
2829 * Put the number characters in buf2[].
2830 */
2831 if (pre == 'b' || pre == 'B')
2832 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002833 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002834 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002835
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002836 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002837 for (bit = bits; bit > 0; bit--)
2838 if ((n >> (bit - 1)) & 0x1) break;
2839
2840 for (i = 0; bit > 0; bit--)
2841 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2842
2843 buf2[i] = '\0';
2844 }
2845 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002846 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002847 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002848 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002849 else if (pre && hexupper)
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 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002852 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002853 length -= (int)STRLEN(buf2);
2854
2855 /*
2856 * Adjust number of zeros to the new number of digits, so the
2857 * total length of the number remains the same.
2858 * Don't do this when
2859 * the result may look like an octal number.
2860 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002861 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002862 while (length-- > 0)
2863 *ptr++ = '0';
2864 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002865
Bram Moolenaard79e5502016-01-10 22:13:02 +01002866 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002867
2868 // Insert just after the first character to be removed, so that any
2869 // text properties will be adjusted. Then delete the old number
2870 // afterwards.
2871 save_pos = curwin->w_cursor;
2872 if (todel > 0)
2873 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002875 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002876
2877 // del_char() will also mark line needing displaying
2878 if (todel > 0)
2879 {
2880 int bytes_after = (int)STRLEN(ml_get_curline())
2881 - curwin->w_cursor.col;
2882
2883 // Delete the one character before the insert.
2884 curwin->w_cursor = save_pos;
2885 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002886 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2887 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002888 --todel;
2889 }
2890 while (todel-- > 0)
2891 (void)del_char(FALSE);
2892
Bram Moolenaard79e5502016-01-10 22:13:02 +01002893 endpos = curwin->w_cursor;
2894 if (did_change && curwin->w_cursor.col)
2895 --curwin->w_cursor.col;
2896 }
2897
Bram Moolenaare1004402020-10-24 20:49:43 +02002898 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002899 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002901 curbuf->b_op_start = startpos;
2902 curbuf->b_op_end = endpos;
2903 if (curbuf->b_op_end.col > 0)
2904 --curbuf->b_op_end.col;
2905 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002906
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002907theend:
2908 if (visual)
2909 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002910 else if (did_change)
2911 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002912 else if (virtual_active())
2913 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002914
Bram Moolenaard79e5502016-01-10 22:13:02 +01002915 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916}
2917
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002919clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002921 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922}
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002925 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 *
2927 * "Words" are counted by looking for boundaries between non-space and
2928 * space characters. (it seems to produce results that match 'wc'.)
2929 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002930 * Return value is byte count; word count for the line is added to "*wc".
2931 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 *
2933 * The function will only examine the first "limit" characters in the
2934 * line, stopping if it encounters an end-of-line (NUL byte). In that
2935 * case, eol_size will be added to the character count to account for
2936 * the size of the EOL character.
2937 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002938 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002939line_count_info(
2940 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002941 varnumber_T *wc,
2942 varnumber_T *cc,
2943 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002944 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002946 varnumber_T i;
2947 varnumber_T words = 0;
2948 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 int is_word = 0;
2950
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002951 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
2953 if (is_word)
2954 {
2955 if (vim_isspace(line[i]))
2956 {
2957 words++;
2958 is_word = 0;
2959 }
2960 }
2961 else if (!vim_isspace(line[i]))
2962 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002963 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002964 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966
2967 if (is_word)
2968 words++;
2969 *wc += words;
2970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002972 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002975 chars += eol_size;
2976 }
2977 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 return i;
2979}
2980
2981/*
2982 * Give some info about the position of the cursor (for "g CTRL-G").
2983 * In Visual mode, give some info about the selected region. (In this case,
2984 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002985 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 */
2987 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002988cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002991 char_u buf1[50];
2992 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002994 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002995 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002996 varnumber_T byte_count_cursor = 0;
2997 varnumber_T char_count = 0;
2998 varnumber_T char_count_cursor = 0;
2999 varnumber_T word_count = 0;
3000 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003001 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003002 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 long line_count_selected = 0;
3004 pos_T min_pos, max_pos;
3005 oparg_T oparg;
3006 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 /*
3009 * Compute the length of the file in characters.
3010 */
3011 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3012 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003013 if (dict == NULL)
3014 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003015 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003016 return;
3017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019 else
3020 {
3021 if (get_fileformat(curbuf) == EOL_DOS)
3022 eol_size = 2;
3023 else
3024 eol_size = 1;
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (VIsual_active)
3027 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003028 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 min_pos = VIsual;
3031 max_pos = curwin->w_cursor;
3032 }
3033 else
3034 {
3035 min_pos = curwin->w_cursor;
3036 max_pos = VIsual;
3037 }
3038 if (*p_sel == 'e' && max_pos.col > 0)
3039 --max_pos.col;
3040
3041 if (VIsual_mode == Ctrl_V)
3042 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003043#ifdef FEAT_LINEBREAK
3044 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003045 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003048 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003049 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 oparg.is_VIsual = 1;
3052 oparg.block_mode = TRUE;
3053 oparg.op_type = OP_NOP;
3054 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003055 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003056#ifdef FEAT_LINEBREAK
3057 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003058 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003059#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003060 if (curwin->w_curswant == MAXCOL)
3061 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003062 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 if (oparg.end_vcol < oparg.start_vcol)
3064 {
3065 oparg.end_vcol += oparg.start_vcol;
3066 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3067 oparg.end_vcol -= oparg.start_vcol;
3068 }
3069 }
3070 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
3073 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3074 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003075 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003076 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 ui_breakcheck();
3079 if (got_int)
3080 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003081 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (VIsual_active
3086 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3087 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003088 char_u *s = NULL;
3089 long len = 0L;
3090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 switch (VIsual_mode)
3092 {
3093 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003097 s = bd.textstart;
3098 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 break;
3100 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003101 s = ml_get(lnum);
3102 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 break;
3104 case 'v':
3105 {
3106 colnr_T start_col = (lnum == min_pos.lnum)
3107 ? min_pos.col : 0;
3108 colnr_T end_col = (lnum == max_pos.lnum)
3109 ? max_pos.col - start_col + 1 : MAXCOL;
3110
Bram Moolenaardef9e822004-12-31 20:58:58 +00003111 s = ml_get(lnum) + start_col;
3112 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
3114 break;
3115 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003116 if (s != NULL)
3117 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003118 byte_count_cursor += line_count_info(s, &word_count_cursor,
3119 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003120 if (lnum == curbuf->b_ml.ml_line_count
3121 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003122 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003123 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003124 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
3127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (lnum == curwin->w_cursor.lnum)
3131 {
3132 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003133 char_count_cursor += char_count;
3134 byte_count_cursor = byte_count +
3135 line_count_info(ml_get(lnum),
3136 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003137 (varnumber_T)(curwin->w_cursor.col + 1),
3138 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
3140 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003141 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003142 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003143 &char_count, (varnumber_T)MAXCOL,
3144 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
3146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003147 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003148 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003149 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
Bram Moolenaared767a22016-01-03 22:49:16 +01003151 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003153 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003155 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3156 {
3157 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3158 &max_pos.col);
3159 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3160 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3161 }
3162 else
3163 buf1[0] = NUL;
3164
3165 if (char_count_cursor == byte_count_cursor
3166 && char_count == byte_count)
3167 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003168 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003169 buf1, line_count_selected,
3170 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003171 (varnumber_T)word_count_cursor,
3172 (varnumber_T)word_count,
3173 (varnumber_T)byte_count_cursor,
3174 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003175 else
3176 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003177 _("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 +01003178 buf1, line_count_selected,
3179 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003180 (varnumber_T)word_count_cursor,
3181 (varnumber_T)word_count,
3182 (varnumber_T)char_count_cursor,
3183 (varnumber_T)char_count,
3184 (varnumber_T)byte_count_cursor,
3185 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003188 {
3189 p = ml_get_curline();
3190 validate_virtcol();
3191 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3192 (int)curwin->w_virtcol + 1);
3193 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3194 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
Bram Moolenaared767a22016-01-03 22:49:16 +01003196 if (char_count_cursor == byte_count_cursor
3197 && char_count == byte_count)
3198 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003199 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003200 (char *)buf1, (char *)buf2,
3201 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003203 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3204 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003205 else
3206 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003207 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003208 (char *)buf1, (char *)buf2,
3209 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003210 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003211 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3212 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3213 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003214 }
3215 }
3216
Bram Moolenaared767a22016-01-03 22:49:16 +01003217 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003218 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003219 {
3220 size_t len = STRLEN(IObuff);
3221
3222 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003223 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003224 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003225 if (dict == NULL)
3226 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003227 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003228 p = p_shm;
3229 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003230 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003231 p_shm = p;
3232 }
3233 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003234#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003235 if (dict != NULL)
3236 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003237 dict_add_number(dict, "words", word_count);
3238 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003239 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003240 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3241 byte_count_cursor);
3242 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3243 char_count_cursor);
3244 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3245 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003249
3250/*
3251 * Handle indent and format operators and visual mode ":".
3252 */
3253 static void
3254op_colon(oparg_T *oap)
3255{
3256 stuffcharReadbuff(':');
3257 if (oap->is_VIsual)
3258 stuffReadbuff((char_u *)"'<,'>");
3259 else
3260 {
3261 // Make the range look nice, so it can be repeated.
3262 if (oap->start.lnum == curwin->w_cursor.lnum)
3263 stuffcharReadbuff('.');
3264 else
3265 stuffnumReadbuff((long)oap->start.lnum);
3266 if (oap->end.lnum != oap->start.lnum)
3267 {
3268 stuffcharReadbuff(',');
3269 if (oap->end.lnum == curwin->w_cursor.lnum)
3270 stuffcharReadbuff('.');
3271 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3272 stuffcharReadbuff('$');
3273 else if (oap->start.lnum == curwin->w_cursor.lnum)
3274 {
3275 stuffReadbuff((char_u *)".+");
3276 stuffnumReadbuff((long)oap->line_count - 1);
3277 }
3278 else
3279 stuffnumReadbuff((long)oap->end.lnum);
3280 }
3281 }
3282 if (oap->op_type != OP_COLON)
3283 stuffReadbuff((char_u *)"!");
3284 if (oap->op_type == OP_INDENT)
3285 {
3286#ifndef FEAT_CINDENT
3287 if (*get_equalprg() == NUL)
3288 stuffReadbuff((char_u *)"indent");
3289 else
3290#endif
3291 stuffReadbuff(get_equalprg());
3292 stuffReadbuff((char_u *)"\n");
3293 }
3294 else if (oap->op_type == OP_FORMAT)
3295 {
3296 if (*curbuf->b_p_fp != NUL)
3297 stuffReadbuff(curbuf->b_p_fp);
3298 else if (*p_fp != NUL)
3299 stuffReadbuff(p_fp);
3300 else
3301 stuffReadbuff((char_u *)"fmt");
3302 stuffReadbuff((char_u *)"\n']");
3303 }
3304
3305 // do_cmdline() does the rest
3306}
3307
3308/*
3309 * Handle the "g@" operator: call 'operatorfunc'.
3310 */
3311 static void
3312op_function(oparg_T *oap UNUSED)
3313{
3314#ifdef FEAT_EVAL
3315 typval_T argv[2];
3316 int save_virtual_op = virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003317 int save_finish_op = finish_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003318 pos_T orig_start = curbuf->b_op_start;
3319 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003320
3321 if (*p_opfunc == NUL)
3322 emsg(_("E774: 'operatorfunc' is empty"));
3323 else
3324 {
3325 // Set '[ and '] marks to text to be operated on.
3326 curbuf->b_op_start = oap->start;
3327 curbuf->b_op_end = oap->end;
3328 if (oap->motion_type != MLINE && !oap->inclusive)
3329 // Exclude the end position.
3330 decl(&curbuf->b_op_end);
3331
3332 argv[0].v_type = VAR_STRING;
3333 if (oap->block_mode)
3334 argv[0].vval.v_string = (char_u *)"block";
3335 else if (oap->motion_type == MLINE)
3336 argv[0].vval.v_string = (char_u *)"line";
3337 else
3338 argv[0].vval.v_string = (char_u *)"char";
3339 argv[1].v_type = VAR_UNKNOWN;
3340
3341 // Reset virtual_op so that 'virtualedit' can be changed in the
3342 // function.
3343 virtual_op = MAYBE;
3344
naohiro ono75c30e92021-10-19 11:15:41 +01003345 // Reset finish_op so that mode() returns the right value.
3346 finish_op = FALSE;
3347
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003348 (void)call_func_noret(p_opfunc, 1, argv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003349
3350 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003351 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003352 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003353 {
3354 curbuf->b_op_start = orig_start;
3355 curbuf->b_op_end = orig_end;
3356 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003357 }
3358#else
3359 emsg(_("E775: Eval feature not available"));
3360#endif
3361}
3362
3363/*
3364 * Calculate start/end virtual columns for operating in block mode.
3365 */
3366 static void
3367get_op_vcol(
3368 oparg_T *oap,
3369 colnr_T redo_VIsual_vcol,
3370 int initial) // when TRUE adjust position for 'selectmode'
3371{
3372 colnr_T start, end;
3373
3374 if (VIsual_mode != Ctrl_V
3375 || (!initial && oap->end.col < curwin->w_width))
3376 return;
3377
3378 oap->block_mode = TRUE;
3379
3380 // prevent from moving onto a trail byte
3381 if (has_mbyte)
3382 mb_adjustpos(curwin->w_buffer, &oap->end);
3383
3384 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3385
3386 if (!redo_VIsual_busy)
3387 {
3388 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3389
3390 if (start < oap->start_vcol)
3391 oap->start_vcol = start;
3392 if (end > oap->end_vcol)
3393 {
3394 if (initial && *p_sel == 'e' && start >= 1
3395 && start - 1 >= oap->end_vcol)
3396 oap->end_vcol = start - 1;
3397 else
3398 oap->end_vcol = end;
3399 }
3400 }
3401
3402 // if '$' was used, get oap->end_vcol from longest line
3403 if (curwin->w_curswant == MAXCOL)
3404 {
3405 curwin->w_cursor.col = MAXCOL;
3406 oap->end_vcol = 0;
3407 for (curwin->w_cursor.lnum = oap->start.lnum;
3408 curwin->w_cursor.lnum <= oap->end.lnum;
3409 ++curwin->w_cursor.lnum)
3410 {
3411 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3412 if (end > oap->end_vcol)
3413 oap->end_vcol = end;
3414 }
3415 }
3416 else if (redo_VIsual_busy)
3417 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3418 // Correct oap->end.col and oap->start.col to be the
3419 // upper-left and lower-right corner of the block area.
3420 //
3421 // (Actually, this does convert column positions into character
3422 // positions)
3423 curwin->w_cursor.lnum = oap->end.lnum;
3424 coladvance(oap->end_vcol);
3425 oap->end = curwin->w_cursor;
3426
3427 curwin->w_cursor = oap->start;
3428 coladvance(oap->start_vcol);
3429 oap->start = curwin->w_cursor;
3430}
3431
3432/*
3433 * Handle an operator after Visual mode or when the movement is finished.
3434 * "gui_yank" is true when yanking text for the clipboard.
3435 */
3436 void
3437do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3438{
3439 oparg_T *oap = cap->oap;
3440 pos_T old_cursor;
3441 int empty_region_error;
3442 int restart_edit_save;
3443#ifdef FEAT_LINEBREAK
3444 int lbr_saved = curwin->w_p_lbr;
3445#endif
3446
3447 // The visual area is remembered for redo
3448 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3449 static linenr_T redo_VIsual_line_count; // number of lines
3450 static colnr_T redo_VIsual_vcol; // number of cols or end column
3451 static long redo_VIsual_count; // count for Visual operator
3452 static int redo_VIsual_arg; // extra argument
3453 int include_line_break = FALSE;
3454
3455#if defined(FEAT_CLIPBOARD)
3456 // Yank the visual area into the GUI selection register before we operate
3457 // on it and lose it forever.
3458 // Don't do it if a specific register was specified, so that ""x"*P works.
3459 // This could call do_pending_operator() recursively, but that's OK
3460 // because gui_yank will be TRUE for the nested call.
3461 if ((clip_star.available || clip_plus.available)
3462 && oap->op_type != OP_NOP
3463 && !gui_yank
3464 && VIsual_active
3465 && !redo_VIsual_busy
3466 && oap->regname == 0)
3467 clip_auto_select();
3468#endif
3469 old_cursor = curwin->w_cursor;
3470
3471 // If an operation is pending, handle it...
3472 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3473 {
3474 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3475 // for the clipboard.
3476 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3477
3478#ifdef FEAT_LINEBREAK
3479 // Avoid a problem with unwanted linebreaks in block mode.
3480 if (curwin->w_p_lbr)
3481 curwin->w_valid &= ~VALID_VIRTCOL;
3482 curwin->w_p_lbr = FALSE;
3483#endif
3484 oap->is_VIsual = VIsual_active;
3485 if (oap->motion_force == 'V')
3486 oap->motion_type = MLINE;
3487 else if (oap->motion_force == 'v')
3488 {
3489 // If the motion was linewise, "inclusive" will not have been set.
3490 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3491 if (oap->motion_type == MLINE)
3492 oap->inclusive = FALSE;
3493 // If the motion already was characterwise, toggle "inclusive"
3494 else if (oap->motion_type == MCHAR)
3495 oap->inclusive = !oap->inclusive;
3496 oap->motion_type = MCHAR;
3497 }
3498 else if (oap->motion_force == Ctrl_V)
3499 {
3500 // Change line- or characterwise motion into Visual block mode.
3501 if (!VIsual_active)
3502 {
3503 VIsual_active = TRUE;
3504 VIsual = oap->start;
3505 }
3506 VIsual_mode = Ctrl_V;
3507 VIsual_select = FALSE;
3508 VIsual_reselect = FALSE;
3509 }
3510
3511 // Only redo yank when 'y' flag is in 'cpoptions'.
3512 // Never redo "zf" (define fold).
3513 if ((redo_yank || oap->op_type != OP_YANK)
3514 && ((!VIsual_active || oap->motion_force)
3515 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003516 || (VIsual_active
3517 && (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3518 && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003519 && cap->cmdchar != 'D'
3520#ifdef FEAT_FOLDING
3521 && oap->op_type != OP_FOLD
3522 && oap->op_type != OP_FOLDOPEN
3523 && oap->op_type != OP_FOLDOPENREC
3524 && oap->op_type != OP_FOLDCLOSE
3525 && oap->op_type != OP_FOLDCLOSEREC
3526 && oap->op_type != OP_FOLDDEL
3527 && oap->op_type != OP_FOLDDELREC
3528#endif
3529 )
3530 {
3531 prep_redo(oap->regname, cap->count0,
3532 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3533 oap->motion_force, cap->cmdchar, cap->nchar);
3534 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3535 {
3536 // If 'cpoptions' does not contain 'r', insert the search
3537 // pattern to really repeat the same command.
3538 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3539 AppendToRedobuffLit(cap->searchbuf, -1);
3540 AppendToRedobuff(NL_STR);
3541 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003542 else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003543 {
3544 // do_cmdline() has stored the first typed line in
3545 // "repeat_cmdline". When several lines are typed repeating
3546 // won't be possible.
3547 if (repeat_cmdline == NULL)
3548 ResetRedobuff();
3549 else
3550 {
3551 AppendToRedobuffLit(repeat_cmdline, -1);
3552 AppendToRedobuff(NL_STR);
3553 VIM_CLEAR(repeat_cmdline);
3554 }
3555 }
3556 }
3557
3558 if (redo_VIsual_busy)
3559 {
3560 // Redo of an operation on a Visual area. Use the same size from
3561 // redo_VIsual_line_count and redo_VIsual_vcol.
3562 oap->start = curwin->w_cursor;
3563 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3564 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3565 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3566 VIsual_mode = redo_VIsual_mode;
3567 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3568 {
3569 if (VIsual_mode == 'v')
3570 {
3571 if (redo_VIsual_line_count <= 1)
3572 {
3573 validate_virtcol();
3574 curwin->w_curswant =
3575 curwin->w_virtcol + redo_VIsual_vcol - 1;
3576 }
3577 else
3578 curwin->w_curswant = redo_VIsual_vcol;
3579 }
3580 else
3581 {
3582 curwin->w_curswant = MAXCOL;
3583 }
3584 coladvance(curwin->w_curswant);
3585 }
3586 cap->count0 = redo_VIsual_count;
3587 if (redo_VIsual_count != 0)
3588 cap->count1 = redo_VIsual_count;
3589 else
3590 cap->count1 = 1;
3591 }
3592 else if (VIsual_active)
3593 {
3594 if (!gui_yank)
3595 {
3596 // Save the current VIsual area for '< and '> marks, and "gv"
3597 curbuf->b_visual.vi_start = VIsual;
3598 curbuf->b_visual.vi_end = curwin->w_cursor;
3599 curbuf->b_visual.vi_mode = VIsual_mode;
3600 restore_visual_mode();
3601 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3602# ifdef FEAT_EVAL
3603 curbuf->b_visual_mode_eval = VIsual_mode;
3604# endif
3605 }
3606
3607 // In Select mode, a linewise selection is operated upon like a
3608 // characterwise selection.
3609 // Special case: gH<Del> deletes the last line.
3610 if (VIsual_select && VIsual_mode == 'V'
3611 && cap->oap->op_type != OP_DELETE)
3612 {
3613 if (LT_POS(VIsual, curwin->w_cursor))
3614 {
3615 VIsual.col = 0;
3616 curwin->w_cursor.col =
3617 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3618 }
3619 else
3620 {
3621 curwin->w_cursor.col = 0;
3622 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3623 }
3624 VIsual_mode = 'v';
3625 }
3626 // If 'selection' is "exclusive", backup one character for
3627 // charwise selections.
3628 else if (VIsual_mode == 'v')
3629 include_line_break = unadjust_for_sel();
3630
3631 oap->start = VIsual;
3632 if (VIsual_mode == 'V')
3633 {
3634 oap->start.col = 0;
3635 oap->start.coladd = 0;
3636 }
3637 }
3638
3639 // Set oap->start to the first position of the operated text, oap->end
3640 // to the end of the operated text. w_cursor is equal to oap->start.
3641 if (LT_POS(oap->start, curwin->w_cursor))
3642 {
3643#ifdef FEAT_FOLDING
3644 // Include folded lines completely.
3645 if (!VIsual_active)
3646 {
3647 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3648 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003649 if ((curwin->w_cursor.col > 0 || oap->inclusive
3650 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003651 && hasFolding(curwin->w_cursor.lnum, NULL,
3652 &curwin->w_cursor.lnum))
3653 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3654 }
3655#endif
3656 oap->end = curwin->w_cursor;
3657 curwin->w_cursor = oap->start;
3658
3659 // w_virtcol may have been updated; if the cursor goes back to its
3660 // previous position w_virtcol becomes invalid and isn't updated
3661 // automatically.
3662 curwin->w_valid &= ~VALID_VIRTCOL;
3663 }
3664 else
3665 {
3666#ifdef FEAT_FOLDING
3667 // Include folded lines completely.
3668 if (!VIsual_active && oap->motion_type == MLINE)
3669 {
3670 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3671 NULL))
3672 curwin->w_cursor.col = 0;
3673 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3674 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3675 }
3676#endif
3677 oap->end = oap->start;
3678 oap->start = curwin->w_cursor;
3679 }
3680
3681 // Just in case lines were deleted that make the position invalid.
3682 check_pos(curwin->w_buffer, &oap->end);
3683 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3684
3685 // Set "virtual_op" before resetting VIsual_active.
3686 virtual_op = virtual_active();
3687
3688 if (VIsual_active || redo_VIsual_busy)
3689 {
3690 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3691
3692 if (!redo_VIsual_busy && !gui_yank)
3693 {
3694 // Prepare to reselect and redo Visual: this is based on the
3695 // size of the Visual text
3696 resel_VIsual_mode = VIsual_mode;
3697 if (curwin->w_curswant == MAXCOL)
3698 resel_VIsual_vcol = MAXCOL;
3699 else
3700 {
3701 if (VIsual_mode != Ctrl_V)
3702 getvvcol(curwin, &(oap->end),
3703 NULL, NULL, &oap->end_vcol);
3704 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3705 {
3706 if (VIsual_mode != Ctrl_V)
3707 getvvcol(curwin, &(oap->start),
3708 &oap->start_vcol, NULL, NULL);
3709 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3710 }
3711 else
3712 resel_VIsual_vcol = oap->end_vcol;
3713 }
3714 resel_VIsual_line_count = oap->line_count;
3715 }
3716
3717 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3718 if ((redo_yank || oap->op_type != OP_YANK)
3719 && oap->op_type != OP_COLON
3720#ifdef FEAT_FOLDING
3721 && oap->op_type != OP_FOLD
3722 && oap->op_type != OP_FOLDOPEN
3723 && oap->op_type != OP_FOLDOPENREC
3724 && oap->op_type != OP_FOLDCLOSE
3725 && oap->op_type != OP_FOLDCLOSEREC
3726 && oap->op_type != OP_FOLDDEL
3727 && oap->op_type != OP_FOLDDELREC
3728#endif
3729 && oap->motion_force == NUL
3730 )
3731 {
3732 // Prepare for redoing. Only use the nchar field for "r",
3733 // otherwise it might be the second char of the operator.
3734 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3735 || cap->nchar == 'N'))
3736 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003737 get_op_char(oap->op_type),
3738 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003739 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003740 else if (cap->cmdchar != ':' && cap->cmdchar != K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003741 {
3742 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3743
3744 // reverse what nv_replace() did
3745 if (nchar == REPLACE_CR_NCHAR)
3746 nchar = CAR;
3747 else if (nchar == REPLACE_NL_NCHAR)
3748 nchar = NL;
3749 prep_redo(oap->regname, 0L, NUL, 'v',
3750 get_op_char(oap->op_type),
3751 get_extra_op_char(oap->op_type),
3752 nchar);
3753 }
3754 if (!redo_VIsual_busy)
3755 {
3756 redo_VIsual_mode = resel_VIsual_mode;
3757 redo_VIsual_vcol = resel_VIsual_vcol;
3758 redo_VIsual_line_count = resel_VIsual_line_count;
3759 redo_VIsual_count = cap->count0;
3760 redo_VIsual_arg = cap->arg;
3761 }
3762 }
3763
3764 // oap->inclusive defaults to TRUE.
3765 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3766 // FALSE. This makes "d}P" and "v}dP" work the same.
3767 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3768 oap->inclusive = TRUE;
3769 if (VIsual_mode == 'V')
3770 oap->motion_type = MLINE;
3771 else
3772 {
3773 oap->motion_type = MCHAR;
3774 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3775 && (include_line_break || !virtual_op))
3776 {
3777 oap->inclusive = FALSE;
3778 // Try to include the newline, unless it's an operator
3779 // that works on lines only.
3780 if (*p_sel != 'o'
3781 && !op_on_lines(oap->op_type)
3782 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3783 {
3784 ++oap->end.lnum;
3785 oap->end.col = 0;
3786 oap->end.coladd = 0;
3787 ++oap->line_count;
3788 }
3789 }
3790 }
3791
3792 redo_VIsual_busy = FALSE;
3793
3794 // Switch Visual off now, so screen updating does
3795 // not show inverted text when the screen is redrawn.
3796 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3797 // no screen redraw, so it is done here to remove the inverted
3798 // part.
3799 if (!gui_yank)
3800 {
3801 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003802 setmouse();
3803 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003804 may_clear_cmdline();
3805 if ((oap->op_type == OP_YANK
3806 || oap->op_type == OP_COLON
3807 || oap->op_type == OP_FUNCTION
3808 || oap->op_type == OP_FILTER)
3809 && oap->motion_force == NUL)
3810 {
3811#ifdef FEAT_LINEBREAK
3812 // make sure redrawing is correct
3813 curwin->w_p_lbr = lbr_saved;
3814#endif
3815 redraw_curbuf_later(INVERTED);
3816 }
3817 }
3818 }
3819
3820 // Include the trailing byte of a multi-byte char.
3821 if (has_mbyte && oap->inclusive)
3822 {
3823 int l;
3824
3825 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3826 if (l > 1)
3827 oap->end.col += l - 1;
3828 }
3829 curwin->w_set_curswant = TRUE;
3830
3831 // oap->empty is set when start and end are the same. The inclusive
3832 // flag affects this too, unless yanking and the end is on a NUL.
3833 oap->empty = (oap->motion_type == MCHAR
3834 && (!oap->inclusive
3835 || (oap->op_type == OP_YANK
3836 && gchar_pos(&oap->end) == NUL))
3837 && EQUAL_POS(oap->start, oap->end)
3838 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3839 // For delete, change and yank, it's an error to operate on an
3840 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3841 empty_region_error = (oap->empty
3842 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3843
3844 // Force a redraw when operating on an empty Visual region, when
3845 // 'modifiable is off or creating a fold.
3846 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3847#ifdef FEAT_FOLDING
3848 || oap->op_type == OP_FOLD
3849#endif
3850 ))
3851 {
3852#ifdef FEAT_LINEBREAK
3853 curwin->w_p_lbr = lbr_saved;
3854#endif
3855 redraw_curbuf_later(INVERTED);
3856 }
3857
3858 // If the end of an operator is in column one while oap->motion_type
3859 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3860 // character in the previous line. If op_start is on or before the
3861 // first non-blank in the line, the operator becomes linewise
3862 // (strange, but that's the way vi does it).
3863 if ( oap->motion_type == MCHAR
3864 && oap->inclusive == FALSE
3865 && !(cap->retval & CA_NO_ADJ_OP_END)
3866 && oap->end.col == 0
3867 && (!oap->is_VIsual || *p_sel == 'o')
3868 && !oap->block_mode
3869 && oap->line_count > 1)
3870 {
3871 oap->end_adjusted = TRUE; // remember that we did this
3872 --oap->line_count;
3873 --oap->end.lnum;
3874 if (inindent(0))
3875 oap->motion_type = MLINE;
3876 else
3877 {
3878 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3879 if (oap->end.col)
3880 {
3881 --oap->end.col;
3882 oap->inclusive = TRUE;
3883 }
3884 }
3885 }
3886 else
3887 oap->end_adjusted = FALSE;
3888
3889 switch (oap->op_type)
3890 {
3891 case OP_LSHIFT:
3892 case OP_RSHIFT:
3893 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3894 auto_format(FALSE, TRUE);
3895 break;
3896
3897 case OP_JOIN_NS:
3898 case OP_JOIN:
3899 if (oap->line_count < 2)
3900 oap->line_count = 2;
3901 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3902 curbuf->b_ml.ml_line_count)
3903 beep_flush();
3904 else
3905 {
3906 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3907 TRUE, TRUE, TRUE);
3908 auto_format(FALSE, TRUE);
3909 }
3910 break;
3911
3912 case OP_DELETE:
3913 VIsual_reselect = FALSE; // don't reselect now
3914 if (empty_region_error)
3915 {
3916 vim_beep(BO_OPER);
3917 CancelRedo();
3918 }
3919 else
3920 {
3921 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01003922 // save cursor line for undo if it wasn't saved yet
3923 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3924 && u_save_cursor() == OK)
3925 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003926 }
3927 break;
3928
3929 case OP_YANK:
3930 if (empty_region_error)
3931 {
3932 if (!gui_yank)
3933 {
3934 vim_beep(BO_OPER);
3935 CancelRedo();
3936 }
3937 }
3938 else
3939 {
3940#ifdef FEAT_LINEBREAK
3941 curwin->w_p_lbr = lbr_saved;
3942#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02003943 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003944 (void)op_yank(oap, FALSE, !gui_yank);
3945 }
3946 check_cursor_col();
3947 break;
3948
3949 case OP_CHANGE:
3950 VIsual_reselect = FALSE; // don't reselect now
3951 if (empty_region_error)
3952 {
3953 vim_beep(BO_OPER);
3954 CancelRedo();
3955 }
3956 else
3957 {
3958 // This is a new edit command, not a restart. Need to
3959 // remember it to make 'insertmode' work with mappings for
3960 // Visual mode. But do this only once and not when typed and
3961 // 'insertmode' isn't set.
3962 if (p_im || !KeyTyped)
3963 restart_edit_save = restart_edit;
3964 else
3965 restart_edit_save = 0;
3966 restart_edit = 0;
3967#ifdef FEAT_LINEBREAK
3968 // Restore linebreak, so that when the user edits it looks as
3969 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01003970 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003971#endif
3972 // Reset finish_op now, don't want it set inside edit().
3973 finish_op = FALSE;
3974 if (op_change(oap)) // will call edit()
3975 cap->retval |= CA_COMMAND_BUSY;
3976 if (restart_edit == 0)
3977 restart_edit = restart_edit_save;
3978 }
3979 break;
3980
3981 case OP_FILTER:
3982 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3983 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
3984 else
3985 bangredo = TRUE; // do_bang() will put cmd in redo buffer
3986 // FALLTHROUGH
3987
3988 case OP_INDENT:
3989 case OP_COLON:
3990
3991#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3992 // If 'equalprg' is empty, do the indenting internally.
3993 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3994 {
3995# ifdef FEAT_LISP
3996 if (curbuf->b_p_lisp)
3997 {
3998 op_reindent(oap, get_lisp_indent);
3999 break;
4000 }
4001# endif
4002# ifdef FEAT_CINDENT
4003 op_reindent(oap,
4004# ifdef FEAT_EVAL
4005 *curbuf->b_p_inde != NUL ? get_expr_indent :
4006# endif
4007 get_c_indent);
4008 break;
4009# endif
4010 }
4011#endif
4012
4013 op_colon(oap);
4014 break;
4015
4016 case OP_TILDE:
4017 case OP_UPPER:
4018 case OP_LOWER:
4019 case OP_ROT13:
4020 if (empty_region_error)
4021 {
4022 vim_beep(BO_OPER);
4023 CancelRedo();
4024 }
4025 else
4026 op_tilde(oap);
4027 check_cursor_col();
4028 break;
4029
4030 case OP_FORMAT:
4031#if defined(FEAT_EVAL)
4032 if (*curbuf->b_p_fex != NUL)
4033 op_formatexpr(oap); // use expression
4034 else
4035#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004036 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004037 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004038 op_colon(oap); // use external command
4039 else
4040 op_format(oap, FALSE); // use internal function
4041 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004042 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004043 case OP_FORMAT2:
4044 op_format(oap, TRUE); // use internal function
4045 break;
4046
4047 case OP_FUNCTION:
4048#ifdef FEAT_LINEBREAK
4049 // Restore linebreak, so that when the user edits it looks as
4050 // before.
4051 curwin->w_p_lbr = lbr_saved;
4052#endif
4053 op_function(oap); // call 'operatorfunc'
4054 break;
4055
4056 case OP_INSERT:
4057 case OP_APPEND:
4058 VIsual_reselect = FALSE; // don't reselect now
4059 if (empty_region_error)
4060 {
4061 vim_beep(BO_OPER);
4062 CancelRedo();
4063 }
4064 else
4065 {
4066 // This is a new edit command, not a restart. Need to
4067 // remember it to make 'insertmode' work with mappings for
4068 // Visual mode. But do this only once.
4069 restart_edit_save = restart_edit;
4070 restart_edit = 0;
4071#ifdef FEAT_LINEBREAK
4072 // Restore linebreak, so that when the user edits it looks as
4073 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004074 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004075#endif
4076 op_insert(oap, cap->count1);
4077#ifdef FEAT_LINEBREAK
4078 // Reset linebreak, so that formatting works correctly.
4079 curwin->w_p_lbr = FALSE;
4080#endif
4081
4082 // TODO: when inserting in several lines, should format all
4083 // the lines.
4084 auto_format(FALSE, TRUE);
4085
4086 if (restart_edit == 0)
4087 restart_edit = restart_edit_save;
4088 else
4089 cap->retval |= CA_COMMAND_BUSY;
4090 }
4091 break;
4092
4093 case OP_REPLACE:
4094 VIsual_reselect = FALSE; // don't reselect now
4095 if (empty_region_error)
4096 {
4097 vim_beep(BO_OPER);
4098 CancelRedo();
4099 }
4100 else
4101 {
4102#ifdef FEAT_LINEBREAK
4103 // Restore linebreak, so that when the user edits it looks as
4104 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004105 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004106#endif
4107 op_replace(oap, cap->nchar);
4108 }
4109 break;
4110
4111#ifdef FEAT_FOLDING
4112 case OP_FOLD:
4113 VIsual_reselect = FALSE; // don't reselect now
4114 foldCreate(oap->start.lnum, oap->end.lnum);
4115 break;
4116
4117 case OP_FOLDOPEN:
4118 case OP_FOLDOPENREC:
4119 case OP_FOLDCLOSE:
4120 case OP_FOLDCLOSEREC:
4121 VIsual_reselect = FALSE; // don't reselect now
4122 opFoldRange(oap->start.lnum, oap->end.lnum,
4123 oap->op_type == OP_FOLDOPEN
4124 || oap->op_type == OP_FOLDOPENREC,
4125 oap->op_type == OP_FOLDOPENREC
4126 || oap->op_type == OP_FOLDCLOSEREC,
4127 oap->is_VIsual);
4128 break;
4129
4130 case OP_FOLDDEL:
4131 case OP_FOLDDELREC:
4132 VIsual_reselect = FALSE; // don't reselect now
4133 deleteFold(oap->start.lnum, oap->end.lnum,
4134 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4135 break;
4136#endif
4137 case OP_NR_ADD:
4138 case OP_NR_SUB:
4139 if (empty_region_error)
4140 {
4141 vim_beep(BO_OPER);
4142 CancelRedo();
4143 }
4144 else
4145 {
4146 VIsual_active = TRUE;
4147#ifdef FEAT_LINEBREAK
4148 curwin->w_p_lbr = lbr_saved;
4149#endif
4150 op_addsub(oap, cap->count1, redo_VIsual_arg);
4151 VIsual_active = FALSE;
4152 }
4153 check_cursor_col();
4154 break;
4155 default:
4156 clearopbeep(oap);
4157 }
4158 virtual_op = MAYBE;
4159 if (!gui_yank)
4160 {
4161 // if 'sol' not set, go back to old column for some commands
4162 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4163 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4164 || oap->op_type == OP_DELETE))
4165 {
4166#ifdef FEAT_LINEBREAK
4167 curwin->w_p_lbr = FALSE;
4168#endif
4169 coladvance(curwin->w_curswant = old_col);
4170 }
4171 }
4172 else
4173 {
4174 curwin->w_cursor = old_cursor;
4175 }
4176 oap->block_mode = FALSE;
4177 clearop(oap);
4178 motion_force = NUL;
4179 }
4180#ifdef FEAT_LINEBREAK
4181 curwin->w_p_lbr = lbr_saved;
4182#endif
4183}