blob: 8f2ca8e36947415a12f78b75dbf1c9c9994a8ba4 [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 Moolenaarb5cf6c32014-08-16 18:36:43 +0200548
Bram Moolenaar964b3742019-05-24 18:54:09 +0200549 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 if (newp == NULL)
551 continue;
552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100553 // copy up to shifted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 mch_memmove(newp, oldp, (size_t)(offset));
555 oldp += offset;
556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200558 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200559 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200562 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 offset += s_len;
564
565 if (spaces && !bdp->is_short)
566 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 // insert post-padding
Bram Moolenaardac13472019-09-16 21:06:21 +0200568 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 // We're splitting a TAB, don't copy it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 oldp++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100571 // We allowed for that TAB, remember this now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 count++;
573 }
574
575 if (spaces > 0)
576 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000577 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
579 ml_replace(lnum, newp, FALSE);
580
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200581 if (b_insert)
582 // correct any text properties
583 inserted_bytes(lnum, startcol, s_len);
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (lnum == oap->end.lnum)
586 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100587 // Set "']" mark to the end of the block instead of the end of
588 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 curbuf->b_op_end.lnum = oap->end.lnum;
590 curbuf->b_op_end.col = offset;
591 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100592 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
595
596 State = oldstate;
597}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200600 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200602 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 */
604 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100605op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 int n;
608 linenr_T lnum;
609 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 char_u *newp, *oldp;
611 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
613 int did_yank = FALSE;
614
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100615 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 return OK;
617
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100618 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (oap->empty)
620 return u_save_cursor();
621
622 if (!curbuf->b_p_ma)
623 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200624 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 return FAIL;
626 }
627
628#ifdef FEAT_CLIPBOARD
629 adjust_clip_reg(&oap->regname);
630#endif
631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (has_mbyte)
633 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
Bram Moolenaard04b7502010-07-08 22:27:55 +0200635 /*
636 * Imitate the strange Vi behaviour: If the delete spans more than one
637 * line and motion_type == MCHAR and the result is a blank line, make the
638 * delete linewise. Don't do this for the change command or Visual mode.
639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000642 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100644 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 && oap->op_type == OP_DELETE)
646 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200647 ptr = ml_get(oap->end.lnum) + oap->end.col;
648 if (*ptr != NUL)
649 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 ptr = skipwhite(ptr);
651 if (*ptr == NUL && inindent(0))
652 oap->motion_type = MLINE;
653 }
654
Bram Moolenaard04b7502010-07-08 22:27:55 +0200655 /*
656 * Check for trying to delete (e.g. "D") in an empty line.
657 * Note: For the change operator it is ok.
658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if ( oap->motion_type == MCHAR
660 && oap->line_count == 1
661 && oap->op_type == OP_DELETE
662 && *ml_get(oap->start.lnum) == NUL)
663 {
664 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000665 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 * 'cpoptions' (Vi compatible).
667 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000668 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
670 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000671 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
673 beep_flush();
674 return OK;
675 }
676
Bram Moolenaard04b7502010-07-08 22:27:55 +0200677 /*
678 * Do a yank of whatever we're about to delete.
679 * If a yank register was specified, put the deleted text into that
680 * register. For the black hole register '_' don't yank anything.
681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 if (oap->regname != '_')
683 {
684 if (oap->regname != 0)
685 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (!valid_yank_reg(oap->regname, TRUE))
688 {
689 beep_flush();
690 return OK;
691 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100692 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
693 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 did_yank = TRUE;
695 }
696
697 /*
698 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100699 * delete contains a line break, or when using a specific operator (Vi
700 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200701 * Use the register name from before adjust_clip_reg() may have
702 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100704 if (oap->motion_type == MLINE || oap->line_count > 1
705 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100707 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (op_yank(oap, TRUE, FALSE) == OK)
709 did_yank = TRUE;
710 }
711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // Yank into small delete register when no named register specified
713 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200714 if ((
715#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200716 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
717 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200718#endif
719 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 && oap->line_count == 1)
721 {
722 oap->regname = '-';
723 get_yank_register(oap->regname, TRUE);
724 if (op_yank(oap, TRUE, FALSE) == OK)
725 did_yank = TRUE;
726 oap->regname = 0;
727 }
728
729 /*
730 * If there's too much stuff to fit in the yank register, then get a
731 * confirmation before doing the delete. This is crude, but simple.
732 * And it avoids doing a delete of something we can't put back if we
733 * want.
734 */
735 if (!did_yank)
736 {
737 int msg_silent_save = msg_silent;
738
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100739 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
741 msg_silent = msg_silent_save;
742 if (n != 'y')
743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 return FAIL;
746 }
747 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100748
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100749#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100750 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200751 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754
Bram Moolenaard04b7502010-07-08 22:27:55 +0200755 /*
756 * block mode delete
757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (oap->block_mode)
759 {
760 if (u_save((linenr_T)(oap->start.lnum - 1),
761 (linenr_T)(oap->end.lnum + 1)) == FAIL)
762 return FAIL;
763
764 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
765 {
766 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 continue;
769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100770 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (lnum == curwin->w_cursor.lnum)
772 {
773 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
776
Bram Moolenaar8055d172019-05-17 22:57:26 +0200777 // "n" == number of chars deleted
778 // If we delete a TAB, it may be replaced by several characters.
779 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 n = bd.textlen - bd.startspaces - bd.endspaces;
781 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200782 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (newp == NULL)
784 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100787 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200788 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100790 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000792 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100793 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200795
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100796#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200797 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200798 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801
802 check_cursor_col();
803 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
804 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100805 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100807 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
809 if (oap->op_type == OP_CHANGE)
810 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100811 // Delete the lines except the first one. Temporarily move the
812 // cursor to the next line. Save the current line number, if the
813 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (oap->line_count > 1)
815 {
816 lnum = curwin->w_cursor.lnum;
817 ++curwin->w_cursor.lnum;
818 del_lines((long)(oap->line_count - 1), TRUE);
819 curwin->w_cursor.lnum = lnum;
820 }
821 if (u_save_cursor() == FAIL)
822 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100823 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100825 beginline(BL_WHITE); // cursor on first non-white
826 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ai_col = curwin->w_cursor.col;
828 }
829 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 beginline(0); // cursor in column 0
831 truncate_line(FALSE); // delete the rest of the line
832 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100834 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836 else
837 {
838 del_lines(oap->line_count, TRUE);
839 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100840 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843 else
844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (virtual_op)
846 {
847 int endcol = 0;
848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100849 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (gchar_pos(&oap->start) == '\t')
851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100852 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 return FAIL;
854 if (oap->line_count == 1)
855 endcol = getviscol2(oap->end.col, oap->end.coladd);
856 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
857 oap->start = curwin->w_cursor;
858 if (oap->line_count == 1)
859 {
860 coladvance(endcol);
861 oap->end.col = curwin->w_cursor.col;
862 oap->end.coladd = curwin->w_cursor.coladd;
863 curwin->w_cursor = oap->start;
864 }
865 }
866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (gchar_pos(&oap->end) == '\t'
869 && (int)oap->end.coladd < oap->inclusive)
870 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (u_save((linenr_T)(oap->end.lnum - 1),
873 (linenr_T)(oap->end.lnum + 1)) == FAIL)
874 return FAIL;
875 curwin->w_cursor = oap->end;
876 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
877 oap->end = curwin->w_cursor;
878 curwin->w_cursor = oap->start;
879 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100880 if (has_mbyte)
881 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100884 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 return FAIL;
888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100889 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100890 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 && oap->op_type == OP_CHANGE
892 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100893 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 display_dollar(oap->end.col - !oap->inclusive);
895
896 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (virtual_op)
899 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100900 // fix up things for virtualedit-delete:
901 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 char_u *curline = ml_get_curline();
903 int len = (int)STRLEN(curline);
904
905 if (oap->end.coladd != 0
906 && (int)oap->end.col >= len - 1
907 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
908 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100909 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (n == 0 && oap->start.coladd != oap->end.coladd)
911 n = 1;
912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100913 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (gchar_cursor() != NUL)
915 curwin->w_cursor.coladd = 0;
916 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200917 (void)del_bytes((long)n, !virtual_op,
918 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
922 pos_T curpos;
923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
926 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
927 return FAIL;
928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100929 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 ++curwin->w_cursor.lnum;
933 del_lines((long)(oap->line_count - 2), FALSE);
934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200936 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200937 curwin->w_cursor.col = 0;
938 (void)del_bytes((long)n, !virtual_op,
939 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100940 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200941 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaard0a1dee2020-12-20 13:07:48 +0100943 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945
946 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
947
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000948setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200949 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100951 if (oap->block_mode)
952 {
953 curbuf->b_op_end.lnum = oap->end.lnum;
954 curbuf->b_op_end.col = oap->start.col;
955 }
956 else
957 curbuf->b_op_end = oap->start;
958 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 return OK;
962}
963
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964/*
965 * Adjust end of operating area for ending on a multi-byte character.
966 * Used for deletion.
967 */
968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100969mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 char_u *p;
972
973 if (oap->inclusive)
974 {
975 p = ml_get(oap->end.lnum);
976 oap->end.col += mb_tail_off(p, p + oap->end.col);
977 }
978}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar630afe82018-06-28 19:26:28 +0200980/*
981 * Replace the character under the cursor with "c".
982 * This takes care of multi-byte characters.
983 */
984 static void
985replace_character(int c)
986{
987 int n = State;
988
989 State = REPLACE;
990 ins_char(c);
991 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100992 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200993 dec_cursor();
994}
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996/*
997 * Replace a whole area with one character.
998 */
999 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
1002 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 char_u *newp, *oldp;
1005 size_t oldlen;
1006 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001007 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001008 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001011 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001013 if (c == REPLACE_CR_NCHAR)
1014 {
1015 had_ctrl_v_cr = TRUE;
1016 c = CAR;
1017 }
1018 else if (c == REPLACE_NL_NCHAR)
1019 {
1020 had_ctrl_v_cr = TRUE;
1021 c = NL;
1022 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (has_mbyte)
1025 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 if (u_save((linenr_T)(oap->start.lnum - 1),
1028 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1029 return FAIL;
1030
1031 /*
1032 * block mode replace
1033 */
1034 if (oap->block_mode)
1035 {
1036 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1037 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1038 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001039 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1041 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001042 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 // n == number of extra chars required
1045 // If we split a TAB, it may be replaced by several characters.
1046 // Thus the number of characters may increase!
1047 // If the range starts in virtual space, count the initial
1048 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1050 {
1051 pos_T vpos;
1052
Bram Moolenaara1381de2009-11-03 15:44:21 +00001053 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 getvpos(&vpos, oap->start_vcol);
1055 bd.startspaces += vpos.coladd;
1056 n = bd.startspaces;
1057 }
1058 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001059 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001062 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001066 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 numc = oap->end_vcol - oap->start_vcol + 1;
1068 if (bd.is_short && (!virtual_op || bd.is_MAX))
1069 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 // A double-wide character can be replaced only up to half the
1072 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if ((*mb_char2cells)(c) > 1)
1074 {
1075 if ((numc & 1) && !bd.is_short)
1076 {
1077 ++bd.endspaces;
1078 ++n;
1079 }
1080 numc = numc / 2;
1081 }
1082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001083 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 num_chars = numc;
1085 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001086 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 n += numc - bd.textlen;
1088
1089 oldp = ml_get_curline();
1090 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001091 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (newp == NULL)
1093 continue;
1094 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001095 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 mch_memmove(newp, oldp, (size_t)bd.textcol);
1097 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001099 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // insert replacement chars CHECK FOR ALLOCATED SPACE
1101 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1102 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001103 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001105 if (has_mbyte)
1106 {
1107 n = (int)STRLEN(newp);
1108 while (--num_chars >= 0)
1109 n += (*mb_char2bytes)(c, newp + n);
1110 }
1111 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001112 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001113 if (!bd.is_short)
1114 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001115 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001116 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001117 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001118 STRMOVE(newp + STRLEN(newp), oldp);
1119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
1121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001123 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001124 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001125 if (after_p != NULL)
1126 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001128 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001130 if (after_p != NULL)
1131 {
1132 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1133 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1134 oap->end.lnum++;
1135 vim_free(after_p);
1136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
1138 }
1139 else
1140 {
1141 /*
1142 * MCHAR and MLINE motion replace.
1143 */
1144 if (oap->motion_type == MLINE)
1145 {
1146 oap->start.col = 0;
1147 curwin->w_cursor.col = 0;
1148 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1149 if (oap->end.col)
1150 --oap->end.col;
1151 }
1152 else if (!oap->inclusive)
1153 dec(&(oap->end));
1154
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001155 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 n = gchar_cursor();
1158 if (n != NUL)
1159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1161 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001162 // This is slow, but it handles replacing a single-byte
1163 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001164 if (curwin->w_cursor.lnum == oap->end.lnum)
1165 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001166 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (n == TAB)
1171 {
1172 int end_vcol = 0;
1173
1174 if (curwin->w_cursor.lnum == oap->end.lnum)
1175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001176 // oap->end has to be recalculated when
1177 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 end_vcol = getviscol2(oap->end.col,
1179 oap->end.coladd);
1180 }
1181 coladvance_force(getviscol());
1182 if (curwin->w_cursor.lnum == oap->end.lnum)
1183 getvpos(&oap->end, end_vcol);
1184 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001185 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1189 {
1190 int virtcols = oap->end.coladd;
1191
1192 if (curwin->w_cursor.lnum == oap->start.lnum
1193 && oap->start.col == oap->end.col && oap->start.coladd)
1194 virtcols -= oap->start.coladd;
1195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001196 // oap->end has been trimmed so it's effectively inclusive;
1197 // as a result an extra +1 must be counted so we don't
1198 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1200 curwin->w_cursor.col -= (virtcols + 1);
1201 for (; virtcols >= 0; virtcols--)
1202 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001203 if ((*mb_char2len)(c) > 1)
1204 replace_character(c);
1205 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001206 PBYTE(curwin->w_cursor, c);
1207 if (inc(&curwin->w_cursor) == -1)
1208 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001212 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (inc_cursor() == -1)
1214 break;
1215 }
1216 }
1217
1218 curwin->w_cursor = oap->start;
1219 check_cursor();
1220 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1221
Bram Moolenaare1004402020-10-24 20:49:43 +02001222 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001223 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001224 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001225 curbuf->b_op_start = oap->start;
1226 curbuf->b_op_end = oap->end;
1227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
1229 return OK;
1230}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001232static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234/*
1235 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1236 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001238op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
1240 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001242 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
1244 if (u_save((linenr_T)(oap->start.lnum - 1),
1245 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1246 return;
1247
1248 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001249 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
1251 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1252 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001253 int one_change;
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 block_prep(oap, &bd, pos.lnum, FALSE);
1256 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001257 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1258 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001259
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001260#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001261 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1264
Bram Moolenaar009b2592004-10-24 19:18:58 +00001265 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1266 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001268 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 if (did_change)
1273 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1274 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 {
1277 if (oap->motion_type == MLINE)
1278 {
1279 oap->start.col = 0;
1280 pos.col = 0;
1281 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1282 if (oap->end.col)
1283 --oap->end.col;
1284 }
1285 else if (!oap->inclusive)
1286 dec(&(oap->end));
1287
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001288 if (pos.lnum == oap->end.lnum)
1289 did_change = swapchars(oap->op_type, &pos,
1290 oap->end.col - pos.col + 1);
1291 else
1292 for (;;)
1293 {
1294 did_change |= swapchars(oap->op_type, &pos,
1295 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1296 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001297 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001298 break;
1299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (did_change)
1301 {
1302 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1303 0L);
1304#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001305 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
1307 char_u *ptr;
1308 int count;
1309
1310 pos = oap->start;
1311 while (pos.lnum < oap->end.lnum)
1312 {
1313 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001314 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001315 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001317 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 pos.col = 0;
1319 pos.lnum++;
1320 }
1321 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1322 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001323 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001325 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327#endif
1328 }
1329 }
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001332 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
Bram Moolenaare1004402020-10-24 20:49:43 +02001335 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001336 {
1337 // Set '[ and '] marks.
1338 curbuf->b_op_start = oap->start;
1339 curbuf->b_op_end = oap->end;
1340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001343 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001344 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345}
1346
1347/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001348 * Invoke swapchar() on "length" bytes at position "pos".
1349 * "pos" is advanced to just after the changed characters.
1350 * "length" is rounded up to include the whole last multi-byte character.
1351 * Also works correctly when the number of bytes changes.
1352 * Returns TRUE if some character was changed.
1353 */
1354 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001355swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001356{
1357 int todo;
1358 int did_change = 0;
1359
1360 for (todo = length; todo > 0; --todo)
1361 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001362 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001363 {
1364 int len = (*mb_ptr2len)(ml_get_pos(pos));
1365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001366 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001367 if (len > 0)
1368 todo -= len - 1;
1369 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001370 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001371 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001372 break;
1373 }
1374 return did_change;
1375}
1376
1377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 * If op_type == OP_UPPER: make uppercase,
1379 * if op_type == OP_LOWER: make lowercase,
1380 * if op_type == OP_ROT13: do rot13 encoding,
1381 * else swap case of character at 'pos'
1382 * returns TRUE when something actually changed.
1383 */
1384 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001385swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 int c;
1388 int nc;
1389
1390 c = gchar_pos(pos);
1391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001392 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (c >= 0x80 && op_type == OP_ROT13)
1394 return FALSE;
1395
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001396 if (op_type == OP_UPPER && c == 0xdf
1397 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001398 {
1399 pos_T sp = curwin->w_cursor;
1400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001402 curwin->w_cursor = *pos;
1403 del_char(FALSE);
1404 ins_char('S');
1405 ins_char('S');
1406 curwin->w_cursor = sp;
1407 inc(pos);
1408 }
1409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001410 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 nc = c;
1413 if (MB_ISLOWER(c))
1414 {
1415 if (op_type == OP_ROT13)
1416 nc = ROT13(c, 'a');
1417 else if (op_type != OP_LOWER)
1418 nc = MB_TOUPPER(c);
1419 }
1420 else if (MB_ISUPPER(c))
1421 {
1422 if (op_type == OP_ROT13)
1423 nc = ROT13(c, 'A');
1424 else if (op_type != OP_UPPER)
1425 nc = MB_TOLOWER(c);
1426 }
1427 if (nc != c)
1428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1430 {
1431 pos_T sp = curwin->w_cursor;
1432
1433 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001434 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001435 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ins_char(nc);
1437 curwin->w_cursor = sp;
1438 }
1439 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001440 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 return TRUE;
1442 }
1443 return FALSE;
1444}
1445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446/*
1447 * op_insert - Insert and append operators for Visual mode.
1448 */
1449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001450op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 long ins_len, pre_textlen = 0;
1453 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001454 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 struct block_def bd;
1456 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001457 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001459 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001462 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 curwin->w_cursor.lnum = oap->start.lnum;
1464 update_screen(INVERTED);
1465
1466 if (oap->block_mode)
1467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001468 // When 'virtualedit' is used, need to insert the extra spaces before
1469 // doing block_prep(). When only "block" is used, virtual edit is
1470 // already disabled, but still need it when calling
1471 // coladvance_force().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (curwin->w_cursor.coladd > 0)
1473 {
1474 int old_ve_flags = ve_flags;
1475
1476 ve_flags = VE_ALL;
1477 if (u_save_cursor() == FAIL)
1478 return;
1479 coladvance_force(oap->op_type == OP_APPEND
1480 ? oap->end_vcol + 1 : getviscol());
1481 if (oap->op_type == OP_APPEND)
1482 --curwin->w_cursor.col;
1483 ve_flags = old_ve_flags;
1484 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001485 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001487 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001488 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (oap->op_type == OP_APPEND)
1492 firstline += bd.textlen;
1493 pre_textlen = (long)STRLEN(firstline);
1494 }
1495
1496 if (oap->op_type == OP_APPEND)
1497 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001498 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001500 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 curwin->w_set_curswant = TRUE;
1502 while (*ml_get_cursor() != NUL
1503 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1504 ++curwin->w_cursor.col;
1505 if (bd.is_short && !bd.is_MAX)
1506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001507 // First line was too short, make it longer and adjust the
1508 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (u_save_cursor() == FAIL)
1510 return;
1511 for (i = 0; i < bd.endspaces; ++i)
1512 ins_char(' ');
1513 bd.textlen += bd.endspaces;
1514 }
1515 }
1516 else
1517 {
1518 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001519 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001521 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001522 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 && oap->start_vcol != oap->end_vcol)
1524 inc_cursor();
1525 }
1526 }
1527
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001528 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001529 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 // When a tab was inserted, and the characters in front of the tab
1532 // have been converted to a tab as well, the column of the cursor
1533 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001534 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001535 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001536 oap->start = curbuf->b_op_start_orig;
1537
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001538 // If user has moved off this line, we don't know what to do, so do
1539 // nothing.
1540 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001541 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return;
1543
1544 if (oap->block_mode)
1545 {
1546 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001547 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001548 size_t len;
1549 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001551 // If indent kicked in, the firstline might have changed
1552 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001553 ind_post = (colnr_T)getwhitecols_curline();
1554 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1555 {
1556 bd.textcol += ind_post - ind_pre;
1557 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001558 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001559 }
1560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001561 // The user may have moved the cursor before inserting something, try
1562 // to adjust the block for that. But only do it, if the difference
1563 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001564 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1565 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001566 {
1567 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001568 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001569 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001570 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001571 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001572 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001573 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001574 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001575 pre_textlen -= t - oap->start_vcol;
1576 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001577 }
1578 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001579 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001580 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001581 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001582 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001583 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001584 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001585 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001586 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001587 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001588 pre_textlen -= t - oap->start_vcol;
1589 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001590 oap->op_type = OP_INSERT;
1591 }
1592 }
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 /*
1595 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001596 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 * Don't do this when "$" used, end-of-line will have changed.
1598 */
1599 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1600 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1601 {
1602 if (oap->op_type == OP_APPEND)
1603 {
1604 pre_textlen += bd2.textlen - bd.textlen;
1605 if (bd2.endspaces)
1606 --bd2.textlen;
1607 }
1608 bd.textcol = bd2.textcol;
1609 bd.textlen = bd2.textlen;
1610 }
1611
1612 /*
1613 * Subsequent calls to ml_get() flush the firstline data - take a
1614 * copy of the required string.
1615 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001616 firstline = ml_get(oap->start.lnum);
1617 len = STRLEN(firstline);
1618 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001620 add += bd.textlen;
1621 if ((size_t)add > len)
1622 firstline += len; // short line, point to the NUL
1623 else
1624 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001625 if (pre_textlen >= 0
1626 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001628 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (ins_text != NULL)
1630 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001631 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (u_save(oap->start.lnum,
1633 (linenr_T)(oap->end.lnum + 1)) == OK)
1634 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1635 &bd);
1636
1637 curwin->w_cursor.col = oap->start.col;
1638 check_cursor();
1639 vim_free(ins_text);
1640 }
1641 }
1642 }
1643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645/*
1646 * op_change - handle a change operation
1647 *
1648 * return TRUE if edit() returns because of a CTRL-O command
1649 */
1650 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001651op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653 colnr_T l;
1654 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 long offset;
1656 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001657 long ins_len;
1658 long pre_textlen = 0;
1659 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 char_u *firstline;
1661 char_u *ins_text, *newp, *oldp;
1662 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664 l = oap->start.col;
1665 if (oap->motion_type == MLINE)
1666 {
1667 l = 0;
1668#ifdef FEAT_SMARTINDENT
1669 if (!p_paste && curbuf->b_p_si
1670# ifdef FEAT_CINDENT
1671 && !curbuf->b_p_cin
1672# endif
1673 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001674 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675#endif
1676 }
1677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001678 // First delete the text in the region. In an empty buffer only need to
1679 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1681 {
1682 if (u_save_cursor() == FAIL)
1683 return FALSE;
1684 }
1685 else if (op_delete(oap) == FAIL)
1686 return FALSE;
1687
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001688 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 && !virtual_op)
1690 inc_cursor();
1691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001692 // check for still on same line (<CR> in inserted text meaningless)
1693 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (oap->block_mode)
1695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001696 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (virtual_op && (curwin->w_cursor.coladd > 0
1698 || gchar_cursor() == NUL))
1699 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001700 firstline = ml_get(oap->start.lnum);
1701 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001702 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 bd.textcol = curwin->w_cursor.col;
1704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1707 if (oap->motion_type == MLINE)
1708 fix_indent();
1709#endif
1710
1711 retval = edit(NUL, FALSE, (linenr_T)1);
1712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001714 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001716 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001718 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001720 // Auto-indenting may have changed the indent. If the cursor was past
1721 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001723 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001725 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001726
1727 pre_textlen += new_indent - pre_indent;
1728 bd.textcol += new_indent - pre_indent;
1729 }
1730
1731 ins_len = (long)STRLEN(firstline) - pre_textlen;
1732 if (ins_len > 0)
1733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001734 // Subsequent calls to ml_get() flush the firstline data - take a
1735 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001736 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001738 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1740 linenr++)
1741 {
1742 block_prep(oap, &bd, linenr, TRUE);
1743 if (!bd.is_short || virtual_op)
1744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 pos_T vpos;
1746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001747 // If the block starts in virtual space, count the
1748 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (bd.is_short)
1750 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001751 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754 else
1755 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001757 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if (newp == NULL)
1759 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001760 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 mch_memmove(newp, oldp, (size_t)bd.textcol);
1762 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001763 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1766 offset += ins_len;
1767 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001768 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 ml_replace(linenr, newp, FALSE);
1770 }
1771 }
1772 check_cursor();
1773
1774 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1775 }
1776 vim_free(ins_text);
1777 }
1778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 return retval;
1781}
1782
1783/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001784 * When the cursor is on the NUL past the end of the line and it should not be
1785 * there move it left.
1786 */
1787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001789{
1790 if (curwin->w_cursor.col > 0
1791 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001792 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 && !(restart_edit || (State & INSERT)))
1794 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001795 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001796 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001799 {
1800 colnr_T scol, ecol;
1801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001802 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001803 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1804 curwin->w_cursor.coladd = ecol - scol + 1;
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
1807}
1808
Bram Moolenaar81340392012-06-06 16:12:59 +02001809/*
1810 * If "process" is TRUE and the line begins with a comment leader (possibly
1811 * after some white space), return a pointer to the text after it. Put a boolean
1812 * value indicating whether the line ends with an unclosed comment in
1813 * "is_comment".
1814 * line - line to be processed,
1815 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001816 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001817 * include_space - whether to also skip space following the comment leader,
1818 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001819 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001820 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001821 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001822skip_comment(
1823 char_u *line,
1824 int process,
1825 int include_space,
1826 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001827{
1828 char_u *comment_flags = NULL;
1829 int lead_len;
1830 int leader_offset = get_last_leader_offset(line, &comment_flags);
1831
1832 *is_comment = FALSE;
1833 if (leader_offset != -1)
1834 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001835 // Let's check whether the line ends with an unclosed comment.
1836 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001837 while (*comment_flags)
1838 {
1839 if (*comment_flags == COM_END
1840 || *comment_flags == ':')
1841 break;
1842 ++comment_flags;
1843 }
1844 if (*comment_flags != COM_END)
1845 *is_comment = TRUE;
1846 }
1847
1848 if (process == FALSE)
1849 return line;
1850
1851 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1852
1853 if (lead_len == 0)
1854 return line;
1855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001856 // Find:
1857 // - COM_END,
1858 // - colon,
1859 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001860 while (*comment_flags)
1861 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001862 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001863 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001864 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001865 ++comment_flags;
1866 }
1867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001868 // If we found a colon, it means that we are not processing a line
1869 // starting with a closing part of a three-part comment. That's good,
1870 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001871 if (*comment_flags == ':' || *comment_flags == NUL)
1872 line += lead_len;
1873
1874 return line;
1875}
Bram Moolenaar81340392012-06-06 16:12:59 +02001876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001878 * Join 'count' lines (minimal 2) at cursor position.
1879 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001880 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1881 * leaders should not be removed.
1882 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1883 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001885 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 */
1887 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001888do_join(
1889 long count,
1890 int insert_space,
1891 int save_undo,
1892 int use_formatoptions UNUSED,
1893 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001895 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001896 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001897 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001899 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001900 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001901 int endcurr1 = NUL;
1902 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001903 int currsize = 0; // size of the current line
1904 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001906 colnr_T col = 0;
1907 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001908 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001909 int remove_comments = (use_formatoptions == TRUE)
1910 && has_format_option(FO_REMOVE_COMS);
1911 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001912#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001913 int propcount = 0; // number of props over all joined lines
1914 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001917 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1918 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1919 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001921 // Allocate an array to store the number of spaces inserted before each
1922 // line. We will use it to pre-compute the length of the new line and the
1923 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001924 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001925 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001927 if (remove_comments)
1928 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001929 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001930 if (comments == NULL)
1931 {
1932 vim_free(spaces);
1933 return FAIL;
1934 }
1935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001938 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001939 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001940 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001942 for (t = 0; t < count; ++t)
1943 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001944 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001945#ifdef FEAT_PROP_POPUP
1946 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1947#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001948 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001949 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001950 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001951 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1952 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1953 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001954 if (remove_comments)
1955 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001956 // We don't want to remove the comment leader if the
1957 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001958 if (t > 0 && prev_was_comment)
1959 {
1960
1961 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1962 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001963 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001964 curr = new_curr;
1965 }
1966 else
1967 curr = skip_comment(curr, FALSE, insert_space,
1968 &prev_was_comment);
1969 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001970
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001971 if (insert_space && t > 0)
1972 {
1973 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01001974 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01001975 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001976 && (!has_format_option(FO_MBYTE_JOIN)
1977 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
1978 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02001979 || (mb_ptr2char(curr) < 0x100
1980 && !(enc_utf8 && utf_eat_space(endcurr1)))
1981 || (endcurr1 < 0x100
1982 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001983 )
1984 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001985 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001986 if (endcurr1 == ' ')
1987 endcurr1 = endcurr2;
1988 else
1989 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001990 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001991 if ( p_js
1992 && (endcurr1 == '.'
1993 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
1994 && (endcurr1 == '?' || endcurr1 == '!'))))
1995 ++spaces[t];
1996 }
1997 }
1998 currsize = (int)STRLEN(curr);
1999 sumsize += currsize + spaces[t];
2000 endcurr1 = endcurr2 = NUL;
2001 if (insert_space && currsize > 0)
2002 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002003 if (has_mbyte)
2004 {
2005 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002006 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002007 endcurr1 = (*mb_ptr2char)(cend);
2008 if (cend > curr)
2009 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002010 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002011 endcurr2 = (*mb_ptr2char)(cend);
2012 }
2013 }
2014 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002015 {
2016 endcurr1 = *(curr + currsize - 1);
2017 if (currsize > 1)
2018 endcurr2 = *(curr + currsize - 2);
2019 }
2020 }
2021 line_breakcheck();
2022 if (got_int)
2023 {
2024 ret = FAIL;
2025 goto theend;
2026 }
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002030 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002032 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002033 newp_len = sumsize + 1;
2034#ifdef FEAT_PROP_POPUP
2035 newp_len += propcount * sizeof(textprop_T);
2036#endif
2037 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002038 if (newp == NULL)
2039 {
2040 ret = FAIL;
2041 goto theend;
2042 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002043 cend = newp + sumsize;
2044 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002046 /*
2047 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002048 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002049 *
2050 * Move marks from each deleted line to the joined line, adjusting the
2051 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2052 * should not really be a problem.
2053 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002054#ifdef FEAT_PROP_POPUP
2055 props_remaining = propcount;
2056#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002057 for (t = count - 1; ; --t)
2058 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002059 int spaces_removed;
2060
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002061 cend -= currsize;
2062 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002063
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002064 if (spaces[t] > 0)
2065 {
2066 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002067 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002068 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002069
2070 // If deleting more spaces than adding, the cursor moves no more than
2071 // what is added if it is inside these spaces.
2072 spaces_removed = (curr - curr_start) - spaces[t];
2073
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002074 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002075 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002076#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002077 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2078 curwin->w_cursor.lnum + t, t == count - 1,
2079 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002080#endif
2081
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002082 if (t == 0)
2083 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002084 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002085 if (remove_comments)
2086 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002087 if (insert_space && t > 1)
2088 curr = skipwhite(curr);
2089 currsize = (int)STRLEN(curr);
2090 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002091
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002092 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
Bram Moolenaare1004402020-10-24 20:49:43 +02002094 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002095 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002096 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002097 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002098 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002099 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002101 // Only report the change in the first line here, del_lines() will report
2102 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 changed_lines(curwin->w_cursor.lnum, currsize,
2104 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002106 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 * briefly, and then move it back. After del_lines() the cursor may
2108 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 */
2110 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002112 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 curwin->w_cursor.lnum = t;
2114
2115 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002116 * Set the cursor column:
2117 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002118 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002120 curwin->w_cursor.col =
2121 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 curwin->w_set_curswant = TRUE;
2126
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002127theend:
2128 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002129 if (remove_comments)
2130 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002131 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132}
2133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * prepare a few things for block mode yank/delete/tilde
2136 *
2137 * for delete:
2138 * - textlen includes the first/last char to be (partly) deleted
2139 * - start/endspaces is the number of columns that are taken by the
2140 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002141 * deleted.
2142 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 * - textlen includes the first/last char to be wholly yanked
2144 * - start/endspaces is the number of columns of the first/last yanked char
2145 * that are to be yanked.
2146 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002147 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002148block_prep(
2149 oparg_T *oap,
2150 struct block_def *bdp,
2151 linenr_T lnum,
2152 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 int incr = 0;
2155 char_u *pend;
2156 char_u *pstart;
2157 char_u *line;
2158 char_u *prev_pstart;
2159 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002160#ifdef FEAT_LINEBREAK
2161 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002163 // Avoid a problem with unwanted linebreaks in block mode.
2164 curwin->w_p_lbr = FALSE;
2165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 bdp->startspaces = 0;
2167 bdp->endspaces = 0;
2168 bdp->textlen = 0;
2169 bdp->start_vcol = 0;
2170 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 bdp->is_short = FALSE;
2172 bdp->is_oneChar = FALSE;
2173 bdp->pre_whitesp = 0;
2174 bdp->pre_whitesp_c = 0;
2175 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 bdp->start_char_vcols = 0;
2177
2178 line = ml_get(lnum);
2179 pstart = line;
2180 prev_pstart = line;
2181 while (bdp->start_vcol < oap->start_vcol && *pstart)
2182 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002183 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002184 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002186 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
2188 bdp->pre_whitesp += incr;
2189 bdp->pre_whitesp_c++;
2190 }
2191 else
2192 {
2193 bdp->pre_whitesp = 0;
2194 bdp->pre_whitesp_c = 0;
2195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002197 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
2202 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (!is_del || oap->op_type == OP_APPEND)
2205 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2206 }
2207 else
2208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 // notice: this converts partly selected Multibyte characters to
2210 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2212 if (is_del && bdp->startspaces)
2213 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2214 pend = pstart;
2215 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002216 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (oap->op_type == OP_INSERT)
2220 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2221 else if (oap->op_type == OP_APPEND)
2222 {
2223 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2224 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2225 }
2226 else
2227 {
2228 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2229 if (is_del && oap->op_type != OP_LSHIFT)
2230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002231 // just putting the sum of those two into
2232 // bdp->startspaces doesn't work for Visual replace,
2233 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 bdp->startspaces = bdp->start_char_vcols
2235 - (bdp->start_vcol - oap->start_vcol);
2236 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2237 }
2238 }
2239 }
2240 else
2241 {
2242 prev_pend = pend;
2243 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002247 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 bdp->end_vcol += incr;
2249 }
2250 if (bdp->end_vcol <= oap->end_vcol
2251 && (!is_del
2252 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002253 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002256 // Alternative: include spaces to fill up the block.
2257 // Disadvantage: can lead to trailing spaces when the line is
2258 // short where the text is put
2259 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (oap->op_type == OP_APPEND || virtual_op)
2261 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002262 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002264 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266 else if (bdp->end_vcol > oap->end_vcol)
2267 {
2268 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2269 if (!is_del && bdp->endspaces)
2270 {
2271 bdp->endspaces = incr - bdp->endspaces;
2272 if (pend != pstart)
2273 pend = prev_pend;
2274 }
2275 }
2276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (is_del && bdp->startspaces)
2279 pstart = prev_pstart;
2280 bdp->textlen = (int)(pend - pstart);
2281 }
2282 bdp->textcol = (colnr_T) (pstart - line);
2283 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002284#ifdef FEAT_LINEBREAK
2285 curwin->w_p_lbr = lbr_saved;
2286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002290 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002292 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002293op_addsub(
2294 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002295 linenr_T Prenum1, // Amount of add/subtract
2296 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002298 pos_T pos;
2299 struct block_def bd;
2300 int change_cnt = 0;
2301 linenr_T amount = Prenum1;
2302
Bram Moolenaar6b731882018-11-16 20:54:47 +01002303 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002304 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002305 // the call to changed_lines().
2306#ifdef FEAT_FOLDING
2307 disable_fold_update++;
2308#endif
2309
Bram Moolenaard79e5502016-01-10 22:13:02 +01002310 if (!VIsual_active)
2311 {
2312 pos = curwin->w_cursor;
2313 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002314 {
2315#ifdef FEAT_FOLDING
2316 disable_fold_update--;
2317#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002318 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002319 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002320 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002321#ifdef FEAT_FOLDING
2322 disable_fold_update--;
2323#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002324 if (change_cnt)
2325 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2326 }
2327 else
2328 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002329 int one_change;
2330 int length;
2331 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002332
2333 if (u_save((linenr_T)(oap->start.lnum - 1),
2334 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002335 {
2336#ifdef FEAT_FOLDING
2337 disable_fold_update--;
2338#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002339 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002340 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002341
2342 pos = oap->start;
2343 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2344 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002345 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002346 {
2347 block_prep(oap, &bd, pos.lnum, FALSE);
2348 pos.col = bd.textcol;
2349 length = bd.textlen;
2350 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002351 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002352 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002353 curwin->w_cursor.col = 0;
2354 pos.col = 0;
2355 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2356 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002357 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002358 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002359 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002360 dec(&(oap->end));
2361 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2362 pos.col = 0;
2363 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002364 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002365 pos.col += oap->start.col;
2366 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002367 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002368 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002369 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002370 length = (int)STRLEN(ml_get(oap->end.lnum));
2371 if (oap->end.col >= length)
2372 oap->end.col = length - 1;
2373 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002374 }
2375 }
2376 one_change = do_addsub(oap->op_type, &pos, length, amount);
2377 if (one_change)
2378 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002379 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002380 if (change_cnt == 0)
2381 startpos = curbuf->b_op_start;
2382 ++change_cnt;
2383 }
2384
2385#ifdef FEAT_NETBEANS_INTG
2386 if (netbeans_active() && one_change)
2387 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002388 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002389
2390 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002391 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002392 netbeans_inserted(curbuf, pos.lnum, pos.col,
2393 &ptr[pos.col], length);
2394 }
2395#endif
2396 if (g_cmd && one_change)
2397 amount += Prenum1;
2398 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002399
2400#ifdef FEAT_FOLDING
2401 disable_fold_update--;
2402#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002403 if (change_cnt)
2404 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2405
2406 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002408 redraw_curbuf_later(INVERTED);
2409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002410 // Set '[ mark if something changed. Keep the last end
2411 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002412 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002413 curbuf->b_op_start = startpos;
2414
2415 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002416 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002417 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002418 }
2419}
2420
2421/*
2422 * Add or subtract 'Prenum1' from a number in a line
2423 * op_type is OP_NR_ADD or OP_NR_SUB
2424 *
2425 * Returns TRUE if some character was changed.
2426 */
2427 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002428do_addsub(
2429 int op_type,
2430 pos_T *pos,
2431 int length,
2432 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 int col;
2435 char_u *buf1;
2436 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002437 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2438 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002439 uvarnumber_T n;
2440 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 char_u *ptr;
2442 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002444 int do_hex;
2445 int do_oct;
2446 int do_bin;
2447 int do_alpha;
2448 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002451 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002452 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002453 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002454 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002455 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002456 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002457 pos_T startpos;
2458 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002459 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002461 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2462 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2463 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2464 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2465 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002467 if (virtual_active())
2468 {
2469 save_coladd = pos->coladd;
2470 pos->coladd = 0;
2471 }
2472
Bram Moolenaard79e5502016-01-10 22:13:02 +01002473 curwin->w_cursor = *pos;
2474 ptr = ml_get(pos->lnum);
2475 col = pos->col;
2476
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002477 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002478 goto theend;
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 /*
2481 * First check if we are on a hexadecimal number, after the "0x".
2482 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002483 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002485 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002486 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002487 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002488 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002489 if (has_mbyte)
2490 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002491 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002492
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002493 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002494 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002495 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002496 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002497 if (has_mbyte)
2498 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002499 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002500
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002501 if ( do_bin
2502 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002503 && ! ((col > 0
2504 && (ptr[col] == 'X'
2505 || ptr[col] == 'x')
2506 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002507 && (!has_mbyte ||
2508 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002509 && vim_isxdigit(ptr[col + 1]))))
2510 {
2511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002512 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002513
Bram Moolenaard79e5502016-01-10 22:13:02 +01002514 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002515
2516 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002517 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002518 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002519 if (has_mbyte)
2520 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002521 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002522 }
2523
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002524 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002525 && col > 0
2526 && (ptr[col] == 'X'
2527 || ptr[col] == 'x')
2528 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002529 && (!has_mbyte ||
2530 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002531 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002532 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002533 && col > 0
2534 && (ptr[col] == 'B'
2535 || ptr[col] == 'b')
2536 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002537 && (!has_mbyte ||
2538 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002539 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002540 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002541 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002542 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002543 if (has_mbyte)
2544 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002545 }
2546 else
2547 {
2548 /*
2549 * Search forward and then backward to find the start of number.
2550 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002551 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002552
2553 while (ptr[col] != NUL
2554 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002555 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002556 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002557
2558 while (col > 0
2559 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002560 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002561 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002562 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002563 if (has_mbyte)
2564 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002565 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002566 }
2567 }
2568
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002569 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002570 {
2571 while (ptr[col] != NUL && length > 0
2572 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002573 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002574 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002575 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002576
2577 col += mb_len;
2578 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002579 }
2580
2581 if (length == 0)
2582 goto theend;
2583
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002584 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002585 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2586 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002587 {
2588 negative = TRUE;
2589 was_positive = FALSE;
2590 }
2591 }
2592
2593 /*
2594 * If a number was found, and saving for undo works, replace the number.
2595 */
2596 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002597 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002598 {
2599 beep_flush();
2600 goto theend;
2601 }
2602
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002603 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002604 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002605 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002606 if (op_type == OP_NR_SUB)
2607 {
2608 if (CharOrd(firstdigit) < Prenum1)
2609 {
2610 if (isupper(firstdigit))
2611 firstdigit = 'A';
2612 else
2613 firstdigit = 'a';
2614 }
2615 else
2616#ifdef EBCDIC
2617 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2618#else
2619 firstdigit -= Prenum1;
2620#endif
2621 }
2622 else
2623 {
2624 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2625 {
2626 if (isupper(firstdigit))
2627 firstdigit = 'Z';
2628 else
2629 firstdigit = 'z';
2630 }
2631 else
2632#ifdef EBCDIC
2633 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2634#else
2635 firstdigit += Prenum1;
2636#endif
2637 }
2638 curwin->w_cursor.col = col;
2639 if (!did_change)
2640 startpos = curwin->w_cursor;
2641 did_change = TRUE;
2642 (void)del_char(FALSE);
2643 ins_char(firstdigit);
2644 endpos = curwin->w_cursor;
2645 curwin->w_cursor.col = col;
2646 }
2647 else
2648 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002649 pos_T save_pos;
2650 int i;
2651
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002652 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002653 && (!has_mbyte ||
2654 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002655 && !visual
2656 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002657 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002659 --col;
2660 negative = TRUE;
2661 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002662 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002663 if (visual && VIsual_mode != 'V')
2664 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2665 ? (int)STRLEN(ptr) - col
2666 : length);
2667
2668 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002669 0 + (do_bin ? STR2NR_BIN : 0)
2670 + (do_oct ? STR2NR_OCT : 0)
2671 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002672 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002673
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002674 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002675 if (pre && negative)
2676 {
2677 ++col;
2678 --length;
2679 negative = FALSE;
2680 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002681 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002682 subtract = FALSE;
2683 if (op_type == OP_NR_SUB)
2684 subtract ^= TRUE;
2685 if (negative)
2686 subtract ^= TRUE;
2687
2688 oldn = n;
2689 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002690 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002691 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002692 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002694 if (!pre)
2695 {
2696 if (subtract)
2697 {
2698 if (n > oldn)
2699 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002700 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002701 negative ^= TRUE;
2702 }
2703 }
2704 else
2705 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002707 if (n < oldn)
2708 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002709 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002710 negative ^= TRUE;
2711 }
2712 }
2713 if (n == 0)
2714 negative = FALSE;
2715 }
2716
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002717 if (do_unsigned && negative)
2718 {
2719 if (subtract)
2720 // sticking at zero.
2721 n = (uvarnumber_T)0;
2722 else
2723 // sticking at 2^64 - 1.
2724 n = (uvarnumber_T)(-1);
2725 negative = FALSE;
2726 }
2727
Bram Moolenaard79e5502016-01-10 22:13:02 +01002728 if (visual && !was_positive && !negative && col > 0)
2729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002731 col--;
2732 length++;
2733 }
2734
2735 /*
2736 * Delete the old number.
2737 */
2738 curwin->w_cursor.col = col;
2739 if (!did_change)
2740 startpos = curwin->w_cursor;
2741 did_change = TRUE;
2742 todel = length;
2743 c = gchar_cursor();
2744 /*
2745 * Don't include the '-' in the length, only the length of the
2746 * part after it is kept the same.
2747 */
2748 if (c == '-')
2749 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002750
2751 save_pos = curwin->w_cursor;
2752 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002753 {
2754 if (c < 0x100 && isalpha(c))
2755 {
2756 if (isupper(c))
2757 hexupper = TRUE;
2758 else
2759 hexupper = FALSE;
2760 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002761 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002762 c = gchar_cursor();
2763 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002764 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002765
2766 /*
2767 * Prepare the leading characters in buf1[].
2768 * When there are many leading zeros it could be very long.
2769 * Allocate a bit too much.
2770 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002771 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002772 if (buf1 == NULL)
2773 goto theend;
2774 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002775 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002776 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002777 if (pre)
2778 {
2779 *ptr++ = '0';
2780 --length;
2781 }
2782 if (pre == 'b' || pre == 'B' ||
2783 pre == 'x' || pre == 'X')
2784 {
2785 *ptr++ = pre;
2786 --length;
2787 }
2788
2789 /*
2790 * Put the number characters in buf2[].
2791 */
2792 if (pre == 'b' || pre == 'B')
2793 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002794 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002795 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002798 for (bit = bits; bit > 0; bit--)
2799 if ((n >> (bit - 1)) & 0x1) break;
2800
2801 for (i = 0; bit > 0; bit--)
2802 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2803
2804 buf2[i] = '\0';
2805 }
2806 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002807 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002808 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002809 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002810 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002811 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002812 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002813 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002814 length -= (int)STRLEN(buf2);
2815
2816 /*
2817 * Adjust number of zeros to the new number of digits, so the
2818 * total length of the number remains the same.
2819 * Don't do this when
2820 * the result may look like an octal number.
2821 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002822 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002823 while (length-- > 0)
2824 *ptr++ = '0';
2825 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002826
Bram Moolenaard79e5502016-01-10 22:13:02 +01002827 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002828
2829 // Insert just after the first character to be removed, so that any
2830 // text properties will be adjusted. Then delete the old number
2831 // afterwards.
2832 save_pos = curwin->w_cursor;
2833 if (todel > 0)
2834 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002836 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002837
2838 // del_char() will also mark line needing displaying
2839 if (todel > 0)
2840 {
2841 int bytes_after = (int)STRLEN(ml_get_curline())
2842 - curwin->w_cursor.col;
2843
2844 // Delete the one character before the insert.
2845 curwin->w_cursor = save_pos;
2846 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002847 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2848 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002849 --todel;
2850 }
2851 while (todel-- > 0)
2852 (void)del_char(FALSE);
2853
Bram Moolenaard79e5502016-01-10 22:13:02 +01002854 endpos = curwin->w_cursor;
2855 if (did_change && curwin->w_cursor.col)
2856 --curwin->w_cursor.col;
2857 }
2858
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002862 curbuf->b_op_start = startpos;
2863 curbuf->b_op_end = endpos;
2864 if (curbuf->b_op_end.col > 0)
2865 --curbuf->b_op_end.col;
2866 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002867
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002868theend:
2869 if (visual)
2870 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002871 else if (did_change)
2872 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002873 else if (virtual_active())
2874 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002875
Bram Moolenaard79e5502016-01-10 22:13:02 +01002876 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877}
2878
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002880clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002882 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883}
2884
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002886 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 *
2888 * "Words" are counted by looking for boundaries between non-space and
2889 * space characters. (it seems to produce results that match 'wc'.)
2890 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002891 * Return value is byte count; word count for the line is added to "*wc".
2892 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 *
2894 * The function will only examine the first "limit" characters in the
2895 * line, stopping if it encounters an end-of-line (NUL byte). In that
2896 * case, eol_size will be added to the character count to account for
2897 * the size of the EOL character.
2898 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002899 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002900line_count_info(
2901 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002902 varnumber_T *wc,
2903 varnumber_T *cc,
2904 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002905 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002907 varnumber_T i;
2908 varnumber_T words = 0;
2909 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 int is_word = 0;
2911
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002912 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
2914 if (is_word)
2915 {
2916 if (vim_isspace(line[i]))
2917 {
2918 words++;
2919 is_word = 0;
2920 }
2921 }
2922 else if (!vim_isspace(line[i]))
2923 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002924 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002925 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927
2928 if (is_word)
2929 words++;
2930 *wc += words;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002933 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002936 chars += eol_size;
2937 }
2938 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 return i;
2940}
2941
2942/*
2943 * Give some info about the position of the cursor (for "g CTRL-G").
2944 * In Visual mode, give some info about the selected region. (In this case,
2945 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002946 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 */
2948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002949cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002952 char_u buf1[50];
2953 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002955 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002956 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002957 varnumber_T byte_count_cursor = 0;
2958 varnumber_T char_count = 0;
2959 varnumber_T char_count_cursor = 0;
2960 varnumber_T word_count = 0;
2961 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002962 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002963 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 long line_count_selected = 0;
2965 pos_T min_pos, max_pos;
2966 oparg_T oparg;
2967 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /*
2970 * Compute the length of the file in characters.
2971 */
2972 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2973 {
Bram Moolenaared767a22016-01-03 22:49:16 +01002974 if (dict == NULL)
2975 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002976 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01002977 return;
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 else
2981 {
2982 if (get_fileformat(curbuf) == EOL_DOS)
2983 eol_size = 2;
2984 else
2985 eol_size = 1;
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (VIsual_active)
2988 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002989 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 {
2991 min_pos = VIsual;
2992 max_pos = curwin->w_cursor;
2993 }
2994 else
2995 {
2996 min_pos = curwin->w_cursor;
2997 max_pos = VIsual;
2998 }
2999 if (*p_sel == 'e' && max_pos.col > 0)
3000 --max_pos.col;
3001
3002 if (VIsual_mode == Ctrl_V)
3003 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003004#ifdef FEAT_LINEBREAK
3005 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003006 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003009 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003010 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 oparg.is_VIsual = 1;
3013 oparg.block_mode = TRUE;
3014 oparg.op_type = OP_NOP;
3015 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003016 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003017#ifdef FEAT_LINEBREAK
3018 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003019 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003020#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003021 if (curwin->w_curswant == MAXCOL)
3022 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (oparg.end_vcol < oparg.start_vcol)
3025 {
3026 oparg.end_vcol += oparg.start_vcol;
3027 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3028 oparg.end_vcol -= oparg.start_vcol;
3029 }
3030 }
3031 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003037 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 ui_breakcheck();
3040 if (got_int)
3041 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003042 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 }
3044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (VIsual_active
3047 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3048 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003049 char_u *s = NULL;
3050 long len = 0L;
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 switch (VIsual_mode)
3053 {
3054 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003058 s = bd.textstart;
3059 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 break;
3061 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003062 s = ml_get(lnum);
3063 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 break;
3065 case 'v':
3066 {
3067 colnr_T start_col = (lnum == min_pos.lnum)
3068 ? min_pos.col : 0;
3069 colnr_T end_col = (lnum == max_pos.lnum)
3070 ? max_pos.col - start_col + 1 : MAXCOL;
3071
Bram Moolenaardef9e822004-12-31 20:58:58 +00003072 s = ml_get(lnum) + start_col;
3073 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075 break;
3076 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003077 if (s != NULL)
3078 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003079 byte_count_cursor += line_count_info(s, &word_count_cursor,
3080 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003081 if (lnum == curbuf->b_ml.ml_line_count
3082 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003083 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003084 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003085 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (lnum == curwin->w_cursor.lnum)
3092 {
3093 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003094 char_count_cursor += char_count;
3095 byte_count_cursor = byte_count +
3096 line_count_info(ml_get(lnum),
3097 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003098 (varnumber_T)(curwin->w_cursor.col + 1),
3099 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003103 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003104 &char_count, (varnumber_T)MAXCOL,
3105 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 }
3107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003109 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003110 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
Bram Moolenaared767a22016-01-03 22:49:16 +01003112 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003114 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003116 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3117 {
3118 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3119 &max_pos.col);
3120 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3121 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3122 }
3123 else
3124 buf1[0] = NUL;
3125
3126 if (char_count_cursor == byte_count_cursor
3127 && char_count == byte_count)
3128 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003129 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003130 buf1, line_count_selected,
3131 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003132 (varnumber_T)word_count_cursor,
3133 (varnumber_T)word_count,
3134 (varnumber_T)byte_count_cursor,
3135 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003136 else
3137 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003138 _("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 +01003139 buf1, line_count_selected,
3140 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003141 (varnumber_T)word_count_cursor,
3142 (varnumber_T)word_count,
3143 (varnumber_T)char_count_cursor,
3144 (varnumber_T)char_count,
3145 (varnumber_T)byte_count_cursor,
3146 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
3148 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003149 {
3150 p = ml_get_curline();
3151 validate_virtcol();
3152 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3153 (int)curwin->w_virtcol + 1);
3154 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3155 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaared767a22016-01-03 22:49:16 +01003157 if (char_count_cursor == byte_count_cursor
3158 && char_count == byte_count)
3159 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003160 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003161 (char *)buf1, (char *)buf2,
3162 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003164 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3165 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003166 else
3167 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003168 _("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 +01003169 (char *)buf1, (char *)buf2,
3170 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003171 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003172 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3173 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3174 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003175 }
3176 }
3177
Bram Moolenaared767a22016-01-03 22:49:16 +01003178 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003179 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003180 {
3181 size_t len = STRLEN(IObuff);
3182
3183 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003184 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003185 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003186 if (dict == NULL)
3187 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003188 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003189 p = p_shm;
3190 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003191 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003192 p_shm = p;
3193 }
3194 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003195#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003196 if (dict != NULL)
3197 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003198 dict_add_number(dict, "words", word_count);
3199 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003200 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003201 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3202 byte_count_cursor);
3203 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3204 char_count_cursor);
3205 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3206 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003210
3211/*
3212 * Handle indent and format operators and visual mode ":".
3213 */
3214 static void
3215op_colon(oparg_T *oap)
3216{
3217 stuffcharReadbuff(':');
3218 if (oap->is_VIsual)
3219 stuffReadbuff((char_u *)"'<,'>");
3220 else
3221 {
3222 // Make the range look nice, so it can be repeated.
3223 if (oap->start.lnum == curwin->w_cursor.lnum)
3224 stuffcharReadbuff('.');
3225 else
3226 stuffnumReadbuff((long)oap->start.lnum);
3227 if (oap->end.lnum != oap->start.lnum)
3228 {
3229 stuffcharReadbuff(',');
3230 if (oap->end.lnum == curwin->w_cursor.lnum)
3231 stuffcharReadbuff('.');
3232 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3233 stuffcharReadbuff('$');
3234 else if (oap->start.lnum == curwin->w_cursor.lnum)
3235 {
3236 stuffReadbuff((char_u *)".+");
3237 stuffnumReadbuff((long)oap->line_count - 1);
3238 }
3239 else
3240 stuffnumReadbuff((long)oap->end.lnum);
3241 }
3242 }
3243 if (oap->op_type != OP_COLON)
3244 stuffReadbuff((char_u *)"!");
3245 if (oap->op_type == OP_INDENT)
3246 {
3247#ifndef FEAT_CINDENT
3248 if (*get_equalprg() == NUL)
3249 stuffReadbuff((char_u *)"indent");
3250 else
3251#endif
3252 stuffReadbuff(get_equalprg());
3253 stuffReadbuff((char_u *)"\n");
3254 }
3255 else if (oap->op_type == OP_FORMAT)
3256 {
3257 if (*curbuf->b_p_fp != NUL)
3258 stuffReadbuff(curbuf->b_p_fp);
3259 else if (*p_fp != NUL)
3260 stuffReadbuff(p_fp);
3261 else
3262 stuffReadbuff((char_u *)"fmt");
3263 stuffReadbuff((char_u *)"\n']");
3264 }
3265
3266 // do_cmdline() does the rest
3267}
3268
3269/*
3270 * Handle the "g@" operator: call 'operatorfunc'.
3271 */
3272 static void
3273op_function(oparg_T *oap UNUSED)
3274{
3275#ifdef FEAT_EVAL
3276 typval_T argv[2];
3277 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003278 pos_T orig_start = curbuf->b_op_start;
3279 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003280
3281 if (*p_opfunc == NUL)
3282 emsg(_("E774: 'operatorfunc' is empty"));
3283 else
3284 {
3285 // Set '[ and '] marks to text to be operated on.
3286 curbuf->b_op_start = oap->start;
3287 curbuf->b_op_end = oap->end;
3288 if (oap->motion_type != MLINE && !oap->inclusive)
3289 // Exclude the end position.
3290 decl(&curbuf->b_op_end);
3291
3292 argv[0].v_type = VAR_STRING;
3293 if (oap->block_mode)
3294 argv[0].vval.v_string = (char_u *)"block";
3295 else if (oap->motion_type == MLINE)
3296 argv[0].vval.v_string = (char_u *)"line";
3297 else
3298 argv[0].vval.v_string = (char_u *)"char";
3299 argv[1].v_type = VAR_UNKNOWN;
3300
3301 // Reset virtual_op so that 'virtualedit' can be changed in the
3302 // function.
3303 virtual_op = MAYBE;
3304
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003305 (void)call_func_noret(p_opfunc, 1, argv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003306
3307 virtual_op = save_virtual_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003308 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003309 {
3310 curbuf->b_op_start = orig_start;
3311 curbuf->b_op_end = orig_end;
3312 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003313 }
3314#else
3315 emsg(_("E775: Eval feature not available"));
3316#endif
3317}
3318
3319/*
3320 * Calculate start/end virtual columns for operating in block mode.
3321 */
3322 static void
3323get_op_vcol(
3324 oparg_T *oap,
3325 colnr_T redo_VIsual_vcol,
3326 int initial) // when TRUE adjust position for 'selectmode'
3327{
3328 colnr_T start, end;
3329
3330 if (VIsual_mode != Ctrl_V
3331 || (!initial && oap->end.col < curwin->w_width))
3332 return;
3333
3334 oap->block_mode = TRUE;
3335
3336 // prevent from moving onto a trail byte
3337 if (has_mbyte)
3338 mb_adjustpos(curwin->w_buffer, &oap->end);
3339
3340 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3341
3342 if (!redo_VIsual_busy)
3343 {
3344 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3345
3346 if (start < oap->start_vcol)
3347 oap->start_vcol = start;
3348 if (end > oap->end_vcol)
3349 {
3350 if (initial && *p_sel == 'e' && start >= 1
3351 && start - 1 >= oap->end_vcol)
3352 oap->end_vcol = start - 1;
3353 else
3354 oap->end_vcol = end;
3355 }
3356 }
3357
3358 // if '$' was used, get oap->end_vcol from longest line
3359 if (curwin->w_curswant == MAXCOL)
3360 {
3361 curwin->w_cursor.col = MAXCOL;
3362 oap->end_vcol = 0;
3363 for (curwin->w_cursor.lnum = oap->start.lnum;
3364 curwin->w_cursor.lnum <= oap->end.lnum;
3365 ++curwin->w_cursor.lnum)
3366 {
3367 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3368 if (end > oap->end_vcol)
3369 oap->end_vcol = end;
3370 }
3371 }
3372 else if (redo_VIsual_busy)
3373 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3374 // Correct oap->end.col and oap->start.col to be the
3375 // upper-left and lower-right corner of the block area.
3376 //
3377 // (Actually, this does convert column positions into character
3378 // positions)
3379 curwin->w_cursor.lnum = oap->end.lnum;
3380 coladvance(oap->end_vcol);
3381 oap->end = curwin->w_cursor;
3382
3383 curwin->w_cursor = oap->start;
3384 coladvance(oap->start_vcol);
3385 oap->start = curwin->w_cursor;
3386}
3387
3388/*
3389 * Handle an operator after Visual mode or when the movement is finished.
3390 * "gui_yank" is true when yanking text for the clipboard.
3391 */
3392 void
3393do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3394{
3395 oparg_T *oap = cap->oap;
3396 pos_T old_cursor;
3397 int empty_region_error;
3398 int restart_edit_save;
3399#ifdef FEAT_LINEBREAK
3400 int lbr_saved = curwin->w_p_lbr;
3401#endif
3402
3403 // The visual area is remembered for redo
3404 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3405 static linenr_T redo_VIsual_line_count; // number of lines
3406 static colnr_T redo_VIsual_vcol; // number of cols or end column
3407 static long redo_VIsual_count; // count for Visual operator
3408 static int redo_VIsual_arg; // extra argument
3409 int include_line_break = FALSE;
3410
3411#if defined(FEAT_CLIPBOARD)
3412 // Yank the visual area into the GUI selection register before we operate
3413 // on it and lose it forever.
3414 // Don't do it if a specific register was specified, so that ""x"*P works.
3415 // This could call do_pending_operator() recursively, but that's OK
3416 // because gui_yank will be TRUE for the nested call.
3417 if ((clip_star.available || clip_plus.available)
3418 && oap->op_type != OP_NOP
3419 && !gui_yank
3420 && VIsual_active
3421 && !redo_VIsual_busy
3422 && oap->regname == 0)
3423 clip_auto_select();
3424#endif
3425 old_cursor = curwin->w_cursor;
3426
3427 // If an operation is pending, handle it...
3428 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3429 {
3430 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3431 // for the clipboard.
3432 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3433
3434#ifdef FEAT_LINEBREAK
3435 // Avoid a problem with unwanted linebreaks in block mode.
3436 if (curwin->w_p_lbr)
3437 curwin->w_valid &= ~VALID_VIRTCOL;
3438 curwin->w_p_lbr = FALSE;
3439#endif
3440 oap->is_VIsual = VIsual_active;
3441 if (oap->motion_force == 'V')
3442 oap->motion_type = MLINE;
3443 else if (oap->motion_force == 'v')
3444 {
3445 // If the motion was linewise, "inclusive" will not have been set.
3446 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3447 if (oap->motion_type == MLINE)
3448 oap->inclusive = FALSE;
3449 // If the motion already was characterwise, toggle "inclusive"
3450 else if (oap->motion_type == MCHAR)
3451 oap->inclusive = !oap->inclusive;
3452 oap->motion_type = MCHAR;
3453 }
3454 else if (oap->motion_force == Ctrl_V)
3455 {
3456 // Change line- or characterwise motion into Visual block mode.
3457 if (!VIsual_active)
3458 {
3459 VIsual_active = TRUE;
3460 VIsual = oap->start;
3461 }
3462 VIsual_mode = Ctrl_V;
3463 VIsual_select = FALSE;
3464 VIsual_reselect = FALSE;
3465 }
3466
3467 // Only redo yank when 'y' flag is in 'cpoptions'.
3468 // Never redo "zf" (define fold).
3469 if ((redo_yank || oap->op_type != OP_YANK)
3470 && ((!VIsual_active || oap->motion_force)
3471 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003472 || (VIsual_active
3473 && (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3474 && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003475 && cap->cmdchar != 'D'
3476#ifdef FEAT_FOLDING
3477 && oap->op_type != OP_FOLD
3478 && oap->op_type != OP_FOLDOPEN
3479 && oap->op_type != OP_FOLDOPENREC
3480 && oap->op_type != OP_FOLDCLOSE
3481 && oap->op_type != OP_FOLDCLOSEREC
3482 && oap->op_type != OP_FOLDDEL
3483 && oap->op_type != OP_FOLDDELREC
3484#endif
3485 )
3486 {
3487 prep_redo(oap->regname, cap->count0,
3488 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3489 oap->motion_force, cap->cmdchar, cap->nchar);
3490 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3491 {
3492 // If 'cpoptions' does not contain 'r', insert the search
3493 // pattern to really repeat the same command.
3494 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3495 AppendToRedobuffLit(cap->searchbuf, -1);
3496 AppendToRedobuff(NL_STR);
3497 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003498 else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003499 {
3500 // do_cmdline() has stored the first typed line in
3501 // "repeat_cmdline". When several lines are typed repeating
3502 // won't be possible.
3503 if (repeat_cmdline == NULL)
3504 ResetRedobuff();
3505 else
3506 {
3507 AppendToRedobuffLit(repeat_cmdline, -1);
3508 AppendToRedobuff(NL_STR);
3509 VIM_CLEAR(repeat_cmdline);
3510 }
3511 }
3512 }
3513
3514 if (redo_VIsual_busy)
3515 {
3516 // Redo of an operation on a Visual area. Use the same size from
3517 // redo_VIsual_line_count and redo_VIsual_vcol.
3518 oap->start = curwin->w_cursor;
3519 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3520 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3521 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3522 VIsual_mode = redo_VIsual_mode;
3523 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3524 {
3525 if (VIsual_mode == 'v')
3526 {
3527 if (redo_VIsual_line_count <= 1)
3528 {
3529 validate_virtcol();
3530 curwin->w_curswant =
3531 curwin->w_virtcol + redo_VIsual_vcol - 1;
3532 }
3533 else
3534 curwin->w_curswant = redo_VIsual_vcol;
3535 }
3536 else
3537 {
3538 curwin->w_curswant = MAXCOL;
3539 }
3540 coladvance(curwin->w_curswant);
3541 }
3542 cap->count0 = redo_VIsual_count;
3543 if (redo_VIsual_count != 0)
3544 cap->count1 = redo_VIsual_count;
3545 else
3546 cap->count1 = 1;
3547 }
3548 else if (VIsual_active)
3549 {
3550 if (!gui_yank)
3551 {
3552 // Save the current VIsual area for '< and '> marks, and "gv"
3553 curbuf->b_visual.vi_start = VIsual;
3554 curbuf->b_visual.vi_end = curwin->w_cursor;
3555 curbuf->b_visual.vi_mode = VIsual_mode;
3556 restore_visual_mode();
3557 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3558# ifdef FEAT_EVAL
3559 curbuf->b_visual_mode_eval = VIsual_mode;
3560# endif
3561 }
3562
3563 // In Select mode, a linewise selection is operated upon like a
3564 // characterwise selection.
3565 // Special case: gH<Del> deletes the last line.
3566 if (VIsual_select && VIsual_mode == 'V'
3567 && cap->oap->op_type != OP_DELETE)
3568 {
3569 if (LT_POS(VIsual, curwin->w_cursor))
3570 {
3571 VIsual.col = 0;
3572 curwin->w_cursor.col =
3573 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3574 }
3575 else
3576 {
3577 curwin->w_cursor.col = 0;
3578 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3579 }
3580 VIsual_mode = 'v';
3581 }
3582 // If 'selection' is "exclusive", backup one character for
3583 // charwise selections.
3584 else if (VIsual_mode == 'v')
3585 include_line_break = unadjust_for_sel();
3586
3587 oap->start = VIsual;
3588 if (VIsual_mode == 'V')
3589 {
3590 oap->start.col = 0;
3591 oap->start.coladd = 0;
3592 }
3593 }
3594
3595 // Set oap->start to the first position of the operated text, oap->end
3596 // to the end of the operated text. w_cursor is equal to oap->start.
3597 if (LT_POS(oap->start, curwin->w_cursor))
3598 {
3599#ifdef FEAT_FOLDING
3600 // Include folded lines completely.
3601 if (!VIsual_active)
3602 {
3603 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3604 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003605 if ((curwin->w_cursor.col > 0 || oap->inclusive
3606 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003607 && hasFolding(curwin->w_cursor.lnum, NULL,
3608 &curwin->w_cursor.lnum))
3609 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3610 }
3611#endif
3612 oap->end = curwin->w_cursor;
3613 curwin->w_cursor = oap->start;
3614
3615 // w_virtcol may have been updated; if the cursor goes back to its
3616 // previous position w_virtcol becomes invalid and isn't updated
3617 // automatically.
3618 curwin->w_valid &= ~VALID_VIRTCOL;
3619 }
3620 else
3621 {
3622#ifdef FEAT_FOLDING
3623 // Include folded lines completely.
3624 if (!VIsual_active && oap->motion_type == MLINE)
3625 {
3626 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3627 NULL))
3628 curwin->w_cursor.col = 0;
3629 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3630 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3631 }
3632#endif
3633 oap->end = oap->start;
3634 oap->start = curwin->w_cursor;
3635 }
3636
3637 // Just in case lines were deleted that make the position invalid.
3638 check_pos(curwin->w_buffer, &oap->end);
3639 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3640
3641 // Set "virtual_op" before resetting VIsual_active.
3642 virtual_op = virtual_active();
3643
3644 if (VIsual_active || redo_VIsual_busy)
3645 {
3646 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3647
3648 if (!redo_VIsual_busy && !gui_yank)
3649 {
3650 // Prepare to reselect and redo Visual: this is based on the
3651 // size of the Visual text
3652 resel_VIsual_mode = VIsual_mode;
3653 if (curwin->w_curswant == MAXCOL)
3654 resel_VIsual_vcol = MAXCOL;
3655 else
3656 {
3657 if (VIsual_mode != Ctrl_V)
3658 getvvcol(curwin, &(oap->end),
3659 NULL, NULL, &oap->end_vcol);
3660 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3661 {
3662 if (VIsual_mode != Ctrl_V)
3663 getvvcol(curwin, &(oap->start),
3664 &oap->start_vcol, NULL, NULL);
3665 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3666 }
3667 else
3668 resel_VIsual_vcol = oap->end_vcol;
3669 }
3670 resel_VIsual_line_count = oap->line_count;
3671 }
3672
3673 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3674 if ((redo_yank || oap->op_type != OP_YANK)
3675 && oap->op_type != OP_COLON
3676#ifdef FEAT_FOLDING
3677 && oap->op_type != OP_FOLD
3678 && oap->op_type != OP_FOLDOPEN
3679 && oap->op_type != OP_FOLDOPENREC
3680 && oap->op_type != OP_FOLDCLOSE
3681 && oap->op_type != OP_FOLDCLOSEREC
3682 && oap->op_type != OP_FOLDDEL
3683 && oap->op_type != OP_FOLDDELREC
3684#endif
3685 && oap->motion_force == NUL
3686 )
3687 {
3688 // Prepare for redoing. Only use the nchar field for "r",
3689 // otherwise it might be the second char of the operator.
3690 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3691 || cap->nchar == 'N'))
3692 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003693 get_op_char(oap->op_type),
3694 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003695 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003696 else if (cap->cmdchar != ':' && cap->cmdchar != K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003697 {
3698 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3699
3700 // reverse what nv_replace() did
3701 if (nchar == REPLACE_CR_NCHAR)
3702 nchar = CAR;
3703 else if (nchar == REPLACE_NL_NCHAR)
3704 nchar = NL;
3705 prep_redo(oap->regname, 0L, NUL, 'v',
3706 get_op_char(oap->op_type),
3707 get_extra_op_char(oap->op_type),
3708 nchar);
3709 }
3710 if (!redo_VIsual_busy)
3711 {
3712 redo_VIsual_mode = resel_VIsual_mode;
3713 redo_VIsual_vcol = resel_VIsual_vcol;
3714 redo_VIsual_line_count = resel_VIsual_line_count;
3715 redo_VIsual_count = cap->count0;
3716 redo_VIsual_arg = cap->arg;
3717 }
3718 }
3719
3720 // oap->inclusive defaults to TRUE.
3721 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3722 // FALSE. This makes "d}P" and "v}dP" work the same.
3723 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3724 oap->inclusive = TRUE;
3725 if (VIsual_mode == 'V')
3726 oap->motion_type = MLINE;
3727 else
3728 {
3729 oap->motion_type = MCHAR;
3730 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3731 && (include_line_break || !virtual_op))
3732 {
3733 oap->inclusive = FALSE;
3734 // Try to include the newline, unless it's an operator
3735 // that works on lines only.
3736 if (*p_sel != 'o'
3737 && !op_on_lines(oap->op_type)
3738 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3739 {
3740 ++oap->end.lnum;
3741 oap->end.col = 0;
3742 oap->end.coladd = 0;
3743 ++oap->line_count;
3744 }
3745 }
3746 }
3747
3748 redo_VIsual_busy = FALSE;
3749
3750 // Switch Visual off now, so screen updating does
3751 // not show inverted text when the screen is redrawn.
3752 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3753 // no screen redraw, so it is done here to remove the inverted
3754 // part.
3755 if (!gui_yank)
3756 {
3757 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003758 setmouse();
3759 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003760 may_clear_cmdline();
3761 if ((oap->op_type == OP_YANK
3762 || oap->op_type == OP_COLON
3763 || oap->op_type == OP_FUNCTION
3764 || oap->op_type == OP_FILTER)
3765 && oap->motion_force == NUL)
3766 {
3767#ifdef FEAT_LINEBREAK
3768 // make sure redrawing is correct
3769 curwin->w_p_lbr = lbr_saved;
3770#endif
3771 redraw_curbuf_later(INVERTED);
3772 }
3773 }
3774 }
3775
3776 // Include the trailing byte of a multi-byte char.
3777 if (has_mbyte && oap->inclusive)
3778 {
3779 int l;
3780
3781 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3782 if (l > 1)
3783 oap->end.col += l - 1;
3784 }
3785 curwin->w_set_curswant = TRUE;
3786
3787 // oap->empty is set when start and end are the same. The inclusive
3788 // flag affects this too, unless yanking and the end is on a NUL.
3789 oap->empty = (oap->motion_type == MCHAR
3790 && (!oap->inclusive
3791 || (oap->op_type == OP_YANK
3792 && gchar_pos(&oap->end) == NUL))
3793 && EQUAL_POS(oap->start, oap->end)
3794 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3795 // For delete, change and yank, it's an error to operate on an
3796 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3797 empty_region_error = (oap->empty
3798 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3799
3800 // Force a redraw when operating on an empty Visual region, when
3801 // 'modifiable is off or creating a fold.
3802 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3803#ifdef FEAT_FOLDING
3804 || oap->op_type == OP_FOLD
3805#endif
3806 ))
3807 {
3808#ifdef FEAT_LINEBREAK
3809 curwin->w_p_lbr = lbr_saved;
3810#endif
3811 redraw_curbuf_later(INVERTED);
3812 }
3813
3814 // If the end of an operator is in column one while oap->motion_type
3815 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3816 // character in the previous line. If op_start is on or before the
3817 // first non-blank in the line, the operator becomes linewise
3818 // (strange, but that's the way vi does it).
3819 if ( oap->motion_type == MCHAR
3820 && oap->inclusive == FALSE
3821 && !(cap->retval & CA_NO_ADJ_OP_END)
3822 && oap->end.col == 0
3823 && (!oap->is_VIsual || *p_sel == 'o')
3824 && !oap->block_mode
3825 && oap->line_count > 1)
3826 {
3827 oap->end_adjusted = TRUE; // remember that we did this
3828 --oap->line_count;
3829 --oap->end.lnum;
3830 if (inindent(0))
3831 oap->motion_type = MLINE;
3832 else
3833 {
3834 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3835 if (oap->end.col)
3836 {
3837 --oap->end.col;
3838 oap->inclusive = TRUE;
3839 }
3840 }
3841 }
3842 else
3843 oap->end_adjusted = FALSE;
3844
3845 switch (oap->op_type)
3846 {
3847 case OP_LSHIFT:
3848 case OP_RSHIFT:
3849 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3850 auto_format(FALSE, TRUE);
3851 break;
3852
3853 case OP_JOIN_NS:
3854 case OP_JOIN:
3855 if (oap->line_count < 2)
3856 oap->line_count = 2;
3857 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3858 curbuf->b_ml.ml_line_count)
3859 beep_flush();
3860 else
3861 {
3862 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3863 TRUE, TRUE, TRUE);
3864 auto_format(FALSE, TRUE);
3865 }
3866 break;
3867
3868 case OP_DELETE:
3869 VIsual_reselect = FALSE; // don't reselect now
3870 if (empty_region_error)
3871 {
3872 vim_beep(BO_OPER);
3873 CancelRedo();
3874 }
3875 else
3876 {
3877 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01003878 // save cursor line for undo if it wasn't saved yet
3879 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3880 && u_save_cursor() == OK)
3881 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003882 }
3883 break;
3884
3885 case OP_YANK:
3886 if (empty_region_error)
3887 {
3888 if (!gui_yank)
3889 {
3890 vim_beep(BO_OPER);
3891 CancelRedo();
3892 }
3893 }
3894 else
3895 {
3896#ifdef FEAT_LINEBREAK
3897 curwin->w_p_lbr = lbr_saved;
3898#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02003899 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003900 (void)op_yank(oap, FALSE, !gui_yank);
3901 }
3902 check_cursor_col();
3903 break;
3904
3905 case OP_CHANGE:
3906 VIsual_reselect = FALSE; // don't reselect now
3907 if (empty_region_error)
3908 {
3909 vim_beep(BO_OPER);
3910 CancelRedo();
3911 }
3912 else
3913 {
3914 // This is a new edit command, not a restart. Need to
3915 // remember it to make 'insertmode' work with mappings for
3916 // Visual mode. But do this only once and not when typed and
3917 // 'insertmode' isn't set.
3918 if (p_im || !KeyTyped)
3919 restart_edit_save = restart_edit;
3920 else
3921 restart_edit_save = 0;
3922 restart_edit = 0;
3923#ifdef FEAT_LINEBREAK
3924 // Restore linebreak, so that when the user edits it looks as
3925 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01003926 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003927#endif
3928 // Reset finish_op now, don't want it set inside edit().
3929 finish_op = FALSE;
3930 if (op_change(oap)) // will call edit()
3931 cap->retval |= CA_COMMAND_BUSY;
3932 if (restart_edit == 0)
3933 restart_edit = restart_edit_save;
3934 }
3935 break;
3936
3937 case OP_FILTER:
3938 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3939 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
3940 else
3941 bangredo = TRUE; // do_bang() will put cmd in redo buffer
3942 // FALLTHROUGH
3943
3944 case OP_INDENT:
3945 case OP_COLON:
3946
3947#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3948 // If 'equalprg' is empty, do the indenting internally.
3949 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3950 {
3951# ifdef FEAT_LISP
3952 if (curbuf->b_p_lisp)
3953 {
3954 op_reindent(oap, get_lisp_indent);
3955 break;
3956 }
3957# endif
3958# ifdef FEAT_CINDENT
3959 op_reindent(oap,
3960# ifdef FEAT_EVAL
3961 *curbuf->b_p_inde != NUL ? get_expr_indent :
3962# endif
3963 get_c_indent);
3964 break;
3965# endif
3966 }
3967#endif
3968
3969 op_colon(oap);
3970 break;
3971
3972 case OP_TILDE:
3973 case OP_UPPER:
3974 case OP_LOWER:
3975 case OP_ROT13:
3976 if (empty_region_error)
3977 {
3978 vim_beep(BO_OPER);
3979 CancelRedo();
3980 }
3981 else
3982 op_tilde(oap);
3983 check_cursor_col();
3984 break;
3985
3986 case OP_FORMAT:
3987#if defined(FEAT_EVAL)
3988 if (*curbuf->b_p_fex != NUL)
3989 op_formatexpr(oap); // use expression
3990 else
3991#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003992 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003993 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003994 op_colon(oap); // use external command
3995 else
3996 op_format(oap, FALSE); // use internal function
3997 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003998 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003999 case OP_FORMAT2:
4000 op_format(oap, TRUE); // use internal function
4001 break;
4002
4003 case OP_FUNCTION:
4004#ifdef FEAT_LINEBREAK
4005 // Restore linebreak, so that when the user edits it looks as
4006 // before.
4007 curwin->w_p_lbr = lbr_saved;
4008#endif
4009 op_function(oap); // call 'operatorfunc'
4010 break;
4011
4012 case OP_INSERT:
4013 case OP_APPEND:
4014 VIsual_reselect = FALSE; // don't reselect now
4015 if (empty_region_error)
4016 {
4017 vim_beep(BO_OPER);
4018 CancelRedo();
4019 }
4020 else
4021 {
4022 // This is a new edit command, not a restart. Need to
4023 // remember it to make 'insertmode' work with mappings for
4024 // Visual mode. But do this only once.
4025 restart_edit_save = restart_edit;
4026 restart_edit = 0;
4027#ifdef FEAT_LINEBREAK
4028 // Restore linebreak, so that when the user edits it looks as
4029 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004030 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004031#endif
4032 op_insert(oap, cap->count1);
4033#ifdef FEAT_LINEBREAK
4034 // Reset linebreak, so that formatting works correctly.
4035 curwin->w_p_lbr = FALSE;
4036#endif
4037
4038 // TODO: when inserting in several lines, should format all
4039 // the lines.
4040 auto_format(FALSE, TRUE);
4041
4042 if (restart_edit == 0)
4043 restart_edit = restart_edit_save;
4044 else
4045 cap->retval |= CA_COMMAND_BUSY;
4046 }
4047 break;
4048
4049 case OP_REPLACE:
4050 VIsual_reselect = FALSE; // don't reselect now
4051 if (empty_region_error)
4052 {
4053 vim_beep(BO_OPER);
4054 CancelRedo();
4055 }
4056 else
4057 {
4058#ifdef FEAT_LINEBREAK
4059 // Restore linebreak, so that when the user edits it looks as
4060 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004061 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004062#endif
4063 op_replace(oap, cap->nchar);
4064 }
4065 break;
4066
4067#ifdef FEAT_FOLDING
4068 case OP_FOLD:
4069 VIsual_reselect = FALSE; // don't reselect now
4070 foldCreate(oap->start.lnum, oap->end.lnum);
4071 break;
4072
4073 case OP_FOLDOPEN:
4074 case OP_FOLDOPENREC:
4075 case OP_FOLDCLOSE:
4076 case OP_FOLDCLOSEREC:
4077 VIsual_reselect = FALSE; // don't reselect now
4078 opFoldRange(oap->start.lnum, oap->end.lnum,
4079 oap->op_type == OP_FOLDOPEN
4080 || oap->op_type == OP_FOLDOPENREC,
4081 oap->op_type == OP_FOLDOPENREC
4082 || oap->op_type == OP_FOLDCLOSEREC,
4083 oap->is_VIsual);
4084 break;
4085
4086 case OP_FOLDDEL:
4087 case OP_FOLDDELREC:
4088 VIsual_reselect = FALSE; // don't reselect now
4089 deleteFold(oap->start.lnum, oap->end.lnum,
4090 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4091 break;
4092#endif
4093 case OP_NR_ADD:
4094 case OP_NR_SUB:
4095 if (empty_region_error)
4096 {
4097 vim_beep(BO_OPER);
4098 CancelRedo();
4099 }
4100 else
4101 {
4102 VIsual_active = TRUE;
4103#ifdef FEAT_LINEBREAK
4104 curwin->w_p_lbr = lbr_saved;
4105#endif
4106 op_addsub(oap, cap->count1, redo_VIsual_arg);
4107 VIsual_active = FALSE;
4108 }
4109 check_cursor_col();
4110 break;
4111 default:
4112 clearopbeep(oap);
4113 }
4114 virtual_op = MAYBE;
4115 if (!gui_yank)
4116 {
4117 // if 'sol' not set, go back to old column for some commands
4118 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4119 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4120 || oap->op_type == OP_DELETE))
4121 {
4122#ifdef FEAT_LINEBREAK
4123 curwin->w_p_lbr = FALSE;
4124#endif
4125 coladvance(curwin->w_curswant = old_col);
4126 }
4127 }
4128 else
4129 {
4130 curwin->w_cursor = old_cursor;
4131 }
4132 oap->block_mode = FALSE;
4133 clearop(oap);
4134 motion_force = NUL;
4135 }
4136#ifdef FEAT_LINEBREAK
4137 curwin->w_p_lbr = lbr_saved;
4138#endif
4139}