blob: 2988a185b4d947525b5f867cd8549b768bff2d16 [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 Moolenaarf4a1d1c2019-11-16 13:50:25 +0100207 if (!cmdmod.lockmarks)
208 {
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
484 unsigned s_len; // STRLEN(s)
485 char_u *newp, *oldp; // new, old lines
486 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 int oldstate = State;
488
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 s_len = (unsigned)STRLEN(s);
491
492 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
493 {
494 block_prep(oap, bdp, lnum, TRUE);
495 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
498 oldp = ml_get(lnum);
499
500 if (b_insert)
501 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200502 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 spaces = bdp->startspaces;
504 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100505 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 offset = bdp->textcol;
507 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200510 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100511 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200513 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100515 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 offset = bdp->textcol + bdp->textlen - (spaces != 0);
517 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100518 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100520 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (!bdp->is_MAX)
522 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
523 count = spaces;
524 offset = bdp->textcol + bdp->textlen;
525 }
526 }
527
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200528 if (has_mbyte && spaces > 0)
529 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100530 int off;
531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 // Avoid starting halfway a multi-byte character.
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200533 if (b_insert)
534 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100535 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200536 }
537 else
538 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100539 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200540 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200541 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100542 spaces -= off;
543 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200544 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200545
Bram Moolenaar964b3742019-05-24 18:54:09 +0200546 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 if (newp == NULL)
548 continue;
549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100550 // copy up to shifted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 mch_memmove(newp, oldp, (size_t)(offset));
552 oldp += offset;
553
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100554 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200555 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 // copy the new text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
559 offset += s_len;
560
561 if (spaces && !bdp->is_short)
562 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100563 // insert post-padding
Bram Moolenaardac13472019-09-16 21:06:21 +0200564 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100565 // We're splitting a TAB, don't copy it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 oldp++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 // We allowed for that TAB, remember this now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 count++;
569 }
570
571 if (spaces > 0)
572 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000573 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575 ml_replace(lnum, newp, FALSE);
576
577 if (lnum == oap->end.lnum)
578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100579 // Set "']" mark to the end of the block instead of the end of
580 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 curbuf->b_op_end.lnum = oap->end.lnum;
582 curbuf->b_op_end.col = offset;
583 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100584 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
587
588 State = oldstate;
589}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200592 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200594 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 */
596 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100597op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 int n;
600 linenr_T lnum;
601 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 char_u *newp, *oldp;
603 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
605 int did_yank = FALSE;
606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100607 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 return OK;
609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100610 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 if (oap->empty)
612 return u_save_cursor();
613
614 if (!curbuf->b_p_ma)
615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100616 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 return FAIL;
618 }
619
620#ifdef FEAT_CLIPBOARD
621 adjust_clip_reg(&oap->regname);
622#endif
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (has_mbyte)
625 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaard04b7502010-07-08 22:27:55 +0200627 /*
628 * Imitate the strange Vi behaviour: If the delete spans more than one
629 * line and motion_type == MCHAR and the result is a blank line, make the
630 * delete linewise. Don't do this for the change command or Visual mode.
631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000634 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100636 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 && oap->op_type == OP_DELETE)
638 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200639 ptr = ml_get(oap->end.lnum) + oap->end.col;
640 if (*ptr != NUL)
641 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 ptr = skipwhite(ptr);
643 if (*ptr == NUL && inindent(0))
644 oap->motion_type = MLINE;
645 }
646
Bram Moolenaard04b7502010-07-08 22:27:55 +0200647 /*
648 * Check for trying to delete (e.g. "D") in an empty line.
649 * Note: For the change operator it is ok.
650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if ( oap->motion_type == MCHAR
652 && oap->line_count == 1
653 && oap->op_type == OP_DELETE
654 && *ml_get(oap->start.lnum) == NUL)
655 {
656 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000657 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 * 'cpoptions' (Vi compatible).
659 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000660 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100661 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
662 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000663 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
665 beep_flush();
666 return OK;
667 }
668
Bram Moolenaard04b7502010-07-08 22:27:55 +0200669 /*
670 * Do a yank of whatever we're about to delete.
671 * If a yank register was specified, put the deleted text into that
672 * register. For the black hole register '_' don't yank anything.
673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 if (oap->regname != '_')
675 {
676 if (oap->regname != 0)
677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (!valid_yank_reg(oap->regname, TRUE))
680 {
681 beep_flush();
682 return OK;
683 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
685 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 did_yank = TRUE;
687 }
688
689 /*
690 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100691 * delete contains a line break, or when using a specific operator (Vi
692 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200693 * Use the register name from before adjust_clip_reg() may have
694 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100696 if (oap->motion_type == MLINE || oap->line_count > 1
697 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100699 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (op_yank(oap, TRUE, FALSE) == OK)
701 did_yank = TRUE;
702 }
703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // Yank into small delete register when no named register specified
705 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200706 if ((
707#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200708 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
709 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200710#endif
711 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 && oap->line_count == 1)
713 {
714 oap->regname = '-';
715 get_yank_register(oap->regname, TRUE);
716 if (op_yank(oap, TRUE, FALSE) == OK)
717 did_yank = TRUE;
718 oap->regname = 0;
719 }
720
721 /*
722 * If there's too much stuff to fit in the yank register, then get a
723 * confirmation before doing the delete. This is crude, but simple.
724 * And it avoids doing a delete of something we can't put back if we
725 * want.
726 */
727 if (!did_yank)
728 {
729 int msg_silent_save = msg_silent;
730
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100731 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
733 msg_silent = msg_silent_save;
734 if (n != 'y')
735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100736 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 return FAIL;
738 }
739 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100740
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100741#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100742 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200743 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746
Bram Moolenaard04b7502010-07-08 22:27:55 +0200747 /*
748 * block mode delete
749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (oap->block_mode)
751 {
752 if (u_save((linenr_T)(oap->start.lnum - 1),
753 (linenr_T)(oap->end.lnum + 1)) == FAIL)
754 return FAIL;
755
756 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
757 {
758 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100759 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 continue;
761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100762 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (lnum == curwin->w_cursor.lnum)
764 {
765 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768
Bram Moolenaar8055d172019-05-17 22:57:26 +0200769 // "n" == number of chars deleted
770 // If we delete a TAB, it may be replaced by several characters.
771 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 n = bd.textlen - bd.startspaces - bd.endspaces;
773 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200774 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (newp == NULL)
776 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100777 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100779 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200780 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100782 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000784 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200787
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100788#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200789 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200790 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793
794 check_cursor_col();
795 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
796 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100799 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
801 if (oap->op_type == OP_CHANGE)
802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100803 // Delete the lines except the first one. Temporarily move the
804 // cursor to the next line. Save the current line number, if the
805 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (oap->line_count > 1)
807 {
808 lnum = curwin->w_cursor.lnum;
809 ++curwin->w_cursor.lnum;
810 del_lines((long)(oap->line_count - 1), TRUE);
811 curwin->w_cursor.lnum = lnum;
812 }
813 if (u_save_cursor() == FAIL)
814 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100815 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100817 beginline(BL_WHITE); // cursor on first non-white
818 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 ai_col = curwin->w_cursor.col;
820 }
821 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100822 beginline(0); // cursor in column 0
823 truncate_line(FALSE); // delete the rest of the line
824 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
828 else
829 {
830 del_lines(oap->line_count, TRUE);
831 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100832 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 }
835 else
836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (virtual_op)
838 {
839 int endcol = 0;
840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (gchar_pos(&oap->start) == '\t')
843 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 return FAIL;
846 if (oap->line_count == 1)
847 endcol = getviscol2(oap->end.col, oap->end.coladd);
848 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
849 oap->start = curwin->w_cursor;
850 if (oap->line_count == 1)
851 {
852 coladvance(endcol);
853 oap->end.col = curwin->w_cursor.col;
854 oap->end.coladd = curwin->w_cursor.coladd;
855 curwin->w_cursor = oap->start;
856 }
857 }
858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (gchar_pos(&oap->end) == '\t'
861 && (int)oap->end.coladd < oap->inclusive)
862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (u_save((linenr_T)(oap->end.lnum - 1),
865 (linenr_T)(oap->end.lnum + 1)) == FAIL)
866 return FAIL;
867 curwin->w_cursor = oap->end;
868 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
869 oap->end = curwin->w_cursor;
870 curwin->w_cursor = oap->start;
871 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100872 if (has_mbyte)
873 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100876 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 return FAIL;
880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100881 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100882 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 && oap->op_type == OP_CHANGE
884 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100885 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 display_dollar(oap->end.col - !oap->inclusive);
887
888 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (virtual_op)
891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100892 // fix up things for virtualedit-delete:
893 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 char_u *curline = ml_get_curline();
895 int len = (int)STRLEN(curline);
896
897 if (oap->end.coladd != 0
898 && (int)oap->end.col >= len - 1
899 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
900 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (n == 0 && oap->start.coladd != oap->end.coladd)
903 n = 1;
904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100905 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (gchar_cursor() != NUL)
907 curwin->w_cursor.coladd = 0;
908 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200909 (void)del_bytes((long)n, !virtual_op,
910 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100912 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
914 pos_T curpos;
915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100916 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
918 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
919 return FAIL;
920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100921 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100923 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 ++curwin->w_cursor.lnum;
925 del_lines((long)(oap->line_count - 2), FALSE);
926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100927 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200928 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200929 curwin->w_cursor.col = 0;
930 (void)del_bytes((long)n, !virtual_op,
931 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100932 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200933 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935 }
936
937 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
938
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000939setmarks:
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100940 if (!cmdmod.lockmarks)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100942 if (oap->block_mode)
943 {
944 curbuf->b_op_end.lnum = oap->end.lnum;
945 curbuf->b_op_end.col = oap->start.col;
946 }
947 else
948 curbuf->b_op_end = oap->start;
949 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 return OK;
953}
954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955/*
956 * Adjust end of operating area for ending on a multi-byte character.
957 * Used for deletion.
958 */
959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100960mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 char_u *p;
963
964 if (oap->inclusive)
965 {
966 p = ml_get(oap->end.lnum);
967 oap->end.col += mb_tail_off(p, p + oap->end.col);
968 }
969}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
Bram Moolenaar630afe82018-06-28 19:26:28 +0200971/*
972 * Replace the character under the cursor with "c".
973 * This takes care of multi-byte characters.
974 */
975 static void
976replace_character(int c)
977{
978 int n = State;
979
980 State = REPLACE;
981 ins_char(c);
982 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100983 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200984 dec_cursor();
985}
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987/*
988 * Replace a whole area with one character.
989 */
990 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100991op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 char_u *newp, *oldp;
996 size_t oldlen;
997 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +0100998 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +0100999 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001002 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001004 if (c == REPLACE_CR_NCHAR)
1005 {
1006 had_ctrl_v_cr = TRUE;
1007 c = CAR;
1008 }
1009 else if (c == REPLACE_NL_NCHAR)
1010 {
1011 had_ctrl_v_cr = TRUE;
1012 c = NL;
1013 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (has_mbyte)
1016 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 if (u_save((linenr_T)(oap->start.lnum - 1),
1019 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1020 return FAIL;
1021
1022 /*
1023 * block mode replace
1024 */
1025 if (oap->block_mode)
1026 {
1027 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1028 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1029 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001030 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1032 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001033 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001035 // n == number of extra chars required
1036 // If we split a TAB, it may be replaced by several characters.
1037 // Thus the number of characters may increase!
1038 // If the range starts in virtual space, count the initial
1039 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1041 {
1042 pos_T vpos;
1043
Bram Moolenaara1381de2009-11-03 15:44:21 +00001044 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 getvpos(&vpos, oap->start_vcol);
1046 bd.startspaces += vpos.coladd;
1047 n = bd.startspaces;
1048 }
1049 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001050 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001057 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 numc = oap->end_vcol - oap->start_vcol + 1;
1059 if (bd.is_short && (!virtual_op || bd.is_MAX))
1060 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001062 // A double-wide character can be replaced only up to half the
1063 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if ((*mb_char2cells)(c) > 1)
1065 {
1066 if ((numc & 1) && !bd.is_short)
1067 {
1068 ++bd.endspaces;
1069 ++n;
1070 }
1071 numc = numc / 2;
1072 }
1073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001074 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 num_chars = numc;
1076 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 n += numc - bd.textlen;
1079
1080 oldp = ml_get_curline();
1081 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001082 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (newp == NULL)
1084 continue;
1085 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001086 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 mch_memmove(newp, oldp, (size_t)bd.textcol);
1088 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001089 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001090 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001091 // insert replacement chars CHECK FOR ALLOCATED SPACE
1092 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1093 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001094 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001096 if (has_mbyte)
1097 {
1098 n = (int)STRLEN(newp);
1099 while (--num_chars >= 0)
1100 n += (*mb_char2bytes)(c, newp + n);
1101 }
1102 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001103 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001104 if (!bd.is_short)
1105 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001106 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001107 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001108 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001109 STRMOVE(newp + STRLEN(newp), oldp);
1110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001115 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001116 if (after_p != NULL)
1117 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001119 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001121 if (after_p != NULL)
1122 {
1123 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1124 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1125 oap->end.lnum++;
1126 vim_free(after_p);
1127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 }
1130 else
1131 {
1132 /*
1133 * MCHAR and MLINE motion replace.
1134 */
1135 if (oap->motion_type == MLINE)
1136 {
1137 oap->start.col = 0;
1138 curwin->w_cursor.col = 0;
1139 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1140 if (oap->end.col)
1141 --oap->end.col;
1142 }
1143 else if (!oap->inclusive)
1144 dec(&(oap->end));
1145
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001146 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
1148 n = gchar_cursor();
1149 if (n != NUL)
1150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1152 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001153 // This is slow, but it handles replacing a single-byte
1154 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001155 if (curwin->w_cursor.lnum == oap->end.lnum)
1156 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001157 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (n == TAB)
1162 {
1163 int end_vcol = 0;
1164
1165 if (curwin->w_cursor.lnum == oap->end.lnum)
1166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001167 // oap->end has to be recalculated when
1168 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 end_vcol = getviscol2(oap->end.col,
1170 oap->end.coladd);
1171 }
1172 coladvance_force(getviscol());
1173 if (curwin->w_cursor.lnum == oap->end.lnum)
1174 getvpos(&oap->end, end_vcol);
1175 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001176 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
1178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1180 {
1181 int virtcols = oap->end.coladd;
1182
1183 if (curwin->w_cursor.lnum == oap->start.lnum
1184 && oap->start.col == oap->end.col && oap->start.coladd)
1185 virtcols -= oap->start.coladd;
1186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001187 // oap->end has been trimmed so it's effectively inclusive;
1188 // as a result an extra +1 must be counted so we don't
1189 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1191 curwin->w_cursor.col -= (virtcols + 1);
1192 for (; virtcols >= 0; virtcols--)
1193 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001194 if ((*mb_char2len)(c) > 1)
1195 replace_character(c);
1196 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001197 PBYTE(curwin->w_cursor, c);
1198 if (inc(&curwin->w_cursor) == -1)
1199 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 }
1201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001203 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (inc_cursor() == -1)
1205 break;
1206 }
1207 }
1208
1209 curwin->w_cursor = oap->start;
1210 check_cursor();
1211 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1212
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001213 if (!cmdmod.lockmarks)
1214 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001215 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001216 curbuf->b_op_start = oap->start;
1217 curbuf->b_op_end = oap->end;
1218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 return OK;
1221}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001223static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225/*
1226 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1227 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001229op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
1231 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001233 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
1235 if (u_save((linenr_T)(oap->start.lnum - 1),
1236 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1237 return;
1238
1239 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
1242 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1243 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001244 int one_change;
1245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 block_prep(oap, &bd, pos.lnum, FALSE);
1247 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001248 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1249 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001250
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001251#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001252 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
1254 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1255
Bram Moolenaar009b2592004-10-24 19:18:58 +00001256 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1257 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001259 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263 if (did_change)
1264 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1265 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001266 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268 if (oap->motion_type == MLINE)
1269 {
1270 oap->start.col = 0;
1271 pos.col = 0;
1272 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1273 if (oap->end.col)
1274 --oap->end.col;
1275 }
1276 else if (!oap->inclusive)
1277 dec(&(oap->end));
1278
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001279 if (pos.lnum == oap->end.lnum)
1280 did_change = swapchars(oap->op_type, &pos,
1281 oap->end.col - pos.col + 1);
1282 else
1283 for (;;)
1284 {
1285 did_change |= swapchars(oap->op_type, &pos,
1286 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1287 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001288 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001289 break;
1290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (did_change)
1292 {
1293 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1294 0L);
1295#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001296 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {
1298 char_u *ptr;
1299 int count;
1300
1301 pos = oap->start;
1302 while (pos.lnum < oap->end.lnum)
1303 {
1304 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001305 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001306 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001308 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 pos.col = 0;
1310 pos.lnum++;
1311 }
1312 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1313 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001314 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001316 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318#endif
1319 }
1320 }
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001323 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001326 if (!cmdmod.lockmarks)
1327 {
1328 // Set '[ and '] marks.
1329 curbuf->b_op_start = oap->start;
1330 curbuf->b_op_end = oap->end;
1331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001334 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001335 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336}
1337
1338/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001339 * Invoke swapchar() on "length" bytes at position "pos".
1340 * "pos" is advanced to just after the changed characters.
1341 * "length" is rounded up to include the whole last multi-byte character.
1342 * Also works correctly when the number of bytes changes.
1343 * Returns TRUE if some character was changed.
1344 */
1345 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001346swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001347{
1348 int todo;
1349 int did_change = 0;
1350
1351 for (todo = length; todo > 0; --todo)
1352 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001353 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001354 {
1355 int len = (*mb_ptr2len)(ml_get_pos(pos));
1356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001357 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001358 if (len > 0)
1359 todo -= len - 1;
1360 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001361 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001362 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001363 break;
1364 }
1365 return did_change;
1366}
1367
1368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * If op_type == OP_UPPER: make uppercase,
1370 * if op_type == OP_LOWER: make lowercase,
1371 * if op_type == OP_ROT13: do rot13 encoding,
1372 * else swap case of character at 'pos'
1373 * returns TRUE when something actually changed.
1374 */
1375 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001376swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 int c;
1379 int nc;
1380
1381 c = gchar_pos(pos);
1382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001383 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (c >= 0x80 && op_type == OP_ROT13)
1385 return FALSE;
1386
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001387 if (op_type == OP_UPPER && c == 0xdf
1388 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001389 {
1390 pos_T sp = curwin->w_cursor;
1391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001392 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001393 curwin->w_cursor = *pos;
1394 del_char(FALSE);
1395 ins_char('S');
1396 ins_char('S');
1397 curwin->w_cursor = sp;
1398 inc(pos);
1399 }
1400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 nc = c;
1404 if (MB_ISLOWER(c))
1405 {
1406 if (op_type == OP_ROT13)
1407 nc = ROT13(c, 'a');
1408 else if (op_type != OP_LOWER)
1409 nc = MB_TOUPPER(c);
1410 }
1411 else if (MB_ISUPPER(c))
1412 {
1413 if (op_type == OP_ROT13)
1414 nc = ROT13(c, 'A');
1415 else if (op_type != OP_UPPER)
1416 nc = MB_TOLOWER(c);
1417 }
1418 if (nc != c)
1419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1421 {
1422 pos_T sp = curwin->w_cursor;
1423
1424 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001426 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 ins_char(nc);
1428 curwin->w_cursor = sp;
1429 }
1430 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001431 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 return TRUE;
1433 }
1434 return FALSE;
1435}
1436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437/*
1438 * op_insert - Insert and append operators for Visual mode.
1439 */
1440 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001441op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 long ins_len, pre_textlen = 0;
1444 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001445 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 struct block_def bd;
1447 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001448 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001450 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 curwin->w_cursor.lnum = oap->start.lnum;
1455 update_screen(INVERTED);
1456
1457 if (oap->block_mode)
1458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001459 // When 'virtualedit' is used, need to insert the extra spaces before
1460 // doing block_prep(). When only "block" is used, virtual edit is
1461 // already disabled, but still need it when calling
1462 // coladvance_force().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (curwin->w_cursor.coladd > 0)
1464 {
1465 int old_ve_flags = ve_flags;
1466
1467 ve_flags = VE_ALL;
1468 if (u_save_cursor() == FAIL)
1469 return;
1470 coladvance_force(oap->op_type == OP_APPEND
1471 ? oap->end_vcol + 1 : getviscol());
1472 if (oap->op_type == OP_APPEND)
1473 --curwin->w_cursor.col;
1474 ve_flags = old_ve_flags;
1475 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001478 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001479 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (oap->op_type == OP_APPEND)
1483 firstline += bd.textlen;
1484 pre_textlen = (long)STRLEN(firstline);
1485 }
1486
1487 if (oap->op_type == OP_APPEND)
1488 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001489 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001491 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 curwin->w_set_curswant = TRUE;
1493 while (*ml_get_cursor() != NUL
1494 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1495 ++curwin->w_cursor.col;
1496 if (bd.is_short && !bd.is_MAX)
1497 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 // First line was too short, make it longer and adjust the
1499 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (u_save_cursor() == FAIL)
1501 return;
1502 for (i = 0; i < bd.endspaces; ++i)
1503 ins_char(' ');
1504 bd.textlen += bd.endspaces;
1505 }
1506 }
1507 else
1508 {
1509 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001510 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001512 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001513 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 && oap->start_vcol != oap->end_vcol)
1515 inc_cursor();
1516 }
1517 }
1518
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001519 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001520 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001522 // When a tab was inserted, and the characters in front of the tab
1523 // have been converted to a tab as well, the column of the cursor
1524 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001525 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001526 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001527 oap->start = curbuf->b_op_start_orig;
1528
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 // If user has moved off this line, we don't know what to do, so do
1530 // nothing.
1531 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001532 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 return;
1534
1535 if (oap->block_mode)
1536 {
1537 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001538 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001539 size_t len;
1540 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001542 // If indent kicked in, the firstline might have changed
1543 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001544 ind_post = (colnr_T)getwhitecols_curline();
1545 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1546 {
1547 bd.textcol += ind_post - ind_pre;
1548 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001549 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001550 }
1551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001552 // The user may have moved the cursor before inserting something, try
1553 // to adjust the block for that. But only do it, if the difference
1554 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001555 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1556 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001557 {
1558 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001559 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001560 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001561 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001562 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001563 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001564 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001565 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001566 pre_textlen -= t - oap->start_vcol;
1567 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001568 }
1569 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001570 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001571 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001572 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001573 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001574 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001575 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001576 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001577 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001578 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001579 pre_textlen -= t - oap->start_vcol;
1580 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001581 oap->op_type = OP_INSERT;
1582 }
1583 }
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /*
1586 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001587 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 * Don't do this when "$" used, end-of-line will have changed.
1589 */
1590 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1591 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1592 {
1593 if (oap->op_type == OP_APPEND)
1594 {
1595 pre_textlen += bd2.textlen - bd.textlen;
1596 if (bd2.endspaces)
1597 --bd2.textlen;
1598 }
1599 bd.textcol = bd2.textcol;
1600 bd.textlen = bd2.textlen;
1601 }
1602
1603 /*
1604 * Subsequent calls to ml_get() flush the firstline data - take a
1605 * copy of the required string.
1606 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001607 firstline = ml_get(oap->start.lnum);
1608 len = STRLEN(firstline);
1609 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001611 add += bd.textlen;
1612 if ((size_t)add > len)
1613 firstline += len; // short line, point to the NUL
1614 else
1615 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001616 if (pre_textlen >= 0
1617 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001619 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (ins_text != NULL)
1621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001622 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (u_save(oap->start.lnum,
1624 (linenr_T)(oap->end.lnum + 1)) == OK)
1625 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1626 &bd);
1627
1628 curwin->w_cursor.col = oap->start.col;
1629 check_cursor();
1630 vim_free(ins_text);
1631 }
1632 }
1633 }
1634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636/*
1637 * op_change - handle a change operation
1638 *
1639 * return TRUE if edit() returns because of a CTRL-O command
1640 */
1641 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001642op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 colnr_T l;
1645 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 long offset;
1647 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001648 long ins_len;
1649 long pre_textlen = 0;
1650 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 char_u *firstline;
1652 char_u *ins_text, *newp, *oldp;
1653 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655 l = oap->start.col;
1656 if (oap->motion_type == MLINE)
1657 {
1658 l = 0;
1659#ifdef FEAT_SMARTINDENT
1660 if (!p_paste && curbuf->b_p_si
1661# ifdef FEAT_CINDENT
1662 && !curbuf->b_p_cin
1663# endif
1664 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001665 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666#endif
1667 }
1668
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 // First delete the text in the region. In an empty buffer only need to
1670 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1672 {
1673 if (u_save_cursor() == FAIL)
1674 return FALSE;
1675 }
1676 else if (op_delete(oap) == FAIL)
1677 return FALSE;
1678
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001679 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 && !virtual_op)
1681 inc_cursor();
1682
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001683 // check for still on same line (<CR> in inserted text meaningless)
1684 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (oap->block_mode)
1686 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001687 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (virtual_op && (curwin->w_cursor.coladd > 0
1689 || gchar_cursor() == NUL))
1690 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001691 firstline = ml_get(oap->start.lnum);
1692 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001693 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 bd.textcol = curwin->w_cursor.col;
1695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
1697#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1698 if (oap->motion_type == MLINE)
1699 fix_indent();
1700#endif
1701
1702 retval = edit(NUL, FALSE, (linenr_T)1);
1703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001705 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001707 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001709 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001711 // Auto-indenting may have changed the indent. If the cursor was past
1712 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001714 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001716 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001717
1718 pre_textlen += new_indent - pre_indent;
1719 bd.textcol += new_indent - pre_indent;
1720 }
1721
1722 ins_len = (long)STRLEN(firstline) - pre_textlen;
1723 if (ins_len > 0)
1724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001725 // Subsequent calls to ml_get() flush the firstline data - take a
1726 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001727 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001729 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1731 linenr++)
1732 {
1733 block_prep(oap, &bd, linenr, TRUE);
1734 if (!bd.is_short || virtual_op)
1735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 pos_T vpos;
1737
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001738 // If the block starts in virtual space, count the
1739 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (bd.is_short)
1741 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001742 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
1745 else
1746 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001748 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (newp == NULL)
1750 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001751 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 mch_memmove(newp, oldp, (size_t)bd.textcol);
1753 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001754 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1757 offset += ins_len;
1758 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001759 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ml_replace(linenr, newp, FALSE);
1761 }
1762 }
1763 check_cursor();
1764
1765 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1766 }
1767 vim_free(ins_text);
1768 }
1769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
1771 return retval;
1772}
1773
1774/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001775 * When the cursor is on the NUL past the end of the line and it should not be
1776 * there move it left.
1777 */
1778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001779adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001780{
1781 if (curwin->w_cursor.col > 0
1782 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001783 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 && !(restart_edit || (State & INSERT)))
1785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001786 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001787 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001790 {
1791 colnr_T scol, ecol;
1792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001793 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001794 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1795 curwin->w_cursor.coladd = ecol - scol + 1;
1796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798}
1799
Bram Moolenaar81340392012-06-06 16:12:59 +02001800/*
1801 * If "process" is TRUE and the line begins with a comment leader (possibly
1802 * after some white space), return a pointer to the text after it. Put a boolean
1803 * value indicating whether the line ends with an unclosed comment in
1804 * "is_comment".
1805 * line - line to be processed,
1806 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001807 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001808 * include_space - whether to also skip space following the comment leader,
1809 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001810 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001811 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001812 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001813skip_comment(
1814 char_u *line,
1815 int process,
1816 int include_space,
1817 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001818{
1819 char_u *comment_flags = NULL;
1820 int lead_len;
1821 int leader_offset = get_last_leader_offset(line, &comment_flags);
1822
1823 *is_comment = FALSE;
1824 if (leader_offset != -1)
1825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001826 // Let's check whether the line ends with an unclosed comment.
1827 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001828 while (*comment_flags)
1829 {
1830 if (*comment_flags == COM_END
1831 || *comment_flags == ':')
1832 break;
1833 ++comment_flags;
1834 }
1835 if (*comment_flags != COM_END)
1836 *is_comment = TRUE;
1837 }
1838
1839 if (process == FALSE)
1840 return line;
1841
1842 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1843
1844 if (lead_len == 0)
1845 return line;
1846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001847 // Find:
1848 // - COM_END,
1849 // - colon,
1850 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001851 while (*comment_flags)
1852 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001853 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001854 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001855 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001856 ++comment_flags;
1857 }
1858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001859 // If we found a colon, it means that we are not processing a line
1860 // starting with a closing part of a three-part comment. That's good,
1861 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 if (*comment_flags == ':' || *comment_flags == NUL)
1863 line += lead_len;
1864
1865 return line;
1866}
Bram Moolenaar81340392012-06-06 16:12:59 +02001867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001869 * Join 'count' lines (minimal 2) at cursor position.
1870 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001871 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1872 * leaders should not be removed.
1873 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1874 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001876 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 */
1878 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001879do_join(
1880 long count,
1881 int insert_space,
1882 int save_undo,
1883 int use_formatoptions UNUSED,
1884 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001886 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001887 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001888 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001890 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001891 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001892 int endcurr1 = NUL;
1893 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001894 int currsize = 0; // size of the current line
1895 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001897 colnr_T col = 0;
1898 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001899 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001900 int remove_comments = (use_formatoptions == TRUE)
1901 && has_format_option(FO_REMOVE_COMS);
1902 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001903#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001904 int propcount = 0; // number of props over all joined lines
1905 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001908 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1909 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1910 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001912 // Allocate an array to store the number of spaces inserted before each
1913 // line. We will use it to pre-compute the length of the new line and the
1914 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001915 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001916 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001918 if (remove_comments)
1919 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001920 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001921 if (comments == NULL)
1922 {
1923 vim_free(spaces);
1924 return FAIL;
1925 }
1926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001929 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001930 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001931 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001933 for (t = 0; t < count; ++t)
1934 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001935 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001936#ifdef FEAT_PROP_POPUP
1937 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1938#endif
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001939 if (t == 0 && setmark && !cmdmod.lockmarks)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001940 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001941 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001942 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1943 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1944 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001945 if (remove_comments)
1946 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // We don't want to remove the comment leader if the
1948 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001949 if (t > 0 && prev_was_comment)
1950 {
1951
1952 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1953 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001954 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001955 curr = new_curr;
1956 }
1957 else
1958 curr = skip_comment(curr, FALSE, insert_space,
1959 &prev_was_comment);
1960 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001961
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001962 if (insert_space && t > 0)
1963 {
1964 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01001965 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01001966 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001967 && (!has_format_option(FO_MBYTE_JOIN)
1968 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
1969 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02001970 || (mb_ptr2char(curr) < 0x100
1971 && !(enc_utf8 && utf_eat_space(endcurr1)))
1972 || (endcurr1 < 0x100
1973 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001974 )
1975 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001976 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001977 if (endcurr1 == ' ')
1978 endcurr1 = endcurr2;
1979 else
1980 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001981 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001982 if ( p_js
1983 && (endcurr1 == '.'
1984 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
1985 && (endcurr1 == '?' || endcurr1 == '!'))))
1986 ++spaces[t];
1987 }
1988 }
1989 currsize = (int)STRLEN(curr);
1990 sumsize += currsize + spaces[t];
1991 endcurr1 = endcurr2 = NUL;
1992 if (insert_space && currsize > 0)
1993 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001994 if (has_mbyte)
1995 {
1996 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001997 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001998 endcurr1 = (*mb_ptr2char)(cend);
1999 if (cend > curr)
2000 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002001 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002002 endcurr2 = (*mb_ptr2char)(cend);
2003 }
2004 }
2005 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002006 {
2007 endcurr1 = *(curr + currsize - 1);
2008 if (currsize > 1)
2009 endcurr2 = *(curr + currsize - 2);
2010 }
2011 }
2012 line_breakcheck();
2013 if (got_int)
2014 {
2015 ret = FAIL;
2016 goto theend;
2017 }
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002020 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002021 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002023 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002024 newp_len = sumsize + 1;
2025#ifdef FEAT_PROP_POPUP
2026 newp_len += propcount * sizeof(textprop_T);
2027#endif
2028 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002029 if (newp == NULL)
2030 {
2031 ret = FAIL;
2032 goto theend;
2033 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002034 cend = newp + sumsize;
2035 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002037 /*
2038 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002039 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002040 *
2041 * Move marks from each deleted line to the joined line, adjusting the
2042 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2043 * should not really be a problem.
2044 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002045#ifdef FEAT_PROP_POPUP
2046 props_remaining = propcount;
2047#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002048 for (t = count - 1; ; --t)
2049 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002050 int spaces_removed;
2051
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002052 cend -= currsize;
2053 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002054
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002055 if (spaces[t] > 0)
2056 {
2057 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002058 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002059 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002060
2061 // If deleting more spaces than adding, the cursor moves no more than
2062 // what is added if it is inside these spaces.
2063 spaces_removed = (curr - curr_start) - spaces[t];
2064
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002066 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002067#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002068 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2069 curwin->w_cursor.lnum + t, t == count - 1,
2070 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002071#endif
2072
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002073 if (t == 0)
2074 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002075 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002076 if (remove_comments)
2077 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002078 if (insert_space && t > 1)
2079 curr = skipwhite(curr);
2080 currsize = (int)STRLEN(curr);
2081 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002082
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002083 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002085 if (setmark && !cmdmod.lockmarks)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002086 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002087 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002088 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002089 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002090 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 // Only report the change in the first line here, del_lines() will report
2093 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 changed_lines(curwin->w_cursor.lnum, currsize,
2095 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002097 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 * briefly, and then move it back. After del_lines() the cursor may
2099 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 */
2101 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002103 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 curwin->w_cursor.lnum = t;
2105
2106 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002107 * Set the cursor column:
2108 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002109 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002111 curwin->w_cursor.col =
2112 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 curwin->w_set_curswant = TRUE;
2117
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002118theend:
2119 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002120 if (remove_comments)
2121 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002122 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123}
2124
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 * prepare a few things for block mode yank/delete/tilde
2127 *
2128 * for delete:
2129 * - textlen includes the first/last char to be (partly) deleted
2130 * - start/endspaces is the number of columns that are taken by the
2131 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002132 * deleted.
2133 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 * - textlen includes the first/last char to be wholly yanked
2135 * - start/endspaces is the number of columns of the first/last yanked char
2136 * that are to be yanked.
2137 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139block_prep(
2140 oparg_T *oap,
2141 struct block_def *bdp,
2142 linenr_T lnum,
2143 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 int incr = 0;
2146 char_u *pend;
2147 char_u *pstart;
2148 char_u *line;
2149 char_u *prev_pstart;
2150 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002151#ifdef FEAT_LINEBREAK
2152 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002154 // Avoid a problem with unwanted linebreaks in block mode.
2155 curwin->w_p_lbr = FALSE;
2156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 bdp->startspaces = 0;
2158 bdp->endspaces = 0;
2159 bdp->textlen = 0;
2160 bdp->start_vcol = 0;
2161 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 bdp->is_short = FALSE;
2163 bdp->is_oneChar = FALSE;
2164 bdp->pre_whitesp = 0;
2165 bdp->pre_whitesp_c = 0;
2166 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 bdp->start_char_vcols = 0;
2168
2169 line = ml_get(lnum);
2170 pstart = line;
2171 prev_pstart = line;
2172 while (bdp->start_vcol < oap->start_vcol && *pstart)
2173 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002174 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002175 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002177 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 bdp->pre_whitesp += incr;
2180 bdp->pre_whitesp_c++;
2181 }
2182 else
2183 {
2184 bdp->pre_whitesp = 0;
2185 bdp->pre_whitesp_c = 0;
2186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002188 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
2190 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002191 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (!is_del || oap->op_type == OP_APPEND)
2196 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2197 }
2198 else
2199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // notice: this converts partly selected Multibyte characters to
2201 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2203 if (is_del && bdp->startspaces)
2204 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2205 pend = pstart;
2206 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (oap->op_type == OP_INSERT)
2211 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2212 else if (oap->op_type == OP_APPEND)
2213 {
2214 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2215 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2216 }
2217 else
2218 {
2219 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2220 if (is_del && oap->op_type != OP_LSHIFT)
2221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 // just putting the sum of those two into
2223 // bdp->startspaces doesn't work for Visual replace,
2224 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 bdp->startspaces = bdp->start_char_vcols
2226 - (bdp->start_vcol - oap->start_vcol);
2227 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2228 }
2229 }
2230 }
2231 else
2232 {
2233 prev_pend = pend;
2234 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2235 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002236 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002238 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 bdp->end_vcol += incr;
2240 }
2241 if (bdp->end_vcol <= oap->end_vcol
2242 && (!is_del
2243 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002247 // Alternative: include spaces to fill up the block.
2248 // Disadvantage: can lead to trailing spaces when the line is
2249 // short where the text is put
2250 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 if (oap->op_type == OP_APPEND || virtual_op)
2252 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002253 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257 else if (bdp->end_vcol > oap->end_vcol)
2258 {
2259 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2260 if (!is_del && bdp->endspaces)
2261 {
2262 bdp->endspaces = incr - bdp->endspaces;
2263 if (pend != pstart)
2264 pend = prev_pend;
2265 }
2266 }
2267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (is_del && bdp->startspaces)
2270 pstart = prev_pstart;
2271 bdp->textlen = (int)(pend - pstart);
2272 }
2273 bdp->textcol = (colnr_T) (pstart - line);
2274 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002275#ifdef FEAT_LINEBREAK
2276 curwin->w_p_lbr = lbr_saved;
2277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002281 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002283 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284op_addsub(
2285 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002286 linenr_T Prenum1, // Amount of add/subtract
2287 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002289 pos_T pos;
2290 struct block_def bd;
2291 int change_cnt = 0;
2292 linenr_T amount = Prenum1;
2293
Bram Moolenaar6b731882018-11-16 20:54:47 +01002294 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002295 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002296 // the call to changed_lines().
2297#ifdef FEAT_FOLDING
2298 disable_fold_update++;
2299#endif
2300
Bram Moolenaard79e5502016-01-10 22:13:02 +01002301 if (!VIsual_active)
2302 {
2303 pos = curwin->w_cursor;
2304 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002305 {
2306#ifdef FEAT_FOLDING
2307 disable_fold_update--;
2308#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002309 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002310 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002311 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002312#ifdef FEAT_FOLDING
2313 disable_fold_update--;
2314#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002315 if (change_cnt)
2316 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2317 }
2318 else
2319 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002320 int one_change;
2321 int length;
2322 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002323
2324 if (u_save((linenr_T)(oap->start.lnum - 1),
2325 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002326 {
2327#ifdef FEAT_FOLDING
2328 disable_fold_update--;
2329#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002330 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002331 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002332
2333 pos = oap->start;
2334 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002336 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002337 {
2338 block_prep(oap, &bd, pos.lnum, FALSE);
2339 pos.col = bd.textcol;
2340 length = bd.textlen;
2341 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002342 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002343 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002344 curwin->w_cursor.col = 0;
2345 pos.col = 0;
2346 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2347 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002348 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002349 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002350 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002351 dec(&(oap->end));
2352 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2353 pos.col = 0;
2354 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002355 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002356 pos.col += oap->start.col;
2357 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002358 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002359 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002360 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002361 length = (int)STRLEN(ml_get(oap->end.lnum));
2362 if (oap->end.col >= length)
2363 oap->end.col = length - 1;
2364 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002365 }
2366 }
2367 one_change = do_addsub(oap->op_type, &pos, length, amount);
2368 if (one_change)
2369 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002370 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002371 if (change_cnt == 0)
2372 startpos = curbuf->b_op_start;
2373 ++change_cnt;
2374 }
2375
2376#ifdef FEAT_NETBEANS_INTG
2377 if (netbeans_active() && one_change)
2378 {
2379 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2380
2381 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2382 netbeans_inserted(curbuf, pos.lnum, pos.col,
2383 &ptr[pos.col], length);
2384 }
2385#endif
2386 if (g_cmd && one_change)
2387 amount += Prenum1;
2388 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002389
2390#ifdef FEAT_FOLDING
2391 disable_fold_update--;
2392#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002393 if (change_cnt)
2394 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2395
2396 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002397 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002398 redraw_curbuf_later(INVERTED);
2399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002400 // Set '[ mark if something changed. Keep the last end
2401 // position from do_addsub().
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002402 if (change_cnt > 0 && !cmdmod.lockmarks)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002403 curbuf->b_op_start = startpos;
2404
2405 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002406 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002407 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002408 }
2409}
2410
2411/*
2412 * Add or subtract 'Prenum1' from a number in a line
2413 * op_type is OP_NR_ADD or OP_NR_SUB
2414 *
2415 * Returns TRUE if some character was changed.
2416 */
2417 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002418do_addsub(
2419 int op_type,
2420 pos_T *pos,
2421 int length,
2422 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002423{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 int col;
2425 char_u *buf1;
2426 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002427 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2428 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002429 uvarnumber_T n;
2430 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 char_u *ptr;
2432 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002434 int do_hex;
2435 int do_oct;
2436 int do_bin;
2437 int do_alpha;
2438 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002441 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002442 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002443 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002444 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002445 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002446 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002447 pos_T startpos;
2448 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002449 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002451 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2452 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2453 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2454 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2455 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002457 if (virtual_active())
2458 {
2459 save_coladd = pos->coladd;
2460 pos->coladd = 0;
2461 }
2462
Bram Moolenaard79e5502016-01-10 22:13:02 +01002463 curwin->w_cursor = *pos;
2464 ptr = ml_get(pos->lnum);
2465 col = pos->col;
2466
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002467 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002468 goto theend;
2469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 /*
2471 * First check if we are on a hexadecimal number, after the "0x".
2472 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002473 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002475 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002476 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002477 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002478 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002479 if (has_mbyte)
2480 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002481 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002482
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002483 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002484 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002485 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002486 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002487 if (has_mbyte)
2488 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002489 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002490
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002491 if ( do_bin
2492 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002493 && ! ((col > 0
2494 && (ptr[col] == 'X'
2495 || ptr[col] == 'x')
2496 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002497 && (!has_mbyte ||
2498 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002499 && vim_isxdigit(ptr[col + 1]))))
2500 {
2501
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002502 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002503
Bram Moolenaard79e5502016-01-10 22:13:02 +01002504 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002505
2506 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002507 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002508 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002509 if (has_mbyte)
2510 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002511 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002512 }
2513
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002514 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002515 && col > 0
2516 && (ptr[col] == 'X'
2517 || ptr[col] == 'x')
2518 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002519 && (!has_mbyte ||
2520 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002521 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002522 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002523 && col > 0
2524 && (ptr[col] == 'B'
2525 || ptr[col] == 'b')
2526 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002527 && (!has_mbyte ||
2528 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002529 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002532 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002533 if (has_mbyte)
2534 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002535 }
2536 else
2537 {
2538 /*
2539 * Search forward and then backward to find the start of number.
2540 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002541 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002542
2543 while (ptr[col] != NUL
2544 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002545 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002546 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002547
2548 while (col > 0
2549 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002550 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002551 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002552 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002553 if (has_mbyte)
2554 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002555 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002556 }
2557 }
2558
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002559 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002560 {
2561 while (ptr[col] != NUL && length > 0
2562 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002563 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002564 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002565 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002566
2567 col += mb_len;
2568 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002569 }
2570
2571 if (length == 0)
2572 goto theend;
2573
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002574 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002575 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2576 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002577 {
2578 negative = TRUE;
2579 was_positive = FALSE;
2580 }
2581 }
2582
2583 /*
2584 * If a number was found, and saving for undo works, replace the number.
2585 */
2586 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002587 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002588 {
2589 beep_flush();
2590 goto theend;
2591 }
2592
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002593 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002595 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002596 if (op_type == OP_NR_SUB)
2597 {
2598 if (CharOrd(firstdigit) < Prenum1)
2599 {
2600 if (isupper(firstdigit))
2601 firstdigit = 'A';
2602 else
2603 firstdigit = 'a';
2604 }
2605 else
2606#ifdef EBCDIC
2607 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2608#else
2609 firstdigit -= Prenum1;
2610#endif
2611 }
2612 else
2613 {
2614 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2615 {
2616 if (isupper(firstdigit))
2617 firstdigit = 'Z';
2618 else
2619 firstdigit = 'z';
2620 }
2621 else
2622#ifdef EBCDIC
2623 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2624#else
2625 firstdigit += Prenum1;
2626#endif
2627 }
2628 curwin->w_cursor.col = col;
2629 if (!did_change)
2630 startpos = curwin->w_cursor;
2631 did_change = TRUE;
2632 (void)del_char(FALSE);
2633 ins_char(firstdigit);
2634 endpos = curwin->w_cursor;
2635 curwin->w_cursor.col = col;
2636 }
2637 else
2638 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002639 pos_T save_pos;
2640 int i;
2641
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002642 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002643 && (!has_mbyte ||
2644 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002645 && !visual
2646 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002647 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002648 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002649 --col;
2650 negative = TRUE;
2651 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002652 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002653 if (visual && VIsual_mode != 'V')
2654 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2655 ? (int)STRLEN(ptr) - col
2656 : length);
2657
2658 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002659 0 + (do_bin ? STR2NR_BIN : 0)
2660 + (do_oct ? STR2NR_OCT : 0)
2661 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002662 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002663
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002665 if (pre && negative)
2666 {
2667 ++col;
2668 --length;
2669 negative = FALSE;
2670 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002672 subtract = FALSE;
2673 if (op_type == OP_NR_SUB)
2674 subtract ^= TRUE;
2675 if (negative)
2676 subtract ^= TRUE;
2677
2678 oldn = n;
2679 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002680 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002681 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002682 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002683 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002684 if (!pre)
2685 {
2686 if (subtract)
2687 {
2688 if (n > oldn)
2689 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002690 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002691 negative ^= TRUE;
2692 }
2693 }
2694 else
2695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002697 if (n < oldn)
2698 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002699 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002700 negative ^= TRUE;
2701 }
2702 }
2703 if (n == 0)
2704 negative = FALSE;
2705 }
2706
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002707 if (do_unsigned && negative)
2708 {
2709 if (subtract)
2710 // sticking at zero.
2711 n = (uvarnumber_T)0;
2712 else
2713 // sticking at 2^64 - 1.
2714 n = (uvarnumber_T)(-1);
2715 negative = FALSE;
2716 }
2717
Bram Moolenaard79e5502016-01-10 22:13:02 +01002718 if (visual && !was_positive && !negative && col > 0)
2719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002721 col--;
2722 length++;
2723 }
2724
2725 /*
2726 * Delete the old number.
2727 */
2728 curwin->w_cursor.col = col;
2729 if (!did_change)
2730 startpos = curwin->w_cursor;
2731 did_change = TRUE;
2732 todel = length;
2733 c = gchar_cursor();
2734 /*
2735 * Don't include the '-' in the length, only the length of the
2736 * part after it is kept the same.
2737 */
2738 if (c == '-')
2739 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002740
2741 save_pos = curwin->w_cursor;
2742 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002743 {
2744 if (c < 0x100 && isalpha(c))
2745 {
2746 if (isupper(c))
2747 hexupper = TRUE;
2748 else
2749 hexupper = FALSE;
2750 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002751 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002752 c = gchar_cursor();
2753 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002754 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002755
2756 /*
2757 * Prepare the leading characters in buf1[].
2758 * When there are many leading zeros it could be very long.
2759 * Allocate a bit too much.
2760 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002761 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002762 if (buf1 == NULL)
2763 goto theend;
2764 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002765 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002766 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002767 if (pre)
2768 {
2769 *ptr++ = '0';
2770 --length;
2771 }
2772 if (pre == 'b' || pre == 'B' ||
2773 pre == 'x' || pre == 'X')
2774 {
2775 *ptr++ = pre;
2776 --length;
2777 }
2778
2779 /*
2780 * Put the number characters in buf2[].
2781 */
2782 if (pre == 'b' || pre == 'B')
2783 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002784 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002785 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002787 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002788 for (bit = bits; bit > 0; bit--)
2789 if ((n >> (bit - 1)) & 0x1) break;
2790
2791 for (i = 0; bit > 0; bit--)
2792 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2793
2794 buf2[i] = '\0';
2795 }
2796 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002797 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002798 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002799 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002800 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002801 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002802 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002803 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002804 length -= (int)STRLEN(buf2);
2805
2806 /*
2807 * Adjust number of zeros to the new number of digits, so the
2808 * total length of the number remains the same.
2809 * Don't do this when
2810 * the result may look like an octal number.
2811 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002812 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002813 while (length-- > 0)
2814 *ptr++ = '0';
2815 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002816
Bram Moolenaard79e5502016-01-10 22:13:02 +01002817 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002818
2819 // Insert just after the first character to be removed, so that any
2820 // text properties will be adjusted. Then delete the old number
2821 // afterwards.
2822 save_pos = curwin->w_cursor;
2823 if (todel > 0)
2824 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002826 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002827
2828 // del_char() will also mark line needing displaying
2829 if (todel > 0)
2830 {
2831 int bytes_after = (int)STRLEN(ml_get_curline())
2832 - curwin->w_cursor.col;
2833
2834 // Delete the one character before the insert.
2835 curwin->w_cursor = save_pos;
2836 (void)del_char(FALSE);
2837 curwin->w_cursor.col = STRLEN(ml_get_curline()) - bytes_after;
2838 --todel;
2839 }
2840 while (todel-- > 0)
2841 (void)del_char(FALSE);
2842
Bram Moolenaard79e5502016-01-10 22:13:02 +01002843 endpos = curwin->w_cursor;
2844 if (did_change && curwin->w_cursor.col)
2845 --curwin->w_cursor.col;
2846 }
2847
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002848 if (did_change && !cmdmod.lockmarks)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002851 curbuf->b_op_start = startpos;
2852 curbuf->b_op_end = endpos;
2853 if (curbuf->b_op_end.col > 0)
2854 --curbuf->b_op_end.col;
2855 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002856
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002857theend:
2858 if (visual)
2859 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002860 else if (did_change)
2861 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002862 else if (virtual_active())
2863 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002864
Bram Moolenaard79e5502016-01-10 22:13:02 +01002865 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866}
2867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002869clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002871 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872}
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002875 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 *
2877 * "Words" are counted by looking for boundaries between non-space and
2878 * space characters. (it seems to produce results that match 'wc'.)
2879 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002880 * Return value is byte count; word count for the line is added to "*wc".
2881 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 *
2883 * The function will only examine the first "limit" characters in the
2884 * line, stopping if it encounters an end-of-line (NUL byte). In that
2885 * case, eol_size will be added to the character count to account for
2886 * the size of the EOL character.
2887 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002888 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889line_count_info(
2890 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002891 varnumber_T *wc,
2892 varnumber_T *cc,
2893 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002894 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002896 varnumber_T i;
2897 varnumber_T words = 0;
2898 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 int is_word = 0;
2900
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002901 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 if (is_word)
2904 {
2905 if (vim_isspace(line[i]))
2906 {
2907 words++;
2908 is_word = 0;
2909 }
2910 }
2911 else if (!vim_isspace(line[i]))
2912 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002913 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002914 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916
2917 if (is_word)
2918 words++;
2919 *wc += words;
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002922 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002925 chars += eol_size;
2926 }
2927 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 return i;
2929}
2930
2931/*
2932 * Give some info about the position of the cursor (for "g CTRL-G").
2933 * In Visual mode, give some info about the selected region. (In this case,
2934 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01002935 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 */
2937 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002938cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00002941 char_u buf1[50];
2942 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002944 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002945 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002946 varnumber_T byte_count_cursor = 0;
2947 varnumber_T char_count = 0;
2948 varnumber_T char_count_cursor = 0;
2949 varnumber_T word_count = 0;
2950 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002951 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002952 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 long line_count_selected = 0;
2954 pos_T min_pos, max_pos;
2955 oparg_T oparg;
2956 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /*
2959 * Compute the length of the file in characters.
2960 */
2961 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2962 {
Bram Moolenaared767a22016-01-03 22:49:16 +01002963 if (dict == NULL)
2964 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002965 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01002966 return;
2967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969 else
2970 {
2971 if (get_fileformat(curbuf) == EOL_DOS)
2972 eol_size = 2;
2973 else
2974 eol_size = 1;
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (VIsual_active)
2977 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002978 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
2980 min_pos = VIsual;
2981 max_pos = curwin->w_cursor;
2982 }
2983 else
2984 {
2985 min_pos = curwin->w_cursor;
2986 max_pos = VIsual;
2987 }
2988 if (*p_sel == 'e' && max_pos.col > 0)
2989 --max_pos.col;
2990
2991 if (VIsual_mode == Ctrl_V)
2992 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00002993#ifdef FEAT_LINEBREAK
2994 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01002995 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00002996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00002998 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01002999 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 oparg.is_VIsual = 1;
3002 oparg.block_mode = TRUE;
3003 oparg.op_type = OP_NOP;
3004 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003005 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003006#ifdef FEAT_LINEBREAK
3007 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003008 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003009#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003010 if (curwin->w_curswant == MAXCOL)
3011 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (oparg.end_vcol < oparg.start_vcol)
3014 {
3015 oparg.end_vcol += oparg.start_vcol;
3016 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3017 oparg.end_vcol -= oparg.start_vcol;
3018 }
3019 }
3020 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003026 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
3028 ui_breakcheck();
3029 if (got_int)
3030 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003031 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 if (VIsual_active
3036 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3037 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003038 char_u *s = NULL;
3039 long len = 0L;
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 switch (VIsual_mode)
3042 {
3043 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003047 s = bd.textstart;
3048 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 break;
3050 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003051 s = ml_get(lnum);
3052 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 break;
3054 case 'v':
3055 {
3056 colnr_T start_col = (lnum == min_pos.lnum)
3057 ? min_pos.col : 0;
3058 colnr_T end_col = (lnum == max_pos.lnum)
3059 ? max_pos.col - start_col + 1 : MAXCOL;
3060
Bram Moolenaardef9e822004-12-31 20:58:58 +00003061 s = ml_get(lnum) + start_col;
3062 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064 break;
3065 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003066 if (s != NULL)
3067 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003068 byte_count_cursor += line_count_info(s, &word_count_cursor,
3069 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003070 if (lnum == curbuf->b_ml.ml_line_count
3071 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003072 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003073 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003074 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (lnum == curwin->w_cursor.lnum)
3081 {
3082 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003083 char_count_cursor += char_count;
3084 byte_count_cursor = byte_count +
3085 line_count_info(ml_get(lnum),
3086 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003087 (varnumber_T)(curwin->w_cursor.col + 1),
3088 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003092 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003093 &char_count, (varnumber_T)MAXCOL,
3094 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 }
3096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003098 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003099 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaared767a22016-01-03 22:49:16 +01003101 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003103 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003105 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3106 {
3107 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3108 &max_pos.col);
3109 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3110 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3111 }
3112 else
3113 buf1[0] = NUL;
3114
3115 if (char_count_cursor == byte_count_cursor
3116 && char_count == byte_count)
3117 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003118 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003119 buf1, line_count_selected,
3120 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003121 (varnumber_T)word_count_cursor,
3122 (varnumber_T)word_count,
3123 (varnumber_T)byte_count_cursor,
3124 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003125 else
3126 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003127 _("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 +01003128 buf1, line_count_selected,
3129 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003130 (varnumber_T)word_count_cursor,
3131 (varnumber_T)word_count,
3132 (varnumber_T)char_count_cursor,
3133 (varnumber_T)char_count,
3134 (varnumber_T)byte_count_cursor,
3135 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 }
3137 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003138 {
3139 p = ml_get_curline();
3140 validate_virtcol();
3141 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3142 (int)curwin->w_virtcol + 1);
3143 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3144 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaared767a22016-01-03 22:49:16 +01003146 if (char_count_cursor == byte_count_cursor
3147 && char_count == byte_count)
3148 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003149 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003150 (char *)buf1, (char *)buf2,
3151 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003153 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3154 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003155 else
3156 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003157 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003158 (char *)buf1, (char *)buf2,
3159 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003160 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003161 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3162 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3163 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003164 }
3165 }
3166
Bram Moolenaared767a22016-01-03 22:49:16 +01003167 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003168 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003169 {
3170 size_t len = STRLEN(IObuff);
3171
3172 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003173 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003174 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003175 if (dict == NULL)
3176 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003177 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003178 p = p_shm;
3179 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003180 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003181 p_shm = p;
3182 }
3183 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003184#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003185 if (dict != NULL)
3186 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003187 dict_add_number(dict, "words", word_count);
3188 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003189 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003190 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3191 byte_count_cursor);
3192 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3193 char_count_cursor);
3194 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3195 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003199
3200/*
3201 * Handle indent and format operators and visual mode ":".
3202 */
3203 static void
3204op_colon(oparg_T *oap)
3205{
3206 stuffcharReadbuff(':');
3207 if (oap->is_VIsual)
3208 stuffReadbuff((char_u *)"'<,'>");
3209 else
3210 {
3211 // Make the range look nice, so it can be repeated.
3212 if (oap->start.lnum == curwin->w_cursor.lnum)
3213 stuffcharReadbuff('.');
3214 else
3215 stuffnumReadbuff((long)oap->start.lnum);
3216 if (oap->end.lnum != oap->start.lnum)
3217 {
3218 stuffcharReadbuff(',');
3219 if (oap->end.lnum == curwin->w_cursor.lnum)
3220 stuffcharReadbuff('.');
3221 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3222 stuffcharReadbuff('$');
3223 else if (oap->start.lnum == curwin->w_cursor.lnum)
3224 {
3225 stuffReadbuff((char_u *)".+");
3226 stuffnumReadbuff((long)oap->line_count - 1);
3227 }
3228 else
3229 stuffnumReadbuff((long)oap->end.lnum);
3230 }
3231 }
3232 if (oap->op_type != OP_COLON)
3233 stuffReadbuff((char_u *)"!");
3234 if (oap->op_type == OP_INDENT)
3235 {
3236#ifndef FEAT_CINDENT
3237 if (*get_equalprg() == NUL)
3238 stuffReadbuff((char_u *)"indent");
3239 else
3240#endif
3241 stuffReadbuff(get_equalprg());
3242 stuffReadbuff((char_u *)"\n");
3243 }
3244 else if (oap->op_type == OP_FORMAT)
3245 {
3246 if (*curbuf->b_p_fp != NUL)
3247 stuffReadbuff(curbuf->b_p_fp);
3248 else if (*p_fp != NUL)
3249 stuffReadbuff(p_fp);
3250 else
3251 stuffReadbuff((char_u *)"fmt");
3252 stuffReadbuff((char_u *)"\n']");
3253 }
3254
3255 // do_cmdline() does the rest
3256}
3257
3258/*
3259 * Handle the "g@" operator: call 'operatorfunc'.
3260 */
3261 static void
3262op_function(oparg_T *oap UNUSED)
3263{
3264#ifdef FEAT_EVAL
3265 typval_T argv[2];
3266 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003267 pos_T orig_start = curbuf->b_op_start;
3268 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003269
3270 if (*p_opfunc == NUL)
3271 emsg(_("E774: 'operatorfunc' is empty"));
3272 else
3273 {
3274 // Set '[ and '] marks to text to be operated on.
3275 curbuf->b_op_start = oap->start;
3276 curbuf->b_op_end = oap->end;
3277 if (oap->motion_type != MLINE && !oap->inclusive)
3278 // Exclude the end position.
3279 decl(&curbuf->b_op_end);
3280
3281 argv[0].v_type = VAR_STRING;
3282 if (oap->block_mode)
3283 argv[0].vval.v_string = (char_u *)"block";
3284 else if (oap->motion_type == MLINE)
3285 argv[0].vval.v_string = (char_u *)"line";
3286 else
3287 argv[0].vval.v_string = (char_u *)"char";
3288 argv[1].v_type = VAR_UNKNOWN;
3289
3290 // Reset virtual_op so that 'virtualedit' can be changed in the
3291 // function.
3292 virtual_op = MAYBE;
3293
3294 (void)call_func_retnr(p_opfunc, 1, argv);
3295
3296 virtual_op = save_virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003297 if (cmdmod.lockmarks)
3298 {
3299 curbuf->b_op_start = orig_start;
3300 curbuf->b_op_end = orig_end;
3301 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003302 }
3303#else
3304 emsg(_("E775: Eval feature not available"));
3305#endif
3306}
3307
3308/*
3309 * Calculate start/end virtual columns for operating in block mode.
3310 */
3311 static void
3312get_op_vcol(
3313 oparg_T *oap,
3314 colnr_T redo_VIsual_vcol,
3315 int initial) // when TRUE adjust position for 'selectmode'
3316{
3317 colnr_T start, end;
3318
3319 if (VIsual_mode != Ctrl_V
3320 || (!initial && oap->end.col < curwin->w_width))
3321 return;
3322
3323 oap->block_mode = TRUE;
3324
3325 // prevent from moving onto a trail byte
3326 if (has_mbyte)
3327 mb_adjustpos(curwin->w_buffer, &oap->end);
3328
3329 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3330
3331 if (!redo_VIsual_busy)
3332 {
3333 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3334
3335 if (start < oap->start_vcol)
3336 oap->start_vcol = start;
3337 if (end > oap->end_vcol)
3338 {
3339 if (initial && *p_sel == 'e' && start >= 1
3340 && start - 1 >= oap->end_vcol)
3341 oap->end_vcol = start - 1;
3342 else
3343 oap->end_vcol = end;
3344 }
3345 }
3346
3347 // if '$' was used, get oap->end_vcol from longest line
3348 if (curwin->w_curswant == MAXCOL)
3349 {
3350 curwin->w_cursor.col = MAXCOL;
3351 oap->end_vcol = 0;
3352 for (curwin->w_cursor.lnum = oap->start.lnum;
3353 curwin->w_cursor.lnum <= oap->end.lnum;
3354 ++curwin->w_cursor.lnum)
3355 {
3356 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3357 if (end > oap->end_vcol)
3358 oap->end_vcol = end;
3359 }
3360 }
3361 else if (redo_VIsual_busy)
3362 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3363 // Correct oap->end.col and oap->start.col to be the
3364 // upper-left and lower-right corner of the block area.
3365 //
3366 // (Actually, this does convert column positions into character
3367 // positions)
3368 curwin->w_cursor.lnum = oap->end.lnum;
3369 coladvance(oap->end_vcol);
3370 oap->end = curwin->w_cursor;
3371
3372 curwin->w_cursor = oap->start;
3373 coladvance(oap->start_vcol);
3374 oap->start = curwin->w_cursor;
3375}
3376
3377/*
3378 * Handle an operator after Visual mode or when the movement is finished.
3379 * "gui_yank" is true when yanking text for the clipboard.
3380 */
3381 void
3382do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3383{
3384 oparg_T *oap = cap->oap;
3385 pos_T old_cursor;
3386 int empty_region_error;
3387 int restart_edit_save;
3388#ifdef FEAT_LINEBREAK
3389 int lbr_saved = curwin->w_p_lbr;
3390#endif
3391
3392 // The visual area is remembered for redo
3393 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3394 static linenr_T redo_VIsual_line_count; // number of lines
3395 static colnr_T redo_VIsual_vcol; // number of cols or end column
3396 static long redo_VIsual_count; // count for Visual operator
3397 static int redo_VIsual_arg; // extra argument
3398 int include_line_break = FALSE;
3399
3400#if defined(FEAT_CLIPBOARD)
3401 // Yank the visual area into the GUI selection register before we operate
3402 // on it and lose it forever.
3403 // Don't do it if a specific register was specified, so that ""x"*P works.
3404 // This could call do_pending_operator() recursively, but that's OK
3405 // because gui_yank will be TRUE for the nested call.
3406 if ((clip_star.available || clip_plus.available)
3407 && oap->op_type != OP_NOP
3408 && !gui_yank
3409 && VIsual_active
3410 && !redo_VIsual_busy
3411 && oap->regname == 0)
3412 clip_auto_select();
3413#endif
3414 old_cursor = curwin->w_cursor;
3415
3416 // If an operation is pending, handle it...
3417 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3418 {
3419 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3420 // for the clipboard.
3421 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3422
3423#ifdef FEAT_LINEBREAK
3424 // Avoid a problem with unwanted linebreaks in block mode.
3425 if (curwin->w_p_lbr)
3426 curwin->w_valid &= ~VALID_VIRTCOL;
3427 curwin->w_p_lbr = FALSE;
3428#endif
3429 oap->is_VIsual = VIsual_active;
3430 if (oap->motion_force == 'V')
3431 oap->motion_type = MLINE;
3432 else if (oap->motion_force == 'v')
3433 {
3434 // If the motion was linewise, "inclusive" will not have been set.
3435 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3436 if (oap->motion_type == MLINE)
3437 oap->inclusive = FALSE;
3438 // If the motion already was characterwise, toggle "inclusive"
3439 else if (oap->motion_type == MCHAR)
3440 oap->inclusive = !oap->inclusive;
3441 oap->motion_type = MCHAR;
3442 }
3443 else if (oap->motion_force == Ctrl_V)
3444 {
3445 // Change line- or characterwise motion into Visual block mode.
3446 if (!VIsual_active)
3447 {
3448 VIsual_active = TRUE;
3449 VIsual = oap->start;
3450 }
3451 VIsual_mode = Ctrl_V;
3452 VIsual_select = FALSE;
3453 VIsual_reselect = FALSE;
3454 }
3455
3456 // Only redo yank when 'y' flag is in 'cpoptions'.
3457 // Never redo "zf" (define fold).
3458 if ((redo_yank || oap->op_type != OP_YANK)
3459 && ((!VIsual_active || oap->motion_force)
3460 // Also redo Operator-pending Visual mode mappings
3461 || (VIsual_active && cap->cmdchar == ':'
3462 && oap->op_type != OP_COLON))
3463 && cap->cmdchar != 'D'
3464#ifdef FEAT_FOLDING
3465 && oap->op_type != OP_FOLD
3466 && oap->op_type != OP_FOLDOPEN
3467 && oap->op_type != OP_FOLDOPENREC
3468 && oap->op_type != OP_FOLDCLOSE
3469 && oap->op_type != OP_FOLDCLOSEREC
3470 && oap->op_type != OP_FOLDDEL
3471 && oap->op_type != OP_FOLDDELREC
3472#endif
3473 )
3474 {
3475 prep_redo(oap->regname, cap->count0,
3476 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3477 oap->motion_force, cap->cmdchar, cap->nchar);
3478 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3479 {
3480 // If 'cpoptions' does not contain 'r', insert the search
3481 // pattern to really repeat the same command.
3482 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3483 AppendToRedobuffLit(cap->searchbuf, -1);
3484 AppendToRedobuff(NL_STR);
3485 }
3486 else if (cap->cmdchar == ':')
3487 {
3488 // do_cmdline() has stored the first typed line in
3489 // "repeat_cmdline". When several lines are typed repeating
3490 // won't be possible.
3491 if (repeat_cmdline == NULL)
3492 ResetRedobuff();
3493 else
3494 {
3495 AppendToRedobuffLit(repeat_cmdline, -1);
3496 AppendToRedobuff(NL_STR);
3497 VIM_CLEAR(repeat_cmdline);
3498 }
3499 }
3500 }
3501
3502 if (redo_VIsual_busy)
3503 {
3504 // Redo of an operation on a Visual area. Use the same size from
3505 // redo_VIsual_line_count and redo_VIsual_vcol.
3506 oap->start = curwin->w_cursor;
3507 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3508 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3509 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3510 VIsual_mode = redo_VIsual_mode;
3511 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3512 {
3513 if (VIsual_mode == 'v')
3514 {
3515 if (redo_VIsual_line_count <= 1)
3516 {
3517 validate_virtcol();
3518 curwin->w_curswant =
3519 curwin->w_virtcol + redo_VIsual_vcol - 1;
3520 }
3521 else
3522 curwin->w_curswant = redo_VIsual_vcol;
3523 }
3524 else
3525 {
3526 curwin->w_curswant = MAXCOL;
3527 }
3528 coladvance(curwin->w_curswant);
3529 }
3530 cap->count0 = redo_VIsual_count;
3531 if (redo_VIsual_count != 0)
3532 cap->count1 = redo_VIsual_count;
3533 else
3534 cap->count1 = 1;
3535 }
3536 else if (VIsual_active)
3537 {
3538 if (!gui_yank)
3539 {
3540 // Save the current VIsual area for '< and '> marks, and "gv"
3541 curbuf->b_visual.vi_start = VIsual;
3542 curbuf->b_visual.vi_end = curwin->w_cursor;
3543 curbuf->b_visual.vi_mode = VIsual_mode;
3544 restore_visual_mode();
3545 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3546# ifdef FEAT_EVAL
3547 curbuf->b_visual_mode_eval = VIsual_mode;
3548# endif
3549 }
3550
3551 // In Select mode, a linewise selection is operated upon like a
3552 // characterwise selection.
3553 // Special case: gH<Del> deletes the last line.
3554 if (VIsual_select && VIsual_mode == 'V'
3555 && cap->oap->op_type != OP_DELETE)
3556 {
3557 if (LT_POS(VIsual, curwin->w_cursor))
3558 {
3559 VIsual.col = 0;
3560 curwin->w_cursor.col =
3561 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3562 }
3563 else
3564 {
3565 curwin->w_cursor.col = 0;
3566 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3567 }
3568 VIsual_mode = 'v';
3569 }
3570 // If 'selection' is "exclusive", backup one character for
3571 // charwise selections.
3572 else if (VIsual_mode == 'v')
3573 include_line_break = unadjust_for_sel();
3574
3575 oap->start = VIsual;
3576 if (VIsual_mode == 'V')
3577 {
3578 oap->start.col = 0;
3579 oap->start.coladd = 0;
3580 }
3581 }
3582
3583 // Set oap->start to the first position of the operated text, oap->end
3584 // to the end of the operated text. w_cursor is equal to oap->start.
3585 if (LT_POS(oap->start, curwin->w_cursor))
3586 {
3587#ifdef FEAT_FOLDING
3588 // Include folded lines completely.
3589 if (!VIsual_active)
3590 {
3591 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3592 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003593 if ((curwin->w_cursor.col > 0 || oap->inclusive
3594 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003595 && hasFolding(curwin->w_cursor.lnum, NULL,
3596 &curwin->w_cursor.lnum))
3597 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3598 }
3599#endif
3600 oap->end = curwin->w_cursor;
3601 curwin->w_cursor = oap->start;
3602
3603 // w_virtcol may have been updated; if the cursor goes back to its
3604 // previous position w_virtcol becomes invalid and isn't updated
3605 // automatically.
3606 curwin->w_valid &= ~VALID_VIRTCOL;
3607 }
3608 else
3609 {
3610#ifdef FEAT_FOLDING
3611 // Include folded lines completely.
3612 if (!VIsual_active && oap->motion_type == MLINE)
3613 {
3614 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3615 NULL))
3616 curwin->w_cursor.col = 0;
3617 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3618 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3619 }
3620#endif
3621 oap->end = oap->start;
3622 oap->start = curwin->w_cursor;
3623 }
3624
3625 // Just in case lines were deleted that make the position invalid.
3626 check_pos(curwin->w_buffer, &oap->end);
3627 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3628
3629 // Set "virtual_op" before resetting VIsual_active.
3630 virtual_op = virtual_active();
3631
3632 if (VIsual_active || redo_VIsual_busy)
3633 {
3634 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3635
3636 if (!redo_VIsual_busy && !gui_yank)
3637 {
3638 // Prepare to reselect and redo Visual: this is based on the
3639 // size of the Visual text
3640 resel_VIsual_mode = VIsual_mode;
3641 if (curwin->w_curswant == MAXCOL)
3642 resel_VIsual_vcol = MAXCOL;
3643 else
3644 {
3645 if (VIsual_mode != Ctrl_V)
3646 getvvcol(curwin, &(oap->end),
3647 NULL, NULL, &oap->end_vcol);
3648 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3649 {
3650 if (VIsual_mode != Ctrl_V)
3651 getvvcol(curwin, &(oap->start),
3652 &oap->start_vcol, NULL, NULL);
3653 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3654 }
3655 else
3656 resel_VIsual_vcol = oap->end_vcol;
3657 }
3658 resel_VIsual_line_count = oap->line_count;
3659 }
3660
3661 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3662 if ((redo_yank || oap->op_type != OP_YANK)
3663 && oap->op_type != OP_COLON
3664#ifdef FEAT_FOLDING
3665 && oap->op_type != OP_FOLD
3666 && oap->op_type != OP_FOLDOPEN
3667 && oap->op_type != OP_FOLDOPENREC
3668 && oap->op_type != OP_FOLDCLOSE
3669 && oap->op_type != OP_FOLDCLOSEREC
3670 && oap->op_type != OP_FOLDDEL
3671 && oap->op_type != OP_FOLDDELREC
3672#endif
3673 && oap->motion_force == NUL
3674 )
3675 {
3676 // Prepare for redoing. Only use the nchar field for "r",
3677 // otherwise it might be the second char of the operator.
3678 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3679 || cap->nchar == 'N'))
3680 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003681 get_op_char(oap->op_type),
3682 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003683 oap->motion_force, cap->cmdchar, cap->nchar);
3684 else if (cap->cmdchar != ':')
3685 {
3686 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3687
3688 // reverse what nv_replace() did
3689 if (nchar == REPLACE_CR_NCHAR)
3690 nchar = CAR;
3691 else if (nchar == REPLACE_NL_NCHAR)
3692 nchar = NL;
3693 prep_redo(oap->regname, 0L, NUL, 'v',
3694 get_op_char(oap->op_type),
3695 get_extra_op_char(oap->op_type),
3696 nchar);
3697 }
3698 if (!redo_VIsual_busy)
3699 {
3700 redo_VIsual_mode = resel_VIsual_mode;
3701 redo_VIsual_vcol = resel_VIsual_vcol;
3702 redo_VIsual_line_count = resel_VIsual_line_count;
3703 redo_VIsual_count = cap->count0;
3704 redo_VIsual_arg = cap->arg;
3705 }
3706 }
3707
3708 // oap->inclusive defaults to TRUE.
3709 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3710 // FALSE. This makes "d}P" and "v}dP" work the same.
3711 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3712 oap->inclusive = TRUE;
3713 if (VIsual_mode == 'V')
3714 oap->motion_type = MLINE;
3715 else
3716 {
3717 oap->motion_type = MCHAR;
3718 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3719 && (include_line_break || !virtual_op))
3720 {
3721 oap->inclusive = FALSE;
3722 // Try to include the newline, unless it's an operator
3723 // that works on lines only.
3724 if (*p_sel != 'o'
3725 && !op_on_lines(oap->op_type)
3726 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3727 {
3728 ++oap->end.lnum;
3729 oap->end.col = 0;
3730 oap->end.coladd = 0;
3731 ++oap->line_count;
3732 }
3733 }
3734 }
3735
3736 redo_VIsual_busy = FALSE;
3737
3738 // Switch Visual off now, so screen updating does
3739 // not show inverted text when the screen is redrawn.
3740 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3741 // no screen redraw, so it is done here to remove the inverted
3742 // part.
3743 if (!gui_yank)
3744 {
3745 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003746 setmouse();
3747 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003748 may_clear_cmdline();
3749 if ((oap->op_type == OP_YANK
3750 || oap->op_type == OP_COLON
3751 || oap->op_type == OP_FUNCTION
3752 || oap->op_type == OP_FILTER)
3753 && oap->motion_force == NUL)
3754 {
3755#ifdef FEAT_LINEBREAK
3756 // make sure redrawing is correct
3757 curwin->w_p_lbr = lbr_saved;
3758#endif
3759 redraw_curbuf_later(INVERTED);
3760 }
3761 }
3762 }
3763
3764 // Include the trailing byte of a multi-byte char.
3765 if (has_mbyte && oap->inclusive)
3766 {
3767 int l;
3768
3769 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3770 if (l > 1)
3771 oap->end.col += l - 1;
3772 }
3773 curwin->w_set_curswant = TRUE;
3774
3775 // oap->empty is set when start and end are the same. The inclusive
3776 // flag affects this too, unless yanking and the end is on a NUL.
3777 oap->empty = (oap->motion_type == MCHAR
3778 && (!oap->inclusive
3779 || (oap->op_type == OP_YANK
3780 && gchar_pos(&oap->end) == NUL))
3781 && EQUAL_POS(oap->start, oap->end)
3782 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3783 // For delete, change and yank, it's an error to operate on an
3784 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3785 empty_region_error = (oap->empty
3786 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3787
3788 // Force a redraw when operating on an empty Visual region, when
3789 // 'modifiable is off or creating a fold.
3790 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3791#ifdef FEAT_FOLDING
3792 || oap->op_type == OP_FOLD
3793#endif
3794 ))
3795 {
3796#ifdef FEAT_LINEBREAK
3797 curwin->w_p_lbr = lbr_saved;
3798#endif
3799 redraw_curbuf_later(INVERTED);
3800 }
3801
3802 // If the end of an operator is in column one while oap->motion_type
3803 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3804 // character in the previous line. If op_start is on or before the
3805 // first non-blank in the line, the operator becomes linewise
3806 // (strange, but that's the way vi does it).
3807 if ( oap->motion_type == MCHAR
3808 && oap->inclusive == FALSE
3809 && !(cap->retval & CA_NO_ADJ_OP_END)
3810 && oap->end.col == 0
3811 && (!oap->is_VIsual || *p_sel == 'o')
3812 && !oap->block_mode
3813 && oap->line_count > 1)
3814 {
3815 oap->end_adjusted = TRUE; // remember that we did this
3816 --oap->line_count;
3817 --oap->end.lnum;
3818 if (inindent(0))
3819 oap->motion_type = MLINE;
3820 else
3821 {
3822 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3823 if (oap->end.col)
3824 {
3825 --oap->end.col;
3826 oap->inclusive = TRUE;
3827 }
3828 }
3829 }
3830 else
3831 oap->end_adjusted = FALSE;
3832
3833 switch (oap->op_type)
3834 {
3835 case OP_LSHIFT:
3836 case OP_RSHIFT:
3837 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3838 auto_format(FALSE, TRUE);
3839 break;
3840
3841 case OP_JOIN_NS:
3842 case OP_JOIN:
3843 if (oap->line_count < 2)
3844 oap->line_count = 2;
3845 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3846 curbuf->b_ml.ml_line_count)
3847 beep_flush();
3848 else
3849 {
3850 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3851 TRUE, TRUE, TRUE);
3852 auto_format(FALSE, TRUE);
3853 }
3854 break;
3855
3856 case OP_DELETE:
3857 VIsual_reselect = FALSE; // don't reselect now
3858 if (empty_region_error)
3859 {
3860 vim_beep(BO_OPER);
3861 CancelRedo();
3862 }
3863 else
3864 {
3865 (void)op_delete(oap);
3866 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
3867 u_save_cursor(); // cursor line wasn't saved yet
3868 auto_format(FALSE, TRUE);
3869 }
3870 break;
3871
3872 case OP_YANK:
3873 if (empty_region_error)
3874 {
3875 if (!gui_yank)
3876 {
3877 vim_beep(BO_OPER);
3878 CancelRedo();
3879 }
3880 }
3881 else
3882 {
3883#ifdef FEAT_LINEBREAK
3884 curwin->w_p_lbr = lbr_saved;
3885#endif
3886 (void)op_yank(oap, FALSE, !gui_yank);
3887 }
3888 check_cursor_col();
3889 break;
3890
3891 case OP_CHANGE:
3892 VIsual_reselect = FALSE; // don't reselect now
3893 if (empty_region_error)
3894 {
3895 vim_beep(BO_OPER);
3896 CancelRedo();
3897 }
3898 else
3899 {
3900 // This is a new edit command, not a restart. Need to
3901 // remember it to make 'insertmode' work with mappings for
3902 // Visual mode. But do this only once and not when typed and
3903 // 'insertmode' isn't set.
3904 if (p_im || !KeyTyped)
3905 restart_edit_save = restart_edit;
3906 else
3907 restart_edit_save = 0;
3908 restart_edit = 0;
3909#ifdef FEAT_LINEBREAK
3910 // Restore linebreak, so that when the user edits it looks as
3911 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01003912 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003913#endif
3914 // Reset finish_op now, don't want it set inside edit().
3915 finish_op = FALSE;
3916 if (op_change(oap)) // will call edit()
3917 cap->retval |= CA_COMMAND_BUSY;
3918 if (restart_edit == 0)
3919 restart_edit = restart_edit_save;
3920 }
3921 break;
3922
3923 case OP_FILTER:
3924 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3925 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
3926 else
3927 bangredo = TRUE; // do_bang() will put cmd in redo buffer
3928 // FALLTHROUGH
3929
3930 case OP_INDENT:
3931 case OP_COLON:
3932
3933#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3934 // If 'equalprg' is empty, do the indenting internally.
3935 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3936 {
3937# ifdef FEAT_LISP
3938 if (curbuf->b_p_lisp)
3939 {
3940 op_reindent(oap, get_lisp_indent);
3941 break;
3942 }
3943# endif
3944# ifdef FEAT_CINDENT
3945 op_reindent(oap,
3946# ifdef FEAT_EVAL
3947 *curbuf->b_p_inde != NUL ? get_expr_indent :
3948# endif
3949 get_c_indent);
3950 break;
3951# endif
3952 }
3953#endif
3954
3955 op_colon(oap);
3956 break;
3957
3958 case OP_TILDE:
3959 case OP_UPPER:
3960 case OP_LOWER:
3961 case OP_ROT13:
3962 if (empty_region_error)
3963 {
3964 vim_beep(BO_OPER);
3965 CancelRedo();
3966 }
3967 else
3968 op_tilde(oap);
3969 check_cursor_col();
3970 break;
3971
3972 case OP_FORMAT:
3973#if defined(FEAT_EVAL)
3974 if (*curbuf->b_p_fex != NUL)
3975 op_formatexpr(oap); // use expression
3976 else
3977#endif
3978 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
3979 op_colon(oap); // use external command
3980 else
3981 op_format(oap, FALSE); // use internal function
3982 break;
3983
3984 case OP_FORMAT2:
3985 op_format(oap, TRUE); // use internal function
3986 break;
3987
3988 case OP_FUNCTION:
3989#ifdef FEAT_LINEBREAK
3990 // Restore linebreak, so that when the user edits it looks as
3991 // before.
3992 curwin->w_p_lbr = lbr_saved;
3993#endif
3994 op_function(oap); // call 'operatorfunc'
3995 break;
3996
3997 case OP_INSERT:
3998 case OP_APPEND:
3999 VIsual_reselect = FALSE; // don't reselect now
4000 if (empty_region_error)
4001 {
4002 vim_beep(BO_OPER);
4003 CancelRedo();
4004 }
4005 else
4006 {
4007 // This is a new edit command, not a restart. Need to
4008 // remember it to make 'insertmode' work with mappings for
4009 // Visual mode. But do this only once.
4010 restart_edit_save = restart_edit;
4011 restart_edit = 0;
4012#ifdef FEAT_LINEBREAK
4013 // Restore linebreak, so that when the user edits it looks as
4014 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004015 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004016#endif
4017 op_insert(oap, cap->count1);
4018#ifdef FEAT_LINEBREAK
4019 // Reset linebreak, so that formatting works correctly.
4020 curwin->w_p_lbr = FALSE;
4021#endif
4022
4023 // TODO: when inserting in several lines, should format all
4024 // the lines.
4025 auto_format(FALSE, TRUE);
4026
4027 if (restart_edit == 0)
4028 restart_edit = restart_edit_save;
4029 else
4030 cap->retval |= CA_COMMAND_BUSY;
4031 }
4032 break;
4033
4034 case OP_REPLACE:
4035 VIsual_reselect = FALSE; // don't reselect now
4036 if (empty_region_error)
4037 {
4038 vim_beep(BO_OPER);
4039 CancelRedo();
4040 }
4041 else
4042 {
4043#ifdef FEAT_LINEBREAK
4044 // Restore linebreak, so that when the user edits it looks as
4045 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004046 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004047#endif
4048 op_replace(oap, cap->nchar);
4049 }
4050 break;
4051
4052#ifdef FEAT_FOLDING
4053 case OP_FOLD:
4054 VIsual_reselect = FALSE; // don't reselect now
4055 foldCreate(oap->start.lnum, oap->end.lnum);
4056 break;
4057
4058 case OP_FOLDOPEN:
4059 case OP_FOLDOPENREC:
4060 case OP_FOLDCLOSE:
4061 case OP_FOLDCLOSEREC:
4062 VIsual_reselect = FALSE; // don't reselect now
4063 opFoldRange(oap->start.lnum, oap->end.lnum,
4064 oap->op_type == OP_FOLDOPEN
4065 || oap->op_type == OP_FOLDOPENREC,
4066 oap->op_type == OP_FOLDOPENREC
4067 || oap->op_type == OP_FOLDCLOSEREC,
4068 oap->is_VIsual);
4069 break;
4070
4071 case OP_FOLDDEL:
4072 case OP_FOLDDELREC:
4073 VIsual_reselect = FALSE; // don't reselect now
4074 deleteFold(oap->start.lnum, oap->end.lnum,
4075 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4076 break;
4077#endif
4078 case OP_NR_ADD:
4079 case OP_NR_SUB:
4080 if (empty_region_error)
4081 {
4082 vim_beep(BO_OPER);
4083 CancelRedo();
4084 }
4085 else
4086 {
4087 VIsual_active = TRUE;
4088#ifdef FEAT_LINEBREAK
4089 curwin->w_p_lbr = lbr_saved;
4090#endif
4091 op_addsub(oap, cap->count1, redo_VIsual_arg);
4092 VIsual_active = FALSE;
4093 }
4094 check_cursor_col();
4095 break;
4096 default:
4097 clearopbeep(oap);
4098 }
4099 virtual_op = MAYBE;
4100 if (!gui_yank)
4101 {
4102 // if 'sol' not set, go back to old column for some commands
4103 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4104 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4105 || oap->op_type == OP_DELETE))
4106 {
4107#ifdef FEAT_LINEBREAK
4108 curwin->w_p_lbr = FALSE;
4109#endif
4110 coladvance(curwin->w_curswant = old_col);
4111 }
4112 }
4113 else
4114 {
4115 curwin->w_cursor = old_cursor;
4116 }
4117 oap->block_mode = FALSE;
4118 clearop(oap);
4119 motion_force = NUL;
4120 }
4121#ifdef FEAT_LINEBREAK
4122 curwin->w_p_lbr = lbr_saved;
4123#endif
4124}