blob: d3e1e47f32218861096f2be34f83948e37018463 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +010020static int ends_in_white(linenr_T lnum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010021static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
Bram Moolenaarf2732452018-06-03 14:47:35 +020023// Flags for third item in "opchars".
24#define OPF_LINES 1 // operator always works on lines
25#define OPF_CHANGE 2 // operator changes text
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027/*
28 * The names of operators.
29 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020030 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 */
32static char opchars[][3] =
33{
Bram Moolenaarf2732452018-06-03 14:47:35 +020034 {NUL, NUL, 0}, // OP_NOP
35 {'d', NUL, OPF_CHANGE}, // OP_DELETE
36 {'y', NUL, 0}, // OP_YANK
37 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
38 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
39 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
40 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
41 {'g', '~', OPF_CHANGE}, // OP_TILDE
42 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
43 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
44 {':', NUL, OPF_LINES}, // OP_COLON
45 {'g', 'U', OPF_CHANGE}, // OP_UPPER
46 {'g', 'u', OPF_CHANGE}, // OP_LOWER
47 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
48 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
49 {'g', '?', OPF_CHANGE}, // OP_ROT13
50 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
51 {'I', NUL, OPF_CHANGE}, // OP_INSERT
52 {'A', NUL, OPF_CHANGE}, // OP_APPEND
53 {'z', 'f', OPF_LINES}, // OP_FOLD
54 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
55 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
56 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
57 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
58 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
59 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
60 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
61 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
62 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
63 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000064};
65
66/*
67 * Translate a command name into an operator type.
68 * Must only be called with a valid operator name!
69 */
70 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010071get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
73 int i;
74
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010075 if (char1 == 'r') // ignore second character
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 return OP_REPLACE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010077 if (char1 == '~') // when tilde is an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 return OP_TILDE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010079 if (char1 == 'g' && char2 == Ctrl_A) // add
Bram Moolenaard79e5502016-01-10 22:13:02 +010080 return OP_NR_ADD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010081 if (char1 == 'g' && char2 == Ctrl_X) // subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +010082 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 if (opchars[i][0] == char1 && opchars[i][1] == char2)
86 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +010087 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
88 {
89 internal_error("get_op_type()");
90 break;
91 }
92 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return i;
94}
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * Return TRUE if operator "op" always works on whole lines.
98 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020099 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100100op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200102 return opchars[op][2] & OPF_LINES;
103}
104
Bram Moolenaar113e1072019-01-20 15:30:40 +0100105#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200106/*
107 * Return TRUE if operator "op" changes text.
108 */
109 int
110op_is_change(int op)
111{
112 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
116/*
117 * Get first operator command character.
118 * Returns 'g' or 'z' if there is another command character.
119 */
120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100121get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return opchars[optype][0];
124}
125
126/*
127 * Get second operator command character.
128 */
129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100130get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 return opchars[optype][1];
133}
134
135/*
136 * op_shift - handle a shift operation
137 */
138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100139op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 long i;
142 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145 if (u_save((linenr_T)(oap->start.lnum - 1),
146 (linenr_T)(oap->end.lnum + 1)) == FAIL)
147 return;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 if (oap->block_mode)
150 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152 for (i = oap->line_count; --i >= 0; )
153 {
154 first_char = *ml_get_curline();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100155 if (first_char == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 else if (oap->block_mode)
158 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100160 // Move the line right if it doesn't start with '#', 'smartindent'
161 // isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
163 if (first_char != '#' || !preprocs_left())
164#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000165 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++curwin->w_cursor.lnum;
167 }
168
169 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (oap->block_mode)
171 {
172 curwin->w_cursor.lnum = oap->start.lnum;
173 curwin->w_cursor.col = block_col;
174 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100175 else if (curs_top) // put cursor on first line, for ">>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 {
177 curwin->w_cursor.lnum = oap->start.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100178 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
180 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100181 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100184 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100185 foldOpenCursor();
186#endif
187
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (oap->line_count > p_report)
190 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200191 char *op;
192 char *msg_line_single;
193 char *msg_line_plural;
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200196 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200198 op = "<";
199 msg_line_single = NGETTEXT("%ld line %sed %d time",
200 "%ld line %sed %d times", amount);
201 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
202 "%ld lines %sed %d times", amount);
203 vim_snprintf((char *)IObuff, IOSIZE,
204 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
205 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100206 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
208
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100209 if (!cmdmod.lockmarks)
210 {
211 // Set "'[" and "']" marks.
212 curbuf->b_op_start = oap->start;
213 curbuf->b_op_end.lnum = oap->end.lnum;
214 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
215 if (curbuf->b_op_end.col > 0)
216 --curbuf->b_op_end.col;
217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
220/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100221 * Shift the current line one shiftwidth left (if left != 0) or right
222 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 */
224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100225shift_line(
226 int left,
227 int round,
228 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200229 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 int count;
232 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200233 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200235 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200237 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200239 i = count / sw_val; // number of 'shiftwidth' rounded down
240 j = count % sw_val; // extra spaces
241 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 --amount;
243 if (left)
244 {
245 i -= amount;
246 if (i < 0)
247 i = 0;
248 }
249 else
250 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200251 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200253 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
255 if (left)
256 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200257 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 if (count < 0)
259 count = 0;
260 }
261 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200262 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200265 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000267 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000269 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270}
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272/*
273 * Shift one line of the current block one shiftwidth right or left.
274 * Leaves cursor on first character in block.
275 */
276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100277shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278{
279 int left = (oap->op_type == OP_LSHIFT);
280 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000281 int total;
282 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 int sw_val = (int)get_sw_value_indent(curbuf);
285 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000288 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int i = 0, j = 0;
290 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifdef FEAT_RIGHTLEFT
292 int old_p_ri = p_ri;
293
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100294 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100297 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
299 if (bd.is_short)
300 return;
301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100302 // total is number of screen columns to be inserted/removed
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 total = (int)((unsigned)amount * (unsigned)sw_val);
304 if ((total / sw_val) != amount)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100305 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 oldp = ml_get_curline();
308
309 if (!left)
310 {
311 /*
312 * 1. Get start vcol
313 * 2. Total ws vcols
314 * 3. Divvy into TABs & spp
315 * 4. Construct new string
316 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100317 total += bd.pre_whitesp; // all virtual WS upto & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 ws_vcol = bd.start_vcol - bd.pre_whitesp;
319 if (bd.startspaces)
320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100322 {
323 if ((*mb_ptr2len)(bd.textstart) == 1)
324 ++bd.textstart;
325 else
326 {
327 ws_vcol = 0;
328 bd.startspaces = 0;
329 }
330 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000331 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000332 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100334 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100336 // TODO: is passing bd.textstart for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200337 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
338 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 total += incr;
340 bd.start_vcol += incr;
341 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100342 // OK, now total=all the VWS reqd, and textstart points at the 1st
343 // non-ws char in the block.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200344#ifdef FEAT_VARTABS
345 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200346 tabstop_fromto(ws_vcol, ws_vcol + total,
347 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348 else
349 j = total;
350#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 if (!curbuf->b_p_et)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100352 i = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (i)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100354 j = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 else
356 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100358 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
360 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200361 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (newp == NULL)
363 return;
364 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
365 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200366 vim_memset(newp + bd.textcol, TAB, (size_t)i);
367 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100368 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
370 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100371 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100373 colnr_T destination_col; // column to which text in block will
374 // be shifted
375 char_u *verbatim_copy_end; // end of the part of the line which is
376 // copied verbatim
377 colnr_T verbatim_copy_width;// the (displayed) width of this part
378 // of line
379 unsigned fill; // nr of spaces that replace a TAB
380 unsigned new_line_len; // the length of the line after the
381 // block shift
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000382 size_t block_space_width;
383 size_t shift_amount;
384 char_u *non_white = bd.textstart;
385 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000387 /*
388 * Firstly, let's find the first non-whitespace character that is
389 * displayed after the block's start column and the character's column
390 * number. Also, let's calculate the width of all the whitespace
391 * characters that are displayed in the block and precede the searched
392 * non-whitespace character.
393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100395 // If "bd.startspaces" is set, "bd.textstart" points to the character,
396 // the part of which is displayed at the block's beginning. Let's start
397 // searching from the next character.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000398 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100399 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100401 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000402 non_white_col = bd.start_vcol;
403
Bram Moolenaar1c465442017-03-12 20:10:05 +0100404 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000405 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200406 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000407 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000410 block_space_width = non_white_col - oap->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100411 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000412 shift_amount = (block_space_width < (size_t)total
413 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100415 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000416 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Now let's find out how much of the beginning of the line we can
419 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000420 verbatim_copy_end = bd.textstart;
421 verbatim_copy_width = bd.start_vcol;
422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100423 // If "bd.startspaces" is set, "bd.textstart" points to the character
424 // preceding the block. We have to subtract its width to obtain its
425 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000426 if (bd.startspaces)
427 verbatim_copy_width -= bd.start_char_vcols;
428 while (verbatim_copy_width < destination_col)
429 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200430 char_u *line = verbatim_copy_end;
431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100432 // TODO: is passing verbatim_copy_end for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200433 incr = lbr_chartabsize(line, verbatim_copy_end,
434 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000435 if (verbatim_copy_width + incr > destination_col)
436 break;
437 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100438 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000439 }
440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100441 // If "destination_col" is different from the width of the initial
442 // part of the line that will be copied, it means we encountered a tab
443 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000444 fill = destination_col - verbatim_copy_width;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // The replacement line will consist of:
447 // - the beginning of the original line up to "verbatim_copy_end",
448 // - "fill" number of spaces,
449 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000450 new_line_len = (unsigned)(verbatim_copy_end - oldp)
451 + fill
452 + (unsigned)STRLEN(non_white) + 1;
453
Bram Moolenaar964b3742019-05-24 18:54:09 +0200454 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (newp == NULL)
456 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200458 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000459 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100461 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
463 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
464 State = oldstate;
465 curwin->w_cursor.col = oldcol;
466#ifdef FEAT_RIGHTLEFT
467 p_ri = old_p_ri;
468#endif
469}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * Insert string "s" (b_insert ? before : after) block :AKelly
473 * Caller must prepare for undo.
474 */
475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100476block_insert(
477 oparg_T *oap,
478 char_u *s,
479 int b_insert,
480 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaardac13472019-09-16 21:06:21 +0200482 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100483 int count = 0; // extra spaces to replace a cut TAB
484 int spaces = 0; // non-zero if cutting a TAB
485 colnr_T offset; // pointer along new line
486 unsigned s_len; // STRLEN(s)
487 char_u *newp, *oldp; // new, old lines
488 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int oldstate = State;
490
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 s_len = (unsigned)STRLEN(s);
493
494 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
495 {
496 block_prep(oap, bdp, lnum, TRUE);
497 if (bdp->is_short && b_insert)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100498 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
500 oldp = ml_get(lnum);
501
502 if (b_insert)
503 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200504 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 spaces = bdp->startspaces;
506 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100507 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 offset = bdp->textcol;
509 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100510 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200512 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100513 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200515 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (spaces != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100517 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 offset = bdp->textcol + bdp->textlen - (spaces != 0);
519 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100520 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100522 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 if (!bdp->is_MAX)
524 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
525 count = spaces;
526 offset = bdp->textcol + bdp->textlen;
527 }
528 }
529
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200530 if (has_mbyte && spaces > 0)
531 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100532 int off;
533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100534 // Avoid starting halfway a multi-byte character.
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200535 if (b_insert)
536 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100537 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200538 }
539 else
540 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100541 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200542 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200543 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100544 spaces -= off;
545 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200546 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200547
Bram Moolenaar964b3742019-05-24 18:54:09 +0200548 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (newp == NULL)
550 continue;
551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100552 // copy up to shifted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mch_memmove(newp, oldp, (size_t)(offset));
554 oldp += offset;
555
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200557 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 // copy the new text
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
561 offset += s_len;
562
563 if (spaces && !bdp->is_short)
564 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100565 // insert post-padding
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100567 // We're splitting a TAB, don't copy it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 oldp++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 // We allowed for that TAB, remember this now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 count++;
571 }
572
573 if (spaces > 0)
574 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000575 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577 ml_replace(lnum, newp, FALSE);
578
579 if (lnum == oap->end.lnum)
580 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100581 // Set "']" mark to the end of the block instead of the end of
582 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 curbuf->b_op_end.lnum = oap->end.lnum;
584 curbuf->b_op_end.col = offset;
585 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100586 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
589
590 State = oldstate;
591}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593/*
594 * Stuff a string into the typeahead buffer, such that edit() will insert it
595 * literally ("literally" TRUE) or interpret is as typed characters.
596 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 int c;
601 char_u *start;
602
603 while (*arg != NUL)
604 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 // Stuff a sequence of normal ASCII characters, that's fast. Also
606 // stuff K_SPECIAL to get the effect of a special key when "literally"
607 // is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 start = arg;
609 while ((*arg >= ' '
610#ifndef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 && *arg < DEL // EBCDIC: chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#endif
613 )
614 || (*arg == K_SPECIAL && !literally))
615 ++arg;
616 if (arg > start)
617 stuffReadbuffLen(start, (long)(arg - start));
618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100619 // stuff a single special character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (*arg != NUL)
621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +0200623 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 c = *arg++;
626 if (literally && ((c < ' ' && c != TAB) || c == DEL))
627 stuffcharReadbuff(Ctrl_V);
628 stuffcharReadbuff(c);
629 }
630 }
631}
632
633/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200634 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200636 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 */
638 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100639op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 int n;
642 linenr_T lnum;
643 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 char_u *newp, *oldp;
645 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
647 int did_yank = FALSE;
648
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100649 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 return OK;
651
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100652 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (oap->empty)
654 return u_save_cursor();
655
656 if (!curbuf->b_p_ma)
657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100658 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return FAIL;
660 }
661
662#ifdef FEAT_CLIPBOARD
663 adjust_clip_reg(&oap->regname);
664#endif
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (has_mbyte)
667 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaard04b7502010-07-08 22:27:55 +0200669 /*
670 * Imitate the strange Vi behaviour: If the delete spans more than one
671 * line and motion_type == MCHAR and the result is a blank line, make the
672 * delete linewise. Don't do this for the change command or Visual mode.
673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000676 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100678 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 && oap->op_type == OP_DELETE)
680 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200681 ptr = ml_get(oap->end.lnum) + oap->end.col;
682 if (*ptr != NUL)
683 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ptr = skipwhite(ptr);
685 if (*ptr == NUL && inindent(0))
686 oap->motion_type = MLINE;
687 }
688
Bram Moolenaard04b7502010-07-08 22:27:55 +0200689 /*
690 * Check for trying to delete (e.g. "D") in an empty line.
691 * Note: For the change operator it is ok.
692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if ( oap->motion_type == MCHAR
694 && oap->line_count == 1
695 && oap->op_type == OP_DELETE
696 && *ml_get(oap->start.lnum) == NUL)
697 {
698 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000699 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 * 'cpoptions' (Vi compatible).
701 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000702 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100703 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
704 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000705 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
707 beep_flush();
708 return OK;
709 }
710
Bram Moolenaard04b7502010-07-08 22:27:55 +0200711 /*
712 * Do a yank of whatever we're about to delete.
713 * If a yank register was specified, put the deleted text into that
714 * register. For the black hole register '_' don't yank anything.
715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (oap->regname != '_')
717 {
718 if (oap->regname != 0)
719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (!valid_yank_reg(oap->regname, TRUE))
722 {
723 beep_flush();
724 return OK;
725 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100726 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
727 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 did_yank = TRUE;
729 }
730
731 /*
732 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100733 * delete contains a line break, or when using a specific operator (Vi
734 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200735 * Use the register name from before adjust_clip_reg() may have
736 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100738 if (oap->motion_type == MLINE || oap->line_count > 1
739 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100741 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 if (op_yank(oap, TRUE, FALSE) == OK)
743 did_yank = TRUE;
744 }
745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100746 // Yank into small delete register when no named register specified
747 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200748 if ((
749#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200750 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
751 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200752#endif
753 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 && oap->line_count == 1)
755 {
756 oap->regname = '-';
757 get_yank_register(oap->regname, TRUE);
758 if (op_yank(oap, TRUE, FALSE) == OK)
759 did_yank = TRUE;
760 oap->regname = 0;
761 }
762
763 /*
764 * If there's too much stuff to fit in the yank register, then get a
765 * confirmation before doing the delete. This is crude, but simple.
766 * And it avoids doing a delete of something we can't put back if we
767 * want.
768 */
769 if (!did_yank)
770 {
771 int msg_silent_save = msg_silent;
772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100773 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
775 msg_silent = msg_silent_save;
776 if (n != 'y')
777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 return FAIL;
780 }
781 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100782
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100783#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100784 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200785 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788
Bram Moolenaard04b7502010-07-08 22:27:55 +0200789 /*
790 * block mode delete
791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (oap->block_mode)
793 {
794 if (u_save((linenr_T)(oap->start.lnum - 1),
795 (linenr_T)(oap->end.lnum + 1)) == FAIL)
796 return FAIL;
797
798 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
799 {
800 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 continue;
803
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100804 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 if (lnum == curwin->w_cursor.lnum)
806 {
807 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810
Bram Moolenaar8055d172019-05-17 22:57:26 +0200811 // "n" == number of chars deleted
812 // If we delete a TAB, it may be replaced by several characters.
813 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 n = bd.textlen - bd.startspaces - bd.endspaces;
815 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200816 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (newp == NULL)
818 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100821 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200822 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100824 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000826 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200829
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100830#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200831 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200832 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835
836 check_cursor_col();
837 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
838 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100839 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100841 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 if (oap->op_type == OP_CHANGE)
844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 // Delete the lines except the first one. Temporarily move the
846 // cursor to the next line. Save the current line number, if the
847 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (oap->line_count > 1)
849 {
850 lnum = curwin->w_cursor.lnum;
851 ++curwin->w_cursor.lnum;
852 del_lines((long)(oap->line_count - 1), TRUE);
853 curwin->w_cursor.lnum = lnum;
854 }
855 if (u_save_cursor() == FAIL)
856 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 beginline(BL_WHITE); // cursor on first non-white
860 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 ai_col = curwin->w_cursor.col;
862 }
863 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 beginline(0); // cursor in column 0
865 truncate_line(FALSE); // delete the rest of the line
866 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100868 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 else
871 {
872 del_lines(oap->line_count, TRUE);
873 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100874 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876 }
877 else
878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (virtual_op)
880 {
881 int endcol = 0;
882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (gchar_pos(&oap->start) == '\t')
885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 return FAIL;
888 if (oap->line_count == 1)
889 endcol = getviscol2(oap->end.col, oap->end.coladd);
890 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
891 oap->start = curwin->w_cursor;
892 if (oap->line_count == 1)
893 {
894 coladvance(endcol);
895 oap->end.col = curwin->w_cursor.col;
896 oap->end.coladd = curwin->w_cursor.coladd;
897 curwin->w_cursor = oap->start;
898 }
899 }
900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (gchar_pos(&oap->end) == '\t'
903 && (int)oap->end.coladd < oap->inclusive)
904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100905 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (u_save((linenr_T)(oap->end.lnum - 1),
907 (linenr_T)(oap->end.lnum + 1)) == FAIL)
908 return FAIL;
909 curwin->w_cursor = oap->end;
910 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
911 oap->end = curwin->w_cursor;
912 curwin->w_cursor = oap->start;
913 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100914 if (has_mbyte)
915 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return FAIL;
922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100923 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100924 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 && oap->op_type == OP_CHANGE
926 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100927 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 display_dollar(oap->end.col - !oap->inclusive);
929
930 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (virtual_op)
933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100934 // fix up things for virtualedit-delete:
935 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 char_u *curline = ml_get_curline();
937 int len = (int)STRLEN(curline);
938
939 if (oap->end.coladd != 0
940 && (int)oap->end.col >= len - 1
941 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
942 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100943 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (n == 0 && oap->start.coladd != oap->end.coladd)
945 n = 1;
946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (gchar_cursor() != NUL)
949 curwin->w_cursor.coladd = 0;
950 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200951 (void)del_bytes((long)n, !virtual_op,
952 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100954 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956 pos_T curpos;
957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
960 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
961 return FAIL;
962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100963 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100965 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 ++curwin->w_cursor.lnum;
967 del_lines((long)(oap->line_count - 2), FALSE);
968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100969 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200970 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200971 curwin->w_cursor.col = 0;
972 (void)del_bytes((long)n, !virtual_op,
973 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100974 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200975 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 }
978
979 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
980
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000981setmarks:
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100982 if (!cmdmod.lockmarks)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100984 if (oap->block_mode)
985 {
986 curbuf->b_op_end.lnum = oap->end.lnum;
987 curbuf->b_op_end.col = oap->start.col;
988 }
989 else
990 curbuf->b_op_end = oap->start;
991 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 return OK;
995}
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997/*
998 * Adjust end of operating area for ending on a multi-byte character.
999 * Used for deletion.
1000 */
1001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001002mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004 char_u *p;
1005
1006 if (oap->inclusive)
1007 {
1008 p = ml_get(oap->end.lnum);
1009 oap->end.col += mb_tail_off(p, p + oap->end.col);
1010 }
1011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
Bram Moolenaar630afe82018-06-28 19:26:28 +02001013/*
1014 * Replace the character under the cursor with "c".
1015 * This takes care of multi-byte characters.
1016 */
1017 static void
1018replace_character(int c)
1019{
1020 int n = State;
1021
1022 State = REPLACE;
1023 ins_char(c);
1024 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001025 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +02001026 dec_cursor();
1027}
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029/*
1030 * Replace a whole area with one character.
1031 */
1032 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001033op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 char_u *newp, *oldp;
1038 size_t oldlen;
1039 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001040 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001041 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001046 if (c == REPLACE_CR_NCHAR)
1047 {
1048 had_ctrl_v_cr = TRUE;
1049 c = CAR;
1050 }
1051 else if (c == REPLACE_NL_NCHAR)
1052 {
1053 had_ctrl_v_cr = TRUE;
1054 c = NL;
1055 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (has_mbyte)
1058 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 if (u_save((linenr_T)(oap->start.lnum - 1),
1061 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1062 return FAIL;
1063
1064 /*
1065 * block mode replace
1066 */
1067 if (oap->block_mode)
1068 {
1069 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1070 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1071 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001072 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1074 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001075 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 // n == number of extra chars required
1078 // If we split a TAB, it may be replaced by several characters.
1079 // Thus the number of characters may increase!
1080 // If the range starts in virtual space, count the initial
1081 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1083 {
1084 pos_T vpos;
1085
Bram Moolenaara1381de2009-11-03 15:44:21 +00001086 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 getvpos(&vpos, oap->start_vcol);
1088 bd.startspaces += vpos.coladd;
1089 n = bd.startspaces;
1090 }
1091 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001092 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001095 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001099 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 numc = oap->end_vcol - oap->start_vcol + 1;
1101 if (bd.is_short && (!virtual_op || bd.is_MAX))
1102 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001104 // A double-wide character can be replaced only up to half the
1105 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if ((*mb_char2cells)(c) > 1)
1107 {
1108 if ((numc & 1) && !bd.is_short)
1109 {
1110 ++bd.endspaces;
1111 ++n;
1112 }
1113 numc = numc / 2;
1114 }
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 num_chars = numc;
1118 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001119 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 n += numc - bd.textlen;
1121
1122 oldp = ml_get_curline();
1123 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001124 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (newp == NULL)
1126 continue;
1127 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001128 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 mch_memmove(newp, oldp, (size_t)bd.textcol);
1130 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001131 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001132 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001133 // insert replacement chars CHECK FOR ALLOCATED SPACE
1134 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1135 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001136 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001138 if (has_mbyte)
1139 {
1140 n = (int)STRLEN(newp);
1141 while (--num_chars >= 0)
1142 n += (*mb_char2bytes)(c, newp + n);
1143 }
1144 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001145 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001146 if (!bd.is_short)
1147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001148 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001149 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001150 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001151 STRMOVE(newp + STRLEN(newp), oldp);
1152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001157 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001158 if (after_p != NULL)
1159 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001161 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001163 if (after_p != NULL)
1164 {
1165 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1166 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1167 oap->end.lnum++;
1168 vim_free(after_p);
1169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 }
1172 else
1173 {
1174 /*
1175 * MCHAR and MLINE motion replace.
1176 */
1177 if (oap->motion_type == MLINE)
1178 {
1179 oap->start.col = 0;
1180 curwin->w_cursor.col = 0;
1181 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1182 if (oap->end.col)
1183 --oap->end.col;
1184 }
1185 else if (!oap->inclusive)
1186 dec(&(oap->end));
1187
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001188 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
1190 n = gchar_cursor();
1191 if (n != NUL)
1192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1194 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001195 // This is slow, but it handles replacing a single-byte
1196 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001197 if (curwin->w_cursor.lnum == oap->end.lnum)
1198 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001199 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 }
1201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (n == TAB)
1204 {
1205 int end_vcol = 0;
1206
1207 if (curwin->w_cursor.lnum == oap->end.lnum)
1208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001209 // oap->end has to be recalculated when
1210 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 end_vcol = getviscol2(oap->end.col,
1212 oap->end.coladd);
1213 }
1214 coladvance_force(getviscol());
1215 if (curwin->w_cursor.lnum == oap->end.lnum)
1216 getvpos(&oap->end, end_vcol);
1217 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001218 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1222 {
1223 int virtcols = oap->end.coladd;
1224
1225 if (curwin->w_cursor.lnum == oap->start.lnum
1226 && oap->start.col == oap->end.col && oap->start.coladd)
1227 virtcols -= oap->start.coladd;
1228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // oap->end has been trimmed so it's effectively inclusive;
1230 // as a result an extra +1 must be counted so we don't
1231 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1233 curwin->w_cursor.col -= (virtcols + 1);
1234 for (; virtcols >= 0; virtcols--)
1235 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001236 if ((*mb_char2len)(c) > 1)
1237 replace_character(c);
1238 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001239 PBYTE(curwin->w_cursor, c);
1240 if (inc(&curwin->w_cursor) == -1)
1241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (inc_cursor() == -1)
1247 break;
1248 }
1249 }
1250
1251 curwin->w_cursor = oap->start;
1252 check_cursor();
1253 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1254
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001255 if (!cmdmod.lockmarks)
1256 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001258 curbuf->b_op_start = oap->start;
1259 curbuf->b_op_end = oap->end;
1260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
1262 return OK;
1263}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001265static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267/*
1268 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1269 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001271op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001275 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
1277 if (u_save((linenr_T)(oap->start.lnum - 1),
1278 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1279 return;
1280
1281 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001282 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
1284 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1285 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001286 int one_change;
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 block_prep(oap, &bd, pos.lnum, FALSE);
1289 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001290 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1291 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001292
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001293#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001294 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 {
1296 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1297
Bram Moolenaar009b2592004-10-24 19:18:58 +00001298 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1299 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001301 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305 if (did_change)
1306 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1307 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001308 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
1310 if (oap->motion_type == MLINE)
1311 {
1312 oap->start.col = 0;
1313 pos.col = 0;
1314 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1315 if (oap->end.col)
1316 --oap->end.col;
1317 }
1318 else if (!oap->inclusive)
1319 dec(&(oap->end));
1320
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001321 if (pos.lnum == oap->end.lnum)
1322 did_change = swapchars(oap->op_type, &pos,
1323 oap->end.col - pos.col + 1);
1324 else
1325 for (;;)
1326 {
1327 did_change |= swapchars(oap->op_type, &pos,
1328 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1329 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001330 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001331 break;
1332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (did_change)
1334 {
1335 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1336 0L);
1337#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001338 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340 char_u *ptr;
1341 int count;
1342
1343 pos = oap->start;
1344 while (pos.lnum < oap->end.lnum)
1345 {
1346 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001347 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001348 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001350 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 pos.col = 0;
1352 pos.lnum++;
1353 }
1354 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1355 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001356 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001358 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360#endif
1361 }
1362 }
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001365 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001368 if (!cmdmod.lockmarks)
1369 {
1370 // Set '[ and '] marks.
1371 curbuf->b_op_start = oap->start;
1372 curbuf->b_op_end = oap->end;
1373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001376 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001377 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378}
1379
1380/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001381 * Invoke swapchar() on "length" bytes at position "pos".
1382 * "pos" is advanced to just after the changed characters.
1383 * "length" is rounded up to include the whole last multi-byte character.
1384 * Also works correctly when the number of bytes changes.
1385 * Returns TRUE if some character was changed.
1386 */
1387 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001388swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001389{
1390 int todo;
1391 int did_change = 0;
1392
1393 for (todo = length; todo > 0; --todo)
1394 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001395 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001396 {
1397 int len = (*mb_ptr2len)(ml_get_pos(pos));
1398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001399 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001400 if (len > 0)
1401 todo -= len - 1;
1402 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001403 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001404 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001405 break;
1406 }
1407 return did_change;
1408}
1409
1410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 * If op_type == OP_UPPER: make uppercase,
1412 * if op_type == OP_LOWER: make lowercase,
1413 * if op_type == OP_ROT13: do rot13 encoding,
1414 * else swap case of character at 'pos'
1415 * returns TRUE when something actually changed.
1416 */
1417 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001418swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 int c;
1421 int nc;
1422
1423 c = gchar_pos(pos);
1424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (c >= 0x80 && op_type == OP_ROT13)
1427 return FALSE;
1428
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001429 if (op_type == OP_UPPER && c == 0xdf
1430 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001431 {
1432 pos_T sp = curwin->w_cursor;
1433
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001434 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001435 curwin->w_cursor = *pos;
1436 del_char(FALSE);
1437 ins_char('S');
1438 ins_char('S');
1439 curwin->w_cursor = sp;
1440 inc(pos);
1441 }
1442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001443 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 nc = c;
1446 if (MB_ISLOWER(c))
1447 {
1448 if (op_type == OP_ROT13)
1449 nc = ROT13(c, 'a');
1450 else if (op_type != OP_LOWER)
1451 nc = MB_TOUPPER(c);
1452 }
1453 else if (MB_ISUPPER(c))
1454 {
1455 if (op_type == OP_ROT13)
1456 nc = ROT13(c, 'A');
1457 else if (op_type != OP_UPPER)
1458 nc = MB_TOLOWER(c);
1459 }
1460 if (nc != c)
1461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1463 {
1464 pos_T sp = curwin->w_cursor;
1465
1466 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001468 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ins_char(nc);
1470 curwin->w_cursor = sp;
1471 }
1472 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001473 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 return TRUE;
1475 }
1476 return FALSE;
1477}
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479/*
1480 * op_insert - Insert and append operators for Visual mode.
1481 */
1482 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001483op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 long ins_len, pre_textlen = 0;
1486 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001487 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 struct block_def bd;
1489 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001490 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001492 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1494
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001495 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 curwin->w_cursor.lnum = oap->start.lnum;
1497 update_screen(INVERTED);
1498
1499 if (oap->block_mode)
1500 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001501 // When 'virtualedit' is used, need to insert the extra spaces before
1502 // doing block_prep(). When only "block" is used, virtual edit is
1503 // already disabled, but still need it when calling
1504 // coladvance_force().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (curwin->w_cursor.coladd > 0)
1506 {
1507 int old_ve_flags = ve_flags;
1508
1509 ve_flags = VE_ALL;
1510 if (u_save_cursor() == FAIL)
1511 return;
1512 coladvance_force(oap->op_type == OP_APPEND
1513 ? oap->end_vcol + 1 : getviscol());
1514 if (oap->op_type == OP_APPEND)
1515 --curwin->w_cursor.col;
1516 ve_flags = old_ve_flags;
1517 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001518 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001520 // Get indent information
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001521 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (oap->op_type == OP_APPEND)
1525 firstline += bd.textlen;
1526 pre_textlen = (long)STRLEN(firstline);
1527 }
1528
1529 if (oap->op_type == OP_APPEND)
1530 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001531 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001533 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 curwin->w_set_curswant = TRUE;
1535 while (*ml_get_cursor() != NUL
1536 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1537 ++curwin->w_cursor.col;
1538 if (bd.is_short && !bd.is_MAX)
1539 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001540 // First line was too short, make it longer and adjust the
1541 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (u_save_cursor() == FAIL)
1543 return;
1544 for (i = 0; i < bd.endspaces; ++i)
1545 ins_char(' ');
1546 bd.textlen += bd.endspaces;
1547 }
1548 }
1549 else
1550 {
1551 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001552 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001554 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001555 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 && oap->start_vcol != oap->end_vcol)
1557 inc_cursor();
1558 }
1559 }
1560
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001561 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001562 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001564 // When a tab was inserted, and the characters in front of the tab
1565 // have been converted to a tab as well, the column of the cursor
1566 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001567 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001568 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001569 oap->start = curbuf->b_op_start_orig;
1570
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001571 // If user has moved off this line, we don't know what to do, so do
1572 // nothing.
1573 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001574 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 return;
1576
1577 if (oap->block_mode)
1578 {
1579 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001580 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001581 size_t len;
1582 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001584 // If indent kicked in, the firstline might have changed
1585 // but only do that, if the indent actually increased.
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001586 ind_post = (colnr_T)getwhitecols_curline();
1587 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1588 {
1589 bd.textcol += ind_post - ind_pre;
1590 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001591 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001592 }
1593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001594 // The user may have moved the cursor before inserting something, try
1595 // to adjust the block for that. But only do it, if the difference
1596 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001597 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1598 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001599 {
1600 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001601 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001602 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001603 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001604 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001605 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001606 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001607 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001608 pre_textlen -= t - oap->start_vcol;
1609 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001610 }
1611 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001612 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001613 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001614 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001615 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001616 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001617 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001618 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001619 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001620 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001621 pre_textlen -= t - oap->start_vcol;
1622 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001623 oap->op_type = OP_INSERT;
1624 }
1625 }
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 /*
1628 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001629 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 * Don't do this when "$" used, end-of-line will have changed.
1631 */
1632 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1633 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1634 {
1635 if (oap->op_type == OP_APPEND)
1636 {
1637 pre_textlen += bd2.textlen - bd.textlen;
1638 if (bd2.endspaces)
1639 --bd2.textlen;
1640 }
1641 bd.textcol = bd2.textcol;
1642 bd.textlen = bd2.textlen;
1643 }
1644
1645 /*
1646 * Subsequent calls to ml_get() flush the firstline data - take a
1647 * copy of the required string.
1648 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001649 firstline = ml_get(oap->start.lnum);
1650 len = STRLEN(firstline);
1651 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001653 add += bd.textlen;
1654 if ((size_t)add > len)
1655 firstline += len; // short line, point to the NUL
1656 else
1657 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001658 if (pre_textlen >= 0
1659 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
1661 ins_text = vim_strnsave(firstline, (int)ins_len);
1662 if (ins_text != NULL)
1663 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001664 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (u_save(oap->start.lnum,
1666 (linenr_T)(oap->end.lnum + 1)) == OK)
1667 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1668 &bd);
1669
1670 curwin->w_cursor.col = oap->start.col;
1671 check_cursor();
1672 vim_free(ins_text);
1673 }
1674 }
1675 }
1676}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678/*
1679 * op_change - handle a change operation
1680 *
1681 * return TRUE if edit() returns because of a CTRL-O command
1682 */
1683 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001684op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 colnr_T l;
1687 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 long offset;
1689 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001690 long ins_len;
1691 long pre_textlen = 0;
1692 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 char_u *firstline;
1694 char_u *ins_text, *newp, *oldp;
1695 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
1697 l = oap->start.col;
1698 if (oap->motion_type == MLINE)
1699 {
1700 l = 0;
1701#ifdef FEAT_SMARTINDENT
1702 if (!p_paste && curbuf->b_p_si
1703# ifdef FEAT_CINDENT
1704 && !curbuf->b_p_cin
1705# endif
1706 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001707 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709 }
1710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001711 // First delete the text in the region. In an empty buffer only need to
1712 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1714 {
1715 if (u_save_cursor() == FAIL)
1716 return FALSE;
1717 }
1718 else if (op_delete(oap) == FAIL)
1719 return FALSE;
1720
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001721 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 && !virtual_op)
1723 inc_cursor();
1724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001725 // check for still on same line (<CR> in inserted text meaningless)
1726 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (oap->block_mode)
1728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (virtual_op && (curwin->w_cursor.coladd > 0
1731 || gchar_cursor() == NUL))
1732 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001733 firstline = ml_get(oap->start.lnum);
1734 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001735 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 bd.textcol = curwin->w_cursor.col;
1737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
1739#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1740 if (oap->motion_type == MLINE)
1741 fix_indent();
1742#endif
1743
1744 retval = edit(NUL, FALSE, (linenr_T)1);
1745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001747 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001749 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001751 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001753 // Auto-indenting may have changed the indent. If the cursor was past
1754 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001756 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001758 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001759
1760 pre_textlen += new_indent - pre_indent;
1761 bd.textcol += new_indent - pre_indent;
1762 }
1763
1764 ins_len = (long)STRLEN(firstline) - pre_textlen;
1765 if (ins_len > 0)
1766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001767 // Subsequent calls to ml_get() flush the firstline data - take a
1768 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001769 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001771 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1773 linenr++)
1774 {
1775 block_prep(oap, &bd, linenr, TRUE);
1776 if (!bd.is_short || virtual_op)
1777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 pos_T vpos;
1779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001780 // If the block starts in virtual space, count the
1781 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (bd.is_short)
1783 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001784 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
1787 else
1788 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001790 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (newp == NULL)
1792 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001793 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 mch_memmove(newp, oldp, (size_t)bd.textcol);
1795 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001796 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1799 offset += ins_len;
1800 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001801 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 ml_replace(linenr, newp, FALSE);
1803 }
1804 }
1805 check_cursor();
1806
1807 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1808 }
1809 vim_free(ins_text);
1810 }
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 return retval;
1814}
1815
1816/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001817 * When the cursor is on the NUL past the end of the line and it should not be
1818 * there move it left.
1819 */
1820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001821adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001822{
1823 if (curwin->w_cursor.col > 0
1824 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001825 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 && !(restart_edit || (State & INSERT)))
1827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001828 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001829 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001832 {
1833 colnr_T scol, ecol;
1834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001835 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001836 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1837 curwin->w_cursor.coladd = ecol - scol + 1;
1838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840}
1841
Bram Moolenaar81340392012-06-06 16:12:59 +02001842/*
1843 * If "process" is TRUE and the line begins with a comment leader (possibly
1844 * after some white space), return a pointer to the text after it. Put a boolean
1845 * value indicating whether the line ends with an unclosed comment in
1846 * "is_comment".
1847 * line - line to be processed,
1848 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001849 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001850 * include_space - whether to also skip space following the comment leader,
1851 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001852 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001853 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001854 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001855skip_comment(
1856 char_u *line,
1857 int process,
1858 int include_space,
1859 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001860{
1861 char_u *comment_flags = NULL;
1862 int lead_len;
1863 int leader_offset = get_last_leader_offset(line, &comment_flags);
1864
1865 *is_comment = FALSE;
1866 if (leader_offset != -1)
1867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001868 // Let's check whether the line ends with an unclosed comment.
1869 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001870 while (*comment_flags)
1871 {
1872 if (*comment_flags == COM_END
1873 || *comment_flags == ':')
1874 break;
1875 ++comment_flags;
1876 }
1877 if (*comment_flags != COM_END)
1878 *is_comment = TRUE;
1879 }
1880
1881 if (process == FALSE)
1882 return line;
1883
1884 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1885
1886 if (lead_len == 0)
1887 return line;
1888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001889 // Find:
1890 // - COM_END,
1891 // - colon,
1892 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001893 while (*comment_flags)
1894 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001895 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001896 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001897 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001898 ++comment_flags;
1899 }
1900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001901 // If we found a colon, it means that we are not processing a line
1902 // starting with a closing part of a three-part comment. That's good,
1903 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001904 if (*comment_flags == ':' || *comment_flags == NUL)
1905 line += lead_len;
1906
1907 return line;
1908}
Bram Moolenaar81340392012-06-06 16:12:59 +02001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001911 * Join 'count' lines (minimal 2) at cursor position.
1912 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001913 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1914 * leaders should not be removed.
1915 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1916 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001918 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 */
1920 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001921do_join(
1922 long count,
1923 int insert_space,
1924 int save_undo,
1925 int use_formatoptions UNUSED,
1926 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001928 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001929 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001930 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 char_u *newp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001932 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001933 int endcurr1 = NUL;
1934 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001935 int currsize = 0; // size of the current line
1936 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001938 colnr_T col = 0;
1939 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001940 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001941 int remove_comments = (use_formatoptions == TRUE)
1942 && has_format_option(FO_REMOVE_COMS);
1943 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001944#ifdef FEAT_PROP_POPUP
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001945 textprop_T **prop_lines = NULL;
1946 int *prop_lengths = NULL;
1947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001949 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1950 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001953 // Allocate an array to store the number of spaces inserted before each
1954 // line. We will use it to pre-compute the length of the new line and the
1955 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001956 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001957 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001959 if (remove_comments)
1960 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001961 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001962 if (comments == NULL)
1963 {
1964 vim_free(spaces);
1965 return FAIL;
1966 }
1967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001970 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001971 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001972 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001974 for (t = 0; t < count; ++t)
1975 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001976 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001977 if (t == 0 && setmark && !cmdmod.lockmarks)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001978 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001979 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001980 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1981 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1982 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001983 if (remove_comments)
1984 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001985 // We don't want to remove the comment leader if the
1986 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001987 if (t > 0 && prev_was_comment)
1988 {
1989
1990 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1991 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001992 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001993 curr = new_curr;
1994 }
1995 else
1996 curr = skip_comment(curr, FALSE, insert_space,
1997 &prev_was_comment);
1998 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001999
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002000 if (insert_space && t > 0)
2001 {
2002 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002003 if (*curr != NUL && *curr != ')'
2004 && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002005 && (!has_format_option(FO_MBYTE_JOIN)
2006 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2007 && (!has_format_option(FO_MBYTE_JOIN2)
2008 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002009 )
2010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002012 if (endcurr1 == ' ')
2013 endcurr1 = endcurr2;
2014 else
2015 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002016 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002017 if ( p_js
2018 && (endcurr1 == '.'
2019 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2020 && (endcurr1 == '?' || endcurr1 == '!'))))
2021 ++spaces[t];
2022 }
2023 }
2024 currsize = (int)STRLEN(curr);
2025 sumsize += currsize + spaces[t];
2026 endcurr1 = endcurr2 = NUL;
2027 if (insert_space && currsize > 0)
2028 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002029 if (has_mbyte)
2030 {
2031 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002032 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002033 endcurr1 = (*mb_ptr2char)(cend);
2034 if (cend > curr)
2035 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002036 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002037 endcurr2 = (*mb_ptr2char)(cend);
2038 }
2039 }
2040 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002041 {
2042 endcurr1 = *(curr + currsize - 1);
2043 if (currsize > 1)
2044 endcurr2 = *(curr + currsize - 2);
2045 }
2046 }
2047 line_breakcheck();
2048 if (got_int)
2049 {
2050 ret = FAIL;
2051 goto theend;
2052 }
2053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002055 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002056 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 // allocate the space for the new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02002059 newp = alloc(sumsize + 1);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002060 if (newp == NULL)
2061 {
2062 ret = FAIL;
2063 goto theend;
2064 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 cend = newp + sumsize;
2066 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002068#ifdef FEAT_PROP_POPUP
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002069 // We need to move properties of the lines that are going to be deleted to
2070 // the new long one.
2071 if (curbuf->b_has_textprop && !text_prop_frozen)
2072 {
2073 // Allocate an array to copy the text properties of joined lines into.
2074 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002075 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
2076 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002077 if (prop_lengths == NULL)
2078 VIM_CLEAR(prop_lines);
2079 }
2080#endif
2081
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002082 /*
2083 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002084 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002085 *
2086 * Move marks from each deleted line to the joined line, adjusting the
2087 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2088 * should not really be a problem.
2089 */
2090 for (t = count - 1; ; --t)
2091 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002092 int spaces_removed;
2093
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002094 cend -= currsize;
2095 mch_memmove(cend, curr, (size_t)currsize);
2096 if (spaces[t] > 0)
2097 {
2098 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002099 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002100 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002101
2102 // If deleting more spaces than adding, the cursor moves no more than
2103 // what is added if it is inside these spaces.
2104 spaces_removed = (curr - curr_start) - spaces[t];
2105
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002106 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002107 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002108 if (t == 0)
2109 break;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002110#ifdef FEAT_PROP_POPUP
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002111 if (prop_lines != NULL)
2112 adjust_props_for_join(curwin->w_cursor.lnum + t,
2113 prop_lines + t - 1, prop_lengths + t - 1,
2114 (long)(cend - newp - spaces_removed), spaces_removed);
2115#endif
2116
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002117 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002118 if (remove_comments)
2119 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002120 if (insert_space && t > 1)
2121 curr = skipwhite(curr);
2122 currsize = (int)STRLEN(curr);
2123 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002124
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002125#ifdef FEAT_PROP_POPUP
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002126 if (prop_lines != NULL)
2127 join_prop_lines(curwin->w_cursor.lnum, newp,
2128 prop_lines, prop_lengths, count);
2129 else
2130#endif
2131 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002133 if (setmark && !cmdmod.lockmarks)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002134 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002136 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002137 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002138 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002140 // Only report the change in the first line here, del_lines() will report
2141 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 changed_lines(curwin->w_cursor.lnum, currsize,
2143 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002145 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 * briefly, and then move it back. After del_lines() the cursor may
2147 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 */
2149 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002151 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 curwin->w_cursor.lnum = t;
2153
2154 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002155 * Set the cursor column:
2156 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002157 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002159 curwin->w_cursor.col =
2160 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 curwin->w_set_curswant = TRUE;
2165
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002166theend:
2167 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002168 if (remove_comments)
2169 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171}
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173/*
2174 * Return TRUE if the two comment leaders given are the same. "lnum" is
2175 * the first line. White-space is ignored. Note that the whole of
2176 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
2177 */
2178 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179same_leader(
2180 linenr_T lnum,
2181 int leader1_len,
2182 char_u *leader1_flags,
2183 int leader2_len,
2184 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 int idx1 = 0, idx2 = 0;
2187 char_u *p;
2188 char_u *line1;
2189 char_u *line2;
2190
2191 if (leader1_len == 0)
2192 return (leader2_len == 0);
2193
2194 /*
2195 * If first leader has 'f' flag, the lines can be joined only if the
2196 * second line does not have a leader.
2197 * If first leader has 'e' flag, the lines can never be joined.
2198 * If fist leader has 's' flag, the lines can only be joined if there is
2199 * some text after it and the second line has the 'm' flag.
2200 */
2201 if (leader1_flags != NULL)
2202 {
2203 for (p = leader1_flags; *p && *p != ':'; ++p)
2204 {
2205 if (*p == COM_FIRST)
2206 return (leader2_len == 0);
2207 if (*p == COM_END)
2208 return FALSE;
2209 if (*p == COM_START)
2210 {
2211 if (*(ml_get(lnum) + leader1_len) == NUL)
2212 return FALSE;
2213 if (leader2_flags == NULL || leader2_len == 0)
2214 return FALSE;
2215 for (p = leader2_flags; *p && *p != ':'; ++p)
2216 if (*p == COM_MIDDLE)
2217 return TRUE;
2218 return FALSE;
2219 }
2220 }
2221 }
2222
2223 /*
2224 * Get current line and next line, compare the leaders.
2225 * The first line has to be saved, only one line can be locked at a time.
2226 */
2227 line1 = vim_strsave(ml_get(lnum));
2228 if (line1 != NULL)
2229 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002230 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ;
2232 line2 = ml_get(lnum + 1);
2233 for (idx2 = 0; idx2 < leader2_len; ++idx2)
2234 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002235 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 if (line1[idx1++] != line2[idx2])
2238 break;
2239 }
2240 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01002241 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 ++idx1;
2243 }
2244 vim_free(line1);
2245 }
2246 return (idx2 == leader2_len && idx1 == leader1_len);
2247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01002250 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002253op_format(
2254 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 int keep_cursor) // keep cursor on same text char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 long old_line_count = curbuf->b_ml.ml_line_count;
2258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002259 // Place the cursor where the "gq" or "gw" command was given, so that "u"
2260 // can put it back there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 curwin->w_cursor = oap->cursor_start;
2262
2263 if (u_save((linenr_T)(oap->start.lnum - 1),
2264 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2265 return;
2266 curwin->w_cursor = oap->start;
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 // When there is no change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002272 if (!cmdmod.lockmarks)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 // Set '[ mark at the start of the formatted area
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002274 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002276 // For "gw" remember the cursor position and put it back below (adjusted
2277 // for joined and split lines).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (keep_cursor)
2279 saved_cursor = oap->cursor_start;
2280
Bram Moolenaar81a82092008-03-12 16:27:00 +00002281 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283 /*
2284 * Leave the cursor at the first non-blank of the last formatted line.
2285 * If the cursor was moved one line back (e.g. with "Q}") go to the next
2286 * line, so "." will do the next lines.
2287 */
2288 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2289 ++curwin->w_cursor.lnum;
2290 beginline(BL_WHITE | BL_FIX);
2291 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
2292 msgmore(old_line_count);
2293
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002294 if (!cmdmod.lockmarks)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002295 // put '] mark on the end of the formatted area
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002296 curbuf->b_op_end = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 if (keep_cursor)
2299 {
2300 curwin->w_cursor = saved_cursor;
2301 saved_cursor.lnum = 0;
2302 }
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (oap->is_VIsual)
2305 {
2306 win_T *wp;
2307
2308 FOR_ALL_WINDOWS(wp)
2309 {
2310 if (wp->w_old_cursor_lnum != 0)
2311 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002312 // When lines have been inserted or deleted, adjust the end of
2313 // the Visual area to be redrawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
2315 wp->w_old_cursor_lnum += old_line_count;
2316 else
2317 wp->w_old_visual_lnum += old_line_count;
2318 }
2319 }
2320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321}
2322
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002323#if defined(FEAT_EVAL) || defined(PROTO)
2324/*
2325 * Implementation of the format operator 'gq' for when using 'formatexpr'.
2326 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002327 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002328op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002329{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002330 if (oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002331 // When there is no change: need to remove the Visual selection
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002332 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002333
Bram Moolenaar700303e2010-07-11 17:35:50 +02002334 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002335 // As documented: when 'formatexpr' returns non-zero fall back to
2336 // internal formatting.
Bram Moolenaar700303e2010-07-11 17:35:50 +02002337 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002338}
2339
2340 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002341fex_format(
2342 linenr_T lnum,
2343 long count,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002344 int c) // character to be inserted
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002345{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002346 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
2347 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002348 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002349 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002350
2351 /*
2352 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002353 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002354 */
2355 set_vim_var_nr(VV_LNUM, lnum);
2356 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00002357 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002358
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002359 // Make a copy, the option could be changed while calling it.
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002360 fex = vim_strsave(curbuf->b_p_fex);
2361 if (fex == NULL)
2362 return 0;
2363
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002364 /*
2365 * Evaluate the function.
2366 */
2367 if (use_sandbox)
2368 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002369 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370 if (use_sandbox)
2371 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002372
2373 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002374 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002375
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002376 return r;
2377}
2378#endif
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380/*
2381 * Format "line_count" lines, starting at the cursor position.
2382 * When "line_count" is negative, format until the end of the paragraph.
2383 * Lines after the cursor line are saved for undo, caller must have saved the
2384 * first line.
2385 */
2386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002387format_lines(
2388 linenr_T line_count,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002389 int avoid_fex) // don't use 'formatexpr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 int max_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002392 int is_not_par; // current line not part of parag.
2393 int next_is_not_par; // next line not part of paragraph
2394 int is_end_par; // at end of paragraph
2395 int prev_is_end_par = FALSE;// prev. line not part of parag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 int next_is_start_par = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002397 int leader_len = 0; // leader len of current line
2398 int next_leader_len; // leader len of next line
2399 char_u *leader_flags = NULL; // flags for leader of current line
2400 char_u *next_leader_flags; // flags for leader of next line
2401 int do_comments; // format comments
2402 int do_comments_list = 0; // format comments with 'n' or '2'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 int advance = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002404 int second_indent = -1; // indent for second line (comment
2405 // aware)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 int do_second_indent;
2407 int do_number_indent;
2408 int do_trail_white;
2409 int first_par_line = TRUE;
2410 int smd_save;
2411 long count;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002412 int need_set_indent = TRUE; // set indent of next paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 int force_format = FALSE;
2414 int old_State = State;
2415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002416 // length of a line to force formatting: 3 * 'tw'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 max_len = comp_textwidth(TRUE) * 3;
2418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002419 // check for 'q', '2' and '1' in 'formatoptions'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 do_second_indent = has_format_option(FO_Q_SECOND);
2422 do_number_indent = has_format_option(FO_Q_NUMBER);
2423 do_trail_white = has_format_option(FO_WHITE_PAR);
2424
2425 /*
2426 * Get info about the previous and current line.
2427 */
2428 if (curwin->w_cursor.lnum > 1)
2429 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002430 , &leader_len, &leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 else
2432 is_not_par = TRUE;
2433 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002434 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 is_end_par = (is_not_par || next_is_not_par);
2436 if (!is_end_par && do_trail_white)
2437 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
2438
2439 curwin->w_cursor.lnum--;
2440 for (count = line_count; count != 0 && !got_int; --count)
2441 {
2442 /*
2443 * Advance to next paragraph.
2444 */
2445 if (advance)
2446 {
2447 curwin->w_cursor.lnum++;
2448 prev_is_end_par = is_end_par;
2449 is_not_par = next_is_not_par;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 leader_len = next_leader_len;
2451 leader_flags = next_leader_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453
2454 /*
2455 * The last line to be formatted.
2456 */
2457 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2458 {
2459 next_is_not_par = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 next_leader_len = 0;
2461 next_leader_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 else
2464 {
2465 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002466 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (do_number_indent)
2468 next_is_start_par =
2469 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
2470 }
2471 advance = TRUE;
2472 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
2473 if (!is_end_par && do_trail_white)
2474 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
2475
2476 /*
2477 * Skip lines that are not in a paragraph.
2478 */
2479 if (is_not_par)
2480 {
2481 if (line_count < 0)
2482 break;
2483 }
2484 else
2485 {
2486 /*
2487 * For the first line of a paragraph, check indent of second line.
2488 * Don't do this for comments and empty lines.
2489 */
2490 if (first_par_line
2491 && (do_second_indent || do_number_indent)
2492 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002493 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002495 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002496 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002497 if (leader_len == 0 && next_leader_len == 0)
2498 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002499 // no comment found
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002500 second_indent =
2501 get_indent_lnum(curwin->w_cursor.lnum + 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002502 }
2503 else
2504 {
2505 second_indent = next_leader_len;
2506 do_comments_list = 1;
2507 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002510 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002511 if (leader_len == 0 && next_leader_len == 0)
2512 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002513 // no comment found
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002514 second_indent =
2515 get_number_indent(curwin->w_cursor.lnum);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002516 }
2517 else
2518 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002519 // get_number_indent() is now "comment aware"...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002520 second_indent =
2521 get_number_indent(curwin->w_cursor.lnum);
2522 do_comments_list = 1;
2523 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526
2527 /*
2528 * When the comment leader changes, it's the end of the paragraph.
2529 */
2530 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 || !same_leader(curwin->w_cursor.lnum,
2532 leader_len, leader_flags,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002533 next_leader_len, next_leader_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 is_end_par = TRUE;
2535
2536 /*
2537 * If we have got to the end of a paragraph, or the line is
2538 * getting long, format it.
2539 */
2540 if (is_end_par || force_format)
2541 {
2542 if (need_set_indent)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002543 // replace indent in first line with minimal number of
2544 // tabs and spaces, according to current options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 (void)set_indent(get_indent(), SIN_CHANGED);
2546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002547 // put cursor on last non-space
2548 State = NORMAL; // don't go past end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 coladvance((colnr_T)MAXCOL);
2550 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
2551 dec_cursor();
2552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002553 // do the formatting, without 'showmode'
2554 State = INSERT; // for open_line()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 smd_save = p_smd;
2556 p_smd = FALSE;
2557 insertchar(NUL, INSCHAR_FORMAT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002559 + (do_comments && do_comments_list
2560 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar81a82092008-03-12 16:27:00 +00002561 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 State = old_State;
2563 p_smd = smd_save;
2564 second_indent = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002565 // at end of par.: need to set indent of next par.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 need_set_indent = is_end_par;
2567 if (is_end_par)
2568 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002569 // When called with a negative line count, break at the
2570 // end of the paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (line_count < 0)
2572 break;
2573 first_par_line = TRUE;
2574 }
2575 force_format = FALSE;
2576 }
2577
2578 /*
2579 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002580 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 */
2582 if (!is_end_par)
2583 {
2584 advance = FALSE;
2585 curwin->w_cursor.lnum++;
2586 curwin->w_cursor.col = 0;
2587 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002588 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002590 {
2591 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002593 (long)-next_leader_len, 0);
2594 }
2595 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002596 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002597 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002598
2599 if (indent > 0)
2600 {
2601 (void)del_bytes(indent, FALSE, FALSE);
2602 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002603 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01002604 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002607 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 beep_flush();
2610 break;
2611 }
2612 first_par_line = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002613 // If the line is getting long, format it next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (STRLEN(ml_get_curline()) > (size_t)max_len)
2615 force_format = TRUE;
2616 else
2617 force_format = FALSE;
2618 }
2619 }
2620 line_breakcheck();
2621 }
2622}
2623
2624/*
2625 * Return TRUE if line "lnum" ends in a white character.
2626 */
2627 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002628ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 char_u *s = ml_get(lnum);
2631 size_t l;
2632
2633 if (*s == NUL)
2634 return FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 // Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
2636 // invocation may call function multiple times".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002638 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639}
2640
2641/*
2642 * Blank lines, and lines containing only the comment leader, are left
2643 * untouched by the formatting. The function returns TRUE in this
2644 * case. It also returns TRUE when a line starts with the end of a comment
2645 * ('e' in comment flags), so that this line is skipped, and not joined to the
2646 * previous line. A new paragraph starts after a blank line, or when the
2647 * comment leader changes -- webb.
2648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002650fmt_check_par(
2651 linenr_T lnum,
2652 int *leader_len,
2653 char_u **leader_flags,
2654 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002656 char_u *flags = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 char_u *ptr;
2658
2659 ptr = ml_get(lnum);
2660 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002661 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 else
2663 *leader_len = 0;
2664
2665 if (*leader_len > 0)
2666 {
2667 /*
2668 * Search for 'e' flag in comment leader flags.
2669 */
2670 flags = *leader_flags;
2671 while (*flags && *flags != ':' && *flags != COM_END)
2672 ++flags;
2673 }
2674
2675 return (*skipwhite(ptr + *leader_len) == NUL
2676 || (*leader_len > 0 && *flags == COM_END)
2677 || startPS(lnum, NUL, FALSE));
2678}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680/*
2681 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
2682 * previous line is in the same paragraph. Used for auto-formatting.
2683 */
2684 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002685paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002688 int leader_len = 0; // leader len of current line
2689 char_u *leader_flags = NULL; // flags for leader of current line
2690 int next_leader_len; // leader len of next line
2691 char_u *next_leader_flags; // flags for leader of next line
2692 int do_comments; // format comments
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 if (lnum <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002695 return TRUE; // start of the file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 p = ml_get(lnum - 1);
2698 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 return TRUE; // after empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002702 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 return TRUE; // after non-paragraph line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002705 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 return TRUE; // "lnum" is not a paragraph line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 return TRUE; // missing trailing space in previous line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002712 return TRUE; // numbered item starts in "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if (!same_leader(lnum - 1, leader_len, leader_flags,
2715 next_leader_len, next_leader_flags))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 return TRUE; // change of comment leader.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 return FALSE;
2719}
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721/*
2722 * prepare a few things for block mode yank/delete/tilde
2723 *
2724 * for delete:
2725 * - textlen includes the first/last char to be (partly) deleted
2726 * - start/endspaces is the number of columns that are taken by the
2727 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002728 * deleted.
2729 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 * - textlen includes the first/last char to be wholly yanked
2731 * - start/endspaces is the number of columns of the first/last yanked char
2732 * that are to be yanked.
2733 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002734 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002735block_prep(
2736 oparg_T *oap,
2737 struct block_def *bdp,
2738 linenr_T lnum,
2739 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
2741 int incr = 0;
2742 char_u *pend;
2743 char_u *pstart;
2744 char_u *line;
2745 char_u *prev_pstart;
2746 char_u *prev_pend;
2747
2748 bdp->startspaces = 0;
2749 bdp->endspaces = 0;
2750 bdp->textlen = 0;
2751 bdp->start_vcol = 0;
2752 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 bdp->is_short = FALSE;
2754 bdp->is_oneChar = FALSE;
2755 bdp->pre_whitesp = 0;
2756 bdp->pre_whitesp_c = 0;
2757 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 bdp->start_char_vcols = 0;
2759
2760 line = ml_get(lnum);
2761 pstart = line;
2762 prev_pstart = line;
2763 while (bdp->start_vcol < oap->start_vcol && *pstart)
2764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002766 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002768 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
2770 bdp->pre_whitesp += incr;
2771 bdp->pre_whitesp_c++;
2772 }
2773 else
2774 {
2775 bdp->pre_whitesp = 0;
2776 bdp->pre_whitesp_c = 0;
2777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002779 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002782 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
2784 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (!is_del || oap->op_type == OP_APPEND)
2787 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2788 }
2789 else
2790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // notice: this converts partly selected Multibyte characters to
2792 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2794 if (is_del && bdp->startspaces)
2795 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2796 pend = pstart;
2797 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002798 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (oap->op_type == OP_INSERT)
2802 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2803 else if (oap->op_type == OP_APPEND)
2804 {
2805 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2806 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2807 }
2808 else
2809 {
2810 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2811 if (is_del && oap->op_type != OP_LSHIFT)
2812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002813 // just putting the sum of those two into
2814 // bdp->startspaces doesn't work for Visual replace,
2815 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 bdp->startspaces = bdp->start_char_vcols
2817 - (bdp->start_vcol - oap->start_vcol);
2818 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2819 }
2820 }
2821 }
2822 else
2823 {
2824 prev_pend = pend;
2825 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002829 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 bdp->end_vcol += incr;
2831 }
2832 if (bdp->end_vcol <= oap->end_vcol
2833 && (!is_del
2834 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // Alternative: include spaces to fill up the block.
2839 // Disadvantage: can lead to trailing spaces when the line is
2840 // short where the text is put
2841 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (oap->op_type == OP_APPEND || virtual_op)
2843 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002844 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 else if (bdp->end_vcol > oap->end_vcol)
2849 {
2850 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2851 if (!is_del && bdp->endspaces)
2852 {
2853 bdp->endspaces = incr - bdp->endspaces;
2854 if (pend != pstart)
2855 pend = prev_pend;
2856 }
2857 }
2858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (is_del && bdp->startspaces)
2861 pstart = prev_pstart;
2862 bdp->textlen = (int)(pend - pstart);
2863 }
2864 bdp->textcol = (colnr_T) (pstart - line);
2865 bdp->textstart = pstart;
2866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002869 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002872op_addsub(
2873 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 linenr_T Prenum1, // Amount of add/subtract
2875 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002877 pos_T pos;
2878 struct block_def bd;
2879 int change_cnt = 0;
2880 linenr_T amount = Prenum1;
2881
Bram Moolenaar6b731882018-11-16 20:54:47 +01002882 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002883 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002884 // the call to changed_lines().
2885#ifdef FEAT_FOLDING
2886 disable_fold_update++;
2887#endif
2888
Bram Moolenaard79e5502016-01-10 22:13:02 +01002889 if (!VIsual_active)
2890 {
2891 pos = curwin->w_cursor;
2892 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002893 {
2894#ifdef FEAT_FOLDING
2895 disable_fold_update--;
2896#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002897 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002898 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002899 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002900#ifdef FEAT_FOLDING
2901 disable_fold_update--;
2902#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002903 if (change_cnt)
2904 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2905 }
2906 else
2907 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002908 int one_change;
2909 int length;
2910 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002911
2912 if (u_save((linenr_T)(oap->start.lnum - 1),
2913 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002914 {
2915#ifdef FEAT_FOLDING
2916 disable_fold_update--;
2917#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002918 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002919 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002920
2921 pos = oap->start;
2922 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002925 {
2926 block_prep(oap, &bd, pos.lnum, FALSE);
2927 pos.col = bd.textcol;
2928 length = bd.textlen;
2929 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002930 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002931 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002932 curwin->w_cursor.col = 0;
2933 pos.col = 0;
2934 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2935 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002937 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002938 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002939 dec(&(oap->end));
2940 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2941 pos.col = 0;
2942 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002943 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002944 pos.col += oap->start.col;
2945 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002946 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002947 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002948 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002949 length = (int)STRLEN(ml_get(oap->end.lnum));
2950 if (oap->end.col >= length)
2951 oap->end.col = length - 1;
2952 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002953 }
2954 }
2955 one_change = do_addsub(oap->op_type, &pos, length, amount);
2956 if (one_change)
2957 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002959 if (change_cnt == 0)
2960 startpos = curbuf->b_op_start;
2961 ++change_cnt;
2962 }
2963
2964#ifdef FEAT_NETBEANS_INTG
2965 if (netbeans_active() && one_change)
2966 {
2967 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2968
2969 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2970 netbeans_inserted(curbuf, pos.lnum, pos.col,
2971 &ptr[pos.col], length);
2972 }
2973#endif
2974 if (g_cmd && one_change)
2975 amount += Prenum1;
2976 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002977
2978#ifdef FEAT_FOLDING
2979 disable_fold_update--;
2980#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002981 if (change_cnt)
2982 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2983
2984 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002986 redraw_curbuf_later(INVERTED);
2987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 // Set '[ mark if something changed. Keep the last end
2989 // position from do_addsub().
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002990 if (change_cnt > 0 && !cmdmod.lockmarks)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002991 curbuf->b_op_start = startpos;
2992
2993 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002995 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002996 }
2997}
2998
2999/*
3000 * Add or subtract 'Prenum1' from a number in a line
3001 * op_type is OP_NR_ADD or OP_NR_SUB
3002 *
3003 * Returns TRUE if some character was changed.
3004 */
3005 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003006do_addsub(
3007 int op_type,
3008 pos_T *pos,
3009 int length,
3010 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003011{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 int col;
3013 char_u *buf1;
3014 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
3016 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003017 uvarnumber_T n;
3018 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 char_u *ptr;
3020 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 int todel;
3022 int dohex;
3023 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003024 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 int doalp;
3026 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003028 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003029 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003030 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02003031 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003032 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003033 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003034 pos_T startpos;
3035 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Bram Moolenaardac13472019-09-16 21:06:21 +02003037 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
3038 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
3039 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
3040 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaard79e5502016-01-10 22:13:02 +01003042 curwin->w_cursor = *pos;
3043 ptr = ml_get(pos->lnum);
3044 col = pos->col;
3045
3046 if (*ptr == NUL)
3047 goto theend;
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 /*
3050 * First check if we are on a hexadecimal number, after the "0x".
3051 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003052 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003054 if (dobin)
3055 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003056 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003057 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003058 if (has_mbyte)
3059 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003060 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003061
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003062 if (dohex)
3063 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003064 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003065 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003066 if (has_mbyte)
3067 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003068 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003069
3070 if ( dobin
3071 && dohex
3072 && ! ((col > 0
3073 && (ptr[col] == 'X'
3074 || ptr[col] == 'x')
3075 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003076 && (!has_mbyte ||
3077 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003078 && vim_isxdigit(ptr[col + 1]))))
3079 {
3080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003082
Bram Moolenaard79e5502016-01-10 22:13:02 +01003083 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003084
3085 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003086 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003087 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003088 if (has_mbyte)
3089 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003090 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003091 }
3092
3093 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003094 && col > 0
3095 && (ptr[col] == 'X'
3096 || ptr[col] == 'x')
3097 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003098 && (!has_mbyte ||
3099 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003100 && vim_isxdigit(ptr[col + 1])) ||
3101 ( dobin
3102 && col > 0
3103 && (ptr[col] == 'B'
3104 || ptr[col] == 'b')
3105 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003106 && (!has_mbyte ||
3107 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003108 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003109 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003110 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003111 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003112 if (has_mbyte)
3113 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003114 }
3115 else
3116 {
3117 /*
3118 * Search forward and then backward to find the start of number.
3119 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003120 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003121
3122 while (ptr[col] != NUL
3123 && !vim_isdigit(ptr[col])
3124 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02003125 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003126
3127 while (col > 0
3128 && vim_isdigit(ptr[col - 1])
3129 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003130 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003131 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003132 if (has_mbyte)
3133 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003134 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003135 }
3136 }
3137
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003138 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003139 {
3140 while (ptr[col] != NUL && length > 0
3141 && !vim_isdigit(ptr[col])
3142 && !(doalp && ASCII_ISALPHA(ptr[col])))
3143 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02003144 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003145
3146 col += mb_len;
3147 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003148 }
3149
3150 if (length == 0)
3151 goto theend;
3152
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003153 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003154 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003155 {
3156 negative = TRUE;
3157 was_positive = FALSE;
3158 }
3159 }
3160
3161 /*
3162 * If a number was found, and saving for undo works, replace the number.
3163 */
3164 firstdigit = ptr[col];
3165 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
3166 {
3167 beep_flush();
3168 goto theend;
3169 }
3170
3171 if (doalp && ASCII_ISALPHA(firstdigit))
3172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01003174 if (op_type == OP_NR_SUB)
3175 {
3176 if (CharOrd(firstdigit) < Prenum1)
3177 {
3178 if (isupper(firstdigit))
3179 firstdigit = 'A';
3180 else
3181 firstdigit = 'a';
3182 }
3183 else
3184#ifdef EBCDIC
3185 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
3186#else
3187 firstdigit -= Prenum1;
3188#endif
3189 }
3190 else
3191 {
3192 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
3193 {
3194 if (isupper(firstdigit))
3195 firstdigit = 'Z';
3196 else
3197 firstdigit = 'z';
3198 }
3199 else
3200#ifdef EBCDIC
3201 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
3202#else
3203 firstdigit += Prenum1;
3204#endif
3205 }
3206 curwin->w_cursor.col = col;
3207 if (!did_change)
3208 startpos = curwin->w_cursor;
3209 did_change = TRUE;
3210 (void)del_char(FALSE);
3211 ins_char(firstdigit);
3212 endpos = curwin->w_cursor;
3213 curwin->w_cursor.col = col;
3214 }
3215 else
3216 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003217 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003218 && (!has_mbyte ||
3219 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003220 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003221 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003223 --col;
3224 negative = TRUE;
3225 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003227 if (visual && VIsual_mode != 'V')
3228 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
3229 ? (int)STRLEN(ptr) - col
3230 : length);
3231
3232 vim_str2nr(ptr + col, &pre, &length,
3233 0 + (dobin ? STR2NR_BIN : 0)
3234 + (dooct ? STR2NR_OCT : 0)
3235 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003236 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01003239 if (pre && negative)
3240 {
3241 ++col;
3242 --length;
3243 negative = FALSE;
3244 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003245 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01003246 subtract = FALSE;
3247 if (op_type == OP_NR_SUB)
3248 subtract ^= TRUE;
3249 if (negative)
3250 subtract ^= TRUE;
3251
3252 oldn = n;
3253 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003254 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003255 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003256 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01003258 if (!pre)
3259 {
3260 if (subtract)
3261 {
3262 if (n > oldn)
3263 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003264 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003265 negative ^= TRUE;
3266 }
3267 }
3268 else
3269 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003270 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01003271 if (n < oldn)
3272 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003273 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003274 negative ^= TRUE;
3275 }
3276 }
3277 if (n == 0)
3278 negative = FALSE;
3279 }
3280
3281 if (visual && !was_positive && !negative && col > 0)
3282 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003283 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01003284 col--;
3285 length++;
3286 }
3287
3288 /*
3289 * Delete the old number.
3290 */
3291 curwin->w_cursor.col = col;
3292 if (!did_change)
3293 startpos = curwin->w_cursor;
3294 did_change = TRUE;
3295 todel = length;
3296 c = gchar_cursor();
3297 /*
3298 * Don't include the '-' in the length, only the length of the
3299 * part after it is kept the same.
3300 */
3301 if (c == '-')
3302 --length;
3303 while (todel-- > 0)
3304 {
3305 if (c < 0x100 && isalpha(c))
3306 {
3307 if (isupper(c))
3308 hexupper = TRUE;
3309 else
3310 hexupper = FALSE;
3311 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003312 // del_char() will mark line needing displaying
Bram Moolenaard79e5502016-01-10 22:13:02 +01003313 (void)del_char(FALSE);
3314 c = gchar_cursor();
3315 }
3316
3317 /*
3318 * Prepare the leading characters in buf1[].
3319 * When there are many leading zeros it could be very long.
3320 * Allocate a bit too much.
3321 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003322 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003323 if (buf1 == NULL)
3324 goto theend;
3325 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003326 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003327 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003328 if (pre)
3329 {
3330 *ptr++ = '0';
3331 --length;
3332 }
3333 if (pre == 'b' || pre == 'B' ||
3334 pre == 'x' || pre == 'X')
3335 {
3336 *ptr++ = pre;
3337 --length;
3338 }
3339
3340 /*
3341 * Put the number characters in buf2[].
3342 */
3343 if (pre == 'b' || pre == 'B')
3344 {
3345 int i;
3346 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003347 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003348
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003349 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01003350 for (bit = bits; bit > 0; bit--)
3351 if ((n >> (bit - 1)) & 0x1) break;
3352
3353 for (i = 0; bit > 0; bit--)
3354 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3355
3356 buf2[i] = '\0';
3357 }
3358 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02003359 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003360 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003361 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02003362 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003363 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003364 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02003365 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003366 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003367 else
Bram Moolenaarea391762018-04-08 13:07:22 +02003368 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003369 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003370 length -= (int)STRLEN(buf2);
3371
3372 /*
3373 * Adjust number of zeros to the new number of digits, so the
3374 * total length of the number remains the same.
3375 * Don't do this when
3376 * the result may look like an octal number.
3377 */
3378 if (firstdigit == '0' && !(dooct && pre == 0))
3379 while (length-- > 0)
3380 *ptr++ = '0';
3381 *ptr = NUL;
3382 STRCAT(buf1, buf2);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003383 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01003384 vim_free(buf1);
3385 endpos = curwin->w_cursor;
3386 if (did_change && curwin->w_cursor.col)
3387 --curwin->w_cursor.col;
3388 }
3389
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003390 if (did_change && !cmdmod.lockmarks)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003391 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003392 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003393 curbuf->b_op_start = startpos;
3394 curbuf->b_op_end = endpos;
3395 if (curbuf->b_op_end.col > 0)
3396 --curbuf->b_op_end.col;
3397 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003398
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003399theend:
3400 if (visual)
3401 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003402 else if (did_change)
3403 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003404
Bram Moolenaard79e5502016-01-10 22:13:02 +01003405 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406}
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#if defined(FEAT_CLIPBOARD) || defined(PROTO)
3409/*
3410 * SELECTION / PRIMARY ('*')
3411 *
3412 * Text selection stuff that uses the GUI selection register '*'. When using a
3413 * GUI this may be text from another window, otherwise it is the last text we
3414 * had highlighted with VIsual mode. With mouse support, clicking the middle
3415 * button performs the paste, otherwise you will need to do <"*p>. "
3416 * If not under X, it is synonymous with the clipboard register '+'.
3417 *
3418 * X CLIPBOARD ('+')
3419 *
3420 * Text selection stuff that uses the GUI clipboard register '+'.
3421 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
3422 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
3423 * otherwise you will need to do <"+p>. "
3424 * If not under X, it is synonymous with the selection register '*'.
3425 */
3426
3427/*
3428 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003429 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 * only exist while the owning application exists, so we write to the
3431 * permanent (while X runs) store CUT_BUFFER0.
3432 * Dump the CLIPBOARD selection if we own it (it's logically the more
3433 * 'permanent' of the two), otherwise the PRIMARY one.
3434 * For now, use a hard-coded sanity limit of 1Mb of data.
3435 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003436#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003438x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 Display *dpy;
3441 char_u *str = NULL;
3442 long_u len = 0;
3443 int motion_type = -1;
3444
3445# ifdef FEAT_GUI
3446 if (gui.in_use)
3447 dpy = X_DISPLAY;
3448 else
3449# endif
3450# ifdef FEAT_XCLIPBOARD
3451 dpy = xterm_dpy;
3452# else
3453 return;
3454# endif
3455
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 // Get selection to export
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (clip_plus.owned)
3458 motion_type = clip_convert_selection(&str, &len, &clip_plus);
3459 else if (clip_star.owned)
3460 motion_type = clip_convert_selection(&str, &len, &clip_star);
3461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 // Check it's OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (dpy != NULL && str != NULL && motion_type >= 0
3464 && len < 1024*1024 && len > 0)
3465 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003466 int ok = TRUE;
3467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // The CUT_BUFFER0 is supposed to always contain latin1. Convert from
3469 // 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
3470 // encoding conversion usually doesn't work, so keep the text as-is.
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003471 if (has_mbyte)
3472 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003473 vimconv_T vc;
3474
3475 vc.vc_type = CONV_NONE;
3476 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
3477 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003478 int intlen = len;
3479 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003480
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003481 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003482 conv_str = string_convert(&vc, str, &intlen);
3483 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003484 if (conv_str != NULL)
3485 {
3486 vim_free(str);
3487 str = conv_str;
3488 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003489 else
3490 {
3491 ok = FALSE;
3492 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003493 convert_setup(&vc, NULL, NULL);
3494 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003495 else
3496 {
3497 ok = FALSE;
3498 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003499 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003501 // Do not store the string if conversion failed. Better to use any
3502 // other selection than garbled text.
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003503 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003504 {
3505 XStoreBuffer(dpy, (char *)str, (int)len, 0);
3506 XFlush(dpy);
3507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509
3510 vim_free(str);
3511}
3512#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003513#endif // FEAT_CLIPBOARD || PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003516clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 vim_memset(oap, 0, sizeof(oparg_T));
3519}
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003522 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 *
3524 * "Words" are counted by looking for boundaries between non-space and
3525 * space characters. (it seems to produce results that match 'wc'.)
3526 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003527 * Return value is byte count; word count for the line is added to "*wc".
3528 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 *
3530 * The function will only examine the first "limit" characters in the
3531 * line, stopping if it encounters an end-of-line (NUL byte). In that
3532 * case, eol_size will be added to the character count to account for
3533 * the size of the EOL character.
3534 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003535 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003536line_count_info(
3537 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003538 varnumber_T *wc,
3539 varnumber_T *cc,
3540 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003541 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003543 varnumber_T i;
3544 varnumber_T words = 0;
3545 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 int is_word = 0;
3547
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003548 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 if (is_word)
3551 {
3552 if (vim_isspace(line[i]))
3553 {
3554 words++;
3555 is_word = 0;
3556 }
3557 }
3558 else if (!vim_isspace(line[i]))
3559 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003560 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003561 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563
3564 if (is_word)
3565 words++;
3566 *wc += words;
3567
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003568 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003569 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003572 chars += eol_size;
3573 }
3574 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 return i;
3576}
3577
3578/*
3579 * Give some info about the position of the cursor (for "g CTRL-G").
3580 * In Visual mode, give some info about the selected region. (In this case,
3581 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003582 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 */
3584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003585cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
3587 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003588 char_u buf1[50];
3589 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003591 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003592 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003593 varnumber_T byte_count_cursor = 0;
3594 varnumber_T char_count = 0;
3595 varnumber_T char_count_cursor = 0;
3596 varnumber_T word_count = 0;
3597 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003598 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003599 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 long line_count_selected = 0;
3601 pos_T min_pos, max_pos;
3602 oparg_T oparg;
3603 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 /*
3606 * Compute the length of the file in characters.
3607 */
3608 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3609 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003610 if (dict == NULL)
3611 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003612 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003613 return;
3614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616 else
3617 {
3618 if (get_fileformat(curbuf) == EOL_DOS)
3619 eol_size = 2;
3620 else
3621 eol_size = 1;
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (VIsual_active)
3624 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003625 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
3627 min_pos = VIsual;
3628 max_pos = curwin->w_cursor;
3629 }
3630 else
3631 {
3632 min_pos = curwin->w_cursor;
3633 max_pos = VIsual;
3634 }
3635 if (*p_sel == 'e' && max_pos.col > 0)
3636 --max_pos.col;
3637
3638 if (VIsual_mode == Ctrl_V)
3639 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003640#ifdef FEAT_LINEBREAK
3641 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003642 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003644 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003645 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003646 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 oparg.is_VIsual = 1;
3649 oparg.block_mode = TRUE;
3650 oparg.op_type = OP_NOP;
3651 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003652 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003653#ifdef FEAT_LINEBREAK
3654 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003655 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003656#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003657 if (curwin->w_curswant == MAXCOL)
3658 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003659 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (oparg.end_vcol < oparg.start_vcol)
3661 {
3662 oparg.end_vcol += oparg.start_vcol;
3663 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3664 oparg.end_vcol -= oparg.start_vcol;
3665 }
3666 }
3667 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003672 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003673 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 ui_breakcheck();
3676 if (got_int)
3677 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003678 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003681 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (VIsual_active
3683 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3684 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003685 char_u *s = NULL;
3686 long len = 0L;
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 switch (VIsual_mode)
3689 {
3690 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003694 s = bd.textstart;
3695 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 break;
3697 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003698 s = ml_get(lnum);
3699 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 break;
3701 case 'v':
3702 {
3703 colnr_T start_col = (lnum == min_pos.lnum)
3704 ? min_pos.col : 0;
3705 colnr_T end_col = (lnum == max_pos.lnum)
3706 ? max_pos.col - start_col + 1 : MAXCOL;
3707
Bram Moolenaardef9e822004-12-31 20:58:58 +00003708 s = ml_get(lnum) + start_col;
3709 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 break;
3712 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003713 if (s != NULL)
3714 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003715 byte_count_cursor += line_count_info(s, &word_count_cursor,
3716 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003717 if (lnum == curbuf->b_ml.ml_line_count
3718 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003719 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003720 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003721 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (lnum == curwin->w_cursor.lnum)
3728 {
3729 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003730 char_count_cursor += char_count;
3731 byte_count_cursor = byte_count +
3732 line_count_info(ml_get(lnum),
3733 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003734 (varnumber_T)(curwin->w_cursor.col + 1),
3735 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003739 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003740 &char_count, (varnumber_T)MAXCOL,
3741 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003744 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003745 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003746 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
Bram Moolenaared767a22016-01-03 22:49:16 +01003748 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003750 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003752 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3753 {
3754 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3755 &max_pos.col);
3756 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3757 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3758 }
3759 else
3760 buf1[0] = NUL;
3761
3762 if (char_count_cursor == byte_count_cursor
3763 && char_count == byte_count)
3764 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003765 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003766 buf1, line_count_selected,
3767 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003768 (long_long_T)word_count_cursor,
3769 (long_long_T)word_count,
3770 (long_long_T)byte_count_cursor,
3771 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003772 else
3773 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003774 _("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 +01003775 buf1, line_count_selected,
3776 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003777 (long_long_T)word_count_cursor,
3778 (long_long_T)word_count,
3779 (long_long_T)char_count_cursor,
3780 (long_long_T)char_count,
3781 (long_long_T)byte_count_cursor,
3782 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003785 {
3786 p = ml_get_curline();
3787 validate_virtcol();
3788 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3789 (int)curwin->w_virtcol + 1);
3790 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3791 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
Bram Moolenaared767a22016-01-03 22:49:16 +01003793 if (char_count_cursor == byte_count_cursor
3794 && char_count == byte_count)
3795 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003796 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003797 (char *)buf1, (char *)buf2,
3798 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003800 (long_long_T)word_count_cursor, (long_long_T)word_count,
3801 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003802 else
3803 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003804 _("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 +01003805 (char *)buf1, (char *)buf2,
3806 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003807 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003808 (long_long_T)word_count_cursor, (long_long_T)word_count,
3809 (long_long_T)char_count_cursor, (long_long_T)char_count,
3810 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003811 }
3812 }
3813
Bram Moolenaared767a22016-01-03 22:49:16 +01003814 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003815 if (dict == NULL && bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01003816 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003817 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003818 if (dict == NULL)
3819 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003820 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003821 p = p_shm;
3822 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003823 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003824 p_shm = p;
3825 }
3826 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003827#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003828 if (dict != NULL)
3829 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003830 dict_add_number(dict, "words", word_count);
3831 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003832 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003833 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3834 byte_count_cursor);
3835 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3836 char_count_cursor);
3837 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3838 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003842
3843/*
3844 * Handle indent and format operators and visual mode ":".
3845 */
3846 static void
3847op_colon(oparg_T *oap)
3848{
3849 stuffcharReadbuff(':');
3850 if (oap->is_VIsual)
3851 stuffReadbuff((char_u *)"'<,'>");
3852 else
3853 {
3854 // Make the range look nice, so it can be repeated.
3855 if (oap->start.lnum == curwin->w_cursor.lnum)
3856 stuffcharReadbuff('.');
3857 else
3858 stuffnumReadbuff((long)oap->start.lnum);
3859 if (oap->end.lnum != oap->start.lnum)
3860 {
3861 stuffcharReadbuff(',');
3862 if (oap->end.lnum == curwin->w_cursor.lnum)
3863 stuffcharReadbuff('.');
3864 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3865 stuffcharReadbuff('$');
3866 else if (oap->start.lnum == curwin->w_cursor.lnum)
3867 {
3868 stuffReadbuff((char_u *)".+");
3869 stuffnumReadbuff((long)oap->line_count - 1);
3870 }
3871 else
3872 stuffnumReadbuff((long)oap->end.lnum);
3873 }
3874 }
3875 if (oap->op_type != OP_COLON)
3876 stuffReadbuff((char_u *)"!");
3877 if (oap->op_type == OP_INDENT)
3878 {
3879#ifndef FEAT_CINDENT
3880 if (*get_equalprg() == NUL)
3881 stuffReadbuff((char_u *)"indent");
3882 else
3883#endif
3884 stuffReadbuff(get_equalprg());
3885 stuffReadbuff((char_u *)"\n");
3886 }
3887 else if (oap->op_type == OP_FORMAT)
3888 {
3889 if (*curbuf->b_p_fp != NUL)
3890 stuffReadbuff(curbuf->b_p_fp);
3891 else if (*p_fp != NUL)
3892 stuffReadbuff(p_fp);
3893 else
3894 stuffReadbuff((char_u *)"fmt");
3895 stuffReadbuff((char_u *)"\n']");
3896 }
3897
3898 // do_cmdline() does the rest
3899}
3900
3901/*
3902 * Handle the "g@" operator: call 'operatorfunc'.
3903 */
3904 static void
3905op_function(oparg_T *oap UNUSED)
3906{
3907#ifdef FEAT_EVAL
3908 typval_T argv[2];
3909 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003910 pos_T orig_start = curbuf->b_op_start;
3911 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003912
3913 if (*p_opfunc == NUL)
3914 emsg(_("E774: 'operatorfunc' is empty"));
3915 else
3916 {
3917 // Set '[ and '] marks to text to be operated on.
3918 curbuf->b_op_start = oap->start;
3919 curbuf->b_op_end = oap->end;
3920 if (oap->motion_type != MLINE && !oap->inclusive)
3921 // Exclude the end position.
3922 decl(&curbuf->b_op_end);
3923
3924 argv[0].v_type = VAR_STRING;
3925 if (oap->block_mode)
3926 argv[0].vval.v_string = (char_u *)"block";
3927 else if (oap->motion_type == MLINE)
3928 argv[0].vval.v_string = (char_u *)"line";
3929 else
3930 argv[0].vval.v_string = (char_u *)"char";
3931 argv[1].v_type = VAR_UNKNOWN;
3932
3933 // Reset virtual_op so that 'virtualedit' can be changed in the
3934 // function.
3935 virtual_op = MAYBE;
3936
3937 (void)call_func_retnr(p_opfunc, 1, argv);
3938
3939 virtual_op = save_virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003940 if (cmdmod.lockmarks)
3941 {
3942 curbuf->b_op_start = orig_start;
3943 curbuf->b_op_end = orig_end;
3944 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003945 }
3946#else
3947 emsg(_("E775: Eval feature not available"));
3948#endif
3949}
3950
3951/*
3952 * Calculate start/end virtual columns for operating in block mode.
3953 */
3954 static void
3955get_op_vcol(
3956 oparg_T *oap,
3957 colnr_T redo_VIsual_vcol,
3958 int initial) // when TRUE adjust position for 'selectmode'
3959{
3960 colnr_T start, end;
3961
3962 if (VIsual_mode != Ctrl_V
3963 || (!initial && oap->end.col < curwin->w_width))
3964 return;
3965
3966 oap->block_mode = TRUE;
3967
3968 // prevent from moving onto a trail byte
3969 if (has_mbyte)
3970 mb_adjustpos(curwin->w_buffer, &oap->end);
3971
3972 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3973
3974 if (!redo_VIsual_busy)
3975 {
3976 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3977
3978 if (start < oap->start_vcol)
3979 oap->start_vcol = start;
3980 if (end > oap->end_vcol)
3981 {
3982 if (initial && *p_sel == 'e' && start >= 1
3983 && start - 1 >= oap->end_vcol)
3984 oap->end_vcol = start - 1;
3985 else
3986 oap->end_vcol = end;
3987 }
3988 }
3989
3990 // if '$' was used, get oap->end_vcol from longest line
3991 if (curwin->w_curswant == MAXCOL)
3992 {
3993 curwin->w_cursor.col = MAXCOL;
3994 oap->end_vcol = 0;
3995 for (curwin->w_cursor.lnum = oap->start.lnum;
3996 curwin->w_cursor.lnum <= oap->end.lnum;
3997 ++curwin->w_cursor.lnum)
3998 {
3999 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
4000 if (end > oap->end_vcol)
4001 oap->end_vcol = end;
4002 }
4003 }
4004 else if (redo_VIsual_busy)
4005 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
4006 // Correct oap->end.col and oap->start.col to be the
4007 // upper-left and lower-right corner of the block area.
4008 //
4009 // (Actually, this does convert column positions into character
4010 // positions)
4011 curwin->w_cursor.lnum = oap->end.lnum;
4012 coladvance(oap->end_vcol);
4013 oap->end = curwin->w_cursor;
4014
4015 curwin->w_cursor = oap->start;
4016 coladvance(oap->start_vcol);
4017 oap->start = curwin->w_cursor;
4018}
4019
4020/*
4021 * Handle an operator after Visual mode or when the movement is finished.
4022 * "gui_yank" is true when yanking text for the clipboard.
4023 */
4024 void
4025do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
4026{
4027 oparg_T *oap = cap->oap;
4028 pos_T old_cursor;
4029 int empty_region_error;
4030 int restart_edit_save;
4031#ifdef FEAT_LINEBREAK
4032 int lbr_saved = curwin->w_p_lbr;
4033#endif
4034
4035 // The visual area is remembered for redo
4036 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
4037 static linenr_T redo_VIsual_line_count; // number of lines
4038 static colnr_T redo_VIsual_vcol; // number of cols or end column
4039 static long redo_VIsual_count; // count for Visual operator
4040 static int redo_VIsual_arg; // extra argument
4041 int include_line_break = FALSE;
4042
4043#if defined(FEAT_CLIPBOARD)
4044 // Yank the visual area into the GUI selection register before we operate
4045 // on it and lose it forever.
4046 // Don't do it if a specific register was specified, so that ""x"*P works.
4047 // This could call do_pending_operator() recursively, but that's OK
4048 // because gui_yank will be TRUE for the nested call.
4049 if ((clip_star.available || clip_plus.available)
4050 && oap->op_type != OP_NOP
4051 && !gui_yank
4052 && VIsual_active
4053 && !redo_VIsual_busy
4054 && oap->regname == 0)
4055 clip_auto_select();
4056#endif
4057 old_cursor = curwin->w_cursor;
4058
4059 // If an operation is pending, handle it...
4060 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
4061 {
4062 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
4063 // for the clipboard.
4064 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
4065
4066#ifdef FEAT_LINEBREAK
4067 // Avoid a problem with unwanted linebreaks in block mode.
4068 if (curwin->w_p_lbr)
4069 curwin->w_valid &= ~VALID_VIRTCOL;
4070 curwin->w_p_lbr = FALSE;
4071#endif
4072 oap->is_VIsual = VIsual_active;
4073 if (oap->motion_force == 'V')
4074 oap->motion_type = MLINE;
4075 else if (oap->motion_force == 'v')
4076 {
4077 // If the motion was linewise, "inclusive" will not have been set.
4078 // Use "exclusive" to be consistent. Makes "dvj" work nice.
4079 if (oap->motion_type == MLINE)
4080 oap->inclusive = FALSE;
4081 // If the motion already was characterwise, toggle "inclusive"
4082 else if (oap->motion_type == MCHAR)
4083 oap->inclusive = !oap->inclusive;
4084 oap->motion_type = MCHAR;
4085 }
4086 else if (oap->motion_force == Ctrl_V)
4087 {
4088 // Change line- or characterwise motion into Visual block mode.
4089 if (!VIsual_active)
4090 {
4091 VIsual_active = TRUE;
4092 VIsual = oap->start;
4093 }
4094 VIsual_mode = Ctrl_V;
4095 VIsual_select = FALSE;
4096 VIsual_reselect = FALSE;
4097 }
4098
4099 // Only redo yank when 'y' flag is in 'cpoptions'.
4100 // Never redo "zf" (define fold).
4101 if ((redo_yank || oap->op_type != OP_YANK)
4102 && ((!VIsual_active || oap->motion_force)
4103 // Also redo Operator-pending Visual mode mappings
4104 || (VIsual_active && cap->cmdchar == ':'
4105 && oap->op_type != OP_COLON))
4106 && cap->cmdchar != 'D'
4107#ifdef FEAT_FOLDING
4108 && oap->op_type != OP_FOLD
4109 && oap->op_type != OP_FOLDOPEN
4110 && oap->op_type != OP_FOLDOPENREC
4111 && oap->op_type != OP_FOLDCLOSE
4112 && oap->op_type != OP_FOLDCLOSEREC
4113 && oap->op_type != OP_FOLDDEL
4114 && oap->op_type != OP_FOLDDELREC
4115#endif
4116 )
4117 {
4118 prep_redo(oap->regname, cap->count0,
4119 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
4120 oap->motion_force, cap->cmdchar, cap->nchar);
4121 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
4122 {
4123 // If 'cpoptions' does not contain 'r', insert the search
4124 // pattern to really repeat the same command.
4125 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
4126 AppendToRedobuffLit(cap->searchbuf, -1);
4127 AppendToRedobuff(NL_STR);
4128 }
4129 else if (cap->cmdchar == ':')
4130 {
4131 // do_cmdline() has stored the first typed line in
4132 // "repeat_cmdline". When several lines are typed repeating
4133 // won't be possible.
4134 if (repeat_cmdline == NULL)
4135 ResetRedobuff();
4136 else
4137 {
4138 AppendToRedobuffLit(repeat_cmdline, -1);
4139 AppendToRedobuff(NL_STR);
4140 VIM_CLEAR(repeat_cmdline);
4141 }
4142 }
4143 }
4144
4145 if (redo_VIsual_busy)
4146 {
4147 // Redo of an operation on a Visual area. Use the same size from
4148 // redo_VIsual_line_count and redo_VIsual_vcol.
4149 oap->start = curwin->w_cursor;
4150 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
4151 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4152 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4153 VIsual_mode = redo_VIsual_mode;
4154 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
4155 {
4156 if (VIsual_mode == 'v')
4157 {
4158 if (redo_VIsual_line_count <= 1)
4159 {
4160 validate_virtcol();
4161 curwin->w_curswant =
4162 curwin->w_virtcol + redo_VIsual_vcol - 1;
4163 }
4164 else
4165 curwin->w_curswant = redo_VIsual_vcol;
4166 }
4167 else
4168 {
4169 curwin->w_curswant = MAXCOL;
4170 }
4171 coladvance(curwin->w_curswant);
4172 }
4173 cap->count0 = redo_VIsual_count;
4174 if (redo_VIsual_count != 0)
4175 cap->count1 = redo_VIsual_count;
4176 else
4177 cap->count1 = 1;
4178 }
4179 else if (VIsual_active)
4180 {
4181 if (!gui_yank)
4182 {
4183 // Save the current VIsual area for '< and '> marks, and "gv"
4184 curbuf->b_visual.vi_start = VIsual;
4185 curbuf->b_visual.vi_end = curwin->w_cursor;
4186 curbuf->b_visual.vi_mode = VIsual_mode;
4187 restore_visual_mode();
4188 curbuf->b_visual.vi_curswant = curwin->w_curswant;
4189# ifdef FEAT_EVAL
4190 curbuf->b_visual_mode_eval = VIsual_mode;
4191# endif
4192 }
4193
4194 // In Select mode, a linewise selection is operated upon like a
4195 // characterwise selection.
4196 // Special case: gH<Del> deletes the last line.
4197 if (VIsual_select && VIsual_mode == 'V'
4198 && cap->oap->op_type != OP_DELETE)
4199 {
4200 if (LT_POS(VIsual, curwin->w_cursor))
4201 {
4202 VIsual.col = 0;
4203 curwin->w_cursor.col =
4204 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
4205 }
4206 else
4207 {
4208 curwin->w_cursor.col = 0;
4209 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
4210 }
4211 VIsual_mode = 'v';
4212 }
4213 // If 'selection' is "exclusive", backup one character for
4214 // charwise selections.
4215 else if (VIsual_mode == 'v')
4216 include_line_break = unadjust_for_sel();
4217
4218 oap->start = VIsual;
4219 if (VIsual_mode == 'V')
4220 {
4221 oap->start.col = 0;
4222 oap->start.coladd = 0;
4223 }
4224 }
4225
4226 // Set oap->start to the first position of the operated text, oap->end
4227 // to the end of the operated text. w_cursor is equal to oap->start.
4228 if (LT_POS(oap->start, curwin->w_cursor))
4229 {
4230#ifdef FEAT_FOLDING
4231 // Include folded lines completely.
4232 if (!VIsual_active)
4233 {
4234 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
4235 oap->start.col = 0;
4236 if ((curwin->w_cursor.col > 0 || oap->inclusive)
4237 && hasFolding(curwin->w_cursor.lnum, NULL,
4238 &curwin->w_cursor.lnum))
4239 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
4240 }
4241#endif
4242 oap->end = curwin->w_cursor;
4243 curwin->w_cursor = oap->start;
4244
4245 // w_virtcol may have been updated; if the cursor goes back to its
4246 // previous position w_virtcol becomes invalid and isn't updated
4247 // automatically.
4248 curwin->w_valid &= ~VALID_VIRTCOL;
4249 }
4250 else
4251 {
4252#ifdef FEAT_FOLDING
4253 // Include folded lines completely.
4254 if (!VIsual_active && oap->motion_type == MLINE)
4255 {
4256 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
4257 NULL))
4258 curwin->w_cursor.col = 0;
4259 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
4260 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
4261 }
4262#endif
4263 oap->end = oap->start;
4264 oap->start = curwin->w_cursor;
4265 }
4266
4267 // Just in case lines were deleted that make the position invalid.
4268 check_pos(curwin->w_buffer, &oap->end);
4269 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
4270
4271 // Set "virtual_op" before resetting VIsual_active.
4272 virtual_op = virtual_active();
4273
4274 if (VIsual_active || redo_VIsual_busy)
4275 {
4276 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
4277
4278 if (!redo_VIsual_busy && !gui_yank)
4279 {
4280 // Prepare to reselect and redo Visual: this is based on the
4281 // size of the Visual text
4282 resel_VIsual_mode = VIsual_mode;
4283 if (curwin->w_curswant == MAXCOL)
4284 resel_VIsual_vcol = MAXCOL;
4285 else
4286 {
4287 if (VIsual_mode != Ctrl_V)
4288 getvvcol(curwin, &(oap->end),
4289 NULL, NULL, &oap->end_vcol);
4290 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
4291 {
4292 if (VIsual_mode != Ctrl_V)
4293 getvvcol(curwin, &(oap->start),
4294 &oap->start_vcol, NULL, NULL);
4295 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
4296 }
4297 else
4298 resel_VIsual_vcol = oap->end_vcol;
4299 }
4300 resel_VIsual_line_count = oap->line_count;
4301 }
4302
4303 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
4304 if ((redo_yank || oap->op_type != OP_YANK)
4305 && oap->op_type != OP_COLON
4306#ifdef FEAT_FOLDING
4307 && oap->op_type != OP_FOLD
4308 && oap->op_type != OP_FOLDOPEN
4309 && oap->op_type != OP_FOLDOPENREC
4310 && oap->op_type != OP_FOLDCLOSE
4311 && oap->op_type != OP_FOLDCLOSEREC
4312 && oap->op_type != OP_FOLDDEL
4313 && oap->op_type != OP_FOLDDELREC
4314#endif
4315 && oap->motion_force == NUL
4316 )
4317 {
4318 // Prepare for redoing. Only use the nchar field for "r",
4319 // otherwise it might be the second char of the operator.
4320 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4321 || cap->nchar == 'N'))
4322 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004323 get_op_char(oap->op_type),
4324 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004325 oap->motion_force, cap->cmdchar, cap->nchar);
4326 else if (cap->cmdchar != ':')
4327 {
4328 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4329
4330 // reverse what nv_replace() did
4331 if (nchar == REPLACE_CR_NCHAR)
4332 nchar = CAR;
4333 else if (nchar == REPLACE_NL_NCHAR)
4334 nchar = NL;
4335 prep_redo(oap->regname, 0L, NUL, 'v',
4336 get_op_char(oap->op_type),
4337 get_extra_op_char(oap->op_type),
4338 nchar);
4339 }
4340 if (!redo_VIsual_busy)
4341 {
4342 redo_VIsual_mode = resel_VIsual_mode;
4343 redo_VIsual_vcol = resel_VIsual_vcol;
4344 redo_VIsual_line_count = resel_VIsual_line_count;
4345 redo_VIsual_count = cap->count0;
4346 redo_VIsual_arg = cap->arg;
4347 }
4348 }
4349
4350 // oap->inclusive defaults to TRUE.
4351 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4352 // FALSE. This makes "d}P" and "v}dP" work the same.
4353 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4354 oap->inclusive = TRUE;
4355 if (VIsual_mode == 'V')
4356 oap->motion_type = MLINE;
4357 else
4358 {
4359 oap->motion_type = MCHAR;
4360 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4361 && (include_line_break || !virtual_op))
4362 {
4363 oap->inclusive = FALSE;
4364 // Try to include the newline, unless it's an operator
4365 // that works on lines only.
4366 if (*p_sel != 'o'
4367 && !op_on_lines(oap->op_type)
4368 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4369 {
4370 ++oap->end.lnum;
4371 oap->end.col = 0;
4372 oap->end.coladd = 0;
4373 ++oap->line_count;
4374 }
4375 }
4376 }
4377
4378 redo_VIsual_busy = FALSE;
4379
4380 // Switch Visual off now, so screen updating does
4381 // not show inverted text when the screen is redrawn.
4382 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4383 // no screen redraw, so it is done here to remove the inverted
4384 // part.
4385 if (!gui_yank)
4386 {
4387 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004388 setmouse();
4389 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004390 may_clear_cmdline();
4391 if ((oap->op_type == OP_YANK
4392 || oap->op_type == OP_COLON
4393 || oap->op_type == OP_FUNCTION
4394 || oap->op_type == OP_FILTER)
4395 && oap->motion_force == NUL)
4396 {
4397#ifdef FEAT_LINEBREAK
4398 // make sure redrawing is correct
4399 curwin->w_p_lbr = lbr_saved;
4400#endif
4401 redraw_curbuf_later(INVERTED);
4402 }
4403 }
4404 }
4405
4406 // Include the trailing byte of a multi-byte char.
4407 if (has_mbyte && oap->inclusive)
4408 {
4409 int l;
4410
4411 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4412 if (l > 1)
4413 oap->end.col += l - 1;
4414 }
4415 curwin->w_set_curswant = TRUE;
4416
4417 // oap->empty is set when start and end are the same. The inclusive
4418 // flag affects this too, unless yanking and the end is on a NUL.
4419 oap->empty = (oap->motion_type == MCHAR
4420 && (!oap->inclusive
4421 || (oap->op_type == OP_YANK
4422 && gchar_pos(&oap->end) == NUL))
4423 && EQUAL_POS(oap->start, oap->end)
4424 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4425 // For delete, change and yank, it's an error to operate on an
4426 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4427 empty_region_error = (oap->empty
4428 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4429
4430 // Force a redraw when operating on an empty Visual region, when
4431 // 'modifiable is off or creating a fold.
4432 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4433#ifdef FEAT_FOLDING
4434 || oap->op_type == OP_FOLD
4435#endif
4436 ))
4437 {
4438#ifdef FEAT_LINEBREAK
4439 curwin->w_p_lbr = lbr_saved;
4440#endif
4441 redraw_curbuf_later(INVERTED);
4442 }
4443
4444 // If the end of an operator is in column one while oap->motion_type
4445 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4446 // character in the previous line. If op_start is on or before the
4447 // first non-blank in the line, the operator becomes linewise
4448 // (strange, but that's the way vi does it).
4449 if ( oap->motion_type == MCHAR
4450 && oap->inclusive == FALSE
4451 && !(cap->retval & CA_NO_ADJ_OP_END)
4452 && oap->end.col == 0
4453 && (!oap->is_VIsual || *p_sel == 'o')
4454 && !oap->block_mode
4455 && oap->line_count > 1)
4456 {
4457 oap->end_adjusted = TRUE; // remember that we did this
4458 --oap->line_count;
4459 --oap->end.lnum;
4460 if (inindent(0))
4461 oap->motion_type = MLINE;
4462 else
4463 {
4464 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
4465 if (oap->end.col)
4466 {
4467 --oap->end.col;
4468 oap->inclusive = TRUE;
4469 }
4470 }
4471 }
4472 else
4473 oap->end_adjusted = FALSE;
4474
4475 switch (oap->op_type)
4476 {
4477 case OP_LSHIFT:
4478 case OP_RSHIFT:
4479 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4480 auto_format(FALSE, TRUE);
4481 break;
4482
4483 case OP_JOIN_NS:
4484 case OP_JOIN:
4485 if (oap->line_count < 2)
4486 oap->line_count = 2;
4487 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4488 curbuf->b_ml.ml_line_count)
4489 beep_flush();
4490 else
4491 {
4492 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4493 TRUE, TRUE, TRUE);
4494 auto_format(FALSE, TRUE);
4495 }
4496 break;
4497
4498 case OP_DELETE:
4499 VIsual_reselect = FALSE; // don't reselect now
4500 if (empty_region_error)
4501 {
4502 vim_beep(BO_OPER);
4503 CancelRedo();
4504 }
4505 else
4506 {
4507 (void)op_delete(oap);
4508 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
4509 u_save_cursor(); // cursor line wasn't saved yet
4510 auto_format(FALSE, TRUE);
4511 }
4512 break;
4513
4514 case OP_YANK:
4515 if (empty_region_error)
4516 {
4517 if (!gui_yank)
4518 {
4519 vim_beep(BO_OPER);
4520 CancelRedo();
4521 }
4522 }
4523 else
4524 {
4525#ifdef FEAT_LINEBREAK
4526 curwin->w_p_lbr = lbr_saved;
4527#endif
4528 (void)op_yank(oap, FALSE, !gui_yank);
4529 }
4530 check_cursor_col();
4531 break;
4532
4533 case OP_CHANGE:
4534 VIsual_reselect = FALSE; // don't reselect now
4535 if (empty_region_error)
4536 {
4537 vim_beep(BO_OPER);
4538 CancelRedo();
4539 }
4540 else
4541 {
4542 // This is a new edit command, not a restart. Need to
4543 // remember it to make 'insertmode' work with mappings for
4544 // Visual mode. But do this only once and not when typed and
4545 // 'insertmode' isn't set.
4546 if (p_im || !KeyTyped)
4547 restart_edit_save = restart_edit;
4548 else
4549 restart_edit_save = 0;
4550 restart_edit = 0;
4551#ifdef FEAT_LINEBREAK
4552 // Restore linebreak, so that when the user edits it looks as
4553 // before.
4554 if (curwin->w_p_lbr != lbr_saved)
4555 {
4556 curwin->w_p_lbr = lbr_saved;
4557 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4558 }
4559#endif
4560 // Reset finish_op now, don't want it set inside edit().
4561 finish_op = FALSE;
4562 if (op_change(oap)) // will call edit()
4563 cap->retval |= CA_COMMAND_BUSY;
4564 if (restart_edit == 0)
4565 restart_edit = restart_edit_save;
4566 }
4567 break;
4568
4569 case OP_FILTER:
4570 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4571 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4572 else
4573 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4574 // FALLTHROUGH
4575
4576 case OP_INDENT:
4577 case OP_COLON:
4578
4579#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4580 // If 'equalprg' is empty, do the indenting internally.
4581 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4582 {
4583# ifdef FEAT_LISP
4584 if (curbuf->b_p_lisp)
4585 {
4586 op_reindent(oap, get_lisp_indent);
4587 break;
4588 }
4589# endif
4590# ifdef FEAT_CINDENT
4591 op_reindent(oap,
4592# ifdef FEAT_EVAL
4593 *curbuf->b_p_inde != NUL ? get_expr_indent :
4594# endif
4595 get_c_indent);
4596 break;
4597# endif
4598 }
4599#endif
4600
4601 op_colon(oap);
4602 break;
4603
4604 case OP_TILDE:
4605 case OP_UPPER:
4606 case OP_LOWER:
4607 case OP_ROT13:
4608 if (empty_region_error)
4609 {
4610 vim_beep(BO_OPER);
4611 CancelRedo();
4612 }
4613 else
4614 op_tilde(oap);
4615 check_cursor_col();
4616 break;
4617
4618 case OP_FORMAT:
4619#if defined(FEAT_EVAL)
4620 if (*curbuf->b_p_fex != NUL)
4621 op_formatexpr(oap); // use expression
4622 else
4623#endif
4624 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
4625 op_colon(oap); // use external command
4626 else
4627 op_format(oap, FALSE); // use internal function
4628 break;
4629
4630 case OP_FORMAT2:
4631 op_format(oap, TRUE); // use internal function
4632 break;
4633
4634 case OP_FUNCTION:
4635#ifdef FEAT_LINEBREAK
4636 // Restore linebreak, so that when the user edits it looks as
4637 // before.
4638 curwin->w_p_lbr = lbr_saved;
4639#endif
4640 op_function(oap); // call 'operatorfunc'
4641 break;
4642
4643 case OP_INSERT:
4644 case OP_APPEND:
4645 VIsual_reselect = FALSE; // don't reselect now
4646 if (empty_region_error)
4647 {
4648 vim_beep(BO_OPER);
4649 CancelRedo();
4650 }
4651 else
4652 {
4653 // This is a new edit command, not a restart. Need to
4654 // remember it to make 'insertmode' work with mappings for
4655 // Visual mode. But do this only once.
4656 restart_edit_save = restart_edit;
4657 restart_edit = 0;
4658#ifdef FEAT_LINEBREAK
4659 // Restore linebreak, so that when the user edits it looks as
4660 // before.
4661 if (curwin->w_p_lbr != lbr_saved)
4662 {
4663 curwin->w_p_lbr = lbr_saved;
4664 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4665 }
4666#endif
4667 op_insert(oap, cap->count1);
4668#ifdef FEAT_LINEBREAK
4669 // Reset linebreak, so that formatting works correctly.
4670 curwin->w_p_lbr = FALSE;
4671#endif
4672
4673 // TODO: when inserting in several lines, should format all
4674 // the lines.
4675 auto_format(FALSE, TRUE);
4676
4677 if (restart_edit == 0)
4678 restart_edit = restart_edit_save;
4679 else
4680 cap->retval |= CA_COMMAND_BUSY;
4681 }
4682 break;
4683
4684 case OP_REPLACE:
4685 VIsual_reselect = FALSE; // don't reselect now
4686 if (empty_region_error)
4687 {
4688 vim_beep(BO_OPER);
4689 CancelRedo();
4690 }
4691 else
4692 {
4693#ifdef FEAT_LINEBREAK
4694 // Restore linebreak, so that when the user edits it looks as
4695 // before.
4696 if (curwin->w_p_lbr != lbr_saved)
4697 {
4698 curwin->w_p_lbr = lbr_saved;
4699 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4700 }
4701#endif
4702 op_replace(oap, cap->nchar);
4703 }
4704 break;
4705
4706#ifdef FEAT_FOLDING
4707 case OP_FOLD:
4708 VIsual_reselect = FALSE; // don't reselect now
4709 foldCreate(oap->start.lnum, oap->end.lnum);
4710 break;
4711
4712 case OP_FOLDOPEN:
4713 case OP_FOLDOPENREC:
4714 case OP_FOLDCLOSE:
4715 case OP_FOLDCLOSEREC:
4716 VIsual_reselect = FALSE; // don't reselect now
4717 opFoldRange(oap->start.lnum, oap->end.lnum,
4718 oap->op_type == OP_FOLDOPEN
4719 || oap->op_type == OP_FOLDOPENREC,
4720 oap->op_type == OP_FOLDOPENREC
4721 || oap->op_type == OP_FOLDCLOSEREC,
4722 oap->is_VIsual);
4723 break;
4724
4725 case OP_FOLDDEL:
4726 case OP_FOLDDELREC:
4727 VIsual_reselect = FALSE; // don't reselect now
4728 deleteFold(oap->start.lnum, oap->end.lnum,
4729 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4730 break;
4731#endif
4732 case OP_NR_ADD:
4733 case OP_NR_SUB:
4734 if (empty_region_error)
4735 {
4736 vim_beep(BO_OPER);
4737 CancelRedo();
4738 }
4739 else
4740 {
4741 VIsual_active = TRUE;
4742#ifdef FEAT_LINEBREAK
4743 curwin->w_p_lbr = lbr_saved;
4744#endif
4745 op_addsub(oap, cap->count1, redo_VIsual_arg);
4746 VIsual_active = FALSE;
4747 }
4748 check_cursor_col();
4749 break;
4750 default:
4751 clearopbeep(oap);
4752 }
4753 virtual_op = MAYBE;
4754 if (!gui_yank)
4755 {
4756 // if 'sol' not set, go back to old column for some commands
4757 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4758 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4759 || oap->op_type == OP_DELETE))
4760 {
4761#ifdef FEAT_LINEBREAK
4762 curwin->w_p_lbr = FALSE;
4763#endif
4764 coladvance(curwin->w_curswant = old_col);
4765 }
4766 }
4767 else
4768 {
4769 curwin->w_cursor = old_cursor;
4770 }
4771 oap->block_mode = FALSE;
4772 clearop(oap);
4773 motion_force = NUL;
4774 }
4775#ifdef FEAT_LINEBREAK
4776 curwin->w_p_lbr = lbr_saved;
4777#endif
4778}