blob: 85a3758128138ec0bd367748e9c19bda56b9fdee [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 Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010022static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010024static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
26
Bram Moolenaarf2732452018-06-03 14:47:35 +020027// Flags for third item in "opchars".
28#define OPF_LINES 1 // operator always works on lines
29#define OPF_CHANGE 2 // operator changes text
30
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
32 * The names of operators.
33 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020034 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 */
36static char opchars[][3] =
37{
Bram Moolenaarf2732452018-06-03 14:47:35 +020038 {NUL, NUL, 0}, // OP_NOP
39 {'d', NUL, OPF_CHANGE}, // OP_DELETE
40 {'y', NUL, 0}, // OP_YANK
41 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
42 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
43 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
44 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
45 {'g', '~', OPF_CHANGE}, // OP_TILDE
46 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
47 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
48 {':', NUL, OPF_LINES}, // OP_COLON
49 {'g', 'U', OPF_CHANGE}, // OP_UPPER
50 {'g', 'u', OPF_CHANGE}, // OP_LOWER
51 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
52 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
53 {'g', '?', OPF_CHANGE}, // OP_ROT13
54 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
55 {'I', NUL, OPF_CHANGE}, // OP_INSERT
56 {'A', NUL, OPF_CHANGE}, // OP_APPEND
57 {'z', 'f', OPF_LINES}, // OP_FOLD
58 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
59 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
60 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
61 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
62 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
63 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
64 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
65 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
66 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
67 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000068};
69
70/*
71 * Translate a command name into an operator type.
72 * Must only be called with a valid operator name!
73 */
74 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010075get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 int i;
78
79 if (char1 == 'r') /* ignore second character */
80 return OP_REPLACE;
81 if (char1 == '~') /* when tilde is an operator */
82 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +010083 if (char1 == 'g' && char2 == Ctrl_A) /* add */
84 return OP_NR_ADD;
85 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
86 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 if (opchars[i][0] == char1 && opchars[i][1] == char2)
90 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +010091 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
92 {
93 internal_error("get_op_type()");
94 break;
95 }
96 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 return i;
98}
99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100/*
101 * Return TRUE if operator "op" always works on whole lines.
102 */
103 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100104op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200106 return opchars[op][2] & OPF_LINES;
107}
108
Bram Moolenaar113e1072019-01-20 15:30:40 +0100109#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200110/*
111 * Return TRUE if operator "op" changes text.
112 */
113 int
114op_is_change(int op)
115{
116 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
121 * Get first operator command character.
122 * Returns 'g' or 'z' if there is another command character.
123 */
124 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100125get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126{
127 return opchars[optype][0];
128}
129
130/*
131 * Get second operator command character.
132 */
133 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100134get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135{
136 return opchars[optype][1];
137}
138
139/*
140 * op_shift - handle a shift operation
141 */
142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100143op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 long i;
146 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149 if (u_save((linenr_T)(oap->start.lnum - 1),
150 (linenr_T)(oap->end.lnum + 1)) == FAIL)
151 return;
152
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 if (oap->block_mode)
154 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156 for (i = oap->line_count; --i >= 0; )
157 {
158 first_char = *ml_get_curline();
159 if (first_char == NUL) /* empty line */
160 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 else if (oap->block_mode)
162 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 else
164 /* Move the line right if it doesn't start with '#', 'smartindent'
165 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
166#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
167 if (first_char != '#' || !preprocs_left())
168#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000169 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 ++curwin->w_cursor.lnum;
171 }
172
173 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 if (oap->block_mode)
175 {
176 curwin->w_cursor.lnum = oap->start.lnum;
177 curwin->w_cursor.col = block_col;
178 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100179 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 {
181 curwin->w_cursor.lnum = oap->start.lnum;
182 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
183 }
184 else
185 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
186
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100187#ifdef FEAT_FOLDING
188 /* The cursor line is not in a closed fold */
189 foldOpenCursor();
190#endif
191
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (oap->line_count > p_report)
194 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200195 char *op;
196 char *msg_line_single;
197 char *msg_line_plural;
198
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200200 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200202 op = "<";
203 msg_line_single = NGETTEXT("%ld line %sed %d time",
204 "%ld line %sed %d times", amount);
205 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
206 "%ld lines %sed %d times", amount);
207 vim_snprintf((char *)IObuff, IOSIZE,
208 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
209 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100210 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 }
212
213 /*
214 * Set "'[" and "']" marks.
215 */
216 curbuf->b_op_start = oap->start;
217 curbuf->b_op_end.lnum = oap->end.lnum;
218 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
219 if (curbuf->b_op_end.col > 0)
220 --curbuf->b_op_end.col;
221}
222
223/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100224 * Shift the current line one shiftwidth left (if left != 0) or right
225 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 */
227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100228shift_line(
229 int left,
230 int round,
231 int amount,
232 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 int count;
235 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200236 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
238 count = get_indent(); /* get current indent */
239
240 if (round) /* round off indent */
241 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200242 i = count / sw_val; /* number of p_sw rounded down */
243 j = count % sw_val; /* extra spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 if (j && left) /* first remove extra spaces */
245 --amount;
246 if (left)
247 {
248 i -= amount;
249 if (i < 0)
250 i = 0;
251 }
252 else
253 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200254 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 }
256 else /* original vi indent */
257 {
258 if (left)
259 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200260 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 if (count < 0)
262 count = 0;
263 }
264 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200265 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 }
267
268 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000270 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000272 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273}
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275/*
276 * Shift one line of the current block one shiftwidth right or left.
277 * Leaves cursor on first character in block.
278 */
279 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100280shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281{
282 int left = (oap->op_type == OP_LSHIFT);
283 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000284 int total;
285 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200287 int sw_val = (int)get_sw_value_indent(curbuf);
288 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000291 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 int i = 0, j = 0;
293 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#ifdef FEAT_RIGHTLEFT
295 int old_p_ri = p_ri;
296
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200297 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298#endif
299
300 State = INSERT; /* don't want REPLACE for State */
301 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
302 if (bd.is_short)
303 return;
304
305 /* total is number of screen columns to be inserted/removed */
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 total = (int)((unsigned)amount * (unsigned)sw_val);
307 if ((total / sw_val) != amount)
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200308 return; /* multiplication overflow */
309
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 oldp = ml_get_curline();
311
312 if (!left)
313 {
314 /*
315 * 1. Get start vcol
316 * 2. Total ws vcols
317 * 3. Divvy into TABs & spp
318 * 4. Construct new string
319 */
320 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
321 ws_vcol = bd.start_vcol - bd.pre_whitesp;
322 if (bd.startspaces)
323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100325 {
326 if ((*mb_ptr2len)(bd.textstart) == 1)
327 ++bd.textstart;
328 else
329 {
330 ws_vcol = 0;
331 bd.startspaces = 0;
332 }
333 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000334 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000335 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100337 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200339 /* TODO: is passing bd.textstart for start of the line OK? */
340 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
341 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 total += incr;
343 bd.start_vcol += incr;
344 }
345 /* OK, now total=all the VWS reqd, and textstart points at the 1st
346 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200347#ifdef FEAT_VARTABS
348 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200349 tabstop_fromto(ws_vcol, ws_vcol + total,
350 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200351 else
352 j = total;
353#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200355 i = ((ws_vcol % ts_val) + total) / ts_val; /* number of tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 if (i)
Bram Moolenaardac13472019-09-16 21:06:21 +0200357 j = ((ws_vcol % ts_val) + total) % ts_val; /* number of spp */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 else
359 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 /* if we're splitting a TAB, allow for it */
362 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
363 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200364 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (newp == NULL)
366 return;
367 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
368 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200369 vim_memset(newp + bd.textcol, TAB, (size_t)i);
370 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 /* the end */
372 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
373 }
374 else /* left */
375 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000376 colnr_T destination_col; /* column to which text in block will
377 be shifted */
378 char_u *verbatim_copy_end; /* end of the part of the line which is
379 copied verbatim */
380 colnr_T verbatim_copy_width;/* the (displayed) width of this part
381 of line */
382 unsigned fill; /* nr of spaces that replace a TAB */
383 unsigned new_line_len; /* the length of the line after the
384 block shift */
385 size_t block_space_width;
386 size_t shift_amount;
387 char_u *non_white = bd.textstart;
388 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000390 /*
391 * Firstly, let's find the first non-whitespace character that is
392 * displayed after the block's start column and the character's column
393 * number. Also, let's calculate the width of all the whitespace
394 * characters that are displayed in the block and precede the searched
395 * non-whitespace character.
396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000398 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
399 * the part of which is displayed at the block's beginning. Let's start
400 * searching from the next character. */
401 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100402 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000403
404 /* The character's column is in "bd.start_vcol". */
405 non_white_col = bd.start_vcol;
406
Bram Moolenaar1c465442017-03-12 20:10:05 +0100407 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000408 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200409 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000410 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000413 block_space_width = non_white_col - oap->start_vcol;
414 /* We will shift by "total" or "block_space_width", whichever is less.
415 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000416 shift_amount = (block_space_width < (size_t)total
417 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000418
419 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000420 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000421
422 /* Now let's find out how much of the beginning of the line we can
423 * reuse without modification. */
424 verbatim_copy_end = bd.textstart;
425 verbatim_copy_width = bd.start_vcol;
426
427 /* If "bd.startspaces" is set, "bd.textstart" points to the character
428 * preceding the block. We have to subtract its width to obtain its
429 * column number. */
430 if (bd.startspaces)
431 verbatim_copy_width -= bd.start_char_vcols;
432 while (verbatim_copy_width < destination_col)
433 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200434 char_u *line = verbatim_copy_end;
435
436 /* TODO: is passing verbatim_copy_end for start of the line OK? */
437 incr = lbr_chartabsize(line, verbatim_copy_end,
438 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000439 if (verbatim_copy_width + incr > destination_col)
440 break;
441 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100442 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000443 }
444
445 /* If "destination_col" is different from the width of the initial
446 * part of the line that will be copied, it means we encountered a tab
447 * character, which we will have to partly replace with spaces. */
448 fill = destination_col - verbatim_copy_width;
449
450 /* The replacement line will consist of:
451 * - the beginning of the original line up to "verbatim_copy_end",
452 * - "fill" number of spaces,
453 * - the rest of the line, pointed to by non_white. */
454 new_line_len = (unsigned)(verbatim_copy_end - oldp)
455 + fill
456 + (unsigned)STRLEN(non_white) + 1;
457
Bram Moolenaar964b3742019-05-24 18:54:09 +0200458 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (newp == NULL)
460 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000461 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200462 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000463 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 }
465 /* replace the line */
466 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
467 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
468 State = oldstate;
469 curwin->w_cursor.col = oldcol;
470#ifdef FEAT_RIGHTLEFT
471 p_ri = old_p_ri;
472#endif
473}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475/*
476 * Insert string "s" (b_insert ? before : after) block :AKelly
477 * Caller must prepare for undo.
478 */
479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100480block_insert(
481 oparg_T *oap,
482 char_u *s,
483 int b_insert,
484 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485{
Bram Moolenaardac13472019-09-16 21:06:21 +0200486 int ts_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 int count = 0; /* extra spaces to replace a cut TAB */
488 int spaces = 0; /* non-zero if cutting a TAB */
489 colnr_T offset; /* pointer along new line */
490 unsigned s_len; /* STRLEN(s) */
491 char_u *newp, *oldp; /* new, old lines */
492 linenr_T lnum; /* loop var */
493 int oldstate = State;
494
495 State = INSERT; /* don't want REPLACE for State */
496 s_len = (unsigned)STRLEN(s);
497
498 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
499 {
500 block_prep(oap, bdp, lnum, TRUE);
501 if (bdp->is_short && b_insert)
502 continue; /* OP_INSERT, line ends before block start */
503
504 oldp = ml_get(lnum);
505
506 if (b_insert)
507 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200508 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 spaces = bdp->startspaces;
510 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200511 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 offset = bdp->textcol;
513 }
514 else /* append */
515 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200516 ts_val = bdp->end_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (!bdp->is_short) /* spaces = padding after block */
518 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200519 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200521 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 offset = bdp->textcol + bdp->textlen - (spaces != 0);
523 }
524 else /* spaces = padding to block edge */
525 {
526 /* if $ used, just append to EOL (ie spaces==0) */
527 if (!bdp->is_MAX)
528 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
529 count = spaces;
530 offset = bdp->textcol + bdp->textlen;
531 }
532 }
533
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200534 if (has_mbyte && spaces > 0)
535 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100536 int off;
537
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200538 /* Avoid starting halfway a multi-byte character. */
539 if (b_insert)
540 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100541 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200542 }
543 else
544 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100545 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200546 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200547 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100548 spaces -= off;
549 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200550 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200551
Bram Moolenaar964b3742019-05-24 18:54:09 +0200552 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (newp == NULL)
554 continue;
555
556 /* copy up to shifted part */
557 mch_memmove(newp, oldp, (size_t)(offset));
558 oldp += offset;
559
560 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200561 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
563 /* copy the new text */
564 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
565 offset += s_len;
566
567 if (spaces && !bdp->is_short)
568 {
569 /* insert post-padding */
Bram Moolenaardac13472019-09-16 21:06:21 +0200570 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 /* We're splitting a TAB, don't copy it. */
572 oldp++;
573 /* We allowed for that TAB, remember this now */
574 count++;
575 }
576
577 if (spaces > 0)
578 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000579 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
581 ml_replace(lnum, newp, FALSE);
582
583 if (lnum == oap->end.lnum)
584 {
585 /* Set "']" mark to the end of the block instead of the end of
586 * the insert in the first line. */
587 curbuf->b_op_end.lnum = oap->end.lnum;
588 curbuf->b_op_end.col = offset;
589 }
590 } /* for all lnum */
591
592 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
593
594 State = oldstate;
595}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
597#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
598/*
599 * op_reindent - handle reindenting a block of lines.
600 */
601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100602op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604 long i;
605 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200606 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 linenr_T first_changed = 0;
608 linenr_T last_changed = 0;
609 linenr_T start_lnum = curwin->w_cursor.lnum;
610
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000611 /* Don't even try when 'modifiable' is off. */
612 if (!curbuf->b_p_ma)
613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100614 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000615 return;
616 }
617
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 for (i = oap->line_count; --i >= 0 && !got_int; )
619 {
620 /* it's a slow thing to do, so give feedback so there's no worry that
621 * the computer's just hung. */
622
623 if (i > 1
624 && (i % 50 == 0 || i == oap->line_count - 1)
625 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100626 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628 /*
629 * Be vi-compatible: For lisp indenting the first line is not
630 * indented, unless there is only one line.
631 */
632#ifdef FEAT_LISP
633 if (i != oap->line_count - 1 || oap->line_count == 1
634 || how != get_lisp_indent)
635#endif
636 {
637 l = skipwhite(ml_get_curline());
638 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200639 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200641 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200643 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
645 /* did change the indent, call changed_lines() later */
646 if (first_changed == 0)
647 first_changed = curwin->w_cursor.lnum;
648 last_changed = curwin->w_cursor.lnum;
649 }
650 }
651 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000652 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654
655 /* put cursor on first non-blank of indented line */
656 curwin->w_cursor.lnum = start_lnum;
657 beginline(BL_SOL | BL_FIX);
658
659 /* Mark changed lines so that they will be redrawn. When Visual
660 * highlighting was present, need to continue until the last line. When
661 * there is no change still need to remove the Visual highlighting. */
662 if (last_changed != 0)
663 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 else if (oap->is_VIsual)
667 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 if (oap->line_count > p_report)
670 {
671 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100672 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200673 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
675 /* set '[ and '] marks */
676 curbuf->b_op_start = oap->start;
677 curbuf->b_op_end = oap->end;
678}
679#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Stuff a string into the typeahead buffer, such that edit() will insert it
683 * literally ("literally" TRUE) or interpret is as typed characters.
684 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200685 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100686stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 int c;
689 char_u *start;
690
691 while (*arg != NUL)
692 {
693 /* Stuff a sequence of normal ASCII characters, that's fast. Also
694 * stuff K_SPECIAL to get the effect of a special key when "literally"
695 * is TRUE. */
696 start = arg;
697 while ((*arg >= ' '
698#ifndef EBCDIC
699 && *arg < DEL /* EBCDIC: chars above space are normal */
700#endif
701 )
702 || (*arg == K_SPECIAL && !literally))
703 ++arg;
704 if (arg > start)
705 stuffReadbuffLen(start, (long)(arg - start));
706
707 /* stuff a single special character */
708 if (*arg != NUL)
709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +0200711 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 c = *arg++;
714 if (literally && ((c < ' ' && c != TAB) || c == DEL))
715 stuffcharReadbuff(Ctrl_V);
716 stuffcharReadbuff(c);
717 }
718 }
719}
720
721/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200722 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200724 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 */
726 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100727op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
729 int n;
730 linenr_T lnum;
731 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 char_u *newp, *oldp;
733 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
735 int did_yank = FALSE;
736
737 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
738 return OK;
739
740 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
741 if (oap->empty)
742 return u_save_cursor();
743
744 if (!curbuf->b_p_ma)
745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100746 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 return FAIL;
748 }
749
750#ifdef FEAT_CLIPBOARD
751 adjust_clip_reg(&oap->regname);
752#endif
753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (has_mbyte)
755 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaard04b7502010-07-08 22:27:55 +0200757 /*
758 * Imitate the strange Vi behaviour: If the delete spans more than one
759 * line and motion_type == MCHAR and the result is a blank line, make the
760 * delete linewise. Don't do this for the change command or Visual mode.
761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000764 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100766 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 && oap->op_type == OP_DELETE)
768 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200769 ptr = ml_get(oap->end.lnum) + oap->end.col;
770 if (*ptr != NUL)
771 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 ptr = skipwhite(ptr);
773 if (*ptr == NUL && inindent(0))
774 oap->motion_type = MLINE;
775 }
776
Bram Moolenaard04b7502010-07-08 22:27:55 +0200777 /*
778 * Check for trying to delete (e.g. "D") in an empty line.
779 * Note: For the change operator it is ok.
780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if ( oap->motion_type == MCHAR
782 && oap->line_count == 1
783 && oap->op_type == OP_DELETE
784 && *ml_get(oap->start.lnum) == NUL)
785 {
786 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000787 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 * 'cpoptions' (Vi compatible).
789 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000790 if (virtual_op)
791 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
792 * marks as if it happened. */
793 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
795 beep_flush();
796 return OK;
797 }
798
Bram Moolenaard04b7502010-07-08 22:27:55 +0200799 /*
800 * Do a yank of whatever we're about to delete.
801 * If a yank register was specified, put the deleted text into that
802 * register. For the black hole register '_' don't yank anything.
803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (oap->regname != '_')
805 {
806 if (oap->regname != 0)
807 {
808 /* check for read-only register */
809 if (!valid_yank_reg(oap->regname, TRUE))
810 {
811 beep_flush();
812 return OK;
813 }
814 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
815 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
816 did_yank = TRUE;
817 }
818
819 /*
820 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100821 * delete contains a line break, or when using a specific operator (Vi
822 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200823 * Use the register name from before adjust_clip_reg() may have
824 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100826 if (oap->motion_type == MLINE || oap->line_count > 1
827 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100829 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (op_yank(oap, TRUE, FALSE) == OK)
831 did_yank = TRUE;
832 }
833
Bram Moolenaar84298db2012-04-20 13:46:08 +0200834 /* Yank into small delete register when no named register specified
835 * and the delete is within one line. */
836 if ((
837#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200838 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
839 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200840#endif
841 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 && oap->line_count == 1)
843 {
844 oap->regname = '-';
845 get_yank_register(oap->regname, TRUE);
846 if (op_yank(oap, TRUE, FALSE) == OK)
847 did_yank = TRUE;
848 oap->regname = 0;
849 }
850
851 /*
852 * If there's too much stuff to fit in the yank register, then get a
853 * confirmation before doing the delete. This is crude, but simple.
854 * And it avoids doing a delete of something we can't put back if we
855 * want.
856 */
857 if (!did_yank)
858 {
859 int msg_silent_save = msg_silent;
860
861 msg_silent = 0; /* must display the prompt */
862 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
863 msg_silent = msg_silent_save;
864 if (n != 'y')
865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 return FAIL;
868 }
869 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100870
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100871#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100872 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200873 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876
Bram Moolenaard04b7502010-07-08 22:27:55 +0200877 /*
878 * block mode delete
879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (oap->block_mode)
881 {
882 if (u_save((linenr_T)(oap->start.lnum - 1),
883 (linenr_T)(oap->end.lnum + 1)) == FAIL)
884 return FAIL;
885
886 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
887 {
888 block_prep(oap, &bd, lnum, TRUE);
889 if (bd.textlen == 0) /* nothing to delete */
890 continue;
891
892 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
893 if (lnum == curwin->w_cursor.lnum)
894 {
895 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898
Bram Moolenaar8055d172019-05-17 22:57:26 +0200899 // "n" == number of chars deleted
900 // If we delete a TAB, it may be replaced by several characters.
901 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 n = bd.textlen - bd.startspaces - bd.endspaces;
903 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200904 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (newp == NULL)
906 continue;
907 /* copy up to deleted part */
908 mch_memmove(newp, oldp, (size_t)bd.textcol);
909 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200910 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 (size_t)(bd.startspaces + bd.endspaces));
912 /* copy the part after the deleted part */
913 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000914 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 /* replace the line */
916 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200917
918#ifdef FEAT_TEXT_PROP
919 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200920 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923
924 check_cursor_col();
925 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
926 oap->end.lnum + 1, 0L);
927 oap->line_count = 0; /* no lines deleted */
928 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100929 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
931 if (oap->op_type == OP_CHANGE)
932 {
933 /* Delete the lines except the first one. Temporarily move the
934 * cursor to the next line. Save the current line number, if the
935 * last line is deleted it may be changed.
936 */
937 if (oap->line_count > 1)
938 {
939 lnum = curwin->w_cursor.lnum;
940 ++curwin->w_cursor.lnum;
941 del_lines((long)(oap->line_count - 1), TRUE);
942 curwin->w_cursor.lnum = lnum;
943 }
944 if (u_save_cursor() == FAIL)
945 return FAIL;
946 if (curbuf->b_p_ai) /* don't delete indent */
947 {
948 beginline(BL_WHITE); /* cursor on first non-white */
949 did_ai = TRUE; /* delete the indent when ESC hit */
950 ai_col = curwin->w_cursor.col;
951 }
952 else
953 beginline(0); /* cursor in column 0 */
954 truncate_line(FALSE); /* delete the rest of the line */
955 /* leave cursor past last char in line */
956 if (oap->line_count > 1)
957 u_clearline(); /* "U" command not possible after "2cc" */
958 }
959 else
960 {
961 del_lines(oap->line_count, TRUE);
962 beginline(BL_WHITE | BL_FIX);
963 u_clearline(); /* "U" command not possible after "dd" */
964 }
965 }
966 else
967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (virtual_op)
969 {
970 int endcol = 0;
971
972 /* For virtualedit: break the tabs that are partly included. */
973 if (gchar_pos(&oap->start) == '\t')
974 {
975 if (u_save_cursor() == FAIL) /* save first line for undo */
976 return FAIL;
977 if (oap->line_count == 1)
978 endcol = getviscol2(oap->end.col, oap->end.coladd);
979 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
980 oap->start = curwin->w_cursor;
981 if (oap->line_count == 1)
982 {
983 coladvance(endcol);
984 oap->end.col = curwin->w_cursor.col;
985 oap->end.coladd = curwin->w_cursor.coladd;
986 curwin->w_cursor = oap->start;
987 }
988 }
989
990 /* Break a tab only when it's included in the area. */
991 if (gchar_pos(&oap->end) == '\t'
992 && (int)oap->end.coladd < oap->inclusive)
993 {
994 /* save last line for undo */
995 if (u_save((linenr_T)(oap->end.lnum - 1),
996 (linenr_T)(oap->end.lnum + 1)) == FAIL)
997 return FAIL;
998 curwin->w_cursor = oap->end;
999 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1000 oap->end = curwin->w_cursor;
1001 curwin->w_cursor = oap->start;
1002 }
1003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 if (oap->line_count == 1) /* delete characters within one line */
1006 {
1007 if (u_save_cursor() == FAIL) /* save line for undo */
1008 return FAIL;
1009
1010 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001011 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 && oap->op_type == OP_CHANGE
1013 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001014 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 display_dollar(oap->end.col - !oap->inclusive);
1016
1017 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (virtual_op)
1020 {
1021 /* fix up things for virtualedit-delete:
1022 * break the tabs which are going to get in our way
1023 */
1024 char_u *curline = ml_get_curline();
1025 int len = (int)STRLEN(curline);
1026
1027 if (oap->end.coladd != 0
1028 && (int)oap->end.col >= len - 1
1029 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1030 n++;
1031 /* Delete at least one char (e.g, when on a control char). */
1032 if (n == 0 && oap->start.coladd != oap->end.coladd)
1033 n = 1;
1034
1035 /* When deleted a char in the line, reset coladd. */
1036 if (gchar_cursor() != NUL)
1037 curwin->w_cursor.coladd = 0;
1038 }
Bram Moolenaard009e862015-06-09 20:20:03 +02001039 (void)del_bytes((long)n, !virtual_op,
1040 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
1042 else /* delete characters between lines */
1043 {
1044 pos_T curpos;
1045
1046 /* save deleted and changed lines for undo */
1047 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1048 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1049 return FAIL;
1050
1051 truncate_line(TRUE); /* delete from cursor to end of line */
1052
1053 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1054 ++curwin->w_cursor.lnum;
1055 del_lines((long)(oap->line_count - 2), FALSE);
1056
Bram Moolenaard009e862015-06-09 20:20:03 +02001057 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001058 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001059 curwin->w_cursor.col = 0;
1060 (void)del_bytes((long)n, !virtual_op,
1061 oap->op_type == OP_DELETE && !oap->is_VIsual);
1062 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1063 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065 }
1066
1067 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1068
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (oap->block_mode)
1071 {
1072 curbuf->b_op_end.lnum = oap->end.lnum;
1073 curbuf->b_op_end.col = oap->start.col;
1074 }
1075 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 curbuf->b_op_end = oap->start;
1077 curbuf->b_op_start = oap->start;
1078
1079 return OK;
1080}
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/*
1083 * Adjust end of operating area for ending on a multi-byte character.
1084 * Used for deletion.
1085 */
1086 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001087mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 char_u *p;
1090
1091 if (oap->inclusive)
1092 {
1093 p = ml_get(oap->end.lnum);
1094 oap->end.col += mb_tail_off(p, p + oap->end.col);
1095 }
1096}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
Bram Moolenaar630afe82018-06-28 19:26:28 +02001098/*
1099 * Replace the character under the cursor with "c".
1100 * This takes care of multi-byte characters.
1101 */
1102 static void
1103replace_character(int c)
1104{
1105 int n = State;
1106
1107 State = REPLACE;
1108 ins_char(c);
1109 State = n;
1110 /* Backup to the replaced character. */
1111 dec_cursor();
1112}
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114/*
1115 * Replace a whole area with one character.
1116 */
1117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001118op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 char_u *newp, *oldp;
1123 size_t oldlen;
1124 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001125 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001126 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1129 return OK; /* nothing to do */
1130
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001131 if (c == REPLACE_CR_NCHAR)
1132 {
1133 had_ctrl_v_cr = TRUE;
1134 c = CAR;
1135 }
1136 else if (c == REPLACE_NL_NCHAR)
1137 {
1138 had_ctrl_v_cr = TRUE;
1139 c = NL;
1140 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (has_mbyte)
1143 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 if (u_save((linenr_T)(oap->start.lnum - 1),
1146 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1147 return FAIL;
1148
1149 /*
1150 * block mode replace
1151 */
1152 if (oap->block_mode)
1153 {
1154 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1155 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1156 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001157 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1159 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1160 continue; /* nothing to replace */
1161
1162 /* n == number of extra chars required
1163 * If we split a TAB, it may be replaced by several characters.
1164 * Thus the number of characters may increase!
1165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 /* If the range starts in virtual space, count the initial
1167 * coladd offset as part of "startspaces" */
1168 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1169 {
1170 pos_T vpos;
1171
Bram Moolenaara1381de2009-11-03 15:44:21 +00001172 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 getvpos(&vpos, oap->start_vcol);
1174 bd.startspaces += vpos.coladd;
1175 n = bd.startspaces;
1176 }
1177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 /* allow for pre spaces */
1179 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1180
1181 /* allow for post spp */
1182 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
1185 /* Figure out how many characters to replace. */
1186 numc = oap->end_vcol - oap->start_vcol + 1;
1187 if (bd.is_short && (!virtual_op || bd.is_MAX))
1188 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 /* A double-wide character can be replaced only up to half the
1191 * times. */
1192 if ((*mb_char2cells)(c) > 1)
1193 {
1194 if ((numc & 1) && !bd.is_short)
1195 {
1196 ++bd.endspaces;
1197 ++n;
1198 }
1199 numc = numc / 2;
1200 }
1201
1202 /* Compute bytes needed, move character count to num_chars. */
1203 num_chars = numc;
1204 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 /* oldlen includes textlen, so don't double count */
1206 n += numc - bd.textlen;
1207
1208 oldp = ml_get_curline();
1209 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001210 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (newp == NULL)
1212 continue;
1213 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
1214 /* copy up to deleted part */
1215 mch_memmove(newp, oldp, (size_t)bd.textcol);
1216 oldp += bd.textcol + bd.textlen;
1217 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001218 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001220 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1221 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01001222 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001224 if (has_mbyte)
1225 {
1226 n = (int)STRLEN(newp);
1227 while (--num_chars >= 0)
1228 n += (*mb_char2bytes)(c, newp + n);
1229 }
1230 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001231 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001232 if (!bd.is_short)
1233 {
1234 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001235 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01001236 /* copy the part after the changed part */
1237 STRMOVE(newp + STRLEN(newp), oldp);
1238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001242 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001243 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001244 if (after_p != NULL)
1245 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 }
1247 /* replace the line */
1248 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001249 if (after_p != NULL)
1250 {
1251 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1252 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1253 oap->end.lnum++;
1254 vim_free(after_p);
1255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
1257 }
1258 else
1259 {
1260 /*
1261 * MCHAR and MLINE motion replace.
1262 */
1263 if (oap->motion_type == MLINE)
1264 {
1265 oap->start.col = 0;
1266 curwin->w_cursor.col = 0;
1267 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1268 if (oap->end.col)
1269 --oap->end.col;
1270 }
1271 else if (!oap->inclusive)
1272 dec(&(oap->end));
1273
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001274 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
1276 n = gchar_cursor();
1277 if (n != NUL)
1278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1280 {
1281 /* This is slow, but it handles replacing a single-byte
1282 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01001283 if (curwin->w_cursor.lnum == oap->end.lnum)
1284 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001285 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (n == TAB)
1290 {
1291 int end_vcol = 0;
1292
1293 if (curwin->w_cursor.lnum == oap->end.lnum)
1294 {
1295 /* oap->end has to be recalculated when
1296 * the tab breaks */
1297 end_vcol = getviscol2(oap->end.col,
1298 oap->end.coladd);
1299 }
1300 coladvance_force(getviscol());
1301 if (curwin->w_cursor.lnum == oap->end.lnum)
1302 getvpos(&oap->end, end_vcol);
1303 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001304 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1308 {
1309 int virtcols = oap->end.coladd;
1310
1311 if (curwin->w_cursor.lnum == oap->start.lnum
1312 && oap->start.col == oap->end.col && oap->start.coladd)
1313 virtcols -= oap->start.coladd;
1314
1315 /* oap->end has been trimmed so it's effectively inclusive;
1316 * as a result an extra +1 must be counted so we don't
1317 * trample the NUL byte. */
1318 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1319 curwin->w_cursor.col -= (virtcols + 1);
1320 for (; virtcols >= 0; virtcols--)
1321 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001322 if ((*mb_char2len)(c) > 1)
1323 replace_character(c);
1324 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001325 PBYTE(curwin->w_cursor, c);
1326 if (inc(&curwin->w_cursor) == -1)
1327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 /* Advance to next character, stop at the end of the file. */
1332 if (inc_cursor() == -1)
1333 break;
1334 }
1335 }
1336
1337 curwin->w_cursor = oap->start;
1338 check_cursor();
1339 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1340
1341 /* Set "'[" and "']" marks. */
1342 curbuf->b_op_start = oap->start;
1343 curbuf->b_op_end = oap->end;
1344
1345 return OK;
1346}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001348static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350/*
1351 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1352 */
1353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001354op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001358 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 if (u_save((linenr_T)(oap->start.lnum - 1),
1361 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1362 return;
1363
1364 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (oap->block_mode) /* Visual block mode */
1366 {
1367 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1368 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001369 int one_change;
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 block_prep(oap, &bd, pos.lnum, FALSE);
1372 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001373 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1374 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001375
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001376#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001377 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 {
1379 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1380
Bram Moolenaar009b2592004-10-24 19:18:58 +00001381 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1382 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001384 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 if (did_change)
1389 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1390 }
1391 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
1393 if (oap->motion_type == MLINE)
1394 {
1395 oap->start.col = 0;
1396 pos.col = 0;
1397 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1398 if (oap->end.col)
1399 --oap->end.col;
1400 }
1401 else if (!oap->inclusive)
1402 dec(&(oap->end));
1403
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001404 if (pos.lnum == oap->end.lnum)
1405 did_change = swapchars(oap->op_type, &pos,
1406 oap->end.col - pos.col + 1);
1407 else
1408 for (;;)
1409 {
1410 did_change |= swapchars(oap->op_type, &pos,
1411 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1412 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001413 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001414 break;
1415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (did_change)
1417 {
1418 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1419 0L);
1420#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001421 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
1423 char_u *ptr;
1424 int count;
1425
1426 pos = oap->start;
1427 while (pos.lnum < oap->end.lnum)
1428 {
1429 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001430 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001431 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001433 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 pos.col = 0;
1435 pos.lnum++;
1436 }
1437 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1438 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001439 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001441 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443#endif
1444 }
1445 }
1446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (!did_change && oap->is_VIsual)
1448 /* No change: need to remove the Visual selection */
1449 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451 /*
1452 * Set '[ and '] marks.
1453 */
1454 curbuf->b_op_start = oap->start;
1455 curbuf->b_op_end = oap->end;
1456
1457 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001458 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001459 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460}
1461
1462/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001463 * Invoke swapchar() on "length" bytes at position "pos".
1464 * "pos" is advanced to just after the changed characters.
1465 * "length" is rounded up to include the whole last multi-byte character.
1466 * Also works correctly when the number of bytes changes.
1467 * Returns TRUE if some character was changed.
1468 */
1469 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001470swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001471{
1472 int todo;
1473 int did_change = 0;
1474
1475 for (todo = length; todo > 0; --todo)
1476 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001477 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001478 {
1479 int len = (*mb_ptr2len)(ml_get_pos(pos));
1480
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001481 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001482 if (len > 0)
1483 todo -= len - 1;
1484 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001485 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001486 if (inc(pos) == -1) /* at end of file */
1487 break;
1488 }
1489 return did_change;
1490}
1491
1492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 * If op_type == OP_UPPER: make uppercase,
1494 * if op_type == OP_LOWER: make lowercase,
1495 * if op_type == OP_ROT13: do rot13 encoding,
1496 * else swap case of character at 'pos'
1497 * returns TRUE when something actually changed.
1498 */
1499 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001500swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 int c;
1503 int nc;
1504
1505 c = gchar_pos(pos);
1506
1507 /* Only do rot13 encoding for ASCII characters. */
1508 if (c >= 0x80 && op_type == OP_ROT13)
1509 return FALSE;
1510
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001511 if (op_type == OP_UPPER && c == 0xdf
1512 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001513 {
1514 pos_T sp = curwin->w_cursor;
1515
1516 /* Special handling of German sharp s: change to "SS". */
1517 curwin->w_cursor = *pos;
1518 del_char(FALSE);
1519 ins_char('S');
1520 ins_char('S');
1521 curwin->w_cursor = sp;
1522 inc(pos);
1523 }
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
1526 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 nc = c;
1528 if (MB_ISLOWER(c))
1529 {
1530 if (op_type == OP_ROT13)
1531 nc = ROT13(c, 'a');
1532 else if (op_type != OP_LOWER)
1533 nc = MB_TOUPPER(c);
1534 }
1535 else if (MB_ISUPPER(c))
1536 {
1537 if (op_type == OP_ROT13)
1538 nc = ROT13(c, 'A');
1539 else if (op_type != OP_UPPER)
1540 nc = MB_TOLOWER(c);
1541 }
1542 if (nc != c)
1543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1545 {
1546 pos_T sp = curwin->w_cursor;
1547
1548 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001549 /* don't use del_char(), it also removes composing chars */
1550 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 ins_char(nc);
1552 curwin->w_cursor = sp;
1553 }
1554 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001555 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return TRUE;
1557 }
1558 return FALSE;
1559}
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
1562 * op_insert - Insert and append operators for Visual mode.
1563 */
1564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
1567 long ins_len, pre_textlen = 0;
1568 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001569 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 struct block_def bd;
1571 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001572 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 /* edit() changes this - record it for OP_APPEND */
1575 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1576
1577 /* vis block is still marked. Get rid of it now. */
1578 curwin->w_cursor.lnum = oap->start.lnum;
1579 update_screen(INVERTED);
1580
1581 if (oap->block_mode)
1582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 /* When 'virtualedit' is used, need to insert the extra spaces before
1584 * doing block_prep(). When only "block" is used, virtual edit is
1585 * already disabled, but still need it when calling
1586 * coladvance_force(). */
1587 if (curwin->w_cursor.coladd > 0)
1588 {
1589 int old_ve_flags = ve_flags;
1590
1591 ve_flags = VE_ALL;
1592 if (u_save_cursor() == FAIL)
1593 return;
1594 coladvance_force(oap->op_type == OP_APPEND
1595 ? oap->end_vcol + 1 : getviscol());
1596 if (oap->op_type == OP_APPEND)
1597 --curwin->w_cursor.col;
1598 ve_flags = old_ve_flags;
1599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 /* Get the info about the block before entering the text */
1601 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001602 /* Get indent information */
1603 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (oap->op_type == OP_APPEND)
1607 firstline += bd.textlen;
1608 pre_textlen = (long)STRLEN(firstline);
1609 }
1610
1611 if (oap->op_type == OP_APPEND)
1612 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001613 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 /* Move the cursor to the character right of the block. */
1616 curwin->w_set_curswant = TRUE;
1617 while (*ml_get_cursor() != NUL
1618 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1619 ++curwin->w_cursor.col;
1620 if (bd.is_short && !bd.is_MAX)
1621 {
1622 /* First line was too short, make it longer and adjust the
1623 * values in "bd". */
1624 if (u_save_cursor() == FAIL)
1625 return;
1626 for (i = 0; i < bd.endspaces; ++i)
1627 ins_char(' ');
1628 bd.textlen += bd.endspaces;
1629 }
1630 }
1631 else
1632 {
1633 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001634 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001637 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 && oap->start_vcol != oap->end_vcol)
1639 inc_cursor();
1640 }
1641 }
1642
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001643 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001644 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001646 /* When a tab was inserted, and the characters in front of the tab
1647 * have been converted to a tab as well, the column of the cursor
1648 * might have actually been reduced, so need to adjust here. */
1649 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001650 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001651 oap->start = curbuf->b_op_start_orig;
1652
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001653 /* If user has moved off this line, we don't know what to do, so do
1654 * nothing.
1655 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
1656 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 return;
1658
1659 if (oap->block_mode)
1660 {
1661 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001662 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001663 size_t len;
1664 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001666 /* If indent kicked in, the firstline might have changed
1667 * but only do that, if the indent actually increased. */
1668 ind_post = (colnr_T)getwhitecols_curline();
1669 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1670 {
1671 bd.textcol += ind_post - ind_pre;
1672 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001673 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001674 }
1675
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001676 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001677 * to adjust the block for that. But only do it, if the difference
1678 * does not come from indent kicking in. */
1679 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1680 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001681 {
1682 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001683 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001684 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001685 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001686 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001687 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001688 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001689 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001690 pre_textlen -= t - oap->start_vcol;
1691 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001692 }
1693 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001694 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001695 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001696 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001697 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001698 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001699 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001700 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001701 /* reset pre_textlen to the value of OP_INSERT */
1702 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001703 pre_textlen -= t - oap->start_vcol;
1704 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001705 oap->op_type = OP_INSERT;
1706 }
1707 }
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 /*
1710 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001711 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 * Don't do this when "$" used, end-of-line will have changed.
1713 */
1714 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1715 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1716 {
1717 if (oap->op_type == OP_APPEND)
1718 {
1719 pre_textlen += bd2.textlen - bd.textlen;
1720 if (bd2.endspaces)
1721 --bd2.textlen;
1722 }
1723 bd.textcol = bd2.textcol;
1724 bd.textlen = bd2.textlen;
1725 }
1726
1727 /*
1728 * Subsequent calls to ml_get() flush the firstline data - take a
1729 * copy of the required string.
1730 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001731 firstline = ml_get(oap->start.lnum);
1732 len = STRLEN(firstline);
1733 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001735 add += bd.textlen;
1736 if ((size_t)add > len)
1737 firstline += len; // short line, point to the NUL
1738 else
1739 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001740 if (pre_textlen >= 0
1741 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 {
1743 ins_text = vim_strnsave(firstline, (int)ins_len);
1744 if (ins_text != NULL)
1745 {
1746 /* block handled here */
1747 if (u_save(oap->start.lnum,
1748 (linenr_T)(oap->end.lnum + 1)) == OK)
1749 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1750 &bd);
1751
1752 curwin->w_cursor.col = oap->start.col;
1753 check_cursor();
1754 vim_free(ins_text);
1755 }
1756 }
1757 }
1758}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760/*
1761 * op_change - handle a change operation
1762 *
1763 * return TRUE if edit() returns because of a CTRL-O command
1764 */
1765 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 colnr_T l;
1769 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 long offset;
1771 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001772 long ins_len;
1773 long pre_textlen = 0;
1774 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 char_u *firstline;
1776 char_u *ins_text, *newp, *oldp;
1777 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779 l = oap->start.col;
1780 if (oap->motion_type == MLINE)
1781 {
1782 l = 0;
1783#ifdef FEAT_SMARTINDENT
1784 if (!p_paste && curbuf->b_p_si
1785# ifdef FEAT_CINDENT
1786 && !curbuf->b_p_cin
1787# endif
1788 )
1789 can_si = TRUE; /* It's like opening a new line, do si */
1790#endif
1791 }
1792
1793 /* First delete the text in the region. In an empty buffer only need to
1794 * save for undo */
1795 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1796 {
1797 if (u_save_cursor() == FAIL)
1798 return FALSE;
1799 }
1800 else if (op_delete(oap) == FAIL)
1801 return FALSE;
1802
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001803 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 && !virtual_op)
1805 inc_cursor();
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 /* check for still on same line (<CR> in inserted text meaningless) */
1808 /* skip blank lines too */
1809 if (oap->block_mode)
1810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 /* Add spaces before getting the current line length. */
1812 if (virtual_op && (curwin->w_cursor.coladd > 0
1813 || gchar_cursor() == NUL))
1814 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001815 firstline = ml_get(oap->start.lnum);
1816 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001817 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 bd.textcol = curwin->w_cursor.col;
1819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1822 if (oap->motion_type == MLINE)
1823 fix_indent();
1824#endif
1825
1826 retval = edit(NUL, FALSE, (linenr_T)1);
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001829 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001831 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001833 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00001835 /* Auto-indenting may have changed the indent. If the cursor was past
1836 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001838 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001840 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001841
1842 pre_textlen += new_indent - pre_indent;
1843 bd.textcol += new_indent - pre_indent;
1844 }
1845
1846 ins_len = (long)STRLEN(firstline) - pre_textlen;
1847 if (ins_len > 0)
1848 {
1849 /* Subsequent calls to ml_get() flush the firstline data - take a
1850 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001851 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001853 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1855 linenr++)
1856 {
1857 block_prep(oap, &bd, linenr, TRUE);
1858 if (!bd.is_short || virtual_op)
1859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 pos_T vpos;
1861
1862 /* If the block starts in virtual space, count the
1863 * initial coladd offset as part of "startspaces" */
1864 if (bd.is_short)
1865 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001866 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869 else
1870 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001872 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (newp == NULL)
1874 continue;
1875 /* copy up to block start */
1876 mch_memmove(newp, oldp, (size_t)bd.textcol);
1877 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001878 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1881 offset += ins_len;
1882 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001883 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 ml_replace(linenr, newp, FALSE);
1885 }
1886 }
1887 check_cursor();
1888
1889 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1890 }
1891 vim_free(ins_text);
1892 }
1893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
1895 return retval;
1896}
1897
1898/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001899 * When the cursor is on the NUL past the end of the line and it should not be
1900 * there move it left.
1901 */
1902 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001903adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001904{
1905 if (curwin->w_cursor.col > 0
1906 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001907 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 && !(restart_edit || (State & INSERT)))
1909 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00001910 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00001911 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001914 {
1915 colnr_T scol, ecol;
1916
1917 /* Coladd is set to the width of the last character. */
1918 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1919 curwin->w_cursor.coladd = ecol - scol + 1;
1920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922}
1923
1924#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
1925/*
1926 * Return TRUE if lines starting with '#' should be left aligned.
1927 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001928 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001929preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
1931 return
1932# ifdef FEAT_SMARTINDENT
1933# ifdef FEAT_CINDENT
1934 (curbuf->b_p_si && !curbuf->b_p_cin) ||
1935# else
1936 curbuf->b_p_si
1937# endif
1938# endif
1939# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01001940 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1941 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942# endif
1943 ;
1944}
1945#endif
1946
Bram Moolenaar81340392012-06-06 16:12:59 +02001947#if defined(FEAT_COMMENTS) || defined(PROTO)
1948/*
1949 * If "process" is TRUE and the line begins with a comment leader (possibly
1950 * after some white space), return a pointer to the text after it. Put a boolean
1951 * value indicating whether the line ends with an unclosed comment in
1952 * "is_comment".
1953 * line - line to be processed,
1954 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001955 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001956 * include_space - whether to also skip space following the comment leader,
1957 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001958 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001959 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001960 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001961skip_comment(
1962 char_u *line,
1963 int process,
1964 int include_space,
1965 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001966{
1967 char_u *comment_flags = NULL;
1968 int lead_len;
1969 int leader_offset = get_last_leader_offset(line, &comment_flags);
1970
1971 *is_comment = FALSE;
1972 if (leader_offset != -1)
1973 {
1974 /* Let's check whether the line ends with an unclosed comment.
1975 * If the last comment leader has COM_END in flags, there's no comment.
1976 */
1977 while (*comment_flags)
1978 {
1979 if (*comment_flags == COM_END
1980 || *comment_flags == ':')
1981 break;
1982 ++comment_flags;
1983 }
1984 if (*comment_flags != COM_END)
1985 *is_comment = TRUE;
1986 }
1987
1988 if (process == FALSE)
1989 return line;
1990
1991 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1992
1993 if (lead_len == 0)
1994 return line;
1995
1996 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02001997 * - COM_END,
1998 * - colon,
1999 * whichever comes first.
2000 */
2001 while (*comment_flags)
2002 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02002003 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02002004 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02002005 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02002006 ++comment_flags;
2007 }
2008
2009 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02002010 * starting with a closing part of a three-part comment. That's good,
2011 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02002012 */
2013 if (*comment_flags == ':' || *comment_flags == NUL)
2014 line += lead_len;
2015
2016 return line;
2017}
2018#endif
2019
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002021 * Join 'count' lines (minimal 2) at cursor position.
2022 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002023 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
2024 * leaders should not be removed.
2025 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
2026 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00002028 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 */
2030 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031do_join(
2032 long count,
2033 int insert_space,
2034 int save_undo,
2035 int use_formatoptions UNUSED,
2036 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002038 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002039 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002040 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002042 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02002043 int endcurr1 = NUL;
2044 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002045 int currsize = 0; /* size of the current line */
2046 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002048 colnr_T col = 0;
2049 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02002050#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02002051 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002052 int remove_comments = (use_formatoptions == TRUE)
2053 && has_format_option(FO_REMOVE_COMS);
2054 int prev_was_comment;
2055#endif
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002056#ifdef FEAT_TEXT_PROP
2057 textprop_T **prop_lines = NULL;
2058 int *prop_lengths = NULL;
2059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002061 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2062 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2063 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 /* Allocate an array to store the number of spaces inserted before each
2066 * line. We will use it to pre-compute the length of the new line and the
2067 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002068 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002069 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002071#if defined(FEAT_COMMENTS) || defined(PROTO)
2072 if (remove_comments)
2073 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002074 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002075 if (comments == NULL)
2076 {
2077 vim_free(spaces);
2078 return FAIL;
2079 }
2080 }
2081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002084 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002085 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002086 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002088 for (t = 0; t < count; ++t)
2089 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002090 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002091 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002092 {
2093 /* Set the '[ mark. */
2094 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2095 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2096 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002097#if defined(FEAT_COMMENTS) || defined(PROTO)
2098 if (remove_comments)
2099 {
2100 /* We don't want to remove the comment leader if the
2101 * previous line is not a comment. */
2102 if (t > 0 && prev_was_comment)
2103 {
2104
2105 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2106 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002107 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002108 curr = new_curr;
2109 }
2110 else
2111 curr = skip_comment(curr, FALSE, insert_space,
2112 &prev_was_comment);
2113 }
2114#endif
2115
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002116 if (insert_space && t > 0)
2117 {
2118 curr = skipwhite(curr);
2119 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002120 && (!has_format_option(FO_MBYTE_JOIN)
2121 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2122 && (!has_format_option(FO_MBYTE_JOIN2)
2123 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002124 )
2125 {
2126 /* don't add a space if the line is ending in a space */
2127 if (endcurr1 == ' ')
2128 endcurr1 = endcurr2;
2129 else
2130 ++spaces[t];
2131 /* extra space when 'joinspaces' set and line ends in '.' */
2132 if ( p_js
2133 && (endcurr1 == '.'
2134 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2135 && (endcurr1 == '?' || endcurr1 == '!'))))
2136 ++spaces[t];
2137 }
2138 }
2139 currsize = (int)STRLEN(curr);
2140 sumsize += currsize + spaces[t];
2141 endcurr1 = endcurr2 = NUL;
2142 if (insert_space && currsize > 0)
2143 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002144 if (has_mbyte)
2145 {
2146 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002147 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002148 endcurr1 = (*mb_ptr2char)(cend);
2149 if (cend > curr)
2150 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002151 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002152 endcurr2 = (*mb_ptr2char)(cend);
2153 }
2154 }
2155 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002156 {
2157 endcurr1 = *(curr + currsize - 1);
2158 if (currsize > 1)
2159 endcurr2 = *(curr + currsize - 2);
2160 }
2161 }
2162 line_breakcheck();
2163 if (got_int)
2164 {
2165 ret = FAIL;
2166 goto theend;
2167 }
2168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002170 /* store the column position before last line */
2171 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002173 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002174 newp = alloc(sumsize + 1);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002175 if (newp == NULL)
2176 {
2177 ret = FAIL;
2178 goto theend;
2179 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002180 cend = newp + sumsize;
2181 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002183#ifdef FEAT_TEXT_PROP
2184 // We need to move properties of the lines that are going to be deleted to
2185 // the new long one.
2186 if (curbuf->b_has_textprop && !text_prop_frozen)
2187 {
2188 // Allocate an array to copy the text properties of joined lines into.
2189 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002190 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
2191 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002192 if (prop_lengths == NULL)
2193 VIM_CLEAR(prop_lines);
2194 }
2195#endif
2196
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002197 /*
2198 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002199 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002200 *
2201 * Move marks from each deleted line to the joined line, adjusting the
2202 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2203 * should not really be a problem.
2204 */
2205 for (t = count - 1; ; --t)
2206 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002207 int spaces_removed;
2208
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002209 cend -= currsize;
2210 mch_memmove(cend, curr, (size_t)currsize);
2211 if (spaces[t] > 0)
2212 {
2213 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002214 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002215 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002216
2217 // If deleting more spaces than adding, the cursor moves no more than
2218 // what is added if it is inside these spaces.
2219 spaces_removed = (curr - curr_start) - spaces[t];
2220
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002221 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002222 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002223 if (t == 0)
2224 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002225#ifdef FEAT_TEXT_PROP
2226 if (prop_lines != NULL)
2227 adjust_props_for_join(curwin->w_cursor.lnum + t,
2228 prop_lines + t - 1, prop_lengths + t - 1,
2229 (long)(cend - newp - spaces_removed), spaces_removed);
2230#endif
2231
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002232 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002233#if defined(FEAT_COMMENTS)
Bram Moolenaar81340392012-06-06 16:12:59 +02002234 if (remove_comments)
2235 curr += comments[t - 1];
2236#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002237 if (insert_space && t > 1)
2238 curr = skipwhite(curr);
2239 currsize = (int)STRLEN(curr);
2240 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002241
2242#ifdef FEAT_TEXT_PROP
2243 if (prop_lines != NULL)
2244 join_prop_lines(curwin->w_cursor.lnum, newp,
2245 prop_lines, prop_lengths, count);
2246 else
2247#endif
2248 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002250 if (setmark)
2251 {
2252 /* Set the '] mark. */
2253 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002254 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002255 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 /* Only report the change in the first line here, del_lines() will report
2258 * the deleted line. */
2259 changed_lines(curwin->w_cursor.lnum, currsize,
2260 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002262 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 * briefly, and then move it back. After del_lines() the cursor may
2264 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 */
2266 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002268 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 curwin->w_cursor.lnum = t;
2270
2271 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002272 * Set the cursor column:
2273 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002274 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002276 curwin->w_cursor.col =
2277 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 curwin->w_set_curswant = TRUE;
2282
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002283theend:
2284 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002285#if defined(FEAT_COMMENTS) || defined(PROTO)
2286 if (remove_comments)
2287 vim_free(comments);
2288#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002289 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290}
2291
2292#ifdef FEAT_COMMENTS
2293/*
2294 * Return TRUE if the two comment leaders given are the same. "lnum" is
2295 * the first line. White-space is ignored. Note that the whole of
2296 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
2297 */
2298 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002299same_leader(
2300 linenr_T lnum,
2301 int leader1_len,
2302 char_u *leader1_flags,
2303 int leader2_len,
2304 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 int idx1 = 0, idx2 = 0;
2307 char_u *p;
2308 char_u *line1;
2309 char_u *line2;
2310
2311 if (leader1_len == 0)
2312 return (leader2_len == 0);
2313
2314 /*
2315 * If first leader has 'f' flag, the lines can be joined only if the
2316 * second line does not have a leader.
2317 * If first leader has 'e' flag, the lines can never be joined.
2318 * If fist leader has 's' flag, the lines can only be joined if there is
2319 * some text after it and the second line has the 'm' flag.
2320 */
2321 if (leader1_flags != NULL)
2322 {
2323 for (p = leader1_flags; *p && *p != ':'; ++p)
2324 {
2325 if (*p == COM_FIRST)
2326 return (leader2_len == 0);
2327 if (*p == COM_END)
2328 return FALSE;
2329 if (*p == COM_START)
2330 {
2331 if (*(ml_get(lnum) + leader1_len) == NUL)
2332 return FALSE;
2333 if (leader2_flags == NULL || leader2_len == 0)
2334 return FALSE;
2335 for (p = leader2_flags; *p && *p != ':'; ++p)
2336 if (*p == COM_MIDDLE)
2337 return TRUE;
2338 return FALSE;
2339 }
2340 }
2341 }
2342
2343 /*
2344 * Get current line and next line, compare the leaders.
2345 * The first line has to be saved, only one line can be locked at a time.
2346 */
2347 line1 = vim_strsave(ml_get(lnum));
2348 if (line1 != NULL)
2349 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002350 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 ;
2352 line2 = ml_get(lnum + 1);
2353 for (idx2 = 0; idx2 < leader2_len; ++idx2)
2354 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002355 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
2357 if (line1[idx1++] != line2[idx2])
2358 break;
2359 }
2360 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01002361 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 ++idx1;
2363 }
2364 vim_free(line1);
2365 }
2366 return (idx2 == leader2_len && idx1 == leader1_len);
2367}
2368#endif
2369
2370/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01002371 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 */
2373 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002374op_format(
2375 oparg_T *oap,
2376 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
2378 long old_line_count = curbuf->b_ml.ml_line_count;
2379
2380 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
2381 * can put it back there. */
2382 curwin->w_cursor = oap->cursor_start;
2383
2384 if (u_save((linenr_T)(oap->start.lnum - 1),
2385 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2386 return;
2387 curwin->w_cursor = oap->start;
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (oap->is_VIsual)
2390 /* When there is no change: need to remove the Visual selection */
2391 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 /* Set '[ mark at the start of the formatted area */
2394 curbuf->b_op_start = oap->start;
2395
2396 /* For "gw" remember the cursor position and put it back below (adjusted
2397 * for joined and split lines). */
2398 if (keep_cursor)
2399 saved_cursor = oap->cursor_start;
2400
Bram Moolenaar81a82092008-03-12 16:27:00 +00002401 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 /*
2404 * Leave the cursor at the first non-blank of the last formatted line.
2405 * If the cursor was moved one line back (e.g. with "Q}") go to the next
2406 * line, so "." will do the next lines.
2407 */
2408 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2409 ++curwin->w_cursor.lnum;
2410 beginline(BL_WHITE | BL_FIX);
2411 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
2412 msgmore(old_line_count);
2413
2414 /* put '] mark on the end of the formatted area */
2415 curbuf->b_op_end = curwin->w_cursor;
2416
2417 if (keep_cursor)
2418 {
2419 curwin->w_cursor = saved_cursor;
2420 saved_cursor.lnum = 0;
2421 }
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (oap->is_VIsual)
2424 {
2425 win_T *wp;
2426
2427 FOR_ALL_WINDOWS(wp)
2428 {
2429 if (wp->w_old_cursor_lnum != 0)
2430 {
2431 /* When lines have been inserted or deleted, adjust the end of
2432 * the Visual area to be redrawn. */
2433 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
2434 wp->w_old_cursor_lnum += old_line_count;
2435 else
2436 wp->w_old_visual_lnum += old_line_count;
2437 }
2438 }
2439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440}
2441
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002442#if defined(FEAT_EVAL) || defined(PROTO)
2443/*
2444 * Implementation of the format operator 'gq' for when using 'formatexpr'.
2445 */
2446 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002447op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002448{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002449 if (oap->is_VIsual)
2450 /* When there is no change: need to remove the Visual selection */
2451 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002452
Bram Moolenaar700303e2010-07-11 17:35:50 +02002453 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
2454 /* As documented: when 'formatexpr' returns non-zero fall back to
2455 * internal formatting. */
2456 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002457}
2458
2459 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002460fex_format(
2461 linenr_T lnum,
2462 long count,
2463 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002464{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002465 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
2466 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002467 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002468 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002469
2470 /*
2471 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002472 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002473 */
2474 set_vim_var_nr(VV_LNUM, lnum);
2475 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00002476 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002477
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002478 /* Make a copy, the option could be changed while calling it. */
2479 fex = vim_strsave(curbuf->b_p_fex);
2480 if (fex == NULL)
2481 return 0;
2482
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002483 /*
2484 * Evaluate the function.
2485 */
2486 if (use_sandbox)
2487 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002488 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002489 if (use_sandbox)
2490 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002491
2492 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002493 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002494
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002495 return r;
2496}
2497#endif
2498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499/*
2500 * Format "line_count" lines, starting at the cursor position.
2501 * When "line_count" is negative, format until the end of the paragraph.
2502 * Lines after the cursor line are saved for undo, caller must have saved the
2503 * first line.
2504 */
2505 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002506format_lines(
2507 linenr_T line_count,
2508 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 int max_len;
2511 int is_not_par; /* current line not part of parag. */
2512 int next_is_not_par; /* next line not part of paragraph */
2513 int is_end_par; /* at end of paragraph */
2514 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
2515 int next_is_start_par = FALSE;
2516#ifdef FEAT_COMMENTS
2517 int leader_len = 0; /* leader len of current line */
2518 int next_leader_len; /* leader len of next line */
2519 char_u *leader_flags = NULL; /* flags for leader of current line */
2520 char_u *next_leader_flags; /* flags for leader of next line */
2521 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002522 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523#endif
2524 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002525 int second_indent = -1; /* indent for second line (comment
2526 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 int do_second_indent;
2528 int do_number_indent;
2529 int do_trail_white;
2530 int first_par_line = TRUE;
2531 int smd_save;
2532 long count;
2533 int need_set_indent = TRUE; /* set indent of next paragraph */
2534 int force_format = FALSE;
2535 int old_State = State;
2536
2537 /* length of a line to force formatting: 3 * 'tw' */
2538 max_len = comp_textwidth(TRUE) * 3;
2539
2540 /* check for 'q', '2' and '1' in 'formatoptions' */
2541#ifdef FEAT_COMMENTS
2542 do_comments = has_format_option(FO_Q_COMS);
2543#endif
2544 do_second_indent = has_format_option(FO_Q_SECOND);
2545 do_number_indent = has_format_option(FO_Q_NUMBER);
2546 do_trail_white = has_format_option(FO_WHITE_PAR);
2547
2548 /*
2549 * Get info about the previous and current line.
2550 */
2551 if (curwin->w_cursor.lnum > 1)
2552 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
2553#ifdef FEAT_COMMENTS
2554 , &leader_len, &leader_flags, do_comments
2555#endif
2556 );
2557 else
2558 is_not_par = TRUE;
2559 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
2560#ifdef FEAT_COMMENTS
2561 , &next_leader_len, &next_leader_flags, do_comments
2562#endif
2563 );
2564 is_end_par = (is_not_par || next_is_not_par);
2565 if (!is_end_par && do_trail_white)
2566 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
2567
2568 curwin->w_cursor.lnum--;
2569 for (count = line_count; count != 0 && !got_int; --count)
2570 {
2571 /*
2572 * Advance to next paragraph.
2573 */
2574 if (advance)
2575 {
2576 curwin->w_cursor.lnum++;
2577 prev_is_end_par = is_end_par;
2578 is_not_par = next_is_not_par;
2579#ifdef FEAT_COMMENTS
2580 leader_len = next_leader_len;
2581 leader_flags = next_leader_flags;
2582#endif
2583 }
2584
2585 /*
2586 * The last line to be formatted.
2587 */
2588 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2589 {
2590 next_is_not_par = TRUE;
2591#ifdef FEAT_COMMENTS
2592 next_leader_len = 0;
2593 next_leader_flags = NULL;
2594#endif
2595 }
2596 else
2597 {
2598 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
2599#ifdef FEAT_COMMENTS
2600 , &next_leader_len, &next_leader_flags, do_comments
2601#endif
2602 );
2603 if (do_number_indent)
2604 next_is_start_par =
2605 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
2606 }
2607 advance = TRUE;
2608 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
2609 if (!is_end_par && do_trail_white)
2610 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
2611
2612 /*
2613 * Skip lines that are not in a paragraph.
2614 */
2615 if (is_not_par)
2616 {
2617 if (line_count < 0)
2618 break;
2619 }
2620 else
2621 {
2622 /*
2623 * For the first line of a paragraph, check indent of second line.
2624 * Don't do this for comments and empty lines.
2625 */
2626 if (first_par_line
2627 && (do_second_indent || do_number_indent)
2628 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002629 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002631 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002632 {
2633#ifdef FEAT_COMMENTS
2634 if (leader_len == 0 && next_leader_len == 0)
2635 {
2636 /* no comment found */
2637#endif
2638 second_indent =
2639 get_indent_lnum(curwin->w_cursor.lnum + 1);
2640#ifdef FEAT_COMMENTS
2641 }
2642 else
2643 {
2644 second_indent = next_leader_len;
2645 do_comments_list = 1;
2646 }
2647#endif
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002650 {
2651#ifdef FEAT_COMMENTS
2652 if (leader_len == 0 && next_leader_len == 0)
2653 {
2654 /* no comment found */
2655#endif
2656 second_indent =
2657 get_number_indent(curwin->w_cursor.lnum);
2658#ifdef FEAT_COMMENTS
2659 }
2660 else
2661 {
2662 /* get_number_indent() is now "comment aware"... */
2663 second_indent =
2664 get_number_indent(curwin->w_cursor.lnum);
2665 do_comments_list = 1;
2666 }
2667#endif
2668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
2670
2671 /*
2672 * When the comment leader changes, it's the end of the paragraph.
2673 */
2674 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
2675#ifdef FEAT_COMMENTS
2676 || !same_leader(curwin->w_cursor.lnum,
2677 leader_len, leader_flags,
2678 next_leader_len, next_leader_flags)
2679#endif
2680 )
2681 is_end_par = TRUE;
2682
2683 /*
2684 * If we have got to the end of a paragraph, or the line is
2685 * getting long, format it.
2686 */
2687 if (is_end_par || force_format)
2688 {
2689 if (need_set_indent)
2690 /* replace indent in first line with minimal number of
2691 * tabs and spaces, according to current options */
2692 (void)set_indent(get_indent(), SIN_CHANGED);
2693
2694 /* put cursor on last non-space */
2695 State = NORMAL; /* don't go past end-of-line */
2696 coladvance((colnr_T)MAXCOL);
2697 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
2698 dec_cursor();
2699
2700 /* do the formatting, without 'showmode' */
2701 State = INSERT; /* for open_line() */
2702 smd_save = p_smd;
2703 p_smd = FALSE;
2704 insertchar(NUL, INSCHAR_FORMAT
2705#ifdef FEAT_COMMENTS
2706 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002707 + (do_comments && do_comments_list
2708 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00002710 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 State = old_State;
2712 p_smd = smd_save;
2713 second_indent = -1;
2714 /* at end of par.: need to set indent of next par. */
2715 need_set_indent = is_end_par;
2716 if (is_end_par)
2717 {
2718 /* When called with a negative line count, break at the
2719 * end of the paragraph. */
2720 if (line_count < 0)
2721 break;
2722 first_par_line = TRUE;
2723 }
2724 force_format = FALSE;
2725 }
2726
2727 /*
2728 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002729 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 */
2731 if (!is_end_par)
2732 {
2733 advance = FALSE;
2734 curwin->w_cursor.lnum++;
2735 curwin->w_cursor.col = 0;
2736 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002740 {
2741 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002743 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002744 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002746 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
2747 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002748 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002749
2750 if (indent > 0)
2751 {
2752 (void)del_bytes(indent, FALSE, FALSE);
2753 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002754 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01002755 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002758 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
2760 beep_flush();
2761 break;
2762 }
2763 first_par_line = FALSE;
2764 /* If the line is getting long, format it next time */
2765 if (STRLEN(ml_get_curline()) > (size_t)max_len)
2766 force_format = TRUE;
2767 else
2768 force_format = FALSE;
2769 }
2770 }
2771 line_breakcheck();
2772 }
2773}
2774
2775/*
2776 * Return TRUE if line "lnum" ends in a white character.
2777 */
2778 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002779ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 char_u *s = ml_get(lnum);
2782 size_t l;
2783
2784 if (*s == NUL)
2785 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002786 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 * invocation may call function multiple times". */
2788 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002789 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
2792/*
2793 * Blank lines, and lines containing only the comment leader, are left
2794 * untouched by the formatting. The function returns TRUE in this
2795 * case. It also returns TRUE when a line starts with the end of a comment
2796 * ('e' in comment flags), so that this line is skipped, and not joined to the
2797 * previous line. A new paragraph starts after a blank line, or when the
2798 * comment leader changes -- webb.
2799 */
2800#ifdef FEAT_COMMENTS
2801 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002802fmt_check_par(
2803 linenr_T lnum,
2804 int *leader_len,
2805 char_u **leader_flags,
2806 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 char_u *flags = NULL; /* init for GCC */
2809 char_u *ptr;
2810
2811 ptr = ml_get(lnum);
2812 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002813 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 else
2815 *leader_len = 0;
2816
2817 if (*leader_len > 0)
2818 {
2819 /*
2820 * Search for 'e' flag in comment leader flags.
2821 */
2822 flags = *leader_flags;
2823 while (*flags && *flags != ':' && *flags != COM_END)
2824 ++flags;
2825 }
2826
2827 return (*skipwhite(ptr + *leader_len) == NUL
2828 || (*leader_len > 0 && *flags == COM_END)
2829 || startPS(lnum, NUL, FALSE));
2830}
2831#else
2832 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002833fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
2836}
2837#endif
2838
2839/*
2840 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
2841 * previous line is in the same paragraph. Used for auto-formatting.
2842 */
2843 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002844paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 char_u *p;
2847#ifdef FEAT_COMMENTS
2848 int leader_len = 0; /* leader len of current line */
2849 char_u *leader_flags = NULL; /* flags for leader of current line */
2850 int next_leader_len; /* leader len of next line */
2851 char_u *next_leader_flags; /* flags for leader of next line */
2852 int do_comments; /* format comments */
2853#endif
2854
2855 if (lnum <= 1)
2856 return TRUE; /* start of the file */
2857
2858 p = ml_get(lnum - 1);
2859 if (*p == NUL)
2860 return TRUE; /* after empty line */
2861
2862#ifdef FEAT_COMMENTS
2863 do_comments = has_format_option(FO_Q_COMS);
2864#endif
2865 if (fmt_check_par(lnum - 1
2866#ifdef FEAT_COMMENTS
2867 , &leader_len, &leader_flags, do_comments
2868#endif
2869 ))
2870 return TRUE; /* after non-paragraph line */
2871
2872 if (fmt_check_par(lnum
2873#ifdef FEAT_COMMENTS
2874 , &next_leader_len, &next_leader_flags, do_comments
2875#endif
2876 ))
2877 return TRUE; /* "lnum" is not a paragraph line */
2878
2879 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
2880 return TRUE; /* missing trailing space in previous line. */
2881
2882 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
2883 return TRUE; /* numbered item starts in "lnum". */
2884
2885#ifdef FEAT_COMMENTS
2886 if (!same_leader(lnum - 1, leader_len, leader_flags,
2887 next_leader_len, next_leader_flags))
2888 return TRUE; /* change of comment leader. */
2889#endif
2890
2891 return FALSE;
2892}
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894/*
2895 * prepare a few things for block mode yank/delete/tilde
2896 *
2897 * for delete:
2898 * - textlen includes the first/last char to be (partly) deleted
2899 * - start/endspaces is the number of columns that are taken by the
2900 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002901 * deleted.
2902 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 * - textlen includes the first/last char to be wholly yanked
2904 * - start/endspaces is the number of columns of the first/last yanked char
2905 * that are to be yanked.
2906 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002908block_prep(
2909 oparg_T *oap,
2910 struct block_def *bdp,
2911 linenr_T lnum,
2912 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 int incr = 0;
2915 char_u *pend;
2916 char_u *pstart;
2917 char_u *line;
2918 char_u *prev_pstart;
2919 char_u *prev_pend;
2920
2921 bdp->startspaces = 0;
2922 bdp->endspaces = 0;
2923 bdp->textlen = 0;
2924 bdp->start_vcol = 0;
2925 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 bdp->is_short = FALSE;
2927 bdp->is_oneChar = FALSE;
2928 bdp->pre_whitesp = 0;
2929 bdp->pre_whitesp_c = 0;
2930 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 bdp->start_char_vcols = 0;
2932
2933 line = ml_get(lnum);
2934 pstart = line;
2935 prev_pstart = line;
2936 while (bdp->start_vcol < oap->start_vcol && *pstart)
2937 {
2938 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002939 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002941 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
2943 bdp->pre_whitesp += incr;
2944 bdp->pre_whitesp_c++;
2945 }
2946 else
2947 {
2948 bdp->pre_whitesp = 0;
2949 bdp->pre_whitesp_c = 0;
2950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002952 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 bdp->start_char_vcols = incr;
2955 if (bdp->start_vcol < oap->start_vcol) /* line too short */
2956 {
2957 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (!is_del || oap->op_type == OP_APPEND)
2960 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2961 }
2962 else
2963 {
2964 /* notice: this converts partly selected Multibyte characters to
2965 * spaces, too. */
2966 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2967 if (is_del && bdp->startspaces)
2968 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2969 pend = pstart;
2970 bdp->end_vcol = bdp->start_vcol;
2971 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
2972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (oap->op_type == OP_INSERT)
2975 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2976 else if (oap->op_type == OP_APPEND)
2977 {
2978 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2979 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2980 }
2981 else
2982 {
2983 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2984 if (is_del && oap->op_type != OP_LSHIFT)
2985 {
2986 /* just putting the sum of those two into
2987 * bdp->startspaces doesn't work for Visual replace,
2988 * so we have to split the tab in two */
2989 bdp->startspaces = bdp->start_char_vcols
2990 - (bdp->start_vcol - oap->start_vcol);
2991 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2992 }
2993 }
2994 }
2995 else
2996 {
2997 prev_pend = pend;
2998 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2999 {
3000 /* Count a tab for what it's worth (if list mode not on) */
3001 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01003002 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 bdp->end_vcol += incr;
3004 }
3005 if (bdp->end_vcol <= oap->end_vcol
3006 && (!is_del
3007 || oap->op_type == OP_APPEND
3008 || oap->op_type == OP_REPLACE)) /* line too short */
3009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 /* Alternative: include spaces to fill up the block.
3012 * Disadvantage: can lead to trailing spaces when the line is
3013 * short where the text is put */
3014 /* if (!is_del || oap->op_type == OP_APPEND) */
3015 if (oap->op_type == OP_APPEND || virtual_op)
3016 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003017 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 else
3019 bdp->endspaces = 0; /* replace doesn't add characters */
3020 }
3021 else if (bdp->end_vcol > oap->end_vcol)
3022 {
3023 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
3024 if (!is_del && bdp->endspaces)
3025 {
3026 bdp->endspaces = incr - bdp->endspaces;
3027 if (pend != pstart)
3028 pend = prev_pend;
3029 }
3030 }
3031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (is_del && bdp->startspaces)
3034 pstart = prev_pstart;
3035 bdp->textlen = (int)(pend - pstart);
3036 }
3037 bdp->textcol = (colnr_T) (pstart - line);
3038 bdp->textstart = pstart;
3039}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01003042 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003045op_addsub(
3046 oparg_T *oap,
3047 linenr_T Prenum1, /* Amount of add/subtract */
3048 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
Bram Moolenaard79e5502016-01-10 22:13:02 +01003050 pos_T pos;
3051 struct block_def bd;
3052 int change_cnt = 0;
3053 linenr_T amount = Prenum1;
3054
Bram Moolenaar6b731882018-11-16 20:54:47 +01003055 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01003056 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01003057 // the call to changed_lines().
3058#ifdef FEAT_FOLDING
3059 disable_fold_update++;
3060#endif
3061
Bram Moolenaard79e5502016-01-10 22:13:02 +01003062 if (!VIsual_active)
3063 {
3064 pos = curwin->w_cursor;
3065 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01003066 {
3067#ifdef FEAT_FOLDING
3068 disable_fold_update--;
3069#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003070 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01003071 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003072 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01003073#ifdef FEAT_FOLDING
3074 disable_fold_update--;
3075#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003076 if (change_cnt)
3077 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
3078 }
3079 else
3080 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003081 int one_change;
3082 int length;
3083 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003084
3085 if (u_save((linenr_T)(oap->start.lnum - 1),
3086 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01003087 {
3088#ifdef FEAT_FOLDING
3089 disable_fold_update--;
3090#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003091 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01003092 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003093
3094 pos = oap->start;
3095 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
3096 {
3097 if (oap->block_mode) /* Visual block mode */
3098 {
3099 block_prep(oap, &bd, pos.lnum, FALSE);
3100 pos.col = bd.textcol;
3101 length = bd.textlen;
3102 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003103 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003104 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003105 curwin->w_cursor.col = 0;
3106 pos.col = 0;
3107 length = (colnr_T)STRLEN(ml_get(pos.lnum));
3108 }
3109 else /* oap->motion_type == MCHAR */
3110 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01003111 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003112 dec(&(oap->end));
3113 length = (colnr_T)STRLEN(ml_get(pos.lnum));
3114 pos.col = 0;
3115 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003116 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003117 pos.col += oap->start.col;
3118 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003119 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003120 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003121 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003122 length = (int)STRLEN(ml_get(oap->end.lnum));
3123 if (oap->end.col >= length)
3124 oap->end.col = length - 1;
3125 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003126 }
3127 }
3128 one_change = do_addsub(oap->op_type, &pos, length, amount);
3129 if (one_change)
3130 {
3131 /* Remember the start position of the first change. */
3132 if (change_cnt == 0)
3133 startpos = curbuf->b_op_start;
3134 ++change_cnt;
3135 }
3136
3137#ifdef FEAT_NETBEANS_INTG
3138 if (netbeans_active() && one_change)
3139 {
3140 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
3141
3142 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
3143 netbeans_inserted(curbuf, pos.lnum, pos.col,
3144 &ptr[pos.col], length);
3145 }
3146#endif
3147 if (g_cmd && one_change)
3148 amount += Prenum1;
3149 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01003150
3151#ifdef FEAT_FOLDING
3152 disable_fold_update--;
3153#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003154 if (change_cnt)
3155 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
3156
3157 if (!change_cnt && oap->is_VIsual)
3158 /* No change: need to remove the Visual selection */
3159 redraw_curbuf_later(INVERTED);
3160
3161 /* Set '[ mark if something changed. Keep the last end
3162 * position from do_addsub(). */
3163 if (change_cnt > 0)
3164 curbuf->b_op_start = startpos;
3165
3166 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003167 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003168 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003169 }
3170}
3171
3172/*
3173 * Add or subtract 'Prenum1' from a number in a line
3174 * op_type is OP_NR_ADD or OP_NR_SUB
3175 *
3176 * Returns TRUE if some character was changed.
3177 */
3178 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003179do_addsub(
3180 int op_type,
3181 pos_T *pos,
3182 int length,
3183 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003184{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 int col;
3186 char_u *buf1;
3187 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003188 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003190 uvarnumber_T n;
3191 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 char_u *ptr;
3193 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 int todel;
3195 int dohex;
3196 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003197 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 int doalp;
3199 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003201 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003202 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003203 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02003204 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003205 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003206 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003207 pos_T startpos;
3208 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaardac13472019-09-16 21:06:21 +02003210 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
3211 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
3212 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
3213 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaard79e5502016-01-10 22:13:02 +01003215 curwin->w_cursor = *pos;
3216 ptr = ml_get(pos->lnum);
3217 col = pos->col;
3218
3219 if (*ptr == NUL)
3220 goto theend;
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 /*
3223 * First check if we are on a hexadecimal number, after the "0x".
3224 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003225 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003227 if (dobin)
3228 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003229 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003230 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003231 if (has_mbyte)
3232 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003233 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003234
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003235 if (dohex)
3236 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003237 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003238 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003239 if (has_mbyte)
3240 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003241 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003242
3243 if ( dobin
3244 && dohex
3245 && ! ((col > 0
3246 && (ptr[col] == 'X'
3247 || ptr[col] == 'x')
3248 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003249 && (!has_mbyte ||
3250 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003251 && vim_isxdigit(ptr[col + 1]))))
3252 {
3253
3254 /* In case of binary/hexadecimal pattern overlap match, rescan */
3255
Bram Moolenaard79e5502016-01-10 22:13:02 +01003256 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003257
3258 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003259 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003260 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003261 if (has_mbyte)
3262 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003263 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003264 }
3265
3266 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003267 && col > 0
3268 && (ptr[col] == 'X'
3269 || ptr[col] == 'x')
3270 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003271 && (!has_mbyte ||
3272 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003273 && vim_isxdigit(ptr[col + 1])) ||
3274 ( dobin
3275 && col > 0
3276 && (ptr[col] == 'B'
3277 || ptr[col] == 'b')
3278 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003279 && (!has_mbyte ||
3280 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003281 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003282 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003283 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003284 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003285 if (has_mbyte)
3286 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003287 }
3288 else
3289 {
3290 /*
3291 * Search forward and then backward to find the start of number.
3292 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003293 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003294
3295 while (ptr[col] != NUL
3296 && !vim_isdigit(ptr[col])
3297 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003298 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003299
3300 while (col > 0
3301 && vim_isdigit(ptr[col - 1])
3302 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003303 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003304 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003305 if (has_mbyte)
3306 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003307 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003308 }
3309 }
3310
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003311 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003312 {
3313 while (ptr[col] != NUL && length > 0
3314 && !vim_isdigit(ptr[col])
3315 && !(doalp && ASCII_ISALPHA(ptr[col])))
3316 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003317 int mb_len = MB_PTR2LEN(ptr + col);
3318
3319 col += mb_len;
3320 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003321 }
3322
3323 if (length == 0)
3324 goto theend;
3325
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003326 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003327 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003328 {
3329 negative = TRUE;
3330 was_positive = FALSE;
3331 }
3332 }
3333
3334 /*
3335 * If a number was found, and saving for undo works, replace the number.
3336 */
3337 firstdigit = ptr[col];
3338 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
3339 {
3340 beep_flush();
3341 goto theend;
3342 }
3343
3344 if (doalp && ASCII_ISALPHA(firstdigit))
3345 {
3346 /* decrement or increment alphabetic character */
3347 if (op_type == OP_NR_SUB)
3348 {
3349 if (CharOrd(firstdigit) < Prenum1)
3350 {
3351 if (isupper(firstdigit))
3352 firstdigit = 'A';
3353 else
3354 firstdigit = 'a';
3355 }
3356 else
3357#ifdef EBCDIC
3358 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
3359#else
3360 firstdigit -= Prenum1;
3361#endif
3362 }
3363 else
3364 {
3365 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
3366 {
3367 if (isupper(firstdigit))
3368 firstdigit = 'Z';
3369 else
3370 firstdigit = 'z';
3371 }
3372 else
3373#ifdef EBCDIC
3374 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
3375#else
3376 firstdigit += Prenum1;
3377#endif
3378 }
3379 curwin->w_cursor.col = col;
3380 if (!did_change)
3381 startpos = curwin->w_cursor;
3382 did_change = TRUE;
3383 (void)del_char(FALSE);
3384 ins_char(firstdigit);
3385 endpos = curwin->w_cursor;
3386 curwin->w_cursor.col = col;
3387 }
3388 else
3389 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003390 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003391 && (!has_mbyte ||
3392 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003393 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003394 {
3395 /* negative number */
3396 --col;
3397 negative = TRUE;
3398 }
3399 /* get the number value (unsigned) */
3400 if (visual && VIsual_mode != 'V')
3401 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
3402 ? (int)STRLEN(ptr) - col
3403 : length);
3404
3405 vim_str2nr(ptr + col, &pre, &length,
3406 0 + (dobin ? STR2NR_BIN : 0)
3407 + (dooct ? STR2NR_OCT : 0)
3408 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003409 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003410
3411 /* ignore leading '-' for hex and octal and bin numbers */
3412 if (pre && negative)
3413 {
3414 ++col;
3415 --length;
3416 negative = FALSE;
3417 }
3418 /* add or subtract */
3419 subtract = FALSE;
3420 if (op_type == OP_NR_SUB)
3421 subtract ^= TRUE;
3422 if (negative)
3423 subtract ^= TRUE;
3424
3425 oldn = n;
3426 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003427 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003428 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003429 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003430 /* handle wraparound for decimal numbers */
3431 if (!pre)
3432 {
3433 if (subtract)
3434 {
3435 if (n > oldn)
3436 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003437 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003438 negative ^= TRUE;
3439 }
3440 }
3441 else
3442 {
3443 /* add */
3444 if (n < oldn)
3445 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003446 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003447 negative ^= TRUE;
3448 }
3449 }
3450 if (n == 0)
3451 negative = FALSE;
3452 }
3453
3454 if (visual && !was_positive && !negative && col > 0)
3455 {
3456 /* need to remove the '-' */
3457 col--;
3458 length++;
3459 }
3460
3461 /*
3462 * Delete the old number.
3463 */
3464 curwin->w_cursor.col = col;
3465 if (!did_change)
3466 startpos = curwin->w_cursor;
3467 did_change = TRUE;
3468 todel = length;
3469 c = gchar_cursor();
3470 /*
3471 * Don't include the '-' in the length, only the length of the
3472 * part after it is kept the same.
3473 */
3474 if (c == '-')
3475 --length;
3476 while (todel-- > 0)
3477 {
3478 if (c < 0x100 && isalpha(c))
3479 {
3480 if (isupper(c))
3481 hexupper = TRUE;
3482 else
3483 hexupper = FALSE;
3484 }
3485 /* del_char() will mark line needing displaying */
3486 (void)del_char(FALSE);
3487 c = gchar_cursor();
3488 }
3489
3490 /*
3491 * Prepare the leading characters in buf1[].
3492 * When there are many leading zeros it could be very long.
3493 * Allocate a bit too much.
3494 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003495 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003496 if (buf1 == NULL)
3497 goto theend;
3498 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003499 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003500 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003501 if (pre)
3502 {
3503 *ptr++ = '0';
3504 --length;
3505 }
3506 if (pre == 'b' || pre == 'B' ||
3507 pre == 'x' || pre == 'X')
3508 {
3509 *ptr++ = pre;
3510 --length;
3511 }
3512
3513 /*
3514 * Put the number characters in buf2[].
3515 */
3516 if (pre == 'b' || pre == 'B')
3517 {
3518 int i;
3519 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003520 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003521
3522 /* leading zeros */
3523 for (bit = bits; bit > 0; bit--)
3524 if ((n >> (bit - 1)) & 0x1) break;
3525
3526 for (i = 0; bit > 0; bit--)
3527 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3528
3529 buf2[i] = '\0';
3530 }
3531 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02003532 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003533 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003534 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02003535 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003536 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003537 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02003538 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003539 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003540 else
Bram Moolenaarea391762018-04-08 13:07:22 +02003541 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003542 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003543 length -= (int)STRLEN(buf2);
3544
3545 /*
3546 * Adjust number of zeros to the new number of digits, so the
3547 * total length of the number remains the same.
3548 * Don't do this when
3549 * the result may look like an octal number.
3550 */
3551 if (firstdigit == '0' && !(dooct && pre == 0))
3552 while (length-- > 0)
3553 *ptr++ = '0';
3554 *ptr = NUL;
3555 STRCAT(buf1, buf2);
3556 ins_str(buf1); /* insert the new number */
3557 vim_free(buf1);
3558 endpos = curwin->w_cursor;
3559 if (did_change && curwin->w_cursor.col)
3560 --curwin->w_cursor.col;
3561 }
3562
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003563 if (did_change)
3564 {
3565 /* set the '[ and '] marks */
3566 curbuf->b_op_start = startpos;
3567 curbuf->b_op_end = endpos;
3568 if (curbuf->b_op_end.col > 0)
3569 --curbuf->b_op_end.col;
3570 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003571
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003572theend:
3573 if (visual)
3574 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003575 else if (did_change)
3576 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003577
Bram Moolenaard79e5502016-01-10 22:13:02 +01003578 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579}
3580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581#if defined(FEAT_CLIPBOARD) || defined(PROTO)
3582/*
3583 * SELECTION / PRIMARY ('*')
3584 *
3585 * Text selection stuff that uses the GUI selection register '*'. When using a
3586 * GUI this may be text from another window, otherwise it is the last text we
3587 * had highlighted with VIsual mode. With mouse support, clicking the middle
3588 * button performs the paste, otherwise you will need to do <"*p>. "
3589 * If not under X, it is synonymous with the clipboard register '+'.
3590 *
3591 * X CLIPBOARD ('+')
3592 *
3593 * Text selection stuff that uses the GUI clipboard register '+'.
3594 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
3595 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
3596 * otherwise you will need to do <"+p>. "
3597 * If not under X, it is synonymous with the selection register '*'.
3598 */
3599
3600/*
3601 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003602 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 * only exist while the owning application exists, so we write to the
3604 * permanent (while X runs) store CUT_BUFFER0.
3605 * Dump the CLIPBOARD selection if we own it (it's logically the more
3606 * 'permanent' of the two), otherwise the PRIMARY one.
3607 * For now, use a hard-coded sanity limit of 1Mb of data.
3608 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003609#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003611x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612{
3613 Display *dpy;
3614 char_u *str = NULL;
3615 long_u len = 0;
3616 int motion_type = -1;
3617
3618# ifdef FEAT_GUI
3619 if (gui.in_use)
3620 dpy = X_DISPLAY;
3621 else
3622# endif
3623# ifdef FEAT_XCLIPBOARD
3624 dpy = xterm_dpy;
3625# else
3626 return;
3627# endif
3628
3629 /* Get selection to export */
3630 if (clip_plus.owned)
3631 motion_type = clip_convert_selection(&str, &len, &clip_plus);
3632 else if (clip_star.owned)
3633 motion_type = clip_convert_selection(&str, &len, &clip_star);
3634
3635 /* Check it's OK */
3636 if (dpy != NULL && str != NULL && motion_type >= 0
3637 && len < 1024*1024 && len > 0)
3638 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003639 int ok = TRUE;
3640
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003641 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
3642 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
3643 * encoding conversion usually doesn't work, so keep the text as-is.
3644 */
3645 if (has_mbyte)
3646 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003647 vimconv_T vc;
3648
3649 vc.vc_type = CONV_NONE;
3650 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
3651 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003652 int intlen = len;
3653 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003654
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003655 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003656 conv_str = string_convert(&vc, str, &intlen);
3657 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003658 if (conv_str != NULL)
3659 {
3660 vim_free(str);
3661 str = conv_str;
3662 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003663 else
3664 {
3665 ok = FALSE;
3666 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003667 convert_setup(&vc, NULL, NULL);
3668 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003669 else
3670 {
3671 ok = FALSE;
3672 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003673 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003674
3675 /* Do not store the string if conversion failed. Better to use any
3676 * other selection than garbled text. */
3677 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003678 {
3679 XStoreBuffer(dpy, (char *)str, (int)len, 0);
3680 XFlush(dpy);
3681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683
3684 vim_free(str);
3685}
3686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687#endif /* FEAT_CLIPBOARD || PROTO */
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003690clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
3692 vim_memset(oap, 0, sizeof(oparg_T));
3693}
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003696 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 *
3698 * "Words" are counted by looking for boundaries between non-space and
3699 * space characters. (it seems to produce results that match 'wc'.)
3700 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003701 * Return value is byte count; word count for the line is added to "*wc".
3702 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 *
3704 * The function will only examine the first "limit" characters in the
3705 * line, stopping if it encounters an end-of-line (NUL byte). In that
3706 * case, eol_size will be added to the character count to account for
3707 * the size of the EOL character.
3708 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003709 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710line_count_info(
3711 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003712 varnumber_T *wc,
3713 varnumber_T *cc,
3714 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003715 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003717 varnumber_T i;
3718 varnumber_T words = 0;
3719 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 int is_word = 0;
3721
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003722 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
3724 if (is_word)
3725 {
3726 if (vim_isspace(line[i]))
3727 {
3728 words++;
3729 is_word = 0;
3730 }
3731 }
3732 else if (!vim_isspace(line[i]))
3733 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003734 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003735 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
3738 if (is_word)
3739 words++;
3740 *wc += words;
3741
3742 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003743 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003746 chars += eol_size;
3747 }
3748 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 return i;
3750}
3751
3752/*
3753 * Give some info about the position of the cursor (for "g CTRL-G").
3754 * In Visual mode, give some info about the selected region. (In this case,
3755 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003756 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 */
3758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003759cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
3761 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003762 char_u buf1[50];
3763 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003765 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003766 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003767 varnumber_T byte_count_cursor = 0;
3768 varnumber_T char_count = 0;
3769 varnumber_T char_count_cursor = 0;
3770 varnumber_T word_count = 0;
3771 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003772 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003773 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 long line_count_selected = 0;
3775 pos_T min_pos, max_pos;
3776 oparg_T oparg;
3777 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 /*
3780 * Compute the length of the file in characters.
3781 */
3782 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3783 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003784 if (dict == NULL)
3785 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003786 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003787 return;
3788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 else
3791 {
3792 if (get_fileformat(curbuf) == EOL_DOS)
3793 eol_size = 2;
3794 else
3795 eol_size = 1;
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (VIsual_active)
3798 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003799 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
3801 min_pos = VIsual;
3802 max_pos = curwin->w_cursor;
3803 }
3804 else
3805 {
3806 min_pos = curwin->w_cursor;
3807 max_pos = VIsual;
3808 }
3809 if (*p_sel == 'e' && max_pos.col > 0)
3810 --max_pos.col;
3811
3812 if (VIsual_mode == Ctrl_V)
3813 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003814#ifdef FEAT_LINEBREAK
3815 char_u * saved_sbr = p_sbr;
3816
3817 /* Make 'sbr' empty for a moment to get the correct size. */
3818 p_sbr = empty_option;
3819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 oparg.is_VIsual = 1;
3821 oparg.block_mode = TRUE;
3822 oparg.op_type = OP_NOP;
3823 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003824 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003825#ifdef FEAT_LINEBREAK
3826 p_sbr = saved_sbr;
3827#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003828 if (curwin->w_curswant == MAXCOL)
3829 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 /* Swap the start, end vcol if needed */
3831 if (oparg.end_vcol < oparg.start_vcol)
3832 {
3833 oparg.end_vcol += oparg.start_vcol;
3834 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3835 oparg.end_vcol -= oparg.start_vcol;
3836 }
3837 }
3838 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
3841 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3842 {
3843 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003844 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 ui_breakcheck();
3847 if (got_int)
3848 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003849 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 /* Do extra processing for VIsual mode. */
3853 if (VIsual_active
3854 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3855 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003856 char_u *s = NULL;
3857 long len = 0L;
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 switch (VIsual_mode)
3860 {
3861 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003865 s = bd.textstart;
3866 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 break;
3868 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003869 s = ml_get(lnum);
3870 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 break;
3872 case 'v':
3873 {
3874 colnr_T start_col = (lnum == min_pos.lnum)
3875 ? min_pos.col : 0;
3876 colnr_T end_col = (lnum == max_pos.lnum)
3877 ? max_pos.col - start_col + 1 : MAXCOL;
3878
Bram Moolenaardef9e822004-12-31 20:58:58 +00003879 s = ml_get(lnum) + start_col;
3880 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882 break;
3883 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003884 if (s != NULL)
3885 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003886 byte_count_cursor += line_count_info(s, &word_count_cursor,
3887 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003888 if (lnum == curbuf->b_ml.ml_line_count
3889 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003890 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003891 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003892 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 /* In non-visual mode, check for the line the cursor is on */
3898 if (lnum == curwin->w_cursor.lnum)
3899 {
3900 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003901 char_count_cursor += char_count;
3902 byte_count_cursor = byte_count +
3903 line_count_info(ml_get(lnum),
3904 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003905 (varnumber_T)(curwin->w_cursor.col + 1),
3906 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003910 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003911 &char_count, (varnumber_T)MAXCOL,
3912 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914
3915 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003916 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003917 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
Bram Moolenaared767a22016-01-03 22:49:16 +01003919 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003921 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003923 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3924 {
3925 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3926 &max_pos.col);
3927 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3928 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3929 }
3930 else
3931 buf1[0] = NUL;
3932
3933 if (char_count_cursor == byte_count_cursor
3934 && char_count == byte_count)
3935 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003936 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003937 buf1, line_count_selected,
3938 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003939 (long_long_T)word_count_cursor,
3940 (long_long_T)word_count,
3941 (long_long_T)byte_count_cursor,
3942 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003943 else
3944 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003945 _("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 +01003946 buf1, line_count_selected,
3947 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003948 (long_long_T)word_count_cursor,
3949 (long_long_T)word_count,
3950 (long_long_T)char_count_cursor,
3951 (long_long_T)char_count,
3952 (long_long_T)byte_count_cursor,
3953 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003956 {
3957 p = ml_get_curline();
3958 validate_virtcol();
3959 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3960 (int)curwin->w_virtcol + 1);
3961 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3962 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaared767a22016-01-03 22:49:16 +01003964 if (char_count_cursor == byte_count_cursor
3965 && char_count == byte_count)
3966 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003967 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003968 (char *)buf1, (char *)buf2,
3969 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003971 (long_long_T)word_count_cursor, (long_long_T)word_count,
3972 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003973 else
3974 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003975 _("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 +01003976 (char *)buf1, (char *)buf2,
3977 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003978 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003979 (long_long_T)word_count_cursor, (long_long_T)word_count,
3980 (long_long_T)char_count_cursor, (long_long_T)char_count,
3981 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003982 }
3983 }
3984
Bram Moolenaared767a22016-01-03 22:49:16 +01003985 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003986 if (dict == NULL && bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01003987 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003988 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003989 if (dict == NULL)
3990 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003991 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003992 p = p_shm;
3993 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003994 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003995 p_shm = p;
3996 }
3997 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003998#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003999 if (dict != NULL)
4000 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004001 dict_add_number(dict, "words", word_count);
4002 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004003 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004004 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
4005 byte_count_cursor);
4006 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
4007 char_count_cursor);
4008 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
4009 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01004011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012}