blob: 6a9fbba8163e49d3d0e61b65dcaa9a4793807665 [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 }
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 if (oap->line_count == 1) /* delete characters within one line */
918 {
919 if (u_save_cursor() == FAIL) /* save line for undo */
920 return FAIL;
921
922 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100923 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 && oap->op_type == OP_CHANGE
925 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100926 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 display_dollar(oap->end.col - !oap->inclusive);
928
929 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (virtual_op)
932 {
933 /* fix up things for virtualedit-delete:
934 * break the tabs which are going to get in our way
935 */
936 char_u *curline = ml_get_curline();
937 int len = (int)STRLEN(curline);
938
939 if (oap->end.coladd != 0
940 && (int)oap->end.col >= len - 1
941 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
942 n++;
943 /* Delete at least one char (e.g, when on a control char). */
944 if (n == 0 && oap->start.coladd != oap->end.coladd)
945 n = 1;
946
947 /* When deleted a char in the line, reset coladd. */
948 if (gchar_cursor() != NUL)
949 curwin->w_cursor.coladd = 0;
950 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200951 (void)del_bytes((long)n, !virtual_op,
952 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954 else /* delete characters between lines */
955 {
956 pos_T curpos;
957
958 /* save deleted and changed lines for undo */
959 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
960 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
961 return FAIL;
962
963 truncate_line(TRUE); /* delete from cursor to end of line */
964
965 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
966 ++curwin->w_cursor.lnum;
967 del_lines((long)(oap->line_count - 2), FALSE);
968
Bram Moolenaard009e862015-06-09 20:20:03 +0200969 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200970 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200971 curwin->w_cursor.col = 0;
972 (void)del_bytes((long)n, !virtual_op,
973 oap->op_type == OP_DELETE && !oap->is_VIsual);
974 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
975 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 }
978
979 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
980
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000981setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (oap->block_mode)
983 {
984 curbuf->b_op_end.lnum = oap->end.lnum;
985 curbuf->b_op_end.col = oap->start.col;
986 }
987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 curbuf->b_op_end = oap->start;
989 curbuf->b_op_start = oap->start;
990
991 return OK;
992}
993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994/*
995 * Adjust end of operating area for ending on a multi-byte character.
996 * Used for deletion.
997 */
998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100999mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
1001 char_u *p;
1002
1003 if (oap->inclusive)
1004 {
1005 p = ml_get(oap->end.lnum);
1006 oap->end.col += mb_tail_off(p, p + oap->end.col);
1007 }
1008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar630afe82018-06-28 19:26:28 +02001010/*
1011 * Replace the character under the cursor with "c".
1012 * This takes care of multi-byte characters.
1013 */
1014 static void
1015replace_character(int c)
1016{
1017 int n = State;
1018
1019 State = REPLACE;
1020 ins_char(c);
1021 State = n;
1022 /* Backup to the replaced character. */
1023 dec_cursor();
1024}
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026/*
1027 * Replace a whole area with one character.
1028 */
1029 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001030op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
1032 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 char_u *newp, *oldp;
1035 size_t oldlen;
1036 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001037 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001038 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1041 return OK; /* nothing to do */
1042
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001043 if (c == REPLACE_CR_NCHAR)
1044 {
1045 had_ctrl_v_cr = TRUE;
1046 c = CAR;
1047 }
1048 else if (c == REPLACE_NL_NCHAR)
1049 {
1050 had_ctrl_v_cr = TRUE;
1051 c = NL;
1052 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (has_mbyte)
1055 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
1057 if (u_save((linenr_T)(oap->start.lnum - 1),
1058 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1059 return FAIL;
1060
1061 /*
1062 * block mode replace
1063 */
1064 if (oap->block_mode)
1065 {
1066 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1067 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1068 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001069 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1071 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1072 continue; /* nothing to replace */
1073
1074 /* n == number of extra chars required
1075 * If we split a TAB, it may be replaced by several characters.
1076 * Thus the number of characters may increase!
1077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 /* If the range starts in virtual space, count the initial
1079 * coladd offset as part of "startspaces" */
1080 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1081 {
1082 pos_T vpos;
1083
Bram Moolenaara1381de2009-11-03 15:44:21 +00001084 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 getvpos(&vpos, oap->start_vcol);
1086 bd.startspaces += vpos.coladd;
1087 n = bd.startspaces;
1088 }
1089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 /* allow for pre spaces */
1091 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1092
1093 /* allow for post spp */
1094 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
1097 /* Figure out how many characters to replace. */
1098 numc = oap->end_vcol - oap->start_vcol + 1;
1099 if (bd.is_short && (!virtual_op || bd.is_MAX))
1100 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 /* A double-wide character can be replaced only up to half the
1103 * times. */
1104 if ((*mb_char2cells)(c) > 1)
1105 {
1106 if ((numc & 1) && !bd.is_short)
1107 {
1108 ++bd.endspaces;
1109 ++n;
1110 }
1111 numc = numc / 2;
1112 }
1113
1114 /* Compute bytes needed, move character count to num_chars. */
1115 num_chars = numc;
1116 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 /* oldlen includes textlen, so don't double count */
1118 n += numc - bd.textlen;
1119
1120 oldp = ml_get_curline();
1121 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001122 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (newp == NULL)
1124 continue;
1125 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
1126 /* copy up to deleted part */
1127 mch_memmove(newp, oldp, (size_t)bd.textcol);
1128 oldp += bd.textcol + bd.textlen;
1129 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001130 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001132 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1133 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01001134 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001136 if (has_mbyte)
1137 {
1138 n = (int)STRLEN(newp);
1139 while (--num_chars >= 0)
1140 n += (*mb_char2bytes)(c, newp + n);
1141 }
1142 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001143 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001144 if (!bd.is_short)
1145 {
1146 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001147 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01001148 /* copy the part after the changed part */
1149 STRMOVE(newp + STRLEN(newp), oldp);
1150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 }
1152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001154 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001155 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001156 if (after_p != NULL)
1157 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159 /* replace the line */
1160 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001161 if (after_p != NULL)
1162 {
1163 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1164 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1165 oap->end.lnum++;
1166 vim_free(after_p);
1167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169 }
1170 else
1171 {
1172 /*
1173 * MCHAR and MLINE motion replace.
1174 */
1175 if (oap->motion_type == MLINE)
1176 {
1177 oap->start.col = 0;
1178 curwin->w_cursor.col = 0;
1179 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1180 if (oap->end.col)
1181 --oap->end.col;
1182 }
1183 else if (!oap->inclusive)
1184 dec(&(oap->end));
1185
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001186 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
1188 n = gchar_cursor();
1189 if (n != NUL)
1190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1192 {
1193 /* This is slow, but it handles replacing a single-byte
1194 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01001195 if (curwin->w_cursor.lnum == oap->end.lnum)
1196 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001197 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (n == TAB)
1202 {
1203 int end_vcol = 0;
1204
1205 if (curwin->w_cursor.lnum == oap->end.lnum)
1206 {
1207 /* oap->end has to be recalculated when
1208 * the tab breaks */
1209 end_vcol = getviscol2(oap->end.col,
1210 oap->end.coladd);
1211 }
1212 coladvance_force(getviscol());
1213 if (curwin->w_cursor.lnum == oap->end.lnum)
1214 getvpos(&oap->end, end_vcol);
1215 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001216 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1220 {
1221 int virtcols = oap->end.coladd;
1222
1223 if (curwin->w_cursor.lnum == oap->start.lnum
1224 && oap->start.col == oap->end.col && oap->start.coladd)
1225 virtcols -= oap->start.coladd;
1226
1227 /* oap->end has been trimmed so it's effectively inclusive;
1228 * as a result an extra +1 must be counted so we don't
1229 * trample the NUL byte. */
1230 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1231 curwin->w_cursor.col -= (virtcols + 1);
1232 for (; virtcols >= 0; virtcols--)
1233 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001234 if ((*mb_char2len)(c) > 1)
1235 replace_character(c);
1236 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001237 PBYTE(curwin->w_cursor, c);
1238 if (inc(&curwin->w_cursor) == -1)
1239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243 /* Advance to next character, stop at the end of the file. */
1244 if (inc_cursor() == -1)
1245 break;
1246 }
1247 }
1248
1249 curwin->w_cursor = oap->start;
1250 check_cursor();
1251 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1252
1253 /* Set "'[" and "']" marks. */
1254 curbuf->b_op_start = oap->start;
1255 curbuf->b_op_end = oap->end;
1256
1257 return OK;
1258}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001260static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262/*
1263 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1264 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001266op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
1268 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001270 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
1272 if (u_save((linenr_T)(oap->start.lnum - 1),
1273 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1274 return;
1275
1276 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 if (oap->block_mode) /* Visual block mode */
1278 {
1279 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1280 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001281 int one_change;
1282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 block_prep(oap, &bd, pos.lnum, FALSE);
1284 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001285 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1286 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001287
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001288#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001289 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
1291 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1292
Bram Moolenaar009b2592004-10-24 19:18:58 +00001293 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1294 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001296 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300 if (did_change)
1301 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1302 }
1303 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
1305 if (oap->motion_type == MLINE)
1306 {
1307 oap->start.col = 0;
1308 pos.col = 0;
1309 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1310 if (oap->end.col)
1311 --oap->end.col;
1312 }
1313 else if (!oap->inclusive)
1314 dec(&(oap->end));
1315
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001316 if (pos.lnum == oap->end.lnum)
1317 did_change = swapchars(oap->op_type, &pos,
1318 oap->end.col - pos.col + 1);
1319 else
1320 for (;;)
1321 {
1322 did_change |= swapchars(oap->op_type, &pos,
1323 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1324 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001325 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001326 break;
1327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (did_change)
1329 {
1330 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1331 0L);
1332#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001333 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
1335 char_u *ptr;
1336 int count;
1337
1338 pos = oap->start;
1339 while (pos.lnum < oap->end.lnum)
1340 {
1341 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001342 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001343 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001345 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 pos.col = 0;
1347 pos.lnum++;
1348 }
1349 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1350 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001351 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001353 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355#endif
1356 }
1357 }
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (!did_change && oap->is_VIsual)
1360 /* No change: need to remove the Visual selection */
1361 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
1363 /*
1364 * Set '[ and '] marks.
1365 */
1366 curbuf->b_op_start = oap->start;
1367 curbuf->b_op_end = oap->end;
1368
1369 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001370 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001371 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372}
1373
1374/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001375 * Invoke swapchar() on "length" bytes at position "pos".
1376 * "pos" is advanced to just after the changed characters.
1377 * "length" is rounded up to include the whole last multi-byte character.
1378 * Also works correctly when the number of bytes changes.
1379 * Returns TRUE if some character was changed.
1380 */
1381 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001382swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001383{
1384 int todo;
1385 int did_change = 0;
1386
1387 for (todo = length; todo > 0; --todo)
1388 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001389 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001390 {
1391 int len = (*mb_ptr2len)(ml_get_pos(pos));
1392
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001393 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001394 if (len > 0)
1395 todo -= len - 1;
1396 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001397 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001398 if (inc(pos) == -1) /* at end of file */
1399 break;
1400 }
1401 return did_change;
1402}
1403
1404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * If op_type == OP_UPPER: make uppercase,
1406 * if op_type == OP_LOWER: make lowercase,
1407 * if op_type == OP_ROT13: do rot13 encoding,
1408 * else swap case of character at 'pos'
1409 * returns TRUE when something actually changed.
1410 */
1411 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001412swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 int c;
1415 int nc;
1416
1417 c = gchar_pos(pos);
1418
1419 /* Only do rot13 encoding for ASCII characters. */
1420 if (c >= 0x80 && op_type == OP_ROT13)
1421 return FALSE;
1422
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001423 if (op_type == OP_UPPER && c == 0xdf
1424 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001425 {
1426 pos_T sp = curwin->w_cursor;
1427
1428 /* Special handling of German sharp s: change to "SS". */
1429 curwin->w_cursor = *pos;
1430 del_char(FALSE);
1431 ins_char('S');
1432 ins_char('S');
1433 curwin->w_cursor = sp;
1434 inc(pos);
1435 }
1436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
1438 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 nc = c;
1440 if (MB_ISLOWER(c))
1441 {
1442 if (op_type == OP_ROT13)
1443 nc = ROT13(c, 'a');
1444 else if (op_type != OP_LOWER)
1445 nc = MB_TOUPPER(c);
1446 }
1447 else if (MB_ISUPPER(c))
1448 {
1449 if (op_type == OP_ROT13)
1450 nc = ROT13(c, 'A');
1451 else if (op_type != OP_UPPER)
1452 nc = MB_TOLOWER(c);
1453 }
1454 if (nc != c)
1455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1457 {
1458 pos_T sp = curwin->w_cursor;
1459
1460 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001461 /* don't use del_char(), it also removes composing chars */
1462 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 ins_char(nc);
1464 curwin->w_cursor = sp;
1465 }
1466 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001467 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 return TRUE;
1469 }
1470 return FALSE;
1471}
1472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473/*
1474 * op_insert - Insert and append operators for Visual mode.
1475 */
1476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001477op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
1479 long ins_len, pre_textlen = 0;
1480 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001481 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 struct block_def bd;
1483 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001484 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 /* edit() changes this - record it for OP_APPEND */
1487 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1488
1489 /* vis block is still marked. Get rid of it now. */
1490 curwin->w_cursor.lnum = oap->start.lnum;
1491 update_screen(INVERTED);
1492
1493 if (oap->block_mode)
1494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 /* When 'virtualedit' is used, need to insert the extra spaces before
1496 * doing block_prep(). When only "block" is used, virtual edit is
1497 * already disabled, but still need it when calling
1498 * coladvance_force(). */
1499 if (curwin->w_cursor.coladd > 0)
1500 {
1501 int old_ve_flags = ve_flags;
1502
1503 ve_flags = VE_ALL;
1504 if (u_save_cursor() == FAIL)
1505 return;
1506 coladvance_force(oap->op_type == OP_APPEND
1507 ? oap->end_vcol + 1 : getviscol());
1508 if (oap->op_type == OP_APPEND)
1509 --curwin->w_cursor.col;
1510 ve_flags = old_ve_flags;
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 /* Get the info about the block before entering the text */
1513 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001514 /* Get indent information */
1515 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001517
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if (oap->op_type == OP_APPEND)
1519 firstline += bd.textlen;
1520 pre_textlen = (long)STRLEN(firstline);
1521 }
1522
1523 if (oap->op_type == OP_APPEND)
1524 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001525 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
1527 /* Move the cursor to the character right of the block. */
1528 curwin->w_set_curswant = TRUE;
1529 while (*ml_get_cursor() != NUL
1530 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1531 ++curwin->w_cursor.col;
1532 if (bd.is_short && !bd.is_MAX)
1533 {
1534 /* First line was too short, make it longer and adjust the
1535 * values in "bd". */
1536 if (u_save_cursor() == FAIL)
1537 return;
1538 for (i = 0; i < bd.endspaces; ++i)
1539 ins_char(' ');
1540 bd.textlen += bd.endspaces;
1541 }
1542 }
1543 else
1544 {
1545 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001546 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001549 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 && oap->start_vcol != oap->end_vcol)
1551 inc_cursor();
1552 }
1553 }
1554
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001555 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001556 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001558 /* When a tab was inserted, and the characters in front of the tab
1559 * have been converted to a tab as well, the column of the cursor
1560 * might have actually been reduced, so need to adjust here. */
1561 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001562 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001563 oap->start = curbuf->b_op_start_orig;
1564
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001565 /* If user has moved off this line, we don't know what to do, so do
1566 * nothing.
1567 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
1568 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return;
1570
1571 if (oap->block_mode)
1572 {
1573 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001574 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001575 size_t len;
1576 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001578 /* If indent kicked in, the firstline might have changed
1579 * but only do that, if the indent actually increased. */
1580 ind_post = (colnr_T)getwhitecols_curline();
1581 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1582 {
1583 bd.textcol += ind_post - ind_pre;
1584 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001585 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001586 }
1587
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001588 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001589 * to adjust the block for that. But only do it, if the difference
1590 * does not come from indent kicking in. */
1591 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1592 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001593 {
1594 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001595 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001596 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001597 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001598 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001599 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001600 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001601 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001602 pre_textlen -= t - oap->start_vcol;
1603 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001604 }
1605 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001606 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001607 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001608 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001609 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001610 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001611 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001612 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001613 /* reset pre_textlen to the value of OP_INSERT */
1614 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001615 pre_textlen -= t - oap->start_vcol;
1616 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001617 oap->op_type = OP_INSERT;
1618 }
1619 }
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /*
1622 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001623 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 * Don't do this when "$" used, end-of-line will have changed.
1625 */
1626 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1627 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1628 {
1629 if (oap->op_type == OP_APPEND)
1630 {
1631 pre_textlen += bd2.textlen - bd.textlen;
1632 if (bd2.endspaces)
1633 --bd2.textlen;
1634 }
1635 bd.textcol = bd2.textcol;
1636 bd.textlen = bd2.textlen;
1637 }
1638
1639 /*
1640 * Subsequent calls to ml_get() flush the firstline data - take a
1641 * copy of the required string.
1642 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001643 firstline = ml_get(oap->start.lnum);
1644 len = STRLEN(firstline);
1645 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001647 add += bd.textlen;
1648 if ((size_t)add > len)
1649 firstline += len; // short line, point to the NUL
1650 else
1651 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001652 if (pre_textlen >= 0
1653 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
1655 ins_text = vim_strnsave(firstline, (int)ins_len);
1656 if (ins_text != NULL)
1657 {
1658 /* block handled here */
1659 if (u_save(oap->start.lnum,
1660 (linenr_T)(oap->end.lnum + 1)) == OK)
1661 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1662 &bd);
1663
1664 curwin->w_cursor.col = oap->start.col;
1665 check_cursor();
1666 vim_free(ins_text);
1667 }
1668 }
1669 }
1670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672/*
1673 * op_change - handle a change operation
1674 *
1675 * return TRUE if edit() returns because of a CTRL-O command
1676 */
1677 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001678op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 colnr_T l;
1681 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 long offset;
1683 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001684 long ins_len;
1685 long pre_textlen = 0;
1686 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 char_u *firstline;
1688 char_u *ins_text, *newp, *oldp;
1689 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
1691 l = oap->start.col;
1692 if (oap->motion_type == MLINE)
1693 {
1694 l = 0;
1695#ifdef FEAT_SMARTINDENT
1696 if (!p_paste && curbuf->b_p_si
1697# ifdef FEAT_CINDENT
1698 && !curbuf->b_p_cin
1699# endif
1700 )
1701 can_si = TRUE; /* It's like opening a new line, do si */
1702#endif
1703 }
1704
1705 /* First delete the text in the region. In an empty buffer only need to
1706 * save for undo */
1707 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1708 {
1709 if (u_save_cursor() == FAIL)
1710 return FALSE;
1711 }
1712 else if (op_delete(oap) == FAIL)
1713 return FALSE;
1714
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001715 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 && !virtual_op)
1717 inc_cursor();
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 /* check for still on same line (<CR> in inserted text meaningless) */
1720 /* skip blank lines too */
1721 if (oap->block_mode)
1722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 /* Add spaces before getting the current line length. */
1724 if (virtual_op && (curwin->w_cursor.coladd > 0
1725 || gchar_cursor() == NUL))
1726 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001727 firstline = ml_get(oap->start.lnum);
1728 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001729 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 bd.textcol = curwin->w_cursor.col;
1731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1734 if (oap->motion_type == MLINE)
1735 fix_indent();
1736#endif
1737
1738 retval = edit(NUL, FALSE, (linenr_T)1);
1739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001741 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001743 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001745 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00001747 /* Auto-indenting may have changed the indent. If the cursor was past
1748 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001750 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001752 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001753
1754 pre_textlen += new_indent - pre_indent;
1755 bd.textcol += new_indent - pre_indent;
1756 }
1757
1758 ins_len = (long)STRLEN(firstline) - pre_textlen;
1759 if (ins_len > 0)
1760 {
1761 /* Subsequent calls to ml_get() flush the firstline data - take a
1762 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001763 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001765 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1767 linenr++)
1768 {
1769 block_prep(oap, &bd, linenr, TRUE);
1770 if (!bd.is_short || virtual_op)
1771 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 pos_T vpos;
1773
1774 /* If the block starts in virtual space, count the
1775 * initial coladd offset as part of "startspaces" */
1776 if (bd.is_short)
1777 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001778 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 }
1781 else
1782 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001784 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (newp == NULL)
1786 continue;
1787 /* copy up to block start */
1788 mch_memmove(newp, oldp, (size_t)bd.textcol);
1789 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001790 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1793 offset += ins_len;
1794 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001795 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 ml_replace(linenr, newp, FALSE);
1797 }
1798 }
1799 check_cursor();
1800
1801 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1802 }
1803 vim_free(ins_text);
1804 }
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 return retval;
1808}
1809
1810/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001811 * When the cursor is on the NUL past the end of the line and it should not be
1812 * there move it left.
1813 */
1814 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001815adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001816{
1817 if (curwin->w_cursor.col > 0
1818 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001819 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 && !(restart_edit || (State & INSERT)))
1821 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00001822 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00001823 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001826 {
1827 colnr_T scol, ecol;
1828
1829 /* Coladd is set to the width of the last character. */
1830 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1831 curwin->w_cursor.coladd = ecol - scol + 1;
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834}
1835
Bram Moolenaar81340392012-06-06 16:12:59 +02001836/*
1837 * If "process" is TRUE and the line begins with a comment leader (possibly
1838 * after some white space), return a pointer to the text after it. Put a boolean
1839 * value indicating whether the line ends with an unclosed comment in
1840 * "is_comment".
1841 * line - line to be processed,
1842 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001843 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001844 * include_space - whether to also skip space following the comment leader,
1845 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001846 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001847 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001848 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001849skip_comment(
1850 char_u *line,
1851 int process,
1852 int include_space,
1853 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001854{
1855 char_u *comment_flags = NULL;
1856 int lead_len;
1857 int leader_offset = get_last_leader_offset(line, &comment_flags);
1858
1859 *is_comment = FALSE;
1860 if (leader_offset != -1)
1861 {
1862 /* Let's check whether the line ends with an unclosed comment.
1863 * If the last comment leader has COM_END in flags, there's no comment.
1864 */
1865 while (*comment_flags)
1866 {
1867 if (*comment_flags == COM_END
1868 || *comment_flags == ':')
1869 break;
1870 ++comment_flags;
1871 }
1872 if (*comment_flags != COM_END)
1873 *is_comment = TRUE;
1874 }
1875
1876 if (process == FALSE)
1877 return line;
1878
1879 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1880
1881 if (lead_len == 0)
1882 return line;
1883
1884 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02001885 * - COM_END,
1886 * - colon,
1887 * whichever comes first.
1888 */
1889 while (*comment_flags)
1890 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001891 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001892 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001893 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001894 ++comment_flags;
1895 }
1896
1897 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001898 * starting with a closing part of a three-part comment. That's good,
1899 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001900 */
1901 if (*comment_flags == ':' || *comment_flags == NUL)
1902 line += lead_len;
1903
1904 return line;
1905}
Bram Moolenaar81340392012-06-06 16:12:59 +02001906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001908 * Join 'count' lines (minimal 2) at cursor position.
1909 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001910 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1911 * leaders should not be removed.
1912 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1913 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001915 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 */
1917 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918do_join(
1919 long count,
1920 int insert_space,
1921 int save_undo,
1922 int use_formatoptions UNUSED,
1923 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001925 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001926 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001927 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001929 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02001930 int endcurr1 = NUL;
1931 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001932 int currsize = 0; /* size of the current line */
1933 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001935 colnr_T col = 0;
1936 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001937 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001938 int remove_comments = (use_formatoptions == TRUE)
1939 && has_format_option(FO_REMOVE_COMS);
1940 int prev_was_comment;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001941#ifdef FEAT_TEXT_PROP
1942 textprop_T **prop_lines = NULL;
1943 int *prop_lengths = NULL;
1944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001946 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1947 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001950 /* Allocate an array to store the number of spaces inserted before each
1951 * line. We will use it to pre-compute the length of the new line and the
1952 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001953 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001954 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001956 if (remove_comments)
1957 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001958 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001959 if (comments == NULL)
1960 {
1961 vim_free(spaces);
1962 return FAIL;
1963 }
1964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001967 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001968 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001969 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001971 for (t = 0; t < count; ++t)
1972 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001973 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001974 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001975 {
1976 /* Set the '[ mark. */
1977 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1978 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1979 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001980 if (remove_comments)
1981 {
1982 /* We don't want to remove the comment leader if the
1983 * previous line is not a comment. */
1984 if (t > 0 && prev_was_comment)
1985 {
1986
1987 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1988 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001989 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02001990 curr = new_curr;
1991 }
1992 else
1993 curr = skip_comment(curr, FALSE, insert_space,
1994 &prev_was_comment);
1995 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001996
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001997 if (insert_space && t > 0)
1998 {
1999 curr = skipwhite(curr);
2000 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002001 && (!has_format_option(FO_MBYTE_JOIN)
2002 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2003 && (!has_format_option(FO_MBYTE_JOIN2)
2004 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002005 )
2006 {
2007 /* don't add a space if the line is ending in a space */
2008 if (endcurr1 == ' ')
2009 endcurr1 = endcurr2;
2010 else
2011 ++spaces[t];
2012 /* extra space when 'joinspaces' set and line ends in '.' */
2013 if ( p_js
2014 && (endcurr1 == '.'
2015 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2016 && (endcurr1 == '?' || endcurr1 == '!'))))
2017 ++spaces[t];
2018 }
2019 }
2020 currsize = (int)STRLEN(curr);
2021 sumsize += currsize + spaces[t];
2022 endcurr1 = endcurr2 = NUL;
2023 if (insert_space && currsize > 0)
2024 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002025 if (has_mbyte)
2026 {
2027 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002028 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002029 endcurr1 = (*mb_ptr2char)(cend);
2030 if (cend > curr)
2031 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002032 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002033 endcurr2 = (*mb_ptr2char)(cend);
2034 }
2035 }
2036 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002037 {
2038 endcurr1 = *(curr + currsize - 1);
2039 if (currsize > 1)
2040 endcurr2 = *(curr + currsize - 2);
2041 }
2042 }
2043 line_breakcheck();
2044 if (got_int)
2045 {
2046 ret = FAIL;
2047 goto theend;
2048 }
2049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002051 /* store the column position before last line */
2052 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002054 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002055 newp = alloc(sumsize + 1);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002056 if (newp == NULL)
2057 {
2058 ret = FAIL;
2059 goto theend;
2060 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002061 cend = newp + sumsize;
2062 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002064#ifdef FEAT_TEXT_PROP
2065 // We need to move properties of the lines that are going to be deleted to
2066 // the new long one.
2067 if (curbuf->b_has_textprop && !text_prop_frozen)
2068 {
2069 // Allocate an array to copy the text properties of joined lines into.
2070 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002071 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
2072 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002073 if (prop_lengths == NULL)
2074 VIM_CLEAR(prop_lines);
2075 }
2076#endif
2077
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002078 /*
2079 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002080 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002081 *
2082 * Move marks from each deleted line to the joined line, adjusting the
2083 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2084 * should not really be a problem.
2085 */
2086 for (t = count - 1; ; --t)
2087 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002088 int spaces_removed;
2089
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002090 cend -= currsize;
2091 mch_memmove(cend, curr, (size_t)currsize);
2092 if (spaces[t] > 0)
2093 {
2094 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002095 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002096 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002097
2098 // If deleting more spaces than adding, the cursor moves no more than
2099 // what is added if it is inside these spaces.
2100 spaces_removed = (curr - curr_start) - spaces[t];
2101
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002102 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002103 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 if (t == 0)
2105 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002106#ifdef FEAT_TEXT_PROP
2107 if (prop_lines != NULL)
2108 adjust_props_for_join(curwin->w_cursor.lnum + t,
2109 prop_lines + t - 1, prop_lengths + t - 1,
2110 (long)(cend - newp - spaces_removed), spaces_removed);
2111#endif
2112
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002113 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002114 if (remove_comments)
2115 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002116 if (insert_space && t > 1)
2117 curr = skipwhite(curr);
2118 currsize = (int)STRLEN(curr);
2119 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002120
2121#ifdef FEAT_TEXT_PROP
2122 if (prop_lines != NULL)
2123 join_prop_lines(curwin->w_cursor.lnum, newp,
2124 prop_lines, prop_lengths, count);
2125 else
2126#endif
2127 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002129 if (setmark)
2130 {
2131 /* Set the '] mark. */
2132 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002133 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002134 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002135
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 /* Only report the change in the first line here, del_lines() will report
2137 * the deleted line. */
2138 changed_lines(curwin->w_cursor.lnum, currsize,
2139 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002141 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 * briefly, and then move it back. After del_lines() the cursor may
2143 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 */
2145 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002147 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 curwin->w_cursor.lnum = t;
2149
2150 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002151 * Set the cursor column:
2152 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002153 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002155 curwin->w_cursor.col =
2156 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 curwin->w_set_curswant = TRUE;
2161
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002162theend:
2163 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002164 if (remove_comments)
2165 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002166 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169/*
2170 * Return TRUE if the two comment leaders given are the same. "lnum" is
2171 * the first line. White-space is ignored. Note that the whole of
2172 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
2173 */
2174 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002175same_leader(
2176 linenr_T lnum,
2177 int leader1_len,
2178 char_u *leader1_flags,
2179 int leader2_len,
2180 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 int idx1 = 0, idx2 = 0;
2183 char_u *p;
2184 char_u *line1;
2185 char_u *line2;
2186
2187 if (leader1_len == 0)
2188 return (leader2_len == 0);
2189
2190 /*
2191 * If first leader has 'f' flag, the lines can be joined only if the
2192 * second line does not have a leader.
2193 * If first leader has 'e' flag, the lines can never be joined.
2194 * If fist leader has 's' flag, the lines can only be joined if there is
2195 * some text after it and the second line has the 'm' flag.
2196 */
2197 if (leader1_flags != NULL)
2198 {
2199 for (p = leader1_flags; *p && *p != ':'; ++p)
2200 {
2201 if (*p == COM_FIRST)
2202 return (leader2_len == 0);
2203 if (*p == COM_END)
2204 return FALSE;
2205 if (*p == COM_START)
2206 {
2207 if (*(ml_get(lnum) + leader1_len) == NUL)
2208 return FALSE;
2209 if (leader2_flags == NULL || leader2_len == 0)
2210 return FALSE;
2211 for (p = leader2_flags; *p && *p != ':'; ++p)
2212 if (*p == COM_MIDDLE)
2213 return TRUE;
2214 return FALSE;
2215 }
2216 }
2217 }
2218
2219 /*
2220 * Get current line and next line, compare the leaders.
2221 * The first line has to be saved, only one line can be locked at a time.
2222 */
2223 line1 = vim_strsave(ml_get(lnum));
2224 if (line1 != NULL)
2225 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002226 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 ;
2228 line2 = ml_get(lnum + 1);
2229 for (idx2 = 0; idx2 < leader2_len; ++idx2)
2230 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002231 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 if (line1[idx1++] != line2[idx2])
2234 break;
2235 }
2236 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01002237 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 ++idx1;
2239 }
2240 vim_free(line1);
2241 }
2242 return (idx2 == leader2_len && idx1 == leader1_len);
2243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
2245/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01002246 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002249op_format(
2250 oparg_T *oap,
2251 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 long old_line_count = curbuf->b_ml.ml_line_count;
2254
2255 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
2256 * can put it back there. */
2257 curwin->w_cursor = oap->cursor_start;
2258
2259 if (u_save((linenr_T)(oap->start.lnum - 1),
2260 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2261 return;
2262 curwin->w_cursor = oap->start;
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (oap->is_VIsual)
2265 /* When there is no change: need to remove the Visual selection */
2266 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
2268 /* Set '[ mark at the start of the formatted area */
2269 curbuf->b_op_start = oap->start;
2270
2271 /* For "gw" remember the cursor position and put it back below (adjusted
2272 * for joined and split lines). */
2273 if (keep_cursor)
2274 saved_cursor = oap->cursor_start;
2275
Bram Moolenaar81a82092008-03-12 16:27:00 +00002276 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 /*
2279 * Leave the cursor at the first non-blank of the last formatted line.
2280 * If the cursor was moved one line back (e.g. with "Q}") go to the next
2281 * line, so "." will do the next lines.
2282 */
2283 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2284 ++curwin->w_cursor.lnum;
2285 beginline(BL_WHITE | BL_FIX);
2286 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
2287 msgmore(old_line_count);
2288
2289 /* put '] mark on the end of the formatted area */
2290 curbuf->b_op_end = curwin->w_cursor;
2291
2292 if (keep_cursor)
2293 {
2294 curwin->w_cursor = saved_cursor;
2295 saved_cursor.lnum = 0;
2296 }
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (oap->is_VIsual)
2299 {
2300 win_T *wp;
2301
2302 FOR_ALL_WINDOWS(wp)
2303 {
2304 if (wp->w_old_cursor_lnum != 0)
2305 {
2306 /* When lines have been inserted or deleted, adjust the end of
2307 * the Visual area to be redrawn. */
2308 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
2309 wp->w_old_cursor_lnum += old_line_count;
2310 else
2311 wp->w_old_visual_lnum += old_line_count;
2312 }
2313 }
2314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315}
2316
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002317#if defined(FEAT_EVAL) || defined(PROTO)
2318/*
2319 * Implementation of the format operator 'gq' for when using 'formatexpr'.
2320 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002321 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002322op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002323{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002324 if (oap->is_VIsual)
2325 /* When there is no change: need to remove the Visual selection */
2326 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002327
Bram Moolenaar700303e2010-07-11 17:35:50 +02002328 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
2329 /* As documented: when 'formatexpr' returns non-zero fall back to
2330 * internal formatting. */
2331 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002332}
2333
2334 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002335fex_format(
2336 linenr_T lnum,
2337 long count,
2338 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002339{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002340 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
2341 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002342 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002343 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002344
2345 /*
2346 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002347 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002348 */
2349 set_vim_var_nr(VV_LNUM, lnum);
2350 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00002351 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002352
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002353 /* Make a copy, the option could be changed while calling it. */
2354 fex = vim_strsave(curbuf->b_p_fex);
2355 if (fex == NULL)
2356 return 0;
2357
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002358 /*
2359 * Evaluate the function.
2360 */
2361 if (use_sandbox)
2362 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002363 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002364 if (use_sandbox)
2365 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002366
2367 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002368 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002369
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370 return r;
2371}
2372#endif
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374/*
2375 * Format "line_count" lines, starting at the cursor position.
2376 * When "line_count" is negative, format until the end of the paragraph.
2377 * Lines after the cursor line are saved for undo, caller must have saved the
2378 * first line.
2379 */
2380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002381format_lines(
2382 linenr_T line_count,
2383 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 int max_len;
2386 int is_not_par; /* current line not part of parag. */
2387 int next_is_not_par; /* next line not part of paragraph */
2388 int is_end_par; /* at end of paragraph */
2389 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
2390 int next_is_start_par = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 int leader_len = 0; /* leader len of current line */
2392 int next_leader_len; /* leader len of next line */
2393 char_u *leader_flags = NULL; /* flags for leader of current line */
2394 char_u *next_leader_flags; /* flags for leader of next line */
2395 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002396 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002398 int second_indent = -1; /* indent for second line (comment
2399 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 int do_second_indent;
2401 int do_number_indent;
2402 int do_trail_white;
2403 int first_par_line = TRUE;
2404 int smd_save;
2405 long count;
2406 int need_set_indent = TRUE; /* set indent of next paragraph */
2407 int force_format = FALSE;
2408 int old_State = State;
2409
2410 /* length of a line to force formatting: 3 * 'tw' */
2411 max_len = comp_textwidth(TRUE) * 3;
2412
2413 /* check for 'q', '2' and '1' in 'formatoptions' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 do_second_indent = has_format_option(FO_Q_SECOND);
2416 do_number_indent = has_format_option(FO_Q_NUMBER);
2417 do_trail_white = has_format_option(FO_WHITE_PAR);
2418
2419 /*
2420 * Get info about the previous and current line.
2421 */
2422 if (curwin->w_cursor.lnum > 1)
2423 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002424 , &leader_len, &leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 else
2426 is_not_par = TRUE;
2427 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002428 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 is_end_par = (is_not_par || next_is_not_par);
2430 if (!is_end_par && do_trail_white)
2431 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
2432
2433 curwin->w_cursor.lnum--;
2434 for (count = line_count; count != 0 && !got_int; --count)
2435 {
2436 /*
2437 * Advance to next paragraph.
2438 */
2439 if (advance)
2440 {
2441 curwin->w_cursor.lnum++;
2442 prev_is_end_par = is_end_par;
2443 is_not_par = next_is_not_par;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 leader_len = next_leader_len;
2445 leader_flags = next_leader_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447
2448 /*
2449 * The last line to be formatted.
2450 */
2451 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2452 {
2453 next_is_not_par = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 next_leader_len = 0;
2455 next_leader_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457 else
2458 {
2459 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002460 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (do_number_indent)
2462 next_is_start_par =
2463 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
2464 }
2465 advance = TRUE;
2466 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
2467 if (!is_end_par && do_trail_white)
2468 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
2469
2470 /*
2471 * Skip lines that are not in a paragraph.
2472 */
2473 if (is_not_par)
2474 {
2475 if (line_count < 0)
2476 break;
2477 }
2478 else
2479 {
2480 /*
2481 * For the first line of a paragraph, check indent of second line.
2482 * Don't do this for comments and empty lines.
2483 */
2484 if (first_par_line
2485 && (do_second_indent || do_number_indent)
2486 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002487 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002489 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002490 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002491 if (leader_len == 0 && next_leader_len == 0)
2492 {
2493 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002494 second_indent =
2495 get_indent_lnum(curwin->w_cursor.lnum + 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002496 }
2497 else
2498 {
2499 second_indent = next_leader_len;
2500 do_comments_list = 1;
2501 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002504 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002505 if (leader_len == 0 && next_leader_len == 0)
2506 {
2507 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002508 second_indent =
2509 get_number_indent(curwin->w_cursor.lnum);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002510 }
2511 else
2512 {
2513 /* get_number_indent() is now "comment aware"... */
2514 second_indent =
2515 get_number_indent(curwin->w_cursor.lnum);
2516 do_comments_list = 1;
2517 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520
2521 /*
2522 * When the comment leader changes, it's the end of the paragraph.
2523 */
2524 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 || !same_leader(curwin->w_cursor.lnum,
2526 leader_len, leader_flags,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002527 next_leader_len, next_leader_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 is_end_par = TRUE;
2529
2530 /*
2531 * If we have got to the end of a paragraph, or the line is
2532 * getting long, format it.
2533 */
2534 if (is_end_par || force_format)
2535 {
2536 if (need_set_indent)
2537 /* replace indent in first line with minimal number of
2538 * tabs and spaces, according to current options */
2539 (void)set_indent(get_indent(), SIN_CHANGED);
2540
2541 /* put cursor on last non-space */
2542 State = NORMAL; /* don't go past end-of-line */
2543 coladvance((colnr_T)MAXCOL);
2544 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
2545 dec_cursor();
2546
2547 /* do the formatting, without 'showmode' */
2548 State = INSERT; /* for open_line() */
2549 smd_save = p_smd;
2550 p_smd = FALSE;
2551 insertchar(NUL, INSCHAR_FORMAT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002553 + (do_comments && do_comments_list
2554 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar81a82092008-03-12 16:27:00 +00002555 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 State = old_State;
2557 p_smd = smd_save;
2558 second_indent = -1;
2559 /* at end of par.: need to set indent of next par. */
2560 need_set_indent = is_end_par;
2561 if (is_end_par)
2562 {
2563 /* When called with a negative line count, break at the
2564 * end of the paragraph. */
2565 if (line_count < 0)
2566 break;
2567 first_par_line = TRUE;
2568 }
2569 force_format = FALSE;
2570 }
2571
2572 /*
2573 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002574 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 */
2576 if (!is_end_par)
2577 {
2578 advance = FALSE;
2579 curwin->w_cursor.lnum++;
2580 curwin->w_cursor.col = 0;
2581 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002584 {
2585 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002587 (long)-next_leader_len, 0);
2588 }
2589 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002590 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002591 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002592
2593 if (indent > 0)
2594 {
2595 (void)del_bytes(indent, FALSE, FALSE);
2596 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002597 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01002598 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002601 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 beep_flush();
2604 break;
2605 }
2606 first_par_line = FALSE;
2607 /* If the line is getting long, format it next time */
2608 if (STRLEN(ml_get_curline()) > (size_t)max_len)
2609 force_format = TRUE;
2610 else
2611 force_format = FALSE;
2612 }
2613 }
2614 line_breakcheck();
2615 }
2616}
2617
2618/*
2619 * Return TRUE if line "lnum" ends in a white character.
2620 */
2621 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002622ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624 char_u *s = ml_get(lnum);
2625 size_t l;
2626
2627 if (*s == NUL)
2628 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002629 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 * invocation may call function multiple times". */
2631 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002632 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633}
2634
2635/*
2636 * Blank lines, and lines containing only the comment leader, are left
2637 * untouched by the formatting. The function returns TRUE in this
2638 * case. It also returns TRUE when a line starts with the end of a comment
2639 * ('e' in comment flags), so that this line is skipped, and not joined to the
2640 * previous line. A new paragraph starts after a blank line, or when the
2641 * comment leader changes -- webb.
2642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002644fmt_check_par(
2645 linenr_T lnum,
2646 int *leader_len,
2647 char_u **leader_flags,
2648 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 char_u *flags = NULL; /* init for GCC */
2651 char_u *ptr;
2652
2653 ptr = ml_get(lnum);
2654 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002655 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 else
2657 *leader_len = 0;
2658
2659 if (*leader_len > 0)
2660 {
2661 /*
2662 * Search for 'e' flag in comment leader flags.
2663 */
2664 flags = *leader_flags;
2665 while (*flags && *flags != ':' && *flags != COM_END)
2666 ++flags;
2667 }
2668
2669 return (*skipwhite(ptr + *leader_len) == NUL
2670 || (*leader_len > 0 && *flags == COM_END)
2671 || startPS(lnum, NUL, FALSE));
2672}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674/*
2675 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
2676 * previous line is in the same paragraph. Used for auto-formatting.
2677 */
2678 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002679paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int leader_len = 0; /* leader len of current line */
2683 char_u *leader_flags = NULL; /* flags for leader of current line */
2684 int next_leader_len; /* leader len of next line */
2685 char_u *next_leader_flags; /* flags for leader of next line */
2686 int do_comments; /* format comments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 if (lnum <= 1)
2689 return TRUE; /* start of the file */
2690
2691 p = ml_get(lnum - 1);
2692 if (*p == NUL)
2693 return TRUE; /* after empty line */
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002696 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return TRUE; /* after non-paragraph line */
2698
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002699 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return TRUE; /* "lnum" is not a paragraph line */
2701
2702 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
2703 return TRUE; /* missing trailing space in previous line. */
2704
2705 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
2706 return TRUE; /* numbered item starts in "lnum". */
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (!same_leader(lnum - 1, leader_len, leader_flags,
2709 next_leader_len, next_leader_flags))
2710 return TRUE; /* change of comment leader. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712 return FALSE;
2713}
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715/*
2716 * prepare a few things for block mode yank/delete/tilde
2717 *
2718 * for delete:
2719 * - textlen includes the first/last char to be (partly) deleted
2720 * - start/endspaces is the number of columns that are taken by the
2721 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002722 * deleted.
2723 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * - textlen includes the first/last char to be wholly yanked
2725 * - start/endspaces is the number of columns of the first/last yanked char
2726 * that are to be yanked.
2727 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002729block_prep(
2730 oparg_T *oap,
2731 struct block_def *bdp,
2732 linenr_T lnum,
2733 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
2735 int incr = 0;
2736 char_u *pend;
2737 char_u *pstart;
2738 char_u *line;
2739 char_u *prev_pstart;
2740 char_u *prev_pend;
2741
2742 bdp->startspaces = 0;
2743 bdp->endspaces = 0;
2744 bdp->textlen = 0;
2745 bdp->start_vcol = 0;
2746 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 bdp->is_short = FALSE;
2748 bdp->is_oneChar = FALSE;
2749 bdp->pre_whitesp = 0;
2750 bdp->pre_whitesp_c = 0;
2751 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 bdp->start_char_vcols = 0;
2753
2754 line = ml_get(lnum);
2755 pstart = line;
2756 prev_pstart = line;
2757 while (bdp->start_vcol < oap->start_vcol && *pstart)
2758 {
2759 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002760 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002762 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
2764 bdp->pre_whitesp += incr;
2765 bdp->pre_whitesp_c++;
2766 }
2767 else
2768 {
2769 bdp->pre_whitesp = 0;
2770 bdp->pre_whitesp_c = 0;
2771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002773 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 bdp->start_char_vcols = incr;
2776 if (bdp->start_vcol < oap->start_vcol) /* line too short */
2777 {
2778 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (!is_del || oap->op_type == OP_APPEND)
2781 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2782 }
2783 else
2784 {
2785 /* notice: this converts partly selected Multibyte characters to
2786 * spaces, too. */
2787 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2788 if (is_del && bdp->startspaces)
2789 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2790 pend = pstart;
2791 bdp->end_vcol = bdp->start_vcol;
2792 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
2793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (oap->op_type == OP_INSERT)
2796 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2797 else if (oap->op_type == OP_APPEND)
2798 {
2799 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2800 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2801 }
2802 else
2803 {
2804 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2805 if (is_del && oap->op_type != OP_LSHIFT)
2806 {
2807 /* just putting the sum of those two into
2808 * bdp->startspaces doesn't work for Visual replace,
2809 * so we have to split the tab in two */
2810 bdp->startspaces = bdp->start_char_vcols
2811 - (bdp->start_vcol - oap->start_vcol);
2812 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2813 }
2814 }
2815 }
2816 else
2817 {
2818 prev_pend = pend;
2819 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2820 {
2821 /* Count a tab for what it's worth (if list mode not on) */
2822 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002823 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 bdp->end_vcol += incr;
2825 }
2826 if (bdp->end_vcol <= oap->end_vcol
2827 && (!is_del
2828 || oap->op_type == OP_APPEND
2829 || oap->op_type == OP_REPLACE)) /* line too short */
2830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 /* Alternative: include spaces to fill up the block.
2833 * Disadvantage: can lead to trailing spaces when the line is
2834 * short where the text is put */
2835 /* if (!is_del || oap->op_type == OP_APPEND) */
2836 if (oap->op_type == OP_APPEND || virtual_op)
2837 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002838 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 else
2840 bdp->endspaces = 0; /* replace doesn't add characters */
2841 }
2842 else if (bdp->end_vcol > oap->end_vcol)
2843 {
2844 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2845 if (!is_del && bdp->endspaces)
2846 {
2847 bdp->endspaces = incr - bdp->endspaces;
2848 if (pend != pstart)
2849 pend = prev_pend;
2850 }
2851 }
2852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 if (is_del && bdp->startspaces)
2855 pstart = prev_pstart;
2856 bdp->textlen = (int)(pend - pstart);
2857 }
2858 bdp->textcol = (colnr_T) (pstart - line);
2859 bdp->textstart = pstart;
2860}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002863 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002865 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002866op_addsub(
2867 oparg_T *oap,
2868 linenr_T Prenum1, /* Amount of add/subtract */
2869 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002871 pos_T pos;
2872 struct block_def bd;
2873 int change_cnt = 0;
2874 linenr_T amount = Prenum1;
2875
Bram Moolenaar6b731882018-11-16 20:54:47 +01002876 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002877 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002878 // the call to changed_lines().
2879#ifdef FEAT_FOLDING
2880 disable_fold_update++;
2881#endif
2882
Bram Moolenaard79e5502016-01-10 22:13:02 +01002883 if (!VIsual_active)
2884 {
2885 pos = curwin->w_cursor;
2886 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002887 {
2888#ifdef FEAT_FOLDING
2889 disable_fold_update--;
2890#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002891 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002892 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002893 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002894#ifdef FEAT_FOLDING
2895 disable_fold_update--;
2896#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002897 if (change_cnt)
2898 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2899 }
2900 else
2901 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002902 int one_change;
2903 int length;
2904 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002905
2906 if (u_save((linenr_T)(oap->start.lnum - 1),
2907 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002908 {
2909#ifdef FEAT_FOLDING
2910 disable_fold_update--;
2911#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002912 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002913 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002914
2915 pos = oap->start;
2916 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2917 {
2918 if (oap->block_mode) /* Visual block mode */
2919 {
2920 block_prep(oap, &bd, pos.lnum, FALSE);
2921 pos.col = bd.textcol;
2922 length = bd.textlen;
2923 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002924 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002925 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002926 curwin->w_cursor.col = 0;
2927 pos.col = 0;
2928 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2929 }
2930 else /* oap->motion_type == MCHAR */
2931 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002932 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002933 dec(&(oap->end));
2934 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2935 pos.col = 0;
2936 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002937 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002938 pos.col += oap->start.col;
2939 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002940 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002941 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002942 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002943 length = (int)STRLEN(ml_get(oap->end.lnum));
2944 if (oap->end.col >= length)
2945 oap->end.col = length - 1;
2946 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002947 }
2948 }
2949 one_change = do_addsub(oap->op_type, &pos, length, amount);
2950 if (one_change)
2951 {
2952 /* Remember the start position of the first change. */
2953 if (change_cnt == 0)
2954 startpos = curbuf->b_op_start;
2955 ++change_cnt;
2956 }
2957
2958#ifdef FEAT_NETBEANS_INTG
2959 if (netbeans_active() && one_change)
2960 {
2961 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2962
2963 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2964 netbeans_inserted(curbuf, pos.lnum, pos.col,
2965 &ptr[pos.col], length);
2966 }
2967#endif
2968 if (g_cmd && one_change)
2969 amount += Prenum1;
2970 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002971
2972#ifdef FEAT_FOLDING
2973 disable_fold_update--;
2974#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002975 if (change_cnt)
2976 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2977
2978 if (!change_cnt && oap->is_VIsual)
2979 /* No change: need to remove the Visual selection */
2980 redraw_curbuf_later(INVERTED);
2981
2982 /* Set '[ mark if something changed. Keep the last end
2983 * position from do_addsub(). */
2984 if (change_cnt > 0)
2985 curbuf->b_op_start = startpos;
2986
2987 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002988 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002989 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002990 }
2991}
2992
2993/*
2994 * Add or subtract 'Prenum1' from a number in a line
2995 * op_type is OP_NR_ADD or OP_NR_SUB
2996 *
2997 * Returns TRUE if some character was changed.
2998 */
2999 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003000do_addsub(
3001 int op_type,
3002 pos_T *pos,
3003 int length,
3004 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003005{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 int col;
3007 char_u *buf1;
3008 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003009 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003011 uvarnumber_T n;
3012 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 char_u *ptr;
3014 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 int todel;
3016 int dohex;
3017 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003018 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 int doalp;
3020 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003022 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003023 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003024 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02003025 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003026 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003027 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003028 pos_T startpos;
3029 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
Bram Moolenaardac13472019-09-16 21:06:21 +02003031 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
3032 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
3033 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
3034 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaard79e5502016-01-10 22:13:02 +01003036 curwin->w_cursor = *pos;
3037 ptr = ml_get(pos->lnum);
3038 col = pos->col;
3039
3040 if (*ptr == NUL)
3041 goto theend;
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 /*
3044 * First check if we are on a hexadecimal number, after the "0x".
3045 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003046 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003048 if (dobin)
3049 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003050 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003051 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003052 if (has_mbyte)
3053 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003054 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003055
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003056 if (dohex)
3057 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003058 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003059 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003060 if (has_mbyte)
3061 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003062 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003063
3064 if ( dobin
3065 && dohex
3066 && ! ((col > 0
3067 && (ptr[col] == 'X'
3068 || ptr[col] == 'x')
3069 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003070 && (!has_mbyte ||
3071 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003072 && vim_isxdigit(ptr[col + 1]))))
3073 {
3074
3075 /* In case of binary/hexadecimal pattern overlap match, rescan */
3076
Bram Moolenaard79e5502016-01-10 22:13:02 +01003077 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003078
3079 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003080 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003081 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003082 if (has_mbyte)
3083 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003084 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003085 }
3086
3087 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003088 && col > 0
3089 && (ptr[col] == 'X'
3090 || ptr[col] == 'x')
3091 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003092 && (!has_mbyte ||
3093 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003094 && vim_isxdigit(ptr[col + 1])) ||
3095 ( dobin
3096 && col > 0
3097 && (ptr[col] == 'B'
3098 || ptr[col] == 'b')
3099 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003100 && (!has_mbyte ||
3101 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003102 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003103 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003104 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003105 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003106 if (has_mbyte)
3107 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003108 }
3109 else
3110 {
3111 /*
3112 * Search forward and then backward to find the start of number.
3113 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003114 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003115
3116 while (ptr[col] != NUL
3117 && !vim_isdigit(ptr[col])
3118 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02003119 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003120
3121 while (col > 0
3122 && vim_isdigit(ptr[col - 1])
3123 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003124 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003125 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003126 if (has_mbyte)
3127 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003128 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003129 }
3130 }
3131
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003132 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003133 {
3134 while (ptr[col] != NUL && length > 0
3135 && !vim_isdigit(ptr[col])
3136 && !(doalp && ASCII_ISALPHA(ptr[col])))
3137 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02003138 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003139
3140 col += mb_len;
3141 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003142 }
3143
3144 if (length == 0)
3145 goto theend;
3146
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003147 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003148 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003149 {
3150 negative = TRUE;
3151 was_positive = FALSE;
3152 }
3153 }
3154
3155 /*
3156 * If a number was found, and saving for undo works, replace the number.
3157 */
3158 firstdigit = ptr[col];
3159 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
3160 {
3161 beep_flush();
3162 goto theend;
3163 }
3164
3165 if (doalp && ASCII_ISALPHA(firstdigit))
3166 {
3167 /* decrement or increment alphabetic character */
3168 if (op_type == OP_NR_SUB)
3169 {
3170 if (CharOrd(firstdigit) < Prenum1)
3171 {
3172 if (isupper(firstdigit))
3173 firstdigit = 'A';
3174 else
3175 firstdigit = 'a';
3176 }
3177 else
3178#ifdef EBCDIC
3179 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
3180#else
3181 firstdigit -= Prenum1;
3182#endif
3183 }
3184 else
3185 {
3186 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
3187 {
3188 if (isupper(firstdigit))
3189 firstdigit = 'Z';
3190 else
3191 firstdigit = 'z';
3192 }
3193 else
3194#ifdef EBCDIC
3195 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
3196#else
3197 firstdigit += Prenum1;
3198#endif
3199 }
3200 curwin->w_cursor.col = col;
3201 if (!did_change)
3202 startpos = curwin->w_cursor;
3203 did_change = TRUE;
3204 (void)del_char(FALSE);
3205 ins_char(firstdigit);
3206 endpos = curwin->w_cursor;
3207 curwin->w_cursor.col = col;
3208 }
3209 else
3210 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003211 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003212 && (!has_mbyte ||
3213 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003214 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003215 {
3216 /* negative number */
3217 --col;
3218 negative = TRUE;
3219 }
3220 /* get the number value (unsigned) */
3221 if (visual && VIsual_mode != 'V')
3222 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
3223 ? (int)STRLEN(ptr) - col
3224 : length);
3225
3226 vim_str2nr(ptr + col, &pre, &length,
3227 0 + (dobin ? STR2NR_BIN : 0)
3228 + (dooct ? STR2NR_OCT : 0)
3229 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003230 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003231
3232 /* ignore leading '-' for hex and octal and bin numbers */
3233 if (pre && negative)
3234 {
3235 ++col;
3236 --length;
3237 negative = FALSE;
3238 }
3239 /* add or subtract */
3240 subtract = FALSE;
3241 if (op_type == OP_NR_SUB)
3242 subtract ^= TRUE;
3243 if (negative)
3244 subtract ^= TRUE;
3245
3246 oldn = n;
3247 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003248 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003249 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003250 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003251 /* handle wraparound for decimal numbers */
3252 if (!pre)
3253 {
3254 if (subtract)
3255 {
3256 if (n > oldn)
3257 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003258 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003259 negative ^= TRUE;
3260 }
3261 }
3262 else
3263 {
3264 /* add */
3265 if (n < oldn)
3266 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003267 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003268 negative ^= TRUE;
3269 }
3270 }
3271 if (n == 0)
3272 negative = FALSE;
3273 }
3274
3275 if (visual && !was_positive && !negative && col > 0)
3276 {
3277 /* need to remove the '-' */
3278 col--;
3279 length++;
3280 }
3281
3282 /*
3283 * Delete the old number.
3284 */
3285 curwin->w_cursor.col = col;
3286 if (!did_change)
3287 startpos = curwin->w_cursor;
3288 did_change = TRUE;
3289 todel = length;
3290 c = gchar_cursor();
3291 /*
3292 * Don't include the '-' in the length, only the length of the
3293 * part after it is kept the same.
3294 */
3295 if (c == '-')
3296 --length;
3297 while (todel-- > 0)
3298 {
3299 if (c < 0x100 && isalpha(c))
3300 {
3301 if (isupper(c))
3302 hexupper = TRUE;
3303 else
3304 hexupper = FALSE;
3305 }
3306 /* del_char() will mark line needing displaying */
3307 (void)del_char(FALSE);
3308 c = gchar_cursor();
3309 }
3310
3311 /*
3312 * Prepare the leading characters in buf1[].
3313 * When there are many leading zeros it could be very long.
3314 * Allocate a bit too much.
3315 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003316 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003317 if (buf1 == NULL)
3318 goto theend;
3319 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003320 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003321 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003322 if (pre)
3323 {
3324 *ptr++ = '0';
3325 --length;
3326 }
3327 if (pre == 'b' || pre == 'B' ||
3328 pre == 'x' || pre == 'X')
3329 {
3330 *ptr++ = pre;
3331 --length;
3332 }
3333
3334 /*
3335 * Put the number characters in buf2[].
3336 */
3337 if (pre == 'b' || pre == 'B')
3338 {
3339 int i;
3340 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003341 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003342
3343 /* leading zeros */
3344 for (bit = bits; bit > 0; bit--)
3345 if ((n >> (bit - 1)) & 0x1) break;
3346
3347 for (i = 0; bit > 0; bit--)
3348 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3349
3350 buf2[i] = '\0';
3351 }
3352 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02003353 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003354 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003355 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02003356 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003357 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003358 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02003359 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003360 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003361 else
Bram Moolenaarea391762018-04-08 13:07:22 +02003362 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003363 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003364 length -= (int)STRLEN(buf2);
3365
3366 /*
3367 * Adjust number of zeros to the new number of digits, so the
3368 * total length of the number remains the same.
3369 * Don't do this when
3370 * the result may look like an octal number.
3371 */
3372 if (firstdigit == '0' && !(dooct && pre == 0))
3373 while (length-- > 0)
3374 *ptr++ = '0';
3375 *ptr = NUL;
3376 STRCAT(buf1, buf2);
3377 ins_str(buf1); /* insert the new number */
3378 vim_free(buf1);
3379 endpos = curwin->w_cursor;
3380 if (did_change && curwin->w_cursor.col)
3381 --curwin->w_cursor.col;
3382 }
3383
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003384 if (did_change)
3385 {
3386 /* set the '[ and '] marks */
3387 curbuf->b_op_start = startpos;
3388 curbuf->b_op_end = endpos;
3389 if (curbuf->b_op_end.col > 0)
3390 --curbuf->b_op_end.col;
3391 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003392
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003393theend:
3394 if (visual)
3395 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003396 else if (did_change)
3397 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003398
Bram Moolenaard79e5502016-01-10 22:13:02 +01003399 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400}
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#if defined(FEAT_CLIPBOARD) || defined(PROTO)
3403/*
3404 * SELECTION / PRIMARY ('*')
3405 *
3406 * Text selection stuff that uses the GUI selection register '*'. When using a
3407 * GUI this may be text from another window, otherwise it is the last text we
3408 * had highlighted with VIsual mode. With mouse support, clicking the middle
3409 * button performs the paste, otherwise you will need to do <"*p>. "
3410 * If not under X, it is synonymous with the clipboard register '+'.
3411 *
3412 * X CLIPBOARD ('+')
3413 *
3414 * Text selection stuff that uses the GUI clipboard register '+'.
3415 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
3416 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
3417 * otherwise you will need to do <"+p>. "
3418 * If not under X, it is synonymous with the selection register '*'.
3419 */
3420
3421/*
3422 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003423 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * only exist while the owning application exists, so we write to the
3425 * permanent (while X runs) store CUT_BUFFER0.
3426 * Dump the CLIPBOARD selection if we own it (it's logically the more
3427 * 'permanent' of the two), otherwise the PRIMARY one.
3428 * For now, use a hard-coded sanity limit of 1Mb of data.
3429 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003430#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003432x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 Display *dpy;
3435 char_u *str = NULL;
3436 long_u len = 0;
3437 int motion_type = -1;
3438
3439# ifdef FEAT_GUI
3440 if (gui.in_use)
3441 dpy = X_DISPLAY;
3442 else
3443# endif
3444# ifdef FEAT_XCLIPBOARD
3445 dpy = xterm_dpy;
3446# else
3447 return;
3448# endif
3449
3450 /* Get selection to export */
3451 if (clip_plus.owned)
3452 motion_type = clip_convert_selection(&str, &len, &clip_plus);
3453 else if (clip_star.owned)
3454 motion_type = clip_convert_selection(&str, &len, &clip_star);
3455
3456 /* Check it's OK */
3457 if (dpy != NULL && str != NULL && motion_type >= 0
3458 && len < 1024*1024 && len > 0)
3459 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003460 int ok = TRUE;
3461
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003462 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
3463 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
3464 * encoding conversion usually doesn't work, so keep the text as-is.
3465 */
3466 if (has_mbyte)
3467 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003468 vimconv_T vc;
3469
3470 vc.vc_type = CONV_NONE;
3471 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
3472 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003473 int intlen = len;
3474 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003475
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003476 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003477 conv_str = string_convert(&vc, str, &intlen);
3478 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003479 if (conv_str != NULL)
3480 {
3481 vim_free(str);
3482 str = conv_str;
3483 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003484 else
3485 {
3486 ok = FALSE;
3487 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003488 convert_setup(&vc, NULL, NULL);
3489 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003490 else
3491 {
3492 ok = FALSE;
3493 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003494 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003495
3496 /* Do not store the string if conversion failed. Better to use any
3497 * other selection than garbled text. */
3498 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003499 {
3500 XStoreBuffer(dpy, (char *)str, (int)len, 0);
3501 XFlush(dpy);
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504
3505 vim_free(str);
3506}
3507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#endif /* FEAT_CLIPBOARD || PROTO */
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003511clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
3513 vim_memset(oap, 0, sizeof(oparg_T));
3514}
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003517 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 *
3519 * "Words" are counted by looking for boundaries between non-space and
3520 * space characters. (it seems to produce results that match 'wc'.)
3521 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003522 * Return value is byte count; word count for the line is added to "*wc".
3523 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 *
3525 * The function will only examine the first "limit" characters in the
3526 * line, stopping if it encounters an end-of-line (NUL byte). In that
3527 * case, eol_size will be added to the character count to account for
3528 * the size of the EOL character.
3529 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003530 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003531line_count_info(
3532 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003533 varnumber_T *wc,
3534 varnumber_T *cc,
3535 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003536 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003538 varnumber_T i;
3539 varnumber_T words = 0;
3540 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 int is_word = 0;
3542
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003543 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 if (is_word)
3546 {
3547 if (vim_isspace(line[i]))
3548 {
3549 words++;
3550 is_word = 0;
3551 }
3552 }
3553 else if (!vim_isspace(line[i]))
3554 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003555 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003556 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558
3559 if (is_word)
3560 words++;
3561 *wc += words;
3562
3563 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003564 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003567 chars += eol_size;
3568 }
3569 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return i;
3571}
3572
3573/*
3574 * Give some info about the position of the cursor (for "g CTRL-G").
3575 * In Visual mode, give some info about the selected region. (In this case,
3576 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003577 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 */
3579 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003580cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003583 char_u buf1[50];
3584 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003586 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003587 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003588 varnumber_T byte_count_cursor = 0;
3589 varnumber_T char_count = 0;
3590 varnumber_T char_count_cursor = 0;
3591 varnumber_T word_count = 0;
3592 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003593 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003594 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 long line_count_selected = 0;
3596 pos_T min_pos, max_pos;
3597 oparg_T oparg;
3598 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600 /*
3601 * Compute the length of the file in characters.
3602 */
3603 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3604 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003605 if (dict == NULL)
3606 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003607 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003608 return;
3609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611 else
3612 {
3613 if (get_fileformat(curbuf) == EOL_DOS)
3614 eol_size = 2;
3615 else
3616 eol_size = 1;
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (VIsual_active)
3619 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003620 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 min_pos = VIsual;
3623 max_pos = curwin->w_cursor;
3624 }
3625 else
3626 {
3627 min_pos = curwin->w_cursor;
3628 max_pos = VIsual;
3629 }
3630 if (*p_sel == 'e' && max_pos.col > 0)
3631 --max_pos.col;
3632
3633 if (VIsual_mode == Ctrl_V)
3634 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003635#ifdef FEAT_LINEBREAK
3636 char_u * saved_sbr = p_sbr;
3637
3638 /* Make 'sbr' empty for a moment to get the correct size. */
3639 p_sbr = empty_option;
3640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 oparg.is_VIsual = 1;
3642 oparg.block_mode = TRUE;
3643 oparg.op_type = OP_NOP;
3644 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003645 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003646#ifdef FEAT_LINEBREAK
3647 p_sbr = saved_sbr;
3648#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003649 if (curwin->w_curswant == MAXCOL)
3650 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 /* Swap the start, end vcol if needed */
3652 if (oparg.end_vcol < oparg.start_vcol)
3653 {
3654 oparg.end_vcol += oparg.start_vcol;
3655 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3656 oparg.end_vcol -= oparg.start_vcol;
3657 }
3658 }
3659 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3663 {
3664 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003665 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
3667 ui_breakcheck();
3668 if (got_int)
3669 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003670 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 /* Do extra processing for VIsual mode. */
3674 if (VIsual_active
3675 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3676 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003677 char_u *s = NULL;
3678 long len = 0L;
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 switch (VIsual_mode)
3681 {
3682 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003686 s = bd.textstart;
3687 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 break;
3689 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003690 s = ml_get(lnum);
3691 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 break;
3693 case 'v':
3694 {
3695 colnr_T start_col = (lnum == min_pos.lnum)
3696 ? min_pos.col : 0;
3697 colnr_T end_col = (lnum == max_pos.lnum)
3698 ? max_pos.col - start_col + 1 : MAXCOL;
3699
Bram Moolenaardef9e822004-12-31 20:58:58 +00003700 s = ml_get(lnum) + start_col;
3701 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 break;
3704 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003705 if (s != NULL)
3706 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003707 byte_count_cursor += line_count_info(s, &word_count_cursor,
3708 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003709 if (lnum == curbuf->b_ml.ml_line_count
3710 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003711 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003712 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003713 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
3718 /* In non-visual mode, check for the line the cursor is on */
3719 if (lnum == curwin->w_cursor.lnum)
3720 {
3721 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003722 char_count_cursor += char_count;
3723 byte_count_cursor = byte_count +
3724 line_count_info(ml_get(lnum),
3725 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003726 (varnumber_T)(curwin->w_cursor.col + 1),
3727 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729 }
3730 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003731 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003732 &char_count, (varnumber_T)MAXCOL,
3733 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735
3736 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003737 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003738 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
Bram Moolenaared767a22016-01-03 22:49:16 +01003740 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003742 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003744 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3745 {
3746 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3747 &max_pos.col);
3748 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3749 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3750 }
3751 else
3752 buf1[0] = NUL;
3753
3754 if (char_count_cursor == byte_count_cursor
3755 && char_count == byte_count)
3756 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003757 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003758 buf1, line_count_selected,
3759 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003760 (long_long_T)word_count_cursor,
3761 (long_long_T)word_count,
3762 (long_long_T)byte_count_cursor,
3763 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003764 else
3765 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003766 _("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 +01003767 buf1, line_count_selected,
3768 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003769 (long_long_T)word_count_cursor,
3770 (long_long_T)word_count,
3771 (long_long_T)char_count_cursor,
3772 (long_long_T)char_count,
3773 (long_long_T)byte_count_cursor,
3774 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003777 {
3778 p = ml_get_curline();
3779 validate_virtcol();
3780 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3781 (int)curwin->w_virtcol + 1);
3782 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3783 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaared767a22016-01-03 22:49:16 +01003785 if (char_count_cursor == byte_count_cursor
3786 && char_count == byte_count)
3787 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003788 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003789 (char *)buf1, (char *)buf2,
3790 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003792 (long_long_T)word_count_cursor, (long_long_T)word_count,
3793 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003794 else
3795 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003796 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003797 (char *)buf1, (char *)buf2,
3798 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003799 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003800 (long_long_T)word_count_cursor, (long_long_T)word_count,
3801 (long_long_T)char_count_cursor, (long_long_T)char_count,
3802 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003803 }
3804 }
3805
Bram Moolenaared767a22016-01-03 22:49:16 +01003806 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003807 if (dict == NULL && bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01003808 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003809 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003810 if (dict == NULL)
3811 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003812 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003813 p = p_shm;
3814 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003815 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003816 p_shm = p;
3817 }
3818 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003819#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003820 if (dict != NULL)
3821 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003822 dict_add_number(dict, "words", word_count);
3823 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003824 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003825 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3826 byte_count_cursor);
3827 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3828 char_count_cursor);
3829 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3830 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003834
3835/*
3836 * Handle indent and format operators and visual mode ":".
3837 */
3838 static void
3839op_colon(oparg_T *oap)
3840{
3841 stuffcharReadbuff(':');
3842 if (oap->is_VIsual)
3843 stuffReadbuff((char_u *)"'<,'>");
3844 else
3845 {
3846 // Make the range look nice, so it can be repeated.
3847 if (oap->start.lnum == curwin->w_cursor.lnum)
3848 stuffcharReadbuff('.');
3849 else
3850 stuffnumReadbuff((long)oap->start.lnum);
3851 if (oap->end.lnum != oap->start.lnum)
3852 {
3853 stuffcharReadbuff(',');
3854 if (oap->end.lnum == curwin->w_cursor.lnum)
3855 stuffcharReadbuff('.');
3856 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3857 stuffcharReadbuff('$');
3858 else if (oap->start.lnum == curwin->w_cursor.lnum)
3859 {
3860 stuffReadbuff((char_u *)".+");
3861 stuffnumReadbuff((long)oap->line_count - 1);
3862 }
3863 else
3864 stuffnumReadbuff((long)oap->end.lnum);
3865 }
3866 }
3867 if (oap->op_type != OP_COLON)
3868 stuffReadbuff((char_u *)"!");
3869 if (oap->op_type == OP_INDENT)
3870 {
3871#ifndef FEAT_CINDENT
3872 if (*get_equalprg() == NUL)
3873 stuffReadbuff((char_u *)"indent");
3874 else
3875#endif
3876 stuffReadbuff(get_equalprg());
3877 stuffReadbuff((char_u *)"\n");
3878 }
3879 else if (oap->op_type == OP_FORMAT)
3880 {
3881 if (*curbuf->b_p_fp != NUL)
3882 stuffReadbuff(curbuf->b_p_fp);
3883 else if (*p_fp != NUL)
3884 stuffReadbuff(p_fp);
3885 else
3886 stuffReadbuff((char_u *)"fmt");
3887 stuffReadbuff((char_u *)"\n']");
3888 }
3889
3890 // do_cmdline() does the rest
3891}
3892
3893/*
3894 * Handle the "g@" operator: call 'operatorfunc'.
3895 */
3896 static void
3897op_function(oparg_T *oap UNUSED)
3898{
3899#ifdef FEAT_EVAL
3900 typval_T argv[2];
3901 int save_virtual_op = virtual_op;
3902
3903 if (*p_opfunc == NUL)
3904 emsg(_("E774: 'operatorfunc' is empty"));
3905 else
3906 {
3907 // Set '[ and '] marks to text to be operated on.
3908 curbuf->b_op_start = oap->start;
3909 curbuf->b_op_end = oap->end;
3910 if (oap->motion_type != MLINE && !oap->inclusive)
3911 // Exclude the end position.
3912 decl(&curbuf->b_op_end);
3913
3914 argv[0].v_type = VAR_STRING;
3915 if (oap->block_mode)
3916 argv[0].vval.v_string = (char_u *)"block";
3917 else if (oap->motion_type == MLINE)
3918 argv[0].vval.v_string = (char_u *)"line";
3919 else
3920 argv[0].vval.v_string = (char_u *)"char";
3921 argv[1].v_type = VAR_UNKNOWN;
3922
3923 // Reset virtual_op so that 'virtualedit' can be changed in the
3924 // function.
3925 virtual_op = MAYBE;
3926
3927 (void)call_func_retnr(p_opfunc, 1, argv);
3928
3929 virtual_op = save_virtual_op;
3930 }
3931#else
3932 emsg(_("E775: Eval feature not available"));
3933#endif
3934}
3935
3936/*
3937 * Calculate start/end virtual columns for operating in block mode.
3938 */
3939 static void
3940get_op_vcol(
3941 oparg_T *oap,
3942 colnr_T redo_VIsual_vcol,
3943 int initial) // when TRUE adjust position for 'selectmode'
3944{
3945 colnr_T start, end;
3946
3947 if (VIsual_mode != Ctrl_V
3948 || (!initial && oap->end.col < curwin->w_width))
3949 return;
3950
3951 oap->block_mode = TRUE;
3952
3953 // prevent from moving onto a trail byte
3954 if (has_mbyte)
3955 mb_adjustpos(curwin->w_buffer, &oap->end);
3956
3957 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3958
3959 if (!redo_VIsual_busy)
3960 {
3961 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3962
3963 if (start < oap->start_vcol)
3964 oap->start_vcol = start;
3965 if (end > oap->end_vcol)
3966 {
3967 if (initial && *p_sel == 'e' && start >= 1
3968 && start - 1 >= oap->end_vcol)
3969 oap->end_vcol = start - 1;
3970 else
3971 oap->end_vcol = end;
3972 }
3973 }
3974
3975 // if '$' was used, get oap->end_vcol from longest line
3976 if (curwin->w_curswant == MAXCOL)
3977 {
3978 curwin->w_cursor.col = MAXCOL;
3979 oap->end_vcol = 0;
3980 for (curwin->w_cursor.lnum = oap->start.lnum;
3981 curwin->w_cursor.lnum <= oap->end.lnum;
3982 ++curwin->w_cursor.lnum)
3983 {
3984 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3985 if (end > oap->end_vcol)
3986 oap->end_vcol = end;
3987 }
3988 }
3989 else if (redo_VIsual_busy)
3990 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3991 // Correct oap->end.col and oap->start.col to be the
3992 // upper-left and lower-right corner of the block area.
3993 //
3994 // (Actually, this does convert column positions into character
3995 // positions)
3996 curwin->w_cursor.lnum = oap->end.lnum;
3997 coladvance(oap->end_vcol);
3998 oap->end = curwin->w_cursor;
3999
4000 curwin->w_cursor = oap->start;
4001 coladvance(oap->start_vcol);
4002 oap->start = curwin->w_cursor;
4003}
4004
4005/*
4006 * Handle an operator after Visual mode or when the movement is finished.
4007 * "gui_yank" is true when yanking text for the clipboard.
4008 */
4009 void
4010do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
4011{
4012 oparg_T *oap = cap->oap;
4013 pos_T old_cursor;
4014 int empty_region_error;
4015 int restart_edit_save;
4016#ifdef FEAT_LINEBREAK
4017 int lbr_saved = curwin->w_p_lbr;
4018#endif
4019
4020 // The visual area is remembered for redo
4021 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
4022 static linenr_T redo_VIsual_line_count; // number of lines
4023 static colnr_T redo_VIsual_vcol; // number of cols or end column
4024 static long redo_VIsual_count; // count for Visual operator
4025 static int redo_VIsual_arg; // extra argument
4026 int include_line_break = FALSE;
4027
4028#if defined(FEAT_CLIPBOARD)
4029 // Yank the visual area into the GUI selection register before we operate
4030 // on it and lose it forever.
4031 // Don't do it if a specific register was specified, so that ""x"*P works.
4032 // This could call do_pending_operator() recursively, but that's OK
4033 // because gui_yank will be TRUE for the nested call.
4034 if ((clip_star.available || clip_plus.available)
4035 && oap->op_type != OP_NOP
4036 && !gui_yank
4037 && VIsual_active
4038 && !redo_VIsual_busy
4039 && oap->regname == 0)
4040 clip_auto_select();
4041#endif
4042 old_cursor = curwin->w_cursor;
4043
4044 // If an operation is pending, handle it...
4045 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
4046 {
4047 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
4048 // for the clipboard.
4049 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
4050
4051#ifdef FEAT_LINEBREAK
4052 // Avoid a problem with unwanted linebreaks in block mode.
4053 if (curwin->w_p_lbr)
4054 curwin->w_valid &= ~VALID_VIRTCOL;
4055 curwin->w_p_lbr = FALSE;
4056#endif
4057 oap->is_VIsual = VIsual_active;
4058 if (oap->motion_force == 'V')
4059 oap->motion_type = MLINE;
4060 else if (oap->motion_force == 'v')
4061 {
4062 // If the motion was linewise, "inclusive" will not have been set.
4063 // Use "exclusive" to be consistent. Makes "dvj" work nice.
4064 if (oap->motion_type == MLINE)
4065 oap->inclusive = FALSE;
4066 // If the motion already was characterwise, toggle "inclusive"
4067 else if (oap->motion_type == MCHAR)
4068 oap->inclusive = !oap->inclusive;
4069 oap->motion_type = MCHAR;
4070 }
4071 else if (oap->motion_force == Ctrl_V)
4072 {
4073 // Change line- or characterwise motion into Visual block mode.
4074 if (!VIsual_active)
4075 {
4076 VIsual_active = TRUE;
4077 VIsual = oap->start;
4078 }
4079 VIsual_mode = Ctrl_V;
4080 VIsual_select = FALSE;
4081 VIsual_reselect = FALSE;
4082 }
4083
4084 // Only redo yank when 'y' flag is in 'cpoptions'.
4085 // Never redo "zf" (define fold).
4086 if ((redo_yank || oap->op_type != OP_YANK)
4087 && ((!VIsual_active || oap->motion_force)
4088 // Also redo Operator-pending Visual mode mappings
4089 || (VIsual_active && cap->cmdchar == ':'
4090 && oap->op_type != OP_COLON))
4091 && cap->cmdchar != 'D'
4092#ifdef FEAT_FOLDING
4093 && oap->op_type != OP_FOLD
4094 && oap->op_type != OP_FOLDOPEN
4095 && oap->op_type != OP_FOLDOPENREC
4096 && oap->op_type != OP_FOLDCLOSE
4097 && oap->op_type != OP_FOLDCLOSEREC
4098 && oap->op_type != OP_FOLDDEL
4099 && oap->op_type != OP_FOLDDELREC
4100#endif
4101 )
4102 {
4103 prep_redo(oap->regname, cap->count0,
4104 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
4105 oap->motion_force, cap->cmdchar, cap->nchar);
4106 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
4107 {
4108 // If 'cpoptions' does not contain 'r', insert the search
4109 // pattern to really repeat the same command.
4110 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
4111 AppendToRedobuffLit(cap->searchbuf, -1);
4112 AppendToRedobuff(NL_STR);
4113 }
4114 else if (cap->cmdchar == ':')
4115 {
4116 // do_cmdline() has stored the first typed line in
4117 // "repeat_cmdline". When several lines are typed repeating
4118 // won't be possible.
4119 if (repeat_cmdline == NULL)
4120 ResetRedobuff();
4121 else
4122 {
4123 AppendToRedobuffLit(repeat_cmdline, -1);
4124 AppendToRedobuff(NL_STR);
4125 VIM_CLEAR(repeat_cmdline);
4126 }
4127 }
4128 }
4129
4130 if (redo_VIsual_busy)
4131 {
4132 // Redo of an operation on a Visual area. Use the same size from
4133 // redo_VIsual_line_count and redo_VIsual_vcol.
4134 oap->start = curwin->w_cursor;
4135 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
4136 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4137 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4138 VIsual_mode = redo_VIsual_mode;
4139 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
4140 {
4141 if (VIsual_mode == 'v')
4142 {
4143 if (redo_VIsual_line_count <= 1)
4144 {
4145 validate_virtcol();
4146 curwin->w_curswant =
4147 curwin->w_virtcol + redo_VIsual_vcol - 1;
4148 }
4149 else
4150 curwin->w_curswant = redo_VIsual_vcol;
4151 }
4152 else
4153 {
4154 curwin->w_curswant = MAXCOL;
4155 }
4156 coladvance(curwin->w_curswant);
4157 }
4158 cap->count0 = redo_VIsual_count;
4159 if (redo_VIsual_count != 0)
4160 cap->count1 = redo_VIsual_count;
4161 else
4162 cap->count1 = 1;
4163 }
4164 else if (VIsual_active)
4165 {
4166 if (!gui_yank)
4167 {
4168 // Save the current VIsual area for '< and '> marks, and "gv"
4169 curbuf->b_visual.vi_start = VIsual;
4170 curbuf->b_visual.vi_end = curwin->w_cursor;
4171 curbuf->b_visual.vi_mode = VIsual_mode;
4172 restore_visual_mode();
4173 curbuf->b_visual.vi_curswant = curwin->w_curswant;
4174# ifdef FEAT_EVAL
4175 curbuf->b_visual_mode_eval = VIsual_mode;
4176# endif
4177 }
4178
4179 // In Select mode, a linewise selection is operated upon like a
4180 // characterwise selection.
4181 // Special case: gH<Del> deletes the last line.
4182 if (VIsual_select && VIsual_mode == 'V'
4183 && cap->oap->op_type != OP_DELETE)
4184 {
4185 if (LT_POS(VIsual, curwin->w_cursor))
4186 {
4187 VIsual.col = 0;
4188 curwin->w_cursor.col =
4189 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
4190 }
4191 else
4192 {
4193 curwin->w_cursor.col = 0;
4194 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
4195 }
4196 VIsual_mode = 'v';
4197 }
4198 // If 'selection' is "exclusive", backup one character for
4199 // charwise selections.
4200 else if (VIsual_mode == 'v')
4201 include_line_break = unadjust_for_sel();
4202
4203 oap->start = VIsual;
4204 if (VIsual_mode == 'V')
4205 {
4206 oap->start.col = 0;
4207 oap->start.coladd = 0;
4208 }
4209 }
4210
4211 // Set oap->start to the first position of the operated text, oap->end
4212 // to the end of the operated text. w_cursor is equal to oap->start.
4213 if (LT_POS(oap->start, curwin->w_cursor))
4214 {
4215#ifdef FEAT_FOLDING
4216 // Include folded lines completely.
4217 if (!VIsual_active)
4218 {
4219 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
4220 oap->start.col = 0;
4221 if ((curwin->w_cursor.col > 0 || oap->inclusive)
4222 && hasFolding(curwin->w_cursor.lnum, NULL,
4223 &curwin->w_cursor.lnum))
4224 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
4225 }
4226#endif
4227 oap->end = curwin->w_cursor;
4228 curwin->w_cursor = oap->start;
4229
4230 // w_virtcol may have been updated; if the cursor goes back to its
4231 // previous position w_virtcol becomes invalid and isn't updated
4232 // automatically.
4233 curwin->w_valid &= ~VALID_VIRTCOL;
4234 }
4235 else
4236 {
4237#ifdef FEAT_FOLDING
4238 // Include folded lines completely.
4239 if (!VIsual_active && oap->motion_type == MLINE)
4240 {
4241 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
4242 NULL))
4243 curwin->w_cursor.col = 0;
4244 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
4245 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
4246 }
4247#endif
4248 oap->end = oap->start;
4249 oap->start = curwin->w_cursor;
4250 }
4251
4252 // Just in case lines were deleted that make the position invalid.
4253 check_pos(curwin->w_buffer, &oap->end);
4254 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
4255
4256 // Set "virtual_op" before resetting VIsual_active.
4257 virtual_op = virtual_active();
4258
4259 if (VIsual_active || redo_VIsual_busy)
4260 {
4261 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
4262
4263 if (!redo_VIsual_busy && !gui_yank)
4264 {
4265 // Prepare to reselect and redo Visual: this is based on the
4266 // size of the Visual text
4267 resel_VIsual_mode = VIsual_mode;
4268 if (curwin->w_curswant == MAXCOL)
4269 resel_VIsual_vcol = MAXCOL;
4270 else
4271 {
4272 if (VIsual_mode != Ctrl_V)
4273 getvvcol(curwin, &(oap->end),
4274 NULL, NULL, &oap->end_vcol);
4275 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
4276 {
4277 if (VIsual_mode != Ctrl_V)
4278 getvvcol(curwin, &(oap->start),
4279 &oap->start_vcol, NULL, NULL);
4280 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
4281 }
4282 else
4283 resel_VIsual_vcol = oap->end_vcol;
4284 }
4285 resel_VIsual_line_count = oap->line_count;
4286 }
4287
4288 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
4289 if ((redo_yank || oap->op_type != OP_YANK)
4290 && oap->op_type != OP_COLON
4291#ifdef FEAT_FOLDING
4292 && oap->op_type != OP_FOLD
4293 && oap->op_type != OP_FOLDOPEN
4294 && oap->op_type != OP_FOLDOPENREC
4295 && oap->op_type != OP_FOLDCLOSE
4296 && oap->op_type != OP_FOLDCLOSEREC
4297 && oap->op_type != OP_FOLDDEL
4298 && oap->op_type != OP_FOLDDELREC
4299#endif
4300 && oap->motion_force == NUL
4301 )
4302 {
4303 // Prepare for redoing. Only use the nchar field for "r",
4304 // otherwise it might be the second char of the operator.
4305 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4306 || cap->nchar == 'N'))
4307 prep_redo(oap->regname, cap->count0,
4308 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
4309 oap->motion_force, cap->cmdchar, cap->nchar);
4310 else if (cap->cmdchar != ':')
4311 {
4312 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4313
4314 // reverse what nv_replace() did
4315 if (nchar == REPLACE_CR_NCHAR)
4316 nchar = CAR;
4317 else if (nchar == REPLACE_NL_NCHAR)
4318 nchar = NL;
4319 prep_redo(oap->regname, 0L, NUL, 'v',
4320 get_op_char(oap->op_type),
4321 get_extra_op_char(oap->op_type),
4322 nchar);
4323 }
4324 if (!redo_VIsual_busy)
4325 {
4326 redo_VIsual_mode = resel_VIsual_mode;
4327 redo_VIsual_vcol = resel_VIsual_vcol;
4328 redo_VIsual_line_count = resel_VIsual_line_count;
4329 redo_VIsual_count = cap->count0;
4330 redo_VIsual_arg = cap->arg;
4331 }
4332 }
4333
4334 // oap->inclusive defaults to TRUE.
4335 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4336 // FALSE. This makes "d}P" and "v}dP" work the same.
4337 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4338 oap->inclusive = TRUE;
4339 if (VIsual_mode == 'V')
4340 oap->motion_type = MLINE;
4341 else
4342 {
4343 oap->motion_type = MCHAR;
4344 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4345 && (include_line_break || !virtual_op))
4346 {
4347 oap->inclusive = FALSE;
4348 // Try to include the newline, unless it's an operator
4349 // that works on lines only.
4350 if (*p_sel != 'o'
4351 && !op_on_lines(oap->op_type)
4352 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4353 {
4354 ++oap->end.lnum;
4355 oap->end.col = 0;
4356 oap->end.coladd = 0;
4357 ++oap->line_count;
4358 }
4359 }
4360 }
4361
4362 redo_VIsual_busy = FALSE;
4363
4364 // Switch Visual off now, so screen updating does
4365 // not show inverted text when the screen is redrawn.
4366 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4367 // no screen redraw, so it is done here to remove the inverted
4368 // part.
4369 if (!gui_yank)
4370 {
4371 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004372 setmouse();
4373 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004374 may_clear_cmdline();
4375 if ((oap->op_type == OP_YANK
4376 || oap->op_type == OP_COLON
4377 || oap->op_type == OP_FUNCTION
4378 || oap->op_type == OP_FILTER)
4379 && oap->motion_force == NUL)
4380 {
4381#ifdef FEAT_LINEBREAK
4382 // make sure redrawing is correct
4383 curwin->w_p_lbr = lbr_saved;
4384#endif
4385 redraw_curbuf_later(INVERTED);
4386 }
4387 }
4388 }
4389
4390 // Include the trailing byte of a multi-byte char.
4391 if (has_mbyte && oap->inclusive)
4392 {
4393 int l;
4394
4395 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4396 if (l > 1)
4397 oap->end.col += l - 1;
4398 }
4399 curwin->w_set_curswant = TRUE;
4400
4401 // oap->empty is set when start and end are the same. The inclusive
4402 // flag affects this too, unless yanking and the end is on a NUL.
4403 oap->empty = (oap->motion_type == MCHAR
4404 && (!oap->inclusive
4405 || (oap->op_type == OP_YANK
4406 && gchar_pos(&oap->end) == NUL))
4407 && EQUAL_POS(oap->start, oap->end)
4408 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4409 // For delete, change and yank, it's an error to operate on an
4410 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4411 empty_region_error = (oap->empty
4412 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4413
4414 // Force a redraw when operating on an empty Visual region, when
4415 // 'modifiable is off or creating a fold.
4416 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4417#ifdef FEAT_FOLDING
4418 || oap->op_type == OP_FOLD
4419#endif
4420 ))
4421 {
4422#ifdef FEAT_LINEBREAK
4423 curwin->w_p_lbr = lbr_saved;
4424#endif
4425 redraw_curbuf_later(INVERTED);
4426 }
4427
4428 // If the end of an operator is in column one while oap->motion_type
4429 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4430 // character in the previous line. If op_start is on or before the
4431 // first non-blank in the line, the operator becomes linewise
4432 // (strange, but that's the way vi does it).
4433 if ( oap->motion_type == MCHAR
4434 && oap->inclusive == FALSE
4435 && !(cap->retval & CA_NO_ADJ_OP_END)
4436 && oap->end.col == 0
4437 && (!oap->is_VIsual || *p_sel == 'o')
4438 && !oap->block_mode
4439 && oap->line_count > 1)
4440 {
4441 oap->end_adjusted = TRUE; // remember that we did this
4442 --oap->line_count;
4443 --oap->end.lnum;
4444 if (inindent(0))
4445 oap->motion_type = MLINE;
4446 else
4447 {
4448 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
4449 if (oap->end.col)
4450 {
4451 --oap->end.col;
4452 oap->inclusive = TRUE;
4453 }
4454 }
4455 }
4456 else
4457 oap->end_adjusted = FALSE;
4458
4459 switch (oap->op_type)
4460 {
4461 case OP_LSHIFT:
4462 case OP_RSHIFT:
4463 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4464 auto_format(FALSE, TRUE);
4465 break;
4466
4467 case OP_JOIN_NS:
4468 case OP_JOIN:
4469 if (oap->line_count < 2)
4470 oap->line_count = 2;
4471 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4472 curbuf->b_ml.ml_line_count)
4473 beep_flush();
4474 else
4475 {
4476 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4477 TRUE, TRUE, TRUE);
4478 auto_format(FALSE, TRUE);
4479 }
4480 break;
4481
4482 case OP_DELETE:
4483 VIsual_reselect = FALSE; // don't reselect now
4484 if (empty_region_error)
4485 {
4486 vim_beep(BO_OPER);
4487 CancelRedo();
4488 }
4489 else
4490 {
4491 (void)op_delete(oap);
4492 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
4493 u_save_cursor(); // cursor line wasn't saved yet
4494 auto_format(FALSE, TRUE);
4495 }
4496 break;
4497
4498 case OP_YANK:
4499 if (empty_region_error)
4500 {
4501 if (!gui_yank)
4502 {
4503 vim_beep(BO_OPER);
4504 CancelRedo();
4505 }
4506 }
4507 else
4508 {
4509#ifdef FEAT_LINEBREAK
4510 curwin->w_p_lbr = lbr_saved;
4511#endif
4512 (void)op_yank(oap, FALSE, !gui_yank);
4513 }
4514 check_cursor_col();
4515 break;
4516
4517 case OP_CHANGE:
4518 VIsual_reselect = FALSE; // don't reselect now
4519 if (empty_region_error)
4520 {
4521 vim_beep(BO_OPER);
4522 CancelRedo();
4523 }
4524 else
4525 {
4526 // This is a new edit command, not a restart. Need to
4527 // remember it to make 'insertmode' work with mappings for
4528 // Visual mode. But do this only once and not when typed and
4529 // 'insertmode' isn't set.
4530 if (p_im || !KeyTyped)
4531 restart_edit_save = restart_edit;
4532 else
4533 restart_edit_save = 0;
4534 restart_edit = 0;
4535#ifdef FEAT_LINEBREAK
4536 // Restore linebreak, so that when the user edits it looks as
4537 // before.
4538 if (curwin->w_p_lbr != lbr_saved)
4539 {
4540 curwin->w_p_lbr = lbr_saved;
4541 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4542 }
4543#endif
4544 // Reset finish_op now, don't want it set inside edit().
4545 finish_op = FALSE;
4546 if (op_change(oap)) // will call edit()
4547 cap->retval |= CA_COMMAND_BUSY;
4548 if (restart_edit == 0)
4549 restart_edit = restart_edit_save;
4550 }
4551 break;
4552
4553 case OP_FILTER:
4554 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4555 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4556 else
4557 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4558 // FALLTHROUGH
4559
4560 case OP_INDENT:
4561 case OP_COLON:
4562
4563#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4564 // If 'equalprg' is empty, do the indenting internally.
4565 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4566 {
4567# ifdef FEAT_LISP
4568 if (curbuf->b_p_lisp)
4569 {
4570 op_reindent(oap, get_lisp_indent);
4571 break;
4572 }
4573# endif
4574# ifdef FEAT_CINDENT
4575 op_reindent(oap,
4576# ifdef FEAT_EVAL
4577 *curbuf->b_p_inde != NUL ? get_expr_indent :
4578# endif
4579 get_c_indent);
4580 break;
4581# endif
4582 }
4583#endif
4584
4585 op_colon(oap);
4586 break;
4587
4588 case OP_TILDE:
4589 case OP_UPPER:
4590 case OP_LOWER:
4591 case OP_ROT13:
4592 if (empty_region_error)
4593 {
4594 vim_beep(BO_OPER);
4595 CancelRedo();
4596 }
4597 else
4598 op_tilde(oap);
4599 check_cursor_col();
4600 break;
4601
4602 case OP_FORMAT:
4603#if defined(FEAT_EVAL)
4604 if (*curbuf->b_p_fex != NUL)
4605 op_formatexpr(oap); // use expression
4606 else
4607#endif
4608 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
4609 op_colon(oap); // use external command
4610 else
4611 op_format(oap, FALSE); // use internal function
4612 break;
4613
4614 case OP_FORMAT2:
4615 op_format(oap, TRUE); // use internal function
4616 break;
4617
4618 case OP_FUNCTION:
4619#ifdef FEAT_LINEBREAK
4620 // Restore linebreak, so that when the user edits it looks as
4621 // before.
4622 curwin->w_p_lbr = lbr_saved;
4623#endif
4624 op_function(oap); // call 'operatorfunc'
4625 break;
4626
4627 case OP_INSERT:
4628 case OP_APPEND:
4629 VIsual_reselect = FALSE; // don't reselect now
4630 if (empty_region_error)
4631 {
4632 vim_beep(BO_OPER);
4633 CancelRedo();
4634 }
4635 else
4636 {
4637 // This is a new edit command, not a restart. Need to
4638 // remember it to make 'insertmode' work with mappings for
4639 // Visual mode. But do this only once.
4640 restart_edit_save = restart_edit;
4641 restart_edit = 0;
4642#ifdef FEAT_LINEBREAK
4643 // Restore linebreak, so that when the user edits it looks as
4644 // before.
4645 if (curwin->w_p_lbr != lbr_saved)
4646 {
4647 curwin->w_p_lbr = lbr_saved;
4648 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4649 }
4650#endif
4651 op_insert(oap, cap->count1);
4652#ifdef FEAT_LINEBREAK
4653 // Reset linebreak, so that formatting works correctly.
4654 curwin->w_p_lbr = FALSE;
4655#endif
4656
4657 // TODO: when inserting in several lines, should format all
4658 // the lines.
4659 auto_format(FALSE, TRUE);
4660
4661 if (restart_edit == 0)
4662 restart_edit = restart_edit_save;
4663 else
4664 cap->retval |= CA_COMMAND_BUSY;
4665 }
4666 break;
4667
4668 case OP_REPLACE:
4669 VIsual_reselect = FALSE; // don't reselect now
4670 if (empty_region_error)
4671 {
4672 vim_beep(BO_OPER);
4673 CancelRedo();
4674 }
4675 else
4676 {
4677#ifdef FEAT_LINEBREAK
4678 // Restore linebreak, so that when the user edits it looks as
4679 // before.
4680 if (curwin->w_p_lbr != lbr_saved)
4681 {
4682 curwin->w_p_lbr = lbr_saved;
4683 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4684 }
4685#endif
4686 op_replace(oap, cap->nchar);
4687 }
4688 break;
4689
4690#ifdef FEAT_FOLDING
4691 case OP_FOLD:
4692 VIsual_reselect = FALSE; // don't reselect now
4693 foldCreate(oap->start.lnum, oap->end.lnum);
4694 break;
4695
4696 case OP_FOLDOPEN:
4697 case OP_FOLDOPENREC:
4698 case OP_FOLDCLOSE:
4699 case OP_FOLDCLOSEREC:
4700 VIsual_reselect = FALSE; // don't reselect now
4701 opFoldRange(oap->start.lnum, oap->end.lnum,
4702 oap->op_type == OP_FOLDOPEN
4703 || oap->op_type == OP_FOLDOPENREC,
4704 oap->op_type == OP_FOLDOPENREC
4705 || oap->op_type == OP_FOLDCLOSEREC,
4706 oap->is_VIsual);
4707 break;
4708
4709 case OP_FOLDDEL:
4710 case OP_FOLDDELREC:
4711 VIsual_reselect = FALSE; // don't reselect now
4712 deleteFold(oap->start.lnum, oap->end.lnum,
4713 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4714 break;
4715#endif
4716 case OP_NR_ADD:
4717 case OP_NR_SUB:
4718 if (empty_region_error)
4719 {
4720 vim_beep(BO_OPER);
4721 CancelRedo();
4722 }
4723 else
4724 {
4725 VIsual_active = TRUE;
4726#ifdef FEAT_LINEBREAK
4727 curwin->w_p_lbr = lbr_saved;
4728#endif
4729 op_addsub(oap, cap->count1, redo_VIsual_arg);
4730 VIsual_active = FALSE;
4731 }
4732 check_cursor_col();
4733 break;
4734 default:
4735 clearopbeep(oap);
4736 }
4737 virtual_op = MAYBE;
4738 if (!gui_yank)
4739 {
4740 // if 'sol' not set, go back to old column for some commands
4741 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4742 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4743 || oap->op_type == OP_DELETE))
4744 {
4745#ifdef FEAT_LINEBREAK
4746 curwin->w_p_lbr = FALSE;
4747#endif
4748 coladvance(curwin->w_curswant = old_col);
4749 }
4750 }
4751 else
4752 {
4753 curwin->w_cursor = old_cursor;
4754 }
4755 oap->block_mode = FALSE;
4756 clearop(oap);
4757 motion_force = NUL;
4758 }
4759#ifdef FEAT_LINEBREAK
4760 curwin->w_p_lbr = lbr_saved;
4761#endif
4762}