blob: df765ef2f2c4ba1feab1b35d1d49847c3bfeb730 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010017static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010020static int ends_in_white(linenr_T lnum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010021static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
Bram Moolenaarf2732452018-06-03 14:47:35 +020023// Flags for third item in "opchars".
24#define OPF_LINES 1 // operator always works on lines
25#define OPF_CHANGE 2 // operator changes text
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027/*
28 * The names of operators.
29 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020030 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 */
32static char opchars[][3] =
33{
Bram Moolenaarf2732452018-06-03 14:47:35 +020034 {NUL, NUL, 0}, // OP_NOP
35 {'d', NUL, OPF_CHANGE}, // OP_DELETE
36 {'y', NUL, 0}, // OP_YANK
37 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
38 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
39 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
40 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
41 {'g', '~', OPF_CHANGE}, // OP_TILDE
42 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
43 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
44 {':', NUL, OPF_LINES}, // OP_COLON
45 {'g', 'U', OPF_CHANGE}, // OP_UPPER
46 {'g', 'u', OPF_CHANGE}, // OP_LOWER
47 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
48 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
49 {'g', '?', OPF_CHANGE}, // OP_ROT13
50 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
51 {'I', NUL, OPF_CHANGE}, // OP_INSERT
52 {'A', NUL, OPF_CHANGE}, // OP_APPEND
53 {'z', 'f', OPF_LINES}, // OP_FOLD
54 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
55 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
56 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
57 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
58 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
59 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
60 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
61 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
62 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
63 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000064};
65
66/*
67 * Translate a command name into an operator type.
68 * Must only be called with a valid operator name!
69 */
70 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010071get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
73 int i;
74
75 if (char1 == 'r') /* ignore second character */
76 return OP_REPLACE;
77 if (char1 == '~') /* when tilde is an operator */
78 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +010079 if (char1 == 'g' && char2 == Ctrl_A) /* add */
80 return OP_NR_ADD;
81 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
82 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 if (opchars[i][0] == char1 && opchars[i][1] == char2)
86 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +010087 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
88 {
89 internal_error("get_op_type()");
90 break;
91 }
92 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return i;
94}
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * Return TRUE if operator "op" always works on whole lines.
98 */
99 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100100op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200102 return opchars[op][2] & OPF_LINES;
103}
104
Bram Moolenaar113e1072019-01-20 15:30:40 +0100105#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200106/*
107 * Return TRUE if operator "op" changes text.
108 */
109 int
110op_is_change(int op)
111{
112 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
116/*
117 * Get first operator command character.
118 * Returns 'g' or 'z' if there is another command character.
119 */
120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100121get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return opchars[optype][0];
124}
125
126/*
127 * Get second operator command character.
128 */
129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100130get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 return opchars[optype][1];
133}
134
135/*
136 * op_shift - handle a shift operation
137 */
138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100139op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 long i;
142 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145 if (u_save((linenr_T)(oap->start.lnum - 1),
146 (linenr_T)(oap->end.lnum + 1)) == FAIL)
147 return;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 if (oap->block_mode)
150 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152 for (i = oap->line_count; --i >= 0; )
153 {
154 first_char = *ml_get_curline();
155 if (first_char == NUL) /* empty line */
156 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 else if (oap->block_mode)
158 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 else
160 /* Move the line right if it doesn't start with '#', 'smartindent'
161 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
162#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
163 if (first_char != '#' || !preprocs_left())
164#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000165 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++curwin->w_cursor.lnum;
167 }
168
169 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (oap->block_mode)
171 {
172 curwin->w_cursor.lnum = oap->start.lnum;
173 curwin->w_cursor.col = block_col;
174 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100175 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 {
177 curwin->w_cursor.lnum = oap->start.lnum;
178 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
179 }
180 else
181 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
182
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183#ifdef FEAT_FOLDING
184 /* The cursor line is not in a closed fold */
185 foldOpenCursor();
186#endif
187
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (oap->line_count > p_report)
190 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200191 char *op;
192 char *msg_line_single;
193 char *msg_line_plural;
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200196 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200198 op = "<";
199 msg_line_single = NGETTEXT("%ld line %sed %d time",
200 "%ld line %sed %d times", amount);
201 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
202 "%ld lines %sed %d times", amount);
203 vim_snprintf((char *)IObuff, IOSIZE,
204 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
205 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100206 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
208
209 /*
210 * Set "'[" and "']" marks.
211 */
212 curbuf->b_op_start = oap->start;
213 curbuf->b_op_end.lnum = oap->end.lnum;
214 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
215 if (curbuf->b_op_end.col > 0)
216 --curbuf->b_op_end.col;
217}
218
219/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100220 * Shift the current line one shiftwidth left (if left != 0) or right
221 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 */
223 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100224shift_line(
225 int left,
226 int round,
227 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200228 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
230 int count;
231 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200232 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200234 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200236 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200238 i = count / sw_val; // number of 'shiftwidth' rounded down
239 j = count % sw_val; // extra spaces
240 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 --amount;
242 if (left)
243 {
244 i -= amount;
245 if (i < 0)
246 i = 0;
247 }
248 else
249 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200250 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200252 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
254 if (left)
255 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200256 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 if (count < 0)
258 count = 0;
259 }
260 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200261 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
263
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200264 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000266 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000268 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269}
270
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271/*
272 * Shift one line of the current block one shiftwidth right or left.
273 * Leaves cursor on first character in block.
274 */
275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100276shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277{
278 int left = (oap->op_type == OP_LSHIFT);
279 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000280 int total;
281 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200283 int sw_val = (int)get_sw_value_indent(curbuf);
284 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000287 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 int i = 0, j = 0;
289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#ifdef FEAT_RIGHTLEFT
291 int old_p_ri = p_ri;
292
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200293 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#endif
295
296 State = INSERT; /* don't want REPLACE for State */
297 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
298 if (bd.is_short)
299 return;
300
301 /* total is number of screen columns to be inserted/removed */
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 total = (int)((unsigned)amount * (unsigned)sw_val);
303 if ((total / sw_val) != amount)
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200304 return; /* multiplication overflow */
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 oldp = ml_get_curline();
307
308 if (!left)
309 {
310 /*
311 * 1. Get start vcol
312 * 2. Total ws vcols
313 * 3. Divvy into TABs & spp
314 * 4. Construct new string
315 */
316 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
317 ws_vcol = bd.start_vcol - bd.pre_whitesp;
318 if (bd.startspaces)
319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100321 {
322 if ((*mb_ptr2len)(bd.textstart) == 1)
323 ++bd.textstart;
324 else
325 {
326 ws_vcol = 0;
327 bd.startspaces = 0;
328 }
329 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000330 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000331 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100333 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200335 /* TODO: is passing bd.textstart for start of the line OK? */
336 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
337 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 total += incr;
339 bd.start_vcol += incr;
340 }
341 /* OK, now total=all the VWS reqd, and textstart points at the 1st
342 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200343#ifdef FEAT_VARTABS
344 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200345 tabstop_fromto(ws_vcol, ws_vcol + total,
346 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200347 else
348 j = total;
349#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200351 i = ((ws_vcol % ts_val) + total) / ts_val; /* number of tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (i)
Bram Moolenaardac13472019-09-16 21:06:21 +0200353 j = ((ws_vcol % ts_val) + total) % ts_val; /* number of spp */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 else
355 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 /* if we're splitting a TAB, allow for it */
358 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
359 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200360 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 if (newp == NULL)
362 return;
363 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
364 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200365 vim_memset(newp + bd.textcol, TAB, (size_t)i);
366 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 /* the end */
368 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
369 }
370 else /* left */
371 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000372 colnr_T destination_col; /* column to which text in block will
373 be shifted */
374 char_u *verbatim_copy_end; /* end of the part of the line which is
375 copied verbatim */
376 colnr_T verbatim_copy_width;/* the (displayed) width of this part
377 of line */
378 unsigned fill; /* nr of spaces that replace a TAB */
379 unsigned new_line_len; /* the length of the line after the
380 block shift */
381 size_t block_space_width;
382 size_t shift_amount;
383 char_u *non_white = bd.textstart;
384 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000386 /*
387 * Firstly, let's find the first non-whitespace character that is
388 * displayed after the block's start column and the character's column
389 * number. Also, let's calculate the width of all the whitespace
390 * characters that are displayed in the block and precede the searched
391 * non-whitespace character.
392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000394 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
395 * the part of which is displayed at the block's beginning. Let's start
396 * searching from the next character. */
397 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100398 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000399
400 /* The character's column is in "bd.start_vcol". */
401 non_white_col = bd.start_vcol;
402
Bram Moolenaar1c465442017-03-12 20:10:05 +0100403 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000404 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200405 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000406 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000409 block_space_width = non_white_col - oap->start_vcol;
410 /* We will shift by "total" or "block_space_width", whichever is less.
411 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000412 shift_amount = (block_space_width < (size_t)total
413 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000414
415 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000416 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417
418 /* Now let's find out how much of the beginning of the line we can
419 * reuse without modification. */
420 verbatim_copy_end = bd.textstart;
421 verbatim_copy_width = bd.start_vcol;
422
423 /* If "bd.startspaces" is set, "bd.textstart" points to the character
424 * preceding the block. We have to subtract its width to obtain its
425 * column number. */
426 if (bd.startspaces)
427 verbatim_copy_width -= bd.start_char_vcols;
428 while (verbatim_copy_width < destination_col)
429 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200430 char_u *line = verbatim_copy_end;
431
432 /* TODO: is passing verbatim_copy_end for start of the line OK? */
433 incr = lbr_chartabsize(line, verbatim_copy_end,
434 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000435 if (verbatim_copy_width + incr > destination_col)
436 break;
437 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100438 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000439 }
440
441 /* If "destination_col" is different from the width of the initial
442 * part of the line that will be copied, it means we encountered a tab
443 * character, which we will have to partly replace with spaces. */
444 fill = destination_col - verbatim_copy_width;
445
446 /* The replacement line will consist of:
447 * - the beginning of the original line up to "verbatim_copy_end",
448 * - "fill" number of spaces,
449 * - the rest of the line, pointed to by non_white. */
450 new_line_len = (unsigned)(verbatim_copy_end - oldp)
451 + fill
452 + (unsigned)STRLEN(non_white) + 1;
453
Bram Moolenaar964b3742019-05-24 18:54:09 +0200454 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (newp == NULL)
456 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200458 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000459 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
461 /* replace the line */
462 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
463 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
464 State = oldstate;
465 curwin->w_cursor.col = oldcol;
466#ifdef FEAT_RIGHTLEFT
467 p_ri = old_p_ri;
468#endif
469}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * Insert string "s" (b_insert ? before : after) block :AKelly
473 * Caller must prepare for undo.
474 */
475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100476block_insert(
477 oparg_T *oap,
478 char_u *s,
479 int b_insert,
480 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaardac13472019-09-16 21:06:21 +0200482 int ts_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 int count = 0; /* extra spaces to replace a cut TAB */
484 int spaces = 0; /* non-zero if cutting a TAB */
485 colnr_T offset; /* pointer along new line */
486 unsigned s_len; /* STRLEN(s) */
487 char_u *newp, *oldp; /* new, old lines */
488 linenr_T lnum; /* loop var */
489 int oldstate = State;
490
491 State = INSERT; /* don't want REPLACE for State */
492 s_len = (unsigned)STRLEN(s);
493
494 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
495 {
496 block_prep(oap, bdp, lnum, TRUE);
497 if (bdp->is_short && b_insert)
498 continue; /* OP_INSERT, line ends before block start */
499
500 oldp = ml_get(lnum);
501
502 if (b_insert)
503 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200504 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 spaces = bdp->startspaces;
506 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200507 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 offset = bdp->textcol;
509 }
510 else /* append */
511 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200512 ts_val = bdp->end_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (!bdp->is_short) /* spaces = padding after block */
514 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200515 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200517 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 offset = bdp->textcol + bdp->textlen - (spaces != 0);
519 }
520 else /* spaces = padding to block edge */
521 {
522 /* if $ used, just append to EOL (ie spaces==0) */
523 if (!bdp->is_MAX)
524 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
525 count = spaces;
526 offset = bdp->textcol + bdp->textlen;
527 }
528 }
529
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200530 if (has_mbyte && spaces > 0)
531 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100532 int off;
533
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200534 /* Avoid starting halfway a multi-byte character. */
535 if (b_insert)
536 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100537 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200538 }
539 else
540 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100541 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200542 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200543 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100544 spaces -= off;
545 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200546 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200547
Bram Moolenaar964b3742019-05-24 18:54:09 +0200548 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (newp == NULL)
550 continue;
551
552 /* copy up to shifted part */
553 mch_memmove(newp, oldp, (size_t)(offset));
554 oldp += offset;
555
556 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200557 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
559 /* copy the new text */
560 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
561 offset += s_len;
562
563 if (spaces && !bdp->is_short)
564 {
565 /* insert post-padding */
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 /* We're splitting a TAB, don't copy it. */
568 oldp++;
569 /* We allowed for that TAB, remember this now */
570 count++;
571 }
572
573 if (spaces > 0)
574 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000575 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577 ml_replace(lnum, newp, FALSE);
578
579 if (lnum == oap->end.lnum)
580 {
581 /* Set "']" mark to the end of the block instead of the end of
582 * the insert in the first line. */
583 curbuf->b_op_end.lnum = oap->end.lnum;
584 curbuf->b_op_end.col = offset;
585 }
586 } /* for all lnum */
587
588 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
589
590 State = oldstate;
591}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
594/*
595 * op_reindent - handle reindenting a block of lines.
596 */
597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 long i;
601 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200602 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 linenr_T first_changed = 0;
604 linenr_T last_changed = 0;
605 linenr_T start_lnum = curwin->w_cursor.lnum;
606
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000607 /* Don't even try when 'modifiable' is off. */
608 if (!curbuf->b_p_ma)
609 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100610 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000611 return;
612 }
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 for (i = oap->line_count; --i >= 0 && !got_int; )
615 {
616 /* it's a slow thing to do, so give feedback so there's no worry that
617 * the computer's just hung. */
618
619 if (i > 1
620 && (i % 50 == 0 || i == oap->line_count - 1)
621 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100622 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 /*
625 * Be vi-compatible: For lisp indenting the first line is not
626 * indented, unless there is only one line.
627 */
628#ifdef FEAT_LISP
629 if (i != oap->line_count - 1 || oap->line_count == 1
630 || how != get_lisp_indent)
631#endif
632 {
633 l = skipwhite(ml_get_curline());
634 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200635 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200637 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200639 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
641 /* did change the indent, call changed_lines() later */
642 if (first_changed == 0)
643 first_changed = curwin->w_cursor.lnum;
644 last_changed = curwin->w_cursor.lnum;
645 }
646 }
647 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000648 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650
651 /* put cursor on first non-blank of indented line */
652 curwin->w_cursor.lnum = start_lnum;
653 beginline(BL_SOL | BL_FIX);
654
655 /* Mark changed lines so that they will be redrawn. When Visual
656 * highlighting was present, need to continue until the last line. When
657 * there is no change still need to remove the Visual highlighting. */
658 if (last_changed != 0)
659 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 else if (oap->is_VIsual)
663 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
665 if (oap->line_count > p_report)
666 {
667 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100668 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200669 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 /* set '[ and '] marks */
672 curbuf->b_op_start = oap->start;
673 curbuf->b_op_end = oap->end;
674}
675#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677/*
678 * Stuff a string into the typeahead buffer, such that edit() will insert it
679 * literally ("literally" TRUE) or interpret is as typed characters.
680 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100682stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 int c;
685 char_u *start;
686
687 while (*arg != NUL)
688 {
689 /* Stuff a sequence of normal ASCII characters, that's fast. Also
690 * stuff K_SPECIAL to get the effect of a special key when "literally"
691 * is TRUE. */
692 start = arg;
693 while ((*arg >= ' '
694#ifndef EBCDIC
695 && *arg < DEL /* EBCDIC: chars above space are normal */
696#endif
697 )
698 || (*arg == K_SPECIAL && !literally))
699 ++arg;
700 if (arg > start)
701 stuffReadbuffLen(start, (long)(arg - start));
702
703 /* stuff a single special character */
704 if (*arg != NUL)
705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +0200707 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 c = *arg++;
710 if (literally && ((c < ' ' && c != TAB) || c == DEL))
711 stuffcharReadbuff(Ctrl_V);
712 stuffcharReadbuff(c);
713 }
714 }
715}
716
717/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200718 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200720 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 */
722 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100723op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
725 int n;
726 linenr_T lnum;
727 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 char_u *newp, *oldp;
729 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
731 int did_yank = FALSE;
732
733 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
734 return OK;
735
736 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
737 if (oap->empty)
738 return u_save_cursor();
739
740 if (!curbuf->b_p_ma)
741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 return FAIL;
744 }
745
746#ifdef FEAT_CLIPBOARD
747 adjust_clip_reg(&oap->regname);
748#endif
749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (has_mbyte)
751 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaard04b7502010-07-08 22:27:55 +0200753 /*
754 * Imitate the strange Vi behaviour: If the delete spans more than one
755 * line and motion_type == MCHAR and the result is a blank line, make the
756 * delete linewise. Don't do this for the change command or Visual mode.
757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000760 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100762 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 && oap->op_type == OP_DELETE)
764 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200765 ptr = ml_get(oap->end.lnum) + oap->end.col;
766 if (*ptr != NUL)
767 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 ptr = skipwhite(ptr);
769 if (*ptr == NUL && inindent(0))
770 oap->motion_type = MLINE;
771 }
772
Bram Moolenaard04b7502010-07-08 22:27:55 +0200773 /*
774 * Check for trying to delete (e.g. "D") in an empty line.
775 * Note: For the change operator it is ok.
776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if ( oap->motion_type == MCHAR
778 && oap->line_count == 1
779 && oap->op_type == OP_DELETE
780 && *ml_get(oap->start.lnum) == NUL)
781 {
782 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000783 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * 'cpoptions' (Vi compatible).
785 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000786 if (virtual_op)
787 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
788 * marks as if it happened. */
789 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
791 beep_flush();
792 return OK;
793 }
794
Bram Moolenaard04b7502010-07-08 22:27:55 +0200795 /*
796 * Do a yank of whatever we're about to delete.
797 * If a yank register was specified, put the deleted text into that
798 * register. For the black hole register '_' don't yank anything.
799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (oap->regname != '_')
801 {
802 if (oap->regname != 0)
803 {
804 /* check for read-only register */
805 if (!valid_yank_reg(oap->regname, TRUE))
806 {
807 beep_flush();
808 return OK;
809 }
810 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
811 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
812 did_yank = TRUE;
813 }
814
815 /*
816 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100817 * delete contains a line break, or when using a specific operator (Vi
818 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200819 * Use the register name from before adjust_clip_reg() may have
820 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100822 if (oap->motion_type == MLINE || oap->line_count > 1
823 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100825 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (op_yank(oap, TRUE, FALSE) == OK)
827 did_yank = TRUE;
828 }
829
Bram Moolenaar84298db2012-04-20 13:46:08 +0200830 /* Yank into small delete register when no named register specified
831 * and the delete is within one line. */
832 if ((
833#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200834 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
835 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200836#endif
837 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 && oap->line_count == 1)
839 {
840 oap->regname = '-';
841 get_yank_register(oap->regname, TRUE);
842 if (op_yank(oap, TRUE, FALSE) == OK)
843 did_yank = TRUE;
844 oap->regname = 0;
845 }
846
847 /*
848 * If there's too much stuff to fit in the yank register, then get a
849 * confirmation before doing the delete. This is crude, but simple.
850 * And it avoids doing a delete of something we can't put back if we
851 * want.
852 */
853 if (!did_yank)
854 {
855 int msg_silent_save = msg_silent;
856
857 msg_silent = 0; /* must display the prompt */
858 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
859 msg_silent = msg_silent_save;
860 if (n != 'y')
861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100862 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 return FAIL;
864 }
865 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100866
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100867#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100868 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200869 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872
Bram Moolenaard04b7502010-07-08 22:27:55 +0200873 /*
874 * block mode delete
875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (oap->block_mode)
877 {
878 if (u_save((linenr_T)(oap->start.lnum - 1),
879 (linenr_T)(oap->end.lnum + 1)) == FAIL)
880 return FAIL;
881
882 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
883 {
884 block_prep(oap, &bd, lnum, TRUE);
885 if (bd.textlen == 0) /* nothing to delete */
886 continue;
887
888 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
889 if (lnum == curwin->w_cursor.lnum)
890 {
891 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 }
894
Bram Moolenaar8055d172019-05-17 22:57:26 +0200895 // "n" == number of chars deleted
896 // If we delete a TAB, it may be replaced by several characters.
897 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 n = bd.textlen - bd.startspaces - bd.endspaces;
899 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200900 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (newp == NULL)
902 continue;
903 /* copy up to deleted part */
904 mch_memmove(newp, oldp, (size_t)bd.textcol);
905 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200906 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 (size_t)(bd.startspaces + bd.endspaces));
908 /* copy the part after the deleted part */
909 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000910 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 /* replace the line */
912 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200913
914#ifdef FEAT_TEXT_PROP
915 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200916 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 check_cursor_col();
921 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
922 oap->end.lnum + 1, 0L);
923 oap->line_count = 0; /* no lines deleted */
924 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100925 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
927 if (oap->op_type == OP_CHANGE)
928 {
929 /* Delete the lines except the first one. Temporarily move the
930 * cursor to the next line. Save the current line number, if the
931 * last line is deleted it may be changed.
932 */
933 if (oap->line_count > 1)
934 {
935 lnum = curwin->w_cursor.lnum;
936 ++curwin->w_cursor.lnum;
937 del_lines((long)(oap->line_count - 1), TRUE);
938 curwin->w_cursor.lnum = lnum;
939 }
940 if (u_save_cursor() == FAIL)
941 return FAIL;
942 if (curbuf->b_p_ai) /* don't delete indent */
943 {
944 beginline(BL_WHITE); /* cursor on first non-white */
945 did_ai = TRUE; /* delete the indent when ESC hit */
946 ai_col = curwin->w_cursor.col;
947 }
948 else
949 beginline(0); /* cursor in column 0 */
950 truncate_line(FALSE); /* delete the rest of the line */
951 /* leave cursor past last char in line */
952 if (oap->line_count > 1)
953 u_clearline(); /* "U" command not possible after "2cc" */
954 }
955 else
956 {
957 del_lines(oap->line_count, TRUE);
958 beginline(BL_WHITE | BL_FIX);
959 u_clearline(); /* "U" command not possible after "dd" */
960 }
961 }
962 else
963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (virtual_op)
965 {
966 int endcol = 0;
967
968 /* For virtualedit: break the tabs that are partly included. */
969 if (gchar_pos(&oap->start) == '\t')
970 {
971 if (u_save_cursor() == FAIL) /* save first line for undo */
972 return FAIL;
973 if (oap->line_count == 1)
974 endcol = getviscol2(oap->end.col, oap->end.coladd);
975 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
976 oap->start = curwin->w_cursor;
977 if (oap->line_count == 1)
978 {
979 coladvance(endcol);
980 oap->end.col = curwin->w_cursor.col;
981 oap->end.coladd = curwin->w_cursor.coladd;
982 curwin->w_cursor = oap->start;
983 }
984 }
985
986 /* Break a tab only when it's included in the area. */
987 if (gchar_pos(&oap->end) == '\t'
988 && (int)oap->end.coladd < oap->inclusive)
989 {
990 /* save last line for undo */
991 if (u_save((linenr_T)(oap->end.lnum - 1),
992 (linenr_T)(oap->end.lnum + 1)) == FAIL)
993 return FAIL;
994 curwin->w_cursor = oap->end;
995 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
996 oap->end = curwin->w_cursor;
997 curwin->w_cursor = oap->start;
998 }
999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 if (oap->line_count == 1) /* delete characters within one line */
1002 {
1003 if (u_save_cursor() == FAIL) /* save line for undo */
1004 return FAIL;
1005
1006 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001007 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 && oap->op_type == OP_CHANGE
1009 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001010 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 display_dollar(oap->end.col - !oap->inclusive);
1012
1013 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (virtual_op)
1016 {
1017 /* fix up things for virtualedit-delete:
1018 * break the tabs which are going to get in our way
1019 */
1020 char_u *curline = ml_get_curline();
1021 int len = (int)STRLEN(curline);
1022
1023 if (oap->end.coladd != 0
1024 && (int)oap->end.col >= len - 1
1025 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1026 n++;
1027 /* Delete at least one char (e.g, when on a control char). */
1028 if (n == 0 && oap->start.coladd != oap->end.coladd)
1029 n = 1;
1030
1031 /* When deleted a char in the line, reset coladd. */
1032 if (gchar_cursor() != NUL)
1033 curwin->w_cursor.coladd = 0;
1034 }
Bram Moolenaard009e862015-06-09 20:20:03 +02001035 (void)del_bytes((long)n, !virtual_op,
1036 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 else /* delete characters between lines */
1039 {
1040 pos_T curpos;
1041
1042 /* save deleted and changed lines for undo */
1043 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1044 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1045 return FAIL;
1046
1047 truncate_line(TRUE); /* delete from cursor to end of line */
1048
1049 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1050 ++curwin->w_cursor.lnum;
1051 del_lines((long)(oap->line_count - 2), FALSE);
1052
Bram Moolenaard009e862015-06-09 20:20:03 +02001053 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001054 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001055 curwin->w_cursor.col = 0;
1056 (void)del_bytes((long)n, !virtual_op,
1057 oap->op_type == OP_DELETE && !oap->is_VIsual);
1058 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1059 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
1061 }
1062
1063 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1064
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001065setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (oap->block_mode)
1067 {
1068 curbuf->b_op_end.lnum = oap->end.lnum;
1069 curbuf->b_op_end.col = oap->start.col;
1070 }
1071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 curbuf->b_op_end = oap->start;
1073 curbuf->b_op_start = oap->start;
1074
1075 return OK;
1076}
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078/*
1079 * Adjust end of operating area for ending on a multi-byte character.
1080 * Used for deletion.
1081 */
1082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001083mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
1085 char_u *p;
1086
1087 if (oap->inclusive)
1088 {
1089 p = ml_get(oap->end.lnum);
1090 oap->end.col += mb_tail_off(p, p + oap->end.col);
1091 }
1092}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar630afe82018-06-28 19:26:28 +02001094/*
1095 * Replace the character under the cursor with "c".
1096 * This takes care of multi-byte characters.
1097 */
1098 static void
1099replace_character(int c)
1100{
1101 int n = State;
1102
1103 State = REPLACE;
1104 ins_char(c);
1105 State = n;
1106 /* Backup to the replaced character. */
1107 dec_cursor();
1108}
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110/*
1111 * Replace a whole area with one character.
1112 */
1113 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001114op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 char_u *newp, *oldp;
1119 size_t oldlen;
1120 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001121 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001122 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1125 return OK; /* nothing to do */
1126
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001127 if (c == REPLACE_CR_NCHAR)
1128 {
1129 had_ctrl_v_cr = TRUE;
1130 c = CAR;
1131 }
1132 else if (c == REPLACE_NL_NCHAR)
1133 {
1134 had_ctrl_v_cr = TRUE;
1135 c = NL;
1136 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (has_mbyte)
1139 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141 if (u_save((linenr_T)(oap->start.lnum - 1),
1142 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1143 return FAIL;
1144
1145 /*
1146 * block mode replace
1147 */
1148 if (oap->block_mode)
1149 {
1150 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1151 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1152 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001153 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1155 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1156 continue; /* nothing to replace */
1157
1158 /* n == number of extra chars required
1159 * If we split a TAB, it may be replaced by several characters.
1160 * Thus the number of characters may increase!
1161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 /* If the range starts in virtual space, count the initial
1163 * coladd offset as part of "startspaces" */
1164 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1165 {
1166 pos_T vpos;
1167
Bram Moolenaara1381de2009-11-03 15:44:21 +00001168 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 getvpos(&vpos, oap->start_vcol);
1170 bd.startspaces += vpos.coladd;
1171 n = bd.startspaces;
1172 }
1173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 /* allow for pre spaces */
1175 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1176
1177 /* allow for post spp */
1178 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
1181 /* Figure out how many characters to replace. */
1182 numc = oap->end_vcol - oap->start_vcol + 1;
1183 if (bd.is_short && (!virtual_op || bd.is_MAX))
1184 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 /* A double-wide character can be replaced only up to half the
1187 * times. */
1188 if ((*mb_char2cells)(c) > 1)
1189 {
1190 if ((numc & 1) && !bd.is_short)
1191 {
1192 ++bd.endspaces;
1193 ++n;
1194 }
1195 numc = numc / 2;
1196 }
1197
1198 /* Compute bytes needed, move character count to num_chars. */
1199 num_chars = numc;
1200 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 /* oldlen includes textlen, so don't double count */
1202 n += numc - bd.textlen;
1203
1204 oldp = ml_get_curline();
1205 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001206 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (newp == NULL)
1208 continue;
1209 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
1210 /* copy up to deleted part */
1211 mch_memmove(newp, oldp, (size_t)bd.textcol);
1212 oldp += bd.textcol + bd.textlen;
1213 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001214 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001216 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1217 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01001218 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001220 if (has_mbyte)
1221 {
1222 n = (int)STRLEN(newp);
1223 while (--num_chars >= 0)
1224 n += (*mb_char2bytes)(c, newp + n);
1225 }
1226 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001227 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001228 if (!bd.is_short)
1229 {
1230 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001231 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01001232 /* copy the part after the changed part */
1233 STRMOVE(newp + STRLEN(newp), oldp);
1234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001238 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001239 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001240 if (after_p != NULL)
1241 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243 /* replace the line */
1244 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001245 if (after_p != NULL)
1246 {
1247 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1248 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1249 oap->end.lnum++;
1250 vim_free(after_p);
1251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253 }
1254 else
1255 {
1256 /*
1257 * MCHAR and MLINE motion replace.
1258 */
1259 if (oap->motion_type == MLINE)
1260 {
1261 oap->start.col = 0;
1262 curwin->w_cursor.col = 0;
1263 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1264 if (oap->end.col)
1265 --oap->end.col;
1266 }
1267 else if (!oap->inclusive)
1268 dec(&(oap->end));
1269
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001270 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
1272 n = gchar_cursor();
1273 if (n != NUL)
1274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1276 {
1277 /* This is slow, but it handles replacing a single-byte
1278 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01001279 if (curwin->w_cursor.lnum == oap->end.lnum)
1280 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001281 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (n == TAB)
1286 {
1287 int end_vcol = 0;
1288
1289 if (curwin->w_cursor.lnum == oap->end.lnum)
1290 {
1291 /* oap->end has to be recalculated when
1292 * the tab breaks */
1293 end_vcol = getviscol2(oap->end.col,
1294 oap->end.coladd);
1295 }
1296 coladvance_force(getviscol());
1297 if (curwin->w_cursor.lnum == oap->end.lnum)
1298 getvpos(&oap->end, end_vcol);
1299 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001300 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1304 {
1305 int virtcols = oap->end.coladd;
1306
1307 if (curwin->w_cursor.lnum == oap->start.lnum
1308 && oap->start.col == oap->end.col && oap->start.coladd)
1309 virtcols -= oap->start.coladd;
1310
1311 /* oap->end has been trimmed so it's effectively inclusive;
1312 * as a result an extra +1 must be counted so we don't
1313 * trample the NUL byte. */
1314 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1315 curwin->w_cursor.col -= (virtcols + 1);
1316 for (; virtcols >= 0; virtcols--)
1317 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001318 if ((*mb_char2len)(c) > 1)
1319 replace_character(c);
1320 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001321 PBYTE(curwin->w_cursor, c);
1322 if (inc(&curwin->w_cursor) == -1)
1323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327 /* Advance to next character, stop at the end of the file. */
1328 if (inc_cursor() == -1)
1329 break;
1330 }
1331 }
1332
1333 curwin->w_cursor = oap->start;
1334 check_cursor();
1335 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1336
1337 /* Set "'[" and "']" marks. */
1338 curbuf->b_op_start = oap->start;
1339 curbuf->b_op_end = oap->end;
1340
1341 return OK;
1342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001344static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346/*
1347 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1348 */
1349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001350op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001354 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
1356 if (u_save((linenr_T)(oap->start.lnum - 1),
1357 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1358 return;
1359
1360 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (oap->block_mode) /* Visual block mode */
1362 {
1363 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1364 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001365 int one_change;
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 block_prep(oap, &bd, pos.lnum, FALSE);
1368 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001369 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1370 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001371
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001372#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001373 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1376
Bram Moolenaar009b2592004-10-24 19:18:58 +00001377 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1378 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001380 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 if (did_change)
1385 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1386 }
1387 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
1389 if (oap->motion_type == MLINE)
1390 {
1391 oap->start.col = 0;
1392 pos.col = 0;
1393 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1394 if (oap->end.col)
1395 --oap->end.col;
1396 }
1397 else if (!oap->inclusive)
1398 dec(&(oap->end));
1399
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001400 if (pos.lnum == oap->end.lnum)
1401 did_change = swapchars(oap->op_type, &pos,
1402 oap->end.col - pos.col + 1);
1403 else
1404 for (;;)
1405 {
1406 did_change |= swapchars(oap->op_type, &pos,
1407 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1408 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001409 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001410 break;
1411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (did_change)
1413 {
1414 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1415 0L);
1416#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001417 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
1419 char_u *ptr;
1420 int count;
1421
1422 pos = oap->start;
1423 while (pos.lnum < oap->end.lnum)
1424 {
1425 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001426 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001427 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001429 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 pos.col = 0;
1431 pos.lnum++;
1432 }
1433 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1434 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001435 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001437 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439#endif
1440 }
1441 }
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (!did_change && oap->is_VIsual)
1444 /* No change: need to remove the Visual selection */
1445 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447 /*
1448 * Set '[ and '] marks.
1449 */
1450 curbuf->b_op_start = oap->start;
1451 curbuf->b_op_end = oap->end;
1452
1453 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001454 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001455 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456}
1457
1458/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001459 * Invoke swapchar() on "length" bytes at position "pos".
1460 * "pos" is advanced to just after the changed characters.
1461 * "length" is rounded up to include the whole last multi-byte character.
1462 * Also works correctly when the number of bytes changes.
1463 * Returns TRUE if some character was changed.
1464 */
1465 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001466swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001467{
1468 int todo;
1469 int did_change = 0;
1470
1471 for (todo = length; todo > 0; --todo)
1472 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001473 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001474 {
1475 int len = (*mb_ptr2len)(ml_get_pos(pos));
1476
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001477 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001478 if (len > 0)
1479 todo -= len - 1;
1480 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001481 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001482 if (inc(pos) == -1) /* at end of file */
1483 break;
1484 }
1485 return did_change;
1486}
1487
1488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 * If op_type == OP_UPPER: make uppercase,
1490 * if op_type == OP_LOWER: make lowercase,
1491 * if op_type == OP_ROT13: do rot13 encoding,
1492 * else swap case of character at 'pos'
1493 * returns TRUE when something actually changed.
1494 */
1495 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001496swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 int c;
1499 int nc;
1500
1501 c = gchar_pos(pos);
1502
1503 /* Only do rot13 encoding for ASCII characters. */
1504 if (c >= 0x80 && op_type == OP_ROT13)
1505 return FALSE;
1506
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001507 if (op_type == OP_UPPER && c == 0xdf
1508 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001509 {
1510 pos_T sp = curwin->w_cursor;
1511
1512 /* Special handling of German sharp s: change to "SS". */
1513 curwin->w_cursor = *pos;
1514 del_char(FALSE);
1515 ins_char('S');
1516 ins_char('S');
1517 curwin->w_cursor = sp;
1518 inc(pos);
1519 }
1520
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
1522 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 nc = c;
1524 if (MB_ISLOWER(c))
1525 {
1526 if (op_type == OP_ROT13)
1527 nc = ROT13(c, 'a');
1528 else if (op_type != OP_LOWER)
1529 nc = MB_TOUPPER(c);
1530 }
1531 else if (MB_ISUPPER(c))
1532 {
1533 if (op_type == OP_ROT13)
1534 nc = ROT13(c, 'A');
1535 else if (op_type != OP_UPPER)
1536 nc = MB_TOLOWER(c);
1537 }
1538 if (nc != c)
1539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1541 {
1542 pos_T sp = curwin->w_cursor;
1543
1544 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001545 /* don't use del_char(), it also removes composing chars */
1546 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 ins_char(nc);
1548 curwin->w_cursor = sp;
1549 }
1550 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001551 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return TRUE;
1553 }
1554 return FALSE;
1555}
1556
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557/*
1558 * op_insert - Insert and append operators for Visual mode.
1559 */
1560 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001561op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 long ins_len, pre_textlen = 0;
1564 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001565 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 struct block_def bd;
1567 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001568 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
1570 /* edit() changes this - record it for OP_APPEND */
1571 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1572
1573 /* vis block is still marked. Get rid of it now. */
1574 curwin->w_cursor.lnum = oap->start.lnum;
1575 update_screen(INVERTED);
1576
1577 if (oap->block_mode)
1578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /* When 'virtualedit' is used, need to insert the extra spaces before
1580 * doing block_prep(). When only "block" is used, virtual edit is
1581 * already disabled, but still need it when calling
1582 * coladvance_force(). */
1583 if (curwin->w_cursor.coladd > 0)
1584 {
1585 int old_ve_flags = ve_flags;
1586
1587 ve_flags = VE_ALL;
1588 if (u_save_cursor() == FAIL)
1589 return;
1590 coladvance_force(oap->op_type == OP_APPEND
1591 ? oap->end_vcol + 1 : getviscol());
1592 if (oap->op_type == OP_APPEND)
1593 --curwin->w_cursor.col;
1594 ve_flags = old_ve_flags;
1595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /* Get the info about the block before entering the text */
1597 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001598 /* Get indent information */
1599 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (oap->op_type == OP_APPEND)
1603 firstline += bd.textlen;
1604 pre_textlen = (long)STRLEN(firstline);
1605 }
1606
1607 if (oap->op_type == OP_APPEND)
1608 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001609 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 /* Move the cursor to the character right of the block. */
1612 curwin->w_set_curswant = TRUE;
1613 while (*ml_get_cursor() != NUL
1614 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1615 ++curwin->w_cursor.col;
1616 if (bd.is_short && !bd.is_MAX)
1617 {
1618 /* First line was too short, make it longer and adjust the
1619 * values in "bd". */
1620 if (u_save_cursor() == FAIL)
1621 return;
1622 for (i = 0; i < bd.endspaces; ++i)
1623 ins_char(' ');
1624 bd.textlen += bd.endspaces;
1625 }
1626 }
1627 else
1628 {
1629 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001630 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001633 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 && oap->start_vcol != oap->end_vcol)
1635 inc_cursor();
1636 }
1637 }
1638
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001639 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001640 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001642 /* When a tab was inserted, and the characters in front of the tab
1643 * have been converted to a tab as well, the column of the cursor
1644 * might have actually been reduced, so need to adjust here. */
1645 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001646 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001647 oap->start = curbuf->b_op_start_orig;
1648
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001649 /* If user has moved off this line, we don't know what to do, so do
1650 * nothing.
1651 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
1652 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 return;
1654
1655 if (oap->block_mode)
1656 {
1657 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001658 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001659 size_t len;
1660 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001662 /* If indent kicked in, the firstline might have changed
1663 * but only do that, if the indent actually increased. */
1664 ind_post = (colnr_T)getwhitecols_curline();
1665 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1666 {
1667 bd.textcol += ind_post - ind_pre;
1668 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001669 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001670 }
1671
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001672 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001673 * to adjust the block for that. But only do it, if the difference
1674 * does not come from indent kicking in. */
1675 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1676 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001677 {
1678 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001679 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001680 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001681 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001682 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001683 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001684 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001685 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001686 pre_textlen -= t - oap->start_vcol;
1687 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001688 }
1689 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001690 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001691 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001692 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001693 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001694 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001695 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001696 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001697 /* reset pre_textlen to the value of OP_INSERT */
1698 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001699 pre_textlen -= t - oap->start_vcol;
1700 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001701 oap->op_type = OP_INSERT;
1702 }
1703 }
1704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 /*
1706 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001707 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 * Don't do this when "$" used, end-of-line will have changed.
1709 */
1710 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1711 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1712 {
1713 if (oap->op_type == OP_APPEND)
1714 {
1715 pre_textlen += bd2.textlen - bd.textlen;
1716 if (bd2.endspaces)
1717 --bd2.textlen;
1718 }
1719 bd.textcol = bd2.textcol;
1720 bd.textlen = bd2.textlen;
1721 }
1722
1723 /*
1724 * Subsequent calls to ml_get() flush the firstline data - take a
1725 * copy of the required string.
1726 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001727 firstline = ml_get(oap->start.lnum);
1728 len = STRLEN(firstline);
1729 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001731 add += bd.textlen;
1732 if ((size_t)add > len)
1733 firstline += len; // short line, point to the NUL
1734 else
1735 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001736 if (pre_textlen >= 0
1737 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
1739 ins_text = vim_strnsave(firstline, (int)ins_len);
1740 if (ins_text != NULL)
1741 {
1742 /* block handled here */
1743 if (u_save(oap->start.lnum,
1744 (linenr_T)(oap->end.lnum + 1)) == OK)
1745 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1746 &bd);
1747
1748 curwin->w_cursor.col = oap->start.col;
1749 check_cursor();
1750 vim_free(ins_text);
1751 }
1752 }
1753 }
1754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756/*
1757 * op_change - handle a change operation
1758 *
1759 * return TRUE if edit() returns because of a CTRL-O command
1760 */
1761 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001762op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 colnr_T l;
1765 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 long offset;
1767 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001768 long ins_len;
1769 long pre_textlen = 0;
1770 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 char_u *firstline;
1772 char_u *ins_text, *newp, *oldp;
1773 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775 l = oap->start.col;
1776 if (oap->motion_type == MLINE)
1777 {
1778 l = 0;
1779#ifdef FEAT_SMARTINDENT
1780 if (!p_paste && curbuf->b_p_si
1781# ifdef FEAT_CINDENT
1782 && !curbuf->b_p_cin
1783# endif
1784 )
1785 can_si = TRUE; /* It's like opening a new line, do si */
1786#endif
1787 }
1788
1789 /* First delete the text in the region. In an empty buffer only need to
1790 * save for undo */
1791 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1792 {
1793 if (u_save_cursor() == FAIL)
1794 return FALSE;
1795 }
1796 else if (op_delete(oap) == FAIL)
1797 return FALSE;
1798
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001799 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 && !virtual_op)
1801 inc_cursor();
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /* check for still on same line (<CR> in inserted text meaningless) */
1804 /* skip blank lines too */
1805 if (oap->block_mode)
1806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 /* Add spaces before getting the current line length. */
1808 if (virtual_op && (curwin->w_cursor.coladd > 0
1809 || gchar_cursor() == NUL))
1810 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001811 firstline = ml_get(oap->start.lnum);
1812 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001813 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 bd.textcol = curwin->w_cursor.col;
1815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1818 if (oap->motion_type == MLINE)
1819 fix_indent();
1820#endif
1821
1822 retval = edit(NUL, FALSE, (linenr_T)1);
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001825 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001827 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001829 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00001831 /* Auto-indenting may have changed the indent. If the cursor was past
1832 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001834 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001836 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001837
1838 pre_textlen += new_indent - pre_indent;
1839 bd.textcol += new_indent - pre_indent;
1840 }
1841
1842 ins_len = (long)STRLEN(firstline) - pre_textlen;
1843 if (ins_len > 0)
1844 {
1845 /* Subsequent calls to ml_get() flush the firstline data - take a
1846 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001847 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001849 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1851 linenr++)
1852 {
1853 block_prep(oap, &bd, linenr, TRUE);
1854 if (!bd.is_short || virtual_op)
1855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 pos_T vpos;
1857
1858 /* If the block starts in virtual space, count the
1859 * initial coladd offset as part of "startspaces" */
1860 if (bd.is_short)
1861 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001862 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 }
1865 else
1866 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001868 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (newp == NULL)
1870 continue;
1871 /* copy up to block start */
1872 mch_memmove(newp, oldp, (size_t)bd.textcol);
1873 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001874 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1877 offset += ins_len;
1878 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001879 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 ml_replace(linenr, newp, FALSE);
1881 }
1882 }
1883 check_cursor();
1884
1885 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1886 }
1887 vim_free(ins_text);
1888 }
1889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 return retval;
1892}
1893
1894/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001895 * When the cursor is on the NUL past the end of the line and it should not be
1896 * there move it left.
1897 */
1898 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001899adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001900{
1901 if (curwin->w_cursor.col > 0
1902 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001903 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 && !(restart_edit || (State & INSERT)))
1905 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00001906 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00001907 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001910 {
1911 colnr_T scol, ecol;
1912
1913 /* Coladd is set to the width of the last character. */
1914 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1915 curwin->w_cursor.coladd = ecol - scol + 1;
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
1918}
1919
1920#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
1921/*
1922 * Return TRUE if lines starting with '#' should be left aligned.
1923 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001925preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
1927 return
1928# ifdef FEAT_SMARTINDENT
1929# ifdef FEAT_CINDENT
1930 (curbuf->b_p_si && !curbuf->b_p_cin) ||
1931# else
1932 curbuf->b_p_si
1933# endif
1934# endif
1935# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01001936 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
1937 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938# endif
1939 ;
1940}
1941#endif
1942
Bram Moolenaar81340392012-06-06 16:12:59 +02001943/*
1944 * If "process" is TRUE and the line begins with a comment leader (possibly
1945 * after some white space), return a pointer to the text after it. Put a boolean
1946 * value indicating whether the line ends with an unclosed comment in
1947 * "is_comment".
1948 * line - line to be processed,
1949 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001950 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001951 * include_space - whether to also skip space following the comment leader,
1952 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001953 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001954 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001955 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001956skip_comment(
1957 char_u *line,
1958 int process,
1959 int include_space,
1960 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001961{
1962 char_u *comment_flags = NULL;
1963 int lead_len;
1964 int leader_offset = get_last_leader_offset(line, &comment_flags);
1965
1966 *is_comment = FALSE;
1967 if (leader_offset != -1)
1968 {
1969 /* Let's check whether the line ends with an unclosed comment.
1970 * If the last comment leader has COM_END in flags, there's no comment.
1971 */
1972 while (*comment_flags)
1973 {
1974 if (*comment_flags == COM_END
1975 || *comment_flags == ':')
1976 break;
1977 ++comment_flags;
1978 }
1979 if (*comment_flags != COM_END)
1980 *is_comment = TRUE;
1981 }
1982
1983 if (process == FALSE)
1984 return line;
1985
1986 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1987
1988 if (lead_len == 0)
1989 return line;
1990
1991 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02001992 * - COM_END,
1993 * - colon,
1994 * whichever comes first.
1995 */
1996 while (*comment_flags)
1997 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001998 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001999 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02002000 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02002001 ++comment_flags;
2002 }
2003
2004 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02002005 * starting with a closing part of a three-part comment. That's good,
2006 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02002007 */
2008 if (*comment_flags == ':' || *comment_flags == NUL)
2009 line += lead_len;
2010
2011 return line;
2012}
Bram Moolenaar81340392012-06-06 16:12:59 +02002013
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002015 * Join 'count' lines (minimal 2) at cursor position.
2016 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002017 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
2018 * leaders should not be removed.
2019 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
2020 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00002022 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
2024 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002025do_join(
2026 long count,
2027 int insert_space,
2028 int save_undo,
2029 int use_formatoptions UNUSED,
2030 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002032 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002033 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002034 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002036 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02002037 int endcurr1 = NUL;
2038 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002039 int currsize = 0; /* size of the current line */
2040 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002042 colnr_T col = 0;
2043 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02002044 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002045 int remove_comments = (use_formatoptions == TRUE)
2046 && has_format_option(FO_REMOVE_COMS);
2047 int prev_was_comment;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002048#ifdef FEAT_TEXT_PROP
2049 textprop_T **prop_lines = NULL;
2050 int *prop_lengths = NULL;
2051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002053 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2054 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
2055 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002057 /* Allocate an array to store the number of spaces inserted before each
2058 * line. We will use it to pre-compute the length of the new line and the
2059 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002060 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002061 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02002063 if (remove_comments)
2064 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002065 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02002066 if (comments == NULL)
2067 {
2068 vim_free(spaces);
2069 return FAIL;
2070 }
2071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002074 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002075 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002076 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002078 for (t = 0; t < count; ++t)
2079 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002080 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002081 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002082 {
2083 /* Set the '[ mark. */
2084 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2085 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2086 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002087 if (remove_comments)
2088 {
2089 /* We don't want to remove the comment leader if the
2090 * previous line is not a comment. */
2091 if (t > 0 && prev_was_comment)
2092 {
2093
2094 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2095 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002096 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002097 curr = new_curr;
2098 }
2099 else
2100 curr = skip_comment(curr, FALSE, insert_space,
2101 &prev_was_comment);
2102 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002103
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002104 if (insert_space && t > 0)
2105 {
2106 curr = skipwhite(curr);
2107 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002108 && (!has_format_option(FO_MBYTE_JOIN)
2109 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2110 && (!has_format_option(FO_MBYTE_JOIN2)
2111 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002112 )
2113 {
2114 /* don't add a space if the line is ending in a space */
2115 if (endcurr1 == ' ')
2116 endcurr1 = endcurr2;
2117 else
2118 ++spaces[t];
2119 /* extra space when 'joinspaces' set and line ends in '.' */
2120 if ( p_js
2121 && (endcurr1 == '.'
2122 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2123 && (endcurr1 == '?' || endcurr1 == '!'))))
2124 ++spaces[t];
2125 }
2126 }
2127 currsize = (int)STRLEN(curr);
2128 sumsize += currsize + spaces[t];
2129 endcurr1 = endcurr2 = NUL;
2130 if (insert_space && currsize > 0)
2131 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002132 if (has_mbyte)
2133 {
2134 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002135 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002136 endcurr1 = (*mb_ptr2char)(cend);
2137 if (cend > curr)
2138 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002139 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002140 endcurr2 = (*mb_ptr2char)(cend);
2141 }
2142 }
2143 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002144 {
2145 endcurr1 = *(curr + currsize - 1);
2146 if (currsize > 1)
2147 endcurr2 = *(curr + currsize - 2);
2148 }
2149 }
2150 line_breakcheck();
2151 if (got_int)
2152 {
2153 ret = FAIL;
2154 goto theend;
2155 }
2156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002158 /* store the column position before last line */
2159 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002161 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002162 newp = alloc(sumsize + 1);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002163 if (newp == NULL)
2164 {
2165 ret = FAIL;
2166 goto theend;
2167 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002168 cend = newp + sumsize;
2169 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002171#ifdef FEAT_TEXT_PROP
2172 // We need to move properties of the lines that are going to be deleted to
2173 // the new long one.
2174 if (curbuf->b_has_textprop && !text_prop_frozen)
2175 {
2176 // Allocate an array to copy the text properties of joined lines into.
2177 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002178 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
2179 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002180 if (prop_lengths == NULL)
2181 VIM_CLEAR(prop_lines);
2182 }
2183#endif
2184
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002185 /*
2186 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002187 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002188 *
2189 * Move marks from each deleted line to the joined line, adjusting the
2190 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2191 * should not really be a problem.
2192 */
2193 for (t = count - 1; ; --t)
2194 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002195 int spaces_removed;
2196
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002197 cend -= currsize;
2198 mch_memmove(cend, curr, (size_t)currsize);
2199 if (spaces[t] > 0)
2200 {
2201 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002202 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002203 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002204
2205 // If deleting more spaces than adding, the cursor moves no more than
2206 // what is added if it is inside these spaces.
2207 spaces_removed = (curr - curr_start) - spaces[t];
2208
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002209 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002210 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002211 if (t == 0)
2212 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002213#ifdef FEAT_TEXT_PROP
2214 if (prop_lines != NULL)
2215 adjust_props_for_join(curwin->w_cursor.lnum + t,
2216 prop_lines + t - 1, prop_lengths + t - 1,
2217 (long)(cend - newp - spaces_removed), spaces_removed);
2218#endif
2219
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002220 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002221 if (remove_comments)
2222 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002223 if (insert_space && t > 1)
2224 curr = skipwhite(curr);
2225 currsize = (int)STRLEN(curr);
2226 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002227
2228#ifdef FEAT_TEXT_PROP
2229 if (prop_lines != NULL)
2230 join_prop_lines(curwin->w_cursor.lnum, newp,
2231 prop_lines, prop_lengths, count);
2232 else
2233#endif
2234 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002236 if (setmark)
2237 {
2238 /* Set the '] mark. */
2239 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002240 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002241 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /* Only report the change in the first line here, del_lines() will report
2244 * the deleted line. */
2245 changed_lines(curwin->w_cursor.lnum, currsize,
2246 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002248 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 * briefly, and then move it back. After del_lines() the cursor may
2250 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
2252 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002254 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 curwin->w_cursor.lnum = t;
2256
2257 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002258 * Set the cursor column:
2259 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002260 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002262 curwin->w_cursor.col =
2263 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 curwin->w_set_curswant = TRUE;
2268
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002269theend:
2270 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002271 if (remove_comments)
2272 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002273 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274}
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276/*
2277 * Return TRUE if the two comment leaders given are the same. "lnum" is
2278 * the first line. White-space is ignored. Note that the whole of
2279 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
2280 */
2281 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002282same_leader(
2283 linenr_T lnum,
2284 int leader1_len,
2285 char_u *leader1_flags,
2286 int leader2_len,
2287 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 int idx1 = 0, idx2 = 0;
2290 char_u *p;
2291 char_u *line1;
2292 char_u *line2;
2293
2294 if (leader1_len == 0)
2295 return (leader2_len == 0);
2296
2297 /*
2298 * If first leader has 'f' flag, the lines can be joined only if the
2299 * second line does not have a leader.
2300 * If first leader has 'e' flag, the lines can never be joined.
2301 * If fist leader has 's' flag, the lines can only be joined if there is
2302 * some text after it and the second line has the 'm' flag.
2303 */
2304 if (leader1_flags != NULL)
2305 {
2306 for (p = leader1_flags; *p && *p != ':'; ++p)
2307 {
2308 if (*p == COM_FIRST)
2309 return (leader2_len == 0);
2310 if (*p == COM_END)
2311 return FALSE;
2312 if (*p == COM_START)
2313 {
2314 if (*(ml_get(lnum) + leader1_len) == NUL)
2315 return FALSE;
2316 if (leader2_flags == NULL || leader2_len == 0)
2317 return FALSE;
2318 for (p = leader2_flags; *p && *p != ':'; ++p)
2319 if (*p == COM_MIDDLE)
2320 return TRUE;
2321 return FALSE;
2322 }
2323 }
2324 }
2325
2326 /*
2327 * Get current line and next line, compare the leaders.
2328 * The first line has to be saved, only one line can be locked at a time.
2329 */
2330 line1 = vim_strsave(ml_get(lnum));
2331 if (line1 != NULL)
2332 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002333 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 ;
2335 line2 = ml_get(lnum + 1);
2336 for (idx2 = 0; idx2 < leader2_len; ++idx2)
2337 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002338 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 if (line1[idx1++] != line2[idx2])
2341 break;
2342 }
2343 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01002344 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 ++idx1;
2346 }
2347 vim_free(line1);
2348 }
2349 return (idx2 == leader2_len && idx1 == leader1_len);
2350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01002353 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 */
2355 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002356op_format(
2357 oparg_T *oap,
2358 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 long old_line_count = curbuf->b_ml.ml_line_count;
2361
2362 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
2363 * can put it back there. */
2364 curwin->w_cursor = oap->cursor_start;
2365
2366 if (u_save((linenr_T)(oap->start.lnum - 1),
2367 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2368 return;
2369 curwin->w_cursor = oap->start;
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (oap->is_VIsual)
2372 /* When there is no change: need to remove the Visual selection */
2373 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 /* Set '[ mark at the start of the formatted area */
2376 curbuf->b_op_start = oap->start;
2377
2378 /* For "gw" remember the cursor position and put it back below (adjusted
2379 * for joined and split lines). */
2380 if (keep_cursor)
2381 saved_cursor = oap->cursor_start;
2382
Bram Moolenaar81a82092008-03-12 16:27:00 +00002383 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 /*
2386 * Leave the cursor at the first non-blank of the last formatted line.
2387 * If the cursor was moved one line back (e.g. with "Q}") go to the next
2388 * line, so "." will do the next lines.
2389 */
2390 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2391 ++curwin->w_cursor.lnum;
2392 beginline(BL_WHITE | BL_FIX);
2393 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
2394 msgmore(old_line_count);
2395
2396 /* put '] mark on the end of the formatted area */
2397 curbuf->b_op_end = curwin->w_cursor;
2398
2399 if (keep_cursor)
2400 {
2401 curwin->w_cursor = saved_cursor;
2402 saved_cursor.lnum = 0;
2403 }
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 if (oap->is_VIsual)
2406 {
2407 win_T *wp;
2408
2409 FOR_ALL_WINDOWS(wp)
2410 {
2411 if (wp->w_old_cursor_lnum != 0)
2412 {
2413 /* When lines have been inserted or deleted, adjust the end of
2414 * the Visual area to be redrawn. */
2415 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
2416 wp->w_old_cursor_lnum += old_line_count;
2417 else
2418 wp->w_old_visual_lnum += old_line_count;
2419 }
2420 }
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422}
2423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002424#if defined(FEAT_EVAL) || defined(PROTO)
2425/*
2426 * Implementation of the format operator 'gq' for when using 'formatexpr'.
2427 */
2428 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002429op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002430{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002431 if (oap->is_VIsual)
2432 /* When there is no change: need to remove the Visual selection */
2433 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002434
Bram Moolenaar700303e2010-07-11 17:35:50 +02002435 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
2436 /* As documented: when 'formatexpr' returns non-zero fall back to
2437 * internal formatting. */
2438 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002439}
2440
2441 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002442fex_format(
2443 linenr_T lnum,
2444 long count,
2445 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002446{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002447 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
2448 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002449 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002450 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002451
2452 /*
2453 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002454 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002455 */
2456 set_vim_var_nr(VV_LNUM, lnum);
2457 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00002458 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002459
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002460 /* Make a copy, the option could be changed while calling it. */
2461 fex = vim_strsave(curbuf->b_p_fex);
2462 if (fex == NULL)
2463 return 0;
2464
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002465 /*
2466 * Evaluate the function.
2467 */
2468 if (use_sandbox)
2469 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002470 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002471 if (use_sandbox)
2472 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002473
2474 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002475 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002476
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002477 return r;
2478}
2479#endif
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481/*
2482 * Format "line_count" lines, starting at the cursor position.
2483 * When "line_count" is negative, format until the end of the paragraph.
2484 * Lines after the cursor line are saved for undo, caller must have saved the
2485 * first line.
2486 */
2487 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002488format_lines(
2489 linenr_T line_count,
2490 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 int max_len;
2493 int is_not_par; /* current line not part of parag. */
2494 int next_is_not_par; /* next line not part of paragraph */
2495 int is_end_par; /* at end of paragraph */
2496 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
2497 int next_is_start_par = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 int leader_len = 0; /* leader len of current line */
2499 int next_leader_len; /* leader len of next line */
2500 char_u *leader_flags = NULL; /* flags for leader of current line */
2501 char_u *next_leader_flags; /* flags for leader of next line */
2502 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002503 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002505 int second_indent = -1; /* indent for second line (comment
2506 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 int do_second_indent;
2508 int do_number_indent;
2509 int do_trail_white;
2510 int first_par_line = TRUE;
2511 int smd_save;
2512 long count;
2513 int need_set_indent = TRUE; /* set indent of next paragraph */
2514 int force_format = FALSE;
2515 int old_State = State;
2516
2517 /* length of a line to force formatting: 3 * 'tw' */
2518 max_len = comp_textwidth(TRUE) * 3;
2519
2520 /* check for 'q', '2' and '1' in 'formatoptions' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 do_second_indent = has_format_option(FO_Q_SECOND);
2523 do_number_indent = has_format_option(FO_Q_NUMBER);
2524 do_trail_white = has_format_option(FO_WHITE_PAR);
2525
2526 /*
2527 * Get info about the previous and current line.
2528 */
2529 if (curwin->w_cursor.lnum > 1)
2530 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002531 , &leader_len, &leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 else
2533 is_not_par = TRUE;
2534 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002535 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 is_end_par = (is_not_par || next_is_not_par);
2537 if (!is_end_par && do_trail_white)
2538 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
2539
2540 curwin->w_cursor.lnum--;
2541 for (count = line_count; count != 0 && !got_int; --count)
2542 {
2543 /*
2544 * Advance to next paragraph.
2545 */
2546 if (advance)
2547 {
2548 curwin->w_cursor.lnum++;
2549 prev_is_end_par = is_end_par;
2550 is_not_par = next_is_not_par;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 leader_len = next_leader_len;
2552 leader_flags = next_leader_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554
2555 /*
2556 * The last line to be formatted.
2557 */
2558 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2559 {
2560 next_is_not_par = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 next_leader_len = 0;
2562 next_leader_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 else
2565 {
2566 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002567 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (do_number_indent)
2569 next_is_start_par =
2570 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
2571 }
2572 advance = TRUE;
2573 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
2574 if (!is_end_par && do_trail_white)
2575 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
2576
2577 /*
2578 * Skip lines that are not in a paragraph.
2579 */
2580 if (is_not_par)
2581 {
2582 if (line_count < 0)
2583 break;
2584 }
2585 else
2586 {
2587 /*
2588 * For the first line of a paragraph, check indent of second line.
2589 * Don't do this for comments and empty lines.
2590 */
2591 if (first_par_line
2592 && (do_second_indent || do_number_indent)
2593 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002594 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002596 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002597 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002598 if (leader_len == 0 && next_leader_len == 0)
2599 {
2600 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002601 second_indent =
2602 get_indent_lnum(curwin->w_cursor.lnum + 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002603 }
2604 else
2605 {
2606 second_indent = next_leader_len;
2607 do_comments_list = 1;
2608 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002611 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002612 if (leader_len == 0 && next_leader_len == 0)
2613 {
2614 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002615 second_indent =
2616 get_number_indent(curwin->w_cursor.lnum);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002617 }
2618 else
2619 {
2620 /* get_number_indent() is now "comment aware"... */
2621 second_indent =
2622 get_number_indent(curwin->w_cursor.lnum);
2623 do_comments_list = 1;
2624 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627
2628 /*
2629 * When the comment leader changes, it's the end of the paragraph.
2630 */
2631 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 || !same_leader(curwin->w_cursor.lnum,
2633 leader_len, leader_flags,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002634 next_leader_len, next_leader_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 is_end_par = TRUE;
2636
2637 /*
2638 * If we have got to the end of a paragraph, or the line is
2639 * getting long, format it.
2640 */
2641 if (is_end_par || force_format)
2642 {
2643 if (need_set_indent)
2644 /* replace indent in first line with minimal number of
2645 * tabs and spaces, according to current options */
2646 (void)set_indent(get_indent(), SIN_CHANGED);
2647
2648 /* put cursor on last non-space */
2649 State = NORMAL; /* don't go past end-of-line */
2650 coladvance((colnr_T)MAXCOL);
2651 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
2652 dec_cursor();
2653
2654 /* do the formatting, without 'showmode' */
2655 State = INSERT; /* for open_line() */
2656 smd_save = p_smd;
2657 p_smd = FALSE;
2658 insertchar(NUL, INSCHAR_FORMAT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002660 + (do_comments && do_comments_list
2661 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar81a82092008-03-12 16:27:00 +00002662 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 State = old_State;
2664 p_smd = smd_save;
2665 second_indent = -1;
2666 /* at end of par.: need to set indent of next par. */
2667 need_set_indent = is_end_par;
2668 if (is_end_par)
2669 {
2670 /* When called with a negative line count, break at the
2671 * end of the paragraph. */
2672 if (line_count < 0)
2673 break;
2674 first_par_line = TRUE;
2675 }
2676 force_format = FALSE;
2677 }
2678
2679 /*
2680 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002681 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
2683 if (!is_end_par)
2684 {
2685 advance = FALSE;
2686 curwin->w_cursor.lnum++;
2687 curwin->w_cursor.col = 0;
2688 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002691 {
2692 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002694 (long)-next_leader_len, 0);
2695 }
2696 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002697 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002698 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002699
2700 if (indent > 0)
2701 {
2702 (void)del_bytes(indent, FALSE, FALSE);
2703 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002704 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01002705 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002708 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
2710 beep_flush();
2711 break;
2712 }
2713 first_par_line = FALSE;
2714 /* If the line is getting long, format it next time */
2715 if (STRLEN(ml_get_curline()) > (size_t)max_len)
2716 force_format = TRUE;
2717 else
2718 force_format = FALSE;
2719 }
2720 }
2721 line_breakcheck();
2722 }
2723}
2724
2725/*
2726 * Return TRUE if line "lnum" ends in a white character.
2727 */
2728 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002729ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
2731 char_u *s = ml_get(lnum);
2732 size_t l;
2733
2734 if (*s == NUL)
2735 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002736 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * invocation may call function multiple times". */
2738 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002739 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740}
2741
2742/*
2743 * Blank lines, and lines containing only the comment leader, are left
2744 * untouched by the formatting. The function returns TRUE in this
2745 * case. It also returns TRUE when a line starts with the end of a comment
2746 * ('e' in comment flags), so that this line is skipped, and not joined to the
2747 * previous line. A new paragraph starts after a blank line, or when the
2748 * comment leader changes -- webb.
2749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002751fmt_check_par(
2752 linenr_T lnum,
2753 int *leader_len,
2754 char_u **leader_flags,
2755 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 char_u *flags = NULL; /* init for GCC */
2758 char_u *ptr;
2759
2760 ptr = ml_get(lnum);
2761 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002762 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
2764 *leader_len = 0;
2765
2766 if (*leader_len > 0)
2767 {
2768 /*
2769 * Search for 'e' flag in comment leader flags.
2770 */
2771 flags = *leader_flags;
2772 while (*flags && *flags != ':' && *flags != COM_END)
2773 ++flags;
2774 }
2775
2776 return (*skipwhite(ptr + *leader_len) == NUL
2777 || (*leader_len > 0 && *flags == COM_END)
2778 || startPS(lnum, NUL, FALSE));
2779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781/*
2782 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
2783 * previous line is in the same paragraph. Used for auto-formatting.
2784 */
2785 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002786paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 int leader_len = 0; /* leader len of current line */
2790 char_u *leader_flags = NULL; /* flags for leader of current line */
2791 int next_leader_len; /* leader len of next line */
2792 char_u *next_leader_flags; /* flags for leader of next line */
2793 int do_comments; /* format comments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 if (lnum <= 1)
2796 return TRUE; /* start of the file */
2797
2798 p = ml_get(lnum - 1);
2799 if (*p == NUL)
2800 return TRUE; /* after empty line */
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002803 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 return TRUE; /* after non-paragraph line */
2805
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002806 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 return TRUE; /* "lnum" is not a paragraph line */
2808
2809 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
2810 return TRUE; /* missing trailing space in previous line. */
2811
2812 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
2813 return TRUE; /* numbered item starts in "lnum". */
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (!same_leader(lnum - 1, leader_len, leader_flags,
2816 next_leader_len, next_leader_flags))
2817 return TRUE; /* change of comment leader. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
2819 return FALSE;
2820}
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822/*
2823 * prepare a few things for block mode yank/delete/tilde
2824 *
2825 * for delete:
2826 * - textlen includes the first/last char to be (partly) deleted
2827 * - start/endspaces is the number of columns that are taken by the
2828 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002829 * deleted.
2830 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 * - textlen includes the first/last char to be wholly yanked
2832 * - start/endspaces is the number of columns of the first/last yanked char
2833 * that are to be yanked.
2834 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002836block_prep(
2837 oparg_T *oap,
2838 struct block_def *bdp,
2839 linenr_T lnum,
2840 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
2842 int incr = 0;
2843 char_u *pend;
2844 char_u *pstart;
2845 char_u *line;
2846 char_u *prev_pstart;
2847 char_u *prev_pend;
2848
2849 bdp->startspaces = 0;
2850 bdp->endspaces = 0;
2851 bdp->textlen = 0;
2852 bdp->start_vcol = 0;
2853 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 bdp->is_short = FALSE;
2855 bdp->is_oneChar = FALSE;
2856 bdp->pre_whitesp = 0;
2857 bdp->pre_whitesp_c = 0;
2858 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 bdp->start_char_vcols = 0;
2860
2861 line = ml_get(lnum);
2862 pstart = line;
2863 prev_pstart = line;
2864 while (bdp->start_vcol < oap->start_vcol && *pstart)
2865 {
2866 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002867 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002869 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
2871 bdp->pre_whitesp += incr;
2872 bdp->pre_whitesp_c++;
2873 }
2874 else
2875 {
2876 bdp->pre_whitesp = 0;
2877 bdp->pre_whitesp_c = 0;
2878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002880 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882 bdp->start_char_vcols = incr;
2883 if (bdp->start_vcol < oap->start_vcol) /* line too short */
2884 {
2885 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if (!is_del || oap->op_type == OP_APPEND)
2888 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2889 }
2890 else
2891 {
2892 /* notice: this converts partly selected Multibyte characters to
2893 * spaces, too. */
2894 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2895 if (is_del && bdp->startspaces)
2896 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2897 pend = pstart;
2898 bdp->end_vcol = bdp->start_vcol;
2899 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
2900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 if (oap->op_type == OP_INSERT)
2903 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2904 else if (oap->op_type == OP_APPEND)
2905 {
2906 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2907 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2908 }
2909 else
2910 {
2911 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2912 if (is_del && oap->op_type != OP_LSHIFT)
2913 {
2914 /* just putting the sum of those two into
2915 * bdp->startspaces doesn't work for Visual replace,
2916 * so we have to split the tab in two */
2917 bdp->startspaces = bdp->start_char_vcols
2918 - (bdp->start_vcol - oap->start_vcol);
2919 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2920 }
2921 }
2922 }
2923 else
2924 {
2925 prev_pend = pend;
2926 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2927 {
2928 /* Count a tab for what it's worth (if list mode not on) */
2929 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002930 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 bdp->end_vcol += incr;
2932 }
2933 if (bdp->end_vcol <= oap->end_vcol
2934 && (!is_del
2935 || oap->op_type == OP_APPEND
2936 || oap->op_type == OP_REPLACE)) /* line too short */
2937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 /* Alternative: include spaces to fill up the block.
2940 * Disadvantage: can lead to trailing spaces when the line is
2941 * short where the text is put */
2942 /* if (!is_del || oap->op_type == OP_APPEND) */
2943 if (oap->op_type == OP_APPEND || virtual_op)
2944 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002945 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 else
2947 bdp->endspaces = 0; /* replace doesn't add characters */
2948 }
2949 else if (bdp->end_vcol > oap->end_vcol)
2950 {
2951 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2952 if (!is_del && bdp->endspaces)
2953 {
2954 bdp->endspaces = incr - bdp->endspaces;
2955 if (pend != pstart)
2956 pend = prev_pend;
2957 }
2958 }
2959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (is_del && bdp->startspaces)
2962 pstart = prev_pstart;
2963 bdp->textlen = (int)(pend - pstart);
2964 }
2965 bdp->textcol = (colnr_T) (pstart - line);
2966 bdp->textstart = pstart;
2967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002970 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002973op_addsub(
2974 oparg_T *oap,
2975 linenr_T Prenum1, /* Amount of add/subtract */
2976 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002978 pos_T pos;
2979 struct block_def bd;
2980 int change_cnt = 0;
2981 linenr_T amount = Prenum1;
2982
Bram Moolenaar6b731882018-11-16 20:54:47 +01002983 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002984 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002985 // the call to changed_lines().
2986#ifdef FEAT_FOLDING
2987 disable_fold_update++;
2988#endif
2989
Bram Moolenaard79e5502016-01-10 22:13:02 +01002990 if (!VIsual_active)
2991 {
2992 pos = curwin->w_cursor;
2993 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002994 {
2995#ifdef FEAT_FOLDING
2996 disable_fold_update--;
2997#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002998 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002999 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003000 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01003001#ifdef FEAT_FOLDING
3002 disable_fold_update--;
3003#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003004 if (change_cnt)
3005 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
3006 }
3007 else
3008 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003009 int one_change;
3010 int length;
3011 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003012
3013 if (u_save((linenr_T)(oap->start.lnum - 1),
3014 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01003015 {
3016#ifdef FEAT_FOLDING
3017 disable_fold_update--;
3018#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003019 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01003020 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003021
3022 pos = oap->start;
3023 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
3024 {
3025 if (oap->block_mode) /* Visual block mode */
3026 {
3027 block_prep(oap, &bd, pos.lnum, FALSE);
3028 pos.col = bd.textcol;
3029 length = bd.textlen;
3030 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003031 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003032 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003033 curwin->w_cursor.col = 0;
3034 pos.col = 0;
3035 length = (colnr_T)STRLEN(ml_get(pos.lnum));
3036 }
3037 else /* oap->motion_type == MCHAR */
3038 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01003039 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003040 dec(&(oap->end));
3041 length = (colnr_T)STRLEN(ml_get(pos.lnum));
3042 pos.col = 0;
3043 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003044 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003045 pos.col += oap->start.col;
3046 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003047 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003048 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003049 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003050 length = (int)STRLEN(ml_get(oap->end.lnum));
3051 if (oap->end.col >= length)
3052 oap->end.col = length - 1;
3053 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003054 }
3055 }
3056 one_change = do_addsub(oap->op_type, &pos, length, amount);
3057 if (one_change)
3058 {
3059 /* Remember the start position of the first change. */
3060 if (change_cnt == 0)
3061 startpos = curbuf->b_op_start;
3062 ++change_cnt;
3063 }
3064
3065#ifdef FEAT_NETBEANS_INTG
3066 if (netbeans_active() && one_change)
3067 {
3068 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
3069
3070 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
3071 netbeans_inserted(curbuf, pos.lnum, pos.col,
3072 &ptr[pos.col], length);
3073 }
3074#endif
3075 if (g_cmd && one_change)
3076 amount += Prenum1;
3077 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01003078
3079#ifdef FEAT_FOLDING
3080 disable_fold_update--;
3081#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003082 if (change_cnt)
3083 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
3084
3085 if (!change_cnt && oap->is_VIsual)
3086 /* No change: need to remove the Visual selection */
3087 redraw_curbuf_later(INVERTED);
3088
3089 /* Set '[ mark if something changed. Keep the last end
3090 * position from do_addsub(). */
3091 if (change_cnt > 0)
3092 curbuf->b_op_start = startpos;
3093
3094 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003095 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003096 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003097 }
3098}
3099
3100/*
3101 * Add or subtract 'Prenum1' from a number in a line
3102 * op_type is OP_NR_ADD or OP_NR_SUB
3103 *
3104 * Returns TRUE if some character was changed.
3105 */
3106 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003107do_addsub(
3108 int op_type,
3109 pos_T *pos,
3110 int length,
3111 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003112{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 int col;
3114 char_u *buf1;
3115 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003116 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003118 uvarnumber_T n;
3119 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 char_u *ptr;
3121 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int todel;
3123 int dohex;
3124 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003125 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 int doalp;
3127 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003129 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003130 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003131 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02003132 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003133 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003134 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003135 pos_T startpos;
3136 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
Bram Moolenaardac13472019-09-16 21:06:21 +02003138 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
3139 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
3140 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
3141 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaard79e5502016-01-10 22:13:02 +01003143 curwin->w_cursor = *pos;
3144 ptr = ml_get(pos->lnum);
3145 col = pos->col;
3146
3147 if (*ptr == NUL)
3148 goto theend;
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 /*
3151 * First check if we are on a hexadecimal number, after the "0x".
3152 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003153 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003155 if (dobin)
3156 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003157 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003158 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003159 if (has_mbyte)
3160 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003161 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003162
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003163 if (dohex)
3164 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003165 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003166 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003167 if (has_mbyte)
3168 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003169 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003170
3171 if ( dobin
3172 && dohex
3173 && ! ((col > 0
3174 && (ptr[col] == 'X'
3175 || ptr[col] == 'x')
3176 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003177 && (!has_mbyte ||
3178 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003179 && vim_isxdigit(ptr[col + 1]))))
3180 {
3181
3182 /* In case of binary/hexadecimal pattern overlap match, rescan */
3183
Bram Moolenaard79e5502016-01-10 22:13:02 +01003184 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003185
3186 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003187 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003188 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003189 if (has_mbyte)
3190 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003191 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003192 }
3193
3194 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003195 && col > 0
3196 && (ptr[col] == 'X'
3197 || ptr[col] == 'x')
3198 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003199 && (!has_mbyte ||
3200 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003201 && vim_isxdigit(ptr[col + 1])) ||
3202 ( dobin
3203 && col > 0
3204 && (ptr[col] == 'B'
3205 || ptr[col] == 'b')
3206 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003207 && (!has_mbyte ||
3208 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003209 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003210 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003211 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003212 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003213 if (has_mbyte)
3214 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003215 }
3216 else
3217 {
3218 /*
3219 * Search forward and then backward to find the start of number.
3220 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003221 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003222
3223 while (ptr[col] != NUL
3224 && !vim_isdigit(ptr[col])
3225 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003226 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003227
3228 while (col > 0
3229 && vim_isdigit(ptr[col - 1])
3230 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003231 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003232 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003233 if (has_mbyte)
3234 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003235 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003236 }
3237 }
3238
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003239 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003240 {
3241 while (ptr[col] != NUL && length > 0
3242 && !vim_isdigit(ptr[col])
3243 && !(doalp && ASCII_ISALPHA(ptr[col])))
3244 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003245 int mb_len = MB_PTR2LEN(ptr + col);
3246
3247 col += mb_len;
3248 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003249 }
3250
3251 if (length == 0)
3252 goto theend;
3253
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003254 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003255 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003256 {
3257 negative = TRUE;
3258 was_positive = FALSE;
3259 }
3260 }
3261
3262 /*
3263 * If a number was found, and saving for undo works, replace the number.
3264 */
3265 firstdigit = ptr[col];
3266 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
3267 {
3268 beep_flush();
3269 goto theend;
3270 }
3271
3272 if (doalp && ASCII_ISALPHA(firstdigit))
3273 {
3274 /* decrement or increment alphabetic character */
3275 if (op_type == OP_NR_SUB)
3276 {
3277 if (CharOrd(firstdigit) < Prenum1)
3278 {
3279 if (isupper(firstdigit))
3280 firstdigit = 'A';
3281 else
3282 firstdigit = 'a';
3283 }
3284 else
3285#ifdef EBCDIC
3286 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
3287#else
3288 firstdigit -= Prenum1;
3289#endif
3290 }
3291 else
3292 {
3293 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
3294 {
3295 if (isupper(firstdigit))
3296 firstdigit = 'Z';
3297 else
3298 firstdigit = 'z';
3299 }
3300 else
3301#ifdef EBCDIC
3302 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
3303#else
3304 firstdigit += Prenum1;
3305#endif
3306 }
3307 curwin->w_cursor.col = col;
3308 if (!did_change)
3309 startpos = curwin->w_cursor;
3310 did_change = TRUE;
3311 (void)del_char(FALSE);
3312 ins_char(firstdigit);
3313 endpos = curwin->w_cursor;
3314 curwin->w_cursor.col = col;
3315 }
3316 else
3317 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003318 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003319 && (!has_mbyte ||
3320 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003321 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003322 {
3323 /* negative number */
3324 --col;
3325 negative = TRUE;
3326 }
3327 /* get the number value (unsigned) */
3328 if (visual && VIsual_mode != 'V')
3329 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
3330 ? (int)STRLEN(ptr) - col
3331 : length);
3332
3333 vim_str2nr(ptr + col, &pre, &length,
3334 0 + (dobin ? STR2NR_BIN : 0)
3335 + (dooct ? STR2NR_OCT : 0)
3336 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003337 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003338
3339 /* ignore leading '-' for hex and octal and bin numbers */
3340 if (pre && negative)
3341 {
3342 ++col;
3343 --length;
3344 negative = FALSE;
3345 }
3346 /* add or subtract */
3347 subtract = FALSE;
3348 if (op_type == OP_NR_SUB)
3349 subtract ^= TRUE;
3350 if (negative)
3351 subtract ^= TRUE;
3352
3353 oldn = n;
3354 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003355 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003356 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003357 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003358 /* handle wraparound for decimal numbers */
3359 if (!pre)
3360 {
3361 if (subtract)
3362 {
3363 if (n > oldn)
3364 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003365 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003366 negative ^= TRUE;
3367 }
3368 }
3369 else
3370 {
3371 /* add */
3372 if (n < oldn)
3373 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003374 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003375 negative ^= TRUE;
3376 }
3377 }
3378 if (n == 0)
3379 negative = FALSE;
3380 }
3381
3382 if (visual && !was_positive && !negative && col > 0)
3383 {
3384 /* need to remove the '-' */
3385 col--;
3386 length++;
3387 }
3388
3389 /*
3390 * Delete the old number.
3391 */
3392 curwin->w_cursor.col = col;
3393 if (!did_change)
3394 startpos = curwin->w_cursor;
3395 did_change = TRUE;
3396 todel = length;
3397 c = gchar_cursor();
3398 /*
3399 * Don't include the '-' in the length, only the length of the
3400 * part after it is kept the same.
3401 */
3402 if (c == '-')
3403 --length;
3404 while (todel-- > 0)
3405 {
3406 if (c < 0x100 && isalpha(c))
3407 {
3408 if (isupper(c))
3409 hexupper = TRUE;
3410 else
3411 hexupper = FALSE;
3412 }
3413 /* del_char() will mark line needing displaying */
3414 (void)del_char(FALSE);
3415 c = gchar_cursor();
3416 }
3417
3418 /*
3419 * Prepare the leading characters in buf1[].
3420 * When there are many leading zeros it could be very long.
3421 * Allocate a bit too much.
3422 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003423 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003424 if (buf1 == NULL)
3425 goto theend;
3426 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003427 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003428 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003429 if (pre)
3430 {
3431 *ptr++ = '0';
3432 --length;
3433 }
3434 if (pre == 'b' || pre == 'B' ||
3435 pre == 'x' || pre == 'X')
3436 {
3437 *ptr++ = pre;
3438 --length;
3439 }
3440
3441 /*
3442 * Put the number characters in buf2[].
3443 */
3444 if (pre == 'b' || pre == 'B')
3445 {
3446 int i;
3447 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003448 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003449
3450 /* leading zeros */
3451 for (bit = bits; bit > 0; bit--)
3452 if ((n >> (bit - 1)) & 0x1) break;
3453
3454 for (i = 0; bit > 0; bit--)
3455 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3456
3457 buf2[i] = '\0';
3458 }
3459 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02003460 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003461 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003462 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02003463 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003464 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003465 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02003466 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003467 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003468 else
Bram Moolenaarea391762018-04-08 13:07:22 +02003469 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003470 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003471 length -= (int)STRLEN(buf2);
3472
3473 /*
3474 * Adjust number of zeros to the new number of digits, so the
3475 * total length of the number remains the same.
3476 * Don't do this when
3477 * the result may look like an octal number.
3478 */
3479 if (firstdigit == '0' && !(dooct && pre == 0))
3480 while (length-- > 0)
3481 *ptr++ = '0';
3482 *ptr = NUL;
3483 STRCAT(buf1, buf2);
3484 ins_str(buf1); /* insert the new number */
3485 vim_free(buf1);
3486 endpos = curwin->w_cursor;
3487 if (did_change && curwin->w_cursor.col)
3488 --curwin->w_cursor.col;
3489 }
3490
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003491 if (did_change)
3492 {
3493 /* set the '[ and '] marks */
3494 curbuf->b_op_start = startpos;
3495 curbuf->b_op_end = endpos;
3496 if (curbuf->b_op_end.col > 0)
3497 --curbuf->b_op_end.col;
3498 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003499
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003500theend:
3501 if (visual)
3502 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003503 else if (did_change)
3504 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003505
Bram Moolenaard79e5502016-01-10 22:13:02 +01003506 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507}
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#if defined(FEAT_CLIPBOARD) || defined(PROTO)
3510/*
3511 * SELECTION / PRIMARY ('*')
3512 *
3513 * Text selection stuff that uses the GUI selection register '*'. When using a
3514 * GUI this may be text from another window, otherwise it is the last text we
3515 * had highlighted with VIsual mode. With mouse support, clicking the middle
3516 * button performs the paste, otherwise you will need to do <"*p>. "
3517 * If not under X, it is synonymous with the clipboard register '+'.
3518 *
3519 * X CLIPBOARD ('+')
3520 *
3521 * Text selection stuff that uses the GUI clipboard register '+'.
3522 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
3523 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
3524 * otherwise you will need to do <"+p>. "
3525 * If not under X, it is synonymous with the selection register '*'.
3526 */
3527
3528/*
3529 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003530 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 * only exist while the owning application exists, so we write to the
3532 * permanent (while X runs) store CUT_BUFFER0.
3533 * Dump the CLIPBOARD selection if we own it (it's logically the more
3534 * 'permanent' of the two), otherwise the PRIMARY one.
3535 * For now, use a hard-coded sanity limit of 1Mb of data.
3536 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003537#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003539x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
3541 Display *dpy;
3542 char_u *str = NULL;
3543 long_u len = 0;
3544 int motion_type = -1;
3545
3546# ifdef FEAT_GUI
3547 if (gui.in_use)
3548 dpy = X_DISPLAY;
3549 else
3550# endif
3551# ifdef FEAT_XCLIPBOARD
3552 dpy = xterm_dpy;
3553# else
3554 return;
3555# endif
3556
3557 /* Get selection to export */
3558 if (clip_plus.owned)
3559 motion_type = clip_convert_selection(&str, &len, &clip_plus);
3560 else if (clip_star.owned)
3561 motion_type = clip_convert_selection(&str, &len, &clip_star);
3562
3563 /* Check it's OK */
3564 if (dpy != NULL && str != NULL && motion_type >= 0
3565 && len < 1024*1024 && len > 0)
3566 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003567 int ok = TRUE;
3568
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003569 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
3570 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
3571 * encoding conversion usually doesn't work, so keep the text as-is.
3572 */
3573 if (has_mbyte)
3574 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003575 vimconv_T vc;
3576
3577 vc.vc_type = CONV_NONE;
3578 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
3579 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003580 int intlen = len;
3581 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003582
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003583 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003584 conv_str = string_convert(&vc, str, &intlen);
3585 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003586 if (conv_str != NULL)
3587 {
3588 vim_free(str);
3589 str = conv_str;
3590 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003591 else
3592 {
3593 ok = FALSE;
3594 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003595 convert_setup(&vc, NULL, NULL);
3596 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003597 else
3598 {
3599 ok = FALSE;
3600 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003601 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003602
3603 /* Do not store the string if conversion failed. Better to use any
3604 * other selection than garbled text. */
3605 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003606 {
3607 XStoreBuffer(dpy, (char *)str, (int)len, 0);
3608 XFlush(dpy);
3609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611
3612 vim_free(str);
3613}
3614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615#endif /* FEAT_CLIPBOARD || PROTO */
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003618clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 vim_memset(oap, 0, sizeof(oparg_T));
3621}
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003624 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 *
3626 * "Words" are counted by looking for boundaries between non-space and
3627 * space characters. (it seems to produce results that match 'wc'.)
3628 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003629 * Return value is byte count; word count for the line is added to "*wc".
3630 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 *
3632 * The function will only examine the first "limit" characters in the
3633 * line, stopping if it encounters an end-of-line (NUL byte). In that
3634 * case, eol_size will be added to the character count to account for
3635 * the size of the EOL character.
3636 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003637 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003638line_count_info(
3639 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003640 varnumber_T *wc,
3641 varnumber_T *cc,
3642 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003643 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003645 varnumber_T i;
3646 varnumber_T words = 0;
3647 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 int is_word = 0;
3649
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003650 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 if (is_word)
3653 {
3654 if (vim_isspace(line[i]))
3655 {
3656 words++;
3657 is_word = 0;
3658 }
3659 }
3660 else if (!vim_isspace(line[i]))
3661 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003662 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003663 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665
3666 if (is_word)
3667 words++;
3668 *wc += words;
3669
3670 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003671 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003674 chars += eol_size;
3675 }
3676 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return i;
3678}
3679
3680/*
3681 * Give some info about the position of the cursor (for "g CTRL-G").
3682 * In Visual mode, give some info about the selected region. (In this case,
3683 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003684 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
3686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003687cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003690 char_u buf1[50];
3691 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003693 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003694 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003695 varnumber_T byte_count_cursor = 0;
3696 varnumber_T char_count = 0;
3697 varnumber_T char_count_cursor = 0;
3698 varnumber_T word_count = 0;
3699 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003700 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003701 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 long line_count_selected = 0;
3703 pos_T min_pos, max_pos;
3704 oparg_T oparg;
3705 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 /*
3708 * Compute the length of the file in characters.
3709 */
3710 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3711 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003712 if (dict == NULL)
3713 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003714 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003715 return;
3716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 else
3719 {
3720 if (get_fileformat(curbuf) == EOL_DOS)
3721 eol_size = 2;
3722 else
3723 eol_size = 1;
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 if (VIsual_active)
3726 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003727 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
3729 min_pos = VIsual;
3730 max_pos = curwin->w_cursor;
3731 }
3732 else
3733 {
3734 min_pos = curwin->w_cursor;
3735 max_pos = VIsual;
3736 }
3737 if (*p_sel == 'e' && max_pos.col > 0)
3738 --max_pos.col;
3739
3740 if (VIsual_mode == Ctrl_V)
3741 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003742#ifdef FEAT_LINEBREAK
3743 char_u * saved_sbr = p_sbr;
3744
3745 /* Make 'sbr' empty for a moment to get the correct size. */
3746 p_sbr = empty_option;
3747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 oparg.is_VIsual = 1;
3749 oparg.block_mode = TRUE;
3750 oparg.op_type = OP_NOP;
3751 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003752 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003753#ifdef FEAT_LINEBREAK
3754 p_sbr = saved_sbr;
3755#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003756 if (curwin->w_curswant == MAXCOL)
3757 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 /* Swap the start, end vcol if needed */
3759 if (oparg.end_vcol < oparg.start_vcol)
3760 {
3761 oparg.end_vcol += oparg.start_vcol;
3762 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3763 oparg.end_vcol -= oparg.start_vcol;
3764 }
3765 }
3766 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3770 {
3771 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003772 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
3774 ui_breakcheck();
3775 if (got_int)
3776 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003777 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 /* Do extra processing for VIsual mode. */
3781 if (VIsual_active
3782 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3783 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003784 char_u *s = NULL;
3785 long len = 0L;
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 switch (VIsual_mode)
3788 {
3789 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003793 s = bd.textstart;
3794 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 break;
3796 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003797 s = ml_get(lnum);
3798 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 break;
3800 case 'v':
3801 {
3802 colnr_T start_col = (lnum == min_pos.lnum)
3803 ? min_pos.col : 0;
3804 colnr_T end_col = (lnum == max_pos.lnum)
3805 ? max_pos.col - start_col + 1 : MAXCOL;
3806
Bram Moolenaardef9e822004-12-31 20:58:58 +00003807 s = ml_get(lnum) + start_col;
3808 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 break;
3811 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003812 if (s != NULL)
3813 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003814 byte_count_cursor += line_count_info(s, &word_count_cursor,
3815 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003816 if (lnum == curbuf->b_ml.ml_line_count
3817 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003818 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003819 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003820 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
3825 /* In non-visual mode, check for the line the cursor is on */
3826 if (lnum == curwin->w_cursor.lnum)
3827 {
3828 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003829 char_count_cursor += char_count;
3830 byte_count_cursor = byte_count +
3831 line_count_info(ml_get(lnum),
3832 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003833 (varnumber_T)(curwin->w_cursor.col + 1),
3834 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 }
3837 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003838 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003839 &char_count, (varnumber_T)MAXCOL,
3840 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842
3843 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003844 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003845 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
Bram Moolenaared767a22016-01-03 22:49:16 +01003847 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003849 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003851 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3852 {
3853 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3854 &max_pos.col);
3855 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3856 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3857 }
3858 else
3859 buf1[0] = NUL;
3860
3861 if (char_count_cursor == byte_count_cursor
3862 && char_count == byte_count)
3863 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003864 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003865 buf1, line_count_selected,
3866 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003867 (long_long_T)word_count_cursor,
3868 (long_long_T)word_count,
3869 (long_long_T)byte_count_cursor,
3870 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003871 else
3872 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003873 _("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 +01003874 buf1, line_count_selected,
3875 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003876 (long_long_T)word_count_cursor,
3877 (long_long_T)word_count,
3878 (long_long_T)char_count_cursor,
3879 (long_long_T)char_count,
3880 (long_long_T)byte_count_cursor,
3881 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003884 {
3885 p = ml_get_curline();
3886 validate_virtcol();
3887 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3888 (int)curwin->w_virtcol + 1);
3889 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3890 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaared767a22016-01-03 22:49:16 +01003892 if (char_count_cursor == byte_count_cursor
3893 && char_count == byte_count)
3894 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003895 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003896 (char *)buf1, (char *)buf2,
3897 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003899 (long_long_T)word_count_cursor, (long_long_T)word_count,
3900 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003901 else
3902 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003903 _("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 +01003904 (char *)buf1, (char *)buf2,
3905 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003906 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003907 (long_long_T)word_count_cursor, (long_long_T)word_count,
3908 (long_long_T)char_count_cursor, (long_long_T)char_count,
3909 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003910 }
3911 }
3912
Bram Moolenaared767a22016-01-03 22:49:16 +01003913 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003914 if (dict == NULL && bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01003915 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003916 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003917 if (dict == NULL)
3918 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003919 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003920 p = p_shm;
3921 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003922 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003923 p_shm = p;
3924 }
3925 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003926#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003927 if (dict != NULL)
3928 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003929 dict_add_number(dict, "words", word_count);
3930 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003931 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003932 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3933 byte_count_cursor);
3934 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3935 char_count_cursor);
3936 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3937 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}