blob: 42b0dcbf9d8e6e849928c0a1c3a2adec09f1a8c9 [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
75 if (char1 == 'r') /* ignore second character */
76 return OP_REPLACE;
77 if (char1 == '~') /* when tilde is an operator */
78 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +010079 if (char1 == 'g' && char2 == Ctrl_A) /* add */
80 return OP_NR_ADD;
81 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
82 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();
155 if (first_char == NUL) /* empty line */
156 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
160 /* 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'. */
162#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 Moolenaarf7ff6e82014-03-23 15:13:05 +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;
178 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
179 }
180 else
181 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
182
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183#ifdef FEAT_FOLDING
184 /* The cursor line is not in a closed fold */
185 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
209 /*
210 * Set "'[" and "']" marks.
211 */
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}
218
219/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100220 * Shift the current line one shiftwidth left (if left != 0) or right
221 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 */
223 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100224shift_line(
225 int left,
226 int round,
227 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200228 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
230 int count;
231 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200232 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200234 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200236 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200238 i = count / sw_val; // number of 'shiftwidth' rounded down
239 j = count % sw_val; // extra spaces
240 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 --amount;
242 if (left)
243 {
244 i -= amount;
245 if (i < 0)
246 i = 0;
247 }
248 else
249 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200250 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200252 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
254 if (left)
255 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200256 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 if (count < 0)
258 count = 0;
259 }
260 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200261 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
263
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200264 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000266 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000268 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269}
270
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271/*
272 * Shift one line of the current block one shiftwidth right or left.
273 * Leaves cursor on first character in block.
274 */
275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100276shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277{
278 int left = (oap->op_type == OP_LSHIFT);
279 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000280 int total;
281 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200283 int sw_val = (int)get_sw_value_indent(curbuf);
284 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000287 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 int i = 0, j = 0;
289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#ifdef FEAT_RIGHTLEFT
291 int old_p_ri = p_ri;
292
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200293 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#endif
295
296 State = INSERT; /* don't want REPLACE for State */
297 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
298 if (bd.is_short)
299 return;
300
301 /* total is number of screen columns to be inserted/removed */
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 total = (int)((unsigned)amount * (unsigned)sw_val);
303 if ((total / sw_val) != amount)
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200304 return; /* multiplication overflow */
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 oldp = ml_get_curline();
307
308 if (!left)
309 {
310 /*
311 * 1. Get start vcol
312 * 2. Total ws vcols
313 * 3. Divvy into TABs & spp
314 * 4. Construct new string
315 */
316 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
317 ws_vcol = bd.start_vcol - bd.pre_whitesp;
318 if (bd.startspaces)
319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100321 {
322 if ((*mb_ptr2len)(bd.textstart) == 1)
323 ++bd.textstart;
324 else
325 {
326 ws_vcol = 0;
327 bd.startspaces = 0;
328 }
329 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000330 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000331 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100333 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200335 /* TODO: is passing bd.textstart for start of the line OK? */
336 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
337 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 total += incr;
339 bd.start_vcol += incr;
340 }
341 /* OK, now total=all the VWS reqd, and textstart points at the 1st
342 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200343#ifdef FEAT_VARTABS
344 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200345 tabstop_fromto(ws_vcol, ws_vcol + total,
346 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200347 else
348 j = total;
349#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200351 i = ((ws_vcol % ts_val) + total) / ts_val; /* number of tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (i)
Bram Moolenaardac13472019-09-16 21:06:21 +0200353 j = ((ws_vcol % ts_val) + total) % ts_val; /* number of spp */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 else
355 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 /* if we're splitting a TAB, allow for it */
358 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
359 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200360 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 if (newp == NULL)
362 return;
363 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
364 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200365 vim_memset(newp + bd.textcol, TAB, (size_t)i);
366 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 /* the end */
368 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
369 }
370 else /* left */
371 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000372 colnr_T destination_col; /* column to which text in block will
373 be shifted */
374 char_u *verbatim_copy_end; /* end of the part of the line which is
375 copied verbatim */
376 colnr_T verbatim_copy_width;/* the (displayed) width of this part
377 of line */
378 unsigned fill; /* nr of spaces that replace a TAB */
379 unsigned new_line_len; /* the length of the line after the
380 block shift */
381 size_t block_space_width;
382 size_t shift_amount;
383 char_u *non_white = bd.textstart;
384 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000386 /*
387 * Firstly, let's find the first non-whitespace character that is
388 * displayed after the block's start column and the character's column
389 * number. Also, let's calculate the width of all the whitespace
390 * characters that are displayed in the block and precede the searched
391 * non-whitespace character.
392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000394 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
395 * the part of which is displayed at the block's beginning. Let's start
396 * searching from the next character. */
397 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100398 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000399
400 /* The character's column is in "bd.start_vcol". */
401 non_white_col = bd.start_vcol;
402
Bram Moolenaar1c465442017-03-12 20:10:05 +0100403 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000404 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200405 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000406 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000409 block_space_width = non_white_col - oap->start_vcol;
410 /* We will shift by "total" or "block_space_width", whichever is less.
411 */
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
415 /* 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
418 /* Now let's find out how much of the beginning of the line we can
419 * reuse without modification. */
420 verbatim_copy_end = bd.textstart;
421 verbatim_copy_width = bd.start_vcol;
422
423 /* 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. */
426 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
432 /* TODO: is passing verbatim_copy_end for start of the line OK? */
433 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
441 /* 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. */
444 fill = destination_col - verbatim_copy_width;
445
446 /* 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. */
450 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 }
461 /* replace the line */
462 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 Moolenaar071d4272004-06-13 20:20:40 +0000483 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 */
489 int oldstate = State;
490
491 State = INSERT; /* don't want REPLACE for State */
492 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)
498 continue; /* OP_INSERT, line ends before block start */
499
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 Moolenaardac13472019-09-16 21:06:21 +0200507 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 offset = bdp->textcol;
509 }
510 else /* append */
511 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200512 ts_val = bdp->end_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (!bdp->is_short) /* spaces = padding after block */
514 {
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 Moolenaardac13472019-09-16 21:06:21 +0200517 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 }
520 else /* spaces = padding to block edge */
521 {
522 /* if $ used, just append to EOL (ie spaces==0) */
523 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 Moolenaarb5cf6c32014-08-16 18:36:43 +0200534 /* Avoid starting halfway a multi-byte character. */
535 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
552 /* copy up to shifted part */
553 mch_memmove(newp, oldp, (size_t)(offset));
554 oldp += offset;
555
556 /* 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
559 /* copy the new text */
560 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
561 offset += s_len;
562
563 if (spaces && !bdp->is_short)
564 {
565 /* insert post-padding */
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 /* We're splitting a TAB, don't copy it. */
568 oldp++;
569 /* We allowed for that TAB, remember this now */
570 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 {
581 /* Set "']" mark to the end of the block instead of the end of
582 * the insert in the first line. */
583 curbuf->b_op_end.lnum = oap->end.lnum;
584 curbuf->b_op_end.col = offset;
585 }
586 } /* for all lnum */
587
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 {
605 /* 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. */
608 start = arg;
609 while ((*arg >= ' '
610#ifndef EBCDIC
611 && *arg < DEL /* EBCDIC: chars above space are normal */
612#endif
613 )
614 || (*arg == K_SPECIAL && !literally))
615 ++arg;
616 if (arg > start)
617 stuffReadbuffLen(start, (long)(arg - start));
618
619 /* stuff a single special character */
620 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
649 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
650 return OK;
651
652 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
653 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)
703 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
704 * marks as if it happened. */
705 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 {
720 /* check for read-only register */
721 if (!valid_yank_reg(oap->regname, TRUE))
722 {
723 beep_flush();
724 return OK;
725 }
726 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
727 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
728 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 Moolenaar84298db2012-04-20 13:46:08 +0200746 /* Yank into small delete register when no named register specified
747 * and the delete is within one line. */
748 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
773 msg_silent = 0; /* must display the prompt */
774 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);
801 if (bd.textlen == 0) /* nothing to delete */
802 continue;
803
804 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
805 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;
819 /* copy up to deleted part */
820 mch_memmove(newp, oldp, (size_t)bd.textcol);
821 /* 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));
824 /* copy the part after the deleted part */
825 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000826 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 /* replace the line */
828 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200829
830#ifdef FEAT_TEXT_PROP
831 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);
839 oap->line_count = 0; /* no lines deleted */
840 }
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 {
845 /* 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.
848 */
849 if (oap->line_count > 1)
850 {
851 lnum = curwin->w_cursor.lnum;
852 ++curwin->w_cursor.lnum;
853 del_lines((long)(oap->line_count - 1), TRUE);
854 curwin->w_cursor.lnum = lnum;
855 }
856 if (u_save_cursor() == FAIL)
857 return FAIL;
858 if (curbuf->b_p_ai) /* don't delete indent */
859 {
860 beginline(BL_WHITE); /* cursor on first non-white */
861 did_ai = TRUE; /* delete the indent when ESC hit */
862 ai_col = curwin->w_cursor.col;
863 }
864 else
865 beginline(0); /* cursor in column 0 */
866 truncate_line(FALSE); /* delete the rest of the line */
867 /* leave cursor past last char in line */
868 if (oap->line_count > 1)
869 u_clearline(); /* "U" command not possible after "2cc" */
870 }
871 else
872 {
873 del_lines(oap->line_count, TRUE);
874 beginline(BL_WHITE | BL_FIX);
875 u_clearline(); /* "U" command not possible after "dd" */
876 }
877 }
878 else
879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (virtual_op)
881 {
882 int endcol = 0;
883
884 /* For virtualedit: break the tabs that are partly included. */
885 if (gchar_pos(&oap->start) == '\t')
886 {
887 if (u_save_cursor() == FAIL) /* save first line for undo */
888 return FAIL;
889 if (oap->line_count == 1)
890 endcol = getviscol2(oap->end.col, oap->end.coladd);
891 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
892 oap->start = curwin->w_cursor;
893 if (oap->line_count == 1)
894 {
895 coladvance(endcol);
896 oap->end.col = curwin->w_cursor.col;
897 oap->end.coladd = curwin->w_cursor.coladd;
898 curwin->w_cursor = oap->start;
899 }
900 }
901
902 /* Break a tab only when it's included in the area. */
903 if (gchar_pos(&oap->end) == '\t'
904 && (int)oap->end.coladd < oap->inclusive)
905 {
906 /* save last line for undo */
907 if (u_save((linenr_T)(oap->end.lnum - 1),
908 (linenr_T)(oap->end.lnum + 1)) == FAIL)
909 return FAIL;
910 curwin->w_cursor = oap->end;
911 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
912 oap->end = curwin->w_cursor;
913 curwin->w_cursor = oap->start;
914 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100915 if (has_mbyte)
916 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919 if (oap->line_count == 1) /* delete characters within one line */
920 {
921 if (u_save_cursor() == FAIL) /* save line for undo */
922 return FAIL;
923
924 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100925 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 && oap->op_type == OP_CHANGE
927 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100928 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 display_dollar(oap->end.col - !oap->inclusive);
930
931 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (virtual_op)
934 {
935 /* fix up things for virtualedit-delete:
936 * break the tabs which are going to get in our way
937 */
938 char_u *curline = ml_get_curline();
939 int len = (int)STRLEN(curline);
940
941 if (oap->end.coladd != 0
942 && (int)oap->end.col >= len - 1
943 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
944 n++;
945 /* Delete at least one char (e.g, when on a control char). */
946 if (n == 0 && oap->start.coladd != oap->end.coladd)
947 n = 1;
948
949 /* When deleted a char in the line, reset coladd. */
950 if (gchar_cursor() != NUL)
951 curwin->w_cursor.coladd = 0;
952 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200953 (void)del_bytes((long)n, !virtual_op,
954 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956 else /* delete characters between lines */
957 {
958 pos_T curpos;
959
960 /* save deleted and changed lines for undo */
961 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
962 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
963 return FAIL;
964
965 truncate_line(TRUE); /* delete from cursor to end of line */
966
967 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
968 ++curwin->w_cursor.lnum;
969 del_lines((long)(oap->line_count - 2), FALSE);
970
Bram Moolenaard009e862015-06-09 20:20:03 +0200971 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200972 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200973 curwin->w_cursor.col = 0;
974 (void)del_bytes((long)n, !virtual_op,
975 oap->op_type == OP_DELETE && !oap->is_VIsual);
976 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
977 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979 }
980
981 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
982
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000983setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 curbuf->b_op_end = oap->start;
991 curbuf->b_op_start = oap->start;
992
993 return OK;
994}
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996/*
997 * Adjust end of operating area for ending on a multi-byte character.
998 * Used for deletion.
999 */
1000 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 char_u *p;
1004
1005 if (oap->inclusive)
1006 {
1007 p = ml_get(oap->end.lnum);
1008 oap->end.col += mb_tail_off(p, p + oap->end.col);
1009 }
1010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar630afe82018-06-28 19:26:28 +02001012/*
1013 * Replace the character under the cursor with "c".
1014 * This takes care of multi-byte characters.
1015 */
1016 static void
1017replace_character(int c)
1018{
1019 int n = State;
1020
1021 State = REPLACE;
1022 ins_char(c);
1023 State = n;
1024 /* Backup to the replaced character. */
1025 dec_cursor();
1026}
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028/*
1029 * Replace a whole area with one character.
1030 */
1031 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001032op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 char_u *newp, *oldp;
1037 size_t oldlen;
1038 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001039 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001040 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1043 return OK; /* nothing to do */
1044
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001045 if (c == REPLACE_CR_NCHAR)
1046 {
1047 had_ctrl_v_cr = TRUE;
1048 c = CAR;
1049 }
1050 else if (c == REPLACE_NL_NCHAR)
1051 {
1052 had_ctrl_v_cr = TRUE;
1053 c = NL;
1054 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (has_mbyte)
1057 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 if (u_save((linenr_T)(oap->start.lnum - 1),
1060 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1061 return FAIL;
1062
1063 /*
1064 * block mode replace
1065 */
1066 if (oap->block_mode)
1067 {
1068 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1069 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1070 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001071 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1073 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1074 continue; /* nothing to replace */
1075
1076 /* n == number of extra chars required
1077 * If we split a TAB, it may be replaced by several characters.
1078 * Thus the number of characters may increase!
1079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 /* If the range starts in virtual space, count the initial
1081 * coladd offset as part of "startspaces" */
1082 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 Moolenaar071d4272004-06-13 20:20:40 +00001092 /* allow for pre spaces */
1093 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1094
1095 /* allow for post spp */
1096 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;
1099 /* Figure out how many characters to replace. */
1100 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 Moolenaar071d4272004-06-13 20:20:40 +00001104 /* A double-wide character can be replaced only up to half the
1105 * times. */
1106 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
1116 /* Compute bytes needed, move character count to num_chars. */
1117 num_chars = numc;
1118 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /* oldlen includes textlen, so don't double count */
1120 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));
1128 /* copy up to deleted part */
1129 mch_memmove(newp, oldp, (size_t)bd.textcol);
1130 oldp += bd.textcol + bd.textlen;
1131 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001132 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001134 /* 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 {
1148 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001149 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01001150 /* copy the part after the changed part */
1151 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 Moolenaard9820532013-11-04 01:41:17 +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 }
1161 /* replace the line */
1162 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 {
1195 /* 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 {
1209 /* oap->end has to be recalculated when
1210 * the tab breaks */
1211 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
1229 /* 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. */
1232 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
1245 /* Advance to next character, stop at the end of the file. */
1246 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
1255 /* Set "'[" and "']" marks. */
1256 curbuf->b_op_start = oap->start;
1257 curbuf->b_op_end = oap->end;
1258
1259 return OK;
1260}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001262static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264/*
1265 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1266 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001268op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001272 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
1274 if (u_save((linenr_T)(oap->start.lnum - 1),
1275 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1276 return;
1277
1278 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (oap->block_mode) /* Visual block mode */
1280 {
1281 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1282 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001283 int one_change;
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 block_prep(oap, &bd, pos.lnum, FALSE);
1286 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001287 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1288 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001289
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001290#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001291 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
1293 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1294
Bram Moolenaar009b2592004-10-24 19:18:58 +00001295 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1296 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001298 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302 if (did_change)
1303 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1304 }
1305 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
1307 if (oap->motion_type == MLINE)
1308 {
1309 oap->start.col = 0;
1310 pos.col = 0;
1311 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1312 if (oap->end.col)
1313 --oap->end.col;
1314 }
1315 else if (!oap->inclusive)
1316 dec(&(oap->end));
1317
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001318 if (pos.lnum == oap->end.lnum)
1319 did_change = swapchars(oap->op_type, &pos,
1320 oap->end.col - pos.col + 1);
1321 else
1322 for (;;)
1323 {
1324 did_change |= swapchars(oap->op_type, &pos,
1325 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1326 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001327 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001328 break;
1329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (did_change)
1331 {
1332 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1333 0L);
1334#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001335 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337 char_u *ptr;
1338 int count;
1339
1340 pos = oap->start;
1341 while (pos.lnum < oap->end.lnum)
1342 {
1343 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001344 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001345 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001347 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 pos.col = 0;
1349 pos.lnum++;
1350 }
1351 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1352 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001353 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001355 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357#endif
1358 }
1359 }
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (!did_change && oap->is_VIsual)
1362 /* No change: need to remove the Visual selection */
1363 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 /*
1366 * Set '[ and '] marks.
1367 */
1368 curbuf->b_op_start = oap->start;
1369 curbuf->b_op_end = oap->end;
1370
1371 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001372 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001373 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374}
1375
1376/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001377 * Invoke swapchar() on "length" bytes at position "pos".
1378 * "pos" is advanced to just after the changed characters.
1379 * "length" is rounded up to include the whole last multi-byte character.
1380 * Also works correctly when the number of bytes changes.
1381 * Returns TRUE if some character was changed.
1382 */
1383 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001384swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001385{
1386 int todo;
1387 int did_change = 0;
1388
1389 for (todo = length; todo > 0; --todo)
1390 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001391 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001392 {
1393 int len = (*mb_ptr2len)(ml_get_pos(pos));
1394
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001395 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001396 if (len > 0)
1397 todo -= len - 1;
1398 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001399 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001400 if (inc(pos) == -1) /* at end of file */
1401 break;
1402 }
1403 return did_change;
1404}
1405
1406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * If op_type == OP_UPPER: make uppercase,
1408 * if op_type == OP_LOWER: make lowercase,
1409 * if op_type == OP_ROT13: do rot13 encoding,
1410 * else swap case of character at 'pos'
1411 * returns TRUE when something actually changed.
1412 */
1413 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001414swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 int c;
1417 int nc;
1418
1419 c = gchar_pos(pos);
1420
1421 /* Only do rot13 encoding for ASCII characters. */
1422 if (c >= 0x80 && op_type == OP_ROT13)
1423 return FALSE;
1424
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001425 if (op_type == OP_UPPER && c == 0xdf
1426 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001427 {
1428 pos_T sp = curwin->w_cursor;
1429
1430 /* Special handling of German sharp s: change to "SS". */
1431 curwin->w_cursor = *pos;
1432 del_char(FALSE);
1433 ins_char('S');
1434 ins_char('S');
1435 curwin->w_cursor = sp;
1436 inc(pos);
1437 }
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
1440 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 nc = c;
1442 if (MB_ISLOWER(c))
1443 {
1444 if (op_type == OP_ROT13)
1445 nc = ROT13(c, 'a');
1446 else if (op_type != OP_LOWER)
1447 nc = MB_TOUPPER(c);
1448 }
1449 else if (MB_ISUPPER(c))
1450 {
1451 if (op_type == OP_ROT13)
1452 nc = ROT13(c, 'A');
1453 else if (op_type != OP_UPPER)
1454 nc = MB_TOLOWER(c);
1455 }
1456 if (nc != c)
1457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1459 {
1460 pos_T sp = curwin->w_cursor;
1461
1462 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001463 /* don't use del_char(), it also removes composing chars */
1464 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 ins_char(nc);
1466 curwin->w_cursor = sp;
1467 }
1468 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001469 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 return TRUE;
1471 }
1472 return FALSE;
1473}
1474
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475/*
1476 * op_insert - Insert and append operators for Visual mode.
1477 */
1478 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001479op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 long ins_len, pre_textlen = 0;
1482 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001483 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 struct block_def bd;
1485 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001486 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 /* edit() changes this - record it for OP_APPEND */
1489 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1490
1491 /* vis block is still marked. Get rid of it now. */
1492 curwin->w_cursor.lnum = oap->start.lnum;
1493 update_screen(INVERTED);
1494
1495 if (oap->block_mode)
1496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 /* When 'virtualedit' is used, need to insert the extra spaces before
1498 * doing block_prep(). When only "block" is used, virtual edit is
1499 * already disabled, but still need it when calling
1500 * coladvance_force(). */
1501 if (curwin->w_cursor.coladd > 0)
1502 {
1503 int old_ve_flags = ve_flags;
1504
1505 ve_flags = VE_ALL;
1506 if (u_save_cursor() == FAIL)
1507 return;
1508 coladvance_force(oap->op_type == OP_APPEND
1509 ? oap->end_vcol + 1 : getviscol());
1510 if (oap->op_type == OP_APPEND)
1511 --curwin->w_cursor.col;
1512 ve_flags = old_ve_flags;
1513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /* Get the info about the block before entering the text */
1515 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001516 /* Get indent information */
1517 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (oap->op_type == OP_APPEND)
1521 firstline += bd.textlen;
1522 pre_textlen = (long)STRLEN(firstline);
1523 }
1524
1525 if (oap->op_type == OP_APPEND)
1526 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001527 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 /* Move the cursor to the character right of the block. */
1530 curwin->w_set_curswant = TRUE;
1531 while (*ml_get_cursor() != NUL
1532 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1533 ++curwin->w_cursor.col;
1534 if (bd.is_short && !bd.is_MAX)
1535 {
1536 /* First line was too short, make it longer and adjust the
1537 * values in "bd". */
1538 if (u_save_cursor() == FAIL)
1539 return;
1540 for (i = 0; i < bd.endspaces; ++i)
1541 ins_char(' ');
1542 bd.textlen += bd.endspaces;
1543 }
1544 }
1545 else
1546 {
1547 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001548 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
1550 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001551 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 && oap->start_vcol != oap->end_vcol)
1553 inc_cursor();
1554 }
1555 }
1556
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001557 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001558 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001560 /* When a tab was inserted, and the characters in front of the tab
1561 * have been converted to a tab as well, the column of the cursor
1562 * might have actually been reduced, so need to adjust here. */
1563 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001564 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001565 oap->start = curbuf->b_op_start_orig;
1566
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001567 /* If user has moved off this line, we don't know what to do, so do
1568 * nothing.
1569 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
1570 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 return;
1572
1573 if (oap->block_mode)
1574 {
1575 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001576 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001577 size_t len;
1578 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001580 /* If indent kicked in, the firstline might have changed
1581 * but only do that, if the indent actually increased. */
1582 ind_post = (colnr_T)getwhitecols_curline();
1583 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1584 {
1585 bd.textcol += ind_post - ind_pre;
1586 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001587 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001588 }
1589
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001590 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001591 * to adjust the block for that. But only do it, if the difference
1592 * does not come from indent kicking in. */
1593 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1594 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001595 {
1596 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001597 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001598 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001599 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001600 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001601 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001602 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001603 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001604 pre_textlen -= t - oap->start_vcol;
1605 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001606 }
1607 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001608 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001609 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001610 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001611 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001612 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001613 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001614 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001615 /* reset pre_textlen to the value of OP_INSERT */
1616 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001617 pre_textlen -= t - oap->start_vcol;
1618 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001619 oap->op_type = OP_INSERT;
1620 }
1621 }
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 /*
1624 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001625 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * Don't do this when "$" used, end-of-line will have changed.
1627 */
1628 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1629 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1630 {
1631 if (oap->op_type == OP_APPEND)
1632 {
1633 pre_textlen += bd2.textlen - bd.textlen;
1634 if (bd2.endspaces)
1635 --bd2.textlen;
1636 }
1637 bd.textcol = bd2.textcol;
1638 bd.textlen = bd2.textlen;
1639 }
1640
1641 /*
1642 * Subsequent calls to ml_get() flush the firstline data - take a
1643 * copy of the required string.
1644 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001645 firstline = ml_get(oap->start.lnum);
1646 len = STRLEN(firstline);
1647 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001649 add += bd.textlen;
1650 if ((size_t)add > len)
1651 firstline += len; // short line, point to the NUL
1652 else
1653 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001654 if (pre_textlen >= 0
1655 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 ins_text = vim_strnsave(firstline, (int)ins_len);
1658 if (ins_text != NULL)
1659 {
1660 /* block handled here */
1661 if (u_save(oap->start.lnum,
1662 (linenr_T)(oap->end.lnum + 1)) == OK)
1663 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1664 &bd);
1665
1666 curwin->w_cursor.col = oap->start.col;
1667 check_cursor();
1668 vim_free(ins_text);
1669 }
1670 }
1671 }
1672}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
1674/*
1675 * op_change - handle a change operation
1676 *
1677 * return TRUE if edit() returns because of a CTRL-O command
1678 */
1679 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001680op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 colnr_T l;
1683 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 long offset;
1685 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001686 long ins_len;
1687 long pre_textlen = 0;
1688 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 char_u *firstline;
1690 char_u *ins_text, *newp, *oldp;
1691 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 l = oap->start.col;
1694 if (oap->motion_type == MLINE)
1695 {
1696 l = 0;
1697#ifdef FEAT_SMARTINDENT
1698 if (!p_paste && curbuf->b_p_si
1699# ifdef FEAT_CINDENT
1700 && !curbuf->b_p_cin
1701# endif
1702 )
1703 can_si = TRUE; /* It's like opening a new line, do si */
1704#endif
1705 }
1706
1707 /* First delete the text in the region. In an empty buffer only need to
1708 * save for undo */
1709 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1710 {
1711 if (u_save_cursor() == FAIL)
1712 return FALSE;
1713 }
1714 else if (op_delete(oap) == FAIL)
1715 return FALSE;
1716
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001717 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 && !virtual_op)
1719 inc_cursor();
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 /* check for still on same line (<CR> in inserted text meaningless) */
1722 /* skip blank lines too */
1723 if (oap->block_mode)
1724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 /* Add spaces before getting the current line length. */
1726 if (virtual_op && (curwin->w_cursor.coladd > 0
1727 || gchar_cursor() == NUL))
1728 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001729 firstline = ml_get(oap->start.lnum);
1730 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001731 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 bd.textcol = curwin->w_cursor.col;
1733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
1735#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1736 if (oap->motion_type == MLINE)
1737 fix_indent();
1738#endif
1739
1740 retval = edit(NUL, FALSE, (linenr_T)1);
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001743 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001745 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001747 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00001749 /* Auto-indenting may have changed the indent. If the cursor was past
1750 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001752 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001754 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001755
1756 pre_textlen += new_indent - pre_indent;
1757 bd.textcol += new_indent - pre_indent;
1758 }
1759
1760 ins_len = (long)STRLEN(firstline) - pre_textlen;
1761 if (ins_len > 0)
1762 {
1763 /* Subsequent calls to ml_get() flush the firstline data - take a
1764 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001765 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001767 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1769 linenr++)
1770 {
1771 block_prep(oap, &bd, linenr, TRUE);
1772 if (!bd.is_short || virtual_op)
1773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 pos_T vpos;
1775
1776 /* If the block starts in virtual space, count the
1777 * initial coladd offset as part of "startspaces" */
1778 if (bd.is_short)
1779 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001780 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
1783 else
1784 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001786 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (newp == NULL)
1788 continue;
1789 /* copy up to block start */
1790 mch_memmove(newp, oldp, (size_t)bd.textcol);
1791 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001792 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1795 offset += ins_len;
1796 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001797 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 ml_replace(linenr, newp, FALSE);
1799 }
1800 }
1801 check_cursor();
1802
1803 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1804 }
1805 vim_free(ins_text);
1806 }
1807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 return retval;
1810}
1811
1812/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001813 * When the cursor is on the NUL past the end of the line and it should not be
1814 * there move it left.
1815 */
1816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001817adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001818{
1819 if (curwin->w_cursor.col > 0
1820 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001821 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 && !(restart_edit || (State & INSERT)))
1823 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00001824 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00001825 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001828 {
1829 colnr_T scol, ecol;
1830
1831 /* Coladd is set to the width of the last character. */
1832 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1833 curwin->w_cursor.coladd = ecol - scol + 1;
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836}
1837
Bram Moolenaar81340392012-06-06 16:12:59 +02001838/*
1839 * If "process" is TRUE and the line begins with a comment leader (possibly
1840 * after some white space), return a pointer to the text after it. Put a boolean
1841 * value indicating whether the line ends with an unclosed comment in
1842 * "is_comment".
1843 * line - line to be processed,
1844 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001845 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001846 * include_space - whether to also skip space following the comment leader,
1847 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001848 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001849 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001850 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001851skip_comment(
1852 char_u *line,
1853 int process,
1854 int include_space,
1855 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001856{
1857 char_u *comment_flags = NULL;
1858 int lead_len;
1859 int leader_offset = get_last_leader_offset(line, &comment_flags);
1860
1861 *is_comment = FALSE;
1862 if (leader_offset != -1)
1863 {
1864 /* Let's check whether the line ends with an unclosed comment.
1865 * If the last comment leader has COM_END in flags, there's no comment.
1866 */
1867 while (*comment_flags)
1868 {
1869 if (*comment_flags == COM_END
1870 || *comment_flags == ':')
1871 break;
1872 ++comment_flags;
1873 }
1874 if (*comment_flags != COM_END)
1875 *is_comment = TRUE;
1876 }
1877
1878 if (process == FALSE)
1879 return line;
1880
1881 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1882
1883 if (lead_len == 0)
1884 return line;
1885
1886 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02001887 * - COM_END,
1888 * - colon,
1889 * whichever comes first.
1890 */
1891 while (*comment_flags)
1892 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001893 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001894 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001895 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001896 ++comment_flags;
1897 }
1898
1899 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001900 * starting with a closing part of a three-part comment. That's good,
1901 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001902 */
1903 if (*comment_flags == ':' || *comment_flags == NUL)
1904 line += lead_len;
1905
1906 return line;
1907}
Bram Moolenaar81340392012-06-06 16:12:59 +02001908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001910 * Join 'count' lines (minimal 2) at cursor position.
1911 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001912 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1913 * leaders should not be removed.
1914 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1915 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001917 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 */
1919 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001920do_join(
1921 long count,
1922 int insert_space,
1923 int save_undo,
1924 int use_formatoptions UNUSED,
1925 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001927 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001928 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001929 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001931 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02001932 int endcurr1 = NUL;
1933 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001934 int currsize = 0; /* size of the current line */
1935 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001937 colnr_T col = 0;
1938 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001939 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001940 int remove_comments = (use_formatoptions == TRUE)
1941 && has_format_option(FO_REMOVE_COMS);
1942 int prev_was_comment;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001943#ifdef FEAT_TEXT_PROP
1944 textprop_T **prop_lines = NULL;
1945 int *prop_lengths = NULL;
1946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001948 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1949 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001952 /* Allocate an array to store the number of spaces inserted before each
1953 * line. We will use it to pre-compute the length of the new line and the
1954 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001955 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001956 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001958 if (remove_comments)
1959 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001960 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001961 if (comments == NULL)
1962 {
1963 vim_free(spaces);
1964 return FAIL;
1965 }
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001969 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001970 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001971 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001973 for (t = 0; t < count; ++t)
1974 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001975 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001976 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001977 {
1978 /* Set the '[ mark. */
1979 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1980 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1981 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001982 if (remove_comments)
1983 {
1984 /* We don't want to remove the comment leader if the
1985 * previous line is not a comment. */
1986 if (t > 0 && prev_was_comment)
1987 {
1988
1989 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1990 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001991 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001992 curr = new_curr;
1993 }
1994 else
1995 curr = skip_comment(curr, FALSE, insert_space,
1996 &prev_was_comment);
1997 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001998
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001999 if (insert_space && t > 0)
2000 {
2001 curr = skipwhite(curr);
2002 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002003 && (!has_format_option(FO_MBYTE_JOIN)
2004 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2005 && (!has_format_option(FO_MBYTE_JOIN2)
2006 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002007 )
2008 {
2009 /* don't add a space if the line is ending in a space */
2010 if (endcurr1 == ' ')
2011 endcurr1 = endcurr2;
2012 else
2013 ++spaces[t];
2014 /* extra space when 'joinspaces' set and line ends in '.' */
2015 if ( p_js
2016 && (endcurr1 == '.'
2017 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2018 && (endcurr1 == '?' || endcurr1 == '!'))))
2019 ++spaces[t];
2020 }
2021 }
2022 currsize = (int)STRLEN(curr);
2023 sumsize += currsize + spaces[t];
2024 endcurr1 = endcurr2 = NUL;
2025 if (insert_space && currsize > 0)
2026 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002027 if (has_mbyte)
2028 {
2029 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002030 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002031 endcurr1 = (*mb_ptr2char)(cend);
2032 if (cend > curr)
2033 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002034 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002035 endcurr2 = (*mb_ptr2char)(cend);
2036 }
2037 }
2038 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002039 {
2040 endcurr1 = *(curr + currsize - 1);
2041 if (currsize > 1)
2042 endcurr2 = *(curr + currsize - 2);
2043 }
2044 }
2045 line_breakcheck();
2046 if (got_int)
2047 {
2048 ret = FAIL;
2049 goto theend;
2050 }
2051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002053 /* store the column position before last line */
2054 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002056 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002057 newp = alloc(sumsize + 1);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002058 if (newp == NULL)
2059 {
2060 ret = FAIL;
2061 goto theend;
2062 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002063 cend = newp + sumsize;
2064 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002066#ifdef FEAT_TEXT_PROP
2067 // We need to move properties of the lines that are going to be deleted to
2068 // the new long one.
2069 if (curbuf->b_has_textprop && !text_prop_frozen)
2070 {
2071 // Allocate an array to copy the text properties of joined lines into.
2072 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002073 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
2074 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002075 if (prop_lengths == NULL)
2076 VIM_CLEAR(prop_lines);
2077 }
2078#endif
2079
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002080 /*
2081 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002082 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002083 *
2084 * Move marks from each deleted line to the joined line, adjusting the
2085 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2086 * should not really be a problem.
2087 */
2088 for (t = count - 1; ; --t)
2089 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002090 int spaces_removed;
2091
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002092 cend -= currsize;
2093 mch_memmove(cend, curr, (size_t)currsize);
2094 if (spaces[t] > 0)
2095 {
2096 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002097 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002098 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002099
2100 // If deleting more spaces than adding, the cursor moves no more than
2101 // what is added if it is inside these spaces.
2102 spaces_removed = (curr - curr_start) - spaces[t];
2103
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002105 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002106 if (t == 0)
2107 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002108#ifdef FEAT_TEXT_PROP
2109 if (prop_lines != NULL)
2110 adjust_props_for_join(curwin->w_cursor.lnum + t,
2111 prop_lines + t - 1, prop_lengths + t - 1,
2112 (long)(cend - newp - spaces_removed), spaces_removed);
2113#endif
2114
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002115 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002116 if (remove_comments)
2117 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002118 if (insert_space && t > 1)
2119 curr = skipwhite(curr);
2120 currsize = (int)STRLEN(curr);
2121 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002122
2123#ifdef FEAT_TEXT_PROP
2124 if (prop_lines != NULL)
2125 join_prop_lines(curwin->w_cursor.lnum, newp,
2126 prop_lines, prop_lengths, count);
2127 else
2128#endif
2129 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002131 if (setmark)
2132 {
2133 /* Set the '] mark. */
2134 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002135 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002136 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 /* Only report the change in the first line here, del_lines() will report
2139 * the deleted line. */
2140 changed_lines(curwin->w_cursor.lnum, currsize,
2141 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002143 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 * briefly, and then move it back. After del_lines() the cursor may
2145 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 */
2147 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002149 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 curwin->w_cursor.lnum = t;
2151
2152 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002153 * Set the cursor column:
2154 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002155 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002157 curwin->w_cursor.col =
2158 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 curwin->w_set_curswant = TRUE;
2163
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002164theend:
2165 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002166 if (remove_comments)
2167 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002168 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169}
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171/*
2172 * Return TRUE if the two comment leaders given are the same. "lnum" is
2173 * the first line. White-space is ignored. Note that the whole of
2174 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
2175 */
2176 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002177same_leader(
2178 linenr_T lnum,
2179 int leader1_len,
2180 char_u *leader1_flags,
2181 int leader2_len,
2182 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
2184 int idx1 = 0, idx2 = 0;
2185 char_u *p;
2186 char_u *line1;
2187 char_u *line2;
2188
2189 if (leader1_len == 0)
2190 return (leader2_len == 0);
2191
2192 /*
2193 * If first leader has 'f' flag, the lines can be joined only if the
2194 * second line does not have a leader.
2195 * If first leader has 'e' flag, the lines can never be joined.
2196 * If fist leader has 's' flag, the lines can only be joined if there is
2197 * some text after it and the second line has the 'm' flag.
2198 */
2199 if (leader1_flags != NULL)
2200 {
2201 for (p = leader1_flags; *p && *p != ':'; ++p)
2202 {
2203 if (*p == COM_FIRST)
2204 return (leader2_len == 0);
2205 if (*p == COM_END)
2206 return FALSE;
2207 if (*p == COM_START)
2208 {
2209 if (*(ml_get(lnum) + leader1_len) == NUL)
2210 return FALSE;
2211 if (leader2_flags == NULL || leader2_len == 0)
2212 return FALSE;
2213 for (p = leader2_flags; *p && *p != ':'; ++p)
2214 if (*p == COM_MIDDLE)
2215 return TRUE;
2216 return FALSE;
2217 }
2218 }
2219 }
2220
2221 /*
2222 * Get current line and next line, compare the leaders.
2223 * The first line has to be saved, only one line can be locked at a time.
2224 */
2225 line1 = vim_strsave(ml_get(lnum));
2226 if (line1 != NULL)
2227 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002228 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 ;
2230 line2 = ml_get(lnum + 1);
2231 for (idx2 = 0; idx2 < leader2_len; ++idx2)
2232 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002233 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 if (line1[idx1++] != line2[idx2])
2236 break;
2237 }
2238 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01002239 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ++idx1;
2241 }
2242 vim_free(line1);
2243 }
2244 return (idx2 == leader2_len && idx1 == leader1_len);
2245}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01002248 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002251op_format(
2252 oparg_T *oap,
2253 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 long old_line_count = curbuf->b_ml.ml_line_count;
2256
2257 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
2258 * can put it back there. */
2259 curwin->w_cursor = oap->cursor_start;
2260
2261 if (u_save((linenr_T)(oap->start.lnum - 1),
2262 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2263 return;
2264 curwin->w_cursor = oap->start;
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (oap->is_VIsual)
2267 /* When there is no change: need to remove the Visual selection */
2268 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 /* Set '[ mark at the start of the formatted area */
2271 curbuf->b_op_start = oap->start;
2272
2273 /* For "gw" remember the cursor position and put it back below (adjusted
2274 * for joined and split lines). */
2275 if (keep_cursor)
2276 saved_cursor = oap->cursor_start;
2277
Bram Moolenaar81a82092008-03-12 16:27:00 +00002278 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280 /*
2281 * Leave the cursor at the first non-blank of the last formatted line.
2282 * If the cursor was moved one line back (e.g. with "Q}") go to the next
2283 * line, so "." will do the next lines.
2284 */
2285 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2286 ++curwin->w_cursor.lnum;
2287 beginline(BL_WHITE | BL_FIX);
2288 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
2289 msgmore(old_line_count);
2290
2291 /* put '] mark on the end of the formatted area */
2292 curbuf->b_op_end = curwin->w_cursor;
2293
2294 if (keep_cursor)
2295 {
2296 curwin->w_cursor = saved_cursor;
2297 saved_cursor.lnum = 0;
2298 }
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (oap->is_VIsual)
2301 {
2302 win_T *wp;
2303
2304 FOR_ALL_WINDOWS(wp)
2305 {
2306 if (wp->w_old_cursor_lnum != 0)
2307 {
2308 /* When lines have been inserted or deleted, adjust the end of
2309 * the Visual area to be redrawn. */
2310 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
2311 wp->w_old_cursor_lnum += old_line_count;
2312 else
2313 wp->w_old_visual_lnum += old_line_count;
2314 }
2315 }
2316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317}
2318
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002319#if defined(FEAT_EVAL) || defined(PROTO)
2320/*
2321 * Implementation of the format operator 'gq' for when using 'formatexpr'.
2322 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002324op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002325{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002326 if (oap->is_VIsual)
2327 /* When there is no change: need to remove the Visual selection */
2328 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002329
Bram Moolenaar700303e2010-07-11 17:35:50 +02002330 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
2331 /* As documented: when 'formatexpr' returns non-zero fall back to
2332 * internal formatting. */
2333 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002334}
2335
2336 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002337fex_format(
2338 linenr_T lnum,
2339 long count,
2340 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002341{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002342 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
2343 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002344 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002345 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002346
2347 /*
2348 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002349 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002350 */
2351 set_vim_var_nr(VV_LNUM, lnum);
2352 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00002353 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002354
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002355 /* Make a copy, the option could be changed while calling it. */
2356 fex = vim_strsave(curbuf->b_p_fex);
2357 if (fex == NULL)
2358 return 0;
2359
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002360 /*
2361 * Evaluate the function.
2362 */
2363 if (use_sandbox)
2364 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002365 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002366 if (use_sandbox)
2367 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002368
2369 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002370 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002372 return r;
2373}
2374#endif
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376/*
2377 * Format "line_count" lines, starting at the cursor position.
2378 * When "line_count" is negative, format until the end of the paragraph.
2379 * Lines after the cursor line are saved for undo, caller must have saved the
2380 * first line.
2381 */
2382 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002383format_lines(
2384 linenr_T line_count,
2385 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
2387 int max_len;
2388 int is_not_par; /* current line not part of parag. */
2389 int next_is_not_par; /* next line not part of paragraph */
2390 int is_end_par; /* at end of paragraph */
2391 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
2392 int next_is_start_par = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 int leader_len = 0; /* leader len of current line */
2394 int next_leader_len; /* leader len of next line */
2395 char_u *leader_flags = NULL; /* flags for leader of current line */
2396 char_u *next_leader_flags; /* flags for leader of next line */
2397 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002398 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002400 int second_indent = -1; /* indent for second line (comment
2401 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 int do_second_indent;
2403 int do_number_indent;
2404 int do_trail_white;
2405 int first_par_line = TRUE;
2406 int smd_save;
2407 long count;
2408 int need_set_indent = TRUE; /* set indent of next paragraph */
2409 int force_format = FALSE;
2410 int old_State = State;
2411
2412 /* length of a line to force formatting: 3 * 'tw' */
2413 max_len = comp_textwidth(TRUE) * 3;
2414
2415 /* check for 'q', '2' and '1' in 'formatoptions' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 do_second_indent = has_format_option(FO_Q_SECOND);
2418 do_number_indent = has_format_option(FO_Q_NUMBER);
2419 do_trail_white = has_format_option(FO_WHITE_PAR);
2420
2421 /*
2422 * Get info about the previous and current line.
2423 */
2424 if (curwin->w_cursor.lnum > 1)
2425 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002426 , &leader_len, &leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 else
2428 is_not_par = TRUE;
2429 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002430 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 is_end_par = (is_not_par || next_is_not_par);
2432 if (!is_end_par && do_trail_white)
2433 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
2434
2435 curwin->w_cursor.lnum--;
2436 for (count = line_count; count != 0 && !got_int; --count)
2437 {
2438 /*
2439 * Advance to next paragraph.
2440 */
2441 if (advance)
2442 {
2443 curwin->w_cursor.lnum++;
2444 prev_is_end_par = is_end_par;
2445 is_not_par = next_is_not_par;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 leader_len = next_leader_len;
2447 leader_flags = next_leader_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449
2450 /*
2451 * The last line to be formatted.
2452 */
2453 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2454 {
2455 next_is_not_par = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 next_leader_len = 0;
2457 next_leader_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459 else
2460 {
2461 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002462 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (do_number_indent)
2464 next_is_start_par =
2465 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
2466 }
2467 advance = TRUE;
2468 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
2469 if (!is_end_par && do_trail_white)
2470 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
2471
2472 /*
2473 * Skip lines that are not in a paragraph.
2474 */
2475 if (is_not_par)
2476 {
2477 if (line_count < 0)
2478 break;
2479 }
2480 else
2481 {
2482 /*
2483 * For the first line of a paragraph, check indent of second line.
2484 * Don't do this for comments and empty lines.
2485 */
2486 if (first_par_line
2487 && (do_second_indent || do_number_indent)
2488 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002489 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002491 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002492 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002493 if (leader_len == 0 && next_leader_len == 0)
2494 {
2495 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002496 second_indent =
2497 get_indent_lnum(curwin->w_cursor.lnum + 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002498 }
2499 else
2500 {
2501 second_indent = next_leader_len;
2502 do_comments_list = 1;
2503 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002506 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002507 if (leader_len == 0 && next_leader_len == 0)
2508 {
2509 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002510 second_indent =
2511 get_number_indent(curwin->w_cursor.lnum);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002512 }
2513 else
2514 {
2515 /* get_number_indent() is now "comment aware"... */
2516 second_indent =
2517 get_number_indent(curwin->w_cursor.lnum);
2518 do_comments_list = 1;
2519 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 }
2522
2523 /*
2524 * When the comment leader changes, it's the end of the paragraph.
2525 */
2526 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 || !same_leader(curwin->w_cursor.lnum,
2528 leader_len, leader_flags,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002529 next_leader_len, next_leader_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 is_end_par = TRUE;
2531
2532 /*
2533 * If we have got to the end of a paragraph, or the line is
2534 * getting long, format it.
2535 */
2536 if (is_end_par || force_format)
2537 {
2538 if (need_set_indent)
2539 /* replace indent in first line with minimal number of
2540 * tabs and spaces, according to current options */
2541 (void)set_indent(get_indent(), SIN_CHANGED);
2542
2543 /* put cursor on last non-space */
2544 State = NORMAL; /* don't go past end-of-line */
2545 coladvance((colnr_T)MAXCOL);
2546 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
2547 dec_cursor();
2548
2549 /* do the formatting, without 'showmode' */
2550 State = INSERT; /* for open_line() */
2551 smd_save = p_smd;
2552 p_smd = FALSE;
2553 insertchar(NUL, INSCHAR_FORMAT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002555 + (do_comments && do_comments_list
2556 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar81a82092008-03-12 16:27:00 +00002557 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 State = old_State;
2559 p_smd = smd_save;
2560 second_indent = -1;
2561 /* at end of par.: need to set indent of next par. */
2562 need_set_indent = is_end_par;
2563 if (is_end_par)
2564 {
2565 /* When called with a negative line count, break at the
2566 * end of the paragraph. */
2567 if (line_count < 0)
2568 break;
2569 first_par_line = TRUE;
2570 }
2571 force_format = FALSE;
2572 }
2573
2574 /*
2575 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002576 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 */
2578 if (!is_end_par)
2579 {
2580 advance = FALSE;
2581 curwin->w_cursor.lnum++;
2582 curwin->w_cursor.col = 0;
2583 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002584 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002586 {
2587 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002589 (long)-next_leader_len, 0);
2590 }
2591 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002592 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002593 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002594
2595 if (indent > 0)
2596 {
2597 (void)del_bytes(indent, FALSE, FALSE);
2598 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002599 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01002600 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002603 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
2605 beep_flush();
2606 break;
2607 }
2608 first_par_line = FALSE;
2609 /* If the line is getting long, format it next time */
2610 if (STRLEN(ml_get_curline()) > (size_t)max_len)
2611 force_format = TRUE;
2612 else
2613 force_format = FALSE;
2614 }
2615 }
2616 line_breakcheck();
2617 }
2618}
2619
2620/*
2621 * Return TRUE if line "lnum" ends in a white character.
2622 */
2623 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002624ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 char_u *s = ml_get(lnum);
2627 size_t l;
2628
2629 if (*s == NUL)
2630 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002631 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 * invocation may call function multiple times". */
2633 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002634 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635}
2636
2637/*
2638 * Blank lines, and lines containing only the comment leader, are left
2639 * untouched by the formatting. The function returns TRUE in this
2640 * case. It also returns TRUE when a line starts with the end of a comment
2641 * ('e' in comment flags), so that this line is skipped, and not joined to the
2642 * previous line. A new paragraph starts after a blank line, or when the
2643 * comment leader changes -- webb.
2644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002646fmt_check_par(
2647 linenr_T lnum,
2648 int *leader_len,
2649 char_u **leader_flags,
2650 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 char_u *flags = NULL; /* init for GCC */
2653 char_u *ptr;
2654
2655 ptr = ml_get(lnum);
2656 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002657 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 else
2659 *leader_len = 0;
2660
2661 if (*leader_len > 0)
2662 {
2663 /*
2664 * Search for 'e' flag in comment leader flags.
2665 */
2666 flags = *leader_flags;
2667 while (*flags && *flags != ':' && *flags != COM_END)
2668 ++flags;
2669 }
2670
2671 return (*skipwhite(ptr + *leader_len) == NUL
2672 || (*leader_len > 0 && *flags == COM_END)
2673 || startPS(lnum, NUL, FALSE));
2674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676/*
2677 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
2678 * previous line is in the same paragraph. Used for auto-formatting.
2679 */
2680 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002681paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int leader_len = 0; /* leader len of current line */
2685 char_u *leader_flags = NULL; /* flags for leader of current line */
2686 int next_leader_len; /* leader len of next line */
2687 char_u *next_leader_flags; /* flags for leader of next line */
2688 int do_comments; /* format comments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 if (lnum <= 1)
2691 return TRUE; /* start of the file */
2692
2693 p = ml_get(lnum - 1);
2694 if (*p == NUL)
2695 return TRUE; /* after empty line */
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002698 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return TRUE; /* after non-paragraph line */
2700
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002701 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 return TRUE; /* "lnum" is not a paragraph line */
2703
2704 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
2705 return TRUE; /* missing trailing space in previous line. */
2706
2707 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
2708 return TRUE; /* numbered item starts in "lnum". */
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (!same_leader(lnum - 1, leader_len, leader_flags,
2711 next_leader_len, next_leader_flags))
2712 return TRUE; /* change of comment leader. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 return FALSE;
2715}
2716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717/*
2718 * prepare a few things for block mode yank/delete/tilde
2719 *
2720 * for delete:
2721 * - textlen includes the first/last char to be (partly) deleted
2722 * - start/endspaces is the number of columns that are taken by the
2723 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002724 * deleted.
2725 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 * - textlen includes the first/last char to be wholly yanked
2727 * - start/endspaces is the number of columns of the first/last yanked char
2728 * that are to be yanked.
2729 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002731block_prep(
2732 oparg_T *oap,
2733 struct block_def *bdp,
2734 linenr_T lnum,
2735 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
2737 int incr = 0;
2738 char_u *pend;
2739 char_u *pstart;
2740 char_u *line;
2741 char_u *prev_pstart;
2742 char_u *prev_pend;
2743
2744 bdp->startspaces = 0;
2745 bdp->endspaces = 0;
2746 bdp->textlen = 0;
2747 bdp->start_vcol = 0;
2748 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 bdp->is_short = FALSE;
2750 bdp->is_oneChar = FALSE;
2751 bdp->pre_whitesp = 0;
2752 bdp->pre_whitesp_c = 0;
2753 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 bdp->start_char_vcols = 0;
2755
2756 line = ml_get(lnum);
2757 pstart = line;
2758 prev_pstart = line;
2759 while (bdp->start_vcol < oap->start_vcol && *pstart)
2760 {
2761 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002762 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002764 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
2766 bdp->pre_whitesp += incr;
2767 bdp->pre_whitesp_c++;
2768 }
2769 else
2770 {
2771 bdp->pre_whitesp = 0;
2772 bdp->pre_whitesp_c = 0;
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002775 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 bdp->start_char_vcols = incr;
2778 if (bdp->start_vcol < oap->start_vcol) /* line too short */
2779 {
2780 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (!is_del || oap->op_type == OP_APPEND)
2783 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2784 }
2785 else
2786 {
2787 /* notice: this converts partly selected Multibyte characters to
2788 * spaces, too. */
2789 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2790 if (is_del && bdp->startspaces)
2791 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2792 pend = pstart;
2793 bdp->end_vcol = bdp->start_vcol;
2794 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
2795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (oap->op_type == OP_INSERT)
2798 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2799 else if (oap->op_type == OP_APPEND)
2800 {
2801 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2802 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2803 }
2804 else
2805 {
2806 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2807 if (is_del && oap->op_type != OP_LSHIFT)
2808 {
2809 /* just putting the sum of those two into
2810 * bdp->startspaces doesn't work for Visual replace,
2811 * so we have to split the tab in two */
2812 bdp->startspaces = bdp->start_char_vcols
2813 - (bdp->start_vcol - oap->start_vcol);
2814 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2815 }
2816 }
2817 }
2818 else
2819 {
2820 prev_pend = pend;
2821 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2822 {
2823 /* Count a tab for what it's worth (if list mode not on) */
2824 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002825 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 bdp->end_vcol += incr;
2827 }
2828 if (bdp->end_vcol <= oap->end_vcol
2829 && (!is_del
2830 || oap->op_type == OP_APPEND
2831 || oap->op_type == OP_REPLACE)) /* line too short */
2832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 /* Alternative: include spaces to fill up the block.
2835 * Disadvantage: can lead to trailing spaces when the line is
2836 * short where the text is put */
2837 /* if (!is_del || oap->op_type == OP_APPEND) */
2838 if (oap->op_type == OP_APPEND || virtual_op)
2839 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002840 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else
2842 bdp->endspaces = 0; /* replace doesn't add characters */
2843 }
2844 else if (bdp->end_vcol > oap->end_vcol)
2845 {
2846 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2847 if (!is_del && bdp->endspaces)
2848 {
2849 bdp->endspaces = incr - bdp->endspaces;
2850 if (pend != pstart)
2851 pend = prev_pend;
2852 }
2853 }
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 if (is_del && bdp->startspaces)
2857 pstart = prev_pstart;
2858 bdp->textlen = (int)(pend - pstart);
2859 }
2860 bdp->textcol = (colnr_T) (pstart - line);
2861 bdp->textstart = pstart;
2862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002865 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002868op_addsub(
2869 oparg_T *oap,
2870 linenr_T Prenum1, /* Amount of add/subtract */
2871 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002873 pos_T pos;
2874 struct block_def bd;
2875 int change_cnt = 0;
2876 linenr_T amount = Prenum1;
2877
Bram Moolenaar6b731882018-11-16 20:54:47 +01002878 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002879 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002880 // the call to changed_lines().
2881#ifdef FEAT_FOLDING
2882 disable_fold_update++;
2883#endif
2884
Bram Moolenaard79e5502016-01-10 22:13:02 +01002885 if (!VIsual_active)
2886 {
2887 pos = curwin->w_cursor;
2888 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002889 {
2890#ifdef FEAT_FOLDING
2891 disable_fold_update--;
2892#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002893 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002894 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002895 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002896#ifdef FEAT_FOLDING
2897 disable_fold_update--;
2898#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002899 if (change_cnt)
2900 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2901 }
2902 else
2903 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002904 int one_change;
2905 int length;
2906 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002907
2908 if (u_save((linenr_T)(oap->start.lnum - 1),
2909 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002910 {
2911#ifdef FEAT_FOLDING
2912 disable_fold_update--;
2913#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002914 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002915 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002916
2917 pos = oap->start;
2918 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2919 {
2920 if (oap->block_mode) /* Visual block mode */
2921 {
2922 block_prep(oap, &bd, pos.lnum, FALSE);
2923 pos.col = bd.textcol;
2924 length = bd.textlen;
2925 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002926 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002927 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002928 curwin->w_cursor.col = 0;
2929 pos.col = 0;
2930 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2931 }
2932 else /* oap->motion_type == MCHAR */
2933 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002934 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002935 dec(&(oap->end));
2936 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2937 pos.col = 0;
2938 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002939 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002940 pos.col += oap->start.col;
2941 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002942 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002943 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002944 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002945 length = (int)STRLEN(ml_get(oap->end.lnum));
2946 if (oap->end.col >= length)
2947 oap->end.col = length - 1;
2948 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002949 }
2950 }
2951 one_change = do_addsub(oap->op_type, &pos, length, amount);
2952 if (one_change)
2953 {
2954 /* Remember the start position of the first change. */
2955 if (change_cnt == 0)
2956 startpos = curbuf->b_op_start;
2957 ++change_cnt;
2958 }
2959
2960#ifdef FEAT_NETBEANS_INTG
2961 if (netbeans_active() && one_change)
2962 {
2963 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2964
2965 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2966 netbeans_inserted(curbuf, pos.lnum, pos.col,
2967 &ptr[pos.col], length);
2968 }
2969#endif
2970 if (g_cmd && one_change)
2971 amount += Prenum1;
2972 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002973
2974#ifdef FEAT_FOLDING
2975 disable_fold_update--;
2976#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002977 if (change_cnt)
2978 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2979
2980 if (!change_cnt && oap->is_VIsual)
2981 /* No change: need to remove the Visual selection */
2982 redraw_curbuf_later(INVERTED);
2983
2984 /* Set '[ mark if something changed. Keep the last end
2985 * position from do_addsub(). */
2986 if (change_cnt > 0)
2987 curbuf->b_op_start = startpos;
2988
2989 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002991 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002992 }
2993}
2994
2995/*
2996 * Add or subtract 'Prenum1' from a number in a line
2997 * op_type is OP_NR_ADD or OP_NR_SUB
2998 *
2999 * Returns TRUE if some character was changed.
3000 */
3001 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003002do_addsub(
3003 int op_type,
3004 pos_T *pos,
3005 int length,
3006 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003007{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 int col;
3009 char_u *buf1;
3010 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003011 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003013 uvarnumber_T n;
3014 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 char_u *ptr;
3016 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 int todel;
3018 int dohex;
3019 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003020 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 int doalp;
3022 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003024 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003025 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003026 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02003027 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003028 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003029 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003030 pos_T startpos;
3031 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
Bram Moolenaardac13472019-09-16 21:06:21 +02003033 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
3034 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
3035 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
3036 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaard79e5502016-01-10 22:13:02 +01003038 curwin->w_cursor = *pos;
3039 ptr = ml_get(pos->lnum);
3040 col = pos->col;
3041
3042 if (*ptr == NUL)
3043 goto theend;
3044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 /*
3046 * First check if we are on a hexadecimal number, after the "0x".
3047 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003048 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003050 if (dobin)
3051 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003052 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003053 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003054 if (has_mbyte)
3055 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003056 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003057
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003058 if (dohex)
3059 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003060 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003061 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003062 if (has_mbyte)
3063 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003064 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003065
3066 if ( dobin
3067 && dohex
3068 && ! ((col > 0
3069 && (ptr[col] == 'X'
3070 || ptr[col] == 'x')
3071 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003072 && (!has_mbyte ||
3073 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003074 && vim_isxdigit(ptr[col + 1]))))
3075 {
3076
3077 /* In case of binary/hexadecimal pattern overlap match, rescan */
3078
Bram Moolenaard79e5502016-01-10 22:13:02 +01003079 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003080
3081 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003082 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003083 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003084 if (has_mbyte)
3085 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003086 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003087 }
3088
3089 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003090 && col > 0
3091 && (ptr[col] == 'X'
3092 || ptr[col] == 'x')
3093 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003094 && (!has_mbyte ||
3095 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003096 && vim_isxdigit(ptr[col + 1])) ||
3097 ( dobin
3098 && col > 0
3099 && (ptr[col] == 'B'
3100 || ptr[col] == 'b')
3101 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003102 && (!has_mbyte ||
3103 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003104 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003105 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003106 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003107 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003108 if (has_mbyte)
3109 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003110 }
3111 else
3112 {
3113 /*
3114 * Search forward and then backward to find the start of number.
3115 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003116 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003117
3118 while (ptr[col] != NUL
3119 && !vim_isdigit(ptr[col])
3120 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02003121 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003122
3123 while (col > 0
3124 && vim_isdigit(ptr[col - 1])
3125 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003126 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003127 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003128 if (has_mbyte)
3129 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003130 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003131 }
3132 }
3133
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003134 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003135 {
3136 while (ptr[col] != NUL && length > 0
3137 && !vim_isdigit(ptr[col])
3138 && !(doalp && ASCII_ISALPHA(ptr[col])))
3139 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02003140 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003141
3142 col += mb_len;
3143 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003144 }
3145
3146 if (length == 0)
3147 goto theend;
3148
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003149 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003150 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003151 {
3152 negative = TRUE;
3153 was_positive = FALSE;
3154 }
3155 }
3156
3157 /*
3158 * If a number was found, and saving for undo works, replace the number.
3159 */
3160 firstdigit = ptr[col];
3161 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
3162 {
3163 beep_flush();
3164 goto theend;
3165 }
3166
3167 if (doalp && ASCII_ISALPHA(firstdigit))
3168 {
3169 /* decrement or increment alphabetic character */
3170 if (op_type == OP_NR_SUB)
3171 {
3172 if (CharOrd(firstdigit) < Prenum1)
3173 {
3174 if (isupper(firstdigit))
3175 firstdigit = 'A';
3176 else
3177 firstdigit = 'a';
3178 }
3179 else
3180#ifdef EBCDIC
3181 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
3182#else
3183 firstdigit -= Prenum1;
3184#endif
3185 }
3186 else
3187 {
3188 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
3189 {
3190 if (isupper(firstdigit))
3191 firstdigit = 'Z';
3192 else
3193 firstdigit = 'z';
3194 }
3195 else
3196#ifdef EBCDIC
3197 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
3198#else
3199 firstdigit += Prenum1;
3200#endif
3201 }
3202 curwin->w_cursor.col = col;
3203 if (!did_change)
3204 startpos = curwin->w_cursor;
3205 did_change = TRUE;
3206 (void)del_char(FALSE);
3207 ins_char(firstdigit);
3208 endpos = curwin->w_cursor;
3209 curwin->w_cursor.col = col;
3210 }
3211 else
3212 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003213 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003214 && (!has_mbyte ||
3215 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003216 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003217 {
3218 /* negative number */
3219 --col;
3220 negative = TRUE;
3221 }
3222 /* get the number value (unsigned) */
3223 if (visual && VIsual_mode != 'V')
3224 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
3225 ? (int)STRLEN(ptr) - col
3226 : length);
3227
3228 vim_str2nr(ptr + col, &pre, &length,
3229 0 + (dobin ? STR2NR_BIN : 0)
3230 + (dooct ? STR2NR_OCT : 0)
3231 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003232 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003233
3234 /* ignore leading '-' for hex and octal and bin numbers */
3235 if (pre && negative)
3236 {
3237 ++col;
3238 --length;
3239 negative = FALSE;
3240 }
3241 /* add or subtract */
3242 subtract = FALSE;
3243 if (op_type == OP_NR_SUB)
3244 subtract ^= TRUE;
3245 if (negative)
3246 subtract ^= TRUE;
3247
3248 oldn = n;
3249 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003250 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003251 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003252 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003253 /* handle wraparound for decimal numbers */
3254 if (!pre)
3255 {
3256 if (subtract)
3257 {
3258 if (n > oldn)
3259 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003260 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003261 negative ^= TRUE;
3262 }
3263 }
3264 else
3265 {
3266 /* add */
3267 if (n < oldn)
3268 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003269 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003270 negative ^= TRUE;
3271 }
3272 }
3273 if (n == 0)
3274 negative = FALSE;
3275 }
3276
3277 if (visual && !was_positive && !negative && col > 0)
3278 {
3279 /* need to remove the '-' */
3280 col--;
3281 length++;
3282 }
3283
3284 /*
3285 * Delete the old number.
3286 */
3287 curwin->w_cursor.col = col;
3288 if (!did_change)
3289 startpos = curwin->w_cursor;
3290 did_change = TRUE;
3291 todel = length;
3292 c = gchar_cursor();
3293 /*
3294 * Don't include the '-' in the length, only the length of the
3295 * part after it is kept the same.
3296 */
3297 if (c == '-')
3298 --length;
3299 while (todel-- > 0)
3300 {
3301 if (c < 0x100 && isalpha(c))
3302 {
3303 if (isupper(c))
3304 hexupper = TRUE;
3305 else
3306 hexupper = FALSE;
3307 }
3308 /* del_char() will mark line needing displaying */
3309 (void)del_char(FALSE);
3310 c = gchar_cursor();
3311 }
3312
3313 /*
3314 * Prepare the leading characters in buf1[].
3315 * When there are many leading zeros it could be very long.
3316 * Allocate a bit too much.
3317 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003318 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003319 if (buf1 == NULL)
3320 goto theend;
3321 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003322 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003323 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003324 if (pre)
3325 {
3326 *ptr++ = '0';
3327 --length;
3328 }
3329 if (pre == 'b' || pre == 'B' ||
3330 pre == 'x' || pre == 'X')
3331 {
3332 *ptr++ = pre;
3333 --length;
3334 }
3335
3336 /*
3337 * Put the number characters in buf2[].
3338 */
3339 if (pre == 'b' || pre == 'B')
3340 {
3341 int i;
3342 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003343 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003344
3345 /* leading zeros */
3346 for (bit = bits; bit > 0; bit--)
3347 if ((n >> (bit - 1)) & 0x1) break;
3348
3349 for (i = 0; bit > 0; bit--)
3350 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3351
3352 buf2[i] = '\0';
3353 }
3354 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02003355 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003356 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003357 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02003358 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003359 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003360 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02003361 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003362 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003363 else
Bram Moolenaarea391762018-04-08 13:07:22 +02003364 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003365 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003366 length -= (int)STRLEN(buf2);
3367
3368 /*
3369 * Adjust number of zeros to the new number of digits, so the
3370 * total length of the number remains the same.
3371 * Don't do this when
3372 * the result may look like an octal number.
3373 */
3374 if (firstdigit == '0' && !(dooct && pre == 0))
3375 while (length-- > 0)
3376 *ptr++ = '0';
3377 *ptr = NUL;
3378 STRCAT(buf1, buf2);
3379 ins_str(buf1); /* insert the new number */
3380 vim_free(buf1);
3381 endpos = curwin->w_cursor;
3382 if (did_change && curwin->w_cursor.col)
3383 --curwin->w_cursor.col;
3384 }
3385
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003386 if (did_change)
3387 {
3388 /* set the '[ and '] marks */
3389 curbuf->b_op_start = startpos;
3390 curbuf->b_op_end = endpos;
3391 if (curbuf->b_op_end.col > 0)
3392 --curbuf->b_op_end.col;
3393 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003394
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003395theend:
3396 if (visual)
3397 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003398 else if (did_change)
3399 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003400
Bram Moolenaard79e5502016-01-10 22:13:02 +01003401 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402}
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#if defined(FEAT_CLIPBOARD) || defined(PROTO)
3405/*
3406 * SELECTION / PRIMARY ('*')
3407 *
3408 * Text selection stuff that uses the GUI selection register '*'. When using a
3409 * GUI this may be text from another window, otherwise it is the last text we
3410 * had highlighted with VIsual mode. With mouse support, clicking the middle
3411 * button performs the paste, otherwise you will need to do <"*p>. "
3412 * If not under X, it is synonymous with the clipboard register '+'.
3413 *
3414 * X CLIPBOARD ('+')
3415 *
3416 * Text selection stuff that uses the GUI clipboard register '+'.
3417 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
3418 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
3419 * otherwise you will need to do <"+p>. "
3420 * If not under X, it is synonymous with the selection register '*'.
3421 */
3422
3423/*
3424 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003425 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 * only exist while the owning application exists, so we write to the
3427 * permanent (while X runs) store CUT_BUFFER0.
3428 * Dump the CLIPBOARD selection if we own it (it's logically the more
3429 * 'permanent' of the two), otherwise the PRIMARY one.
3430 * For now, use a hard-coded sanity limit of 1Mb of data.
3431 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003432#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003434x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
3436 Display *dpy;
3437 char_u *str = NULL;
3438 long_u len = 0;
3439 int motion_type = -1;
3440
3441# ifdef FEAT_GUI
3442 if (gui.in_use)
3443 dpy = X_DISPLAY;
3444 else
3445# endif
3446# ifdef FEAT_XCLIPBOARD
3447 dpy = xterm_dpy;
3448# else
3449 return;
3450# endif
3451
3452 /* Get selection to export */
3453 if (clip_plus.owned)
3454 motion_type = clip_convert_selection(&str, &len, &clip_plus);
3455 else if (clip_star.owned)
3456 motion_type = clip_convert_selection(&str, &len, &clip_star);
3457
3458 /* Check it's OK */
3459 if (dpy != NULL && str != NULL && motion_type >= 0
3460 && len < 1024*1024 && len > 0)
3461 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003462 int ok = TRUE;
3463
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003464 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
3465 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
3466 * encoding conversion usually doesn't work, so keep the text as-is.
3467 */
3468 if (has_mbyte)
3469 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003470 vimconv_T vc;
3471
3472 vc.vc_type = CONV_NONE;
3473 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
3474 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003475 int intlen = len;
3476 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003477
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003478 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003479 conv_str = string_convert(&vc, str, &intlen);
3480 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003481 if (conv_str != NULL)
3482 {
3483 vim_free(str);
3484 str = conv_str;
3485 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003486 else
3487 {
3488 ok = FALSE;
3489 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003490 convert_setup(&vc, NULL, NULL);
3491 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003492 else
3493 {
3494 ok = FALSE;
3495 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003496 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003497
3498 /* Do not store the string if conversion failed. Better to use any
3499 * other selection than garbled text. */
3500 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003501 {
3502 XStoreBuffer(dpy, (char *)str, (int)len, 0);
3503 XFlush(dpy);
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506
3507 vim_free(str);
3508}
3509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#endif /* FEAT_CLIPBOARD || PROTO */
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003513clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 vim_memset(oap, 0, sizeof(oparg_T));
3516}
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003519 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 *
3521 * "Words" are counted by looking for boundaries between non-space and
3522 * space characters. (it seems to produce results that match 'wc'.)
3523 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003524 * Return value is byte count; word count for the line is added to "*wc".
3525 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 *
3527 * The function will only examine the first "limit" characters in the
3528 * line, stopping if it encounters an end-of-line (NUL byte). In that
3529 * case, eol_size will be added to the character count to account for
3530 * the size of the EOL character.
3531 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003532 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003533line_count_info(
3534 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003535 varnumber_T *wc,
3536 varnumber_T *cc,
3537 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003538 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003540 varnumber_T i;
3541 varnumber_T words = 0;
3542 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 int is_word = 0;
3544
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003545 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
3547 if (is_word)
3548 {
3549 if (vim_isspace(line[i]))
3550 {
3551 words++;
3552 is_word = 0;
3553 }
3554 }
3555 else if (!vim_isspace(line[i]))
3556 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003557 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003558 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560
3561 if (is_word)
3562 words++;
3563 *wc += words;
3564
3565 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003566 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003569 chars += eol_size;
3570 }
3571 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 return i;
3573}
3574
3575/*
3576 * Give some info about the position of the cursor (for "g CTRL-G").
3577 * In Visual mode, give some info about the selected region. (In this case,
3578 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003579 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 */
3581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003582cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
3584 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003585 char_u buf1[50];
3586 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003588 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003589 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003590 varnumber_T byte_count_cursor = 0;
3591 varnumber_T char_count = 0;
3592 varnumber_T char_count_cursor = 0;
3593 varnumber_T word_count = 0;
3594 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003595 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003596 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 long line_count_selected = 0;
3598 pos_T min_pos, max_pos;
3599 oparg_T oparg;
3600 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 /*
3603 * Compute the length of the file in characters.
3604 */
3605 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3606 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003607 if (dict == NULL)
3608 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003609 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003610 return;
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 else
3614 {
3615 if (get_fileformat(curbuf) == EOL_DOS)
3616 eol_size = 2;
3617 else
3618 eol_size = 1;
3619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 if (VIsual_active)
3621 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003622 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 min_pos = VIsual;
3625 max_pos = curwin->w_cursor;
3626 }
3627 else
3628 {
3629 min_pos = curwin->w_cursor;
3630 max_pos = VIsual;
3631 }
3632 if (*p_sel == 'e' && max_pos.col > 0)
3633 --max_pos.col;
3634
3635 if (VIsual_mode == Ctrl_V)
3636 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003637#ifdef FEAT_LINEBREAK
3638 char_u * saved_sbr = p_sbr;
3639
3640 /* Make 'sbr' empty for a moment to get the correct size. */
3641 p_sbr = empty_option;
3642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 oparg.is_VIsual = 1;
3644 oparg.block_mode = TRUE;
3645 oparg.op_type = OP_NOP;
3646 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003647 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003648#ifdef FEAT_LINEBREAK
3649 p_sbr = saved_sbr;
3650#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003651 if (curwin->w_curswant == MAXCOL)
3652 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 /* Swap the start, end vcol if needed */
3654 if (oparg.end_vcol < oparg.start_vcol)
3655 {
3656 oparg.end_vcol += oparg.start_vcol;
3657 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3658 oparg.end_vcol -= oparg.start_vcol;
3659 }
3660 }
3661 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
3664 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3665 {
3666 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003667 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 ui_breakcheck();
3670 if (got_int)
3671 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003672 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 /* Do extra processing for VIsual mode. */
3676 if (VIsual_active
3677 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3678 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003679 char_u *s = NULL;
3680 long len = 0L;
3681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 switch (VIsual_mode)
3683 {
3684 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003688 s = bd.textstart;
3689 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 break;
3691 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003692 s = ml_get(lnum);
3693 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 break;
3695 case 'v':
3696 {
3697 colnr_T start_col = (lnum == min_pos.lnum)
3698 ? min_pos.col : 0;
3699 colnr_T end_col = (lnum == max_pos.lnum)
3700 ? max_pos.col - start_col + 1 : MAXCOL;
3701
Bram Moolenaardef9e822004-12-31 20:58:58 +00003702 s = ml_get(lnum) + start_col;
3703 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705 break;
3706 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003707 if (s != NULL)
3708 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003709 byte_count_cursor += line_count_info(s, &word_count_cursor,
3710 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003711 if (lnum == curbuf->b_ml.ml_line_count
3712 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003713 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003714 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003715 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
3720 /* In non-visual mode, check for the line the cursor is on */
3721 if (lnum == curwin->w_cursor.lnum)
3722 {
3723 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003724 char_count_cursor += char_count;
3725 byte_count_cursor = byte_count +
3726 line_count_info(ml_get(lnum),
3727 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003728 (varnumber_T)(curwin->w_cursor.col + 1),
3729 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731 }
3732 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003733 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003734 &char_count, (varnumber_T)MAXCOL,
3735 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
3738 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003739 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003740 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaared767a22016-01-03 22:49:16 +01003742 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003744 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003746 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3747 {
3748 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3749 &max_pos.col);
3750 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3751 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3752 }
3753 else
3754 buf1[0] = NUL;
3755
3756 if (char_count_cursor == byte_count_cursor
3757 && char_count == byte_count)
3758 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003759 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003760 buf1, line_count_selected,
3761 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003762 (long_long_T)word_count_cursor,
3763 (long_long_T)word_count,
3764 (long_long_T)byte_count_cursor,
3765 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003766 else
3767 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003768 _("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 +01003769 buf1, line_count_selected,
3770 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003771 (long_long_T)word_count_cursor,
3772 (long_long_T)word_count,
3773 (long_long_T)char_count_cursor,
3774 (long_long_T)char_count,
3775 (long_long_T)byte_count_cursor,
3776 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003779 {
3780 p = ml_get_curline();
3781 validate_virtcol();
3782 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3783 (int)curwin->w_virtcol + 1);
3784 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3785 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaared767a22016-01-03 22:49:16 +01003787 if (char_count_cursor == byte_count_cursor
3788 && char_count == byte_count)
3789 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003790 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003791 (char *)buf1, (char *)buf2,
3792 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003794 (long_long_T)word_count_cursor, (long_long_T)word_count,
3795 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003796 else
3797 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003798 _("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 +01003799 (char *)buf1, (char *)buf2,
3800 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003801 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003802 (long_long_T)word_count_cursor, (long_long_T)word_count,
3803 (long_long_T)char_count_cursor, (long_long_T)char_count,
3804 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003805 }
3806 }
3807
Bram Moolenaared767a22016-01-03 22:49:16 +01003808 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003809 if (dict == NULL && bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01003810 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003811 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003812 if (dict == NULL)
3813 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003814 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003815 p = p_shm;
3816 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003817 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003818 p_shm = p;
3819 }
3820 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003821#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003822 if (dict != NULL)
3823 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003824 dict_add_number(dict, "words", word_count);
3825 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003826 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003827 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3828 byte_count_cursor);
3829 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3830 char_count_cursor);
3831 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3832 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003836
3837/*
3838 * Handle indent and format operators and visual mode ":".
3839 */
3840 static void
3841op_colon(oparg_T *oap)
3842{
3843 stuffcharReadbuff(':');
3844 if (oap->is_VIsual)
3845 stuffReadbuff((char_u *)"'<,'>");
3846 else
3847 {
3848 // Make the range look nice, so it can be repeated.
3849 if (oap->start.lnum == curwin->w_cursor.lnum)
3850 stuffcharReadbuff('.');
3851 else
3852 stuffnumReadbuff((long)oap->start.lnum);
3853 if (oap->end.lnum != oap->start.lnum)
3854 {
3855 stuffcharReadbuff(',');
3856 if (oap->end.lnum == curwin->w_cursor.lnum)
3857 stuffcharReadbuff('.');
3858 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3859 stuffcharReadbuff('$');
3860 else if (oap->start.lnum == curwin->w_cursor.lnum)
3861 {
3862 stuffReadbuff((char_u *)".+");
3863 stuffnumReadbuff((long)oap->line_count - 1);
3864 }
3865 else
3866 stuffnumReadbuff((long)oap->end.lnum);
3867 }
3868 }
3869 if (oap->op_type != OP_COLON)
3870 stuffReadbuff((char_u *)"!");
3871 if (oap->op_type == OP_INDENT)
3872 {
3873#ifndef FEAT_CINDENT
3874 if (*get_equalprg() == NUL)
3875 stuffReadbuff((char_u *)"indent");
3876 else
3877#endif
3878 stuffReadbuff(get_equalprg());
3879 stuffReadbuff((char_u *)"\n");
3880 }
3881 else if (oap->op_type == OP_FORMAT)
3882 {
3883 if (*curbuf->b_p_fp != NUL)
3884 stuffReadbuff(curbuf->b_p_fp);
3885 else if (*p_fp != NUL)
3886 stuffReadbuff(p_fp);
3887 else
3888 stuffReadbuff((char_u *)"fmt");
3889 stuffReadbuff((char_u *)"\n']");
3890 }
3891
3892 // do_cmdline() does the rest
3893}
3894
3895/*
3896 * Handle the "g@" operator: call 'operatorfunc'.
3897 */
3898 static void
3899op_function(oparg_T *oap UNUSED)
3900{
3901#ifdef FEAT_EVAL
3902 typval_T argv[2];
3903 int save_virtual_op = virtual_op;
3904
3905 if (*p_opfunc == NUL)
3906 emsg(_("E774: 'operatorfunc' is empty"));
3907 else
3908 {
3909 // Set '[ and '] marks to text to be operated on.
3910 curbuf->b_op_start = oap->start;
3911 curbuf->b_op_end = oap->end;
3912 if (oap->motion_type != MLINE && !oap->inclusive)
3913 // Exclude the end position.
3914 decl(&curbuf->b_op_end);
3915
3916 argv[0].v_type = VAR_STRING;
3917 if (oap->block_mode)
3918 argv[0].vval.v_string = (char_u *)"block";
3919 else if (oap->motion_type == MLINE)
3920 argv[0].vval.v_string = (char_u *)"line";
3921 else
3922 argv[0].vval.v_string = (char_u *)"char";
3923 argv[1].v_type = VAR_UNKNOWN;
3924
3925 // Reset virtual_op so that 'virtualedit' can be changed in the
3926 // function.
3927 virtual_op = MAYBE;
3928
3929 (void)call_func_retnr(p_opfunc, 1, argv);
3930
3931 virtual_op = save_virtual_op;
3932 }
3933#else
3934 emsg(_("E775: Eval feature not available"));
3935#endif
3936}
3937
3938/*
3939 * Calculate start/end virtual columns for operating in block mode.
3940 */
3941 static void
3942get_op_vcol(
3943 oparg_T *oap,
3944 colnr_T redo_VIsual_vcol,
3945 int initial) // when TRUE adjust position for 'selectmode'
3946{
3947 colnr_T start, end;
3948
3949 if (VIsual_mode != Ctrl_V
3950 || (!initial && oap->end.col < curwin->w_width))
3951 return;
3952
3953 oap->block_mode = TRUE;
3954
3955 // prevent from moving onto a trail byte
3956 if (has_mbyte)
3957 mb_adjustpos(curwin->w_buffer, &oap->end);
3958
3959 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3960
3961 if (!redo_VIsual_busy)
3962 {
3963 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3964
3965 if (start < oap->start_vcol)
3966 oap->start_vcol = start;
3967 if (end > oap->end_vcol)
3968 {
3969 if (initial && *p_sel == 'e' && start >= 1
3970 && start - 1 >= oap->end_vcol)
3971 oap->end_vcol = start - 1;
3972 else
3973 oap->end_vcol = end;
3974 }
3975 }
3976
3977 // if '$' was used, get oap->end_vcol from longest line
3978 if (curwin->w_curswant == MAXCOL)
3979 {
3980 curwin->w_cursor.col = MAXCOL;
3981 oap->end_vcol = 0;
3982 for (curwin->w_cursor.lnum = oap->start.lnum;
3983 curwin->w_cursor.lnum <= oap->end.lnum;
3984 ++curwin->w_cursor.lnum)
3985 {
3986 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3987 if (end > oap->end_vcol)
3988 oap->end_vcol = end;
3989 }
3990 }
3991 else if (redo_VIsual_busy)
3992 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3993 // Correct oap->end.col and oap->start.col to be the
3994 // upper-left and lower-right corner of the block area.
3995 //
3996 // (Actually, this does convert column positions into character
3997 // positions)
3998 curwin->w_cursor.lnum = oap->end.lnum;
3999 coladvance(oap->end_vcol);
4000 oap->end = curwin->w_cursor;
4001
4002 curwin->w_cursor = oap->start;
4003 coladvance(oap->start_vcol);
4004 oap->start = curwin->w_cursor;
4005}
4006
4007/*
4008 * Handle an operator after Visual mode or when the movement is finished.
4009 * "gui_yank" is true when yanking text for the clipboard.
4010 */
4011 void
4012do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
4013{
4014 oparg_T *oap = cap->oap;
4015 pos_T old_cursor;
4016 int empty_region_error;
4017 int restart_edit_save;
4018#ifdef FEAT_LINEBREAK
4019 int lbr_saved = curwin->w_p_lbr;
4020#endif
4021
4022 // The visual area is remembered for redo
4023 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
4024 static linenr_T redo_VIsual_line_count; // number of lines
4025 static colnr_T redo_VIsual_vcol; // number of cols or end column
4026 static long redo_VIsual_count; // count for Visual operator
4027 static int redo_VIsual_arg; // extra argument
4028 int include_line_break = FALSE;
4029
4030#if defined(FEAT_CLIPBOARD)
4031 // Yank the visual area into the GUI selection register before we operate
4032 // on it and lose it forever.
4033 // Don't do it if a specific register was specified, so that ""x"*P works.
4034 // This could call do_pending_operator() recursively, but that's OK
4035 // because gui_yank will be TRUE for the nested call.
4036 if ((clip_star.available || clip_plus.available)
4037 && oap->op_type != OP_NOP
4038 && !gui_yank
4039 && VIsual_active
4040 && !redo_VIsual_busy
4041 && oap->regname == 0)
4042 clip_auto_select();
4043#endif
4044 old_cursor = curwin->w_cursor;
4045
4046 // If an operation is pending, handle it...
4047 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
4048 {
4049 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
4050 // for the clipboard.
4051 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
4052
4053#ifdef FEAT_LINEBREAK
4054 // Avoid a problem with unwanted linebreaks in block mode.
4055 if (curwin->w_p_lbr)
4056 curwin->w_valid &= ~VALID_VIRTCOL;
4057 curwin->w_p_lbr = FALSE;
4058#endif
4059 oap->is_VIsual = VIsual_active;
4060 if (oap->motion_force == 'V')
4061 oap->motion_type = MLINE;
4062 else if (oap->motion_force == 'v')
4063 {
4064 // If the motion was linewise, "inclusive" will not have been set.
4065 // Use "exclusive" to be consistent. Makes "dvj" work nice.
4066 if (oap->motion_type == MLINE)
4067 oap->inclusive = FALSE;
4068 // If the motion already was characterwise, toggle "inclusive"
4069 else if (oap->motion_type == MCHAR)
4070 oap->inclusive = !oap->inclusive;
4071 oap->motion_type = MCHAR;
4072 }
4073 else if (oap->motion_force == Ctrl_V)
4074 {
4075 // Change line- or characterwise motion into Visual block mode.
4076 if (!VIsual_active)
4077 {
4078 VIsual_active = TRUE;
4079 VIsual = oap->start;
4080 }
4081 VIsual_mode = Ctrl_V;
4082 VIsual_select = FALSE;
4083 VIsual_reselect = FALSE;
4084 }
4085
4086 // Only redo yank when 'y' flag is in 'cpoptions'.
4087 // Never redo "zf" (define fold).
4088 if ((redo_yank || oap->op_type != OP_YANK)
4089 && ((!VIsual_active || oap->motion_force)
4090 // Also redo Operator-pending Visual mode mappings
4091 || (VIsual_active && cap->cmdchar == ':'
4092 && oap->op_type != OP_COLON))
4093 && cap->cmdchar != 'D'
4094#ifdef FEAT_FOLDING
4095 && oap->op_type != OP_FOLD
4096 && oap->op_type != OP_FOLDOPEN
4097 && oap->op_type != OP_FOLDOPENREC
4098 && oap->op_type != OP_FOLDCLOSE
4099 && oap->op_type != OP_FOLDCLOSEREC
4100 && oap->op_type != OP_FOLDDEL
4101 && oap->op_type != OP_FOLDDELREC
4102#endif
4103 )
4104 {
4105 prep_redo(oap->regname, cap->count0,
4106 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
4107 oap->motion_force, cap->cmdchar, cap->nchar);
4108 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
4109 {
4110 // If 'cpoptions' does not contain 'r', insert the search
4111 // pattern to really repeat the same command.
4112 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
4113 AppendToRedobuffLit(cap->searchbuf, -1);
4114 AppendToRedobuff(NL_STR);
4115 }
4116 else if (cap->cmdchar == ':')
4117 {
4118 // do_cmdline() has stored the first typed line in
4119 // "repeat_cmdline". When several lines are typed repeating
4120 // won't be possible.
4121 if (repeat_cmdline == NULL)
4122 ResetRedobuff();
4123 else
4124 {
4125 AppendToRedobuffLit(repeat_cmdline, -1);
4126 AppendToRedobuff(NL_STR);
4127 VIM_CLEAR(repeat_cmdline);
4128 }
4129 }
4130 }
4131
4132 if (redo_VIsual_busy)
4133 {
4134 // Redo of an operation on a Visual area. Use the same size from
4135 // redo_VIsual_line_count and redo_VIsual_vcol.
4136 oap->start = curwin->w_cursor;
4137 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
4138 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4139 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4140 VIsual_mode = redo_VIsual_mode;
4141 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
4142 {
4143 if (VIsual_mode == 'v')
4144 {
4145 if (redo_VIsual_line_count <= 1)
4146 {
4147 validate_virtcol();
4148 curwin->w_curswant =
4149 curwin->w_virtcol + redo_VIsual_vcol - 1;
4150 }
4151 else
4152 curwin->w_curswant = redo_VIsual_vcol;
4153 }
4154 else
4155 {
4156 curwin->w_curswant = MAXCOL;
4157 }
4158 coladvance(curwin->w_curswant);
4159 }
4160 cap->count0 = redo_VIsual_count;
4161 if (redo_VIsual_count != 0)
4162 cap->count1 = redo_VIsual_count;
4163 else
4164 cap->count1 = 1;
4165 }
4166 else if (VIsual_active)
4167 {
4168 if (!gui_yank)
4169 {
4170 // Save the current VIsual area for '< and '> marks, and "gv"
4171 curbuf->b_visual.vi_start = VIsual;
4172 curbuf->b_visual.vi_end = curwin->w_cursor;
4173 curbuf->b_visual.vi_mode = VIsual_mode;
4174 restore_visual_mode();
4175 curbuf->b_visual.vi_curswant = curwin->w_curswant;
4176# ifdef FEAT_EVAL
4177 curbuf->b_visual_mode_eval = VIsual_mode;
4178# endif
4179 }
4180
4181 // In Select mode, a linewise selection is operated upon like a
4182 // characterwise selection.
4183 // Special case: gH<Del> deletes the last line.
4184 if (VIsual_select && VIsual_mode == 'V'
4185 && cap->oap->op_type != OP_DELETE)
4186 {
4187 if (LT_POS(VIsual, curwin->w_cursor))
4188 {
4189 VIsual.col = 0;
4190 curwin->w_cursor.col =
4191 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
4192 }
4193 else
4194 {
4195 curwin->w_cursor.col = 0;
4196 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
4197 }
4198 VIsual_mode = 'v';
4199 }
4200 // If 'selection' is "exclusive", backup one character for
4201 // charwise selections.
4202 else if (VIsual_mode == 'v')
4203 include_line_break = unadjust_for_sel();
4204
4205 oap->start = VIsual;
4206 if (VIsual_mode == 'V')
4207 {
4208 oap->start.col = 0;
4209 oap->start.coladd = 0;
4210 }
4211 }
4212
4213 // Set oap->start to the first position of the operated text, oap->end
4214 // to the end of the operated text. w_cursor is equal to oap->start.
4215 if (LT_POS(oap->start, curwin->w_cursor))
4216 {
4217#ifdef FEAT_FOLDING
4218 // Include folded lines completely.
4219 if (!VIsual_active)
4220 {
4221 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
4222 oap->start.col = 0;
4223 if ((curwin->w_cursor.col > 0 || oap->inclusive)
4224 && hasFolding(curwin->w_cursor.lnum, NULL,
4225 &curwin->w_cursor.lnum))
4226 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
4227 }
4228#endif
4229 oap->end = curwin->w_cursor;
4230 curwin->w_cursor = oap->start;
4231
4232 // w_virtcol may have been updated; if the cursor goes back to its
4233 // previous position w_virtcol becomes invalid and isn't updated
4234 // automatically.
4235 curwin->w_valid &= ~VALID_VIRTCOL;
4236 }
4237 else
4238 {
4239#ifdef FEAT_FOLDING
4240 // Include folded lines completely.
4241 if (!VIsual_active && oap->motion_type == MLINE)
4242 {
4243 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
4244 NULL))
4245 curwin->w_cursor.col = 0;
4246 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
4247 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
4248 }
4249#endif
4250 oap->end = oap->start;
4251 oap->start = curwin->w_cursor;
4252 }
4253
4254 // Just in case lines were deleted that make the position invalid.
4255 check_pos(curwin->w_buffer, &oap->end);
4256 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
4257
4258 // Set "virtual_op" before resetting VIsual_active.
4259 virtual_op = virtual_active();
4260
4261 if (VIsual_active || redo_VIsual_busy)
4262 {
4263 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
4264
4265 if (!redo_VIsual_busy && !gui_yank)
4266 {
4267 // Prepare to reselect and redo Visual: this is based on the
4268 // size of the Visual text
4269 resel_VIsual_mode = VIsual_mode;
4270 if (curwin->w_curswant == MAXCOL)
4271 resel_VIsual_vcol = MAXCOL;
4272 else
4273 {
4274 if (VIsual_mode != Ctrl_V)
4275 getvvcol(curwin, &(oap->end),
4276 NULL, NULL, &oap->end_vcol);
4277 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
4278 {
4279 if (VIsual_mode != Ctrl_V)
4280 getvvcol(curwin, &(oap->start),
4281 &oap->start_vcol, NULL, NULL);
4282 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
4283 }
4284 else
4285 resel_VIsual_vcol = oap->end_vcol;
4286 }
4287 resel_VIsual_line_count = oap->line_count;
4288 }
4289
4290 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
4291 if ((redo_yank || oap->op_type != OP_YANK)
4292 && oap->op_type != OP_COLON
4293#ifdef FEAT_FOLDING
4294 && oap->op_type != OP_FOLD
4295 && oap->op_type != OP_FOLDOPEN
4296 && oap->op_type != OP_FOLDOPENREC
4297 && oap->op_type != OP_FOLDCLOSE
4298 && oap->op_type != OP_FOLDCLOSEREC
4299 && oap->op_type != OP_FOLDDEL
4300 && oap->op_type != OP_FOLDDELREC
4301#endif
4302 && oap->motion_force == NUL
4303 )
4304 {
4305 // Prepare for redoing. Only use the nchar field for "r",
4306 // otherwise it might be the second char of the operator.
4307 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4308 || cap->nchar == 'N'))
4309 prep_redo(oap->regname, cap->count0,
4310 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
4311 oap->motion_force, cap->cmdchar, cap->nchar);
4312 else if (cap->cmdchar != ':')
4313 {
4314 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4315
4316 // reverse what nv_replace() did
4317 if (nchar == REPLACE_CR_NCHAR)
4318 nchar = CAR;
4319 else if (nchar == REPLACE_NL_NCHAR)
4320 nchar = NL;
4321 prep_redo(oap->regname, 0L, NUL, 'v',
4322 get_op_char(oap->op_type),
4323 get_extra_op_char(oap->op_type),
4324 nchar);
4325 }
4326 if (!redo_VIsual_busy)
4327 {
4328 redo_VIsual_mode = resel_VIsual_mode;
4329 redo_VIsual_vcol = resel_VIsual_vcol;
4330 redo_VIsual_line_count = resel_VIsual_line_count;
4331 redo_VIsual_count = cap->count0;
4332 redo_VIsual_arg = cap->arg;
4333 }
4334 }
4335
4336 // oap->inclusive defaults to TRUE.
4337 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4338 // FALSE. This makes "d}P" and "v}dP" work the same.
4339 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4340 oap->inclusive = TRUE;
4341 if (VIsual_mode == 'V')
4342 oap->motion_type = MLINE;
4343 else
4344 {
4345 oap->motion_type = MCHAR;
4346 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4347 && (include_line_break || !virtual_op))
4348 {
4349 oap->inclusive = FALSE;
4350 // Try to include the newline, unless it's an operator
4351 // that works on lines only.
4352 if (*p_sel != 'o'
4353 && !op_on_lines(oap->op_type)
4354 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4355 {
4356 ++oap->end.lnum;
4357 oap->end.col = 0;
4358 oap->end.coladd = 0;
4359 ++oap->line_count;
4360 }
4361 }
4362 }
4363
4364 redo_VIsual_busy = FALSE;
4365
4366 // Switch Visual off now, so screen updating does
4367 // not show inverted text when the screen is redrawn.
4368 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4369 // no screen redraw, so it is done here to remove the inverted
4370 // part.
4371 if (!gui_yank)
4372 {
4373 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004374 setmouse();
4375 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004376 may_clear_cmdline();
4377 if ((oap->op_type == OP_YANK
4378 || oap->op_type == OP_COLON
4379 || oap->op_type == OP_FUNCTION
4380 || oap->op_type == OP_FILTER)
4381 && oap->motion_force == NUL)
4382 {
4383#ifdef FEAT_LINEBREAK
4384 // make sure redrawing is correct
4385 curwin->w_p_lbr = lbr_saved;
4386#endif
4387 redraw_curbuf_later(INVERTED);
4388 }
4389 }
4390 }
4391
4392 // Include the trailing byte of a multi-byte char.
4393 if (has_mbyte && oap->inclusive)
4394 {
4395 int l;
4396
4397 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4398 if (l > 1)
4399 oap->end.col += l - 1;
4400 }
4401 curwin->w_set_curswant = TRUE;
4402
4403 // oap->empty is set when start and end are the same. The inclusive
4404 // flag affects this too, unless yanking and the end is on a NUL.
4405 oap->empty = (oap->motion_type == MCHAR
4406 && (!oap->inclusive
4407 || (oap->op_type == OP_YANK
4408 && gchar_pos(&oap->end) == NUL))
4409 && EQUAL_POS(oap->start, oap->end)
4410 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4411 // For delete, change and yank, it's an error to operate on an
4412 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4413 empty_region_error = (oap->empty
4414 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4415
4416 // Force a redraw when operating on an empty Visual region, when
4417 // 'modifiable is off or creating a fold.
4418 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4419#ifdef FEAT_FOLDING
4420 || oap->op_type == OP_FOLD
4421#endif
4422 ))
4423 {
4424#ifdef FEAT_LINEBREAK
4425 curwin->w_p_lbr = lbr_saved;
4426#endif
4427 redraw_curbuf_later(INVERTED);
4428 }
4429
4430 // If the end of an operator is in column one while oap->motion_type
4431 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4432 // character in the previous line. If op_start is on or before the
4433 // first non-blank in the line, the operator becomes linewise
4434 // (strange, but that's the way vi does it).
4435 if ( oap->motion_type == MCHAR
4436 && oap->inclusive == FALSE
4437 && !(cap->retval & CA_NO_ADJ_OP_END)
4438 && oap->end.col == 0
4439 && (!oap->is_VIsual || *p_sel == 'o')
4440 && !oap->block_mode
4441 && oap->line_count > 1)
4442 {
4443 oap->end_adjusted = TRUE; // remember that we did this
4444 --oap->line_count;
4445 --oap->end.lnum;
4446 if (inindent(0))
4447 oap->motion_type = MLINE;
4448 else
4449 {
4450 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
4451 if (oap->end.col)
4452 {
4453 --oap->end.col;
4454 oap->inclusive = TRUE;
4455 }
4456 }
4457 }
4458 else
4459 oap->end_adjusted = FALSE;
4460
4461 switch (oap->op_type)
4462 {
4463 case OP_LSHIFT:
4464 case OP_RSHIFT:
4465 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4466 auto_format(FALSE, TRUE);
4467 break;
4468
4469 case OP_JOIN_NS:
4470 case OP_JOIN:
4471 if (oap->line_count < 2)
4472 oap->line_count = 2;
4473 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4474 curbuf->b_ml.ml_line_count)
4475 beep_flush();
4476 else
4477 {
4478 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4479 TRUE, TRUE, TRUE);
4480 auto_format(FALSE, TRUE);
4481 }
4482 break;
4483
4484 case OP_DELETE:
4485 VIsual_reselect = FALSE; // don't reselect now
4486 if (empty_region_error)
4487 {
4488 vim_beep(BO_OPER);
4489 CancelRedo();
4490 }
4491 else
4492 {
4493 (void)op_delete(oap);
4494 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
4495 u_save_cursor(); // cursor line wasn't saved yet
4496 auto_format(FALSE, TRUE);
4497 }
4498 break;
4499
4500 case OP_YANK:
4501 if (empty_region_error)
4502 {
4503 if (!gui_yank)
4504 {
4505 vim_beep(BO_OPER);
4506 CancelRedo();
4507 }
4508 }
4509 else
4510 {
4511#ifdef FEAT_LINEBREAK
4512 curwin->w_p_lbr = lbr_saved;
4513#endif
4514 (void)op_yank(oap, FALSE, !gui_yank);
4515 }
4516 check_cursor_col();
4517 break;
4518
4519 case OP_CHANGE:
4520 VIsual_reselect = FALSE; // don't reselect now
4521 if (empty_region_error)
4522 {
4523 vim_beep(BO_OPER);
4524 CancelRedo();
4525 }
4526 else
4527 {
4528 // This is a new edit command, not a restart. Need to
4529 // remember it to make 'insertmode' work with mappings for
4530 // Visual mode. But do this only once and not when typed and
4531 // 'insertmode' isn't set.
4532 if (p_im || !KeyTyped)
4533 restart_edit_save = restart_edit;
4534 else
4535 restart_edit_save = 0;
4536 restart_edit = 0;
4537#ifdef FEAT_LINEBREAK
4538 // Restore linebreak, so that when the user edits it looks as
4539 // before.
4540 if (curwin->w_p_lbr != lbr_saved)
4541 {
4542 curwin->w_p_lbr = lbr_saved;
4543 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4544 }
4545#endif
4546 // Reset finish_op now, don't want it set inside edit().
4547 finish_op = FALSE;
4548 if (op_change(oap)) // will call edit()
4549 cap->retval |= CA_COMMAND_BUSY;
4550 if (restart_edit == 0)
4551 restart_edit = restart_edit_save;
4552 }
4553 break;
4554
4555 case OP_FILTER:
4556 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4557 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4558 else
4559 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4560 // FALLTHROUGH
4561
4562 case OP_INDENT:
4563 case OP_COLON:
4564
4565#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4566 // If 'equalprg' is empty, do the indenting internally.
4567 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4568 {
4569# ifdef FEAT_LISP
4570 if (curbuf->b_p_lisp)
4571 {
4572 op_reindent(oap, get_lisp_indent);
4573 break;
4574 }
4575# endif
4576# ifdef FEAT_CINDENT
4577 op_reindent(oap,
4578# ifdef FEAT_EVAL
4579 *curbuf->b_p_inde != NUL ? get_expr_indent :
4580# endif
4581 get_c_indent);
4582 break;
4583# endif
4584 }
4585#endif
4586
4587 op_colon(oap);
4588 break;
4589
4590 case OP_TILDE:
4591 case OP_UPPER:
4592 case OP_LOWER:
4593 case OP_ROT13:
4594 if (empty_region_error)
4595 {
4596 vim_beep(BO_OPER);
4597 CancelRedo();
4598 }
4599 else
4600 op_tilde(oap);
4601 check_cursor_col();
4602 break;
4603
4604 case OP_FORMAT:
4605#if defined(FEAT_EVAL)
4606 if (*curbuf->b_p_fex != NUL)
4607 op_formatexpr(oap); // use expression
4608 else
4609#endif
4610 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
4611 op_colon(oap); // use external command
4612 else
4613 op_format(oap, FALSE); // use internal function
4614 break;
4615
4616 case OP_FORMAT2:
4617 op_format(oap, TRUE); // use internal function
4618 break;
4619
4620 case OP_FUNCTION:
4621#ifdef FEAT_LINEBREAK
4622 // Restore linebreak, so that when the user edits it looks as
4623 // before.
4624 curwin->w_p_lbr = lbr_saved;
4625#endif
4626 op_function(oap); // call 'operatorfunc'
4627 break;
4628
4629 case OP_INSERT:
4630 case OP_APPEND:
4631 VIsual_reselect = FALSE; // don't reselect now
4632 if (empty_region_error)
4633 {
4634 vim_beep(BO_OPER);
4635 CancelRedo();
4636 }
4637 else
4638 {
4639 // This is a new edit command, not a restart. Need to
4640 // remember it to make 'insertmode' work with mappings for
4641 // Visual mode. But do this only once.
4642 restart_edit_save = restart_edit;
4643 restart_edit = 0;
4644#ifdef FEAT_LINEBREAK
4645 // Restore linebreak, so that when the user edits it looks as
4646 // before.
4647 if (curwin->w_p_lbr != lbr_saved)
4648 {
4649 curwin->w_p_lbr = lbr_saved;
4650 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4651 }
4652#endif
4653 op_insert(oap, cap->count1);
4654#ifdef FEAT_LINEBREAK
4655 // Reset linebreak, so that formatting works correctly.
4656 curwin->w_p_lbr = FALSE;
4657#endif
4658
4659 // TODO: when inserting in several lines, should format all
4660 // the lines.
4661 auto_format(FALSE, TRUE);
4662
4663 if (restart_edit == 0)
4664 restart_edit = restart_edit_save;
4665 else
4666 cap->retval |= CA_COMMAND_BUSY;
4667 }
4668 break;
4669
4670 case OP_REPLACE:
4671 VIsual_reselect = FALSE; // don't reselect now
4672 if (empty_region_error)
4673 {
4674 vim_beep(BO_OPER);
4675 CancelRedo();
4676 }
4677 else
4678 {
4679#ifdef FEAT_LINEBREAK
4680 // Restore linebreak, so that when the user edits it looks as
4681 // before.
4682 if (curwin->w_p_lbr != lbr_saved)
4683 {
4684 curwin->w_p_lbr = lbr_saved;
4685 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4686 }
4687#endif
4688 op_replace(oap, cap->nchar);
4689 }
4690 break;
4691
4692#ifdef FEAT_FOLDING
4693 case OP_FOLD:
4694 VIsual_reselect = FALSE; // don't reselect now
4695 foldCreate(oap->start.lnum, oap->end.lnum);
4696 break;
4697
4698 case OP_FOLDOPEN:
4699 case OP_FOLDOPENREC:
4700 case OP_FOLDCLOSE:
4701 case OP_FOLDCLOSEREC:
4702 VIsual_reselect = FALSE; // don't reselect now
4703 opFoldRange(oap->start.lnum, oap->end.lnum,
4704 oap->op_type == OP_FOLDOPEN
4705 || oap->op_type == OP_FOLDOPENREC,
4706 oap->op_type == OP_FOLDOPENREC
4707 || oap->op_type == OP_FOLDCLOSEREC,
4708 oap->is_VIsual);
4709 break;
4710
4711 case OP_FOLDDEL:
4712 case OP_FOLDDELREC:
4713 VIsual_reselect = FALSE; // don't reselect now
4714 deleteFold(oap->start.lnum, oap->end.lnum,
4715 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4716 break;
4717#endif
4718 case OP_NR_ADD:
4719 case OP_NR_SUB:
4720 if (empty_region_error)
4721 {
4722 vim_beep(BO_OPER);
4723 CancelRedo();
4724 }
4725 else
4726 {
4727 VIsual_active = TRUE;
4728#ifdef FEAT_LINEBREAK
4729 curwin->w_p_lbr = lbr_saved;
4730#endif
4731 op_addsub(oap, cap->count1, redo_VIsual_arg);
4732 VIsual_active = FALSE;
4733 }
4734 check_cursor_col();
4735 break;
4736 default:
4737 clearopbeep(oap);
4738 }
4739 virtual_op = MAYBE;
4740 if (!gui_yank)
4741 {
4742 // if 'sol' not set, go back to old column for some commands
4743 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4744 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4745 || oap->op_type == OP_DELETE))
4746 {
4747#ifdef FEAT_LINEBREAK
4748 curwin->w_p_lbr = FALSE;
4749#endif
4750 coladvance(curwin->w_curswant = old_col);
4751 }
4752 }
4753 else
4754 {
4755 curwin->w_cursor = old_cursor;
4756 }
4757 oap->block_mode = FALSE;
4758 clearop(oap);
4759 motion_force = NUL;
4760 }
4761#ifdef FEAT_LINEBREAK
4762 curwin->w_p_lbr = lbr_saved;
4763#endif
4764}