blob: 7ede1f73a2adc0ce7ab4b7a621e7179cae274267 [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
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020027static yankreg_T y_regs[NUM_REGISTERS];
28
29static yankreg_T *y_current; /* ptr to current yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int y_append; /* TRUE when appending */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020031static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33/*
34 * structure used by block_prep, op_delete and op_yank for blockwise operators
35 * also op_change, op_shift, op_insert, op_replace - AKelly
36 */
37struct block_def
38{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000039 int startspaces; /* 'extra' cols before first char */
40 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000042 char_u *textstart; /* pointer to 1st char (partially) in block */
43 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 colnr_T start_vcol; /* start col of 1st char wholly inside block */
45 colnr_T end_vcol; /* start col of 1st char wholly after block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 int is_short; /* TRUE if line is too short to fit in block */
47 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
48 int is_oneChar; /* TRUE if block within one character */
49 int pre_whitesp; /* screen cols of ws before block */
50 int pre_whitesp_c; /* chars of ws before block */
51 colnr_T end_char_vcols; /* number of vcols of post-block char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 colnr_T start_char_vcols; /* number of vcols of pre-block char */
53};
54
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int stuff_yank(int, char_u *);
57static void put_reedit_in_typebuf(int silent);
58static int put_in_typebuf(char_u *s, int esc, int colon,
59 int silent);
60static void stuffescaped(char_u *arg, int literally);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void free_yank_all(void);
63static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020065static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020068#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
69static int preprocs_left(void);
70#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010071static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010072static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
73static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020075static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010079static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
83
Bram Moolenaarf2732452018-06-03 14:47:35 +020084// Flags for third item in "opchars".
85#define OPF_LINES 1 // operator always works on lines
86#define OPF_CHANGE 2 // operator changes text
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088/*
89 * The names of operators.
90 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020091 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
93static char opchars[][3] =
94{
Bram Moolenaarf2732452018-06-03 14:47:35 +020095 {NUL, NUL, 0}, // OP_NOP
96 {'d', NUL, OPF_CHANGE}, // OP_DELETE
97 {'y', NUL, 0}, // OP_YANK
98 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
99 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
100 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
101 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
102 {'g', '~', OPF_CHANGE}, // OP_TILDE
103 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
104 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
105 {':', NUL, OPF_LINES}, // OP_COLON
106 {'g', 'U', OPF_CHANGE}, // OP_UPPER
107 {'g', 'u', OPF_CHANGE}, // OP_LOWER
108 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
109 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
110 {'g', '?', OPF_CHANGE}, // OP_ROT13
111 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
112 {'I', NUL, OPF_CHANGE}, // OP_INSERT
113 {'A', NUL, OPF_CHANGE}, // OP_APPEND
114 {'z', 'f', OPF_LINES}, // OP_FOLD
115 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
116 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
117 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
118 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
119 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
120 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
121 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
122 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
123 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
124 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125};
126
Bram Moolenaarc3328162019-07-23 22:15:25 +0200127 yankreg_T *
128get_y_regs(void)
129{
130 return y_regs;
131}
132
133 yankreg_T *
134get_y_current(void)
135{
136 return y_current;
137}
138
139 yankreg_T *
140get_y_previous(void)
141{
142 return y_previous;
143}
144
145 void
146set_y_previous(yankreg_T *yreg)
147{
148 y_previous = yreg;
149}
150
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Translate a command name into an operator type.
154 * Must only be called with a valid operator name!
155 */
156 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100157get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158{
159 int i;
160
161 if (char1 == 'r') /* ignore second character */
162 return OP_REPLACE;
163 if (char1 == '~') /* when tilde is an operator */
164 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100165 if (char1 == 'g' && char2 == Ctrl_A) /* add */
166 return OP_NR_ADD;
167 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
168 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (opchars[i][0] == char1 && opchars[i][1] == char2)
172 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100173 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
174 {
175 internal_error("get_op_type()");
176 break;
177 }
178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 return i;
180}
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Return TRUE if operator "op" always works on whole lines.
184 */
185 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100186op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200188 return opchars[op][2] & OPF_LINES;
189}
190
Bram Moolenaar113e1072019-01-20 15:30:40 +0100191#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200192/*
193 * Return TRUE if operator "op" changes text.
194 */
195 int
196op_is_change(int op)
197{
198 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202/*
203 * Get first operator command character.
204 * Returns 'g' or 'z' if there is another command character.
205 */
206 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100207get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 return opchars[optype][0];
210}
211
212/*
213 * Get second operator command character.
214 */
215 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100216get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 return opchars[optype][1];
219}
220
221/*
222 * op_shift - handle a shift operation
223 */
224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100225op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226{
227 long i;
228 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231 if (u_save((linenr_T)(oap->start.lnum - 1),
232 (linenr_T)(oap->end.lnum + 1)) == FAIL)
233 return;
234
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (oap->block_mode)
236 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
238 for (i = oap->line_count; --i >= 0; )
239 {
240 first_char = *ml_get_curline();
241 if (first_char == NUL) /* empty line */
242 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 else if (oap->block_mode)
244 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 else
246 /* Move the line right if it doesn't start with '#', 'smartindent'
247 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
248#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
249 if (first_char != '#' || !preprocs_left())
250#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000251 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 ++curwin->w_cursor.lnum;
253 }
254
255 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 if (oap->block_mode)
257 {
258 curwin->w_cursor.lnum = oap->start.lnum;
259 curwin->w_cursor.col = block_col;
260 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100261 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 curwin->w_cursor.lnum = oap->start.lnum;
264 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
265 }
266 else
267 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
268
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100269#ifdef FEAT_FOLDING
270 /* The cursor line is not in a closed fold */
271 foldOpenCursor();
272#endif
273
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 if (oap->line_count > p_report)
276 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200277 char *op;
278 char *msg_line_single;
279 char *msg_line_plural;
280
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200282 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200284 op = "<";
285 msg_line_single = NGETTEXT("%ld line %sed %d time",
286 "%ld line %sed %d times", amount);
287 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
288 "%ld lines %sed %d times", amount);
289 vim_snprintf((char *)IObuff, IOSIZE,
290 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
291 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100292 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 }
294
295 /*
296 * Set "'[" and "']" marks.
297 */
298 curbuf->b_op_start = oap->start;
299 curbuf->b_op_end.lnum = oap->end.lnum;
300 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
301 if (curbuf->b_op_end.col > 0)
302 --curbuf->b_op_end.col;
303}
304
305/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100306 * Shift the current line one shiftwidth left (if left != 0) or right
307 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 */
309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100310shift_line(
311 int left,
312 int round,
313 int amount,
314 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315{
316 int count;
317 int i, j;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100318 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319
320 count = get_indent(); /* get current indent */
321
322 if (round) /* round off indent */
323 {
324 i = count / p_sw; /* number of p_sw rounded down */
325 j = count % p_sw; /* extra spaces */
326 if (j && left) /* first remove extra spaces */
327 --amount;
328 if (left)
329 {
330 i -= amount;
331 if (i < 0)
332 i = 0;
333 }
334 else
335 i += amount;
336 count = i * p_sw;
337 }
338 else /* original vi indent */
339 {
340 if (left)
341 {
342 count -= p_sw * amount;
343 if (count < 0)
344 count = 0;
345 }
346 else
347 count += p_sw * amount;
348 }
349
350 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000352 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000354 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355}
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357/*
358 * Shift one line of the current block one shiftwidth right or left.
359 * Leaves cursor on first character in block.
360 */
361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100362shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364 int left = (oap->op_type == OP_LSHIFT);
365 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000366 int total;
367 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 int oldcol = curwin->w_cursor.col;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100369 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200370#ifdef FEAT_VARTABS
371 int *p_vts = curbuf->b_p_vts_array;
372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 int p_ts = (int)curbuf->b_p_ts;
374 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000376 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 int i = 0, j = 0;
378 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379#ifdef FEAT_RIGHTLEFT
380 int old_p_ri = p_ri;
381
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200382 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383#endif
384
385 State = INSERT; /* don't want REPLACE for State */
386 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
387 if (bd.is_short)
388 return;
389
390 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200391 total = (int)((unsigned)amount * (unsigned)p_sw);
392 if ((total / p_sw) != amount)
393 return; /* multiplication overflow */
394
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 oldp = ml_get_curline();
396
397 if (!left)
398 {
399 /*
400 * 1. Get start vcol
401 * 2. Total ws vcols
402 * 3. Divvy into TABs & spp
403 * 4. Construct new string
404 */
405 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
406 ws_vcol = bd.start_vcol - bd.pre_whitesp;
407 if (bd.startspaces)
408 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100410 {
411 if ((*mb_ptr2len)(bd.textstart) == 1)
412 ++bd.textstart;
413 else
414 {
415 ws_vcol = 0;
416 bd.startspaces = 0;
417 }
418 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000419 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000420 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100422 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200424 /* TODO: is passing bd.textstart for start of the line OK? */
425 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
426 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 total += incr;
428 bd.start_vcol += incr;
429 }
430 /* OK, now total=all the VWS reqd, and textstart points at the 1st
431 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200432#ifdef FEAT_VARTABS
433 if (!curbuf->b_p_et)
434 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
435 else
436 j = total;
437#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if (!curbuf->b_p_et)
439 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
440 if (i)
441 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
442 else
443 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 /* if we're splitting a TAB, allow for it */
446 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
447 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200448 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (newp == NULL)
450 return;
451 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
452 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200453 vim_memset(newp + bd.textcol, TAB, (size_t)i);
454 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 /* the end */
456 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
457 }
458 else /* left */
459 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000460 colnr_T destination_col; /* column to which text in block will
461 be shifted */
462 char_u *verbatim_copy_end; /* end of the part of the line which is
463 copied verbatim */
464 colnr_T verbatim_copy_width;/* the (displayed) width of this part
465 of line */
466 unsigned fill; /* nr of spaces that replace a TAB */
467 unsigned new_line_len; /* the length of the line after the
468 block shift */
469 size_t block_space_width;
470 size_t shift_amount;
471 char_u *non_white = bd.textstart;
472 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000474 /*
475 * Firstly, let's find the first non-whitespace character that is
476 * displayed after the block's start column and the character's column
477 * number. Also, let's calculate the width of all the whitespace
478 * characters that are displayed in the block and precede the searched
479 * non-whitespace character.
480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000482 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
483 * the part of which is displayed at the block's beginning. Let's start
484 * searching from the next character. */
485 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100486 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000487
488 /* The character's column is in "bd.start_vcol". */
489 non_white_col = bd.start_vcol;
490
Bram Moolenaar1c465442017-03-12 20:10:05 +0100491 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000492 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200493 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 }
496
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497 block_space_width = non_white_col - oap->start_vcol;
498 /* We will shift by "total" or "block_space_width", whichever is less.
499 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000500 shift_amount = (block_space_width < (size_t)total
501 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502
503 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000504 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000505
506 /* Now let's find out how much of the beginning of the line we can
507 * reuse without modification. */
508 verbatim_copy_end = bd.textstart;
509 verbatim_copy_width = bd.start_vcol;
510
511 /* If "bd.startspaces" is set, "bd.textstart" points to the character
512 * preceding the block. We have to subtract its width to obtain its
513 * column number. */
514 if (bd.startspaces)
515 verbatim_copy_width -= bd.start_char_vcols;
516 while (verbatim_copy_width < destination_col)
517 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200518 char_u *line = verbatim_copy_end;
519
520 /* TODO: is passing verbatim_copy_end for start of the line OK? */
521 incr = lbr_chartabsize(line, verbatim_copy_end,
522 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000523 if (verbatim_copy_width + incr > destination_col)
524 break;
525 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100526 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000527 }
528
529 /* If "destination_col" is different from the width of the initial
530 * part of the line that will be copied, it means we encountered a tab
531 * character, which we will have to partly replace with spaces. */
532 fill = destination_col - verbatim_copy_width;
533
534 /* The replacement line will consist of:
535 * - the beginning of the original line up to "verbatim_copy_end",
536 * - "fill" number of spaces,
537 * - the rest of the line, pointed to by non_white. */
538 new_line_len = (unsigned)(verbatim_copy_end - oldp)
539 + fill
540 + (unsigned)STRLEN(non_white) + 1;
541
Bram Moolenaar964b3742019-05-24 18:54:09 +0200542 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 if (newp == NULL)
544 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000545 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200546 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000547 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549 /* replace the line */
550 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
551 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
552 State = oldstate;
553 curwin->w_cursor.col = oldcol;
554#ifdef FEAT_RIGHTLEFT
555 p_ri = old_p_ri;
556#endif
557}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
560 * Insert string "s" (b_insert ? before : after) block :AKelly
561 * Caller must prepare for undo.
562 */
563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100564block_insert(
565 oparg_T *oap,
566 char_u *s,
567 int b_insert,
568 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570 int p_ts;
571 int count = 0; /* extra spaces to replace a cut TAB */
572 int spaces = 0; /* non-zero if cutting a TAB */
573 colnr_T offset; /* pointer along new line */
574 unsigned s_len; /* STRLEN(s) */
575 char_u *newp, *oldp; /* new, old lines */
576 linenr_T lnum; /* loop var */
577 int oldstate = State;
578
579 State = INSERT; /* don't want REPLACE for State */
580 s_len = (unsigned)STRLEN(s);
581
582 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
583 {
584 block_prep(oap, bdp, lnum, TRUE);
585 if (bdp->is_short && b_insert)
586 continue; /* OP_INSERT, line ends before block start */
587
588 oldp = ml_get(lnum);
589
590 if (b_insert)
591 {
592 p_ts = bdp->start_char_vcols;
593 spaces = bdp->startspaces;
594 if (spaces != 0)
595 count = p_ts - 1; /* we're cutting a TAB */
596 offset = bdp->textcol;
597 }
598 else /* append */
599 {
600 p_ts = bdp->end_char_vcols;
601 if (!bdp->is_short) /* spaces = padding after block */
602 {
603 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
604 if (spaces != 0)
605 count = p_ts - 1; /* we're cutting a TAB */
606 offset = bdp->textcol + bdp->textlen - (spaces != 0);
607 }
608 else /* spaces = padding to block edge */
609 {
610 /* if $ used, just append to EOL (ie spaces==0) */
611 if (!bdp->is_MAX)
612 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
613 count = spaces;
614 offset = bdp->textcol + bdp->textlen;
615 }
616 }
617
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200618 if (has_mbyte && spaces > 0)
619 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100620 int off;
621
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200622 /* Avoid starting halfway a multi-byte character. */
623 if (b_insert)
624 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100625 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200626 }
627 else
628 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100629 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200630 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200631 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100632 spaces -= off;
633 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200634 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200635
Bram Moolenaar964b3742019-05-24 18:54:09 +0200636 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (newp == NULL)
638 continue;
639
640 /* copy up to shifted part */
641 mch_memmove(newp, oldp, (size_t)(offset));
642 oldp += offset;
643
644 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200645 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647 /* copy the new text */
648 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
649 offset += s_len;
650
651 if (spaces && !bdp->is_short)
652 {
653 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200654 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 /* We're splitting a TAB, don't copy it. */
656 oldp++;
657 /* We allowed for that TAB, remember this now */
658 count++;
659 }
660
661 if (spaces > 0)
662 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000663 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
665 ml_replace(lnum, newp, FALSE);
666
667 if (lnum == oap->end.lnum)
668 {
669 /* Set "']" mark to the end of the block instead of the end of
670 * the insert in the first line. */
671 curbuf->b_op_end.lnum = oap->end.lnum;
672 curbuf->b_op_end.col = offset;
673 }
674 } /* for all lnum */
675
676 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
677
678 State = oldstate;
679}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
682/*
683 * op_reindent - handle reindenting a block of lines.
684 */
685 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100686op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 long i;
689 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200690 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 linenr_T first_changed = 0;
692 linenr_T last_changed = 0;
693 linenr_T start_lnum = curwin->w_cursor.lnum;
694
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000695 /* Don't even try when 'modifiable' is off. */
696 if (!curbuf->b_p_ma)
697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100698 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000699 return;
700 }
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 for (i = oap->line_count; --i >= 0 && !got_int; )
703 {
704 /* it's a slow thing to do, so give feedback so there's no worry that
705 * the computer's just hung. */
706
707 if (i > 1
708 && (i % 50 == 0 || i == oap->line_count - 1)
709 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100710 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 /*
713 * Be vi-compatible: For lisp indenting the first line is not
714 * indented, unless there is only one line.
715 */
716#ifdef FEAT_LISP
717 if (i != oap->line_count - 1 || oap->line_count == 1
718 || how != get_lisp_indent)
719#endif
720 {
721 l = skipwhite(ml_get_curline());
722 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200723 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200725 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200727 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
729 /* did change the indent, call changed_lines() later */
730 if (first_changed == 0)
731 first_changed = curwin->w_cursor.lnum;
732 last_changed = curwin->w_cursor.lnum;
733 }
734 }
735 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000736 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 }
738
739 /* put cursor on first non-blank of indented line */
740 curwin->w_cursor.lnum = start_lnum;
741 beginline(BL_SOL | BL_FIX);
742
743 /* Mark changed lines so that they will be redrawn. When Visual
744 * highlighting was present, need to continue until the last line. When
745 * there is no change still need to remove the Visual highlighting. */
746 if (last_changed != 0)
747 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 else if (oap->is_VIsual)
751 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
753 if (oap->line_count > p_report)
754 {
755 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100756 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200757 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759 /* set '[ and '] marks */
760 curbuf->b_op_start = oap->start;
761 curbuf->b_op_end = oap->end;
762}
763#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
764
765#if defined(FEAT_EVAL) || defined(PROTO)
766/*
767 * Keep the last expression line here, for repeating.
768 */
769static char_u *expr_line = NULL;
770
771/*
772 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
773 * Returns '=' when OK, NUL otherwise.
774 */
775 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100776get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 char_u *new_line;
779
Bram Moolenaare96a2492019-06-25 04:12:16 +0200780 new_line = getcmdline('=', 0L, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (new_line == NULL)
782 return NUL;
783 if (*new_line == NUL) /* use previous line */
784 vim_free(new_line);
785 else
786 set_expr_line(new_line);
787 return '=';
788}
789
790/*
791 * Set the expression for the '=' register.
792 * Argument must be an allocated string.
793 */
794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100795set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 vim_free(expr_line);
798 expr_line = new_line;
799}
800
801/*
802 * Get the result of the '=' register expression.
803 * Returns a pointer to allocated memory, or NULL for failure.
804 */
805 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100806get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 char_u *expr_copy;
809 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000810 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 if (expr_line == NULL)
813 return NULL;
814
815 /* Make a copy of the expression, because evaluating it may cause it to be
816 * changed. */
817 expr_copy = vim_strsave(expr_line);
818 if (expr_copy == NULL)
819 return NULL;
820
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000821 /* When we are invoked recursively limit the evaluation to 10 levels.
822 * Then return the string as-is. */
823 if (nested >= 10)
824 return expr_copy;
825
826 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000827 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000828 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 vim_free(expr_copy);
830 return rv;
831}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000832
833/*
834 * Get the '=' register expression itself, without evaluating it.
835 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200836 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000838{
839 if (expr_line == NULL)
840 return NULL;
841 return vim_strsave(expr_line);
842}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843#endif /* FEAT_EVAL */
844
845/*
846 * Check if 'regname' is a valid name of a yank register.
847 * Note: There is no check for 0 (default register), caller should do this
848 */
849 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100850valid_yank_reg(
851 int regname,
852 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854 if ( (regname > 0 && ASCII_ISALNUM(regname))
855 || (!writing && vim_strchr((char_u *)
856#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100857 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100859 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif
861 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100862 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 || regname == '"'
864 || regname == '-'
865 || regname == '_'
866#ifdef FEAT_CLIPBOARD
867 || regname == '*'
868 || regname == '+'
869#endif
870#ifdef FEAT_DND
871 || (!writing && regname == '~')
872#endif
873 )
874 return TRUE;
875 return FALSE;
876}
877
878/*
879 * Set y_current and y_append, according to the value of "regname".
880 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000881 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 *
883 * If regname is 0 and writing, use register 0
884 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100885 *
886 * Return TRUE when the register should be inserted literally (selection or
887 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100890get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100893 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
895 y_append = FALSE;
896 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
897 {
898 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100899 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901 i = regname;
902 if (VIM_ISDIGIT(i))
903 i -= '0';
904 else if (ASCII_ISLOWER(i))
905 i = CharOrdLow(i) + 10;
906 else if (ASCII_ISUPPER(i))
907 {
908 i = CharOrdUp(i) + 10;
909 y_append = TRUE;
910 }
911 else if (regname == '-')
912 i = DELETION_REGISTER;
913#ifdef FEAT_CLIPBOARD
914 /* When selection is not available, use register 0 instead of '*' */
915 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100918 ret = TRUE;
919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 /* When clipboard is not available, use register 0 instead of '+' */
921 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100924 ret = TRUE;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926#endif
927#ifdef FEAT_DND
928 else if (!writing && regname == '~')
929 i = TILDE_REGISTER;
930#endif
931 else /* not 0-9, a-z, A-Z or '-': use register 0 */
932 i = 0;
933 y_current = &(y_regs[i]);
934 if (writing) /* remember the register we write into for do_put() */
935 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100936 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
Bram Moolenaar8299df92004-07-10 09:47:34 +0000939#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940/*
941 * When "regname" is a clipboard register, obtain the selection. If it's not
942 * available return zero, otherwise return "regname".
943 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100945may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 if (regname == '*')
948 {
949 if (!clip_star.available)
950 regname = 0;
951 else
952 clip_get_selection(&clip_star);
953 }
954 else if (regname == '+')
955 {
956 if (!clip_plus.available)
957 regname = 0;
958 else
959 clip_get_selection(&clip_plus);
960 }
961 return regname;
962}
963#endif
964
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965/*
966 * Obtain the contents of a "normal" register. The register is made empty.
967 * The returned pointer has allocated memory, use put_register() later.
968 */
969 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100970get_register(
971 int name,
972 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200974 yankreg_T *reg;
975 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977#ifdef FEAT_CLIPBOARD
978 /* When Visual area changed, may have to update selection. Obtain the
979 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000980 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200982 if (clip_isautosel_star())
983 clip_update_selection(&clip_star);
984 may_get_selection(name);
985 }
986 if (name == '+' && clip_plus.available)
987 {
988 if (clip_isautosel_plus())
989 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 may_get_selection(name);
991 }
992#endif
993
994 get_yank_register(name, 0);
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200995 reg = ALLOC_ONE(yankreg_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (reg != NULL)
997 {
998 *reg = *y_current;
999 if (copy)
1000 {
1001 /* If we run out of memory some or all of the lines are empty. */
1002 if (reg->y_size == 0)
1003 reg->y_array = NULL;
1004 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001005 reg->y_array = ALLOC_MULT(char_u *, reg->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (reg->y_array != NULL)
1007 {
1008 for (i = 0; i < reg->y_size; ++i)
1009 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1010 }
1011 }
1012 else
1013 y_current->y_array = NULL;
1014 }
1015 return (void *)reg;
1016}
1017
1018/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001019 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 */
1021 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001022put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
1024 get_yank_register(name, 0);
1025 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001026 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001027 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001029#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 /* Send text written to clipboard register to the clipboard. */
1031 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001034
Bram Moolenaar113e1072019-01-20 15:30:40 +01001035#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
1036 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001038free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001039{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001040 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001041
1042 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001043 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001044 free_yank_all();
1045 vim_free(reg);
1046 *y_current = tmp;
1047}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050#if defined(FEAT_MOUSE) || defined(PROTO)
1051/*
1052 * return TRUE if the current yank register has type MLINE
1053 */
1054 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001055yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
1057 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1058 return FALSE;
1059 if (regname == '_') /* black hole is always empty */
1060 return FALSE;
1061 get_yank_register(regname, FALSE);
1062 return (y_current->y_type == MLINE);
1063}
1064#endif
1065
1066/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001067 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001069 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
1071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001072do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073{
Bram Moolenaard55de222007-05-06 13:38:48 +00001074 char_u *p;
1075 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001076 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001077 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001079 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001081 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1083 retval = FAIL;
1084 else
1085 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001086 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 showmode();
1088 regname = c;
1089 retval = OK;
1090 }
1091 }
1092 else /* stop recording */
1093 {
1094 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001095 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1096 * needs to be removed again to put it in a register. exec_reg then
1097 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001099 reg_recording = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001100 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 p = get_recorded();
1102 if (p == NULL)
1103 retval = FAIL;
1104 else
1105 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001106 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1107 vim_unescape_csi(p);
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 /*
1110 * We don't want to change the default register here, so save and
1111 * restore the current register name.
1112 */
1113 old_y_previous = y_previous;
1114 old_y_current = y_current;
1115
1116 retval = stuff_yank(regname, p);
1117
1118 y_previous = old_y_previous;
1119 y_current = old_y_current;
1120 }
1121 }
1122 return retval;
1123}
1124
1125/*
1126 * Stuff string "p" into yank register "regname" as a single line (append if
1127 * uppercase). "p" must have been alloced.
1128 *
1129 * return FAIL for failure, OK otherwise
1130 */
1131 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001132stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133{
1134 char_u *lp;
1135 char_u **pp;
1136
1137 /* check for read-only register */
1138 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1139 {
1140 vim_free(p);
1141 return FAIL;
1142 }
1143 if (regname == '_') /* black hole: don't do anything */
1144 {
1145 vim_free(p);
1146 return OK;
1147 }
1148 get_yank_register(regname, TRUE);
1149 if (y_append && y_current->y_array != NULL)
1150 {
1151 pp = &(y_current->y_array[y_current->y_size - 1]);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001152 lp = alloc(STRLEN(*pp) + STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (lp == NULL)
1154 {
1155 vim_free(p);
1156 return FAIL;
1157 }
1158 STRCPY(lp, *pp);
1159 STRCAT(lp, p);
1160 vim_free(p);
1161 vim_free(*pp);
1162 *pp = lp;
1163 }
1164 else
1165 {
1166 free_yank_all();
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001167 if ((y_current->y_array = ALLOC_ONE(char_u *)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
1169 vim_free(p);
1170 return FAIL;
1171 }
1172 y_current->y_array[0] = p;
1173 y_current->y_size = 1;
1174 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001175#ifdef FEAT_VIMINFO
1176 y_current->y_time_set = vim_time();
1177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179 return OK;
1180}
1181
Bram Moolenaar42b94362009-05-26 16:12:37 +00001182static int execreg_lastc = NUL;
1183
Bram Moolenaarc3328162019-07-23 22:15:25 +02001184 int
1185get_execreg_lastc(void)
1186{
1187 return execreg_lastc;
1188}
1189
1190 void
1191set_execreg_lastc(int lastc)
1192{
1193 execreg_lastc = lastc;
1194}
1195
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001197 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001199 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
1201 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001202do_execreg(
1203 int regname,
1204 int colon, /* insert ':' before each line */
1205 int addcr, /* always add '\n' to end of line */
1206 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 long i;
1209 char_u *p;
1210 int retval = OK;
1211 int remap;
1212
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001213 // repeat previous one
1214 if (regname == '@')
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001215 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001216 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 emsg(_("E748: No previously used register"));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001219 return FAIL;
1220 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001221 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001222 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001223 // check for valid regname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001225 {
1226 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001228 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001229 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231#ifdef FEAT_CLIPBOARD
1232 regname = may_get_selection(regname);
1233#endif
1234
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001235 // black hole: don't stuff anything
1236 if (regname == '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 return OK;
1238
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001239 // use last command line
1240 if (regname == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
1242 if (last_cmdline == NULL)
1243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001244 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 return FAIL;
1246 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001247 // don't keep the cmdline containing @:
1248 VIM_CLEAR(new_last_cmdline);
1249 // Escape all control characters with a CTRL-V
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001250 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001251 (char_u *)"\001\002\003\004\005\006\007"
1252 "\010\011\012\013\014\015\016\017"
1253 "\020\021\022\023\024\025\026\027"
1254 "\030\031\032\033\034\035\036\037",
1255 Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001256 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001257 {
1258 /* When in Visual mode "'<,'>" will be prepended to the command.
1259 * Remove it when it's already there. */
1260 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001261 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001262 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001263 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001264 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001265 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#ifdef FEAT_EVAL
1268 else if (regname == '=')
1269 {
1270 p = get_expr_line();
1271 if (p == NULL)
1272 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001273 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 vim_free(p);
1275 }
1276#endif
1277 else if (regname == '.') /* use last inserted text */
1278 {
1279 p = get_last_insert_save();
1280 if (p == NULL)
1281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001282 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 return FAIL;
1284 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001285 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 vim_free(p);
1287 }
1288 else
1289 {
1290 get_yank_register(regname, FALSE);
1291 if (y_current->y_array == NULL)
1292 return FAIL;
1293
1294 /* Disallow remaping for ":@r". */
1295 remap = colon ? REMAP_NONE : REMAP_YES;
1296
1297 /*
1298 * Insert lines into typeahead buffer, from last one to first one.
1299 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001300 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 for (i = y_current->y_size; --i >= 0; )
1302 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001303 char_u *escaped;
1304
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 /* insert NL between lines and after last line if type is MLINE */
1306 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1307 || addcr)
1308 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001309 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 return FAIL;
1311 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001312 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1313 if (escaped == NULL)
1314 return FAIL;
1315 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1316 vim_free(escaped);
1317 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001319 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 == FAIL)
1321 return FAIL;
1322 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001323 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325 return retval;
1326}
1327
1328/*
1329 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1330 * used only after other typeahead has been processed.
1331 */
1332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001333put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 char_u buf[3];
1336
1337 if (restart_edit != NUL)
1338 {
1339 if (restart_edit == 'V')
1340 {
1341 buf[0] = 'g';
1342 buf[1] = 'R';
1343 buf[2] = NUL;
1344 }
1345 else
1346 {
1347 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1348 buf[1] = NUL;
1349 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001350 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 restart_edit = NUL;
1352 }
1353}
1354
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001355/*
1356 * Insert register contents "s" into the typeahead buffer, so that it will be
1357 * executed again.
1358 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1359 * no remapping.
1360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001362put_in_typebuf(
1363 char_u *s,
1364 int esc,
1365 int colon, /* add ':' before the line */
1366 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
1368 int retval = OK;
1369
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001370 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001372 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001374 {
1375 char_u *p;
1376
1377 if (esc)
1378 p = vim_strsave_escape_csi(s);
1379 else
1380 p = s;
1381 if (p == NULL)
1382 retval = FAIL;
1383 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001384 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1385 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001386 if (esc)
1387 vim_free(p);
1388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001390 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 return retval;
1392}
1393
1394/*
1395 * Insert a yank register: copy it into the Read buffer.
1396 * Used by CTRL-R command and middle mouse button in insert mode.
1397 *
1398 * return FAIL for failure, OK otherwise
1399 */
1400 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001401insert_reg(
1402 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001403 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
1405 long i;
1406 int retval = OK;
1407 char_u *arg;
1408 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001409 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
1411 /*
1412 * It is possible to get into an endless loop by having CTRL-R a in
1413 * register a and then, in insert mode, doing CTRL-R a.
1414 * If you hit CTRL-C, the loop will be broken here.
1415 */
1416 ui_breakcheck();
1417 if (got_int)
1418 return FAIL;
1419
1420 /* check for valid regname */
1421 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1422 return FAIL;
1423
1424#ifdef FEAT_CLIPBOARD
1425 regname = may_get_selection(regname);
1426#endif
1427
1428 if (regname == '.') /* insert last inserted text */
1429 retval = stuff_inserted(NUL, 1L, TRUE);
1430 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1431 {
1432 if (arg == NULL)
1433 return FAIL;
1434 stuffescaped(arg, literally);
1435 if (allocated)
1436 vim_free(arg);
1437 }
1438 else /* name or number register */
1439 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001440 if (get_yank_register(regname, FALSE))
1441 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (y_current->y_array == NULL)
1443 retval = FAIL;
1444 else
1445 {
1446 for (i = 0; i < y_current->y_size; ++i)
1447 {
1448 stuffescaped(y_current->y_array[i], literally);
1449 /*
1450 * Insert a newline between lines and after last line if
1451 * y_type is MLINE.
1452 */
1453 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1454 stuffcharReadbuff('\n');
1455 }
1456 }
1457 }
1458
1459 return retval;
1460}
1461
1462/*
1463 * Stuff a string into the typeahead buffer, such that edit() will insert it
1464 * literally ("literally" TRUE) or interpret is as typed characters.
1465 */
1466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001467stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 int c;
1470 char_u *start;
1471
1472 while (*arg != NUL)
1473 {
1474 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1475 * stuff K_SPECIAL to get the effect of a special key when "literally"
1476 * is TRUE. */
1477 start = arg;
1478 while ((*arg >= ' '
1479#ifndef EBCDIC
1480 && *arg < DEL /* EBCDIC: chars above space are normal */
1481#endif
1482 )
1483 || (*arg == K_SPECIAL && !literally))
1484 ++arg;
1485 if (arg > start)
1486 stuffReadbuffLen(start, (long)(arg - start));
1487
1488 /* stuff a single special character */
1489 if (*arg != NUL)
1490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001492 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 c = *arg++;
1495 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1496 stuffcharReadbuff(Ctrl_V);
1497 stuffcharReadbuff(c);
1498 }
1499 }
1500}
1501
1502/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001503 * If "regname" is a special register, return TRUE and store a pointer to its
1504 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001507get_spec_reg(
1508 int regname,
1509 char_u **argp,
1510 int *allocated, /* return: TRUE when value was allocated */
1511 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 int cnt;
1514
1515 *argp = NULL;
1516 *allocated = FALSE;
1517 switch (regname)
1518 {
1519 case '%': /* file name */
1520 if (errmsg)
1521 check_fname(); /* will give emsg if not set */
1522 *argp = curbuf->b_fname;
1523 return TRUE;
1524
1525 case '#': /* alternate file name */
1526 *argp = getaltfname(errmsg); /* may give emsg if not set */
1527 return TRUE;
1528
1529#ifdef FEAT_EVAL
1530 case '=': /* result of expression */
1531 *argp = get_expr_line();
1532 *allocated = TRUE;
1533 return TRUE;
1534#endif
1535
1536 case ':': /* last command line */
1537 if (last_cmdline == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001538 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 *argp = last_cmdline;
1540 return TRUE;
1541
1542 case '/': /* last search-pattern */
1543 if (last_search_pat() == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001544 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 *argp = last_search_pat();
1546 return TRUE;
1547
1548 case '.': /* last inserted text */
1549 *argp = get_last_insert_save();
1550 *allocated = TRUE;
1551 if (*argp == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001552 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 return TRUE;
1554
1555#ifdef FEAT_SEARCHPATH
1556 case Ctrl_F: /* Filename under cursor */
1557 case Ctrl_P: /* Path under cursor, expand via "path" */
1558 if (!errmsg)
1559 return FALSE;
1560 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001561 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 *allocated = TRUE;
1563 return TRUE;
1564#endif
1565
1566 case Ctrl_W: /* word under cursor */
1567 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1568 if (!errmsg)
1569 return FALSE;
1570 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1571 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1572 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1573 *allocated = TRUE;
1574 return TRUE;
1575
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001576 case Ctrl_L: /* Line under cursor */
1577 if (!errmsg)
1578 return FALSE;
1579
1580 *argp = ml_get_buf(curwin->w_buffer,
1581 curwin->w_cursor.lnum, FALSE);
1582 return TRUE;
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 case '_': /* black hole: always empty */
1585 *argp = (char_u *)"";
1586 return TRUE;
1587 }
1588
1589 return FALSE;
1590}
1591
1592/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001593 * Paste a yank register into the command line.
1594 * Only for non-special registers.
1595 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 * insert_reg() can't be used here, because special characters from the
1597 * register contents will be interpreted as commands.
1598 *
1599 * return FAIL for failure, OK otherwise
1600 */
1601 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602cmdline_paste_reg(
1603 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001604 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001605 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001608 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001610 if (get_yank_register(regname, FALSE))
1611 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (y_current->y_array == NULL)
1613 return FAIL;
1614
1615 for (i = 0; i < y_current->y_size; ++i)
1616 {
1617 cmdline_paste_str(y_current->y_array[i], literally);
1618
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001619 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001620 * Don't do this when "remcr" is TRUE. */
1621 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 cmdline_paste_str((char_u *)"\r", literally);
1623
1624 /* Check for CTRL-C, in case someone tries to paste a few thousand
1625 * lines and gets bored. */
1626 ui_breakcheck();
1627 if (got_int)
1628 return FAIL;
1629 }
1630 return OK;
1631}
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1634/*
1635 * Adjust the register name pointed to with "rp" for the clipboard being
1636 * used always and the clipboard being available.
1637 */
1638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001639adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001641 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1642 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001643 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1644 {
1645 if (clip_unnamed != 0)
1646 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001647 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001648 else
1649 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1650 ? '+' : '*';
1651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (!clip_star.available && *rp == '*')
1653 *rp = 0;
1654 if (!clip_plus.available && *rp == '+')
1655 *rp = 0;
1656}
1657#endif
1658
1659/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001660 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1661 */
1662 void
1663shift_delete_registers()
1664{
1665 int n;
1666
1667 y_current = &y_regs[9];
1668 free_yank_all(); /* free register nine */
1669 for (n = 9; n > 1; --n)
1670 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001671 y_current = &y_regs[1];
1672 if (!y_append)
1673 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001674 y_regs[1].y_array = NULL; /* set register one to empty */
1675}
1676
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001678 static void
1679yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1680{
1681 static int recursive = FALSE;
1682 dict_T *v_event;
1683 list_T *list;
1684 int n;
1685 char_u buf[NUMBUFLEN + 2];
1686 long reglen = 0;
1687
1688 if (recursive)
1689 return;
1690
1691 v_event = get_vim_var_dict(VV_EVENT);
1692
1693 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001694 if (list == NULL)
1695 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001696 for (n = 0; n < reg->y_size; n++)
1697 list_append_string(list, reg->y_array[n], -1);
1698 list->lv_lock = VAR_FIXED;
1699 dict_add_list(v_event, "regcontents", list);
1700
1701 buf[0] = (char_u)oap->regname;
1702 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001703 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001704
1705 buf[0] = get_op_char(oap->op_type);
1706 buf[1] = get_extra_op_char(oap->op_type);
1707 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001708 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001709
1710 buf[0] = NUL;
1711 buf[1] = NUL;
1712 switch (get_reg_type(oap->regname, &reglen))
1713 {
1714 case MLINE: buf[0] = 'V'; break;
1715 case MCHAR: buf[0] = 'v'; break;
1716 case MBLOCK:
1717 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1718 reglen + 1);
1719 break;
1720 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001721 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001722
1723 /* Lock the dictionary and its keys */
1724 dict_set_items_ro(v_event);
1725
1726 recursive = TRUE;
1727 textlock++;
1728 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1729 textlock--;
1730 recursive = FALSE;
1731
1732 /* Empty the dictionary, v:event is still valid */
1733 dict_free_contents(v_event);
1734 hash_init(&v_event->dv_hashtab);
1735}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001736#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001737
Bram Moolenaara1891842017-02-04 21:34:31 +01001738/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001739 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001741 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 */
1743 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001744op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 int n;
1747 linenr_T lnum;
1748 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 char_u *newp, *oldp;
1750 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1752 int did_yank = FALSE;
1753
1754 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1755 return OK;
1756
1757 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1758 if (oap->empty)
1759 return u_save_cursor();
1760
1761 if (!curbuf->b_p_ma)
1762 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001763 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 return FAIL;
1765 }
1766
1767#ifdef FEAT_CLIPBOARD
1768 adjust_clip_reg(&oap->regname);
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (has_mbyte)
1772 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaard04b7502010-07-08 22:27:55 +02001774 /*
1775 * Imitate the strange Vi behaviour: If the delete spans more than one
1776 * line and motion_type == MCHAR and the result is a blank line, make the
1777 * delete linewise. Don't do this for the change command or Visual mode.
1778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001781 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001783 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 && oap->op_type == OP_DELETE)
1785 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001786 ptr = ml_get(oap->end.lnum) + oap->end.col;
1787 if (*ptr != NUL)
1788 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 ptr = skipwhite(ptr);
1790 if (*ptr == NUL && inindent(0))
1791 oap->motion_type = MLINE;
1792 }
1793
Bram Moolenaard04b7502010-07-08 22:27:55 +02001794 /*
1795 * Check for trying to delete (e.g. "D") in an empty line.
1796 * Note: For the change operator it is ok.
1797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if ( oap->motion_type == MCHAR
1799 && oap->line_count == 1
1800 && oap->op_type == OP_DELETE
1801 && *ml_get(oap->start.lnum) == NUL)
1802 {
1803 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001804 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 * 'cpoptions' (Vi compatible).
1806 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001807 if (virtual_op)
1808 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1809 * marks as if it happened. */
1810 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1812 beep_flush();
1813 return OK;
1814 }
1815
Bram Moolenaard04b7502010-07-08 22:27:55 +02001816 /*
1817 * Do a yank of whatever we're about to delete.
1818 * If a yank register was specified, put the deleted text into that
1819 * register. For the black hole register '_' don't yank anything.
1820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (oap->regname != '_')
1822 {
1823 if (oap->regname != 0)
1824 {
1825 /* check for read-only register */
1826 if (!valid_yank_reg(oap->regname, TRUE))
1827 {
1828 beep_flush();
1829 return OK;
1830 }
1831 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1832 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1833 did_yank = TRUE;
1834 }
1835
1836 /*
1837 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001838 * delete contains a line break, or when using a specific operator (Vi
1839 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +02001840 * Use the register name from before adjust_clip_reg() may have
1841 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001843 if (oap->motion_type == MLINE || oap->line_count > 1
1844 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001846 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (op_yank(oap, TRUE, FALSE) == OK)
1848 did_yank = TRUE;
1849 }
1850
Bram Moolenaar84298db2012-04-20 13:46:08 +02001851 /* Yank into small delete register when no named register specified
1852 * and the delete is within one line. */
1853 if ((
1854#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001855 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1856 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001857#endif
1858 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 && oap->line_count == 1)
1860 {
1861 oap->regname = '-';
1862 get_yank_register(oap->regname, TRUE);
1863 if (op_yank(oap, TRUE, FALSE) == OK)
1864 did_yank = TRUE;
1865 oap->regname = 0;
1866 }
1867
1868 /*
1869 * If there's too much stuff to fit in the yank register, then get a
1870 * confirmation before doing the delete. This is crude, but simple.
1871 * And it avoids doing a delete of something we can't put back if we
1872 * want.
1873 */
1874 if (!did_yank)
1875 {
1876 int msg_silent_save = msg_silent;
1877
1878 msg_silent = 0; /* must display the prompt */
1879 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1880 msg_silent = msg_silent_save;
1881 if (n != 'y')
1882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001883 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 return FAIL;
1885 }
1886 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001887
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001888#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001889 if (did_yank && has_textyankpost())
1890 yank_do_autocmd(oap, y_current);
1891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893
Bram Moolenaard04b7502010-07-08 22:27:55 +02001894 /*
1895 * block mode delete
1896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (oap->block_mode)
1898 {
1899 if (u_save((linenr_T)(oap->start.lnum - 1),
1900 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1901 return FAIL;
1902
1903 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1904 {
1905 block_prep(oap, &bd, lnum, TRUE);
1906 if (bd.textlen == 0) /* nothing to delete */
1907 continue;
1908
1909 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1910 if (lnum == curwin->w_cursor.lnum)
1911 {
1912 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915
Bram Moolenaar8055d172019-05-17 22:57:26 +02001916 // "n" == number of chars deleted
1917 // If we delete a TAB, it may be replaced by several characters.
1918 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 n = bd.textlen - bd.startspaces - bd.endspaces;
1920 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001921 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (newp == NULL)
1923 continue;
1924 /* copy up to deleted part */
1925 mch_memmove(newp, oldp, (size_t)bd.textcol);
1926 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001927 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 (size_t)(bd.startspaces + bd.endspaces));
1929 /* copy the part after the deleted part */
1930 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001931 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 /* replace the line */
1933 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001934
1935#ifdef FEAT_TEXT_PROP
1936 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001937 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940
1941 check_cursor_col();
1942 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1943 oap->end.lnum + 1, 0L);
1944 oap->line_count = 0; /* no lines deleted */
1945 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001946 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
1948 if (oap->op_type == OP_CHANGE)
1949 {
1950 /* Delete the lines except the first one. Temporarily move the
1951 * cursor to the next line. Save the current line number, if the
1952 * last line is deleted it may be changed.
1953 */
1954 if (oap->line_count > 1)
1955 {
1956 lnum = curwin->w_cursor.lnum;
1957 ++curwin->w_cursor.lnum;
1958 del_lines((long)(oap->line_count - 1), TRUE);
1959 curwin->w_cursor.lnum = lnum;
1960 }
1961 if (u_save_cursor() == FAIL)
1962 return FAIL;
1963 if (curbuf->b_p_ai) /* don't delete indent */
1964 {
1965 beginline(BL_WHITE); /* cursor on first non-white */
1966 did_ai = TRUE; /* delete the indent when ESC hit */
1967 ai_col = curwin->w_cursor.col;
1968 }
1969 else
1970 beginline(0); /* cursor in column 0 */
1971 truncate_line(FALSE); /* delete the rest of the line */
1972 /* leave cursor past last char in line */
1973 if (oap->line_count > 1)
1974 u_clearline(); /* "U" command not possible after "2cc" */
1975 }
1976 else
1977 {
1978 del_lines(oap->line_count, TRUE);
1979 beginline(BL_WHITE | BL_FIX);
1980 u_clearline(); /* "U" command not possible after "dd" */
1981 }
1982 }
1983 else
1984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (virtual_op)
1986 {
1987 int endcol = 0;
1988
1989 /* For virtualedit: break the tabs that are partly included. */
1990 if (gchar_pos(&oap->start) == '\t')
1991 {
1992 if (u_save_cursor() == FAIL) /* save first line for undo */
1993 return FAIL;
1994 if (oap->line_count == 1)
1995 endcol = getviscol2(oap->end.col, oap->end.coladd);
1996 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1997 oap->start = curwin->w_cursor;
1998 if (oap->line_count == 1)
1999 {
2000 coladvance(endcol);
2001 oap->end.col = curwin->w_cursor.col;
2002 oap->end.coladd = curwin->w_cursor.coladd;
2003 curwin->w_cursor = oap->start;
2004 }
2005 }
2006
2007 /* Break a tab only when it's included in the area. */
2008 if (gchar_pos(&oap->end) == '\t'
2009 && (int)oap->end.coladd < oap->inclusive)
2010 {
2011 /* save last line for undo */
2012 if (u_save((linenr_T)(oap->end.lnum - 1),
2013 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2014 return FAIL;
2015 curwin->w_cursor = oap->end;
2016 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2017 oap->end = curwin->w_cursor;
2018 curwin->w_cursor = oap->start;
2019 }
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 if (oap->line_count == 1) /* delete characters within one line */
2023 {
2024 if (u_save_cursor() == FAIL) /* save line for undo */
2025 return FAIL;
2026
2027 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002028 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 && oap->op_type == OP_CHANGE
2030 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002031 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 display_dollar(oap->end.col - !oap->inclusive);
2033
2034 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (virtual_op)
2037 {
2038 /* fix up things for virtualedit-delete:
2039 * break the tabs which are going to get in our way
2040 */
2041 char_u *curline = ml_get_curline();
2042 int len = (int)STRLEN(curline);
2043
2044 if (oap->end.coladd != 0
2045 && (int)oap->end.col >= len - 1
2046 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2047 n++;
2048 /* Delete at least one char (e.g, when on a control char). */
2049 if (n == 0 && oap->start.coladd != oap->end.coladd)
2050 n = 1;
2051
2052 /* When deleted a char in the line, reset coladd. */
2053 if (gchar_cursor() != NUL)
2054 curwin->w_cursor.coladd = 0;
2055 }
Bram Moolenaard009e862015-06-09 20:20:03 +02002056 (void)del_bytes((long)n, !virtual_op,
2057 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059 else /* delete characters between lines */
2060 {
2061 pos_T curpos;
2062
2063 /* save deleted and changed lines for undo */
2064 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2065 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2066 return FAIL;
2067
2068 truncate_line(TRUE); /* delete from cursor to end of line */
2069
2070 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2071 ++curwin->w_cursor.lnum;
2072 del_lines((long)(oap->line_count - 2), FALSE);
2073
Bram Moolenaard009e862015-06-09 20:20:03 +02002074 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002075 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002076 curwin->w_cursor.col = 0;
2077 (void)del_bytes((long)n, !virtual_op,
2078 oap->op_type == OP_DELETE && !oap->is_VIsual);
2079 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2080 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 }
2083
2084 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2085
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002086setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (oap->block_mode)
2088 {
2089 curbuf->b_op_end.lnum = oap->end.lnum;
2090 curbuf->b_op_end.col = oap->start.col;
2091 }
2092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 curbuf->b_op_end = oap->start;
2094 curbuf->b_op_start = oap->start;
2095
2096 return OK;
2097}
2098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099/*
2100 * Adjust end of operating area for ending on a multi-byte character.
2101 * Used for deletion.
2102 */
2103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002104mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105{
2106 char_u *p;
2107
2108 if (oap->inclusive)
2109 {
2110 p = ml_get(oap->end.lnum);
2111 oap->end.col += mb_tail_off(p, p + oap->end.col);
2112 }
2113}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar630afe82018-06-28 19:26:28 +02002115/*
2116 * Replace the character under the cursor with "c".
2117 * This takes care of multi-byte characters.
2118 */
2119 static void
2120replace_character(int c)
2121{
2122 int n = State;
2123
2124 State = REPLACE;
2125 ins_char(c);
2126 State = n;
2127 /* Backup to the replaced character. */
2128 dec_cursor();
2129}
2130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131/*
2132 * Replace a whole area with one character.
2133 */
2134 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002135op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
2137 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 char_u *newp, *oldp;
2140 size_t oldlen;
2141 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002142 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002143 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2146 return OK; /* nothing to do */
2147
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002148 if (c == REPLACE_CR_NCHAR)
2149 {
2150 had_ctrl_v_cr = TRUE;
2151 c = CAR;
2152 }
2153 else if (c == REPLACE_NL_NCHAR)
2154 {
2155 had_ctrl_v_cr = TRUE;
2156 c = NL;
2157 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if (has_mbyte)
2160 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
2162 if (u_save((linenr_T)(oap->start.lnum - 1),
2163 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2164 return FAIL;
2165
2166 /*
2167 * block mode replace
2168 */
2169 if (oap->block_mode)
2170 {
2171 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2172 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2173 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002174 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2176 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2177 continue; /* nothing to replace */
2178
2179 /* n == number of extra chars required
2180 * If we split a TAB, it may be replaced by several characters.
2181 * Thus the number of characters may increase!
2182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 /* If the range starts in virtual space, count the initial
2184 * coladd offset as part of "startspaces" */
2185 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2186 {
2187 pos_T vpos;
2188
Bram Moolenaara1381de2009-11-03 15:44:21 +00002189 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 getvpos(&vpos, oap->start_vcol);
2191 bd.startspaces += vpos.coladd;
2192 n = bd.startspaces;
2193 }
2194 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 /* allow for pre spaces */
2196 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2197
2198 /* allow for post spp */
2199 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2202 /* Figure out how many characters to replace. */
2203 numc = oap->end_vcol - oap->start_vcol + 1;
2204 if (bd.is_short && (!virtual_op || bd.is_MAX))
2205 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /* A double-wide character can be replaced only up to half the
2208 * times. */
2209 if ((*mb_char2cells)(c) > 1)
2210 {
2211 if ((numc & 1) && !bd.is_short)
2212 {
2213 ++bd.endspaces;
2214 ++n;
2215 }
2216 numc = numc / 2;
2217 }
2218
2219 /* Compute bytes needed, move character count to num_chars. */
2220 num_chars = numc;
2221 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /* oldlen includes textlen, so don't double count */
2223 n += numc - bd.textlen;
2224
2225 oldp = ml_get_curline();
2226 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002227 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (newp == NULL)
2229 continue;
2230 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2231 /* copy up to deleted part */
2232 mch_memmove(newp, oldp, (size_t)bd.textcol);
2233 oldp += bd.textcol + bd.textlen;
2234 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002235 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002237 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2238 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002239 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002241 if (has_mbyte)
2242 {
2243 n = (int)STRLEN(newp);
2244 while (--num_chars >= 0)
2245 n += (*mb_char2bytes)(c, newp + n);
2246 }
2247 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002248 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002249 if (!bd.is_short)
2250 {
2251 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002252 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002253 /* copy the part after the changed part */
2254 STRMOVE(newp + STRLEN(newp), oldp);
2255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002259 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002260 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01002261 if (after_p != NULL)
2262 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 /* replace the line */
2265 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002266 if (after_p != NULL)
2267 {
2268 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2269 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2270 oap->end.lnum++;
2271 vim_free(after_p);
2272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
2274 }
2275 else
2276 {
2277 /*
2278 * MCHAR and MLINE motion replace.
2279 */
2280 if (oap->motion_type == MLINE)
2281 {
2282 oap->start.col = 0;
2283 curwin->w_cursor.col = 0;
2284 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2285 if (oap->end.col)
2286 --oap->end.col;
2287 }
2288 else if (!oap->inclusive)
2289 dec(&(oap->end));
2290
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002291 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 n = gchar_cursor();
2294 if (n != NUL)
2295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2297 {
2298 /* This is slow, but it handles replacing a single-byte
2299 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002300 if (curwin->w_cursor.lnum == oap->end.lnum)
2301 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002302 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (n == TAB)
2307 {
2308 int end_vcol = 0;
2309
2310 if (curwin->w_cursor.lnum == oap->end.lnum)
2311 {
2312 /* oap->end has to be recalculated when
2313 * the tab breaks */
2314 end_vcol = getviscol2(oap->end.col,
2315 oap->end.coladd);
2316 }
2317 coladvance_force(getviscol());
2318 if (curwin->w_cursor.lnum == oap->end.lnum)
2319 getvpos(&oap->end, end_vcol);
2320 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02002321 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2325 {
2326 int virtcols = oap->end.coladd;
2327
2328 if (curwin->w_cursor.lnum == oap->start.lnum
2329 && oap->start.col == oap->end.col && oap->start.coladd)
2330 virtcols -= oap->start.coladd;
2331
2332 /* oap->end has been trimmed so it's effectively inclusive;
2333 * as a result an extra +1 must be counted so we don't
2334 * trample the NUL byte. */
2335 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2336 curwin->w_cursor.col -= (virtcols + 1);
2337 for (; virtcols >= 0; virtcols--)
2338 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02002339 if ((*mb_char2len)(c) > 1)
2340 replace_character(c);
2341 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002342 PBYTE(curwin->w_cursor, c);
2343 if (inc(&curwin->w_cursor) == -1)
2344 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348 /* Advance to next character, stop at the end of the file. */
2349 if (inc_cursor() == -1)
2350 break;
2351 }
2352 }
2353
2354 curwin->w_cursor = oap->start;
2355 check_cursor();
2356 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2357
2358 /* Set "'[" and "']" marks. */
2359 curbuf->b_op_start = oap->start;
2360 curbuf->b_op_end = oap->end;
2361
2362 return OK;
2363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002365static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367/*
2368 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2369 */
2370 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002371op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002375 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377 if (u_save((linenr_T)(oap->start.lnum - 1),
2378 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2379 return;
2380
2381 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (oap->block_mode) /* Visual block mode */
2383 {
2384 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2385 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002386 int one_change;
2387
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 block_prep(oap, &bd, pos.lnum, FALSE);
2389 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002390 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2391 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002392
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002393#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002394 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2397
Bram Moolenaar009b2592004-10-24 19:18:58 +00002398 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2399 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002401 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405 if (did_change)
2406 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2407 }
2408 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 if (oap->motion_type == MLINE)
2411 {
2412 oap->start.col = 0;
2413 pos.col = 0;
2414 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2415 if (oap->end.col)
2416 --oap->end.col;
2417 }
2418 else if (!oap->inclusive)
2419 dec(&(oap->end));
2420
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002421 if (pos.lnum == oap->end.lnum)
2422 did_change = swapchars(oap->op_type, &pos,
2423 oap->end.col - pos.col + 1);
2424 else
2425 for (;;)
2426 {
2427 did_change |= swapchars(oap->op_type, &pos,
2428 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2429 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002430 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002431 break;
2432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (did_change)
2434 {
2435 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2436 0L);
2437#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002438 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 char_u *ptr;
2441 int count;
2442
2443 pos = oap->start;
2444 while (pos.lnum < oap->end.lnum)
2445 {
2446 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002447 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002448 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002450 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 pos.col = 0;
2452 pos.lnum++;
2453 }
2454 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2455 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002456 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002458 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460#endif
2461 }
2462 }
2463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (!did_change && oap->is_VIsual)
2465 /* No change: need to remove the Visual selection */
2466 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468 /*
2469 * Set '[ and '] marks.
2470 */
2471 curbuf->b_op_start = oap->start;
2472 curbuf->b_op_end = oap->end;
2473
2474 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002475 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002476 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477}
2478
2479/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002480 * Invoke swapchar() on "length" bytes at position "pos".
2481 * "pos" is advanced to just after the changed characters.
2482 * "length" is rounded up to include the whole last multi-byte character.
2483 * Also works correctly when the number of bytes changes.
2484 * Returns TRUE if some character was changed.
2485 */
2486 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002487swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002488{
2489 int todo;
2490 int did_change = 0;
2491
2492 for (todo = length; todo > 0; --todo)
2493 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002494 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002495 {
2496 int len = (*mb_ptr2len)(ml_get_pos(pos));
2497
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002498 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002499 if (len > 0)
2500 todo -= len - 1;
2501 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002502 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002503 if (inc(pos) == -1) /* at end of file */
2504 break;
2505 }
2506 return did_change;
2507}
2508
2509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 * If op_type == OP_UPPER: make uppercase,
2511 * if op_type == OP_LOWER: make lowercase,
2512 * if op_type == OP_ROT13: do rot13 encoding,
2513 * else swap case of character at 'pos'
2514 * returns TRUE when something actually changed.
2515 */
2516 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002517swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 int c;
2520 int nc;
2521
2522 c = gchar_pos(pos);
2523
2524 /* Only do rot13 encoding for ASCII characters. */
2525 if (c >= 0x80 && op_type == OP_ROT13)
2526 return FALSE;
2527
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002528 if (op_type == OP_UPPER && c == 0xdf
2529 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002530 {
2531 pos_T sp = curwin->w_cursor;
2532
2533 /* Special handling of German sharp s: change to "SS". */
2534 curwin->w_cursor = *pos;
2535 del_char(FALSE);
2536 ins_char('S');
2537 ins_char('S');
2538 curwin->w_cursor = sp;
2539 inc(pos);
2540 }
2541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2543 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 nc = c;
2545 if (MB_ISLOWER(c))
2546 {
2547 if (op_type == OP_ROT13)
2548 nc = ROT13(c, 'a');
2549 else if (op_type != OP_LOWER)
2550 nc = MB_TOUPPER(c);
2551 }
2552 else if (MB_ISUPPER(c))
2553 {
2554 if (op_type == OP_ROT13)
2555 nc = ROT13(c, 'A');
2556 else if (op_type != OP_UPPER)
2557 nc = MB_TOLOWER(c);
2558 }
2559 if (nc != c)
2560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2562 {
2563 pos_T sp = curwin->w_cursor;
2564
2565 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002566 /* don't use del_char(), it also removes composing chars */
2567 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 ins_char(nc);
2569 curwin->w_cursor = sp;
2570 }
2571 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002572 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 return TRUE;
2574 }
2575 return FALSE;
2576}
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578/*
2579 * op_insert - Insert and append operators for Visual mode.
2580 */
2581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002582op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
2584 long ins_len, pre_textlen = 0;
2585 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002586 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 struct block_def bd;
2588 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002589 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591 /* edit() changes this - record it for OP_APPEND */
2592 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2593
2594 /* vis block is still marked. Get rid of it now. */
2595 curwin->w_cursor.lnum = oap->start.lnum;
2596 update_screen(INVERTED);
2597
2598 if (oap->block_mode)
2599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 /* When 'virtualedit' is used, need to insert the extra spaces before
2601 * doing block_prep(). When only "block" is used, virtual edit is
2602 * already disabled, but still need it when calling
2603 * coladvance_force(). */
2604 if (curwin->w_cursor.coladd > 0)
2605 {
2606 int old_ve_flags = ve_flags;
2607
2608 ve_flags = VE_ALL;
2609 if (u_save_cursor() == FAIL)
2610 return;
2611 coladvance_force(oap->op_type == OP_APPEND
2612 ? oap->end_vcol + 1 : getviscol());
2613 if (oap->op_type == OP_APPEND)
2614 --curwin->w_cursor.col;
2615 ve_flags = old_ve_flags;
2616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 /* Get the info about the block before entering the text */
2618 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002619 /* Get indent information */
2620 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (oap->op_type == OP_APPEND)
2624 firstline += bd.textlen;
2625 pre_textlen = (long)STRLEN(firstline);
2626 }
2627
2628 if (oap->op_type == OP_APPEND)
2629 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002630 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 /* Move the cursor to the character right of the block. */
2633 curwin->w_set_curswant = TRUE;
2634 while (*ml_get_cursor() != NUL
2635 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2636 ++curwin->w_cursor.col;
2637 if (bd.is_short && !bd.is_MAX)
2638 {
2639 /* First line was too short, make it longer and adjust the
2640 * values in "bd". */
2641 if (u_save_cursor() == FAIL)
2642 return;
2643 for (i = 0; i < bd.endspaces; ++i)
2644 ins_char(' ');
2645 bd.textlen += bd.endspaces;
2646 }
2647 }
2648 else
2649 {
2650 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002651 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002654 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 && oap->start_vcol != oap->end_vcol)
2656 inc_cursor();
2657 }
2658 }
2659
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002660 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002661 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002663 /* When a tab was inserted, and the characters in front of the tab
2664 * have been converted to a tab as well, the column of the cursor
2665 * might have actually been reduced, so need to adjust here. */
2666 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002667 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002668 oap->start = curbuf->b_op_start_orig;
2669
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002670 /* If user has moved off this line, we don't know what to do, so do
2671 * nothing.
2672 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2673 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 return;
2675
2676 if (oap->block_mode)
2677 {
2678 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002679 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002680 size_t len;
2681 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002683 /* If indent kicked in, the firstline might have changed
2684 * but only do that, if the indent actually increased. */
2685 ind_post = (colnr_T)getwhitecols_curline();
2686 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2687 {
2688 bd.textcol += ind_post - ind_pre;
2689 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002690 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002691 }
2692
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002693 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002694 * to adjust the block for that. But only do it, if the difference
2695 * does not come from indent kicking in. */
2696 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2697 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002698 {
2699 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002700 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002701 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002702 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002703 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002704 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002705 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002706 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002707 pre_textlen -= t - oap->start_vcol;
2708 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002709 }
2710 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002711 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002712 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002713 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002714 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002715 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002716 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002717 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002718 /* reset pre_textlen to the value of OP_INSERT */
2719 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002720 pre_textlen -= t - oap->start_vcol;
2721 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002722 oap->op_type = OP_INSERT;
2723 }
2724 }
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 /*
2727 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002728 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 * Don't do this when "$" used, end-of-line will have changed.
2730 */
2731 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2732 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2733 {
2734 if (oap->op_type == OP_APPEND)
2735 {
2736 pre_textlen += bd2.textlen - bd.textlen;
2737 if (bd2.endspaces)
2738 --bd2.textlen;
2739 }
2740 bd.textcol = bd2.textcol;
2741 bd.textlen = bd2.textlen;
2742 }
2743
2744 /*
2745 * Subsequent calls to ml_get() flush the firstline data - take a
2746 * copy of the required string.
2747 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002748 firstline = ml_get(oap->start.lnum);
2749 len = STRLEN(firstline);
2750 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002752 add += bd.textlen;
2753 if ((size_t)add > len)
2754 firstline += len; // short line, point to the NUL
2755 else
2756 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002757 if (pre_textlen >= 0
2758 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
2760 ins_text = vim_strnsave(firstline, (int)ins_len);
2761 if (ins_text != NULL)
2762 {
2763 /* block handled here */
2764 if (u_save(oap->start.lnum,
2765 (linenr_T)(oap->end.lnum + 1)) == OK)
2766 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2767 &bd);
2768
2769 curwin->w_cursor.col = oap->start.col;
2770 check_cursor();
2771 vim_free(ins_text);
2772 }
2773 }
2774 }
2775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
2777/*
2778 * op_change - handle a change operation
2779 *
2780 * return TRUE if edit() returns because of a CTRL-O command
2781 */
2782 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002783op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 colnr_T l;
2786 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 long offset;
2788 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002789 long ins_len;
2790 long pre_textlen = 0;
2791 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 char_u *firstline;
2793 char_u *ins_text, *newp, *oldp;
2794 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
2796 l = oap->start.col;
2797 if (oap->motion_type == MLINE)
2798 {
2799 l = 0;
2800#ifdef FEAT_SMARTINDENT
2801 if (!p_paste && curbuf->b_p_si
2802# ifdef FEAT_CINDENT
2803 && !curbuf->b_p_cin
2804# endif
2805 )
2806 can_si = TRUE; /* It's like opening a new line, do si */
2807#endif
2808 }
2809
2810 /* First delete the text in the region. In an empty buffer only need to
2811 * save for undo */
2812 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2813 {
2814 if (u_save_cursor() == FAIL)
2815 return FALSE;
2816 }
2817 else if (op_delete(oap) == FAIL)
2818 return FALSE;
2819
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002820 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 && !virtual_op)
2822 inc_cursor();
2823
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 /* check for still on same line (<CR> in inserted text meaningless) */
2825 /* skip blank lines too */
2826 if (oap->block_mode)
2827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 /* Add spaces before getting the current line length. */
2829 if (virtual_op && (curwin->w_cursor.coladd > 0
2830 || gchar_cursor() == NUL))
2831 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00002832 firstline = ml_get(oap->start.lnum);
2833 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002834 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 bd.textcol = curwin->w_cursor.col;
2836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2839 if (oap->motion_type == MLINE)
2840 fix_indent();
2841#endif
2842
2843 retval = edit(NUL, FALSE, (linenr_T)1);
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002846 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002848 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002850 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002852 /* Auto-indenting may have changed the indent. If the cursor was past
2853 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002855 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002857 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002858
2859 pre_textlen += new_indent - pre_indent;
2860 bd.textcol += new_indent - pre_indent;
2861 }
2862
2863 ins_len = (long)STRLEN(firstline) - pre_textlen;
2864 if (ins_len > 0)
2865 {
2866 /* Subsequent calls to ml_get() flush the firstline data - take a
2867 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002868 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002870 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2872 linenr++)
2873 {
2874 block_prep(oap, &bd, linenr, TRUE);
2875 if (!bd.is_short || virtual_op)
2876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 pos_T vpos;
2878
2879 /* If the block starts in virtual space, count the
2880 * initial coladd offset as part of "startspaces" */
2881 if (bd.is_short)
2882 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002883 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
2886 else
2887 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002889 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 if (newp == NULL)
2891 continue;
2892 /* copy up to block start */
2893 mch_memmove(newp, oldp, (size_t)bd.textcol);
2894 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002895 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2898 offset += ins_len;
2899 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002900 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 ml_replace(linenr, newp, FALSE);
2902 }
2903 }
2904 check_cursor();
2905
2906 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2907 }
2908 vim_free(ins_text);
2909 }
2910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 return retval;
2913}
2914
2915/*
2916 * set all the yank registers to empty (called from main())
2917 */
2918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002919init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
2921 int i;
2922
2923 for (i = 0; i < NUM_REGISTERS; ++i)
2924 y_regs[i].y_array = NULL;
2925}
2926
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002927#if defined(EXITFREE) || defined(PROTO)
2928 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002929clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002930{
2931 int i;
2932
2933 for (i = 0; i < NUM_REGISTERS; ++i)
2934 {
2935 y_current = &y_regs[i];
2936 if (y_current->y_array != NULL)
2937 free_yank_all();
2938 }
2939}
2940#endif
2941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942/*
2943 * Free "n" lines from the current yank register.
2944 * Called for normal freeing and in case of error.
2945 */
2946 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002947free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
2949 if (y_current->y_array != NULL)
2950 {
2951 long i;
2952
2953 for (i = n; --i >= 0; )
2954 {
2955#ifdef AMIGA /* only for very slow machines */
2956 if ((i & 1023) == 1023) /* this may take a while */
2957 {
2958 /*
2959 * This message should never cause a hit-return message.
2960 * Overwrite this message with any next message.
2961 */
2962 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002963 smsg(_("freeing %ld lines"), i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 --no_wait_return;
2965 msg_didout = FALSE;
2966 msg_col = 0;
2967 }
2968#endif
2969 vim_free(y_current->y_array[i]);
2970 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002971 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#ifdef AMIGA
2973 if (n >= 1000)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002974 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#endif
2976 }
2977}
2978
2979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002980free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 free_yank(y_current->y_size);
2983}
2984
2985/*
2986 * Yank the text between "oap->start" and "oap->end" into a yank register.
2987 * If we are to append (uppercase register), we first yank into a new yank
2988 * register and then concatenate the old and the new one (so we keep the old
2989 * one in case of out-of-memory).
2990 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002991 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 */
2993 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002994op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002997 yankreg_T *curr; /* copy of y_current */
2998 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 char_u **new_ptr;
3000 linenr_T lnum; /* current line number */
3001 long j;
3002 int yanktype = oap->motion_type;
3003 long yanklines = oap->line_count;
3004 linenr_T yankendlnum = oap->end.lnum;
3005 char_u *p;
3006 char_u *pnew;
3007 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003008#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003009 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 /* check for read-only register */
3013 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3014 {
3015 beep_flush();
3016 return FAIL;
3017 }
3018 if (oap->regname == '_') /* black hole: nothing to do */
3019 return OK;
3020
3021#ifdef FEAT_CLIPBOARD
3022 if (!clip_star.available && oap->regname == '*')
3023 oap->regname = 0;
3024 else if (!clip_plus.available && oap->regname == '+')
3025 oap->regname = 0;
3026#endif
3027
3028 if (!deleting) /* op_delete() already set y_current */
3029 get_yank_register(oap->regname, TRUE);
3030
3031 curr = y_current;
3032 /* append to existing contents */
3033 if (y_append && y_current->y_array != NULL)
3034 y_current = &newreg;
3035 else
3036 free_yank_all(); /* free previously yanked lines */
3037
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003038 /*
3039 * If the cursor was in column 1 before and after the movement, and the
3040 * operator is not inclusive, the yank is always linewise.
3041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if ( oap->motion_type == MCHAR
3043 && oap->start.col == 0
3044 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003046 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 && oap->end.col == 0
3048 && yanklines > 1)
3049 {
3050 yanktype = MLINE;
3051 --yankendlnum;
3052 --yanklines;
3053 }
3054
3055 y_current->y_size = yanklines;
3056 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 y_current->y_width = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003058 y_current->y_array = lalloc_clear(sizeof(char_u *) * yanklines, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if (y_current->y_array == NULL)
3060 {
3061 y_current = curr;
3062 return FAIL;
3063 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003064#ifdef FEAT_VIMINFO
3065 y_current->y_time_set = vim_time();
3066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
3068 y_idx = 0;
3069 lnum = oap->start.lnum;
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 if (oap->block_mode)
3072 {
3073 /* Visual block mode */
3074 y_current->y_type = MBLOCK; /* set the yank register type */
3075 y_current->y_width = oap->end_vcol - oap->start_vcol;
3076
3077 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3078 y_current->y_width--;
3079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3082 {
3083 switch (y_current->y_type)
3084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 case MBLOCK:
3086 block_prep(oap, &bd, lnum, FALSE);
3087 if (yank_copy_line(&bd, y_idx) == FAIL)
3088 goto fail;
3089 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
3091 case MLINE:
3092 if ((y_current->y_array[y_idx] =
3093 vim_strsave(ml_get(lnum))) == NULL)
3094 goto fail;
3095 break;
3096
3097 case MCHAR:
3098 {
3099 colnr_T startcol = 0, endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 int is_oneChar = FALSE;
3101 colnr_T cs, ce;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003102
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 p = ml_get(lnum);
3104 bd.startspaces = 0;
3105 bd.endspaces = 0;
3106
3107 if (lnum == oap->start.lnum)
3108 {
3109 startcol = oap->start.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 if (virtual_op)
3111 {
3112 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3113 if (ce != cs && oap->start.coladd > 0)
3114 {
3115 /* Part of a tab selected -- but don't
3116 * double-count it. */
3117 bd.startspaces = (ce - cs + 1)
3118 - oap->start.coladd;
3119 startcol++;
3120 }
3121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
3123
3124 if (lnum == oap->end.lnum)
3125 {
3126 endcol = oap->end.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (virtual_op)
3128 {
3129 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3130 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 /* Don't add space for double-wide
3132 * char; endcol will be on last byte
3133 * of multi-byte char. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003134 && (*mb_head_off)(p, p + endcol) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 if (oap->start.lnum == oap->end.lnum
3137 && oap->start.col == oap->end.col)
3138 {
3139 /* Special case: inside a single char */
3140 is_oneChar = TRUE;
3141 bd.startspaces = oap->end.coladd
3142 - oap->start.coladd + oap->inclusive;
3143 endcol = startcol;
3144 }
3145 else
3146 {
3147 bd.endspaces = oap->end.coladd
3148 + oap->inclusive;
3149 endcol -= oap->inclusive;
3150 }
3151 }
3152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003154 if (endcol == MAXCOL)
3155 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003156 if (startcol > endcol || is_oneChar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 bd.textlen = 0;
3158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 bd.textlen = endcol - startcol + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 bd.textstart = p + startcol;
3161 if (yank_copy_line(&bd, y_idx) == FAIL)
3162 goto fail;
3163 break;
3164 }
3165 /* NOTREACHED */
3166 }
3167 }
3168
3169 if (curr != y_current) /* append the new block to the old block */
3170 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003171 new_ptr = ALLOC_MULT(char_u *, curr->y_size + y_current->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (new_ptr == NULL)
3173 goto fail;
3174 for (j = 0; j < curr->y_size; ++j)
3175 new_ptr[j] = curr->y_array[j];
3176 vim_free(curr->y_array);
3177 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003178#ifdef FEAT_VIMINFO
3179 curr->y_time_set = vim_time();
3180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
3182 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3183 curr->y_type = MLINE;
3184
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003185 /* Concatenate the last line of the old block with the first line of
3186 * the new block, unless being Vi compatible. */
3187 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02003189 pnew = alloc(STRLEN(curr->y_array[curr->y_size - 1])
3190 + STRLEN(y_current->y_array[0]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (pnew == NULL)
3192 {
3193 y_idx = y_current->y_size - 1;
3194 goto fail;
3195 }
3196 STRCPY(pnew, curr->y_array[--j]);
3197 STRCAT(pnew, y_current->y_array[0]);
3198 vim_free(curr->y_array[j]);
3199 vim_free(y_current->y_array[0]);
3200 curr->y_array[j++] = pnew;
3201 y_idx = 1;
3202 }
3203 else
3204 y_idx = 0;
3205 while (y_idx < y_current->y_size)
3206 curr->y_array[j++] = y_current->y_array[y_idx++];
3207 curr->y_size = j;
3208 vim_free(y_current->y_array);
3209 y_current = curr;
3210 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003211 if (curwin->w_p_rnu)
3212 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 if (mess) /* Display message about yank? */
3214 {
3215 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 && yanklines == 1)
3218 yanklines = 0;
3219 /* Some versions of Vi use ">=" here, some don't... */
3220 if (yanklines > p_report)
3221 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003222 char namebuf[100];
3223
3224 if (oap->regname == NUL)
3225 *namebuf = NUL;
3226 else
3227 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003228 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /* redisplay now, so message is not deleted */
3231 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003232 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003234 smsg(NGETTEXT("block of %ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003235 "block of %ld lines yanked%s", yanklines),
3236 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003240 smsg(NGETTEXT("%ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003241 "%ld lines yanked%s", yanklines),
3242 yanklines, namebuf);
3243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245 }
3246
3247 /*
3248 * Set "'[" and "']" marks.
3249 */
3250 curbuf->b_op_start = oap->start;
3251 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003252 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003253 {
3254 curbuf->b_op_start.col = 0;
3255 curbuf->b_op_end.col = MAXCOL;
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258#ifdef FEAT_CLIPBOARD
3259 /*
3260 * If we were yanking to the '*' register, send result to clipboard.
3261 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3262 * to the '*' register.
3263 */
3264 if (clip_star.available
3265 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003266 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003267 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 if (curr != &(y_regs[STAR_REGISTER]))
3270 /* Copy the text from register 0 to the clipboard register. */
3271 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3272
3273 clip_own_selection(&clip_star);
3274 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003275# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003276 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279
3280# ifdef FEAT_X11
3281 /*
3282 * If we were yanking to the '+' register, send result to selection.
3283 * Also copy to the '*' register, in case auto-select is off.
3284 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003285 if (clip_plus.available
3286 && (curr == &(y_regs[PLUS_REGISTER])
3287 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003288 && ((clip_unnamed | clip_unnamed_saved) &
3289 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003291 if (curr != &(y_regs[PLUS_REGISTER]))
3292 /* Copy the text from register 0 to the clipboard register. */
3293 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 clip_own_selection(&clip_plus);
3296 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003297 if (!clip_isautosel_star() && !clip_isautosel_plus()
3298 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3301 clip_own_selection(&clip_star);
3302 clip_gen_set_selection(&clip_star);
3303 }
3304 }
3305# endif
3306#endif
3307
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003308#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003309 if (!deleting && has_textyankpost())
3310 yank_do_autocmd(oap, y_current);
3311#endif
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 return OK;
3314
3315fail: /* free the allocated lines */
3316 free_yank(y_idx + 1);
3317 y_current = curr;
3318 return FAIL;
3319}
3320
3321 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003322yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 char_u *pnew;
3325
3326 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3327 == NULL)
3328 return FAIL;
3329 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003330 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 pnew += bd->startspaces;
3332 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3333 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003334 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 pnew += bd->endspaces;
3336 *pnew = NUL;
3337 return OK;
3338}
3339
3340#ifdef FEAT_CLIPBOARD
3341/*
3342 * Make a copy of the y_current register to register "reg".
3343 */
3344 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003345copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003347 yankreg_T *curr = y_current;
3348 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 y_current = reg;
3351 free_yank_all();
3352 *y_current = *curr;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003353 y_current->y_array = lalloc_clear(
Bram Moolenaar51e14382019-05-25 20:21:28 +02003354 sizeof(char_u *) * y_current->y_size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (y_current->y_array == NULL)
3356 y_current->y_size = 0;
3357 else
3358 for (j = 0; j < y_current->y_size; ++j)
3359 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3360 {
3361 free_yank(j);
3362 y_current->y_size = 0;
3363 break;
3364 }
3365 y_current = curr;
3366}
3367#endif
3368
3369/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003370 * Put contents of register "regname" into the text.
3371 * Caller must check "regname" to be valid!
3372 * "flags": PUT_FIXINDENT make indent look nice
3373 * PUT_CURSEND leave cursor after end of new text
3374 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
3376 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003377do_put(
3378 int regname,
3379 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3380 long count,
3381 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 char_u *ptr;
3384 char_u *newp, *oldp;
3385 int yanklen;
3386 int totlen = 0; /* init for gcc */
3387 linenr_T lnum;
3388 colnr_T col;
3389 long i; /* index in y_array[] */
3390 int y_type;
3391 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 int oldlen;
3393 long y_width = 0;
3394 colnr_T vcol;
3395 int delcount;
3396 int incr = 0;
3397 long j;
3398 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 char_u **y_array = NULL;
3400 long nr_lines = 0;
3401 pos_T new_cursor;
3402 int indent;
3403 int orig_indent = 0; /* init for gcc */
3404 int indent_diff = 0; /* init for gcc */
3405 int first_indent = TRUE;
3406 int lendiff = 0;
3407 pos_T old_pos;
3408 char_u *insert_string = NULL;
3409 int allocated = FALSE;
3410 long cnt;
3411
3412#ifdef FEAT_CLIPBOARD
3413 /* Adjust register name for "unnamed" in 'clipboard'. */
3414 adjust_clip_reg(&regname);
3415 (void)may_get_selection(regname);
3416#endif
3417
3418 if (flags & PUT_FIXINDENT)
3419 orig_indent = get_indent();
3420
3421 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3422 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3423
3424 /*
3425 * Using inserted text works differently, because the register includes
3426 * special characters (newlines, etc.).
3427 */
3428 if (regname == '.')
3429 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003430 if (VIsual_active)
3431 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3433 (count == -1 ? 'O' : 'i')), count, FALSE);
3434 /* Putting the text is done later, so can't really move the cursor to
3435 * the next character. Use "l" to simulate it. */
3436 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3437 stuffcharReadbuff('l');
3438 return;
3439 }
3440
3441 /*
3442 * For special registers '%' (file name), '#' (alternate file name) and
3443 * ':' (last command line), etc. we have to create a fake yank register.
3444 */
3445 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3446 {
3447 if (insert_string == NULL)
3448 return;
3449 }
3450
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003451 /* Autocommands may be executed when saving lines for undo. This might
3452 * make "y_array" invalid, so we start undo now to avoid that. */
3453 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3454 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if (insert_string != NULL)
3457 {
3458 y_type = MCHAR;
3459#ifdef FEAT_EVAL
3460 if (regname == '=')
3461 {
3462 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003463 * characters.
3464 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 for (;;)
3466 {
3467 y_size = 0;
3468 ptr = insert_string;
3469 while (ptr != NULL)
3470 {
3471 if (y_array != NULL)
3472 y_array[y_size] = ptr;
3473 ++y_size;
3474 ptr = vim_strchr(ptr, '\n');
3475 if (ptr != NULL)
3476 {
3477 if (y_array != NULL)
3478 *ptr = NUL;
3479 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003480 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (*ptr == NUL)
3482 {
3483 y_type = MLINE;
3484 break;
3485 }
3486 }
3487 }
3488 if (y_array != NULL)
3489 break;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003490 y_array = ALLOC_MULT(char_u *, y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (y_array == NULL)
3492 goto end;
3493 }
3494 }
3495 else
3496#endif
3497 {
3498 y_size = 1; /* use fake one-line yank register */
3499 y_array = &insert_string;
3500 }
3501 }
3502 else
3503 {
3504 get_yank_register(regname, FALSE);
3505
3506 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 y_size = y_current->y_size;
3509 y_array = y_current->y_array;
3510 }
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (y_type == MLINE)
3513 {
3514 if (flags & PUT_LINE_SPLIT)
3515 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003516 char_u *p;
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 /* "p" or "P" in Visual mode: split the lines to put the text in
3519 * between. */
3520 if (u_save_cursor() == FAIL)
3521 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003522 p = ml_get_cursor();
3523 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003524 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003525 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (ptr == NULL)
3527 goto end;
3528 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3529 vim_free(ptr);
3530
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003531 oldp = ml_get_curline();
3532 p = oldp + curwin->w_cursor.col;
3533 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003534 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003535 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (ptr == NULL)
3537 goto end;
3538 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3539 ++nr_lines;
3540 dir = FORWARD;
3541 }
3542 if (flags & PUT_LINE_FORWARD)
3543 {
3544 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003545 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 dir = FORWARD;
3547 }
3548 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3549 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3553 y_type = MLINE;
3554
3555 if (y_size == 0 || y_array == NULL)
3556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003557 semsg(_("E353: Nothing in register %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 regname == 0 ? (char_u *)"\"" : transchar(regname));
3559 goto end;
3560 }
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (y_type == MBLOCK)
3563 {
3564 lnum = curwin->w_cursor.lnum + y_size + 1;
3565 if (lnum > curbuf->b_ml.ml_line_count)
3566 lnum = curbuf->b_ml.ml_line_count + 1;
3567 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3568 goto end;
3569 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003570 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 lnum = curwin->w_cursor.lnum;
3573#ifdef FEAT_FOLDING
3574 /* Correct line number for closed fold. Don't move the cursor yet,
3575 * u_save() uses it. */
3576 if (dir == BACKWARD)
3577 (void)hasFolding(lnum, &lnum, NULL);
3578 else
3579 (void)hasFolding(lnum, NULL, &lnum);
3580#endif
3581 if (dir == FORWARD)
3582 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003583 /* In an empty buffer the empty line is going to be replaced, include
3584 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003585 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 goto end;
3587#ifdef FEAT_FOLDING
3588 if (dir == FORWARD)
3589 curwin->w_cursor.lnum = lnum - 1;
3590 else
3591 curwin->w_cursor.lnum = lnum;
3592 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3593#endif
3594 }
3595 else if (u_save_cursor() == FAIL)
3596 goto end;
3597
3598 yanklen = (int)STRLEN(y_array[0]);
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (ve_flags == VE_ALL && y_type == MCHAR)
3601 {
3602 if (gchar_cursor() == TAB)
3603 {
3604 /* Don't need to insert spaces when "p" on the last position of a
3605 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003606#ifdef FEAT_VARTABS
3607 int viscol = getviscol();
3608 if (dir == FORWARD
3609 ? tabstop_padding(viscol, curbuf->b_p_ts,
3610 curbuf->b_p_vts_array) != 1
3611 : curwin->w_cursor.coladd > 0)
3612 coladvance_force(viscol);
3613#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (dir == FORWARD
3615 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3616 : curwin->w_cursor.coladd > 0)
3617 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
3620 curwin->w_cursor.coladd = 0;
3621 }
3622 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3623 coladvance_force(getviscol() + (dir == FORWARD));
3624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
3626 lnum = curwin->w_cursor.lnum;
3627 col = curwin->w_cursor.col;
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 /*
3630 * Block mode
3631 */
3632 if (y_type == MBLOCK)
3633 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003634 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 colnr_T endcol2 = 0;
3636
3637 if (dir == FORWARD && c != NUL)
3638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (ve_flags == VE_ALL)
3640 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (has_mbyte)
3645 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003646 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (c != TAB || ve_flags != VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 ++curwin->w_cursor.col;
3650 ++col;
3651 }
3652 else
3653 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003656 if (ve_flags == VE_ALL
3657 && (curwin->w_cursor.coladd > 0
3658 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 if (dir == FORWARD && c == NUL)
3661 ++col;
3662 if (dir != FORWARD && c != NUL)
3663 ++curwin->w_cursor.col;
3664 if (c == TAB)
3665 {
3666 if (dir == BACKWARD && curwin->w_cursor.col)
3667 curwin->w_cursor.col--;
3668 if (dir == FORWARD && col - 1 == endcol2)
3669 curwin->w_cursor.col++;
3670 }
3671 }
3672 curwin->w_cursor.coladd = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003673 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 for (i = 0; i < y_size; ++i)
3675 {
3676 int spaces;
3677 char shortline;
3678
3679 bd.startspaces = 0;
3680 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 vcol = 0;
3682 delcount = 0;
3683
3684 /* add a new line */
3685 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3686 {
3687 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3688 (colnr_T)1, FALSE) == FAIL)
3689 break;
3690 ++nr_lines;
3691 }
3692 /* get the old line and advance to the position to insert at */
3693 oldp = ml_get_curline();
3694 oldlen = (int)STRLEN(oldp);
3695 for (ptr = oldp; vcol < col && *ptr; )
3696 {
3697 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003698 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 vcol += incr;
3700 }
3701 bd.textcol = (colnr_T)(ptr - oldp);
3702
3703 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3704
3705 if (vcol < col) /* line too short, padd with spaces */
3706 bd.startspaces = col - vcol;
3707 else if (vcol > col)
3708 {
3709 bd.endspaces = vcol - col;
3710 bd.startspaces = incr - bd.endspaces;
3711 --bd.textcol;
3712 delcount = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (has_mbyte)
3714 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 if (oldp[bd.textcol] != TAB)
3716 {
3717 /* Only a Tab can be split into spaces. Other
3718 * characters will have to be moved to after the
3719 * block, causing misalignment. */
3720 delcount = 0;
3721 bd.endspaces = 0;
3722 }
3723 }
3724
3725 yanklen = (int)STRLEN(y_array[i]);
3726
3727 /* calculate number of spaces required to fill right side of block*/
3728 spaces = y_width + 1;
3729 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003730 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (spaces < 0)
3732 spaces = 0;
3733
3734 /* insert the new text */
3735 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003736 newp = alloc(totlen + oldlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (newp == NULL)
3738 break;
3739 /* copy part up to cursor to new line */
3740 ptr = newp;
3741 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3742 ptr += bd.textcol;
3743 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003744 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 ptr += bd.startspaces;
3746 /* insert the new text */
3747 for (j = 0; j < count; ++j)
3748 {
3749 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3750 ptr += yanklen;
3751
3752 /* insert block's trailing spaces only if there's text behind */
3753 if ((j < count - 1 || !shortline) && spaces)
3754 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003755 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 ptr += spaces;
3757 }
3758 }
3759 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003760 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 ptr += bd.endspaces;
3762 /* move the text after the cursor to the end of the line. */
3763 mch_memmove(ptr, oldp + bd.textcol + delcount,
3764 (size_t)(oldlen - bd.textcol - delcount + 1));
3765 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3766
3767 ++curwin->w_cursor.lnum;
3768 if (i == 0)
3769 curwin->w_cursor.col += bd.startspaces;
3770 }
3771
3772 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3773
3774 /* Set '[ mark. */
3775 curbuf->b_op_start = curwin->w_cursor;
3776 curbuf->b_op_start.lnum = lnum;
3777
3778 /* adjust '] mark */
3779 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3780 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 curbuf->b_op_end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 if (flags & PUT_CURSEND)
3783 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003784 colnr_T len;
3785
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 curwin->w_cursor = curbuf->b_op_end;
3787 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003788
3789 /* in Insert mode we might be after the NUL, correct for that */
3790 len = (colnr_T)STRLEN(ml_get_curline());
3791 if (curwin->w_cursor.col > len)
3792 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 else
3795 curwin->w_cursor.lnum = lnum;
3796 }
3797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
3799 /*
3800 * Character or Line mode
3801 */
3802 if (y_type == MCHAR)
3803 {
3804 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3805 * char */
3806 if (dir == FORWARD && gchar_cursor() != NUL)
3807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (has_mbyte)
3809 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003810 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
3812 /* put it on the next of the multi-byte character. */
3813 col += bytelen;
3814 if (yanklen)
3815 {
3816 curwin->w_cursor.col += bytelen;
3817 curbuf->b_op_end.col += bytelen;
3818 }
3819 }
3820 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
3822 ++col;
3823 if (yanklen)
3824 {
3825 ++curwin->w_cursor.col;
3826 ++curbuf->b_op_end.col;
3827 }
3828 }
3829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 curbuf->b_op_start = curwin->w_cursor;
3831 }
3832 /*
3833 * Line mode: BACKWARD is the same as FORWARD on the previous line
3834 */
3835 else if (dir == BACKWARD)
3836 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003837 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 /*
3840 * simple case: insert into current line
3841 */
3842 if (y_type == MCHAR && y_size == 1)
3843 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003844 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003845
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003846 if (VIsual_active)
3847 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003848 end_lnum = curbuf->b_visual.vi_end.lnum;
3849 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3850 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003851 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003852
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003853 do {
3854 totlen = count * yanklen;
3855 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003857 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003858 if (VIsual_active && col > (int)STRLEN(oldp))
3859 {
3860 lnum++;
3861 continue;
3862 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02003863 newp = alloc(STRLEN(oldp) + totlen + 1);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003864 if (newp == NULL)
3865 goto end; /* alloc() gave an error message */
3866 mch_memmove(newp, oldp, (size_t)col);
3867 ptr = newp + col;
3868 for (i = 0; i < count; ++i)
3869 {
3870 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3871 ptr += yanklen;
3872 }
3873 STRMOVE(ptr, oldp + col);
3874 ml_replace(lnum, newp, FALSE);
3875 /* Place cursor on last putted char. */
3876 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003877 {
3878 /* make sure curwin->w_virtcol is updated */
3879 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003880 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003883 if (VIsual_active)
3884 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003885 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003886
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003887 if (VIsual_active) /* reset lnum to the last visual line */
3888 lnum--;
3889
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 curbuf->b_op_end = curwin->w_cursor;
3891 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3892 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3893 ++curwin->w_cursor.col;
3894 changed_bytes(lnum, col);
3895 }
3896 else
3897 {
3898 /*
3899 * Insert at least one line. When y_type is MCHAR, break the first
3900 * line in two.
3901 */
3902 for (cnt = 1; cnt <= count; ++cnt)
3903 {
3904 i = 0;
3905 if (y_type == MCHAR)
3906 {
3907 /*
3908 * Split the current line in two at the insert position.
3909 * First insert y_array[size - 1] in front of second line.
3910 * Then append y_array[0] to first line.
3911 */
3912 lnum = new_cursor.lnum;
3913 ptr = ml_get(lnum) + col;
3914 totlen = (int)STRLEN(y_array[y_size - 1]);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003915 newp = alloc(STRLEN(ptr) + totlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (newp == NULL)
3917 goto error;
3918 STRCPY(newp, y_array[y_size - 1]);
3919 STRCAT(newp, ptr);
3920 /* insert second line */
3921 ml_append(lnum, newp, (colnr_T)0, FALSE);
3922 vim_free(newp);
3923
3924 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003925 newp = alloc(col + yanklen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (newp == NULL)
3927 goto error;
3928 /* copy first part of line */
3929 mch_memmove(newp, oldp, (size_t)col);
3930 /* append to first line */
3931 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3932 ml_replace(lnum, newp, FALSE);
3933
3934 curwin->w_cursor.lnum = lnum;
3935 i = 1;
3936 }
3937
3938 for (; i < y_size; ++i)
3939 {
3940 if ((y_type != MCHAR || i < y_size - 1)
3941 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3942 == FAIL)
3943 goto error;
3944 lnum++;
3945 ++nr_lines;
3946 if (flags & PUT_FIXINDENT)
3947 {
3948 old_pos = curwin->w_cursor;
3949 curwin->w_cursor.lnum = lnum;
3950 ptr = ml_get(lnum);
3951 if (cnt == count && i == y_size - 1)
3952 lendiff = (int)STRLEN(ptr);
3953#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3954 if (*ptr == '#' && preprocs_left())
3955 indent = 0; /* Leave # lines at start */
3956 else
3957#endif
3958 if (*ptr == NUL)
3959 indent = 0; /* Ignore empty lines */
3960 else if (first_indent)
3961 {
3962 indent_diff = orig_indent - get_indent();
3963 indent = orig_indent;
3964 first_indent = FALSE;
3965 }
3966 else if ((indent = get_indent() + indent_diff) < 0)
3967 indent = 0;
3968 (void)set_indent(indent, 0);
3969 curwin->w_cursor = old_pos;
3970 /* remember how many chars were removed */
3971 if (cnt == count && i == y_size - 1)
3972 lendiff -= (int)STRLEN(ml_get(lnum));
3973 }
3974 }
3975 }
3976
3977error:
3978 /* Adjust marks. */
3979 if (y_type == MLINE)
3980 {
3981 curbuf->b_op_start.col = 0;
3982 if (dir == FORWARD)
3983 curbuf->b_op_start.lnum++;
3984 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003985 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003986 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02003987 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003988 < curbuf->b_ml.ml_line_count
3989#ifdef FEAT_DIFF
3990 || curwin->w_p_diff
3991#endif
3992 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003993 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 (linenr_T)MAXLNUM, nr_lines, 0L);
3995
3996 /* note changed text for displaying and folding */
3997 if (y_type == MCHAR)
3998 changed_lines(curwin->w_cursor.lnum, col,
3999 curwin->w_cursor.lnum + 1, nr_lines);
4000 else
4001 changed_lines(curbuf->b_op_start.lnum, 0,
4002 curbuf->b_op_start.lnum, nr_lines);
4003
4004 /* put '] mark at last inserted character */
4005 curbuf->b_op_end.lnum = lnum;
4006 /* correct length for change in indent */
4007 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4008 if (col > 1)
4009 curbuf->b_op_end.col = col - 1;
4010 else
4011 curbuf->b_op_end.col = 0;
4012
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004013 if (flags & PUT_CURSLINE)
4014 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004015 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004016 curwin->w_cursor.lnum = lnum;
4017 beginline(BL_WHITE | BL_FIX);
4018 }
4019 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
4021 /* put cursor after inserted text */
4022 if (y_type == MLINE)
4023 {
4024 if (lnum >= curbuf->b_ml.ml_line_count)
4025 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4026 else
4027 curwin->w_cursor.lnum = lnum + 1;
4028 curwin->w_cursor.col = 0;
4029 }
4030 else
4031 {
4032 curwin->w_cursor.lnum = lnum;
4033 curwin->w_cursor.col = col;
4034 }
4035 }
4036 else if (y_type == MLINE)
4037 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004038 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 curwin->w_cursor.col = 0;
4040 if (dir == FORWARD)
4041 ++curwin->w_cursor.lnum;
4042 beginline(BL_WHITE | BL_FIX);
4043 }
4044 else /* put cursor on first inserted character */
4045 curwin->w_cursor = new_cursor;
4046 }
4047 }
4048
4049 msgmore(nr_lines);
4050 curwin->w_set_curswant = TRUE;
4051
4052end:
4053 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004055 if (regname == '=')
4056 vim_free(y_array);
4057
Bram Moolenaar033d8882013-09-25 23:24:57 +02004058 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004059
Bram Moolenaar677ee682005-01-27 14:41:15 +00004060 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004061 adjust_cursor_eol();
4062}
4063
4064/*
4065 * When the cursor is on the NUL past the end of the line and it should not be
4066 * there move it left.
4067 */
4068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004069adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004070{
4071 if (curwin->w_cursor.col > 0
4072 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004073 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 && !(restart_edit || (State & INSERT)))
4075 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004076 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004077 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004080 {
4081 colnr_T scol, ecol;
4082
4083 /* Coladd is set to the width of the last character. */
4084 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4085 curwin->w_cursor.coladd = ecol - scol + 1;
4086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088}
4089
4090#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4091/*
4092 * Return TRUE if lines starting with '#' should be left aligned.
4093 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004094 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004095preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096{
4097 return
4098# ifdef FEAT_SMARTINDENT
4099# ifdef FEAT_CINDENT
4100 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4101# else
4102 curbuf->b_p_si
4103# endif
4104# endif
4105# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004106 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4107 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108# endif
4109 ;
4110}
4111#endif
4112
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004113/*
4114 * Return the character name of the register with the given number.
4115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004117get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
4119 if (num == -1)
4120 return '"';
4121 else if (num < 10)
4122 return num + '0';
4123 else if (num == DELETION_REGISTER)
4124 return '-';
4125#ifdef FEAT_CLIPBOARD
4126 else if (num == STAR_REGISTER)
4127 return '*';
4128 else if (num == PLUS_REGISTER)
4129 return '+';
4130#endif
4131 else
4132 {
4133#ifdef EBCDIC
4134 int i;
4135
4136 /* EBCDIC is really braindead ... */
4137 i = 'a' + (num - 10);
4138 if (i > 'i')
4139 i += 7;
4140 if (i > 'r')
4141 i += 8;
4142 return i;
4143#else
4144 return num + 'a' - 10;
4145#endif
4146 }
4147}
4148
4149/*
4150 * ":dis" and ":registers": Display the contents of the yank registers.
4151 */
4152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004153ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004155 int i, n;
4156 long j;
4157 char_u *p;
4158 yankreg_T *yb;
4159 int name;
4160 int attr;
4161 char_u *arg = eap->arg;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004162 int clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 if (arg != NULL && *arg == NUL)
4165 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004166 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004169 msg_puts_title(_("\n--- Registers ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4171 {
4172 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004173 if (arg != NULL && vim_strchr(arg, name) == NULL
4174#ifdef ONE_CLIPBOARD
4175 /* Star register and plus register contain the same thing. */
4176 && (name != '*' || vim_strchr(arg, '+') == NULL)
4177#endif
4178 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 continue; /* did not ask for this register */
4180
4181#ifdef FEAT_CLIPBOARD
4182 /* Adjust register name for "unnamed" in 'clipboard'.
4183 * When it's a clipboard register, fill it with the current contents
4184 * of the clipboard. */
4185 adjust_clip_reg(&name);
4186 (void)may_get_selection(name);
4187#endif
4188
4189 if (i == -1)
4190 {
4191 if (y_previous != NULL)
4192 yb = y_previous;
4193 else
4194 yb = &(y_regs[0]);
4195 }
4196 else
4197 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004198
4199#ifdef FEAT_EVAL
4200 if (name == MB_TOLOWER(redir_reg)
4201 || (redir_reg == '"' && yb == y_previous))
4202 continue; /* do not list register being written to, the
4203 * pointer can be freed */
4204#endif
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (yb->y_array != NULL)
4207 {
4208 msg_putchar('\n');
4209 msg_putchar('"');
4210 msg_putchar(name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004211 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 n = (int)Columns - 6;
4214 for (j = 0; j < yb->y_size && n > 1; ++j)
4215 {
4216 if (j)
4217 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004218 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 n -= 2;
4220 }
4221 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4222 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004223 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004224 msg_outtrans_len(p, clen);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004225 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227 }
4228 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004229 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 out_flush(); /* show one line at a time */
4231 }
4232 ui_breakcheck();
4233 }
4234
4235 /*
4236 * display last inserted text
4237 */
4238 if ((p = get_last_insert()) != NULL
4239 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4240 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004241 msg_puts("\n\". ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 dis_msg(p, TRUE);
4243 }
4244
4245 /*
4246 * display last command line
4247 */
4248 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4249 && !got_int)
4250 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004251 msg_puts("\n\": ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 dis_msg(last_cmdline, FALSE);
4253 }
4254
4255 /*
4256 * display current file name
4257 */
4258 if (curbuf->b_fname != NULL
4259 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4260 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004261 msg_puts("\n\"% ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 dis_msg(curbuf->b_fname, FALSE);
4263 }
4264
4265 /*
4266 * display alternate file name
4267 */
4268 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4269 {
4270 char_u *fname;
4271 linenr_T dummy;
4272
4273 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4274 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004275 msg_puts("\n\"# ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 dis_msg(fname, FALSE);
4277 }
4278 }
4279
4280 /*
4281 * display last search pattern
4282 */
4283 if (last_search_pat() != NULL
4284 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4285 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004286 msg_puts("\n\"/ ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 dis_msg(last_search_pat(), FALSE);
4288 }
4289
4290#ifdef FEAT_EVAL
4291 /*
4292 * display last used expression
4293 */
4294 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4295 && !got_int)
4296 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004297 msg_puts("\n\"= ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 dis_msg(expr_line, FALSE);
4299 }
4300#endif
4301}
4302
4303/*
4304 * display a string for do_dis()
4305 * truncate at end of screen line
4306 */
4307 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004308dis_msg(
4309 char_u *p,
4310 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
4315 n = (int)Columns - 6;
4316 while (*p != NUL
4317 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4318 && (n -= ptr2cells(p)) >= 0)
4319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004320 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322 msg_outtrans_len(p, l);
4323 p += l;
4324 }
4325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 msg_outtrans_len(p++, 1);
4327 }
4328 ui_breakcheck();
4329}
4330
Bram Moolenaar81340392012-06-06 16:12:59 +02004331#if defined(FEAT_COMMENTS) || defined(PROTO)
4332/*
4333 * If "process" is TRUE and the line begins with a comment leader (possibly
4334 * after some white space), return a pointer to the text after it. Put a boolean
4335 * value indicating whether the line ends with an unclosed comment in
4336 * "is_comment".
4337 * line - line to be processed,
4338 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004339 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004340 * include_space - whether to also skip space following the comment leader,
4341 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004342 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004343 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004344 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004345skip_comment(
4346 char_u *line,
4347 int process,
4348 int include_space,
4349 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004350{
4351 char_u *comment_flags = NULL;
4352 int lead_len;
4353 int leader_offset = get_last_leader_offset(line, &comment_flags);
4354
4355 *is_comment = FALSE;
4356 if (leader_offset != -1)
4357 {
4358 /* Let's check whether the line ends with an unclosed comment.
4359 * If the last comment leader has COM_END in flags, there's no comment.
4360 */
4361 while (*comment_flags)
4362 {
4363 if (*comment_flags == COM_END
4364 || *comment_flags == ':')
4365 break;
4366 ++comment_flags;
4367 }
4368 if (*comment_flags != COM_END)
4369 *is_comment = TRUE;
4370 }
4371
4372 if (process == FALSE)
4373 return line;
4374
4375 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4376
4377 if (lead_len == 0)
4378 return line;
4379
4380 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004381 * - COM_END,
4382 * - colon,
4383 * whichever comes first.
4384 */
4385 while (*comment_flags)
4386 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004387 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004388 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02004389 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02004390 ++comment_flags;
4391 }
4392
4393 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004394 * starting with a closing part of a three-part comment. That's good,
4395 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004396 */
4397 if (*comment_flags == ':' || *comment_flags == NUL)
4398 line += lead_len;
4399
4400 return line;
4401}
4402#endif
4403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004405 * Join 'count' lines (minimal 2) at cursor position.
4406 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004407 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4408 * leaders should not be removed.
4409 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4410 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004412 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 */
4414 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004415do_join(
4416 long count,
4417 int insert_space,
4418 int save_undo,
4419 int use_formatoptions UNUSED,
4420 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004422 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004423 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004424 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004426 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004427 int endcurr1 = NUL;
4428 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004429 int currsize = 0; /* size of the current line */
4430 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004432 colnr_T col = 0;
4433 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004434#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004435 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004436 int remove_comments = (use_formatoptions == TRUE)
4437 && has_format_option(FO_REMOVE_COMS);
4438 int prev_was_comment;
4439#endif
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004440#ifdef FEAT_TEXT_PROP
4441 textprop_T **prop_lines = NULL;
4442 int *prop_lengths = NULL;
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004445 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4446 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4447 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004449 /* Allocate an array to store the number of spaces inserted before each
4450 * line. We will use it to pre-compute the length of the new line and the
4451 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004452 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004453 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004455#if defined(FEAT_COMMENTS) || defined(PROTO)
4456 if (remove_comments)
4457 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004458 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02004459 if (comments == NULL)
4460 {
4461 vim_free(spaces);
4462 return FAIL;
4463 }
4464 }
4465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004468 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004469 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004470 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004472 for (t = 0; t < count; ++t)
4473 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004474 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004475 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004476 {
4477 /* Set the '[ mark. */
4478 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4479 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4480 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004481#if defined(FEAT_COMMENTS) || defined(PROTO)
4482 if (remove_comments)
4483 {
4484 /* We don't want to remove the comment leader if the
4485 * previous line is not a comment. */
4486 if (t > 0 && prev_was_comment)
4487 {
4488
4489 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4490 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004491 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004492 curr = new_curr;
4493 }
4494 else
4495 curr = skip_comment(curr, FALSE, insert_space,
4496 &prev_was_comment);
4497 }
4498#endif
4499
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004500 if (insert_space && t > 0)
4501 {
4502 curr = skipwhite(curr);
4503 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004504 && (!has_format_option(FO_MBYTE_JOIN)
4505 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4506 && (!has_format_option(FO_MBYTE_JOIN2)
4507 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004508 )
4509 {
4510 /* don't add a space if the line is ending in a space */
4511 if (endcurr1 == ' ')
4512 endcurr1 = endcurr2;
4513 else
4514 ++spaces[t];
4515 /* extra space when 'joinspaces' set and line ends in '.' */
4516 if ( p_js
4517 && (endcurr1 == '.'
4518 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4519 && (endcurr1 == '?' || endcurr1 == '!'))))
4520 ++spaces[t];
4521 }
4522 }
4523 currsize = (int)STRLEN(curr);
4524 sumsize += currsize + spaces[t];
4525 endcurr1 = endcurr2 = NUL;
4526 if (insert_space && currsize > 0)
4527 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004528 if (has_mbyte)
4529 {
4530 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004531 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004532 endcurr1 = (*mb_ptr2char)(cend);
4533 if (cend > curr)
4534 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004535 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004536 endcurr2 = (*mb_ptr2char)(cend);
4537 }
4538 }
4539 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004540 {
4541 endcurr1 = *(curr + currsize - 1);
4542 if (currsize > 1)
4543 endcurr2 = *(curr + currsize - 2);
4544 }
4545 }
4546 line_breakcheck();
4547 if (got_int)
4548 {
4549 ret = FAIL;
4550 goto theend;
4551 }
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004554 /* store the column position before last line */
4555 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004557 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004558 newp = alloc(sumsize + 1);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004559 cend = newp + sumsize;
4560 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004562#ifdef FEAT_TEXT_PROP
4563 // We need to move properties of the lines that are going to be deleted to
4564 // the new long one.
4565 if (curbuf->b_has_textprop && !text_prop_frozen)
4566 {
4567 // Allocate an array to copy the text properties of joined lines into.
4568 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004569 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
4570 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004571 if (prop_lengths == NULL)
4572 VIM_CLEAR(prop_lines);
4573 }
4574#endif
4575
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004576 /*
4577 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004578 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004579 *
4580 * Move marks from each deleted line to the joined line, adjusting the
4581 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4582 * should not really be a problem.
4583 */
4584 for (t = count - 1; ; --t)
4585 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004586 int spaces_removed;
4587
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004588 cend -= currsize;
4589 mch_memmove(cend, curr, (size_t)currsize);
4590 if (spaces[t] > 0)
4591 {
4592 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004593 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004594 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004595
4596 // If deleting more spaces than adding, the cursor moves no more than
4597 // what is added if it is inside these spaces.
4598 spaces_removed = (curr - curr_start) - spaces[t];
4599
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004600 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004601 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004602 if (t == 0)
4603 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004604#ifdef FEAT_TEXT_PROP
4605 if (prop_lines != NULL)
4606 adjust_props_for_join(curwin->w_cursor.lnum + t,
4607 prop_lines + t - 1, prop_lengths + t - 1,
4608 (long)(cend - newp - spaces_removed), spaces_removed);
4609#endif
4610
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004611 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004612#if defined(FEAT_COMMENTS)
Bram Moolenaar81340392012-06-06 16:12:59 +02004613 if (remove_comments)
4614 curr += comments[t - 1];
4615#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004616 if (insert_space && t > 1)
4617 curr = skipwhite(curr);
4618 currsize = (int)STRLEN(curr);
4619 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004620
4621#ifdef FEAT_TEXT_PROP
4622 if (prop_lines != NULL)
4623 join_prop_lines(curwin->w_cursor.lnum, newp,
4624 prop_lines, prop_lengths, count);
4625 else
4626#endif
4627 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004629 if (setmark)
4630 {
4631 /* Set the '] mark. */
4632 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02004633 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004634 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 /* Only report the change in the first line here, del_lines() will report
4637 * the deleted line. */
4638 changed_lines(curwin->w_cursor.lnum, currsize,
4639 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004641 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 * briefly, and then move it back. After del_lines() the cursor may
4643 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 */
4645 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004647 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 curwin->w_cursor.lnum = t;
4649
4650 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004651 * Set the cursor column:
4652 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004653 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004655 curwin->w_cursor.col =
4656 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 curwin->w_set_curswant = TRUE;
4661
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004662theend:
4663 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004664#if defined(FEAT_COMMENTS) || defined(PROTO)
4665 if (remove_comments)
4666 vim_free(comments);
4667#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004668 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669}
4670
4671#ifdef FEAT_COMMENTS
4672/*
4673 * Return TRUE if the two comment leaders given are the same. "lnum" is
4674 * the first line. White-space is ignored. Note that the whole of
4675 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4676 */
4677 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004678same_leader(
4679 linenr_T lnum,
4680 int leader1_len,
4681 char_u *leader1_flags,
4682 int leader2_len,
4683 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 int idx1 = 0, idx2 = 0;
4686 char_u *p;
4687 char_u *line1;
4688 char_u *line2;
4689
4690 if (leader1_len == 0)
4691 return (leader2_len == 0);
4692
4693 /*
4694 * If first leader has 'f' flag, the lines can be joined only if the
4695 * second line does not have a leader.
4696 * If first leader has 'e' flag, the lines can never be joined.
4697 * If fist leader has 's' flag, the lines can only be joined if there is
4698 * some text after it and the second line has the 'm' flag.
4699 */
4700 if (leader1_flags != NULL)
4701 {
4702 for (p = leader1_flags; *p && *p != ':'; ++p)
4703 {
4704 if (*p == COM_FIRST)
4705 return (leader2_len == 0);
4706 if (*p == COM_END)
4707 return FALSE;
4708 if (*p == COM_START)
4709 {
4710 if (*(ml_get(lnum) + leader1_len) == NUL)
4711 return FALSE;
4712 if (leader2_flags == NULL || leader2_len == 0)
4713 return FALSE;
4714 for (p = leader2_flags; *p && *p != ':'; ++p)
4715 if (*p == COM_MIDDLE)
4716 return TRUE;
4717 return FALSE;
4718 }
4719 }
4720 }
4721
4722 /*
4723 * Get current line and next line, compare the leaders.
4724 * The first line has to be saved, only one line can be locked at a time.
4725 */
4726 line1 = vim_strsave(ml_get(lnum));
4727 if (line1 != NULL)
4728 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004729 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 ;
4731 line2 = ml_get(lnum + 1);
4732 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4733 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004734 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
4736 if (line1[idx1++] != line2[idx2])
4737 break;
4738 }
4739 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004740 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 ++idx1;
4742 }
4743 vim_free(line1);
4744 }
4745 return (idx2 == leader2_len && idx1 == leader1_len);
4746}
4747#endif
4748
4749/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004750 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 */
4752 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004753op_format(
4754 oparg_T *oap,
4755 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
4757 long old_line_count = curbuf->b_ml.ml_line_count;
4758
4759 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4760 * can put it back there. */
4761 curwin->w_cursor = oap->cursor_start;
4762
4763 if (u_save((linenr_T)(oap->start.lnum - 1),
4764 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4765 return;
4766 curwin->w_cursor = oap->start;
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (oap->is_VIsual)
4769 /* When there is no change: need to remove the Visual selection */
4770 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 /* Set '[ mark at the start of the formatted area */
4773 curbuf->b_op_start = oap->start;
4774
4775 /* For "gw" remember the cursor position and put it back below (adjusted
4776 * for joined and split lines). */
4777 if (keep_cursor)
4778 saved_cursor = oap->cursor_start;
4779
Bram Moolenaar81a82092008-03-12 16:27:00 +00004780 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 /*
4783 * Leave the cursor at the first non-blank of the last formatted line.
4784 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4785 * line, so "." will do the next lines.
4786 */
4787 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4788 ++curwin->w_cursor.lnum;
4789 beginline(BL_WHITE | BL_FIX);
4790 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4791 msgmore(old_line_count);
4792
4793 /* put '] mark on the end of the formatted area */
4794 curbuf->b_op_end = curwin->w_cursor;
4795
4796 if (keep_cursor)
4797 {
4798 curwin->w_cursor = saved_cursor;
4799 saved_cursor.lnum = 0;
4800 }
4801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (oap->is_VIsual)
4803 {
4804 win_T *wp;
4805
4806 FOR_ALL_WINDOWS(wp)
4807 {
4808 if (wp->w_old_cursor_lnum != 0)
4809 {
4810 /* When lines have been inserted or deleted, adjust the end of
4811 * the Visual area to be redrawn. */
4812 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4813 wp->w_old_cursor_lnum += old_line_count;
4814 else
4815 wp->w_old_visual_lnum += old_line_count;
4816 }
4817 }
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819}
4820
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004821#if defined(FEAT_EVAL) || defined(PROTO)
4822/*
4823 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4824 */
4825 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004826op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004827{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004828 if (oap->is_VIsual)
4829 /* When there is no change: need to remove the Visual selection */
4830 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004831
Bram Moolenaar700303e2010-07-11 17:35:50 +02004832 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4833 /* As documented: when 'formatexpr' returns non-zero fall back to
4834 * internal formatting. */
4835 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004836}
4837
4838 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004839fex_format(
4840 linenr_T lnum,
4841 long count,
4842 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004843{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004844 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4845 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004846 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004847 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004848
4849 /*
4850 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004851 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004852 */
4853 set_vim_var_nr(VV_LNUM, lnum);
4854 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004855 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004856
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004857 /* Make a copy, the option could be changed while calling it. */
4858 fex = vim_strsave(curbuf->b_p_fex);
4859 if (fex == NULL)
4860 return 0;
4861
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004862 /*
4863 * Evaluate the function.
4864 */
4865 if (use_sandbox)
4866 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004867 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004868 if (use_sandbox)
4869 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004870
4871 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004872 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004874 return r;
4875}
4876#endif
4877
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878/*
4879 * Format "line_count" lines, starting at the cursor position.
4880 * When "line_count" is negative, format until the end of the paragraph.
4881 * Lines after the cursor line are saved for undo, caller must have saved the
4882 * first line.
4883 */
4884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004885format_lines(
4886 linenr_T line_count,
4887 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
4889 int max_len;
4890 int is_not_par; /* current line not part of parag. */
4891 int next_is_not_par; /* next line not part of paragraph */
4892 int is_end_par; /* at end of paragraph */
4893 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4894 int next_is_start_par = FALSE;
4895#ifdef FEAT_COMMENTS
4896 int leader_len = 0; /* leader len of current line */
4897 int next_leader_len; /* leader len of next line */
4898 char_u *leader_flags = NULL; /* flags for leader of current line */
4899 char_u *next_leader_flags; /* flags for leader of next line */
4900 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004901 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
4903 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004904 int second_indent = -1; /* indent for second line (comment
4905 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 int do_second_indent;
4907 int do_number_indent;
4908 int do_trail_white;
4909 int first_par_line = TRUE;
4910 int smd_save;
4911 long count;
4912 int need_set_indent = TRUE; /* set indent of next paragraph */
4913 int force_format = FALSE;
4914 int old_State = State;
4915
4916 /* length of a line to force formatting: 3 * 'tw' */
4917 max_len = comp_textwidth(TRUE) * 3;
4918
4919 /* check for 'q', '2' and '1' in 'formatoptions' */
4920#ifdef FEAT_COMMENTS
4921 do_comments = has_format_option(FO_Q_COMS);
4922#endif
4923 do_second_indent = has_format_option(FO_Q_SECOND);
4924 do_number_indent = has_format_option(FO_Q_NUMBER);
4925 do_trail_white = has_format_option(FO_WHITE_PAR);
4926
4927 /*
4928 * Get info about the previous and current line.
4929 */
4930 if (curwin->w_cursor.lnum > 1)
4931 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4932#ifdef FEAT_COMMENTS
4933 , &leader_len, &leader_flags, do_comments
4934#endif
4935 );
4936 else
4937 is_not_par = TRUE;
4938 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4939#ifdef FEAT_COMMENTS
4940 , &next_leader_len, &next_leader_flags, do_comments
4941#endif
4942 );
4943 is_end_par = (is_not_par || next_is_not_par);
4944 if (!is_end_par && do_trail_white)
4945 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4946
4947 curwin->w_cursor.lnum--;
4948 for (count = line_count; count != 0 && !got_int; --count)
4949 {
4950 /*
4951 * Advance to next paragraph.
4952 */
4953 if (advance)
4954 {
4955 curwin->w_cursor.lnum++;
4956 prev_is_end_par = is_end_par;
4957 is_not_par = next_is_not_par;
4958#ifdef FEAT_COMMENTS
4959 leader_len = next_leader_len;
4960 leader_flags = next_leader_flags;
4961#endif
4962 }
4963
4964 /*
4965 * The last line to be formatted.
4966 */
4967 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4968 {
4969 next_is_not_par = TRUE;
4970#ifdef FEAT_COMMENTS
4971 next_leader_len = 0;
4972 next_leader_flags = NULL;
4973#endif
4974 }
4975 else
4976 {
4977 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4978#ifdef FEAT_COMMENTS
4979 , &next_leader_len, &next_leader_flags, do_comments
4980#endif
4981 );
4982 if (do_number_indent)
4983 next_is_start_par =
4984 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4985 }
4986 advance = TRUE;
4987 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4988 if (!is_end_par && do_trail_white)
4989 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4990
4991 /*
4992 * Skip lines that are not in a paragraph.
4993 */
4994 if (is_not_par)
4995 {
4996 if (line_count < 0)
4997 break;
4998 }
4999 else
5000 {
5001 /*
5002 * For the first line of a paragraph, check indent of second line.
5003 * Don't do this for comments and empty lines.
5004 */
5005 if (first_par_line
5006 && (do_second_indent || do_number_indent)
5007 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005008 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005010 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005011 {
5012#ifdef FEAT_COMMENTS
5013 if (leader_len == 0 && next_leader_len == 0)
5014 {
5015 /* no comment found */
5016#endif
5017 second_indent =
5018 get_indent_lnum(curwin->w_cursor.lnum + 1);
5019#ifdef FEAT_COMMENTS
5020 }
5021 else
5022 {
5023 second_indent = next_leader_len;
5024 do_comments_list = 1;
5025 }
5026#endif
5027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005029 {
5030#ifdef FEAT_COMMENTS
5031 if (leader_len == 0 && next_leader_len == 0)
5032 {
5033 /* no comment found */
5034#endif
5035 second_indent =
5036 get_number_indent(curwin->w_cursor.lnum);
5037#ifdef FEAT_COMMENTS
5038 }
5039 else
5040 {
5041 /* get_number_indent() is now "comment aware"... */
5042 second_indent =
5043 get_number_indent(curwin->w_cursor.lnum);
5044 do_comments_list = 1;
5045 }
5046#endif
5047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049
5050 /*
5051 * When the comment leader changes, it's the end of the paragraph.
5052 */
5053 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5054#ifdef FEAT_COMMENTS
5055 || !same_leader(curwin->w_cursor.lnum,
5056 leader_len, leader_flags,
5057 next_leader_len, next_leader_flags)
5058#endif
5059 )
5060 is_end_par = TRUE;
5061
5062 /*
5063 * If we have got to the end of a paragraph, or the line is
5064 * getting long, format it.
5065 */
5066 if (is_end_par || force_format)
5067 {
5068 if (need_set_indent)
5069 /* replace indent in first line with minimal number of
5070 * tabs and spaces, according to current options */
5071 (void)set_indent(get_indent(), SIN_CHANGED);
5072
5073 /* put cursor on last non-space */
5074 State = NORMAL; /* don't go past end-of-line */
5075 coladvance((colnr_T)MAXCOL);
5076 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5077 dec_cursor();
5078
5079 /* do the formatting, without 'showmode' */
5080 State = INSERT; /* for open_line() */
5081 smd_save = p_smd;
5082 p_smd = FALSE;
5083 insertchar(NUL, INSCHAR_FORMAT
5084#ifdef FEAT_COMMENTS
5085 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005086 + (do_comments && do_comments_list
5087 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005089 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 State = old_State;
5091 p_smd = smd_save;
5092 second_indent = -1;
5093 /* at end of par.: need to set indent of next par. */
5094 need_set_indent = is_end_par;
5095 if (is_end_par)
5096 {
5097 /* When called with a negative line count, break at the
5098 * end of the paragraph. */
5099 if (line_count < 0)
5100 break;
5101 first_par_line = TRUE;
5102 }
5103 force_format = FALSE;
5104 }
5105
5106 /*
5107 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005108 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 */
5110 if (!is_end_par)
5111 {
5112 advance = FALSE;
5113 curwin->w_cursor.lnum++;
5114 curwin->w_cursor.col = 0;
5115 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005119 {
5120 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005122 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005123 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005125 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5126 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005127 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005128
5129 if (indent > 0)
5130 {
5131 (void)del_bytes(indent, FALSE, FALSE);
5132 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005133 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005134 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005137 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
5139 beep_flush();
5140 break;
5141 }
5142 first_par_line = FALSE;
5143 /* If the line is getting long, format it next time */
5144 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5145 force_format = TRUE;
5146 else
5147 force_format = FALSE;
5148 }
5149 }
5150 line_breakcheck();
5151 }
5152}
5153
5154/*
5155 * Return TRUE if line "lnum" ends in a white character.
5156 */
5157 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005158ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
5160 char_u *s = ml_get(lnum);
5161 size_t l;
5162
5163 if (*s == NUL)
5164 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005165 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 * invocation may call function multiple times". */
5167 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005168 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169}
5170
5171/*
5172 * Blank lines, and lines containing only the comment leader, are left
5173 * untouched by the formatting. The function returns TRUE in this
5174 * case. It also returns TRUE when a line starts with the end of a comment
5175 * ('e' in comment flags), so that this line is skipped, and not joined to the
5176 * previous line. A new paragraph starts after a blank line, or when the
5177 * comment leader changes -- webb.
5178 */
5179#ifdef FEAT_COMMENTS
5180 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181fmt_check_par(
5182 linenr_T lnum,
5183 int *leader_len,
5184 char_u **leader_flags,
5185 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 char_u *flags = NULL; /* init for GCC */
5188 char_u *ptr;
5189
5190 ptr = ml_get(lnum);
5191 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005192 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 else
5194 *leader_len = 0;
5195
5196 if (*leader_len > 0)
5197 {
5198 /*
5199 * Search for 'e' flag in comment leader flags.
5200 */
5201 flags = *leader_flags;
5202 while (*flags && *flags != ':' && *flags != COM_END)
5203 ++flags;
5204 }
5205
5206 return (*skipwhite(ptr + *leader_len) == NUL
5207 || (*leader_len > 0 && *flags == COM_END)
5208 || startPS(lnum, NUL, FALSE));
5209}
5210#else
5211 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005212fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
5214 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5215}
5216#endif
5217
5218/*
5219 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5220 * previous line is in the same paragraph. Used for auto-formatting.
5221 */
5222 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 char_u *p;
5226#ifdef FEAT_COMMENTS
5227 int leader_len = 0; /* leader len of current line */
5228 char_u *leader_flags = NULL; /* flags for leader of current line */
5229 int next_leader_len; /* leader len of next line */
5230 char_u *next_leader_flags; /* flags for leader of next line */
5231 int do_comments; /* format comments */
5232#endif
5233
5234 if (lnum <= 1)
5235 return TRUE; /* start of the file */
5236
5237 p = ml_get(lnum - 1);
5238 if (*p == NUL)
5239 return TRUE; /* after empty line */
5240
5241#ifdef FEAT_COMMENTS
5242 do_comments = has_format_option(FO_Q_COMS);
5243#endif
5244 if (fmt_check_par(lnum - 1
5245#ifdef FEAT_COMMENTS
5246 , &leader_len, &leader_flags, do_comments
5247#endif
5248 ))
5249 return TRUE; /* after non-paragraph line */
5250
5251 if (fmt_check_par(lnum
5252#ifdef FEAT_COMMENTS
5253 , &next_leader_len, &next_leader_flags, do_comments
5254#endif
5255 ))
5256 return TRUE; /* "lnum" is not a paragraph line */
5257
5258 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5259 return TRUE; /* missing trailing space in previous line. */
5260
5261 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5262 return TRUE; /* numbered item starts in "lnum". */
5263
5264#ifdef FEAT_COMMENTS
5265 if (!same_leader(lnum - 1, leader_len, leader_flags,
5266 next_leader_len, next_leader_flags))
5267 return TRUE; /* change of comment leader. */
5268#endif
5269
5270 return FALSE;
5271}
5272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273/*
5274 * prepare a few things for block mode yank/delete/tilde
5275 *
5276 * for delete:
5277 * - textlen includes the first/last char to be (partly) deleted
5278 * - start/endspaces is the number of columns that are taken by the
5279 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005280 * deleted.
5281 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 * - textlen includes the first/last char to be wholly yanked
5283 * - start/endspaces is the number of columns of the first/last yanked char
5284 * that are to be yanked.
5285 */
5286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005287block_prep(
5288 oparg_T *oap,
5289 struct block_def *bdp,
5290 linenr_T lnum,
5291 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293 int incr = 0;
5294 char_u *pend;
5295 char_u *pstart;
5296 char_u *line;
5297 char_u *prev_pstart;
5298 char_u *prev_pend;
5299
5300 bdp->startspaces = 0;
5301 bdp->endspaces = 0;
5302 bdp->textlen = 0;
5303 bdp->start_vcol = 0;
5304 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 bdp->is_short = FALSE;
5306 bdp->is_oneChar = FALSE;
5307 bdp->pre_whitesp = 0;
5308 bdp->pre_whitesp_c = 0;
5309 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 bdp->start_char_vcols = 0;
5311
5312 line = ml_get(lnum);
5313 pstart = line;
5314 prev_pstart = line;
5315 while (bdp->start_vcol < oap->start_vcol && *pstart)
5316 {
5317 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005318 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005320 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
5322 bdp->pre_whitesp += incr;
5323 bdp->pre_whitesp_c++;
5324 }
5325 else
5326 {
5327 bdp->pre_whitesp = 0;
5328 bdp->pre_whitesp_c = 0;
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005331 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333 bdp->start_char_vcols = incr;
5334 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5335 {
5336 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (!is_del || oap->op_type == OP_APPEND)
5339 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5340 }
5341 else
5342 {
5343 /* notice: this converts partly selected Multibyte characters to
5344 * spaces, too. */
5345 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5346 if (is_del && bdp->startspaces)
5347 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5348 pend = pstart;
5349 bdp->end_vcol = bdp->start_vcol;
5350 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 if (oap->op_type == OP_INSERT)
5354 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5355 else if (oap->op_type == OP_APPEND)
5356 {
5357 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5358 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5359 }
5360 else
5361 {
5362 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5363 if (is_del && oap->op_type != OP_LSHIFT)
5364 {
5365 /* just putting the sum of those two into
5366 * bdp->startspaces doesn't work for Visual replace,
5367 * so we have to split the tab in two */
5368 bdp->startspaces = bdp->start_char_vcols
5369 - (bdp->start_vcol - oap->start_vcol);
5370 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5371 }
5372 }
5373 }
5374 else
5375 {
5376 prev_pend = pend;
5377 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5378 {
5379 /* Count a tab for what it's worth (if list mode not on) */
5380 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005381 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 bdp->end_vcol += incr;
5383 }
5384 if (bdp->end_vcol <= oap->end_vcol
5385 && (!is_del
5386 || oap->op_type == OP_APPEND
5387 || oap->op_type == OP_REPLACE)) /* line too short */
5388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 /* Alternative: include spaces to fill up the block.
5391 * Disadvantage: can lead to trailing spaces when the line is
5392 * short where the text is put */
5393 /* if (!is_del || oap->op_type == OP_APPEND) */
5394 if (oap->op_type == OP_APPEND || virtual_op)
5395 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005396 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 else
5398 bdp->endspaces = 0; /* replace doesn't add characters */
5399 }
5400 else if (bdp->end_vcol > oap->end_vcol)
5401 {
5402 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5403 if (!is_del && bdp->endspaces)
5404 {
5405 bdp->endspaces = incr - bdp->endspaces;
5406 if (pend != pstart)
5407 pend = prev_pend;
5408 }
5409 }
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (is_del && bdp->startspaces)
5413 pstart = prev_pstart;
5414 bdp->textlen = (int)(pend - pstart);
5415 }
5416 bdp->textcol = (colnr_T) (pstart - line);
5417 bdp->textstart = pstart;
5418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005421 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005423 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005424op_addsub(
5425 oparg_T *oap,
5426 linenr_T Prenum1, /* Amount of add/subtract */
5427 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005429 pos_T pos;
5430 struct block_def bd;
5431 int change_cnt = 0;
5432 linenr_T amount = Prenum1;
5433
Bram Moolenaar6b731882018-11-16 20:54:47 +01005434 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01005435 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01005436 // the call to changed_lines().
5437#ifdef FEAT_FOLDING
5438 disable_fold_update++;
5439#endif
5440
Bram Moolenaard79e5502016-01-10 22:13:02 +01005441 if (!VIsual_active)
5442 {
5443 pos = curwin->w_cursor;
5444 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005445 {
5446#ifdef FEAT_FOLDING
5447 disable_fold_update--;
5448#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005449 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005450 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005451 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005452#ifdef FEAT_FOLDING
5453 disable_fold_update--;
5454#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005455 if (change_cnt)
5456 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5457 }
5458 else
5459 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005460 int one_change;
5461 int length;
5462 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005463
5464 if (u_save((linenr_T)(oap->start.lnum - 1),
5465 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005466 {
5467#ifdef FEAT_FOLDING
5468 disable_fold_update--;
5469#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005470 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005471 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005472
5473 pos = oap->start;
5474 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5475 {
5476 if (oap->block_mode) /* Visual block mode */
5477 {
5478 block_prep(oap, &bd, pos.lnum, FALSE);
5479 pos.col = bd.textcol;
5480 length = bd.textlen;
5481 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005482 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005483 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005484 curwin->w_cursor.col = 0;
5485 pos.col = 0;
5486 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5487 }
5488 else /* oap->motion_type == MCHAR */
5489 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005490 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005491 dec(&(oap->end));
5492 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5493 pos.col = 0;
5494 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005495 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005496 pos.col += oap->start.col;
5497 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005498 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005499 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005500 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005501 length = (int)STRLEN(ml_get(oap->end.lnum));
5502 if (oap->end.col >= length)
5503 oap->end.col = length - 1;
5504 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005505 }
5506 }
5507 one_change = do_addsub(oap->op_type, &pos, length, amount);
5508 if (one_change)
5509 {
5510 /* Remember the start position of the first change. */
5511 if (change_cnt == 0)
5512 startpos = curbuf->b_op_start;
5513 ++change_cnt;
5514 }
5515
5516#ifdef FEAT_NETBEANS_INTG
5517 if (netbeans_active() && one_change)
5518 {
5519 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5520
5521 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5522 netbeans_inserted(curbuf, pos.lnum, pos.col,
5523 &ptr[pos.col], length);
5524 }
5525#endif
5526 if (g_cmd && one_change)
5527 amount += Prenum1;
5528 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005529
5530#ifdef FEAT_FOLDING
5531 disable_fold_update--;
5532#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005533 if (change_cnt)
5534 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5535
5536 if (!change_cnt && oap->is_VIsual)
5537 /* No change: need to remove the Visual selection */
5538 redraw_curbuf_later(INVERTED);
5539
5540 /* Set '[ mark if something changed. Keep the last end
5541 * position from do_addsub(). */
5542 if (change_cnt > 0)
5543 curbuf->b_op_start = startpos;
5544
5545 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005546 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005547 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005548 }
5549}
5550
5551/*
5552 * Add or subtract 'Prenum1' from a number in a line
5553 * op_type is OP_NR_ADD or OP_NR_SUB
5554 *
5555 * Returns TRUE if some character was changed.
5556 */
5557 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005558do_addsub(
5559 int op_type,
5560 pos_T *pos,
5561 int length,
5562 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 int col;
5565 char_u *buf1;
5566 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005567 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005569 uvarnumber_T n;
5570 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 char_u *ptr;
5572 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 int todel;
5574 int dohex;
5575 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005576 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 int doalp;
5578 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005580 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005581 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005582 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005583 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005584 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005585 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005586 pos_T startpos;
5587 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
5589 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5590 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005591 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5593
Bram Moolenaard79e5502016-01-10 22:13:02 +01005594 curwin->w_cursor = *pos;
5595 ptr = ml_get(pos->lnum);
5596 col = pos->col;
5597
5598 if (*ptr == NUL)
5599 goto theend;
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 /*
5602 * First check if we are on a hexadecimal number, after the "0x".
5603 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005604 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005606 if (dobin)
5607 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005608 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005609 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005610 if (has_mbyte)
5611 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005612 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005613
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005614 if (dohex)
5615 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005616 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005617 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005618 if (has_mbyte)
5619 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005620 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005621
5622 if ( dobin
5623 && dohex
5624 && ! ((col > 0
5625 && (ptr[col] == 'X'
5626 || ptr[col] == 'x')
5627 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005628 && (!has_mbyte ||
5629 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005630 && vim_isxdigit(ptr[col + 1]))))
5631 {
5632
5633 /* In case of binary/hexadecimal pattern overlap match, rescan */
5634
Bram Moolenaard79e5502016-01-10 22:13:02 +01005635 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005636
5637 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005638 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005639 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005640 if (has_mbyte)
5641 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005642 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005643 }
5644
5645 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005646 && col > 0
5647 && (ptr[col] == 'X'
5648 || ptr[col] == 'x')
5649 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005650 && (!has_mbyte ||
5651 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005652 && vim_isxdigit(ptr[col + 1])) ||
5653 ( dobin
5654 && col > 0
5655 && (ptr[col] == 'B'
5656 || ptr[col] == 'b')
5657 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005658 && (!has_mbyte ||
5659 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005660 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005661 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005662 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005663 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005664 if (has_mbyte)
5665 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005666 }
5667 else
5668 {
5669 /*
5670 * Search forward and then backward to find the start of number.
5671 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005672 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005673
5674 while (ptr[col] != NUL
5675 && !vim_isdigit(ptr[col])
5676 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005677 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005678
5679 while (col > 0
5680 && vim_isdigit(ptr[col - 1])
5681 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005682 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005683 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005684 if (has_mbyte)
5685 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005686 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005687 }
5688 }
5689
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005690 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005691 {
5692 while (ptr[col] != NUL && length > 0
5693 && !vim_isdigit(ptr[col])
5694 && !(doalp && ASCII_ISALPHA(ptr[col])))
5695 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005696 int mb_len = MB_PTR2LEN(ptr + col);
5697
5698 col += mb_len;
5699 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005700 }
5701
5702 if (length == 0)
5703 goto theend;
5704
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005705 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005706 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005707 {
5708 negative = TRUE;
5709 was_positive = FALSE;
5710 }
5711 }
5712
5713 /*
5714 * If a number was found, and saving for undo works, replace the number.
5715 */
5716 firstdigit = ptr[col];
5717 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5718 {
5719 beep_flush();
5720 goto theend;
5721 }
5722
5723 if (doalp && ASCII_ISALPHA(firstdigit))
5724 {
5725 /* decrement or increment alphabetic character */
5726 if (op_type == OP_NR_SUB)
5727 {
5728 if (CharOrd(firstdigit) < Prenum1)
5729 {
5730 if (isupper(firstdigit))
5731 firstdigit = 'A';
5732 else
5733 firstdigit = 'a';
5734 }
5735 else
5736#ifdef EBCDIC
5737 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5738#else
5739 firstdigit -= Prenum1;
5740#endif
5741 }
5742 else
5743 {
5744 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5745 {
5746 if (isupper(firstdigit))
5747 firstdigit = 'Z';
5748 else
5749 firstdigit = 'z';
5750 }
5751 else
5752#ifdef EBCDIC
5753 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5754#else
5755 firstdigit += Prenum1;
5756#endif
5757 }
5758 curwin->w_cursor.col = col;
5759 if (!did_change)
5760 startpos = curwin->w_cursor;
5761 did_change = TRUE;
5762 (void)del_char(FALSE);
5763 ins_char(firstdigit);
5764 endpos = curwin->w_cursor;
5765 curwin->w_cursor.col = col;
5766 }
5767 else
5768 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005769 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005770 && (!has_mbyte ||
5771 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005772 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005773 {
5774 /* negative number */
5775 --col;
5776 negative = TRUE;
5777 }
5778 /* get the number value (unsigned) */
5779 if (visual && VIsual_mode != 'V')
5780 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5781 ? (int)STRLEN(ptr) - col
5782 : length);
5783
5784 vim_str2nr(ptr + col, &pre, &length,
5785 0 + (dobin ? STR2NR_BIN : 0)
5786 + (dooct ? STR2NR_OCT : 0)
5787 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005788 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005789
5790 /* ignore leading '-' for hex and octal and bin numbers */
5791 if (pre && negative)
5792 {
5793 ++col;
5794 --length;
5795 negative = FALSE;
5796 }
5797 /* add or subtract */
5798 subtract = FALSE;
5799 if (op_type == OP_NR_SUB)
5800 subtract ^= TRUE;
5801 if (negative)
5802 subtract ^= TRUE;
5803
5804 oldn = n;
5805 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005806 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005807 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005808 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005809 /* handle wraparound for decimal numbers */
5810 if (!pre)
5811 {
5812 if (subtract)
5813 {
5814 if (n > oldn)
5815 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005816 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005817 negative ^= TRUE;
5818 }
5819 }
5820 else
5821 {
5822 /* add */
5823 if (n < oldn)
5824 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005825 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005826 negative ^= TRUE;
5827 }
5828 }
5829 if (n == 0)
5830 negative = FALSE;
5831 }
5832
5833 if (visual && !was_positive && !negative && col > 0)
5834 {
5835 /* need to remove the '-' */
5836 col--;
5837 length++;
5838 }
5839
5840 /*
5841 * Delete the old number.
5842 */
5843 curwin->w_cursor.col = col;
5844 if (!did_change)
5845 startpos = curwin->w_cursor;
5846 did_change = TRUE;
5847 todel = length;
5848 c = gchar_cursor();
5849 /*
5850 * Don't include the '-' in the length, only the length of the
5851 * part after it is kept the same.
5852 */
5853 if (c == '-')
5854 --length;
5855 while (todel-- > 0)
5856 {
5857 if (c < 0x100 && isalpha(c))
5858 {
5859 if (isupper(c))
5860 hexupper = TRUE;
5861 else
5862 hexupper = FALSE;
5863 }
5864 /* del_char() will mark line needing displaying */
5865 (void)del_char(FALSE);
5866 c = gchar_cursor();
5867 }
5868
5869 /*
5870 * Prepare the leading characters in buf1[].
5871 * When there are many leading zeros it could be very long.
5872 * Allocate a bit too much.
5873 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005874 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005875 if (buf1 == NULL)
5876 goto theend;
5877 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005878 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005879 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01005880 if (pre)
5881 {
5882 *ptr++ = '0';
5883 --length;
5884 }
5885 if (pre == 'b' || pre == 'B' ||
5886 pre == 'x' || pre == 'X')
5887 {
5888 *ptr++ = pre;
5889 --length;
5890 }
5891
5892 /*
5893 * Put the number characters in buf2[].
5894 */
5895 if (pre == 'b' || pre == 'B')
5896 {
5897 int i;
5898 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005899 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005900
5901 /* leading zeros */
5902 for (bit = bits; bit > 0; bit--)
5903 if ((n >> (bit - 1)) & 0x1) break;
5904
5905 for (i = 0; bit > 0; bit--)
5906 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5907
5908 buf2[i] = '\0';
5909 }
5910 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02005911 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005912 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005913 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02005914 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005915 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005916 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02005917 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005918 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005919 else
Bram Moolenaarea391762018-04-08 13:07:22 +02005920 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005921 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005922 length -= (int)STRLEN(buf2);
5923
5924 /*
5925 * Adjust number of zeros to the new number of digits, so the
5926 * total length of the number remains the same.
5927 * Don't do this when
5928 * the result may look like an octal number.
5929 */
5930 if (firstdigit == '0' && !(dooct && pre == 0))
5931 while (length-- > 0)
5932 *ptr++ = '0';
5933 *ptr = NUL;
5934 STRCAT(buf1, buf2);
5935 ins_str(buf1); /* insert the new number */
5936 vim_free(buf1);
5937 endpos = curwin->w_cursor;
5938 if (did_change && curwin->w_cursor.col)
5939 --curwin->w_cursor.col;
5940 }
5941
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005942 if (did_change)
5943 {
5944 /* set the '[ and '] marks */
5945 curbuf->b_op_start = startpos;
5946 curbuf->b_op_end = endpos;
5947 if (curbuf->b_op_end.col > 0)
5948 --curbuf->b_op_end.col;
5949 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005950
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005951theend:
5952 if (visual)
5953 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005954 else if (did_change)
5955 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005956
Bram Moolenaard79e5502016-01-10 22:13:02 +01005957 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958}
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5961/*
5962 * SELECTION / PRIMARY ('*')
5963 *
5964 * Text selection stuff that uses the GUI selection register '*'. When using a
5965 * GUI this may be text from another window, otherwise it is the last text we
5966 * had highlighted with VIsual mode. With mouse support, clicking the middle
5967 * button performs the paste, otherwise you will need to do <"*p>. "
5968 * If not under X, it is synonymous with the clipboard register '+'.
5969 *
5970 * X CLIPBOARD ('+')
5971 *
5972 * Text selection stuff that uses the GUI clipboard register '+'.
5973 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5974 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5975 * otherwise you will need to do <"+p>. "
5976 * If not under X, it is synonymous with the selection register '*'.
5977 */
5978
5979/*
5980 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01005981 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 * only exist while the owning application exists, so we write to the
5983 * permanent (while X runs) store CUT_BUFFER0.
5984 * Dump the CLIPBOARD selection if we own it (it's logically the more
5985 * 'permanent' of the two), otherwise the PRIMARY one.
5986 * For now, use a hard-coded sanity limit of 1Mb of data.
5987 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02005988#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005990x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 Display *dpy;
5993 char_u *str = NULL;
5994 long_u len = 0;
5995 int motion_type = -1;
5996
5997# ifdef FEAT_GUI
5998 if (gui.in_use)
5999 dpy = X_DISPLAY;
6000 else
6001# endif
6002# ifdef FEAT_XCLIPBOARD
6003 dpy = xterm_dpy;
6004# else
6005 return;
6006# endif
6007
6008 /* Get selection to export */
6009 if (clip_plus.owned)
6010 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6011 else if (clip_star.owned)
6012 motion_type = clip_convert_selection(&str, &len, &clip_star);
6013
6014 /* Check it's OK */
6015 if (dpy != NULL && str != NULL && motion_type >= 0
6016 && len < 1024*1024 && len > 0)
6017 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006018 int ok = TRUE;
6019
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006020 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6021 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6022 * encoding conversion usually doesn't work, so keep the text as-is.
6023 */
6024 if (has_mbyte)
6025 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006026 vimconv_T vc;
6027
6028 vc.vc_type = CONV_NONE;
6029 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6030 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006031 int intlen = len;
6032 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006033
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006034 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006035 conv_str = string_convert(&vc, str, &intlen);
6036 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006037 if (conv_str != NULL)
6038 {
6039 vim_free(str);
6040 str = conv_str;
6041 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006042 else
6043 {
6044 ok = FALSE;
6045 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006046 convert_setup(&vc, NULL, NULL);
6047 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006048 else
6049 {
6050 ok = FALSE;
6051 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006052 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006053
6054 /* Do not store the string if conversion failed. Better to use any
6055 * other selection than garbled text. */
6056 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006057 {
6058 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6059 XFlush(dpy);
6060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 }
6062
6063 vim_free(str);
6064}
6065#endif
6066
6067 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006068clip_free_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006070 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072 if (cbd == &clip_plus)
6073 y_current = &y_regs[PLUS_REGISTER];
6074 else
6075 y_current = &y_regs[STAR_REGISTER];
6076 free_yank_all();
6077 y_current->y_size = 0;
6078 y_current = y_ptr;
6079}
6080
6081/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006082 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 */
6084 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006085clip_get_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006087 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 pos_T old_visual;
6090 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 colnr_T old_curswant;
6092 int old_set_curswant;
6093 pos_T old_op_start, old_op_end;
6094 oparg_T oa;
6095 cmdarg_T ca;
6096
6097 if (cbd->owned)
6098 {
6099 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6100 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6101 return;
6102
6103 /* Get the text between clip_star.start & clip_star.end */
6104 old_y_previous = y_previous;
6105 old_y_current = y_current;
6106 old_cursor = curwin->w_cursor;
6107 old_curswant = curwin->w_curswant;
6108 old_set_curswant = curwin->w_set_curswant;
6109 old_op_start = curbuf->b_op_start;
6110 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 old_visual = VIsual;
6112 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 clear_oparg(&oa);
6114 oa.regname = (cbd == &clip_plus ? '+' : '*');
6115 oa.op_type = OP_YANK;
6116 vim_memset(&ca, 0, sizeof(ca));
6117 ca.oap = &oa;
6118 ca.cmdchar = 'y';
6119 ca.count1 = 1;
6120 ca.retval = CA_NO_ADJ_OP_END;
6121 do_pending_operator(&ca, 0, TRUE);
6122 y_previous = old_y_previous;
6123 y_current = old_y_current;
6124 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006125 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 curwin->w_curswant = old_curswant;
6127 curwin->w_set_curswant = old_set_curswant;
6128 curbuf->b_op_start = old_op_start;
6129 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 VIsual = old_visual;
6131 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006133 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
6135 clip_free_selection(cbd);
6136
6137 /* Try to get selected text from another window */
6138 clip_gen_request_selection(cbd);
6139 }
6140}
6141
Bram Moolenaard44347f2011-06-19 01:14:29 +02006142/*
6143 * Convert from the GUI selection string into the '*'/'+' register.
6144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006146clip_yank_selection(
6147 int type,
6148 char_u *str,
6149 long len,
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006150 Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006152 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154 if (cbd == &clip_plus)
6155 y_ptr = &y_regs[PLUS_REGISTER];
6156 else
6157 y_ptr = &y_regs[STAR_REGISTER];
6158
6159 clip_free_selection(cbd);
6160
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006161 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162}
6163
6164/*
6165 * Convert the '*'/'+' register into a GUI selection string returned in *str
6166 * with length *len.
6167 * Returns the motion type, or -1 for failure.
6168 */
6169 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006170clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 char_u *p;
6173 int lnum;
6174 int i, j;
6175 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006176 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 if (cbd == &clip_plus)
6179 y_ptr = &y_regs[PLUS_REGISTER];
6180 else
6181 y_ptr = &y_regs[STAR_REGISTER];
6182
6183#ifdef USE_CRNL
6184 eolsize = 2;
6185#else
6186 eolsize = 1;
6187#endif
6188
6189 *str = NULL;
6190 *len = 0;
6191 if (y_ptr->y_array == NULL)
6192 return -1;
6193
6194 for (i = 0; i < y_ptr->y_size; i++)
6195 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6196
6197 /*
6198 * Don't want newline character at end of last line if we're in MCHAR mode.
6199 */
6200 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6201 *len -= eolsize;
6202
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006203 p = *str = alloc(*len + 1); // add one to avoid zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (p == NULL)
6205 return -1;
6206 lnum = 0;
6207 for (i = 0, j = 0; i < (int)*len; i++, j++)
6208 {
6209 if (y_ptr->y_array[lnum][j] == '\n')
6210 p[i] = NUL;
6211 else if (y_ptr->y_array[lnum][j] == NUL)
6212 {
6213#ifdef USE_CRNL
6214 p[i++] = '\r';
6215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 p[i] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 lnum++;
6218 j = -1;
6219 }
6220 else
6221 p[i] = y_ptr->y_array[lnum][j];
6222 }
6223 return y_ptr->y_type;
6224}
6225
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227/*
6228 * If we have written to a clipboard register, send the text to the clipboard.
6229 */
6230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006231may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
6233 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6234 {
6235 clip_own_selection(&clip_star);
6236 clip_gen_set_selection(&clip_star);
6237 }
6238 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6239 {
6240 clip_own_selection(&clip_plus);
6241 clip_gen_set_selection(&clip_plus);
6242 }
6243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245#endif /* FEAT_CLIPBOARD || PROTO */
6246
6247
6248#if defined(FEAT_DND) || defined(PROTO)
6249/*
6250 * Replace the contents of the '~' register with str.
6251 */
6252 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006253dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006255 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257 curr = y_current;
6258 y_current = &y_regs[TILDE_REGISTER];
6259 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006260 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 y_current = curr;
6262}
6263#endif
6264
6265
6266#if defined(FEAT_EVAL) || defined(PROTO)
6267/*
6268 * Return the type of a register.
6269 * Used for getregtype()
6270 * Returns MAUTO for error.
6271 */
6272 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006273get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 switch (regname)
6276 {
6277 case '%': /* file name */
6278 case '#': /* alternate file name */
6279 case '=': /* expression */
6280 case ':': /* last command line */
6281 case '/': /* last search-pattern */
6282 case '.': /* last inserted text */
6283#ifdef FEAT_SEARCHPATH
6284 case Ctrl_F: /* Filename under cursor */
6285 case Ctrl_P: /* Path under cursor, expand via "path" */
6286#endif
6287 case Ctrl_W: /* word under cursor */
6288 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6289 case '_': /* black hole: always empty */
6290 return MCHAR;
6291 }
6292
6293#ifdef FEAT_CLIPBOARD
6294 regname = may_get_selection(regname);
6295#endif
6296
Bram Moolenaar32b92012014-01-14 12:33:36 +01006297 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006298 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 get_yank_register(regname, FALSE);
6301
6302 if (y_current->y_array != NULL)
6303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 if (reglen != NULL && y_current->y_type == MBLOCK)
6305 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 return y_current->y_type;
6307 }
6308 return MAUTO;
6309}
6310
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006311/*
6312 * When "flags" has GREG_LIST return a list with text "s".
6313 * Otherwise just return "s".
6314 */
6315 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006316getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006317{
6318 if (flags & GREG_LIST)
6319 {
6320 list_T *list = list_alloc();
6321
6322 if (list != NULL)
6323 {
6324 if (list_append_string(list, NULL, -1) == FAIL)
6325 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006326 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006327 return NULL;
6328 }
6329 list->lv_first->li_tv.vval.v_string = s;
6330 }
6331 return (char_u *)list;
6332 }
6333 return s;
6334}
6335
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336/*
6337 * Return the contents of a register as a single allocated string.
6338 * Used for "@r" in expressions and for getreg().
6339 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006340 * Flags:
6341 * GREG_NO_EXPR Do not allow expression register
6342 * GREG_EXPR_SRC For the expression register: return expression itself,
6343 * not the result of its evaluation.
6344 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 */
6346 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006347get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
6349 long i;
6350 char_u *retval;
6351 int allocated;
6352 long len;
6353
6354 /* Don't allow using an expression register inside an expression */
6355 if (regname == '=')
6356 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006357 if (flags & GREG_NO_EXPR)
6358 return NULL;
6359 if (flags & GREG_EXPR_SRC)
6360 return getreg_wrap_one_line(get_expr_line_src(), flags);
6361 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363
6364 if (regname == '@') /* "@@" is used for unnamed register */
6365 regname = '"';
6366
6367 /* check for valid regname */
6368 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6369 return NULL;
6370
6371#ifdef FEAT_CLIPBOARD
6372 regname = may_get_selection(regname);
6373#endif
6374
6375 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6376 {
6377 if (retval == NULL)
6378 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006379 if (allocated)
6380 return getreg_wrap_one_line(retval, flags);
6381 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 }
6383
6384 get_yank_register(regname, FALSE);
6385 if (y_current->y_array == NULL)
6386 return NULL;
6387
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006388 if (flags & GREG_LIST)
6389 {
6390 list_T *list = list_alloc();
6391 int error = FALSE;
6392
6393 if (list == NULL)
6394 return NULL;
6395 for (i = 0; i < y_current->y_size; ++i)
6396 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6397 error = TRUE;
6398 if (error)
6399 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006400 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006401 return NULL;
6402 }
6403 return (char_u *)list;
6404 }
6405
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 /*
6407 * Compute length of resulting string.
6408 */
6409 len = 0;
6410 for (i = 0; i < y_current->y_size; ++i)
6411 {
6412 len += (long)STRLEN(y_current->y_array[i]);
6413 /*
6414 * Insert a newline between lines and after last line if
6415 * y_type is MLINE.
6416 */
6417 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6418 ++len;
6419 }
6420
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006421 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
6423 /*
6424 * Copy the lines of the yank register into the string.
6425 */
6426 if (retval != NULL)
6427 {
6428 len = 0;
6429 for (i = 0; i < y_current->y_size; ++i)
6430 {
6431 STRCPY(retval + len, y_current->y_array[i]);
6432 len += (long)STRLEN(retval + len);
6433
6434 /*
6435 * Insert a NL between lines and after the last line if y_type is
6436 * MLINE.
6437 */
6438 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6439 retval[len++] = '\n';
6440 }
6441 retval[len] = NUL;
6442 }
6443
6444 return retval;
6445}
6446
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006447 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006448init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006449 int name,
6450 yankreg_T **old_y_previous,
6451 yankreg_T **old_y_current,
6452 int must_append,
6453 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006454{
6455 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6456 {
6457 emsg_invreg(name);
6458 return FAIL;
6459 }
6460
6461 /* Don't want to change the current (unnamed) register */
6462 *old_y_previous = y_previous;
6463 *old_y_current = y_current;
6464
6465 get_yank_register(name, TRUE);
6466 if (!y_append && !must_append)
6467 free_yank_all();
6468 return OK;
6469}
6470
6471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006473 int name,
6474 yankreg_T *old_y_previous,
6475 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006476{
6477# ifdef FEAT_CLIPBOARD
6478 /* Send text of clipboard register to the clipboard. */
6479 may_set_selection();
6480# endif
6481
6482 /* ':let @" = "val"' should change the meaning of the "" register */
6483 if (name != '"')
6484 y_previous = old_y_previous;
6485 y_current = old_y_current;
6486}
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
6489 * Store string "str" in register "name".
6490 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6491 * If "must_append" is TRUE, always append to the register. Otherwise append
6492 * if "name" is an uppercase letter.
6493 * Note: "maxlen" and "must_append" don't work for the "/" register.
6494 * Careful: 'str' is modified, you may have to use a copy!
6495 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6496 */
6497 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006498write_reg_contents(
6499 int name,
6500 char_u *str,
6501 int maxlen,
6502 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6505}
6506
6507 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006508write_reg_contents_lst(
6509 int name,
6510 char_u **strings,
6511 int maxlen UNUSED,
6512 int must_append,
6513 int yank_type,
6514 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006515{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006516 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006517
6518 if (name == '/'
6519#ifdef FEAT_EVAL
6520 || name == '='
6521#endif
6522 )
6523 {
6524 char_u *s;
6525
6526 if (strings[0] == NULL)
6527 s = (char_u *)"";
6528 else if (strings[1] != NULL)
6529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006530 emsg(_("E883: search pattern and expression register may not "
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006531 "contain two or more lines"));
6532 return;
6533 }
6534 else
6535 s = strings[0];
6536 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6537 return;
6538 }
6539
6540 if (name == '_') /* black hole: nothing to do */
6541 return;
6542
6543 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6544 &yank_type) == FAIL)
6545 return;
6546
6547 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6548
6549 finish_write_reg(name, old_y_previous, old_y_current);
6550}
6551
6552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006553write_reg_contents_ex(
6554 int name,
6555 char_u *str,
6556 int maxlen,
6557 int must_append,
6558 int yank_type,
6559 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006561 yankreg_T *old_y_previous, *old_y_current;
6562 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaare7566042005-06-17 22:00:15 +00006564 if (maxlen >= 0)
6565 len = maxlen;
6566 else
6567 len = (long)STRLEN(str);
6568
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 /* Special case: '/' search pattern */
6570 if (name == '/')
6571 {
6572 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6573 return;
6574 }
6575
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006576 if (name == '#')
6577 {
6578 buf_T *buf;
6579
6580 if (VIM_ISDIGIT(*str))
6581 {
6582 int num = atoi((char *)str);
6583
6584 buf = buflist_findnr(num);
6585 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006586 semsg(_(e_nobufnr), (long)num);
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006587 }
6588 else
6589 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6590 TRUE, FALSE, FALSE));
6591 if (buf == NULL)
6592 return;
6593 curwin->w_alt_fnum = buf->b_fnum;
6594 return;
6595 }
6596
Bram Moolenaare7566042005-06-17 22:00:15 +00006597#ifdef FEAT_EVAL
6598 if (name == '=')
6599 {
6600 char_u *p, *s;
6601
6602 p = vim_strnsave(str, (int)len);
6603 if (p == NULL)
6604 return;
6605 if (must_append)
6606 {
6607 s = concat_str(get_expr_line_src(), p);
6608 vim_free(p);
6609 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006610 }
6611 set_expr_line(p);
6612 return;
6613 }
6614#endif
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (name == '_') /* black hole: nothing to do */
6617 return;
6618
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006619 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6620 &yank_type) == FAIL)
6621 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006623 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006625 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626}
6627#endif /* FEAT_EVAL */
6628
6629#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6630/*
6631 * Put a string into a register. When the register is not empty, the string
6632 * is appended.
6633 */
6634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006635str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006636 yankreg_T *y_ptr, /* pointer to yank register */
6637 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6638 char_u *str, /* string to put in register */
6639 long len, /* length of string */
6640 long blocklen, /* width of Visual block */
6641 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006643 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 int lnum;
6645 long start;
6646 long i;
6647 int extra;
6648 int newlines; /* number of lines added */
6649 int extraline = 0; /* extra line at the end */
6650 int append = FALSE; /* append to last line in register */
6651 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006652 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006656 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 y_ptr->y_size = 0;
6658
Bram Moolenaard44347f2011-06-19 01:14:29 +02006659 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006660 type = ((str_list || (len > 0 && (str[len - 1] == NL
6661 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006662 ? MLINE : MCHAR);
6663 else
6664 type = yank_type;
6665
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 /*
6667 * Count the number of lines within the string
6668 */
6669 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006670 if (str_list)
6671 {
6672 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006677 for (i = 0; i < len; i++)
6678 if (str[i] == '\n')
6679 ++newlines;
6680 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6681 {
6682 extraline = 1;
6683 ++newlines; /* count extra newline at the end */
6684 }
6685 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6686 {
6687 append = TRUE;
6688 --newlines; /* uncount newline when appending first line */
6689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006692 /* Without any lines make the register empty. */
6693 if (y_ptr->y_size + newlines == 0)
6694 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006695 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006696 return;
6697 }
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 /*
6700 * Allocate an array to hold the pointers to the new register lines.
6701 * If the register was not empty, move the existing lines to the new array.
6702 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006703 pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 if (pp == NULL) /* out of memory */
6705 return;
6706 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6707 pp[lnum] = y_ptr->y_array[lnum];
6708 vim_free(y_ptr->y_array);
6709 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712 /*
6713 * Find the end of each line and save it into the array.
6714 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006715 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006717 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6718 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006719 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006720 pp[lnum] = vim_strnsave(*ss, i);
6721 if (i > maxlen)
6722 maxlen = i;
6723 }
6724 }
6725 else
6726 {
6727 for (start = 0; start < len + extraline; start += i + 1)
6728 {
6729 for (i = start; i < len; ++i) /* find the end of the line */
6730 if (str[i] == '\n')
6731 break;
6732 i -= start; /* i is now length of line */
6733 if (i > maxlen)
6734 maxlen = i;
6735 if (append)
6736 {
6737 --lnum;
6738 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6739 }
6740 else
6741 extra = 0;
Bram Moolenaar964b3742019-05-24 18:54:09 +02006742 s = alloc(i + extra + 1);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006743 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006745 if (extra)
6746 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6747 if (append)
6748 vim_free(y_ptr->y_array[lnum]);
6749 if (i)
6750 mch_memmove(s + extra, str + start, (size_t)i);
6751 extra += i;
6752 s[extra] = NUL;
6753 y_ptr->y_array[lnum++] = s;
6754 while (--extra >= 0)
6755 {
6756 if (*s == NUL)
6757 *s = '\n'; /* replace NUL with newline */
6758 ++s;
6759 }
6760 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
6763 y_ptr->y_type = type;
6764 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (type == MBLOCK)
6766 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6767 else
6768 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006769#ifdef FEAT_VIMINFO
6770 y_ptr->y_time_set = vim_time();
6771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772}
6773#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6774
6775 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006776clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 vim_memset(oap, 0, sizeof(oparg_T));
6779}
6780
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006782 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 *
6784 * "Words" are counted by looking for boundaries between non-space and
6785 * space characters. (it seems to produce results that match 'wc'.)
6786 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006787 * Return value is byte count; word count for the line is added to "*wc".
6788 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 *
6790 * The function will only examine the first "limit" characters in the
6791 * line, stopping if it encounters an end-of-line (NUL byte). In that
6792 * case, eol_size will be added to the character count to account for
6793 * the size of the EOL character.
6794 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006795 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006796line_count_info(
6797 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006798 varnumber_T *wc,
6799 varnumber_T *cc,
6800 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01006801 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006803 varnumber_T i;
6804 varnumber_T words = 0;
6805 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 int is_word = 0;
6807
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006808 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
6810 if (is_word)
6811 {
6812 if (vim_isspace(line[i]))
6813 {
6814 words++;
6815 is_word = 0;
6816 }
6817 }
6818 else if (!vim_isspace(line[i]))
6819 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006820 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006821 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823
6824 if (is_word)
6825 words++;
6826 *wc += words;
6827
6828 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006829 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006832 chars += eol_size;
6833 }
6834 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 return i;
6836}
6837
6838/*
6839 * Give some info about the position of the cursor (for "g CTRL-G").
6840 * In Visual mode, give some info about the selected region. (In this case,
6841 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01006842 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 */
6844 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006845cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006848 char_u buf1[50];
6849 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006851 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006852 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006853 varnumber_T byte_count_cursor = 0;
6854 varnumber_T char_count = 0;
6855 varnumber_T char_count_cursor = 0;
6856 varnumber_T word_count = 0;
6857 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006858 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006859 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 long line_count_selected = 0;
6861 pos_T min_pos, max_pos;
6862 oparg_T oparg;
6863 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865 /*
6866 * Compute the length of the file in characters.
6867 */
6868 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6869 {
Bram Moolenaared767a22016-01-03 22:49:16 +01006870 if (dict == NULL)
6871 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006872 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01006873 return;
6874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876 else
6877 {
6878 if (get_fileformat(curbuf) == EOL_DOS)
6879 eol_size = 2;
6880 else
6881 eol_size = 1;
6882
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (VIsual_active)
6884 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006885 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 min_pos = VIsual;
6888 max_pos = curwin->w_cursor;
6889 }
6890 else
6891 {
6892 min_pos = curwin->w_cursor;
6893 max_pos = VIsual;
6894 }
6895 if (*p_sel == 'e' && max_pos.col > 0)
6896 --max_pos.col;
6897
6898 if (VIsual_mode == Ctrl_V)
6899 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006900#ifdef FEAT_LINEBREAK
6901 char_u * saved_sbr = p_sbr;
6902
6903 /* Make 'sbr' empty for a moment to get the correct size. */
6904 p_sbr = empty_option;
6905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 oparg.is_VIsual = 1;
6907 oparg.block_mode = TRUE;
6908 oparg.op_type = OP_NOP;
6909 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006910 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006911#ifdef FEAT_LINEBREAK
6912 p_sbr = saved_sbr;
6913#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006914 if (curwin->w_curswant == MAXCOL)
6915 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 /* Swap the start, end vcol if needed */
6917 if (oparg.end_vcol < oparg.start_vcol)
6918 {
6919 oparg.end_vcol += oparg.start_vcol;
6920 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6921 oparg.end_vcol -= oparg.start_vcol;
6922 }
6923 }
6924 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6928 {
6929 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006930 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
6932 ui_breakcheck();
6933 if (got_int)
6934 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006935 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
6937
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 /* Do extra processing for VIsual mode. */
6939 if (VIsual_active
6940 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6941 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006942 char_u *s = NULL;
6943 long len = 0L;
6944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 switch (VIsual_mode)
6946 {
6947 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006951 s = bd.textstart;
6952 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 break;
6954 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006955 s = ml_get(lnum);
6956 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 break;
6958 case 'v':
6959 {
6960 colnr_T start_col = (lnum == min_pos.lnum)
6961 ? min_pos.col : 0;
6962 colnr_T end_col = (lnum == max_pos.lnum)
6963 ? max_pos.col - start_col + 1 : MAXCOL;
6964
Bram Moolenaardef9e822004-12-31 20:58:58 +00006965 s = ml_get(lnum) + start_col;
6966 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
6968 break;
6969 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006970 if (s != NULL)
6971 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006972 byte_count_cursor += line_count_info(s, &word_count_cursor,
6973 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006974 if (lnum == curbuf->b_ml.ml_line_count
6975 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006976 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006977 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006978 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983 /* In non-visual mode, check for the line the cursor is on */
6984 if (lnum == curwin->w_cursor.lnum)
6985 {
6986 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006987 char_count_cursor += char_count;
6988 byte_count_cursor = byte_count +
6989 line_count_info(ml_get(lnum),
6990 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006991 (varnumber_T)(curwin->w_cursor.col + 1),
6992 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 }
6994 }
6995 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006996 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006997 &char_count, (varnumber_T)MAXCOL,
6998 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 }
7000
7001 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007002 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007003 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
Bram Moolenaared767a22016-01-03 22:49:16 +01007005 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007007 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007009 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7010 {
7011 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7012 &max_pos.col);
7013 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7014 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7015 }
7016 else
7017 buf1[0] = NUL;
7018
7019 if (char_count_cursor == byte_count_cursor
7020 && char_count == byte_count)
7021 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007022 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007023 buf1, line_count_selected,
7024 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007025 (long_long_T)word_count_cursor,
7026 (long_long_T)word_count,
7027 (long_long_T)byte_count_cursor,
7028 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007029 else
7030 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007031 _("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 +01007032 buf1, line_count_selected,
7033 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007034 (long_long_T)word_count_cursor,
7035 (long_long_T)word_count,
7036 (long_long_T)char_count_cursor,
7037 (long_long_T)char_count,
7038 (long_long_T)byte_count_cursor,
7039 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007042 {
7043 p = ml_get_curline();
7044 validate_virtcol();
7045 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7046 (int)curwin->w_virtcol + 1);
7047 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7048 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
Bram Moolenaared767a22016-01-03 22:49:16 +01007050 if (char_count_cursor == byte_count_cursor
7051 && char_count == byte_count)
7052 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007053 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007054 (char *)buf1, (char *)buf2,
7055 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007057 (long_long_T)word_count_cursor, (long_long_T)word_count,
7058 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007059 else
7060 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007061 _("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 +01007062 (char *)buf1, (char *)buf2,
7063 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007064 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007065 (long_long_T)word_count_cursor, (long_long_T)word_count,
7066 (long_long_T)char_count_cursor, (long_long_T)char_count,
7067 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007068 }
7069 }
7070
Bram Moolenaared767a22016-01-03 22:49:16 +01007071 bom_count = bomb_size();
7072 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007073 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007074 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007075 if (dict == NULL)
7076 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007077 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007078 p = p_shm;
7079 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01007080 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01007081 p_shm = p;
7082 }
7083 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007084#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007085 if (dict != NULL)
7086 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007087 dict_add_number(dict, "words", word_count);
7088 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007089 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007090 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7091 byte_count_cursor);
7092 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7093 char_count_cursor);
7094 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7095 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098}