blob: cc25683072ab0d7df1be94b17756654e770feb86 [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,
12 * op_change, op_yank, do_put, do_join
13 */
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 }
941 }
942
943 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
944
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000945setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200946 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100948 if (oap->block_mode)
949 {
950 curbuf->b_op_end.lnum = oap->end.lnum;
951 curbuf->b_op_end.col = oap->start.col;
952 }
953 else
954 curbuf->b_op_end = oap->start;
955 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 return OK;
959}
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961/*
962 * Adjust end of operating area for ending on a multi-byte character.
963 * Used for deletion.
964 */
965 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100966mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968 char_u *p;
969
970 if (oap->inclusive)
971 {
972 p = ml_get(oap->end.lnum);
973 oap->end.col += mb_tail_off(p, p + oap->end.col);
974 }
975}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
Bram Moolenaar630afe82018-06-28 19:26:28 +0200977/*
978 * Replace the character under the cursor with "c".
979 * This takes care of multi-byte characters.
980 */
981 static void
982replace_character(int c)
983{
984 int n = State;
985
986 State = REPLACE;
987 ins_char(c);
988 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200990 dec_cursor();
991}
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993/*
994 * Replace a whole area with one character.
995 */
996 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100997op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
999 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 char_u *newp, *oldp;
1002 size_t oldlen;
1003 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001004 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001005 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001008 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001010 if (c == REPLACE_CR_NCHAR)
1011 {
1012 had_ctrl_v_cr = TRUE;
1013 c = CAR;
1014 }
1015 else if (c == REPLACE_NL_NCHAR)
1016 {
1017 had_ctrl_v_cr = TRUE;
1018 c = NL;
1019 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (has_mbyte)
1022 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 if (u_save((linenr_T)(oap->start.lnum - 1),
1025 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1026 return FAIL;
1027
1028 /*
1029 * block mode replace
1030 */
1031 if (oap->block_mode)
1032 {
1033 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1034 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001036 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1038 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001039 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001041 // n == number of extra chars required
1042 // If we split a TAB, it may be replaced by several characters.
1043 // Thus the number of characters may increase!
1044 // If the range starts in virtual space, count the initial
1045 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1047 {
1048 pos_T vpos;
1049
Bram Moolenaara1381de2009-11-03 15:44:21 +00001050 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 getvpos(&vpos, oap->start_vcol);
1052 bd.startspaces += vpos.coladd;
1053 n = bd.startspaces;
1054 }
1055 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001056 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1058
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001059 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 numc = oap->end_vcol - oap->start_vcol + 1;
1065 if (bd.is_short && (!virtual_op || bd.is_MAX))
1066 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 // A double-wide character can be replaced only up to half the
1069 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if ((*mb_char2cells)(c) > 1)
1071 {
1072 if ((numc & 1) && !bd.is_short)
1073 {
1074 ++bd.endspaces;
1075 ++n;
1076 }
1077 numc = numc / 2;
1078 }
1079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001080 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 num_chars = numc;
1082 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001083 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 n += numc - bd.textlen;
1085
1086 oldp = ml_get_curline();
1087 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001088 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (newp == NULL)
1090 continue;
1091 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001092 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 mch_memmove(newp, oldp, (size_t)bd.textcol);
1094 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001095 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001096 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001097 // insert replacement chars CHECK FOR ALLOCATED SPACE
1098 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1099 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001100 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001102 if (has_mbyte)
1103 {
1104 n = (int)STRLEN(newp);
1105 while (--num_chars >= 0)
1106 n += (*mb_char2bytes)(c, newp + n);
1107 }
1108 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001109 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001110 if (!bd.is_short)
1111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001112 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001113 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001115 STRMOVE(newp + STRLEN(newp), oldp);
1116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001120 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001121 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001122 if (after_p != NULL)
1123 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001125 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001127 if (after_p != NULL)
1128 {
1129 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1130 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1131 oap->end.lnum++;
1132 vim_free(after_p);
1133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 }
1136 else
1137 {
1138 /*
1139 * MCHAR and MLINE motion replace.
1140 */
1141 if (oap->motion_type == MLINE)
1142 {
1143 oap->start.col = 0;
1144 curwin->w_cursor.col = 0;
1145 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1146 if (oap->end.col)
1147 --oap->end.col;
1148 }
1149 else if (!oap->inclusive)
1150 dec(&(oap->end));
1151
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001152 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 n = gchar_cursor();
1155 if (n != NUL)
1156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1158 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001159 // This is slow, but it handles replacing a single-byte
1160 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001161 if (curwin->w_cursor.lnum == oap->end.lnum)
1162 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001163 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (n == TAB)
1168 {
1169 int end_vcol = 0;
1170
1171 if (curwin->w_cursor.lnum == oap->end.lnum)
1172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001173 // oap->end has to be recalculated when
1174 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 end_vcol = getviscol2(oap->end.col,
1176 oap->end.coladd);
1177 }
1178 coladvance_force(getviscol());
1179 if (curwin->w_cursor.lnum == oap->end.lnum)
1180 getvpos(&oap->end, end_vcol);
1181 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001182 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1186 {
1187 int virtcols = oap->end.coladd;
1188
1189 if (curwin->w_cursor.lnum == oap->start.lnum
1190 && oap->start.col == oap->end.col && oap->start.coladd)
1191 virtcols -= oap->start.coladd;
1192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001193 // oap->end has been trimmed so it's effectively inclusive;
1194 // as a result an extra +1 must be counted so we don't
1195 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1197 curwin->w_cursor.col -= (virtcols + 1);
1198 for (; virtcols >= 0; virtcols--)
1199 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001200 if ((*mb_char2len)(c) > 1)
1201 replace_character(c);
1202 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001203 PBYTE(curwin->w_cursor, c);
1204 if (inc(&curwin->w_cursor) == -1)
1205 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001209 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (inc_cursor() == -1)
1211 break;
1212 }
1213 }
1214
1215 curwin->w_cursor = oap->start;
1216 check_cursor();
1217 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1218
Bram Moolenaare1004402020-10-24 20:49:43 +02001219 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001221 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001222 curbuf->b_op_start = oap->start;
1223 curbuf->b_op_end = oap->end;
1224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 return OK;
1227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001229static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231/*
1232 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1233 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001234 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001235op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
1237 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001239 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241 if (u_save((linenr_T)(oap->start.lnum - 1),
1242 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1243 return;
1244
1245 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001246 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1249 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001250 int one_change;
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 block_prep(oap, &bd, pos.lnum, FALSE);
1253 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001254 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1255 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001256
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001257#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001258 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
1260 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1261
Bram Moolenaar009b2592004-10-24 19:18:58 +00001262 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1263 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001265 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 if (did_change)
1270 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1271 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001272 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
1274 if (oap->motion_type == MLINE)
1275 {
1276 oap->start.col = 0;
1277 pos.col = 0;
1278 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1279 if (oap->end.col)
1280 --oap->end.col;
1281 }
1282 else if (!oap->inclusive)
1283 dec(&(oap->end));
1284
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001285 if (pos.lnum == oap->end.lnum)
1286 did_change = swapchars(oap->op_type, &pos,
1287 oap->end.col - pos.col + 1);
1288 else
1289 for (;;)
1290 {
1291 did_change |= swapchars(oap->op_type, &pos,
1292 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1293 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001294 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001295 break;
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if (did_change)
1298 {
1299 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1300 0L);
1301#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001302 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
1304 char_u *ptr;
1305 int count;
1306
1307 pos = oap->start;
1308 while (pos.lnum < oap->end.lnum)
1309 {
1310 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001311 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001312 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001314 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 pos.col = 0;
1316 pos.lnum++;
1317 }
1318 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1319 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001320 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001322 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324#endif
1325 }
1326 }
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001329 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaare1004402020-10-24 20:49:43 +02001332 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001333 {
1334 // Set '[ and '] marks.
1335 curbuf->b_op_start = oap->start;
1336 curbuf->b_op_end = oap->end;
1337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001340 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001341 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342}
1343
1344/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001345 * Invoke swapchar() on "length" bytes at position "pos".
1346 * "pos" is advanced to just after the changed characters.
1347 * "length" is rounded up to include the whole last multi-byte character.
1348 * Also works correctly when the number of bytes changes.
1349 * Returns TRUE if some character was changed.
1350 */
1351 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001352swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001353{
1354 int todo;
1355 int did_change = 0;
1356
1357 for (todo = length; todo > 0; --todo)
1358 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001359 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001360 {
1361 int len = (*mb_ptr2len)(ml_get_pos(pos));
1362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001363 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001364 if (len > 0)
1365 todo -= len - 1;
1366 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001367 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001368 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001369 break;
1370 }
1371 return did_change;
1372}
1373
1374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 * If op_type == OP_UPPER: make uppercase,
1376 * if op_type == OP_LOWER: make lowercase,
1377 * if op_type == OP_ROT13: do rot13 encoding,
1378 * else swap case of character at 'pos'
1379 * returns TRUE when something actually changed.
1380 */
1381 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001382swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 int c;
1385 int nc;
1386
1387 c = gchar_pos(pos);
1388
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001389 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (c >= 0x80 && op_type == OP_ROT13)
1391 return FALSE;
1392
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001393 if (op_type == OP_UPPER && c == 0xdf
1394 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001395 {
1396 pos_T sp = curwin->w_cursor;
1397
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001398 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001399 curwin->w_cursor = *pos;
1400 del_char(FALSE);
1401 ins_char('S');
1402 ins_char('S');
1403 curwin->w_cursor = sp;
1404 inc(pos);
1405 }
1406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001407 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 nc = c;
1410 if (MB_ISLOWER(c))
1411 {
1412 if (op_type == OP_ROT13)
1413 nc = ROT13(c, 'a');
1414 else if (op_type != OP_LOWER)
1415 nc = MB_TOUPPER(c);
1416 }
1417 else if (MB_ISUPPER(c))
1418 {
1419 if (op_type == OP_ROT13)
1420 nc = ROT13(c, 'A');
1421 else if (op_type != OP_UPPER)
1422 nc = MB_TOLOWER(c);
1423 }
1424 if (nc != c)
1425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1427 {
1428 pos_T sp = curwin->w_cursor;
1429
1430 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001431 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001432 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 ins_char(nc);
1434 curwin->w_cursor = sp;
1435 }
1436 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001437 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 return TRUE;
1439 }
1440 return FALSE;
1441}
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443/*
1444 * op_insert - Insert and append operators for Visual mode.
1445 */
1446 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001447op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 long ins_len, pre_textlen = 0;
1450 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001451 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 struct block_def bd;
1453 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001454 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001456 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001459 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 curwin->w_cursor.lnum = oap->start.lnum;
1461 update_screen(INVERTED);
1462
1463 if (oap->block_mode)
1464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001465 // When 'virtualedit' is used, need to insert the extra spaces before
1466 // doing block_prep(). When only "block" is used, virtual edit is
1467 // already disabled, but still need it when calling
1468 // coladvance_force().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (curwin->w_cursor.coladd > 0)
1470 {
1471 int old_ve_flags = ve_flags;
1472
1473 ve_flags = VE_ALL;
1474 if (u_save_cursor() == FAIL)
1475 return;
1476 coladvance_force(oap->op_type == OP_APPEND
1477 ? oap->end_vcol + 1 : getviscol());
1478 if (oap->op_type == OP_APPEND)
1479 --curwin->w_cursor.col;
1480 ve_flags = old_ve_flags;
1481 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001482 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001484 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001485 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (oap->op_type == OP_APPEND)
1489 firstline += bd.textlen;
1490 pre_textlen = (long)STRLEN(firstline);
1491 }
1492
1493 if (oap->op_type == OP_APPEND)
1494 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001495 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001497 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 curwin->w_set_curswant = TRUE;
1499 while (*ml_get_cursor() != NUL
1500 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1501 ++curwin->w_cursor.col;
1502 if (bd.is_short && !bd.is_MAX)
1503 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001504 // First line was too short, make it longer and adjust the
1505 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if (u_save_cursor() == FAIL)
1507 return;
1508 for (i = 0; i < bd.endspaces; ++i)
1509 ins_char(' ');
1510 bd.textlen += bd.endspaces;
1511 }
1512 }
1513 else
1514 {
1515 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001516 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001518 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001519 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 && oap->start_vcol != oap->end_vcol)
1521 inc_cursor();
1522 }
1523 }
1524
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001525 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001526 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 // When a tab was inserted, and the characters in front of the tab
1529 // have been converted to a tab as well, the column of the cursor
1530 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001531 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001532 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001533 oap->start = curbuf->b_op_start_orig;
1534
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001535 // If user has moved off this line, we don't know what to do, so do
1536 // nothing.
1537 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001538 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 return;
1540
1541 if (oap->block_mode)
1542 {
1543 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001544 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001545 size_t len;
1546 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001548 // If indent kicked in, the firstline might have changed
1549 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001550 ind_post = (colnr_T)getwhitecols_curline();
1551 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1552 {
1553 bd.textcol += ind_post - ind_pre;
1554 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001555 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001556 }
1557
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001558 // The user may have moved the cursor before inserting something, try
1559 // to adjust the block for that. But only do it, if the difference
1560 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001561 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1562 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001563 {
1564 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001565 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001566 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001567 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001568 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001569 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001570 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001571 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001572 pre_textlen -= t - oap->start_vcol;
1573 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001574 }
1575 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001576 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001577 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001578 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001579 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001580 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001581 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001582 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001583 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001584 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001585 pre_textlen -= t - oap->start_vcol;
1586 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001587 oap->op_type = OP_INSERT;
1588 }
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /*
1592 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001593 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * Don't do this when "$" used, end-of-line will have changed.
1595 */
1596 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1597 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1598 {
1599 if (oap->op_type == OP_APPEND)
1600 {
1601 pre_textlen += bd2.textlen - bd.textlen;
1602 if (bd2.endspaces)
1603 --bd2.textlen;
1604 }
1605 bd.textcol = bd2.textcol;
1606 bd.textlen = bd2.textlen;
1607 }
1608
1609 /*
1610 * Subsequent calls to ml_get() flush the firstline data - take a
1611 * copy of the required string.
1612 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001613 firstline = ml_get(oap->start.lnum);
1614 len = STRLEN(firstline);
1615 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001617 add += bd.textlen;
1618 if ((size_t)add > len)
1619 firstline += len; // short line, point to the NUL
1620 else
1621 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001622 if (pre_textlen >= 0
1623 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001625 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (ins_text != NULL)
1627 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001628 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (u_save(oap->start.lnum,
1630 (linenr_T)(oap->end.lnum + 1)) == OK)
1631 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1632 &bd);
1633
1634 curwin->w_cursor.col = oap->start.col;
1635 check_cursor();
1636 vim_free(ins_text);
1637 }
1638 }
1639 }
1640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642/*
1643 * op_change - handle a change operation
1644 *
1645 * return TRUE if edit() returns because of a CTRL-O command
1646 */
1647 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001648op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649{
1650 colnr_T l;
1651 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 long offset;
1653 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001654 long ins_len;
1655 long pre_textlen = 0;
1656 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 char_u *firstline;
1658 char_u *ins_text, *newp, *oldp;
1659 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
1661 l = oap->start.col;
1662 if (oap->motion_type == MLINE)
1663 {
1664 l = 0;
1665#ifdef FEAT_SMARTINDENT
1666 if (!p_paste && curbuf->b_p_si
1667# ifdef FEAT_CINDENT
1668 && !curbuf->b_p_cin
1669# endif
1670 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001671 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
1673 }
1674
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001675 // First delete the text in the region. In an empty buffer only need to
1676 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1678 {
1679 if (u_save_cursor() == FAIL)
1680 return FALSE;
1681 }
1682 else if (op_delete(oap) == FAIL)
1683 return FALSE;
1684
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001685 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 && !virtual_op)
1687 inc_cursor();
1688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001689 // check for still on same line (<CR> in inserted text meaningless)
1690 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (oap->block_mode)
1692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001693 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (virtual_op && (curwin->w_cursor.coladd > 0
1695 || gchar_cursor() == NUL))
1696 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001697 firstline = ml_get(oap->start.lnum);
1698 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001699 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 bd.textcol = curwin->w_cursor.col;
1701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1704 if (oap->motion_type == MLINE)
1705 fix_indent();
1706#endif
1707
1708 retval = edit(NUL, FALSE, (linenr_T)1);
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001711 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001713 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001715 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001717 // Auto-indenting may have changed the indent. If the cursor was past
1718 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001720 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001722 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001723
1724 pre_textlen += new_indent - pre_indent;
1725 bd.textcol += new_indent - pre_indent;
1726 }
1727
1728 ins_len = (long)STRLEN(firstline) - pre_textlen;
1729 if (ins_len > 0)
1730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001731 // Subsequent calls to ml_get() flush the firstline data - take a
1732 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001733 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001735 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1737 linenr++)
1738 {
1739 block_prep(oap, &bd, linenr, TRUE);
1740 if (!bd.is_short || virtual_op)
1741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 pos_T vpos;
1743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001744 // If the block starts in virtual space, count the
1745 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (bd.is_short)
1747 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001748 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
1751 else
1752 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001754 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (newp == NULL)
1756 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001757 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 mch_memmove(newp, oldp, (size_t)bd.textcol);
1759 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001760 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1763 offset += ins_len;
1764 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001765 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 ml_replace(linenr, newp, FALSE);
1767 }
1768 }
1769 check_cursor();
1770
1771 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1772 }
1773 vim_free(ins_text);
1774 }
1775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 return retval;
1778}
1779
1780/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001781 * When the cursor is on the NUL past the end of the line and it should not be
1782 * there move it left.
1783 */
1784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001785adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001786{
1787 if (curwin->w_cursor.col > 0
1788 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001789 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 && !(restart_edit || (State & INSERT)))
1791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001792 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001793 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001796 {
1797 colnr_T scol, ecol;
1798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001800 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1801 curwin->w_cursor.coladd = ecol - scol + 1;
1802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804}
1805
Bram Moolenaar81340392012-06-06 16:12:59 +02001806/*
1807 * If "process" is TRUE and the line begins with a comment leader (possibly
1808 * after some white space), return a pointer to the text after it. Put a boolean
1809 * value indicating whether the line ends with an unclosed comment in
1810 * "is_comment".
1811 * line - line to be processed,
1812 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001813 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001814 * include_space - whether to also skip space following the comment leader,
1815 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001816 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001817 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001818 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001819skip_comment(
1820 char_u *line,
1821 int process,
1822 int include_space,
1823 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001824{
1825 char_u *comment_flags = NULL;
1826 int lead_len;
1827 int leader_offset = get_last_leader_offset(line, &comment_flags);
1828
1829 *is_comment = FALSE;
1830 if (leader_offset != -1)
1831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001832 // Let's check whether the line ends with an unclosed comment.
1833 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001834 while (*comment_flags)
1835 {
1836 if (*comment_flags == COM_END
1837 || *comment_flags == ':')
1838 break;
1839 ++comment_flags;
1840 }
1841 if (*comment_flags != COM_END)
1842 *is_comment = TRUE;
1843 }
1844
1845 if (process == FALSE)
1846 return line;
1847
1848 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1849
1850 if (lead_len == 0)
1851 return line;
1852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001853 // Find:
1854 // - COM_END,
1855 // - colon,
1856 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001857 while (*comment_flags)
1858 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001859 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001860 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001861 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 ++comment_flags;
1863 }
1864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001865 // If we found a colon, it means that we are not processing a line
1866 // starting with a closing part of a three-part comment. That's good,
1867 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001868 if (*comment_flags == ':' || *comment_flags == NUL)
1869 line += lead_len;
1870
1871 return line;
1872}
Bram Moolenaar81340392012-06-06 16:12:59 +02001873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001875 * Join 'count' lines (minimal 2) at cursor position.
1876 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001877 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1878 * leaders should not be removed.
1879 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1880 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001882 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 */
1884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001885do_join(
1886 long count,
1887 int insert_space,
1888 int save_undo,
1889 int use_formatoptions UNUSED,
1890 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001892 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001893 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001894 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001896 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001897 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001898 int endcurr1 = NUL;
1899 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001900 int currsize = 0; // size of the current line
1901 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001903 colnr_T col = 0;
1904 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001905 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001906 int remove_comments = (use_formatoptions == TRUE)
1907 && has_format_option(FO_REMOVE_COMS);
1908 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001909#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001910 int propcount = 0; // number of props over all joined lines
1911 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001914 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1915 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // Allocate an array to store the number of spaces inserted before each
1919 // line. We will use it to pre-compute the length of the new line and the
1920 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001921 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001922 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001924 if (remove_comments)
1925 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001926 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001927 if (comments == NULL)
1928 {
1929 vim_free(spaces);
1930 return FAIL;
1931 }
1932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
1934 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001935 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001936 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001937 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001939 for (t = 0; t < count; ++t)
1940 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001941 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001942#ifdef FEAT_PROP_POPUP
1943 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1944#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001945 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001946 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001948 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1949 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1950 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001951 if (remove_comments)
1952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001953 // We don't want to remove the comment leader if the
1954 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001955 if (t > 0 && prev_was_comment)
1956 {
1957
1958 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1959 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001960 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001961 curr = new_curr;
1962 }
1963 else
1964 curr = skip_comment(curr, FALSE, insert_space,
1965 &prev_was_comment);
1966 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001967
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001968 if (insert_space && t > 0)
1969 {
1970 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01001971 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01001972 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001973 && (!has_format_option(FO_MBYTE_JOIN)
1974 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
1975 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02001976 || (mb_ptr2char(curr) < 0x100
1977 && !(enc_utf8 && utf_eat_space(endcurr1)))
1978 || (endcurr1 < 0x100
1979 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001980 )
1981 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001982 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001983 if (endcurr1 == ' ')
1984 endcurr1 = endcurr2;
1985 else
1986 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001987 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001988 if ( p_js
1989 && (endcurr1 == '.'
1990 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
1991 && (endcurr1 == '?' || endcurr1 == '!'))))
1992 ++spaces[t];
1993 }
1994 }
1995 currsize = (int)STRLEN(curr);
1996 sumsize += currsize + spaces[t];
1997 endcurr1 = endcurr2 = NUL;
1998 if (insert_space && currsize > 0)
1999 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002000 if (has_mbyte)
2001 {
2002 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002003 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002004 endcurr1 = (*mb_ptr2char)(cend);
2005 if (cend > curr)
2006 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002007 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002008 endcurr2 = (*mb_ptr2char)(cend);
2009 }
2010 }
2011 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002012 {
2013 endcurr1 = *(curr + currsize - 1);
2014 if (currsize > 1)
2015 endcurr2 = *(curr + currsize - 2);
2016 }
2017 }
2018 line_breakcheck();
2019 if (got_int)
2020 {
2021 ret = FAIL;
2022 goto theend;
2023 }
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002026 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002027 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002029 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002030 newp_len = sumsize + 1;
2031#ifdef FEAT_PROP_POPUP
2032 newp_len += propcount * sizeof(textprop_T);
2033#endif
2034 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002035 if (newp == NULL)
2036 {
2037 ret = FAIL;
2038 goto theend;
2039 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002040 cend = newp + sumsize;
2041 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002043 /*
2044 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002045 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002046 *
2047 * Move marks from each deleted line to the joined line, adjusting the
2048 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2049 * should not really be a problem.
2050 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002051#ifdef FEAT_PROP_POPUP
2052 props_remaining = propcount;
2053#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002054 for (t = count - 1; ; --t)
2055 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002056 int spaces_removed;
2057
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002058 cend -= currsize;
2059 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002060
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002061 if (spaces[t] > 0)
2062 {
2063 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002064 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002066
2067 // If deleting more spaces than adding, the cursor moves no more than
2068 // what is added if it is inside these spaces.
2069 spaces_removed = (curr - curr_start) - spaces[t];
2070
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002071 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002072 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002073#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002074 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2075 curwin->w_cursor.lnum + t, t == count - 1,
2076 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002077#endif
2078
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002079 if (t == 0)
2080 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002081 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002082 if (remove_comments)
2083 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002084 if (insert_space && t > 1)
2085 curr = skipwhite(curr);
2086 currsize = (int)STRLEN(curr);
2087 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002088
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002089 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
Bram Moolenaare1004402020-10-24 20:49:43 +02002091 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002092 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002093 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002094 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002095 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002096 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002098 // Only report the change in the first line here, del_lines() will report
2099 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 changed_lines(curwin->w_cursor.lnum, currsize,
2101 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002103 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 * briefly, and then move it back. After del_lines() the cursor may
2105 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 */
2107 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002109 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 curwin->w_cursor.lnum = t;
2111
2112 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002113 * Set the cursor column:
2114 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002115 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002117 curwin->w_cursor.col =
2118 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 curwin->w_set_curswant = TRUE;
2123
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002124theend:
2125 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002126 if (remove_comments)
2127 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002128 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129}
2130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 * prepare a few things for block mode yank/delete/tilde
2133 *
2134 * for delete:
2135 * - textlen includes the first/last char to be (partly) deleted
2136 * - start/endspaces is the number of columns that are taken by the
2137 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002138 * deleted.
2139 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 * - textlen includes the first/last char to be wholly yanked
2141 * - start/endspaces is the number of columns of the first/last yanked char
2142 * that are to be yanked.
2143 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002144 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002145block_prep(
2146 oparg_T *oap,
2147 struct block_def *bdp,
2148 linenr_T lnum,
2149 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150{
2151 int incr = 0;
2152 char_u *pend;
2153 char_u *pstart;
2154 char_u *line;
2155 char_u *prev_pstart;
2156 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002157#ifdef FEAT_LINEBREAK
2158 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002160 // Avoid a problem with unwanted linebreaks in block mode.
2161 curwin->w_p_lbr = FALSE;
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 bdp->startspaces = 0;
2164 bdp->endspaces = 0;
2165 bdp->textlen = 0;
2166 bdp->start_vcol = 0;
2167 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 bdp->is_short = FALSE;
2169 bdp->is_oneChar = FALSE;
2170 bdp->pre_whitesp = 0;
2171 bdp->pre_whitesp_c = 0;
2172 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 bdp->start_char_vcols = 0;
2174
2175 line = ml_get(lnum);
2176 pstart = line;
2177 prev_pstart = line;
2178 while (bdp->start_vcol < oap->start_vcol && *pstart)
2179 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002180 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002181 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002183 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 bdp->pre_whitesp += incr;
2186 bdp->pre_whitesp_c++;
2187 }
2188 else
2189 {
2190 bdp->pre_whitesp = 0;
2191 bdp->pre_whitesp_c = 0;
2192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002194 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
2196 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002197 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
2199 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (!is_del || oap->op_type == OP_APPEND)
2202 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2203 }
2204 else
2205 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 // notice: this converts partly selected Multibyte characters to
2207 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2209 if (is_del && bdp->startspaces)
2210 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2211 pend = pstart;
2212 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002213 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if (oap->op_type == OP_INSERT)
2217 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2218 else if (oap->op_type == OP_APPEND)
2219 {
2220 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2221 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2222 }
2223 else
2224 {
2225 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2226 if (is_del && oap->op_type != OP_LSHIFT)
2227 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002228 // just putting the sum of those two into
2229 // bdp->startspaces doesn't work for Visual replace,
2230 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 bdp->startspaces = bdp->start_char_vcols
2232 - (bdp->start_vcol - oap->start_vcol);
2233 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2234 }
2235 }
2236 }
2237 else
2238 {
2239 prev_pend = pend;
2240 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2241 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002242 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002244 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 bdp->end_vcol += incr;
2246 }
2247 if (bdp->end_vcol <= oap->end_vcol
2248 && (!is_del
2249 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002250 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002253 // Alternative: include spaces to fill up the block.
2254 // Disadvantage: can lead to trailing spaces when the line is
2255 // short where the text is put
2256 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 if (oap->op_type == OP_APPEND || virtual_op)
2258 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002259 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002261 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263 else if (bdp->end_vcol > oap->end_vcol)
2264 {
2265 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2266 if (!is_del && bdp->endspaces)
2267 {
2268 bdp->endspaces = incr - bdp->endspaces;
2269 if (pend != pstart)
2270 pend = prev_pend;
2271 }
2272 }
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (is_del && bdp->startspaces)
2276 pstart = prev_pstart;
2277 bdp->textlen = (int)(pend - pstart);
2278 }
2279 bdp->textcol = (colnr_T) (pstart - line);
2280 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002281#ifdef FEAT_LINEBREAK
2282 curwin->w_p_lbr = lbr_saved;
2283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002287 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002289 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002290op_addsub(
2291 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002292 linenr_T Prenum1, // Amount of add/subtract
2293 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002295 pos_T pos;
2296 struct block_def bd;
2297 int change_cnt = 0;
2298 linenr_T amount = Prenum1;
2299
Bram Moolenaar6b731882018-11-16 20:54:47 +01002300 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002301 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002302 // the call to changed_lines().
2303#ifdef FEAT_FOLDING
2304 disable_fold_update++;
2305#endif
2306
Bram Moolenaard79e5502016-01-10 22:13:02 +01002307 if (!VIsual_active)
2308 {
2309 pos = curwin->w_cursor;
2310 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002311 {
2312#ifdef FEAT_FOLDING
2313 disable_fold_update--;
2314#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002315 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002316 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002317 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002318#ifdef FEAT_FOLDING
2319 disable_fold_update--;
2320#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002321 if (change_cnt)
2322 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2323 }
2324 else
2325 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002326 int one_change;
2327 int length;
2328 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002329
2330 if (u_save((linenr_T)(oap->start.lnum - 1),
2331 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002332 {
2333#ifdef FEAT_FOLDING
2334 disable_fold_update--;
2335#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002336 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002337 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002338
2339 pos = oap->start;
2340 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2341 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002343 {
2344 block_prep(oap, &bd, pos.lnum, FALSE);
2345 pos.col = bd.textcol;
2346 length = bd.textlen;
2347 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002348 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002349 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002350 curwin->w_cursor.col = 0;
2351 pos.col = 0;
2352 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2353 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002354 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002355 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002356 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002357 dec(&(oap->end));
2358 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2359 pos.col = 0;
2360 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002361 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002362 pos.col += oap->start.col;
2363 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002364 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002365 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002366 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002367 length = (int)STRLEN(ml_get(oap->end.lnum));
2368 if (oap->end.col >= length)
2369 oap->end.col = length - 1;
2370 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002371 }
2372 }
2373 one_change = do_addsub(oap->op_type, &pos, length, amount);
2374 if (one_change)
2375 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002376 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002377 if (change_cnt == 0)
2378 startpos = curbuf->b_op_start;
2379 ++change_cnt;
2380 }
2381
2382#ifdef FEAT_NETBEANS_INTG
2383 if (netbeans_active() && one_change)
2384 {
2385 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2386
2387 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2388 netbeans_inserted(curbuf, pos.lnum, pos.col,
2389 &ptr[pos.col], length);
2390 }
2391#endif
2392 if (g_cmd && one_change)
2393 amount += Prenum1;
2394 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002395
2396#ifdef FEAT_FOLDING
2397 disable_fold_update--;
2398#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002399 if (change_cnt)
2400 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2401
2402 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002403 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002404 redraw_curbuf_later(INVERTED);
2405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002406 // Set '[ mark if something changed. Keep the last end
2407 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002408 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002409 curbuf->b_op_start = startpos;
2410
2411 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002412 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002413 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002414 }
2415}
2416
2417/*
2418 * Add or subtract 'Prenum1' from a number in a line
2419 * op_type is OP_NR_ADD or OP_NR_SUB
2420 *
2421 * Returns TRUE if some character was changed.
2422 */
2423 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002424do_addsub(
2425 int op_type,
2426 pos_T *pos,
2427 int length,
2428 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002429{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 int col;
2431 char_u *buf1;
2432 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002433 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2434 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002435 uvarnumber_T n;
2436 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 char_u *ptr;
2438 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002440 int do_hex;
2441 int do_oct;
2442 int do_bin;
2443 int do_alpha;
2444 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002447 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002448 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002449 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002450 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002451 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002452 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002453 pos_T startpos;
2454 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002455 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002457 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2458 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2459 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2460 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2461 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002463 if (virtual_active())
2464 {
2465 save_coladd = pos->coladd;
2466 pos->coladd = 0;
2467 }
2468
Bram Moolenaard79e5502016-01-10 22:13:02 +01002469 curwin->w_cursor = *pos;
2470 ptr = ml_get(pos->lnum);
2471 col = pos->col;
2472
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002473 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002474 goto theend;
2475
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /*
2477 * First check if we are on a hexadecimal number, after the "0x".
2478 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002479 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002481 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002482 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002483 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002484 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002485 if (has_mbyte)
2486 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002487 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002488
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002489 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002490 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002491 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002492 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002493 if (has_mbyte)
2494 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002495 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002496
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002497 if ( do_bin
2498 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002499 && ! ((col > 0
2500 && (ptr[col] == 'X'
2501 || ptr[col] == 'x')
2502 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002503 && (!has_mbyte ||
2504 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002505 && vim_isxdigit(ptr[col + 1]))))
2506 {
2507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002508 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002509
Bram Moolenaard79e5502016-01-10 22:13:02 +01002510 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002511
2512 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002513 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002514 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002515 if (has_mbyte)
2516 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002517 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002518 }
2519
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002520 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002521 && col > 0
2522 && (ptr[col] == 'X'
2523 || ptr[col] == 'x')
2524 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002525 && (!has_mbyte ||
2526 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002527 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002528 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002529 && col > 0
2530 && (ptr[col] == 'B'
2531 || ptr[col] == 'b')
2532 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002533 && (!has_mbyte ||
2534 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002535 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002536 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002537 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002538 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002539 if (has_mbyte)
2540 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002541 }
2542 else
2543 {
2544 /*
2545 * Search forward and then backward to find the start of number.
2546 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002547 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002548
2549 while (ptr[col] != NUL
2550 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002551 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002552 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002553
2554 while (col > 0
2555 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002556 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002557 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002558 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002559 if (has_mbyte)
2560 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002561 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002562 }
2563 }
2564
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002565 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002566 {
2567 while (ptr[col] != NUL && length > 0
2568 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002569 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002570 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002571 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002572
2573 col += mb_len;
2574 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002575 }
2576
2577 if (length == 0)
2578 goto theend;
2579
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002580 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002581 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2582 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002583 {
2584 negative = TRUE;
2585 was_positive = FALSE;
2586 }
2587 }
2588
2589 /*
2590 * If a number was found, and saving for undo works, replace the number.
2591 */
2592 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002593 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002594 {
2595 beep_flush();
2596 goto theend;
2597 }
2598
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002599 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002601 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002602 if (op_type == OP_NR_SUB)
2603 {
2604 if (CharOrd(firstdigit) < Prenum1)
2605 {
2606 if (isupper(firstdigit))
2607 firstdigit = 'A';
2608 else
2609 firstdigit = 'a';
2610 }
2611 else
2612#ifdef EBCDIC
2613 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2614#else
2615 firstdigit -= Prenum1;
2616#endif
2617 }
2618 else
2619 {
2620 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2621 {
2622 if (isupper(firstdigit))
2623 firstdigit = 'Z';
2624 else
2625 firstdigit = 'z';
2626 }
2627 else
2628#ifdef EBCDIC
2629 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2630#else
2631 firstdigit += Prenum1;
2632#endif
2633 }
2634 curwin->w_cursor.col = col;
2635 if (!did_change)
2636 startpos = curwin->w_cursor;
2637 did_change = TRUE;
2638 (void)del_char(FALSE);
2639 ins_char(firstdigit);
2640 endpos = curwin->w_cursor;
2641 curwin->w_cursor.col = col;
2642 }
2643 else
2644 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002645 pos_T save_pos;
2646 int i;
2647
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002648 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002649 && (!has_mbyte ||
2650 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002651 && !visual
2652 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002653 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002654 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002655 --col;
2656 negative = TRUE;
2657 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002659 if (visual && VIsual_mode != 'V')
2660 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2661 ? (int)STRLEN(ptr) - col
2662 : length);
2663
2664 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002665 0 + (do_bin ? STR2NR_BIN : 0)
2666 + (do_oct ? STR2NR_OCT : 0)
2667 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002668 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002669
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002671 if (pre && negative)
2672 {
2673 ++col;
2674 --length;
2675 negative = FALSE;
2676 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002678 subtract = FALSE;
2679 if (op_type == OP_NR_SUB)
2680 subtract ^= TRUE;
2681 if (negative)
2682 subtract ^= TRUE;
2683
2684 oldn = n;
2685 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002686 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002687 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002688 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002690 if (!pre)
2691 {
2692 if (subtract)
2693 {
2694 if (n > oldn)
2695 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002696 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002697 negative ^= TRUE;
2698 }
2699 }
2700 else
2701 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002702 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002703 if (n < oldn)
2704 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002705 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002706 negative ^= TRUE;
2707 }
2708 }
2709 if (n == 0)
2710 negative = FALSE;
2711 }
2712
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002713 if (do_unsigned && negative)
2714 {
2715 if (subtract)
2716 // sticking at zero.
2717 n = (uvarnumber_T)0;
2718 else
2719 // sticking at 2^64 - 1.
2720 n = (uvarnumber_T)(-1);
2721 negative = FALSE;
2722 }
2723
Bram Moolenaard79e5502016-01-10 22:13:02 +01002724 if (visual && !was_positive && !negative && col > 0)
2725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002727 col--;
2728 length++;
2729 }
2730
2731 /*
2732 * Delete the old number.
2733 */
2734 curwin->w_cursor.col = col;
2735 if (!did_change)
2736 startpos = curwin->w_cursor;
2737 did_change = TRUE;
2738 todel = length;
2739 c = gchar_cursor();
2740 /*
2741 * Don't include the '-' in the length, only the length of the
2742 * part after it is kept the same.
2743 */
2744 if (c == '-')
2745 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002746
2747 save_pos = curwin->w_cursor;
2748 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002749 {
2750 if (c < 0x100 && isalpha(c))
2751 {
2752 if (isupper(c))
2753 hexupper = TRUE;
2754 else
2755 hexupper = FALSE;
2756 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002757 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002758 c = gchar_cursor();
2759 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002760 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002761
2762 /*
2763 * Prepare the leading characters in buf1[].
2764 * When there are many leading zeros it could be very long.
2765 * Allocate a bit too much.
2766 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002767 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002768 if (buf1 == NULL)
2769 goto theend;
2770 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002771 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002772 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002773 if (pre)
2774 {
2775 *ptr++ = '0';
2776 --length;
2777 }
2778 if (pre == 'b' || pre == 'B' ||
2779 pre == 'x' || pre == 'X')
2780 {
2781 *ptr++ = pre;
2782 --length;
2783 }
2784
2785 /*
2786 * Put the number characters in buf2[].
2787 */
2788 if (pre == 'b' || pre == 'B')
2789 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002790 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002791 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002794 for (bit = bits; bit > 0; bit--)
2795 if ((n >> (bit - 1)) & 0x1) break;
2796
2797 for (i = 0; bit > 0; bit--)
2798 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2799
2800 buf2[i] = '\0';
2801 }
2802 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002803 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002804 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002805 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002806 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002807 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002808 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002809 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002810 length -= (int)STRLEN(buf2);
2811
2812 /*
2813 * Adjust number of zeros to the new number of digits, so the
2814 * total length of the number remains the same.
2815 * Don't do this when
2816 * the result may look like an octal number.
2817 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002818 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002819 while (length-- > 0)
2820 *ptr++ = '0';
2821 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002822
Bram Moolenaard79e5502016-01-10 22:13:02 +01002823 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002824
2825 // Insert just after the first character to be removed, so that any
2826 // text properties will be adjusted. Then delete the old number
2827 // afterwards.
2828 save_pos = curwin->w_cursor;
2829 if (todel > 0)
2830 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002832 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002833
2834 // del_char() will also mark line needing displaying
2835 if (todel > 0)
2836 {
2837 int bytes_after = (int)STRLEN(ml_get_curline())
2838 - curwin->w_cursor.col;
2839
2840 // Delete the one character before the insert.
2841 curwin->w_cursor = save_pos;
2842 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002843 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2844 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002845 --todel;
2846 }
2847 while (todel-- > 0)
2848 (void)del_char(FALSE);
2849
Bram Moolenaard79e5502016-01-10 22:13:02 +01002850 endpos = curwin->w_cursor;
2851 if (did_change && curwin->w_cursor.col)
2852 --curwin->w_cursor.col;
2853 }
2854
Bram Moolenaare1004402020-10-24 20:49:43 +02002855 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002858 curbuf->b_op_start = startpos;
2859 curbuf->b_op_end = endpos;
2860 if (curbuf->b_op_end.col > 0)
2861 --curbuf->b_op_end.col;
2862 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002863
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002864theend:
2865 if (visual)
2866 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002867 else if (did_change)
2868 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002869 else if (virtual_active())
2870 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002871
Bram Moolenaard79e5502016-01-10 22:13:02 +01002872 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873}
2874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002876clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002878 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879}
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002882 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 *
2884 * "Words" are counted by looking for boundaries between non-space and
2885 * space characters. (it seems to produce results that match 'wc'.)
2886 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002887 * Return value is byte count; word count for the line is added to "*wc".
2888 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 *
2890 * The function will only examine the first "limit" characters in the
2891 * line, stopping if it encounters an end-of-line (NUL byte). In that
2892 * case, eol_size will be added to the character count to account for
2893 * the size of the EOL character.
2894 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002895 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002896line_count_info(
2897 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002898 varnumber_T *wc,
2899 varnumber_T *cc,
2900 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002901 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002903 varnumber_T i;
2904 varnumber_T words = 0;
2905 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 int is_word = 0;
2907
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002908 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
2910 if (is_word)
2911 {
2912 if (vim_isspace(line[i]))
2913 {
2914 words++;
2915 is_word = 0;
2916 }
2917 }
2918 else if (!vim_isspace(line[i]))
2919 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002920 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002921 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
2923
2924 if (is_word)
2925 words++;
2926 *wc += words;
2927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002929 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002932 chars += eol_size;
2933 }
2934 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 return i;
2936}
2937
2938/*
2939 * Give some info about the position of the cursor (for "g CTRL-G").
2940 * In Visual mode, give some info about the selected region. (In this case,
2941 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002942 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 */
2944 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002945cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
2947 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002948 char_u buf1[50];
2949 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002951 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002952 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002953 varnumber_T byte_count_cursor = 0;
2954 varnumber_T char_count = 0;
2955 varnumber_T char_count_cursor = 0;
2956 varnumber_T word_count = 0;
2957 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002958 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002959 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 long line_count_selected = 0;
2961 pos_T min_pos, max_pos;
2962 oparg_T oparg;
2963 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 /*
2966 * Compute the length of the file in characters.
2967 */
2968 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2969 {
Bram Moolenaared767a22016-01-03 22:49:16 +01002970 if (dict == NULL)
2971 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002972 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01002973 return;
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976 else
2977 {
2978 if (get_fileformat(curbuf) == EOL_DOS)
2979 eol_size = 2;
2980 else
2981 eol_size = 1;
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (VIsual_active)
2984 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002985 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
2987 min_pos = VIsual;
2988 max_pos = curwin->w_cursor;
2989 }
2990 else
2991 {
2992 min_pos = curwin->w_cursor;
2993 max_pos = VIsual;
2994 }
2995 if (*p_sel == 'e' && max_pos.col > 0)
2996 --max_pos.col;
2997
2998 if (VIsual_mode == Ctrl_V)
2999 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003000#ifdef FEAT_LINEBREAK
3001 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003002 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003005 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003006 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 oparg.is_VIsual = 1;
3009 oparg.block_mode = TRUE;
3010 oparg.op_type = OP_NOP;
3011 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003012 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003013#ifdef FEAT_LINEBREAK
3014 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003015 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003016#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003017 if (curwin->w_curswant == MAXCOL)
3018 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003019 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (oparg.end_vcol < oparg.start_vcol)
3021 {
3022 oparg.end_vcol += oparg.start_vcol;
3023 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3024 oparg.end_vcol -= oparg.start_vcol;
3025 }
3026 }
3027 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003032 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003033 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 {
3035 ui_breakcheck();
3036 if (got_int)
3037 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003038 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
3040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003041 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (VIsual_active
3043 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3044 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003045 char_u *s = NULL;
3046 long len = 0L;
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 switch (VIsual_mode)
3049 {
3050 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003054 s = bd.textstart;
3055 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 break;
3057 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003058 s = ml_get(lnum);
3059 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 break;
3061 case 'v':
3062 {
3063 colnr_T start_col = (lnum == min_pos.lnum)
3064 ? min_pos.col : 0;
3065 colnr_T end_col = (lnum == max_pos.lnum)
3066 ? max_pos.col - start_col + 1 : MAXCOL;
3067
Bram Moolenaardef9e822004-12-31 20:58:58 +00003068 s = ml_get(lnum) + start_col;
3069 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071 break;
3072 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003073 if (s != NULL)
3074 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003075 byte_count_cursor += line_count_info(s, &word_count_cursor,
3076 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003077 if (lnum == curbuf->b_ml.ml_line_count
3078 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003079 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003080 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003081 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
3084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003086 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 if (lnum == curwin->w_cursor.lnum)
3088 {
3089 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003090 char_count_cursor += char_count;
3091 byte_count_cursor = byte_count +
3092 line_count_info(ml_get(lnum),
3093 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003094 (varnumber_T)(curwin->w_cursor.col + 1),
3095 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
3097 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003099 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003100 &char_count, (varnumber_T)MAXCOL,
3101 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 }
3103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003104 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003105 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003106 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
Bram Moolenaared767a22016-01-03 22:49:16 +01003108 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003110 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003112 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3113 {
3114 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3115 &max_pos.col);
3116 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3117 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3118 }
3119 else
3120 buf1[0] = NUL;
3121
3122 if (char_count_cursor == byte_count_cursor
3123 && char_count == byte_count)
3124 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003125 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003126 buf1, line_count_selected,
3127 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003128 (varnumber_T)word_count_cursor,
3129 (varnumber_T)word_count,
3130 (varnumber_T)byte_count_cursor,
3131 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003132 else
3133 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003134 _("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 +01003135 buf1, line_count_selected,
3136 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003137 (varnumber_T)word_count_cursor,
3138 (varnumber_T)word_count,
3139 (varnumber_T)char_count_cursor,
3140 (varnumber_T)char_count,
3141 (varnumber_T)byte_count_cursor,
3142 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
3144 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003145 {
3146 p = ml_get_curline();
3147 validate_virtcol();
3148 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3149 (int)curwin->w_virtcol + 1);
3150 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3151 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaared767a22016-01-03 22:49:16 +01003153 if (char_count_cursor == byte_count_cursor
3154 && char_count == byte_count)
3155 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003156 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003157 (char *)buf1, (char *)buf2,
3158 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003160 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3161 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003162 else
3163 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003164 _("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 +01003165 (char *)buf1, (char *)buf2,
3166 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003167 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003168 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3169 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3170 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003171 }
3172 }
3173
Bram Moolenaared767a22016-01-03 22:49:16 +01003174 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003175 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003176 {
3177 size_t len = STRLEN(IObuff);
3178
3179 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003180 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003181 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003182 if (dict == NULL)
3183 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003184 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003185 p = p_shm;
3186 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003187 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003188 p_shm = p;
3189 }
3190 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003191#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003192 if (dict != NULL)
3193 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003194 dict_add_number(dict, "words", word_count);
3195 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003196 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003197 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3198 byte_count_cursor);
3199 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3200 char_count_cursor);
3201 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3202 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003206
3207/*
3208 * Handle indent and format operators and visual mode ":".
3209 */
3210 static void
3211op_colon(oparg_T *oap)
3212{
3213 stuffcharReadbuff(':');
3214 if (oap->is_VIsual)
3215 stuffReadbuff((char_u *)"'<,'>");
3216 else
3217 {
3218 // Make the range look nice, so it can be repeated.
3219 if (oap->start.lnum == curwin->w_cursor.lnum)
3220 stuffcharReadbuff('.');
3221 else
3222 stuffnumReadbuff((long)oap->start.lnum);
3223 if (oap->end.lnum != oap->start.lnum)
3224 {
3225 stuffcharReadbuff(',');
3226 if (oap->end.lnum == curwin->w_cursor.lnum)
3227 stuffcharReadbuff('.');
3228 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3229 stuffcharReadbuff('$');
3230 else if (oap->start.lnum == curwin->w_cursor.lnum)
3231 {
3232 stuffReadbuff((char_u *)".+");
3233 stuffnumReadbuff((long)oap->line_count - 1);
3234 }
3235 else
3236 stuffnumReadbuff((long)oap->end.lnum);
3237 }
3238 }
3239 if (oap->op_type != OP_COLON)
3240 stuffReadbuff((char_u *)"!");
3241 if (oap->op_type == OP_INDENT)
3242 {
3243#ifndef FEAT_CINDENT
3244 if (*get_equalprg() == NUL)
3245 stuffReadbuff((char_u *)"indent");
3246 else
3247#endif
3248 stuffReadbuff(get_equalprg());
3249 stuffReadbuff((char_u *)"\n");
3250 }
3251 else if (oap->op_type == OP_FORMAT)
3252 {
3253 if (*curbuf->b_p_fp != NUL)
3254 stuffReadbuff(curbuf->b_p_fp);
3255 else if (*p_fp != NUL)
3256 stuffReadbuff(p_fp);
3257 else
3258 stuffReadbuff((char_u *)"fmt");
3259 stuffReadbuff((char_u *)"\n']");
3260 }
3261
3262 // do_cmdline() does the rest
3263}
3264
3265/*
3266 * Handle the "g@" operator: call 'operatorfunc'.
3267 */
3268 static void
3269op_function(oparg_T *oap UNUSED)
3270{
3271#ifdef FEAT_EVAL
3272 typval_T argv[2];
3273 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003274 pos_T orig_start = curbuf->b_op_start;
3275 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003276
3277 if (*p_opfunc == NUL)
3278 emsg(_("E774: 'operatorfunc' is empty"));
3279 else
3280 {
3281 // Set '[ and '] marks to text to be operated on.
3282 curbuf->b_op_start = oap->start;
3283 curbuf->b_op_end = oap->end;
3284 if (oap->motion_type != MLINE && !oap->inclusive)
3285 // Exclude the end position.
3286 decl(&curbuf->b_op_end);
3287
3288 argv[0].v_type = VAR_STRING;
3289 if (oap->block_mode)
3290 argv[0].vval.v_string = (char_u *)"block";
3291 else if (oap->motion_type == MLINE)
3292 argv[0].vval.v_string = (char_u *)"line";
3293 else
3294 argv[0].vval.v_string = (char_u *)"char";
3295 argv[1].v_type = VAR_UNKNOWN;
3296
3297 // Reset virtual_op so that 'virtualedit' can be changed in the
3298 // function.
3299 virtual_op = MAYBE;
3300
3301 (void)call_func_retnr(p_opfunc, 1, argv);
3302
3303 virtual_op = save_virtual_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003304 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003305 {
3306 curbuf->b_op_start = orig_start;
3307 curbuf->b_op_end = orig_end;
3308 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003309 }
3310#else
3311 emsg(_("E775: Eval feature not available"));
3312#endif
3313}
3314
3315/*
3316 * Calculate start/end virtual columns for operating in block mode.
3317 */
3318 static void
3319get_op_vcol(
3320 oparg_T *oap,
3321 colnr_T redo_VIsual_vcol,
3322 int initial) // when TRUE adjust position for 'selectmode'
3323{
3324 colnr_T start, end;
3325
3326 if (VIsual_mode != Ctrl_V
3327 || (!initial && oap->end.col < curwin->w_width))
3328 return;
3329
3330 oap->block_mode = TRUE;
3331
3332 // prevent from moving onto a trail byte
3333 if (has_mbyte)
3334 mb_adjustpos(curwin->w_buffer, &oap->end);
3335
3336 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3337
3338 if (!redo_VIsual_busy)
3339 {
3340 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3341
3342 if (start < oap->start_vcol)
3343 oap->start_vcol = start;
3344 if (end > oap->end_vcol)
3345 {
3346 if (initial && *p_sel == 'e' && start >= 1
3347 && start - 1 >= oap->end_vcol)
3348 oap->end_vcol = start - 1;
3349 else
3350 oap->end_vcol = end;
3351 }
3352 }
3353
3354 // if '$' was used, get oap->end_vcol from longest line
3355 if (curwin->w_curswant == MAXCOL)
3356 {
3357 curwin->w_cursor.col = MAXCOL;
3358 oap->end_vcol = 0;
3359 for (curwin->w_cursor.lnum = oap->start.lnum;
3360 curwin->w_cursor.lnum <= oap->end.lnum;
3361 ++curwin->w_cursor.lnum)
3362 {
3363 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3364 if (end > oap->end_vcol)
3365 oap->end_vcol = end;
3366 }
3367 }
3368 else if (redo_VIsual_busy)
3369 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3370 // Correct oap->end.col and oap->start.col to be the
3371 // upper-left and lower-right corner of the block area.
3372 //
3373 // (Actually, this does convert column positions into character
3374 // positions)
3375 curwin->w_cursor.lnum = oap->end.lnum;
3376 coladvance(oap->end_vcol);
3377 oap->end = curwin->w_cursor;
3378
3379 curwin->w_cursor = oap->start;
3380 coladvance(oap->start_vcol);
3381 oap->start = curwin->w_cursor;
3382}
3383
3384/*
3385 * Handle an operator after Visual mode or when the movement is finished.
3386 * "gui_yank" is true when yanking text for the clipboard.
3387 */
3388 void
3389do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3390{
3391 oparg_T *oap = cap->oap;
3392 pos_T old_cursor;
3393 int empty_region_error;
3394 int restart_edit_save;
3395#ifdef FEAT_LINEBREAK
3396 int lbr_saved = curwin->w_p_lbr;
3397#endif
3398
3399 // The visual area is remembered for redo
3400 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3401 static linenr_T redo_VIsual_line_count; // number of lines
3402 static colnr_T redo_VIsual_vcol; // number of cols or end column
3403 static long redo_VIsual_count; // count for Visual operator
3404 static int redo_VIsual_arg; // extra argument
3405 int include_line_break = FALSE;
3406
3407#if defined(FEAT_CLIPBOARD)
3408 // Yank the visual area into the GUI selection register before we operate
3409 // on it and lose it forever.
3410 // Don't do it if a specific register was specified, so that ""x"*P works.
3411 // This could call do_pending_operator() recursively, but that's OK
3412 // because gui_yank will be TRUE for the nested call.
3413 if ((clip_star.available || clip_plus.available)
3414 && oap->op_type != OP_NOP
3415 && !gui_yank
3416 && VIsual_active
3417 && !redo_VIsual_busy
3418 && oap->regname == 0)
3419 clip_auto_select();
3420#endif
3421 old_cursor = curwin->w_cursor;
3422
3423 // If an operation is pending, handle it...
3424 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3425 {
3426 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3427 // for the clipboard.
3428 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3429
3430#ifdef FEAT_LINEBREAK
3431 // Avoid a problem with unwanted linebreaks in block mode.
3432 if (curwin->w_p_lbr)
3433 curwin->w_valid &= ~VALID_VIRTCOL;
3434 curwin->w_p_lbr = FALSE;
3435#endif
3436 oap->is_VIsual = VIsual_active;
3437 if (oap->motion_force == 'V')
3438 oap->motion_type = MLINE;
3439 else if (oap->motion_force == 'v')
3440 {
3441 // If the motion was linewise, "inclusive" will not have been set.
3442 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3443 if (oap->motion_type == MLINE)
3444 oap->inclusive = FALSE;
3445 // If the motion already was characterwise, toggle "inclusive"
3446 else if (oap->motion_type == MCHAR)
3447 oap->inclusive = !oap->inclusive;
3448 oap->motion_type = MCHAR;
3449 }
3450 else if (oap->motion_force == Ctrl_V)
3451 {
3452 // Change line- or characterwise motion into Visual block mode.
3453 if (!VIsual_active)
3454 {
3455 VIsual_active = TRUE;
3456 VIsual = oap->start;
3457 }
3458 VIsual_mode = Ctrl_V;
3459 VIsual_select = FALSE;
3460 VIsual_reselect = FALSE;
3461 }
3462
3463 // Only redo yank when 'y' flag is in 'cpoptions'.
3464 // Never redo "zf" (define fold).
3465 if ((redo_yank || oap->op_type != OP_YANK)
3466 && ((!VIsual_active || oap->motion_force)
3467 // Also redo Operator-pending Visual mode mappings
3468 || (VIsual_active && cap->cmdchar == ':'
3469 && oap->op_type != OP_COLON))
3470 && cap->cmdchar != 'D'
3471#ifdef FEAT_FOLDING
3472 && oap->op_type != OP_FOLD
3473 && oap->op_type != OP_FOLDOPEN
3474 && oap->op_type != OP_FOLDOPENREC
3475 && oap->op_type != OP_FOLDCLOSE
3476 && oap->op_type != OP_FOLDCLOSEREC
3477 && oap->op_type != OP_FOLDDEL
3478 && oap->op_type != OP_FOLDDELREC
3479#endif
3480 )
3481 {
3482 prep_redo(oap->regname, cap->count0,
3483 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3484 oap->motion_force, cap->cmdchar, cap->nchar);
3485 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3486 {
3487 // If 'cpoptions' does not contain 'r', insert the search
3488 // pattern to really repeat the same command.
3489 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3490 AppendToRedobuffLit(cap->searchbuf, -1);
3491 AppendToRedobuff(NL_STR);
3492 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003493 else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003494 {
3495 // do_cmdline() has stored the first typed line in
3496 // "repeat_cmdline". When several lines are typed repeating
3497 // won't be possible.
3498 if (repeat_cmdline == NULL)
3499 ResetRedobuff();
3500 else
3501 {
3502 AppendToRedobuffLit(repeat_cmdline, -1);
3503 AppendToRedobuff(NL_STR);
3504 VIM_CLEAR(repeat_cmdline);
3505 }
3506 }
3507 }
3508
3509 if (redo_VIsual_busy)
3510 {
3511 // Redo of an operation on a Visual area. Use the same size from
3512 // redo_VIsual_line_count and redo_VIsual_vcol.
3513 oap->start = curwin->w_cursor;
3514 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3515 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3516 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3517 VIsual_mode = redo_VIsual_mode;
3518 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3519 {
3520 if (VIsual_mode == 'v')
3521 {
3522 if (redo_VIsual_line_count <= 1)
3523 {
3524 validate_virtcol();
3525 curwin->w_curswant =
3526 curwin->w_virtcol + redo_VIsual_vcol - 1;
3527 }
3528 else
3529 curwin->w_curswant = redo_VIsual_vcol;
3530 }
3531 else
3532 {
3533 curwin->w_curswant = MAXCOL;
3534 }
3535 coladvance(curwin->w_curswant);
3536 }
3537 cap->count0 = redo_VIsual_count;
3538 if (redo_VIsual_count != 0)
3539 cap->count1 = redo_VIsual_count;
3540 else
3541 cap->count1 = 1;
3542 }
3543 else if (VIsual_active)
3544 {
3545 if (!gui_yank)
3546 {
3547 // Save the current VIsual area for '< and '> marks, and "gv"
3548 curbuf->b_visual.vi_start = VIsual;
3549 curbuf->b_visual.vi_end = curwin->w_cursor;
3550 curbuf->b_visual.vi_mode = VIsual_mode;
3551 restore_visual_mode();
3552 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3553# ifdef FEAT_EVAL
3554 curbuf->b_visual_mode_eval = VIsual_mode;
3555# endif
3556 }
3557
3558 // In Select mode, a linewise selection is operated upon like a
3559 // characterwise selection.
3560 // Special case: gH<Del> deletes the last line.
3561 if (VIsual_select && VIsual_mode == 'V'
3562 && cap->oap->op_type != OP_DELETE)
3563 {
3564 if (LT_POS(VIsual, curwin->w_cursor))
3565 {
3566 VIsual.col = 0;
3567 curwin->w_cursor.col =
3568 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3569 }
3570 else
3571 {
3572 curwin->w_cursor.col = 0;
3573 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3574 }
3575 VIsual_mode = 'v';
3576 }
3577 // If 'selection' is "exclusive", backup one character for
3578 // charwise selections.
3579 else if (VIsual_mode == 'v')
3580 include_line_break = unadjust_for_sel();
3581
3582 oap->start = VIsual;
3583 if (VIsual_mode == 'V')
3584 {
3585 oap->start.col = 0;
3586 oap->start.coladd = 0;
3587 }
3588 }
3589
3590 // Set oap->start to the first position of the operated text, oap->end
3591 // to the end of the operated text. w_cursor is equal to oap->start.
3592 if (LT_POS(oap->start, curwin->w_cursor))
3593 {
3594#ifdef FEAT_FOLDING
3595 // Include folded lines completely.
3596 if (!VIsual_active)
3597 {
3598 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3599 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003600 if ((curwin->w_cursor.col > 0 || oap->inclusive
3601 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003602 && hasFolding(curwin->w_cursor.lnum, NULL,
3603 &curwin->w_cursor.lnum))
3604 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3605 }
3606#endif
3607 oap->end = curwin->w_cursor;
3608 curwin->w_cursor = oap->start;
3609
3610 // w_virtcol may have been updated; if the cursor goes back to its
3611 // previous position w_virtcol becomes invalid and isn't updated
3612 // automatically.
3613 curwin->w_valid &= ~VALID_VIRTCOL;
3614 }
3615 else
3616 {
3617#ifdef FEAT_FOLDING
3618 // Include folded lines completely.
3619 if (!VIsual_active && oap->motion_type == MLINE)
3620 {
3621 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3622 NULL))
3623 curwin->w_cursor.col = 0;
3624 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3625 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3626 }
3627#endif
3628 oap->end = oap->start;
3629 oap->start = curwin->w_cursor;
3630 }
3631
3632 // Just in case lines were deleted that make the position invalid.
3633 check_pos(curwin->w_buffer, &oap->end);
3634 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3635
3636 // Set "virtual_op" before resetting VIsual_active.
3637 virtual_op = virtual_active();
3638
3639 if (VIsual_active || redo_VIsual_busy)
3640 {
3641 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3642
3643 if (!redo_VIsual_busy && !gui_yank)
3644 {
3645 // Prepare to reselect and redo Visual: this is based on the
3646 // size of the Visual text
3647 resel_VIsual_mode = VIsual_mode;
3648 if (curwin->w_curswant == MAXCOL)
3649 resel_VIsual_vcol = MAXCOL;
3650 else
3651 {
3652 if (VIsual_mode != Ctrl_V)
3653 getvvcol(curwin, &(oap->end),
3654 NULL, NULL, &oap->end_vcol);
3655 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3656 {
3657 if (VIsual_mode != Ctrl_V)
3658 getvvcol(curwin, &(oap->start),
3659 &oap->start_vcol, NULL, NULL);
3660 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3661 }
3662 else
3663 resel_VIsual_vcol = oap->end_vcol;
3664 }
3665 resel_VIsual_line_count = oap->line_count;
3666 }
3667
3668 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3669 if ((redo_yank || oap->op_type != OP_YANK)
3670 && oap->op_type != OP_COLON
3671#ifdef FEAT_FOLDING
3672 && oap->op_type != OP_FOLD
3673 && oap->op_type != OP_FOLDOPEN
3674 && oap->op_type != OP_FOLDOPENREC
3675 && oap->op_type != OP_FOLDCLOSE
3676 && oap->op_type != OP_FOLDCLOSEREC
3677 && oap->op_type != OP_FOLDDEL
3678 && oap->op_type != OP_FOLDDELREC
3679#endif
3680 && oap->motion_force == NUL
3681 )
3682 {
3683 // Prepare for redoing. Only use the nchar field for "r",
3684 // otherwise it might be the second char of the operator.
3685 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3686 || cap->nchar == 'N'))
3687 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003688 get_op_char(oap->op_type),
3689 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003690 oap->motion_force, cap->cmdchar, cap->nchar);
3691 else if (cap->cmdchar != ':')
3692 {
3693 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3694
3695 // reverse what nv_replace() did
3696 if (nchar == REPLACE_CR_NCHAR)
3697 nchar = CAR;
3698 else if (nchar == REPLACE_NL_NCHAR)
3699 nchar = NL;
3700 prep_redo(oap->regname, 0L, NUL, 'v',
3701 get_op_char(oap->op_type),
3702 get_extra_op_char(oap->op_type),
3703 nchar);
3704 }
3705 if (!redo_VIsual_busy)
3706 {
3707 redo_VIsual_mode = resel_VIsual_mode;
3708 redo_VIsual_vcol = resel_VIsual_vcol;
3709 redo_VIsual_line_count = resel_VIsual_line_count;
3710 redo_VIsual_count = cap->count0;
3711 redo_VIsual_arg = cap->arg;
3712 }
3713 }
3714
3715 // oap->inclusive defaults to TRUE.
3716 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3717 // FALSE. This makes "d}P" and "v}dP" work the same.
3718 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3719 oap->inclusive = TRUE;
3720 if (VIsual_mode == 'V')
3721 oap->motion_type = MLINE;
3722 else
3723 {
3724 oap->motion_type = MCHAR;
3725 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3726 && (include_line_break || !virtual_op))
3727 {
3728 oap->inclusive = FALSE;
3729 // Try to include the newline, unless it's an operator
3730 // that works on lines only.
3731 if (*p_sel != 'o'
3732 && !op_on_lines(oap->op_type)
3733 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3734 {
3735 ++oap->end.lnum;
3736 oap->end.col = 0;
3737 oap->end.coladd = 0;
3738 ++oap->line_count;
3739 }
3740 }
3741 }
3742
3743 redo_VIsual_busy = FALSE;
3744
3745 // Switch Visual off now, so screen updating does
3746 // not show inverted text when the screen is redrawn.
3747 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3748 // no screen redraw, so it is done here to remove the inverted
3749 // part.
3750 if (!gui_yank)
3751 {
3752 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003753 setmouse();
3754 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003755 may_clear_cmdline();
3756 if ((oap->op_type == OP_YANK
3757 || oap->op_type == OP_COLON
3758 || oap->op_type == OP_FUNCTION
3759 || oap->op_type == OP_FILTER)
3760 && oap->motion_force == NUL)
3761 {
3762#ifdef FEAT_LINEBREAK
3763 // make sure redrawing is correct
3764 curwin->w_p_lbr = lbr_saved;
3765#endif
3766 redraw_curbuf_later(INVERTED);
3767 }
3768 }
3769 }
3770
3771 // Include the trailing byte of a multi-byte char.
3772 if (has_mbyte && oap->inclusive)
3773 {
3774 int l;
3775
3776 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3777 if (l > 1)
3778 oap->end.col += l - 1;
3779 }
3780 curwin->w_set_curswant = TRUE;
3781
3782 // oap->empty is set when start and end are the same. The inclusive
3783 // flag affects this too, unless yanking and the end is on a NUL.
3784 oap->empty = (oap->motion_type == MCHAR
3785 && (!oap->inclusive
3786 || (oap->op_type == OP_YANK
3787 && gchar_pos(&oap->end) == NUL))
3788 && EQUAL_POS(oap->start, oap->end)
3789 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3790 // For delete, change and yank, it's an error to operate on an
3791 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3792 empty_region_error = (oap->empty
3793 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3794
3795 // Force a redraw when operating on an empty Visual region, when
3796 // 'modifiable is off or creating a fold.
3797 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3798#ifdef FEAT_FOLDING
3799 || oap->op_type == OP_FOLD
3800#endif
3801 ))
3802 {
3803#ifdef FEAT_LINEBREAK
3804 curwin->w_p_lbr = lbr_saved;
3805#endif
3806 redraw_curbuf_later(INVERTED);
3807 }
3808
3809 // If the end of an operator is in column one while oap->motion_type
3810 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3811 // character in the previous line. If op_start is on or before the
3812 // first non-blank in the line, the operator becomes linewise
3813 // (strange, but that's the way vi does it).
3814 if ( oap->motion_type == MCHAR
3815 && oap->inclusive == FALSE
3816 && !(cap->retval & CA_NO_ADJ_OP_END)
3817 && oap->end.col == 0
3818 && (!oap->is_VIsual || *p_sel == 'o')
3819 && !oap->block_mode
3820 && oap->line_count > 1)
3821 {
3822 oap->end_adjusted = TRUE; // remember that we did this
3823 --oap->line_count;
3824 --oap->end.lnum;
3825 if (inindent(0))
3826 oap->motion_type = MLINE;
3827 else
3828 {
3829 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3830 if (oap->end.col)
3831 {
3832 --oap->end.col;
3833 oap->inclusive = TRUE;
3834 }
3835 }
3836 }
3837 else
3838 oap->end_adjusted = FALSE;
3839
3840 switch (oap->op_type)
3841 {
3842 case OP_LSHIFT:
3843 case OP_RSHIFT:
3844 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3845 auto_format(FALSE, TRUE);
3846 break;
3847
3848 case OP_JOIN_NS:
3849 case OP_JOIN:
3850 if (oap->line_count < 2)
3851 oap->line_count = 2;
3852 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3853 curbuf->b_ml.ml_line_count)
3854 beep_flush();
3855 else
3856 {
3857 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3858 TRUE, TRUE, TRUE);
3859 auto_format(FALSE, TRUE);
3860 }
3861 break;
3862
3863 case OP_DELETE:
3864 VIsual_reselect = FALSE; // don't reselect now
3865 if (empty_region_error)
3866 {
3867 vim_beep(BO_OPER);
3868 CancelRedo();
3869 }
3870 else
3871 {
3872 (void)op_delete(oap);
3873 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
3874 u_save_cursor(); // cursor line wasn't saved yet
3875 auto_format(FALSE, TRUE);
3876 }
3877 break;
3878
3879 case OP_YANK:
3880 if (empty_region_error)
3881 {
3882 if (!gui_yank)
3883 {
3884 vim_beep(BO_OPER);
3885 CancelRedo();
3886 }
3887 }
3888 else
3889 {
3890#ifdef FEAT_LINEBREAK
3891 curwin->w_p_lbr = lbr_saved;
3892#endif
3893 (void)op_yank(oap, FALSE, !gui_yank);
3894 }
3895 check_cursor_col();
3896 break;
3897
3898 case OP_CHANGE:
3899 VIsual_reselect = FALSE; // don't reselect now
3900 if (empty_region_error)
3901 {
3902 vim_beep(BO_OPER);
3903 CancelRedo();
3904 }
3905 else
3906 {
3907 // This is a new edit command, not a restart. Need to
3908 // remember it to make 'insertmode' work with mappings for
3909 // Visual mode. But do this only once and not when typed and
3910 // 'insertmode' isn't set.
3911 if (p_im || !KeyTyped)
3912 restart_edit_save = restart_edit;
3913 else
3914 restart_edit_save = 0;
3915 restart_edit = 0;
3916#ifdef FEAT_LINEBREAK
3917 // Restore linebreak, so that when the user edits it looks as
3918 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01003919 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003920#endif
3921 // Reset finish_op now, don't want it set inside edit().
3922 finish_op = FALSE;
3923 if (op_change(oap)) // will call edit()
3924 cap->retval |= CA_COMMAND_BUSY;
3925 if (restart_edit == 0)
3926 restart_edit = restart_edit_save;
3927 }
3928 break;
3929
3930 case OP_FILTER:
3931 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3932 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
3933 else
3934 bangredo = TRUE; // do_bang() will put cmd in redo buffer
3935 // FALLTHROUGH
3936
3937 case OP_INDENT:
3938 case OP_COLON:
3939
3940#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3941 // If 'equalprg' is empty, do the indenting internally.
3942 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3943 {
3944# ifdef FEAT_LISP
3945 if (curbuf->b_p_lisp)
3946 {
3947 op_reindent(oap, get_lisp_indent);
3948 break;
3949 }
3950# endif
3951# ifdef FEAT_CINDENT
3952 op_reindent(oap,
3953# ifdef FEAT_EVAL
3954 *curbuf->b_p_inde != NUL ? get_expr_indent :
3955# endif
3956 get_c_indent);
3957 break;
3958# endif
3959 }
3960#endif
3961
3962 op_colon(oap);
3963 break;
3964
3965 case OP_TILDE:
3966 case OP_UPPER:
3967 case OP_LOWER:
3968 case OP_ROT13:
3969 if (empty_region_error)
3970 {
3971 vim_beep(BO_OPER);
3972 CancelRedo();
3973 }
3974 else
3975 op_tilde(oap);
3976 check_cursor_col();
3977 break;
3978
3979 case OP_FORMAT:
3980#if defined(FEAT_EVAL)
3981 if (*curbuf->b_p_fex != NUL)
3982 op_formatexpr(oap); // use expression
3983 else
3984#endif
3985 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
3986 op_colon(oap); // use external command
3987 else
3988 op_format(oap, FALSE); // use internal function
3989 break;
3990
3991 case OP_FORMAT2:
3992 op_format(oap, TRUE); // use internal function
3993 break;
3994
3995 case OP_FUNCTION:
3996#ifdef FEAT_LINEBREAK
3997 // Restore linebreak, so that when the user edits it looks as
3998 // before.
3999 curwin->w_p_lbr = lbr_saved;
4000#endif
4001 op_function(oap); // call 'operatorfunc'
4002 break;
4003
4004 case OP_INSERT:
4005 case OP_APPEND:
4006 VIsual_reselect = FALSE; // don't reselect now
4007 if (empty_region_error)
4008 {
4009 vim_beep(BO_OPER);
4010 CancelRedo();
4011 }
4012 else
4013 {
4014 // This is a new edit command, not a restart. Need to
4015 // remember it to make 'insertmode' work with mappings for
4016 // Visual mode. But do this only once.
4017 restart_edit_save = restart_edit;
4018 restart_edit = 0;
4019#ifdef FEAT_LINEBREAK
4020 // Restore linebreak, so that when the user edits it looks as
4021 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004022 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004023#endif
4024 op_insert(oap, cap->count1);
4025#ifdef FEAT_LINEBREAK
4026 // Reset linebreak, so that formatting works correctly.
4027 curwin->w_p_lbr = FALSE;
4028#endif
4029
4030 // TODO: when inserting in several lines, should format all
4031 // the lines.
4032 auto_format(FALSE, TRUE);
4033
4034 if (restart_edit == 0)
4035 restart_edit = restart_edit_save;
4036 else
4037 cap->retval |= CA_COMMAND_BUSY;
4038 }
4039 break;
4040
4041 case OP_REPLACE:
4042 VIsual_reselect = FALSE; // don't reselect now
4043 if (empty_region_error)
4044 {
4045 vim_beep(BO_OPER);
4046 CancelRedo();
4047 }
4048 else
4049 {
4050#ifdef FEAT_LINEBREAK
4051 // Restore linebreak, so that when the user edits it looks as
4052 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004053 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004054#endif
4055 op_replace(oap, cap->nchar);
4056 }
4057 break;
4058
4059#ifdef FEAT_FOLDING
4060 case OP_FOLD:
4061 VIsual_reselect = FALSE; // don't reselect now
4062 foldCreate(oap->start.lnum, oap->end.lnum);
4063 break;
4064
4065 case OP_FOLDOPEN:
4066 case OP_FOLDOPENREC:
4067 case OP_FOLDCLOSE:
4068 case OP_FOLDCLOSEREC:
4069 VIsual_reselect = FALSE; // don't reselect now
4070 opFoldRange(oap->start.lnum, oap->end.lnum,
4071 oap->op_type == OP_FOLDOPEN
4072 || oap->op_type == OP_FOLDOPENREC,
4073 oap->op_type == OP_FOLDOPENREC
4074 || oap->op_type == OP_FOLDCLOSEREC,
4075 oap->is_VIsual);
4076 break;
4077
4078 case OP_FOLDDEL:
4079 case OP_FOLDDELREC:
4080 VIsual_reselect = FALSE; // don't reselect now
4081 deleteFold(oap->start.lnum, oap->end.lnum,
4082 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4083 break;
4084#endif
4085 case OP_NR_ADD:
4086 case OP_NR_SUB:
4087 if (empty_region_error)
4088 {
4089 vim_beep(BO_OPER);
4090 CancelRedo();
4091 }
4092 else
4093 {
4094 VIsual_active = TRUE;
4095#ifdef FEAT_LINEBREAK
4096 curwin->w_p_lbr = lbr_saved;
4097#endif
4098 op_addsub(oap, cap->count1, redo_VIsual_arg);
4099 VIsual_active = FALSE;
4100 }
4101 check_cursor_col();
4102 break;
4103 default:
4104 clearopbeep(oap);
4105 }
4106 virtual_op = MAYBE;
4107 if (!gui_yank)
4108 {
4109 // if 'sol' not set, go back to old column for some commands
4110 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4111 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4112 || oap->op_type == OP_DELETE))
4113 {
4114#ifdef FEAT_LINEBREAK
4115 curwin->w_p_lbr = FALSE;
4116#endif
4117 coladvance(curwin->w_curswant = old_col);
4118 }
4119 }
4120 else
4121 {
4122 curwin->w_cursor = old_cursor;
4123 }
4124 oap->block_mode = FALSE;
4125 clearop(oap);
4126 motion_force = NUL;
4127 }
4128#ifdef FEAT_LINEBREAK
4129 curwin->w_p_lbr = lbr_saved;
4130#endif
4131}