blob: e4590a1e75720d31fbe50ddddd77968dc29dfd2e [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;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 if (opchars[i][0] == char1 && opchars[i][1] == char2)
84 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +010085 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
86 {
87 internal_error("get_op_type()");
88 break;
89 }
90 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 return i;
92}
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094/*
95 * Return TRUE if operator "op" always works on whole lines.
96 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020097 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010098op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200100 return opchars[op][2] & OPF_LINES;
101}
102
Bram Moolenaar113e1072019-01-20 15:30:40 +0100103#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200104/*
105 * Return TRUE if operator "op" changes text.
106 */
107 int
108op_is_change(int op)
109{
110 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
114/*
115 * Get first operator command character.
116 * Returns 'g' or 'z' if there is another command character.
117 */
118 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100119get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120{
121 return opchars[optype][0];
122}
123
124/*
125 * Get second operator command character.
126 */
127 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100128get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129{
130 return opchars[optype][1];
131}
132
133/*
134 * op_shift - handle a shift operation
135 */
136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100137op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138{
139 long i;
140 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143 if (u_save((linenr_T)(oap->start.lnum - 1),
144 (linenr_T)(oap->end.lnum + 1)) == FAIL)
145 return;
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (oap->block_mode)
148 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150 for (i = oap->line_count; --i >= 0; )
151 {
152 first_char = *ml_get_curline();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100153 if (first_char == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 else if (oap->block_mode)
156 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100158 // Move the line right if it doesn't start with '#', 'smartindent'
159 // isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
161 if (first_char != '#' || !preprocs_left())
162#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000163 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 ++curwin->w_cursor.lnum;
165 }
166
167 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (oap->block_mode)
169 {
170 curwin->w_cursor.lnum = oap->start.lnum;
171 curwin->w_cursor.col = block_col;
172 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100173 else if (curs_top) // put cursor on first line, for ">>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 {
175 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100176 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 }
178 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100179 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100181#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100182 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183 foldOpenCursor();
184#endif
185
186
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (oap->line_count > p_report)
188 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200189 char *op;
190 char *msg_line_single;
191 char *msg_line_plural;
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200194 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200196 op = "<";
197 msg_line_single = NGETTEXT("%ld line %sed %d time",
198 "%ld line %sed %d times", amount);
199 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
200 "%ld lines %sed %d times", amount);
201 vim_snprintf((char *)IObuff, IOSIZE,
202 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
203 oap->line_count, op, amount);
Bram Moolenaare4fc7462020-03-15 19:17:50 +0100204 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 }
206
Bram Moolenaare1004402020-10-24 20:49:43 +0200207 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100208 {
209 // Set "'[" and "']" marks.
210 curbuf->b_op_start = oap->start;
211 curbuf->b_op_end.lnum = oap->end.lnum;
212 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
213 if (curbuf->b_op_end.col > 0)
214 --curbuf->b_op_end.col;
215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216}
217
218/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100219 * Shift the current line one shiftwidth left (if left != 0) or right
220 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 */
222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100223shift_line(
224 int left,
225 int round,
226 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200227 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 int count;
230 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200231 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200233 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200235 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200237 i = count / sw_val; // number of 'shiftwidth' rounded down
238 j = count % sw_val; // extra spaces
239 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 --amount;
241 if (left)
242 {
243 i -= amount;
244 if (i < 0)
245 i = 0;
246 }
247 else
248 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200249 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200251 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {
253 if (left)
254 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200255 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 if (count < 0)
257 count = 0;
258 }
259 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200260 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200263 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000265 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000267 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268}
269
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270/*
271 * Shift one line of the current block one shiftwidth right or left.
272 * Leaves cursor on first character in block.
273 */
274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100275shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
277 int left = (oap->op_type == OP_LSHIFT);
278 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000279 int total;
280 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200282 int sw_val = (int)get_sw_value_indent(curbuf);
283 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000286 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 int i = 0, j = 0;
288 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifdef FEAT_RIGHTLEFT
290 int old_p_ri = p_ri;
291
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100292 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293#endif
294
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100295 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
297 if (bd.is_short)
298 return;
299
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100300 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 total = (int)((unsigned)amount * (unsigned)sw_val);
302 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100303 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200304
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 oldp = ml_get_curline();
306
307 if (!left)
308 {
309 /*
310 * 1. Get start vcol
311 * 2. Total ws vcols
312 * 3. Divvy into TABs & spp
313 * 4. Construct new string
314 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100315 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 ws_vcol = bd.start_vcol - bd.pre_whitesp;
317 if (bd.startspaces)
318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100320 {
321 if ((*mb_ptr2len)(bd.textstart) == 1)
322 ++bd.textstart;
323 else
324 {
325 ws_vcol = 0;
326 bd.startspaces = 0;
327 }
328 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000329 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000330 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100332 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100334 // TODO: is passing bd.textstart for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200335 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
336 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 total += incr;
338 bd.start_vcol += incr;
339 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100340 // OK, now total=all the VWS reqd, and textstart points at the 1st
341 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200342#ifdef FEAT_VARTABS
343 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200344 tabstop_fromto(ws_vcol, ws_vcol + total,
345 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200346 else
347 j = total;
348#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (!curbuf->b_p_et)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100350 i = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 if (i)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100352 j = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 else
354 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200355#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100356 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
358 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200359 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 if (newp == NULL)
361 return;
362 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
363 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200364 vim_memset(newp + bd.textcol, TAB, (size_t)i);
365 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100366 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
368 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100369 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100371 colnr_T destination_col; // column to which text in block will
372 // be shifted
373 char_u *verbatim_copy_end; // end of the part of the line which is
374 // copied verbatim
375 colnr_T verbatim_copy_width;// the (displayed) width of this part
376 // of line
377 unsigned fill; // nr of spaces that replace a TAB
378 unsigned new_line_len; // the length of the line after the
379 // block shift
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000380 size_t block_space_width;
381 size_t shift_amount;
382 char_u *non_white = bd.textstart;
383 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000385 /*
386 * Firstly, let's find the first non-whitespace character that is
387 * displayed after the block's start column and the character's column
388 * number. Also, let's calculate the width of all the whitespace
389 * characters that are displayed in the block and precede the searched
390 * non-whitespace character.
391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100393 // If "bd.startspaces" is set, "bd.textstart" points to the character,
394 // the part of which is displayed at the block's beginning. Let's start
395 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000396 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100397 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100399 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000400 non_white_col = bd.start_vcol;
401
Bram Moolenaar1c465442017-03-12 20:10:05 +0100402 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000403 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200404 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000405 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000408 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100409 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000410 shift_amount = (block_space_width < (size_t)total
411 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000414 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100416 // Now let's find out how much of the beginning of the line we can
417 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000418 verbatim_copy_end = bd.textstart;
419 verbatim_copy_width = bd.start_vcol;
420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100421 // If "bd.startspaces" is set, "bd.textstart" points to the character
422 // preceding the block. We have to subtract its width to obtain its
423 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000424 if (bd.startspaces)
425 verbatim_copy_width -= bd.start_char_vcols;
426 while (verbatim_copy_width < destination_col)
427 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200428 char_u *line = verbatim_copy_end;
429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100430 // TODO: is passing verbatim_copy_end for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200431 incr = lbr_chartabsize(line, verbatim_copy_end,
432 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000433 if (verbatim_copy_width + incr > destination_col)
434 break;
435 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100436 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000437 }
438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100439 // If "destination_col" is different from the width of the initial
440 // part of the line that will be copied, it means we encountered a tab
441 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000442 fill = destination_col - verbatim_copy_width;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // The replacement line will consist of:
445 // - the beginning of the original line up to "verbatim_copy_end",
446 // - "fill" number of spaces,
447 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000448 new_line_len = (unsigned)(verbatim_copy_end - oldp)
449 + fill
450 + (unsigned)STRLEN(non_white) + 1;
451
Bram Moolenaar964b3742019-05-24 18:54:09 +0200452 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (newp == NULL)
454 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000455 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200456 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100459 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
461 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
462 State = oldstate;
463 curwin->w_cursor.col = oldcol;
464#ifdef FEAT_RIGHTLEFT
465 p_ri = old_p_ri;
466#endif
467}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
470 * Insert string "s" (b_insert ? before : after) block :AKelly
471 * Caller must prepare for undo.
472 */
473 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100474block_insert(
475 oparg_T *oap,
476 char_u *s,
477 int b_insert,
478 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
Bram Moolenaardac13472019-09-16 21:06:21 +0200480 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100481 int count = 0; // extra spaces to replace a cut TAB
482 int spaces = 0; // non-zero if cutting a TAB
483 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200484 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100485 unsigned s_len; // STRLEN(s)
486 char_u *newp, *oldp; // new, old lines
487 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 int oldstate = State;
489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 s_len = (unsigned)STRLEN(s);
492
493 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
494 {
495 block_prep(oap, bdp, lnum, TRUE);
496 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100497 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
499 oldp = ml_get(lnum);
500
501 if (b_insert)
502 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200503 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 spaces = bdp->startspaces;
505 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100506 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 offset = bdp->textcol;
508 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100509 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200511 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100512 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200514 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100516 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 offset = bdp->textcol + bdp->textlen - (spaces != 0);
518 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100519 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (!bdp->is_MAX)
523 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
524 count = spaces;
525 offset = bdp->textcol + bdp->textlen;
526 }
527 }
528
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200529 if (has_mbyte && spaces > 0)
530 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100531 int off;
532
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 // Avoid starting halfway a multi-byte character.
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200534 if (b_insert)
535 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100536 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200537 }
538 else
539 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100540 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200541 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200542 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100543 spaces -= off;
544 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200545 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200546
Bram Moolenaar964b3742019-05-24 18:54:09 +0200547 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 if (newp == NULL)
549 continue;
550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100551 // copy up to shifted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 mch_memmove(newp, oldp, (size_t)(offset));
553 oldp += offset;
554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100555 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200556 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200557 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200560 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 offset += s_len;
562
563 if (spaces && !bdp->is_short)
564 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100565 // insert post-padding
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 // We're splitting a TAB, don't copy it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 oldp++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 // We allowed for that TAB, remember this now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 count++;
571 }
572
573 if (spaces > 0)
574 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000575 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577 ml_replace(lnum, newp, FALSE);
578
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200579 if (b_insert)
580 // correct any text properties
581 inserted_bytes(lnum, startcol, s_len);
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (lnum == oap->end.lnum)
584 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100585 // Set "']" mark to the end of the block instead of the end of
586 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 curbuf->b_op_end.lnum = oap->end.lnum;
588 curbuf->b_op_end.col = offset;
589 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100590 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
593
594 State = oldstate;
595}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200598 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200600 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 */
602 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100603op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 int n;
606 linenr_T lnum;
607 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 char_u *newp, *oldp;
609 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
611 int did_yank = FALSE;
612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 return OK;
615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100616 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (oap->empty)
618 return u_save_cursor();
619
620 if (!curbuf->b_p_ma)
621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100622 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 return FAIL;
624 }
625
626#ifdef FEAT_CLIPBOARD
627 adjust_clip_reg(&oap->regname);
628#endif
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (has_mbyte)
631 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaard04b7502010-07-08 22:27:55 +0200633 /*
634 * Imitate the strange Vi behaviour: If the delete spans more than one
635 * line and motion_type == MCHAR and the result is a blank line, make the
636 * delete linewise. Don't do this for the change command or Visual mode.
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000640 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100642 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 && oap->op_type == OP_DELETE)
644 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200645 ptr = ml_get(oap->end.lnum) + oap->end.col;
646 if (*ptr != NUL)
647 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 ptr = skipwhite(ptr);
649 if (*ptr == NUL && inindent(0))
650 oap->motion_type = MLINE;
651 }
652
Bram Moolenaard04b7502010-07-08 22:27:55 +0200653 /*
654 * Check for trying to delete (e.g. "D") in an empty line.
655 * Note: For the change operator it is ok.
656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if ( oap->motion_type == MCHAR
658 && oap->line_count == 1
659 && oap->op_type == OP_DELETE
660 && *ml_get(oap->start.lnum) == NUL)
661 {
662 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000663 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 * 'cpoptions' (Vi compatible).
665 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000666 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
668 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000669 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
671 beep_flush();
672 return OK;
673 }
674
Bram Moolenaard04b7502010-07-08 22:27:55 +0200675 /*
676 * Do a yank of whatever we're about to delete.
677 * If a yank register was specified, put the deleted text into that
678 * register. For the black hole register '_' don't yank anything.
679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (oap->regname != '_')
681 {
682 if (oap->regname != 0)
683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (!valid_yank_reg(oap->regname, TRUE))
686 {
687 beep_flush();
688 return OK;
689 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100690 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
691 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 did_yank = TRUE;
693 }
694
695 /*
696 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100697 * delete contains a line break, or when using a specific operator (Vi
698 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200699 * Use the register name from before adjust_clip_reg() may have
700 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100702 if (oap->motion_type == MLINE || oap->line_count > 1
703 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100705 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (op_yank(oap, TRUE, FALSE) == OK)
707 did_yank = TRUE;
708 }
709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100710 // Yank into small delete register when no named register specified
711 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200712 if ((
713#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200714 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
715 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200716#endif
717 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 && oap->line_count == 1)
719 {
720 oap->regname = '-';
721 get_yank_register(oap->regname, TRUE);
722 if (op_yank(oap, TRUE, FALSE) == OK)
723 did_yank = TRUE;
724 oap->regname = 0;
725 }
726
727 /*
728 * If there's too much stuff to fit in the yank register, then get a
729 * confirmation before doing the delete. This is crude, but simple.
730 * And it avoids doing a delete of something we can't put back if we
731 * want.
732 */
733 if (!did_yank)
734 {
735 int msg_silent_save = msg_silent;
736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
739 msg_silent = msg_silent_save;
740 if (n != 'y')
741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 return FAIL;
744 }
745 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100746
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100747#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100748 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200749 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752
Bram Moolenaard04b7502010-07-08 22:27:55 +0200753 /*
754 * block mode delete
755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 if (oap->block_mode)
757 {
758 if (u_save((linenr_T)(oap->start.lnum - 1),
759 (linenr_T)(oap->end.lnum + 1)) == FAIL)
760 return FAIL;
761
762 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
763 {
764 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100765 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 continue;
767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100768 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (lnum == curwin->w_cursor.lnum)
770 {
771 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774
Bram Moolenaar8055d172019-05-17 22:57:26 +0200775 // "n" == number of chars deleted
776 // If we delete a TAB, it may be replaced by several characters.
777 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 n = bd.textlen - bd.startspaces - bd.endspaces;
779 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200780 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (newp == NULL)
782 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200786 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100788 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000790 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200793
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100794#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200795 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200796 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799
800 check_cursor_col();
801 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
802 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100803 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100805 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
807 if (oap->op_type == OP_CHANGE)
808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // Delete the lines except the first one. Temporarily move the
810 // cursor to the next line. Save the current line number, if the
811 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if (oap->line_count > 1)
813 {
814 lnum = curwin->w_cursor.lnum;
815 ++curwin->w_cursor.lnum;
816 del_lines((long)(oap->line_count - 1), TRUE);
817 curwin->w_cursor.lnum = lnum;
818 }
819 if (u_save_cursor() == FAIL)
820 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100821 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100823 beginline(BL_WHITE); // cursor on first non-white
824 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 ai_col = curwin->w_cursor.col;
826 }
827 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 beginline(0); // cursor in column 0
829 truncate_line(FALSE); // delete the rest of the line
830 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100832 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 else
835 {
836 del_lines(oap->line_count, TRUE);
837 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 }
841 else
842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (virtual_op)
844 {
845 int endcol = 0;
846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100847 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (gchar_pos(&oap->start) == '\t')
849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100850 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 return FAIL;
852 if (oap->line_count == 1)
853 endcol = getviscol2(oap->end.col, oap->end.coladd);
854 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
855 oap->start = curwin->w_cursor;
856 if (oap->line_count == 1)
857 {
858 coladvance(endcol);
859 oap->end.col = curwin->w_cursor.col;
860 oap->end.coladd = curwin->w_cursor.coladd;
861 curwin->w_cursor = oap->start;
862 }
863 }
864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (gchar_pos(&oap->end) == '\t'
867 && (int)oap->end.coladd < oap->inclusive)
868 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (u_save((linenr_T)(oap->end.lnum - 1),
871 (linenr_T)(oap->end.lnum + 1)) == FAIL)
872 return FAIL;
873 curwin->w_cursor = oap->end;
874 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
875 oap->end = curwin->w_cursor;
876 curwin->w_cursor = oap->start;
877 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100878 if (has_mbyte)
879 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100882 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100884 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 return FAIL;
886
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100887 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100888 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 && oap->op_type == OP_CHANGE
890 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100891 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 display_dollar(oap->end.col - !oap->inclusive);
893
894 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
895
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (virtual_op)
897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100898 // fix up things for virtualedit-delete:
899 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 char_u *curline = ml_get_curline();
901 int len = (int)STRLEN(curline);
902
903 if (oap->end.coladd != 0
904 && (int)oap->end.col >= len - 1
905 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
906 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100907 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (n == 0 && oap->start.coladd != oap->end.coladd)
909 n = 1;
910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100911 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (gchar_cursor() != NUL)
913 curwin->w_cursor.coladd = 0;
914 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200915 (void)del_bytes((long)n, !virtual_op,
916 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 pos_T curpos;
921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100922 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
924 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
925 return FAIL;
926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100927 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100929 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 ++curwin->w_cursor.lnum;
931 del_lines((long)(oap->line_count - 2), FALSE);
932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100933 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200934 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200935 curwin->w_cursor.col = 0;
936 (void)del_bytes((long)n, !virtual_op,
937 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100938 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200939 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
Bram Moolenaard0a1dee2020-12-20 13:07:48 +0100941 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943
944 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
945
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000946setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200947 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100949 if (oap->block_mode)
950 {
951 curbuf->b_op_end.lnum = oap->end.lnum;
952 curbuf->b_op_end.col = oap->start.col;
953 }
954 else
955 curbuf->b_op_end = oap->start;
956 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
959 return OK;
960}
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962/*
963 * Adjust end of operating area for ending on a multi-byte character.
964 * Used for deletion.
965 */
966 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100967mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
969 char_u *p;
970
971 if (oap->inclusive)
972 {
973 p = ml_get(oap->end.lnum);
974 oap->end.col += mb_tail_off(p, p + oap->end.col);
975 }
976}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar630afe82018-06-28 19:26:28 +0200978/*
979 * Replace the character under the cursor with "c".
980 * This takes care of multi-byte characters.
981 */
982 static void
983replace_character(int c)
984{
985 int n = State;
986
987 State = REPLACE;
988 ins_char(c);
989 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100990 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200991 dec_cursor();
992}
993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994/*
995 * Replace a whole area with one character.
996 */
997 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100998op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
1000 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 char_u *newp, *oldp;
1003 size_t oldlen;
1004 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001005 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001006 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001009 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001011 if (c == REPLACE_CR_NCHAR)
1012 {
1013 had_ctrl_v_cr = TRUE;
1014 c = CAR;
1015 }
1016 else if (c == REPLACE_NL_NCHAR)
1017 {
1018 had_ctrl_v_cr = TRUE;
1019 c = NL;
1020 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (has_mbyte)
1023 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
1025 if (u_save((linenr_T)(oap->start.lnum - 1),
1026 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1027 return FAIL;
1028
1029 /*
1030 * block mode replace
1031 */
1032 if (oap->block_mode)
1033 {
1034 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1035 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001037 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1039 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001040 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001042 // n == number of extra chars required
1043 // If we split a TAB, it may be replaced by several characters.
1044 // Thus the number of characters may increase!
1045 // If the range starts in virtual space, count the initial
1046 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1048 {
1049 pos_T vpos;
1050
Bram Moolenaara1381de2009-11-03 15:44:21 +00001051 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 getvpos(&vpos, oap->start_vcol);
1053 bd.startspaces += vpos.coladd;
1054 n = bd.startspaces;
1055 }
1056 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001057 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001060 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 numc = oap->end_vcol - oap->start_vcol + 1;
1066 if (bd.is_short && (!virtual_op || bd.is_MAX))
1067 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 // A double-wide character can be replaced only up to half the
1070 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if ((*mb_char2cells)(c) > 1)
1072 {
1073 if ((numc & 1) && !bd.is_short)
1074 {
1075 ++bd.endspaces;
1076 ++n;
1077 }
1078 numc = numc / 2;
1079 }
1080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001081 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 num_chars = numc;
1083 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001084 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 n += numc - bd.textlen;
1086
1087 oldp = ml_get_curline();
1088 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001089 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (newp == NULL)
1091 continue;
1092 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001093 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 mch_memmove(newp, oldp, (size_t)bd.textcol);
1095 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001096 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001097 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // insert replacement chars CHECK FOR ALLOCATED SPACE
1099 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1100 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001101 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001103 if (has_mbyte)
1104 {
1105 n = (int)STRLEN(newp);
1106 while (--num_chars >= 0)
1107 n += (*mb_char2bytes)(c, newp + n);
1108 }
1109 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001110 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001111 if (!bd.is_short)
1112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001113 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001114 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001115 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001116 STRMOVE(newp + STRLEN(newp), oldp);
1117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
1119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001121 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001122 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001123 if (after_p != NULL)
1124 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001126 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001128 if (after_p != NULL)
1129 {
1130 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1131 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1132 oap->end.lnum++;
1133 vim_free(after_p);
1134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 }
1137 else
1138 {
1139 /*
1140 * MCHAR and MLINE motion replace.
1141 */
1142 if (oap->motion_type == MLINE)
1143 {
1144 oap->start.col = 0;
1145 curwin->w_cursor.col = 0;
1146 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1147 if (oap->end.col)
1148 --oap->end.col;
1149 }
1150 else if (!oap->inclusive)
1151 dec(&(oap->end));
1152
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001153 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 n = gchar_cursor();
1156 if (n != NUL)
1157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001160 // This is slow, but it handles replacing a single-byte
1161 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001162 if (curwin->w_cursor.lnum == oap->end.lnum)
1163 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001164 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (n == TAB)
1169 {
1170 int end_vcol = 0;
1171
1172 if (curwin->w_cursor.lnum == oap->end.lnum)
1173 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001174 // oap->end has to be recalculated when
1175 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 end_vcol = getviscol2(oap->end.col,
1177 oap->end.coladd);
1178 }
1179 coladvance_force(getviscol());
1180 if (curwin->w_cursor.lnum == oap->end.lnum)
1181 getvpos(&oap->end, end_vcol);
1182 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001183 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1187 {
1188 int virtcols = oap->end.coladd;
1189
1190 if (curwin->w_cursor.lnum == oap->start.lnum
1191 && oap->start.col == oap->end.col && oap->start.coladd)
1192 virtcols -= oap->start.coladd;
1193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001194 // oap->end has been trimmed so it's effectively inclusive;
1195 // as a result an extra +1 must be counted so we don't
1196 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1198 curwin->w_cursor.col -= (virtcols + 1);
1199 for (; virtcols >= 0; virtcols--)
1200 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001201 if ((*mb_char2len)(c) > 1)
1202 replace_character(c);
1203 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001204 PBYTE(curwin->w_cursor, c);
1205 if (inc(&curwin->w_cursor) == -1)
1206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (inc_cursor() == -1)
1212 break;
1213 }
1214 }
1215
1216 curwin->w_cursor = oap->start;
1217 check_cursor();
1218 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1219
Bram Moolenaare1004402020-10-24 20:49:43 +02001220 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001222 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001223 curbuf->b_op_start = oap->start;
1224 curbuf->b_op_end = oap->end;
1225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
1227 return OK;
1228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001230static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1234 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001236op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001240 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242 if (u_save((linenr_T)(oap->start.lnum - 1),
1243 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1244 return;
1245
1246 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001247 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
1249 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1250 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001251 int one_change;
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 block_prep(oap, &bd, pos.lnum, FALSE);
1254 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001255 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1256 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001257
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001258#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001259 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1262
Bram Moolenaar009b2592004-10-24 19:18:58 +00001263 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1264 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001266 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 if (did_change)
1271 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1272 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001273 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 {
1275 if (oap->motion_type == MLINE)
1276 {
1277 oap->start.col = 0;
1278 pos.col = 0;
1279 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1280 if (oap->end.col)
1281 --oap->end.col;
1282 }
1283 else if (!oap->inclusive)
1284 dec(&(oap->end));
1285
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001286 if (pos.lnum == oap->end.lnum)
1287 did_change = swapchars(oap->op_type, &pos,
1288 oap->end.col - pos.col + 1);
1289 else
1290 for (;;)
1291 {
1292 did_change |= swapchars(oap->op_type, &pos,
1293 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1294 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001295 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001296 break;
1297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (did_change)
1299 {
1300 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1301 0L);
1302#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001303 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
1305 char_u *ptr;
1306 int count;
1307
1308 pos = oap->start;
1309 while (pos.lnum < oap->end.lnum)
1310 {
1311 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001312 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001313 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001315 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 pos.col = 0;
1317 pos.lnum++;
1318 }
1319 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1320 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001321 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001323 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325#endif
1326 }
1327 }
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001330 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
Bram Moolenaare1004402020-10-24 20:49:43 +02001333 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001334 {
1335 // Set '[ and '] marks.
1336 curbuf->b_op_start = oap->start;
1337 curbuf->b_op_end = oap->end;
1338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001341 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001342 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343}
1344
1345/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001346 * Invoke swapchar() on "length" bytes at position "pos".
1347 * "pos" is advanced to just after the changed characters.
1348 * "length" is rounded up to include the whole last multi-byte character.
1349 * Also works correctly when the number of bytes changes.
1350 * Returns TRUE if some character was changed.
1351 */
1352 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001353swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001354{
1355 int todo;
1356 int did_change = 0;
1357
1358 for (todo = length; todo > 0; --todo)
1359 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001360 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001361 {
1362 int len = (*mb_ptr2len)(ml_get_pos(pos));
1363
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001364 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001365 if (len > 0)
1366 todo -= len - 1;
1367 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001368 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001369 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001370 break;
1371 }
1372 return did_change;
1373}
1374
1375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * If op_type == OP_UPPER: make uppercase,
1377 * if op_type == OP_LOWER: make lowercase,
1378 * if op_type == OP_ROT13: do rot13 encoding,
1379 * else swap case of character at 'pos'
1380 * returns TRUE when something actually changed.
1381 */
1382 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001383swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 int c;
1386 int nc;
1387
1388 c = gchar_pos(pos);
1389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (c >= 0x80 && op_type == OP_ROT13)
1392 return FALSE;
1393
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001394 if (op_type == OP_UPPER && c == 0xdf
1395 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001396 {
1397 pos_T sp = curwin->w_cursor;
1398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001399 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001400 curwin->w_cursor = *pos;
1401 del_char(FALSE);
1402 ins_char('S');
1403 ins_char('S');
1404 curwin->w_cursor = sp;
1405 inc(pos);
1406 }
1407
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001408 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 nc = c;
1411 if (MB_ISLOWER(c))
1412 {
1413 if (op_type == OP_ROT13)
1414 nc = ROT13(c, 'a');
1415 else if (op_type != OP_LOWER)
1416 nc = MB_TOUPPER(c);
1417 }
1418 else if (MB_ISUPPER(c))
1419 {
1420 if (op_type == OP_ROT13)
1421 nc = ROT13(c, 'A');
1422 else if (op_type != OP_UPPER)
1423 nc = MB_TOLOWER(c);
1424 }
1425 if (nc != c)
1426 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1428 {
1429 pos_T sp = curwin->w_cursor;
1430
1431 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001432 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001433 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 ins_char(nc);
1435 curwin->w_cursor = sp;
1436 }
1437 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001438 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 return TRUE;
1440 }
1441 return FALSE;
1442}
1443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444/*
1445 * op_insert - Insert and append operators for Visual mode.
1446 */
1447 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001448op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 long ins_len, pre_textlen = 0;
1451 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001452 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 struct block_def bd;
1454 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001455 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001457 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001460 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 curwin->w_cursor.lnum = oap->start.lnum;
1462 update_screen(INVERTED);
1463
1464 if (oap->block_mode)
1465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001466 // When 'virtualedit' is used, need to insert the extra spaces before
1467 // doing block_prep(). When only "block" is used, virtual edit is
1468 // already disabled, but still need it when calling
1469 // coladvance_force().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (curwin->w_cursor.coladd > 0)
1471 {
1472 int old_ve_flags = ve_flags;
1473
1474 ve_flags = VE_ALL;
1475 if (u_save_cursor() == FAIL)
1476 return;
1477 coladvance_force(oap->op_type == OP_APPEND
1478 ? oap->end_vcol + 1 : getviscol());
1479 if (oap->op_type == OP_APPEND)
1480 --curwin->w_cursor.col;
1481 ve_flags = old_ve_flags;
1482 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001483 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001485 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001486 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (oap->op_type == OP_APPEND)
1490 firstline += bd.textlen;
1491 pre_textlen = (long)STRLEN(firstline);
1492 }
1493
1494 if (oap->op_type == OP_APPEND)
1495 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001496 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 curwin->w_set_curswant = TRUE;
1500 while (*ml_get_cursor() != NUL
1501 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1502 ++curwin->w_cursor.col;
1503 if (bd.is_short && !bd.is_MAX)
1504 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001505 // First line was too short, make it longer and adjust the
1506 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (u_save_cursor() == FAIL)
1508 return;
1509 for (i = 0; i < bd.endspaces; ++i)
1510 ins_char(' ');
1511 bd.textlen += bd.endspaces;
1512 }
1513 }
1514 else
1515 {
1516 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001517 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001519 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001520 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 && oap->start_vcol != oap->end_vcol)
1522 inc_cursor();
1523 }
1524 }
1525
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001526 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001527 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 // When a tab was inserted, and the characters in front of the tab
1530 // have been converted to a tab as well, the column of the cursor
1531 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001532 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001533 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001534 oap->start = curbuf->b_op_start_orig;
1535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001536 // If user has moved off this line, we don't know what to do, so do
1537 // nothing.
1538 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001539 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 return;
1541
1542 if (oap->block_mode)
1543 {
1544 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001545 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001546 size_t len;
1547 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001549 // If indent kicked in, the firstline might have changed
1550 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001551 ind_post = (colnr_T)getwhitecols_curline();
1552 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1553 {
1554 bd.textcol += ind_post - ind_pre;
1555 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001556 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001557 }
1558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001559 // The user may have moved the cursor before inserting something, try
1560 // to adjust the block for that. But only do it, if the difference
1561 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001562 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1563 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001564 {
1565 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001566 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001567 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001568 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001569 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001570 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001571 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001572 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001573 pre_textlen -= t - oap->start_vcol;
1574 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001575 }
1576 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001577 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001578 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001579 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001580 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001581 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001582 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001583 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001584 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001585 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001586 pre_textlen -= t - oap->start_vcol;
1587 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001588 oap->op_type = OP_INSERT;
1589 }
1590 }
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /*
1593 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001594 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * Don't do this when "$" used, end-of-line will have changed.
1596 */
1597 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1598 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1599 {
1600 if (oap->op_type == OP_APPEND)
1601 {
1602 pre_textlen += bd2.textlen - bd.textlen;
1603 if (bd2.endspaces)
1604 --bd2.textlen;
1605 }
1606 bd.textcol = bd2.textcol;
1607 bd.textlen = bd2.textlen;
1608 }
1609
1610 /*
1611 * Subsequent calls to ml_get() flush the firstline data - take a
1612 * copy of the required string.
1613 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001614 firstline = ml_get(oap->start.lnum);
1615 len = STRLEN(firstline);
1616 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001618 add += bd.textlen;
1619 if ((size_t)add > len)
1620 firstline += len; // short line, point to the NUL
1621 else
1622 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001623 if (pre_textlen >= 0
1624 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001626 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (ins_text != NULL)
1628 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001629 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (u_save(oap->start.lnum,
1631 (linenr_T)(oap->end.lnum + 1)) == OK)
1632 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1633 &bd);
1634
1635 curwin->w_cursor.col = oap->start.col;
1636 check_cursor();
1637 vim_free(ins_text);
1638 }
1639 }
1640 }
1641}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643/*
1644 * op_change - handle a change operation
1645 *
1646 * return TRUE if edit() returns because of a CTRL-O command
1647 */
1648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001649op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651 colnr_T l;
1652 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 long offset;
1654 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001655 long ins_len;
1656 long pre_textlen = 0;
1657 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 char_u *firstline;
1659 char_u *ins_text, *newp, *oldp;
1660 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
1662 l = oap->start.col;
1663 if (oap->motion_type == MLINE)
1664 {
1665 l = 0;
1666#ifdef FEAT_SMARTINDENT
1667 if (!p_paste && curbuf->b_p_si
1668# ifdef FEAT_CINDENT
1669 && !curbuf->b_p_cin
1670# endif
1671 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001672 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673#endif
1674 }
1675
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001676 // First delete the text in the region. In an empty buffer only need to
1677 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1679 {
1680 if (u_save_cursor() == FAIL)
1681 return FALSE;
1682 }
1683 else if (op_delete(oap) == FAIL)
1684 return FALSE;
1685
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001686 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 && !virtual_op)
1688 inc_cursor();
1689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001690 // check for still on same line (<CR> in inserted text meaningless)
1691 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (oap->block_mode)
1693 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001694 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (virtual_op && (curwin->w_cursor.coladd > 0
1696 || gchar_cursor() == NUL))
1697 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001698 firstline = ml_get(oap->start.lnum);
1699 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001700 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 bd.textcol = curwin->w_cursor.col;
1702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1705 if (oap->motion_type == MLINE)
1706 fix_indent();
1707#endif
1708
1709 retval = edit(NUL, FALSE, (linenr_T)1);
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001712 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001714 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001716 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001718 // Auto-indenting may have changed the indent. If the cursor was past
1719 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001721 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001723 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001724
1725 pre_textlen += new_indent - pre_indent;
1726 bd.textcol += new_indent - pre_indent;
1727 }
1728
1729 ins_len = (long)STRLEN(firstline) - pre_textlen;
1730 if (ins_len > 0)
1731 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001732 // Subsequent calls to ml_get() flush the firstline data - take a
1733 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001734 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001736 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1738 linenr++)
1739 {
1740 block_prep(oap, &bd, linenr, TRUE);
1741 if (!bd.is_short || virtual_op)
1742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 pos_T vpos;
1744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001745 // If the block starts in virtual space, count the
1746 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (bd.is_short)
1748 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001749 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 else
1753 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001755 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (newp == NULL)
1757 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001758 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 mch_memmove(newp, oldp, (size_t)bd.textcol);
1760 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001761 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1764 offset += ins_len;
1765 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001766 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 ml_replace(linenr, newp, FALSE);
1768 }
1769 }
1770 check_cursor();
1771
1772 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1773 }
1774 vim_free(ins_text);
1775 }
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 return retval;
1779}
1780
1781/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001782 * When the cursor is on the NUL past the end of the line and it should not be
1783 * there move it left.
1784 */
1785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001786adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001787{
1788 if (curwin->w_cursor.col > 0
1789 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001790 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 && !(restart_edit || (State & INSERT)))
1792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001793 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001794 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001797 {
1798 colnr_T scol, ecol;
1799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001800 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001801 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1802 curwin->w_cursor.coladd = ecol - scol + 1;
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
1805}
1806
Bram Moolenaar81340392012-06-06 16:12:59 +02001807/*
1808 * If "process" is TRUE and the line begins with a comment leader (possibly
1809 * after some white space), return a pointer to the text after it. Put a boolean
1810 * value indicating whether the line ends with an unclosed comment in
1811 * "is_comment".
1812 * line - line to be processed,
1813 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001814 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001815 * include_space - whether to also skip space following the comment leader,
1816 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001817 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001818 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001819 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001820skip_comment(
1821 char_u *line,
1822 int process,
1823 int include_space,
1824 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001825{
1826 char_u *comment_flags = NULL;
1827 int lead_len;
1828 int leader_offset = get_last_leader_offset(line, &comment_flags);
1829
1830 *is_comment = FALSE;
1831 if (leader_offset != -1)
1832 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001833 // Let's check whether the line ends with an unclosed comment.
1834 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001835 while (*comment_flags)
1836 {
1837 if (*comment_flags == COM_END
1838 || *comment_flags == ':')
1839 break;
1840 ++comment_flags;
1841 }
1842 if (*comment_flags != COM_END)
1843 *is_comment = TRUE;
1844 }
1845
1846 if (process == FALSE)
1847 return line;
1848
1849 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1850
1851 if (lead_len == 0)
1852 return line;
1853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001854 // Find:
1855 // - COM_END,
1856 // - colon,
1857 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001858 while (*comment_flags)
1859 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001860 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001861 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001863 ++comment_flags;
1864 }
1865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001866 // If we found a colon, it means that we are not processing a line
1867 // starting with a closing part of a three-part comment. That's good,
1868 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001869 if (*comment_flags == ':' || *comment_flags == NUL)
1870 line += lead_len;
1871
1872 return line;
1873}
Bram Moolenaar81340392012-06-06 16:12:59 +02001874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001876 * Join 'count' lines (minimal 2) at cursor position.
1877 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001878 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1879 * leaders should not be removed.
1880 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1881 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001883 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 */
1885 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001886do_join(
1887 long count,
1888 int insert_space,
1889 int save_undo,
1890 int use_formatoptions UNUSED,
1891 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001893 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001894 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001895 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001897 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001898 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001899 int endcurr1 = NUL;
1900 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001901 int currsize = 0; // size of the current line
1902 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001904 colnr_T col = 0;
1905 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001906 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001907 int remove_comments = (use_formatoptions == TRUE)
1908 && has_format_option(FO_REMOVE_COMS);
1909 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001910#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001911 int propcount = 0; // number of props over all joined lines
1912 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001915 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1916 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001919 // Allocate an array to store the number of spaces inserted before each
1920 // line. We will use it to pre-compute the length of the new line and the
1921 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001922 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001923 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 if (remove_comments)
1926 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001927 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001928 if (comments == NULL)
1929 {
1930 vim_free(spaces);
1931 return FAIL;
1932 }
1933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
1935 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001936 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001937 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001938 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001940 for (t = 0; t < count; ++t)
1941 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001942 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001943#ifdef FEAT_PROP_POPUP
1944 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1945#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001946 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001947 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001948 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001949 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1950 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1951 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 if (remove_comments)
1953 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001954 // We don't want to remove the comment leader if the
1955 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001956 if (t > 0 && prev_was_comment)
1957 {
1958
1959 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1960 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001961 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001962 curr = new_curr;
1963 }
1964 else
1965 curr = skip_comment(curr, FALSE, insert_space,
1966 &prev_was_comment);
1967 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001968
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001969 if (insert_space && t > 0)
1970 {
1971 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01001972 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01001973 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001974 && (!has_format_option(FO_MBYTE_JOIN)
1975 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
1976 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02001977 || (mb_ptr2char(curr) < 0x100
1978 && !(enc_utf8 && utf_eat_space(endcurr1)))
1979 || (endcurr1 < 0x100
1980 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001981 )
1982 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001983 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001984 if (endcurr1 == ' ')
1985 endcurr1 = endcurr2;
1986 else
1987 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001988 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001989 if ( p_js
1990 && (endcurr1 == '.'
1991 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
1992 && (endcurr1 == '?' || endcurr1 == '!'))))
1993 ++spaces[t];
1994 }
1995 }
1996 currsize = (int)STRLEN(curr);
1997 sumsize += currsize + spaces[t];
1998 endcurr1 = endcurr2 = NUL;
1999 if (insert_space && currsize > 0)
2000 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002001 if (has_mbyte)
2002 {
2003 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002004 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002005 endcurr1 = (*mb_ptr2char)(cend);
2006 if (cend > curr)
2007 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002008 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002009 endcurr2 = (*mb_ptr2char)(cend);
2010 }
2011 }
2012 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002013 {
2014 endcurr1 = *(curr + currsize - 1);
2015 if (currsize > 1)
2016 endcurr2 = *(curr + currsize - 2);
2017 }
2018 }
2019 line_breakcheck();
2020 if (got_int)
2021 {
2022 ret = FAIL;
2023 goto theend;
2024 }
2025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002027 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002028 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002030 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002031 newp_len = sumsize + 1;
2032#ifdef FEAT_PROP_POPUP
2033 newp_len += propcount * sizeof(textprop_T);
2034#endif
2035 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002036 if (newp == NULL)
2037 {
2038 ret = FAIL;
2039 goto theend;
2040 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002041 cend = newp + sumsize;
2042 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 /*
2045 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002046 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002047 *
2048 * Move marks from each deleted line to the joined line, adjusting the
2049 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2050 * should not really be a problem.
2051 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002052#ifdef FEAT_PROP_POPUP
2053 props_remaining = propcount;
2054#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002055 for (t = count - 1; ; --t)
2056 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002057 int spaces_removed;
2058
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002059 cend -= currsize;
2060 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002061
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002062 if (spaces[t] > 0)
2063 {
2064 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002065 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002066 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002067
2068 // If deleting more spaces than adding, the cursor moves no more than
2069 // what is added if it is inside these spaces.
2070 spaces_removed = (curr - curr_start) - spaces[t];
2071
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002072 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002073 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002074#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002075 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2076 curwin->w_cursor.lnum + t, t == count - 1,
2077 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002078#endif
2079
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002080 if (t == 0)
2081 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002082 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002083 if (remove_comments)
2084 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002085 if (insert_space && t > 1)
2086 curr = skipwhite(curr);
2087 currsize = (int)STRLEN(curr);
2088 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002089
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002090 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
Bram Moolenaare1004402020-10-24 20:49:43 +02002092 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002094 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002095 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002096 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002097 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002099 // Only report the change in the first line here, del_lines() will report
2100 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 changed_lines(curwin->w_cursor.lnum, currsize,
2102 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 * briefly, and then move it back. After del_lines() the cursor may
2106 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 */
2108 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002110 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 curwin->w_cursor.lnum = t;
2112
2113 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002114 * Set the cursor column:
2115 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002116 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002118 curwin->w_cursor.col =
2119 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 curwin->w_set_curswant = TRUE;
2124
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002125theend:
2126 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002127 if (remove_comments)
2128 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002129 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130}
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 * prepare a few things for block mode yank/delete/tilde
2134 *
2135 * for delete:
2136 * - textlen includes the first/last char to be (partly) deleted
2137 * - start/endspaces is the number of columns that are taken by the
2138 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002139 * deleted.
2140 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 * - textlen includes the first/last char to be wholly yanked
2142 * - start/endspaces is the number of columns of the first/last yanked char
2143 * that are to be yanked.
2144 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002145 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002146block_prep(
2147 oparg_T *oap,
2148 struct block_def *bdp,
2149 linenr_T lnum,
2150 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
2152 int incr = 0;
2153 char_u *pend;
2154 char_u *pstart;
2155 char_u *line;
2156 char_u *prev_pstart;
2157 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002158#ifdef FEAT_LINEBREAK
2159 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002161 // Avoid a problem with unwanted linebreaks in block mode.
2162 curwin->w_p_lbr = FALSE;
2163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 bdp->startspaces = 0;
2165 bdp->endspaces = 0;
2166 bdp->textlen = 0;
2167 bdp->start_vcol = 0;
2168 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 bdp->is_short = FALSE;
2170 bdp->is_oneChar = FALSE;
2171 bdp->pre_whitesp = 0;
2172 bdp->pre_whitesp_c = 0;
2173 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 bdp->start_char_vcols = 0;
2175
2176 line = ml_get(lnum);
2177 pstart = line;
2178 prev_pstart = line;
2179 while (bdp->start_vcol < oap->start_vcol && *pstart)
2180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002181 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002182 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002184 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 bdp->pre_whitesp += incr;
2187 bdp->pre_whitesp_c++;
2188 }
2189 else
2190 {
2191 bdp->pre_whitesp = 0;
2192 bdp->pre_whitesp_c = 0;
2193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002195 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002198 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
2200 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (!is_del || oap->op_type == OP_APPEND)
2203 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2204 }
2205 else
2206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 // notice: this converts partly selected Multibyte characters to
2208 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2210 if (is_del && bdp->startspaces)
2211 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2212 pend = pstart;
2213 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (oap->op_type == OP_INSERT)
2218 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2219 else if (oap->op_type == OP_APPEND)
2220 {
2221 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2222 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2223 }
2224 else
2225 {
2226 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2227 if (is_del && oap->op_type != OP_LSHIFT)
2228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002229 // just putting the sum of those two into
2230 // bdp->startspaces doesn't work for Visual replace,
2231 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 bdp->startspaces = bdp->start_char_vcols
2233 - (bdp->start_vcol - oap->start_vcol);
2234 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2235 }
2236 }
2237 }
2238 else
2239 {
2240 prev_pend = pend;
2241 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2242 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002243 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002245 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 bdp->end_vcol += incr;
2247 }
2248 if (bdp->end_vcol <= oap->end_vcol
2249 && (!is_del
2250 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002254 // Alternative: include spaces to fill up the block.
2255 // Disadvantage: can lead to trailing spaces when the line is
2256 // short where the text is put
2257 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (oap->op_type == OP_APPEND || virtual_op)
2259 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002260 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002262 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 else if (bdp->end_vcol > oap->end_vcol)
2265 {
2266 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2267 if (!is_del && bdp->endspaces)
2268 {
2269 bdp->endspaces = incr - bdp->endspaces;
2270 if (pend != pstart)
2271 pend = prev_pend;
2272 }
2273 }
2274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (is_del && bdp->startspaces)
2277 pstart = prev_pstart;
2278 bdp->textlen = (int)(pend - pstart);
2279 }
2280 bdp->textcol = (colnr_T) (pstart - line);
2281 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002282#ifdef FEAT_LINEBREAK
2283 curwin->w_p_lbr = lbr_saved;
2284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002288 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291op_addsub(
2292 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 linenr_T Prenum1, // Amount of add/subtract
2294 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002296 pos_T pos;
2297 struct block_def bd;
2298 int change_cnt = 0;
2299 linenr_T amount = Prenum1;
2300
Bram Moolenaar6b731882018-11-16 20:54:47 +01002301 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002302 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002303 // the call to changed_lines().
2304#ifdef FEAT_FOLDING
2305 disable_fold_update++;
2306#endif
2307
Bram Moolenaard79e5502016-01-10 22:13:02 +01002308 if (!VIsual_active)
2309 {
2310 pos = curwin->w_cursor;
2311 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002312 {
2313#ifdef FEAT_FOLDING
2314 disable_fold_update--;
2315#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002316 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002317 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002318 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002319#ifdef FEAT_FOLDING
2320 disable_fold_update--;
2321#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002322 if (change_cnt)
2323 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2324 }
2325 else
2326 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002327 int one_change;
2328 int length;
2329 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002330
2331 if (u_save((linenr_T)(oap->start.lnum - 1),
2332 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002333 {
2334#ifdef FEAT_FOLDING
2335 disable_fold_update--;
2336#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002337 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002338 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002339
2340 pos = oap->start;
2341 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2342 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002344 {
2345 block_prep(oap, &bd, pos.lnum, FALSE);
2346 pos.col = bd.textcol;
2347 length = bd.textlen;
2348 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002349 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002350 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002351 curwin->w_cursor.col = 0;
2352 pos.col = 0;
2353 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2354 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002355 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002356 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002357 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002358 dec(&(oap->end));
2359 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2360 pos.col = 0;
2361 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002362 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002363 pos.col += oap->start.col;
2364 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002365 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002366 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002367 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002368 length = (int)STRLEN(ml_get(oap->end.lnum));
2369 if (oap->end.col >= length)
2370 oap->end.col = length - 1;
2371 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002372 }
2373 }
2374 one_change = do_addsub(oap->op_type, &pos, length, amount);
2375 if (one_change)
2376 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002377 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002378 if (change_cnt == 0)
2379 startpos = curbuf->b_op_start;
2380 ++change_cnt;
2381 }
2382
2383#ifdef FEAT_NETBEANS_INTG
2384 if (netbeans_active() && one_change)
2385 {
2386 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2387
2388 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2389 netbeans_inserted(curbuf, pos.lnum, pos.col,
2390 &ptr[pos.col], length);
2391 }
2392#endif
2393 if (g_cmd && one_change)
2394 amount += Prenum1;
2395 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002396
2397#ifdef FEAT_FOLDING
2398 disable_fold_update--;
2399#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002400 if (change_cnt)
2401 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2402
2403 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002404 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002405 redraw_curbuf_later(INVERTED);
2406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 // Set '[ mark if something changed. Keep the last end
2408 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002409 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002410 curbuf->b_op_start = startpos;
2411
2412 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002413 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002414 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002415 }
2416}
2417
2418/*
2419 * Add or subtract 'Prenum1' from a number in a line
2420 * op_type is OP_NR_ADD or OP_NR_SUB
2421 *
2422 * Returns TRUE if some character was changed.
2423 */
2424 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002425do_addsub(
2426 int op_type,
2427 pos_T *pos,
2428 int length,
2429 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 int col;
2432 char_u *buf1;
2433 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002434 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2435 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002436 uvarnumber_T n;
2437 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 char_u *ptr;
2439 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002441 int do_hex;
2442 int do_oct;
2443 int do_bin;
2444 int do_alpha;
2445 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002448 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002449 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002450 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002451 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002452 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002453 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002454 pos_T startpos;
2455 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002456 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002458 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2459 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2460 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2461 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2462 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002464 if (virtual_active())
2465 {
2466 save_coladd = pos->coladd;
2467 pos->coladd = 0;
2468 }
2469
Bram Moolenaard79e5502016-01-10 22:13:02 +01002470 curwin->w_cursor = *pos;
2471 ptr = ml_get(pos->lnum);
2472 col = pos->col;
2473
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002474 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002475 goto theend;
2476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 /*
2478 * First check if we are on a hexadecimal number, after the "0x".
2479 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002480 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002482 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002483 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002484 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002485 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002486 if (has_mbyte)
2487 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002488 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002489
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002490 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002491 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002492 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002493 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002494 if (has_mbyte)
2495 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002496 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002497
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002498 if ( do_bin
2499 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002500 && ! ((col > 0
2501 && (ptr[col] == 'X'
2502 || ptr[col] == 'x')
2503 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002504 && (!has_mbyte ||
2505 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002506 && vim_isxdigit(ptr[col + 1]))))
2507 {
2508
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002509 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002510
Bram Moolenaard79e5502016-01-10 22:13:02 +01002511 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002512
2513 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002514 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002515 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002516 if (has_mbyte)
2517 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002518 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002519 }
2520
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002521 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002522 && col > 0
2523 && (ptr[col] == 'X'
2524 || ptr[col] == 'x')
2525 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002526 && (!has_mbyte ||
2527 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002528 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002529 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002530 && col > 0
2531 && (ptr[col] == 'B'
2532 || ptr[col] == 'b')
2533 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002534 && (!has_mbyte ||
2535 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002536 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002537 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002538 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002539 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002540 if (has_mbyte)
2541 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002542 }
2543 else
2544 {
2545 /*
2546 * Search forward and then backward to find the start of number.
2547 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002548 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002549
2550 while (ptr[col] != NUL
2551 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002552 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002553 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002554
2555 while (col > 0
2556 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002557 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002558 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002559 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002560 if (has_mbyte)
2561 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002562 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002563 }
2564 }
2565
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002566 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002567 {
2568 while (ptr[col] != NUL && length > 0
2569 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002570 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002571 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002572 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002573
2574 col += mb_len;
2575 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002576 }
2577
2578 if (length == 0)
2579 goto theend;
2580
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002581 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002582 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2583 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002584 {
2585 negative = TRUE;
2586 was_positive = FALSE;
2587 }
2588 }
2589
2590 /*
2591 * If a number was found, and saving for undo works, replace the number.
2592 */
2593 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002594 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002595 {
2596 beep_flush();
2597 goto theend;
2598 }
2599
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002600 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002602 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002603 if (op_type == OP_NR_SUB)
2604 {
2605 if (CharOrd(firstdigit) < Prenum1)
2606 {
2607 if (isupper(firstdigit))
2608 firstdigit = 'A';
2609 else
2610 firstdigit = 'a';
2611 }
2612 else
2613#ifdef EBCDIC
2614 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2615#else
2616 firstdigit -= Prenum1;
2617#endif
2618 }
2619 else
2620 {
2621 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2622 {
2623 if (isupper(firstdigit))
2624 firstdigit = 'Z';
2625 else
2626 firstdigit = 'z';
2627 }
2628 else
2629#ifdef EBCDIC
2630 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2631#else
2632 firstdigit += Prenum1;
2633#endif
2634 }
2635 curwin->w_cursor.col = col;
2636 if (!did_change)
2637 startpos = curwin->w_cursor;
2638 did_change = TRUE;
2639 (void)del_char(FALSE);
2640 ins_char(firstdigit);
2641 endpos = curwin->w_cursor;
2642 curwin->w_cursor.col = col;
2643 }
2644 else
2645 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002646 pos_T save_pos;
2647 int i;
2648
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002649 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002650 && (!has_mbyte ||
2651 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002652 && !visual
2653 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002654 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002655 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002656 --col;
2657 negative = TRUE;
2658 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002660 if (visual && VIsual_mode != 'V')
2661 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2662 ? (int)STRLEN(ptr) - col
2663 : length);
2664
2665 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002666 0 + (do_bin ? STR2NR_BIN : 0)
2667 + (do_oct ? STR2NR_OCT : 0)
2668 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002669 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002672 if (pre && negative)
2673 {
2674 ++col;
2675 --length;
2676 negative = FALSE;
2677 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002679 subtract = FALSE;
2680 if (op_type == OP_NR_SUB)
2681 subtract ^= TRUE;
2682 if (negative)
2683 subtract ^= TRUE;
2684
2685 oldn = n;
2686 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002687 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002688 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002689 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002690 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002691 if (!pre)
2692 {
2693 if (subtract)
2694 {
2695 if (n > oldn)
2696 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002697 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002698 negative ^= TRUE;
2699 }
2700 }
2701 else
2702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002704 if (n < oldn)
2705 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002706 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002707 negative ^= TRUE;
2708 }
2709 }
2710 if (n == 0)
2711 negative = FALSE;
2712 }
2713
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002714 if (do_unsigned && negative)
2715 {
2716 if (subtract)
2717 // sticking at zero.
2718 n = (uvarnumber_T)0;
2719 else
2720 // sticking at 2^64 - 1.
2721 n = (uvarnumber_T)(-1);
2722 negative = FALSE;
2723 }
2724
Bram Moolenaard79e5502016-01-10 22:13:02 +01002725 if (visual && !was_positive && !negative && col > 0)
2726 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002728 col--;
2729 length++;
2730 }
2731
2732 /*
2733 * Delete the old number.
2734 */
2735 curwin->w_cursor.col = col;
2736 if (!did_change)
2737 startpos = curwin->w_cursor;
2738 did_change = TRUE;
2739 todel = length;
2740 c = gchar_cursor();
2741 /*
2742 * Don't include the '-' in the length, only the length of the
2743 * part after it is kept the same.
2744 */
2745 if (c == '-')
2746 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002747
2748 save_pos = curwin->w_cursor;
2749 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002750 {
2751 if (c < 0x100 && isalpha(c))
2752 {
2753 if (isupper(c))
2754 hexupper = TRUE;
2755 else
2756 hexupper = FALSE;
2757 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002758 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002759 c = gchar_cursor();
2760 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002761 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002762
2763 /*
2764 * Prepare the leading characters in buf1[].
2765 * When there are many leading zeros it could be very long.
2766 * Allocate a bit too much.
2767 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002768 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002769 if (buf1 == NULL)
2770 goto theend;
2771 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002772 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002773 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002774 if (pre)
2775 {
2776 *ptr++ = '0';
2777 --length;
2778 }
2779 if (pre == 'b' || pre == 'B' ||
2780 pre == 'x' || pre == 'X')
2781 {
2782 *ptr++ = pre;
2783 --length;
2784 }
2785
2786 /*
2787 * Put the number characters in buf2[].
2788 */
2789 if (pre == 'b' || pre == 'B')
2790 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002791 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002792 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002794 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002795 for (bit = bits; bit > 0; bit--)
2796 if ((n >> (bit - 1)) & 0x1) break;
2797
2798 for (i = 0; bit > 0; bit--)
2799 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2800
2801 buf2[i] = '\0';
2802 }
2803 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002804 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002805 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002806 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002807 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002808 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002809 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002810 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002811 length -= (int)STRLEN(buf2);
2812
2813 /*
2814 * Adjust number of zeros to the new number of digits, so the
2815 * total length of the number remains the same.
2816 * Don't do this when
2817 * the result may look like an octal number.
2818 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002819 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002820 while (length-- > 0)
2821 *ptr++ = '0';
2822 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002823
Bram Moolenaard79e5502016-01-10 22:13:02 +01002824 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002825
2826 // Insert just after the first character to be removed, so that any
2827 // text properties will be adjusted. Then delete the old number
2828 // afterwards.
2829 save_pos = curwin->w_cursor;
2830 if (todel > 0)
2831 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002833 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002834
2835 // del_char() will also mark line needing displaying
2836 if (todel > 0)
2837 {
2838 int bytes_after = (int)STRLEN(ml_get_curline())
2839 - curwin->w_cursor.col;
2840
2841 // Delete the one character before the insert.
2842 curwin->w_cursor = save_pos;
2843 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002844 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2845 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002846 --todel;
2847 }
2848 while (todel-- > 0)
2849 (void)del_char(FALSE);
2850
Bram Moolenaard79e5502016-01-10 22:13:02 +01002851 endpos = curwin->w_cursor;
2852 if (did_change && curwin->w_cursor.col)
2853 --curwin->w_cursor.col;
2854 }
2855
Bram Moolenaare1004402020-10-24 20:49:43 +02002856 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002859 curbuf->b_op_start = startpos;
2860 curbuf->b_op_end = endpos;
2861 if (curbuf->b_op_end.col > 0)
2862 --curbuf->b_op_end.col;
2863 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002864
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002865theend:
2866 if (visual)
2867 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002868 else if (did_change)
2869 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002870 else if (virtual_active())
2871 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002872
Bram Moolenaard79e5502016-01-10 22:13:02 +01002873 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874}
2875
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002877clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002879 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880}
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002883 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *
2885 * "Words" are counted by looking for boundaries between non-space and
2886 * space characters. (it seems to produce results that match 'wc'.)
2887 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002888 * Return value is byte count; word count for the line is added to "*wc".
2889 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 *
2891 * The function will only examine the first "limit" characters in the
2892 * line, stopping if it encounters an end-of-line (NUL byte). In that
2893 * case, eol_size will be added to the character count to account for
2894 * the size of the EOL character.
2895 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002896 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002897line_count_info(
2898 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002899 varnumber_T *wc,
2900 varnumber_T *cc,
2901 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002902 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002904 varnumber_T i;
2905 varnumber_T words = 0;
2906 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 int is_word = 0;
2908
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002909 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 if (is_word)
2912 {
2913 if (vim_isspace(line[i]))
2914 {
2915 words++;
2916 is_word = 0;
2917 }
2918 }
2919 else if (!vim_isspace(line[i]))
2920 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002921 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002922 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924
2925 if (is_word)
2926 words++;
2927 *wc += words;
2928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002930 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002933 chars += eol_size;
2934 }
2935 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 return i;
2937}
2938
2939/*
2940 * Give some info about the position of the cursor (for "g CTRL-G").
2941 * In Visual mode, give some info about the selected region. (In this case,
2942 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002943 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 */
2945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002946cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002949 char_u buf1[50];
2950 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002952 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002953 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002954 varnumber_T byte_count_cursor = 0;
2955 varnumber_T char_count = 0;
2956 varnumber_T char_count_cursor = 0;
2957 varnumber_T word_count = 0;
2958 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002959 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002960 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 long line_count_selected = 0;
2962 pos_T min_pos, max_pos;
2963 oparg_T oparg;
2964 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 /*
2967 * Compute the length of the file in characters.
2968 */
2969 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2970 {
Bram Moolenaared767a22016-01-03 22:49:16 +01002971 if (dict == NULL)
2972 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002973 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01002974 return;
2975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977 else
2978 {
2979 if (get_fileformat(curbuf) == EOL_DOS)
2980 eol_size = 2;
2981 else
2982 eol_size = 1;
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 if (VIsual_active)
2985 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002986 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 min_pos = VIsual;
2989 max_pos = curwin->w_cursor;
2990 }
2991 else
2992 {
2993 min_pos = curwin->w_cursor;
2994 max_pos = VIsual;
2995 }
2996 if (*p_sel == 'e' && max_pos.col > 0)
2997 --max_pos.col;
2998
2999 if (VIsual_mode == Ctrl_V)
3000 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003001#ifdef FEAT_LINEBREAK
3002 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003003 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003006 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003007 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 oparg.is_VIsual = 1;
3010 oparg.block_mode = TRUE;
3011 oparg.op_type = OP_NOP;
3012 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003013 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003014#ifdef FEAT_LINEBREAK
3015 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003016 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003017#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003018 if (curwin->w_curswant == MAXCOL)
3019 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (oparg.end_vcol < oparg.start_vcol)
3022 {
3023 oparg.end_vcol += oparg.start_vcol;
3024 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3025 oparg.end_vcol -= oparg.start_vcol;
3026 }
3027 }
3028 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
3031 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3032 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003034 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
3036 ui_breakcheck();
3037 if (got_int)
3038 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003039 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if (VIsual_active
3044 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3045 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003046 char_u *s = NULL;
3047 long len = 0L;
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 switch (VIsual_mode)
3050 {
3051 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003055 s = bd.textstart;
3056 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 break;
3058 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003059 s = ml_get(lnum);
3060 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 break;
3062 case 'v':
3063 {
3064 colnr_T start_col = (lnum == min_pos.lnum)
3065 ? min_pos.col : 0;
3066 colnr_T end_col = (lnum == max_pos.lnum)
3067 ? max_pos.col - start_col + 1 : MAXCOL;
3068
Bram Moolenaardef9e822004-12-31 20:58:58 +00003069 s = ml_get(lnum) + start_col;
3070 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072 break;
3073 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003074 if (s != NULL)
3075 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003076 byte_count_cursor += line_count_info(s, &word_count_cursor,
3077 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003078 if (lnum == curbuf->b_ml.ml_line_count
3079 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003080 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003081 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003082 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003087 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (lnum == curwin->w_cursor.lnum)
3089 {
3090 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003091 char_count_cursor += char_count;
3092 byte_count_cursor = byte_count +
3093 line_count_info(ml_get(lnum),
3094 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003095 (varnumber_T)(curwin->w_cursor.col + 1),
3096 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003100 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003101 &char_count, (varnumber_T)MAXCOL,
3102 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003105 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003106 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003107 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
Bram Moolenaared767a22016-01-03 22:49:16 +01003109 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003111 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003113 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3114 {
3115 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3116 &max_pos.col);
3117 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3118 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3119 }
3120 else
3121 buf1[0] = NUL;
3122
3123 if (char_count_cursor == byte_count_cursor
3124 && char_count == byte_count)
3125 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003126 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003127 buf1, line_count_selected,
3128 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003129 (varnumber_T)word_count_cursor,
3130 (varnumber_T)word_count,
3131 (varnumber_T)byte_count_cursor,
3132 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003133 else
3134 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003135 _("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 +01003136 buf1, line_count_selected,
3137 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003138 (varnumber_T)word_count_cursor,
3139 (varnumber_T)word_count,
3140 (varnumber_T)char_count_cursor,
3141 (varnumber_T)char_count,
3142 (varnumber_T)byte_count_cursor,
3143 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 }
3145 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003146 {
3147 p = ml_get_curline();
3148 validate_virtcol();
3149 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3150 (int)curwin->w_virtcol + 1);
3151 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3152 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaared767a22016-01-03 22:49:16 +01003154 if (char_count_cursor == byte_count_cursor
3155 && char_count == byte_count)
3156 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003157 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003158 (char *)buf1, (char *)buf2,
3159 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003161 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3162 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003163 else
3164 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003165 _("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 +01003166 (char *)buf1, (char *)buf2,
3167 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003168 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003169 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3170 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3171 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003172 }
3173 }
3174
Bram Moolenaared767a22016-01-03 22:49:16 +01003175 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003176 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003177 {
3178 size_t len = STRLEN(IObuff);
3179
3180 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003181 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003182 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003183 if (dict == NULL)
3184 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003185 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003186 p = p_shm;
3187 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003188 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003189 p_shm = p;
3190 }
3191 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003192#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003193 if (dict != NULL)
3194 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003195 dict_add_number(dict, "words", word_count);
3196 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003197 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003198 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3199 byte_count_cursor);
3200 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3201 char_count_cursor);
3202 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3203 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003207
3208/*
3209 * Handle indent and format operators and visual mode ":".
3210 */
3211 static void
3212op_colon(oparg_T *oap)
3213{
3214 stuffcharReadbuff(':');
3215 if (oap->is_VIsual)
3216 stuffReadbuff((char_u *)"'<,'>");
3217 else
3218 {
3219 // Make the range look nice, so it can be repeated.
3220 if (oap->start.lnum == curwin->w_cursor.lnum)
3221 stuffcharReadbuff('.');
3222 else
3223 stuffnumReadbuff((long)oap->start.lnum);
3224 if (oap->end.lnum != oap->start.lnum)
3225 {
3226 stuffcharReadbuff(',');
3227 if (oap->end.lnum == curwin->w_cursor.lnum)
3228 stuffcharReadbuff('.');
3229 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3230 stuffcharReadbuff('$');
3231 else if (oap->start.lnum == curwin->w_cursor.lnum)
3232 {
3233 stuffReadbuff((char_u *)".+");
3234 stuffnumReadbuff((long)oap->line_count - 1);
3235 }
3236 else
3237 stuffnumReadbuff((long)oap->end.lnum);
3238 }
3239 }
3240 if (oap->op_type != OP_COLON)
3241 stuffReadbuff((char_u *)"!");
3242 if (oap->op_type == OP_INDENT)
3243 {
3244#ifndef FEAT_CINDENT
3245 if (*get_equalprg() == NUL)
3246 stuffReadbuff((char_u *)"indent");
3247 else
3248#endif
3249 stuffReadbuff(get_equalprg());
3250 stuffReadbuff((char_u *)"\n");
3251 }
3252 else if (oap->op_type == OP_FORMAT)
3253 {
3254 if (*curbuf->b_p_fp != NUL)
3255 stuffReadbuff(curbuf->b_p_fp);
3256 else if (*p_fp != NUL)
3257 stuffReadbuff(p_fp);
3258 else
3259 stuffReadbuff((char_u *)"fmt");
3260 stuffReadbuff((char_u *)"\n']");
3261 }
3262
3263 // do_cmdline() does the rest
3264}
3265
3266/*
3267 * Handle the "g@" operator: call 'operatorfunc'.
3268 */
3269 static void
3270op_function(oparg_T *oap UNUSED)
3271{
3272#ifdef FEAT_EVAL
3273 typval_T argv[2];
3274 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003275 pos_T orig_start = curbuf->b_op_start;
3276 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003277
3278 if (*p_opfunc == NUL)
3279 emsg(_("E774: 'operatorfunc' is empty"));
3280 else
3281 {
3282 // Set '[ and '] marks to text to be operated on.
3283 curbuf->b_op_start = oap->start;
3284 curbuf->b_op_end = oap->end;
3285 if (oap->motion_type != MLINE && !oap->inclusive)
3286 // Exclude the end position.
3287 decl(&curbuf->b_op_end);
3288
3289 argv[0].v_type = VAR_STRING;
3290 if (oap->block_mode)
3291 argv[0].vval.v_string = (char_u *)"block";
3292 else if (oap->motion_type == MLINE)
3293 argv[0].vval.v_string = (char_u *)"line";
3294 else
3295 argv[0].vval.v_string = (char_u *)"char";
3296 argv[1].v_type = VAR_UNKNOWN;
3297
3298 // Reset virtual_op so that 'virtualedit' can be changed in the
3299 // function.
3300 virtual_op = MAYBE;
3301
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +01003302 (void)call_func_noret(p_opfunc, 1, argv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003303
3304 virtual_op = save_virtual_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003305 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003306 {
3307 curbuf->b_op_start = orig_start;
3308 curbuf->b_op_end = orig_end;
3309 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003310 }
3311#else
3312 emsg(_("E775: Eval feature not available"));
3313#endif
3314}
3315
3316/*
3317 * Calculate start/end virtual columns for operating in block mode.
3318 */
3319 static void
3320get_op_vcol(
3321 oparg_T *oap,
3322 colnr_T redo_VIsual_vcol,
3323 int initial) // when TRUE adjust position for 'selectmode'
3324{
3325 colnr_T start, end;
3326
3327 if (VIsual_mode != Ctrl_V
3328 || (!initial && oap->end.col < curwin->w_width))
3329 return;
3330
3331 oap->block_mode = TRUE;
3332
3333 // prevent from moving onto a trail byte
3334 if (has_mbyte)
3335 mb_adjustpos(curwin->w_buffer, &oap->end);
3336
3337 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3338
3339 if (!redo_VIsual_busy)
3340 {
3341 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3342
3343 if (start < oap->start_vcol)
3344 oap->start_vcol = start;
3345 if (end > oap->end_vcol)
3346 {
3347 if (initial && *p_sel == 'e' && start >= 1
3348 && start - 1 >= oap->end_vcol)
3349 oap->end_vcol = start - 1;
3350 else
3351 oap->end_vcol = end;
3352 }
3353 }
3354
3355 // if '$' was used, get oap->end_vcol from longest line
3356 if (curwin->w_curswant == MAXCOL)
3357 {
3358 curwin->w_cursor.col = MAXCOL;
3359 oap->end_vcol = 0;
3360 for (curwin->w_cursor.lnum = oap->start.lnum;
3361 curwin->w_cursor.lnum <= oap->end.lnum;
3362 ++curwin->w_cursor.lnum)
3363 {
3364 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3365 if (end > oap->end_vcol)
3366 oap->end_vcol = end;
3367 }
3368 }
3369 else if (redo_VIsual_busy)
3370 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3371 // Correct oap->end.col and oap->start.col to be the
3372 // upper-left and lower-right corner of the block area.
3373 //
3374 // (Actually, this does convert column positions into character
3375 // positions)
3376 curwin->w_cursor.lnum = oap->end.lnum;
3377 coladvance(oap->end_vcol);
3378 oap->end = curwin->w_cursor;
3379
3380 curwin->w_cursor = oap->start;
3381 coladvance(oap->start_vcol);
3382 oap->start = curwin->w_cursor;
3383}
3384
3385/*
3386 * Handle an operator after Visual mode or when the movement is finished.
3387 * "gui_yank" is true when yanking text for the clipboard.
3388 */
3389 void
3390do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3391{
3392 oparg_T *oap = cap->oap;
3393 pos_T old_cursor;
3394 int empty_region_error;
3395 int restart_edit_save;
3396#ifdef FEAT_LINEBREAK
3397 int lbr_saved = curwin->w_p_lbr;
3398#endif
3399
3400 // The visual area is remembered for redo
3401 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3402 static linenr_T redo_VIsual_line_count; // number of lines
3403 static colnr_T redo_VIsual_vcol; // number of cols or end column
3404 static long redo_VIsual_count; // count for Visual operator
3405 static int redo_VIsual_arg; // extra argument
3406 int include_line_break = FALSE;
3407
3408#if defined(FEAT_CLIPBOARD)
3409 // Yank the visual area into the GUI selection register before we operate
3410 // on it and lose it forever.
3411 // Don't do it if a specific register was specified, so that ""x"*P works.
3412 // This could call do_pending_operator() recursively, but that's OK
3413 // because gui_yank will be TRUE for the nested call.
3414 if ((clip_star.available || clip_plus.available)
3415 && oap->op_type != OP_NOP
3416 && !gui_yank
3417 && VIsual_active
3418 && !redo_VIsual_busy
3419 && oap->regname == 0)
3420 clip_auto_select();
3421#endif
3422 old_cursor = curwin->w_cursor;
3423
3424 // If an operation is pending, handle it...
3425 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3426 {
3427 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3428 // for the clipboard.
3429 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3430
3431#ifdef FEAT_LINEBREAK
3432 // Avoid a problem with unwanted linebreaks in block mode.
3433 if (curwin->w_p_lbr)
3434 curwin->w_valid &= ~VALID_VIRTCOL;
3435 curwin->w_p_lbr = FALSE;
3436#endif
3437 oap->is_VIsual = VIsual_active;
3438 if (oap->motion_force == 'V')
3439 oap->motion_type = MLINE;
3440 else if (oap->motion_force == 'v')
3441 {
3442 // If the motion was linewise, "inclusive" will not have been set.
3443 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3444 if (oap->motion_type == MLINE)
3445 oap->inclusive = FALSE;
3446 // If the motion already was characterwise, toggle "inclusive"
3447 else if (oap->motion_type == MCHAR)
3448 oap->inclusive = !oap->inclusive;
3449 oap->motion_type = MCHAR;
3450 }
3451 else if (oap->motion_force == Ctrl_V)
3452 {
3453 // Change line- or characterwise motion into Visual block mode.
3454 if (!VIsual_active)
3455 {
3456 VIsual_active = TRUE;
3457 VIsual = oap->start;
3458 }
3459 VIsual_mode = Ctrl_V;
3460 VIsual_select = FALSE;
3461 VIsual_reselect = FALSE;
3462 }
3463
3464 // Only redo yank when 'y' flag is in 'cpoptions'.
3465 // Never redo "zf" (define fold).
3466 if ((redo_yank || oap->op_type != OP_YANK)
3467 && ((!VIsual_active || oap->motion_force)
3468 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003469 || (VIsual_active
3470 && (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3471 && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003472 && cap->cmdchar != 'D'
3473#ifdef FEAT_FOLDING
3474 && oap->op_type != OP_FOLD
3475 && oap->op_type != OP_FOLDOPEN
3476 && oap->op_type != OP_FOLDOPENREC
3477 && oap->op_type != OP_FOLDCLOSE
3478 && oap->op_type != OP_FOLDCLOSEREC
3479 && oap->op_type != OP_FOLDDEL
3480 && oap->op_type != OP_FOLDDELREC
3481#endif
3482 )
3483 {
3484 prep_redo(oap->regname, cap->count0,
3485 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3486 oap->motion_force, cap->cmdchar, cap->nchar);
3487 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3488 {
3489 // If 'cpoptions' does not contain 'r', insert the search
3490 // pattern to really repeat the same command.
3491 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3492 AppendToRedobuffLit(cap->searchbuf, -1);
3493 AppendToRedobuff(NL_STR);
3494 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003495 else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003496 {
3497 // do_cmdline() has stored the first typed line in
3498 // "repeat_cmdline". When several lines are typed repeating
3499 // won't be possible.
3500 if (repeat_cmdline == NULL)
3501 ResetRedobuff();
3502 else
3503 {
3504 AppendToRedobuffLit(repeat_cmdline, -1);
3505 AppendToRedobuff(NL_STR);
3506 VIM_CLEAR(repeat_cmdline);
3507 }
3508 }
3509 }
3510
3511 if (redo_VIsual_busy)
3512 {
3513 // Redo of an operation on a Visual area. Use the same size from
3514 // redo_VIsual_line_count and redo_VIsual_vcol.
3515 oap->start = curwin->w_cursor;
3516 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3517 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3518 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3519 VIsual_mode = redo_VIsual_mode;
3520 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3521 {
3522 if (VIsual_mode == 'v')
3523 {
3524 if (redo_VIsual_line_count <= 1)
3525 {
3526 validate_virtcol();
3527 curwin->w_curswant =
3528 curwin->w_virtcol + redo_VIsual_vcol - 1;
3529 }
3530 else
3531 curwin->w_curswant = redo_VIsual_vcol;
3532 }
3533 else
3534 {
3535 curwin->w_curswant = MAXCOL;
3536 }
3537 coladvance(curwin->w_curswant);
3538 }
3539 cap->count0 = redo_VIsual_count;
3540 if (redo_VIsual_count != 0)
3541 cap->count1 = redo_VIsual_count;
3542 else
3543 cap->count1 = 1;
3544 }
3545 else if (VIsual_active)
3546 {
3547 if (!gui_yank)
3548 {
3549 // Save the current VIsual area for '< and '> marks, and "gv"
3550 curbuf->b_visual.vi_start = VIsual;
3551 curbuf->b_visual.vi_end = curwin->w_cursor;
3552 curbuf->b_visual.vi_mode = VIsual_mode;
3553 restore_visual_mode();
3554 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3555# ifdef FEAT_EVAL
3556 curbuf->b_visual_mode_eval = VIsual_mode;
3557# endif
3558 }
3559
3560 // In Select mode, a linewise selection is operated upon like a
3561 // characterwise selection.
3562 // Special case: gH<Del> deletes the last line.
3563 if (VIsual_select && VIsual_mode == 'V'
3564 && cap->oap->op_type != OP_DELETE)
3565 {
3566 if (LT_POS(VIsual, curwin->w_cursor))
3567 {
3568 VIsual.col = 0;
3569 curwin->w_cursor.col =
3570 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3571 }
3572 else
3573 {
3574 curwin->w_cursor.col = 0;
3575 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3576 }
3577 VIsual_mode = 'v';
3578 }
3579 // If 'selection' is "exclusive", backup one character for
3580 // charwise selections.
3581 else if (VIsual_mode == 'v')
3582 include_line_break = unadjust_for_sel();
3583
3584 oap->start = VIsual;
3585 if (VIsual_mode == 'V')
3586 {
3587 oap->start.col = 0;
3588 oap->start.coladd = 0;
3589 }
3590 }
3591
3592 // Set oap->start to the first position of the operated text, oap->end
3593 // to the end of the operated text. w_cursor is equal to oap->start.
3594 if (LT_POS(oap->start, curwin->w_cursor))
3595 {
3596#ifdef FEAT_FOLDING
3597 // Include folded lines completely.
3598 if (!VIsual_active)
3599 {
3600 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3601 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003602 if ((curwin->w_cursor.col > 0 || oap->inclusive
3603 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003604 && hasFolding(curwin->w_cursor.lnum, NULL,
3605 &curwin->w_cursor.lnum))
3606 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3607 }
3608#endif
3609 oap->end = curwin->w_cursor;
3610 curwin->w_cursor = oap->start;
3611
3612 // w_virtcol may have been updated; if the cursor goes back to its
3613 // previous position w_virtcol becomes invalid and isn't updated
3614 // automatically.
3615 curwin->w_valid &= ~VALID_VIRTCOL;
3616 }
3617 else
3618 {
3619#ifdef FEAT_FOLDING
3620 // Include folded lines completely.
3621 if (!VIsual_active && oap->motion_type == MLINE)
3622 {
3623 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3624 NULL))
3625 curwin->w_cursor.col = 0;
3626 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3627 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3628 }
3629#endif
3630 oap->end = oap->start;
3631 oap->start = curwin->w_cursor;
3632 }
3633
3634 // Just in case lines were deleted that make the position invalid.
3635 check_pos(curwin->w_buffer, &oap->end);
3636 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3637
3638 // Set "virtual_op" before resetting VIsual_active.
3639 virtual_op = virtual_active();
3640
3641 if (VIsual_active || redo_VIsual_busy)
3642 {
3643 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3644
3645 if (!redo_VIsual_busy && !gui_yank)
3646 {
3647 // Prepare to reselect and redo Visual: this is based on the
3648 // size of the Visual text
3649 resel_VIsual_mode = VIsual_mode;
3650 if (curwin->w_curswant == MAXCOL)
3651 resel_VIsual_vcol = MAXCOL;
3652 else
3653 {
3654 if (VIsual_mode != Ctrl_V)
3655 getvvcol(curwin, &(oap->end),
3656 NULL, NULL, &oap->end_vcol);
3657 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3658 {
3659 if (VIsual_mode != Ctrl_V)
3660 getvvcol(curwin, &(oap->start),
3661 &oap->start_vcol, NULL, NULL);
3662 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3663 }
3664 else
3665 resel_VIsual_vcol = oap->end_vcol;
3666 }
3667 resel_VIsual_line_count = oap->line_count;
3668 }
3669
3670 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3671 if ((redo_yank || oap->op_type != OP_YANK)
3672 && oap->op_type != OP_COLON
3673#ifdef FEAT_FOLDING
3674 && oap->op_type != OP_FOLD
3675 && oap->op_type != OP_FOLDOPEN
3676 && oap->op_type != OP_FOLDOPENREC
3677 && oap->op_type != OP_FOLDCLOSE
3678 && oap->op_type != OP_FOLDCLOSEREC
3679 && oap->op_type != OP_FOLDDEL
3680 && oap->op_type != OP_FOLDDELREC
3681#endif
3682 && oap->motion_force == NUL
3683 )
3684 {
3685 // Prepare for redoing. Only use the nchar field for "r",
3686 // otherwise it might be the second char of the operator.
3687 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3688 || cap->nchar == 'N'))
3689 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003690 get_op_char(oap->op_type),
3691 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003692 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003693 else if (cap->cmdchar != ':' && cap->cmdchar != K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003694 {
3695 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3696
3697 // reverse what nv_replace() did
3698 if (nchar == REPLACE_CR_NCHAR)
3699 nchar = CAR;
3700 else if (nchar == REPLACE_NL_NCHAR)
3701 nchar = NL;
3702 prep_redo(oap->regname, 0L, NUL, 'v',
3703 get_op_char(oap->op_type),
3704 get_extra_op_char(oap->op_type),
3705 nchar);
3706 }
3707 if (!redo_VIsual_busy)
3708 {
3709 redo_VIsual_mode = resel_VIsual_mode;
3710 redo_VIsual_vcol = resel_VIsual_vcol;
3711 redo_VIsual_line_count = resel_VIsual_line_count;
3712 redo_VIsual_count = cap->count0;
3713 redo_VIsual_arg = cap->arg;
3714 }
3715 }
3716
3717 // oap->inclusive defaults to TRUE.
3718 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3719 // FALSE. This makes "d}P" and "v}dP" work the same.
3720 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3721 oap->inclusive = TRUE;
3722 if (VIsual_mode == 'V')
3723 oap->motion_type = MLINE;
3724 else
3725 {
3726 oap->motion_type = MCHAR;
3727 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3728 && (include_line_break || !virtual_op))
3729 {
3730 oap->inclusive = FALSE;
3731 // Try to include the newline, unless it's an operator
3732 // that works on lines only.
3733 if (*p_sel != 'o'
3734 && !op_on_lines(oap->op_type)
3735 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3736 {
3737 ++oap->end.lnum;
3738 oap->end.col = 0;
3739 oap->end.coladd = 0;
3740 ++oap->line_count;
3741 }
3742 }
3743 }
3744
3745 redo_VIsual_busy = FALSE;
3746
3747 // Switch Visual off now, so screen updating does
3748 // not show inverted text when the screen is redrawn.
3749 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3750 // no screen redraw, so it is done here to remove the inverted
3751 // part.
3752 if (!gui_yank)
3753 {
3754 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003755 setmouse();
3756 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003757 may_clear_cmdline();
3758 if ((oap->op_type == OP_YANK
3759 || oap->op_type == OP_COLON
3760 || oap->op_type == OP_FUNCTION
3761 || oap->op_type == OP_FILTER)
3762 && oap->motion_force == NUL)
3763 {
3764#ifdef FEAT_LINEBREAK
3765 // make sure redrawing is correct
3766 curwin->w_p_lbr = lbr_saved;
3767#endif
3768 redraw_curbuf_later(INVERTED);
3769 }
3770 }
3771 }
3772
3773 // Include the trailing byte of a multi-byte char.
3774 if (has_mbyte && oap->inclusive)
3775 {
3776 int l;
3777
3778 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3779 if (l > 1)
3780 oap->end.col += l - 1;
3781 }
3782 curwin->w_set_curswant = TRUE;
3783
3784 // oap->empty is set when start and end are the same. The inclusive
3785 // flag affects this too, unless yanking and the end is on a NUL.
3786 oap->empty = (oap->motion_type == MCHAR
3787 && (!oap->inclusive
3788 || (oap->op_type == OP_YANK
3789 && gchar_pos(&oap->end) == NUL))
3790 && EQUAL_POS(oap->start, oap->end)
3791 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3792 // For delete, change and yank, it's an error to operate on an
3793 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3794 empty_region_error = (oap->empty
3795 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3796
3797 // Force a redraw when operating on an empty Visual region, when
3798 // 'modifiable is off or creating a fold.
3799 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3800#ifdef FEAT_FOLDING
3801 || oap->op_type == OP_FOLD
3802#endif
3803 ))
3804 {
3805#ifdef FEAT_LINEBREAK
3806 curwin->w_p_lbr = lbr_saved;
3807#endif
3808 redraw_curbuf_later(INVERTED);
3809 }
3810
3811 // If the end of an operator is in column one while oap->motion_type
3812 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3813 // character in the previous line. If op_start is on or before the
3814 // first non-blank in the line, the operator becomes linewise
3815 // (strange, but that's the way vi does it).
3816 if ( oap->motion_type == MCHAR
3817 && oap->inclusive == FALSE
3818 && !(cap->retval & CA_NO_ADJ_OP_END)
3819 && oap->end.col == 0
3820 && (!oap->is_VIsual || *p_sel == 'o')
3821 && !oap->block_mode
3822 && oap->line_count > 1)
3823 {
3824 oap->end_adjusted = TRUE; // remember that we did this
3825 --oap->line_count;
3826 --oap->end.lnum;
3827 if (inindent(0))
3828 oap->motion_type = MLINE;
3829 else
3830 {
3831 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3832 if (oap->end.col)
3833 {
3834 --oap->end.col;
3835 oap->inclusive = TRUE;
3836 }
3837 }
3838 }
3839 else
3840 oap->end_adjusted = FALSE;
3841
3842 switch (oap->op_type)
3843 {
3844 case OP_LSHIFT:
3845 case OP_RSHIFT:
3846 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3847 auto_format(FALSE, TRUE);
3848 break;
3849
3850 case OP_JOIN_NS:
3851 case OP_JOIN:
3852 if (oap->line_count < 2)
3853 oap->line_count = 2;
3854 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3855 curbuf->b_ml.ml_line_count)
3856 beep_flush();
3857 else
3858 {
3859 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3860 TRUE, TRUE, TRUE);
3861 auto_format(FALSE, TRUE);
3862 }
3863 break;
3864
3865 case OP_DELETE:
3866 VIsual_reselect = FALSE; // don't reselect now
3867 if (empty_region_error)
3868 {
3869 vim_beep(BO_OPER);
3870 CancelRedo();
3871 }
3872 else
3873 {
3874 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01003875 // save cursor line for undo if it wasn't saved yet
3876 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3877 && u_save_cursor() == OK)
3878 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003879 }
3880 break;
3881
3882 case OP_YANK:
3883 if (empty_region_error)
3884 {
3885 if (!gui_yank)
3886 {
3887 vim_beep(BO_OPER);
3888 CancelRedo();
3889 }
3890 }
3891 else
3892 {
3893#ifdef FEAT_LINEBREAK
3894 curwin->w_p_lbr = lbr_saved;
3895#endif
3896 (void)op_yank(oap, FALSE, !gui_yank);
3897 }
3898 check_cursor_col();
3899 break;
3900
3901 case OP_CHANGE:
3902 VIsual_reselect = FALSE; // don't reselect now
3903 if (empty_region_error)
3904 {
3905 vim_beep(BO_OPER);
3906 CancelRedo();
3907 }
3908 else
3909 {
3910 // This is a new edit command, not a restart. Need to
3911 // remember it to make 'insertmode' work with mappings for
3912 // Visual mode. But do this only once and not when typed and
3913 // 'insertmode' isn't set.
3914 if (p_im || !KeyTyped)
3915 restart_edit_save = restart_edit;
3916 else
3917 restart_edit_save = 0;
3918 restart_edit = 0;
3919#ifdef FEAT_LINEBREAK
3920 // Restore linebreak, so that when the user edits it looks as
3921 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01003922 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003923#endif
3924 // Reset finish_op now, don't want it set inside edit().
3925 finish_op = FALSE;
3926 if (op_change(oap)) // will call edit()
3927 cap->retval |= CA_COMMAND_BUSY;
3928 if (restart_edit == 0)
3929 restart_edit = restart_edit_save;
3930 }
3931 break;
3932
3933 case OP_FILTER:
3934 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3935 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
3936 else
3937 bangredo = TRUE; // do_bang() will put cmd in redo buffer
3938 // FALLTHROUGH
3939
3940 case OP_INDENT:
3941 case OP_COLON:
3942
3943#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3944 // If 'equalprg' is empty, do the indenting internally.
3945 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3946 {
3947# ifdef FEAT_LISP
3948 if (curbuf->b_p_lisp)
3949 {
3950 op_reindent(oap, get_lisp_indent);
3951 break;
3952 }
3953# endif
3954# ifdef FEAT_CINDENT
3955 op_reindent(oap,
3956# ifdef FEAT_EVAL
3957 *curbuf->b_p_inde != NUL ? get_expr_indent :
3958# endif
3959 get_c_indent);
3960 break;
3961# endif
3962 }
3963#endif
3964
3965 op_colon(oap);
3966 break;
3967
3968 case OP_TILDE:
3969 case OP_UPPER:
3970 case OP_LOWER:
3971 case OP_ROT13:
3972 if (empty_region_error)
3973 {
3974 vim_beep(BO_OPER);
3975 CancelRedo();
3976 }
3977 else
3978 op_tilde(oap);
3979 check_cursor_col();
3980 break;
3981
3982 case OP_FORMAT:
3983#if defined(FEAT_EVAL)
3984 if (*curbuf->b_p_fex != NUL)
3985 op_formatexpr(oap); // use expression
3986 else
3987#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003988 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003989 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003990 op_colon(oap); // use external command
3991 else
3992 op_format(oap, FALSE); // use internal function
3993 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003994 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003995 case OP_FORMAT2:
3996 op_format(oap, TRUE); // use internal function
3997 break;
3998
3999 case OP_FUNCTION:
4000#ifdef FEAT_LINEBREAK
4001 // Restore linebreak, so that when the user edits it looks as
4002 // before.
4003 curwin->w_p_lbr = lbr_saved;
4004#endif
4005 op_function(oap); // call 'operatorfunc'
4006 break;
4007
4008 case OP_INSERT:
4009 case OP_APPEND:
4010 VIsual_reselect = FALSE; // don't reselect now
4011 if (empty_region_error)
4012 {
4013 vim_beep(BO_OPER);
4014 CancelRedo();
4015 }
4016 else
4017 {
4018 // This is a new edit command, not a restart. Need to
4019 // remember it to make 'insertmode' work with mappings for
4020 // Visual mode. But do this only once.
4021 restart_edit_save = restart_edit;
4022 restart_edit = 0;
4023#ifdef FEAT_LINEBREAK
4024 // Restore linebreak, so that when the user edits it looks as
4025 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004026 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004027#endif
4028 op_insert(oap, cap->count1);
4029#ifdef FEAT_LINEBREAK
4030 // Reset linebreak, so that formatting works correctly.
4031 curwin->w_p_lbr = FALSE;
4032#endif
4033
4034 // TODO: when inserting in several lines, should format all
4035 // the lines.
4036 auto_format(FALSE, TRUE);
4037
4038 if (restart_edit == 0)
4039 restart_edit = restart_edit_save;
4040 else
4041 cap->retval |= CA_COMMAND_BUSY;
4042 }
4043 break;
4044
4045 case OP_REPLACE:
4046 VIsual_reselect = FALSE; // don't reselect now
4047 if (empty_region_error)
4048 {
4049 vim_beep(BO_OPER);
4050 CancelRedo();
4051 }
4052 else
4053 {
4054#ifdef FEAT_LINEBREAK
4055 // Restore linebreak, so that when the user edits it looks as
4056 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004057 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004058#endif
4059 op_replace(oap, cap->nchar);
4060 }
4061 break;
4062
4063#ifdef FEAT_FOLDING
4064 case OP_FOLD:
4065 VIsual_reselect = FALSE; // don't reselect now
4066 foldCreate(oap->start.lnum, oap->end.lnum);
4067 break;
4068
4069 case OP_FOLDOPEN:
4070 case OP_FOLDOPENREC:
4071 case OP_FOLDCLOSE:
4072 case OP_FOLDCLOSEREC:
4073 VIsual_reselect = FALSE; // don't reselect now
4074 opFoldRange(oap->start.lnum, oap->end.lnum,
4075 oap->op_type == OP_FOLDOPEN
4076 || oap->op_type == OP_FOLDOPENREC,
4077 oap->op_type == OP_FOLDOPENREC
4078 || oap->op_type == OP_FOLDCLOSEREC,
4079 oap->is_VIsual);
4080 break;
4081
4082 case OP_FOLDDEL:
4083 case OP_FOLDDELREC:
4084 VIsual_reselect = FALSE; // don't reselect now
4085 deleteFold(oap->start.lnum, oap->end.lnum,
4086 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4087 break;
4088#endif
4089 case OP_NR_ADD:
4090 case OP_NR_SUB:
4091 if (empty_region_error)
4092 {
4093 vim_beep(BO_OPER);
4094 CancelRedo();
4095 }
4096 else
4097 {
4098 VIsual_active = TRUE;
4099#ifdef FEAT_LINEBREAK
4100 curwin->w_p_lbr = lbr_saved;
4101#endif
4102 op_addsub(oap, cap->count1, redo_VIsual_arg);
4103 VIsual_active = FALSE;
4104 }
4105 check_cursor_col();
4106 break;
4107 default:
4108 clearopbeep(oap);
4109 }
4110 virtual_op = MAYBE;
4111 if (!gui_yank)
4112 {
4113 // if 'sol' not set, go back to old column for some commands
4114 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4115 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4116 || oap->op_type == OP_DELETE))
4117 {
4118#ifdef FEAT_LINEBREAK
4119 curwin->w_p_lbr = FALSE;
4120#endif
4121 coladvance(curwin->w_curswant = old_col);
4122 }
4123 }
4124 else
4125 {
4126 curwin->w_cursor = old_cursor;
4127 }
4128 oap->block_mode = FALSE;
4129 clearop(oap);
4130 motion_force = NUL;
4131 }
4132#ifdef FEAT_LINEBREAK
4133 curwin->w_p_lbr = lbr_saved;
4134#endif
4135}