blob: 29ade3f197ce2694d1e0b8830b74595658503726 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +010068static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
70static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020072static 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 +000073#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010074static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010076static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010078static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
80
Bram Moolenaarf2732452018-06-03 14:47:35 +020081// Flags for third item in "opchars".
82#define OPF_LINES 1 // operator always works on lines
83#define OPF_CHANGE 2 // operator changes text
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085/*
86 * The names of operators.
87 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020088 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 */
90static char opchars[][3] =
91{
Bram Moolenaarf2732452018-06-03 14:47:35 +020092 {NUL, NUL, 0}, // OP_NOP
93 {'d', NUL, OPF_CHANGE}, // OP_DELETE
94 {'y', NUL, 0}, // OP_YANK
95 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
96 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
97 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
98 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
99 {'g', '~', OPF_CHANGE}, // OP_TILDE
100 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
101 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
102 {':', NUL, OPF_LINES}, // OP_COLON
103 {'g', 'U', OPF_CHANGE}, // OP_UPPER
104 {'g', 'u', OPF_CHANGE}, // OP_LOWER
105 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
106 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
107 {'g', '?', OPF_CHANGE}, // OP_ROT13
108 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
109 {'I', NUL, OPF_CHANGE}, // OP_INSERT
110 {'A', NUL, OPF_CHANGE}, // OP_APPEND
111 {'z', 'f', OPF_LINES}, // OP_FOLD
112 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
113 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
114 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
115 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
116 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
117 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
118 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
119 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
120 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
121 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122};
123
Bram Moolenaarc3328162019-07-23 22:15:25 +0200124 yankreg_T *
125get_y_regs(void)
126{
127 return y_regs;
128}
129
130 yankreg_T *
131get_y_current(void)
132{
133 return y_current;
134}
135
136 yankreg_T *
137get_y_previous(void)
138{
139 return y_previous;
140}
141
142 void
143set_y_previous(yankreg_T *yreg)
144{
145 y_previous = yreg;
146}
147
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149/*
150 * Translate a command name into an operator type.
151 * Must only be called with a valid operator name!
152 */
153 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100154get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
156 int i;
157
158 if (char1 == 'r') /* ignore second character */
159 return OP_REPLACE;
160 if (char1 == '~') /* when tilde is an operator */
161 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100162 if (char1 == 'g' && char2 == Ctrl_A) /* add */
163 return OP_NR_ADD;
164 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
165 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (opchars[i][0] == char1 && opchars[i][1] == char2)
169 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100170 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
171 {
172 internal_error("get_op_type()");
173 break;
174 }
175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 return i;
177}
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Return TRUE if operator "op" always works on whole lines.
181 */
182 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100183op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200185 return opchars[op][2] & OPF_LINES;
186}
187
Bram Moolenaar113e1072019-01-20 15:30:40 +0100188#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200189/*
190 * Return TRUE if operator "op" changes text.
191 */
192 int
193op_is_change(int op)
194{
195 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199/*
200 * Get first operator command character.
201 * Returns 'g' or 'z' if there is another command character.
202 */
203 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100204get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
206 return opchars[optype][0];
207}
208
209/*
210 * Get second operator command character.
211 */
212 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100213get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214{
215 return opchars[optype][1];
216}
217
218/*
219 * op_shift - handle a shift operation
220 */
221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100222op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 long i;
225 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228 if (u_save((linenr_T)(oap->start.lnum - 1),
229 (linenr_T)(oap->end.lnum + 1)) == FAIL)
230 return;
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 if (oap->block_mode)
233 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
235 for (i = oap->line_count; --i >= 0; )
236 {
237 first_char = *ml_get_curline();
238 if (first_char == NUL) /* empty line */
239 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 else if (oap->block_mode)
241 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 else
243 /* Move the line right if it doesn't start with '#', 'smartindent'
244 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
245#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
246 if (first_char != '#' || !preprocs_left())
247#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000248 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 ++curwin->w_cursor.lnum;
250 }
251
252 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 if (oap->block_mode)
254 {
255 curwin->w_cursor.lnum = oap->start.lnum;
256 curwin->w_cursor.col = block_col;
257 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100258 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 {
260 curwin->w_cursor.lnum = oap->start.lnum;
261 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
262 }
263 else
264 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
265
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100266#ifdef FEAT_FOLDING
267 /* The cursor line is not in a closed fold */
268 foldOpenCursor();
269#endif
270
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (oap->line_count > p_report)
273 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200274 char *op;
275 char *msg_line_single;
276 char *msg_line_plural;
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200279 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200281 op = "<";
282 msg_line_single = NGETTEXT("%ld line %sed %d time",
283 "%ld line %sed %d times", amount);
284 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
285 "%ld lines %sed %d times", amount);
286 vim_snprintf((char *)IObuff, IOSIZE,
287 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
288 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100289 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 }
291
292 /*
293 * Set "'[" and "']" marks.
294 */
295 curbuf->b_op_start = oap->start;
296 curbuf->b_op_end.lnum = oap->end.lnum;
297 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
298 if (curbuf->b_op_end.col > 0)
299 --curbuf->b_op_end.col;
300}
301
302/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100303 * Shift the current line one shiftwidth left (if left != 0) or right
304 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 */
306 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100307shift_line(
308 int left,
309 int round,
310 int amount,
311 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
313 int count;
314 int i, j;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100315 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
317 count = get_indent(); /* get current indent */
318
319 if (round) /* round off indent */
320 {
321 i = count / p_sw; /* number of p_sw rounded down */
322 j = count % p_sw; /* extra spaces */
323 if (j && left) /* first remove extra spaces */
324 --amount;
325 if (left)
326 {
327 i -= amount;
328 if (i < 0)
329 i = 0;
330 }
331 else
332 i += amount;
333 count = i * p_sw;
334 }
335 else /* original vi indent */
336 {
337 if (left)
338 {
339 count -= p_sw * amount;
340 if (count < 0)
341 count = 0;
342 }
343 else
344 count += p_sw * amount;
345 }
346
347 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000349 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000351 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352}
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/*
355 * Shift one line of the current block one shiftwidth right or left.
356 * Leaves cursor on first character in block.
357 */
358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100359shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
361 int left = (oap->op_type == OP_LSHIFT);
362 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000363 int total;
364 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 int oldcol = curwin->w_cursor.col;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100366 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200367#ifdef FEAT_VARTABS
368 int *p_vts = curbuf->b_p_vts_array;
369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 int p_ts = (int)curbuf->b_p_ts;
371 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000373 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 int i = 0, j = 0;
375 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#ifdef FEAT_RIGHTLEFT
377 int old_p_ri = p_ri;
378
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200379 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#endif
381
382 State = INSERT; /* don't want REPLACE for State */
383 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
384 if (bd.is_short)
385 return;
386
387 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200388 total = (int)((unsigned)amount * (unsigned)p_sw);
389 if ((total / p_sw) != amount)
390 return; /* multiplication overflow */
391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 oldp = ml_get_curline();
393
394 if (!left)
395 {
396 /*
397 * 1. Get start vcol
398 * 2. Total ws vcols
399 * 3. Divvy into TABs & spp
400 * 4. Construct new string
401 */
402 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
403 ws_vcol = bd.start_vcol - bd.pre_whitesp;
404 if (bd.startspaces)
405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100407 {
408 if ((*mb_ptr2len)(bd.textstart) == 1)
409 ++bd.textstart;
410 else
411 {
412 ws_vcol = 0;
413 bd.startspaces = 0;
414 }
415 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000416 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100419 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200421 /* TODO: is passing bd.textstart for start of the line OK? */
422 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
423 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 total += incr;
425 bd.start_vcol += incr;
426 }
427 /* OK, now total=all the VWS reqd, and textstart points at the 1st
428 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200429#ifdef FEAT_VARTABS
430 if (!curbuf->b_p_et)
431 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
432 else
433 j = total;
434#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 if (!curbuf->b_p_et)
436 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
437 if (i)
438 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
439 else
440 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 /* if we're splitting a TAB, allow for it */
443 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
444 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200445 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 if (newp == NULL)
447 return;
448 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
449 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200450 vim_memset(newp + bd.textcol, TAB, (size_t)i);
451 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 /* the end */
453 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
454 }
455 else /* left */
456 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 colnr_T destination_col; /* column to which text in block will
458 be shifted */
459 char_u *verbatim_copy_end; /* end of the part of the line which is
460 copied verbatim */
461 colnr_T verbatim_copy_width;/* the (displayed) width of this part
462 of line */
463 unsigned fill; /* nr of spaces that replace a TAB */
464 unsigned new_line_len; /* the length of the line after the
465 block shift */
466 size_t block_space_width;
467 size_t shift_amount;
468 char_u *non_white = bd.textstart;
469 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000471 /*
472 * Firstly, let's find the first non-whitespace character that is
473 * displayed after the block's start column and the character's column
474 * number. Also, let's calculate the width of all the whitespace
475 * characters that are displayed in the block and precede the searched
476 * non-whitespace character.
477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000479 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
480 * the part of which is displayed at the block's beginning. Let's start
481 * searching from the next character. */
482 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100483 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000484
485 /* The character's column is in "bd.start_vcol". */
486 non_white_col = bd.start_vcol;
487
Bram Moolenaar1c465442017-03-12 20:10:05 +0100488 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000489 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200490 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000491 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 }
493
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494 block_space_width = non_white_col - oap->start_vcol;
495 /* We will shift by "total" or "block_space_width", whichever is less.
496 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000497 shift_amount = (block_space_width < (size_t)total
498 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000499
500 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000501 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502
503 /* Now let's find out how much of the beginning of the line we can
504 * reuse without modification. */
505 verbatim_copy_end = bd.textstart;
506 verbatim_copy_width = bd.start_vcol;
507
508 /* If "bd.startspaces" is set, "bd.textstart" points to the character
509 * preceding the block. We have to subtract its width to obtain its
510 * column number. */
511 if (bd.startspaces)
512 verbatim_copy_width -= bd.start_char_vcols;
513 while (verbatim_copy_width < destination_col)
514 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200515 char_u *line = verbatim_copy_end;
516
517 /* TODO: is passing verbatim_copy_end for start of the line OK? */
518 incr = lbr_chartabsize(line, verbatim_copy_end,
519 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000520 if (verbatim_copy_width + incr > destination_col)
521 break;
522 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100523 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000524 }
525
526 /* If "destination_col" is different from the width of the initial
527 * part of the line that will be copied, it means we encountered a tab
528 * character, which we will have to partly replace with spaces. */
529 fill = destination_col - verbatim_copy_width;
530
531 /* The replacement line will consist of:
532 * - the beginning of the original line up to "verbatim_copy_end",
533 * - "fill" number of spaces,
534 * - the rest of the line, pointed to by non_white. */
535 new_line_len = (unsigned)(verbatim_copy_end - oldp)
536 + fill
537 + (unsigned)STRLEN(non_white) + 1;
538
Bram Moolenaar964b3742019-05-24 18:54:09 +0200539 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (newp == NULL)
541 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000542 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200543 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000544 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 /* replace the line */
547 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
548 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
549 State = oldstate;
550 curwin->w_cursor.col = oldcol;
551#ifdef FEAT_RIGHTLEFT
552 p_ri = old_p_ri;
553#endif
554}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556/*
557 * Insert string "s" (b_insert ? before : after) block :AKelly
558 * Caller must prepare for undo.
559 */
560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100561block_insert(
562 oparg_T *oap,
563 char_u *s,
564 int b_insert,
565 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 int p_ts;
568 int count = 0; /* extra spaces to replace a cut TAB */
569 int spaces = 0; /* non-zero if cutting a TAB */
570 colnr_T offset; /* pointer along new line */
571 unsigned s_len; /* STRLEN(s) */
572 char_u *newp, *oldp; /* new, old lines */
573 linenr_T lnum; /* loop var */
574 int oldstate = State;
575
576 State = INSERT; /* don't want REPLACE for State */
577 s_len = (unsigned)STRLEN(s);
578
579 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
580 {
581 block_prep(oap, bdp, lnum, TRUE);
582 if (bdp->is_short && b_insert)
583 continue; /* OP_INSERT, line ends before block start */
584
585 oldp = ml_get(lnum);
586
587 if (b_insert)
588 {
589 p_ts = bdp->start_char_vcols;
590 spaces = bdp->startspaces;
591 if (spaces != 0)
592 count = p_ts - 1; /* we're cutting a TAB */
593 offset = bdp->textcol;
594 }
595 else /* append */
596 {
597 p_ts = bdp->end_char_vcols;
598 if (!bdp->is_short) /* spaces = padding after block */
599 {
600 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
601 if (spaces != 0)
602 count = p_ts - 1; /* we're cutting a TAB */
603 offset = bdp->textcol + bdp->textlen - (spaces != 0);
604 }
605 else /* spaces = padding to block edge */
606 {
607 /* if $ used, just append to EOL (ie spaces==0) */
608 if (!bdp->is_MAX)
609 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
610 count = spaces;
611 offset = bdp->textcol + bdp->textlen;
612 }
613 }
614
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200615 if (has_mbyte && spaces > 0)
616 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100617 int off;
618
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200619 /* Avoid starting halfway a multi-byte character. */
620 if (b_insert)
621 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100622 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200623 }
624 else
625 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100626 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200627 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200628 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100629 spaces -= off;
630 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200631 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200632
Bram Moolenaar964b3742019-05-24 18:54:09 +0200633 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (newp == NULL)
635 continue;
636
637 /* copy up to shifted part */
638 mch_memmove(newp, oldp, (size_t)(offset));
639 oldp += offset;
640
641 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200642 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 /* copy the new text */
645 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
646 offset += s_len;
647
648 if (spaces && !bdp->is_short)
649 {
650 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200651 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 /* We're splitting a TAB, don't copy it. */
653 oldp++;
654 /* We allowed for that TAB, remember this now */
655 count++;
656 }
657
658 if (spaces > 0)
659 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000660 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 ml_replace(lnum, newp, FALSE);
663
664 if (lnum == oap->end.lnum)
665 {
666 /* Set "']" mark to the end of the block instead of the end of
667 * the insert in the first line. */
668 curbuf->b_op_end.lnum = oap->end.lnum;
669 curbuf->b_op_end.col = offset;
670 }
671 } /* for all lnum */
672
673 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
674
675 State = oldstate;
676}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
679/*
680 * op_reindent - handle reindenting a block of lines.
681 */
682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100683op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 long i;
686 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200687 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 linenr_T first_changed = 0;
689 linenr_T last_changed = 0;
690 linenr_T start_lnum = curwin->w_cursor.lnum;
691
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000692 /* Don't even try when 'modifiable' is off. */
693 if (!curbuf->b_p_ma)
694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100695 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000696 return;
697 }
698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 for (i = oap->line_count; --i >= 0 && !got_int; )
700 {
701 /* it's a slow thing to do, so give feedback so there's no worry that
702 * the computer's just hung. */
703
704 if (i > 1
705 && (i % 50 == 0 || i == oap->line_count - 1)
706 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100707 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
709 /*
710 * Be vi-compatible: For lisp indenting the first line is not
711 * indented, unless there is only one line.
712 */
713#ifdef FEAT_LISP
714 if (i != oap->line_count - 1 || oap->line_count == 1
715 || how != get_lisp_indent)
716#endif
717 {
718 l = skipwhite(ml_get_curline());
719 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200720 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200722 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200724 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
726 /* did change the indent, call changed_lines() later */
727 if (first_changed == 0)
728 first_changed = curwin->w_cursor.lnum;
729 last_changed = curwin->w_cursor.lnum;
730 }
731 }
732 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000733 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735
736 /* put cursor on first non-blank of indented line */
737 curwin->w_cursor.lnum = start_lnum;
738 beginline(BL_SOL | BL_FIX);
739
740 /* Mark changed lines so that they will be redrawn. When Visual
741 * highlighting was present, need to continue until the last line. When
742 * there is no change still need to remove the Visual highlighting. */
743 if (last_changed != 0)
744 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 else if (oap->is_VIsual)
748 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750 if (oap->line_count > p_report)
751 {
752 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200754 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756 /* set '[ and '] marks */
757 curbuf->b_op_start = oap->start;
758 curbuf->b_op_end = oap->end;
759}
760#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
761
762#if defined(FEAT_EVAL) || defined(PROTO)
763/*
764 * Keep the last expression line here, for repeating.
765 */
766static char_u *expr_line = NULL;
767
768/*
769 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
770 * Returns '=' when OK, NUL otherwise.
771 */
772 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100773get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 char_u *new_line;
776
Bram Moolenaare96a2492019-06-25 04:12:16 +0200777 new_line = getcmdline('=', 0L, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (new_line == NULL)
779 return NUL;
780 if (*new_line == NUL) /* use previous line */
781 vim_free(new_line);
782 else
783 set_expr_line(new_line);
784 return '=';
785}
786
787/*
788 * Set the expression for the '=' register.
789 * Argument must be an allocated string.
790 */
791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100792set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 vim_free(expr_line);
795 expr_line = new_line;
796}
797
798/*
799 * Get the result of the '=' register expression.
800 * Returns a pointer to allocated memory, or NULL for failure.
801 */
802 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 char_u *expr_copy;
806 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000807 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 if (expr_line == NULL)
810 return NULL;
811
812 /* Make a copy of the expression, because evaluating it may cause it to be
813 * changed. */
814 expr_copy = vim_strsave(expr_line);
815 if (expr_copy == NULL)
816 return NULL;
817
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000818 /* When we are invoked recursively limit the evaluation to 10 levels.
819 * Then return the string as-is. */
820 if (nested >= 10)
821 return expr_copy;
822
823 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000824 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000825 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 vim_free(expr_copy);
827 return rv;
828}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000829
830/*
831 * Get the '=' register expression itself, without evaluating it.
832 */
833 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100834get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000835{
836 if (expr_line == NULL)
837 return NULL;
838 return vim_strsave(expr_line);
839}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#endif /* FEAT_EVAL */
841
842/*
843 * Check if 'regname' is a valid name of a yank register.
844 * Note: There is no check for 0 (default register), caller should do this
845 */
846 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100847valid_yank_reg(
848 int regname,
849 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 if ( (regname > 0 && ASCII_ISALNUM(regname))
852 || (!writing && vim_strchr((char_u *)
853#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100854 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100856 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100859 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 || regname == '"'
861 || regname == '-'
862 || regname == '_'
863#ifdef FEAT_CLIPBOARD
864 || regname == '*'
865 || regname == '+'
866#endif
867#ifdef FEAT_DND
868 || (!writing && regname == '~')
869#endif
870 )
871 return TRUE;
872 return FALSE;
873}
874
875/*
876 * Set y_current and y_append, according to the value of "regname".
877 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000878 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 *
880 * If regname is 0 and writing, use register 0
881 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100882 *
883 * Return TRUE when the register should be inserted literally (selection or
884 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100886 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100887get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
889 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100890 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 y_append = FALSE;
893 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
894 {
895 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100896 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 i = regname;
899 if (VIM_ISDIGIT(i))
900 i -= '0';
901 else if (ASCII_ISLOWER(i))
902 i = CharOrdLow(i) + 10;
903 else if (ASCII_ISUPPER(i))
904 {
905 i = CharOrdUp(i) + 10;
906 y_append = TRUE;
907 }
908 else if (regname == '-')
909 i = DELETION_REGISTER;
910#ifdef FEAT_CLIPBOARD
911 /* When selection is not available, use register 0 instead of '*' */
912 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100915 ret = TRUE;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 /* When clipboard is not available, use register 0 instead of '+' */
918 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100921 ret = TRUE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#endif
924#ifdef FEAT_DND
925 else if (!writing && regname == '~')
926 i = TILDE_REGISTER;
927#endif
928 else /* not 0-9, a-z, A-Z or '-': use register 0 */
929 i = 0;
930 y_current = &(y_regs[i]);
931 if (writing) /* remember the register we write into for do_put() */
932 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100933 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934}
935
Bram Moolenaar8299df92004-07-10 09:47:34 +0000936#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937/*
938 * When "regname" is a clipboard register, obtain the selection. If it's not
939 * available return zero, otherwise return "regname".
940 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000941 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100942may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 if (regname == '*')
945 {
946 if (!clip_star.available)
947 regname = 0;
948 else
949 clip_get_selection(&clip_star);
950 }
951 else if (regname == '+')
952 {
953 if (!clip_plus.available)
954 regname = 0;
955 else
956 clip_get_selection(&clip_plus);
957 }
958 return regname;
959}
960#endif
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962/*
963 * Obtain the contents of a "normal" register. The register is made empty.
964 * The returned pointer has allocated memory, use put_register() later.
965 */
966 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100967get_register(
968 int name,
969 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200971 yankreg_T *reg;
972 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974#ifdef FEAT_CLIPBOARD
975 /* When Visual area changed, may have to update selection. Obtain the
976 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000977 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200979 if (clip_isautosel_star())
980 clip_update_selection(&clip_star);
981 may_get_selection(name);
982 }
983 if (name == '+' && clip_plus.available)
984 {
985 if (clip_isautosel_plus())
986 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 may_get_selection(name);
988 }
989#endif
990
991 get_yank_register(name, 0);
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200992 reg = ALLOC_ONE(yankreg_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (reg != NULL)
994 {
995 *reg = *y_current;
996 if (copy)
997 {
998 /* If we run out of memory some or all of the lines are empty. */
999 if (reg->y_size == 0)
1000 reg->y_array = NULL;
1001 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001002 reg->y_array = ALLOC_MULT(char_u *, reg->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (reg->y_array != NULL)
1004 {
1005 for (i = 0; i < reg->y_size; ++i)
1006 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1007 }
1008 }
1009 else
1010 y_current->y_array = NULL;
1011 }
1012 return (void *)reg;
1013}
1014
1015/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001016 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
1018 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001019put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 get_yank_register(name, 0);
1022 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001023 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001024 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001026#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /* Send text written to clipboard register to the clipboard. */
1028 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001031
Bram Moolenaar113e1072019-01-20 15:30:40 +01001032#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
1033 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001035free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001036{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001037 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001038
1039 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001040 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001041 free_yank_all();
1042 vim_free(reg);
1043 *y_current = tmp;
1044}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047#if defined(FEAT_MOUSE) || defined(PROTO)
1048/*
1049 * return TRUE if the current yank register has type MLINE
1050 */
1051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001052yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1055 return FALSE;
1056 if (regname == '_') /* black hole is always empty */
1057 return FALSE;
1058 get_yank_register(regname, FALSE);
1059 return (y_current->y_type == MLINE);
1060}
1061#endif
1062
1063/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001064 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001066 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 */
1068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001069do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 char_u *p;
1072 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001073 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001074 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001076 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001078 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1080 retval = FAIL;
1081 else
1082 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001083 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 showmode();
1085 regname = c;
1086 retval = OK;
1087 }
1088 }
1089 else /* stop recording */
1090 {
1091 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001092 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1093 * needs to be removed again to put it in a register. exec_reg then
1094 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001096 reg_recording = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001097 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 p = get_recorded();
1099 if (p == NULL)
1100 retval = FAIL;
1101 else
1102 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001103 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1104 vim_unescape_csi(p);
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
1107 * We don't want to change the default register here, so save and
1108 * restore the current register name.
1109 */
1110 old_y_previous = y_previous;
1111 old_y_current = y_current;
1112
1113 retval = stuff_yank(regname, p);
1114
1115 y_previous = old_y_previous;
1116 y_current = old_y_current;
1117 }
1118 }
1119 return retval;
1120}
1121
1122/*
1123 * Stuff string "p" into yank register "regname" as a single line (append if
1124 * uppercase). "p" must have been alloced.
1125 *
1126 * return FAIL for failure, OK otherwise
1127 */
1128 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001129stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 char_u *lp;
1132 char_u **pp;
1133
1134 /* check for read-only register */
1135 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1136 {
1137 vim_free(p);
1138 return FAIL;
1139 }
1140 if (regname == '_') /* black hole: don't do anything */
1141 {
1142 vim_free(p);
1143 return OK;
1144 }
1145 get_yank_register(regname, TRUE);
1146 if (y_append && y_current->y_array != NULL)
1147 {
1148 pp = &(y_current->y_array[y_current->y_size - 1]);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001149 lp = alloc(STRLEN(*pp) + STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (lp == NULL)
1151 {
1152 vim_free(p);
1153 return FAIL;
1154 }
1155 STRCPY(lp, *pp);
1156 STRCAT(lp, p);
1157 vim_free(p);
1158 vim_free(*pp);
1159 *pp = lp;
1160 }
1161 else
1162 {
1163 free_yank_all();
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001164 if ((y_current->y_array = ALLOC_ONE(char_u *)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 vim_free(p);
1167 return FAIL;
1168 }
1169 y_current->y_array[0] = p;
1170 y_current->y_size = 1;
1171 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001172#ifdef FEAT_VIMINFO
1173 y_current->y_time_set = vim_time();
1174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 return OK;
1177}
1178
Bram Moolenaar42b94362009-05-26 16:12:37 +00001179static int execreg_lastc = NUL;
1180
Bram Moolenaarc3328162019-07-23 22:15:25 +02001181 int
1182get_execreg_lastc(void)
1183{
1184 return execreg_lastc;
1185}
1186
1187 void
1188set_execreg_lastc(int lastc)
1189{
1190 execreg_lastc = lastc;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001194 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001196 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 */
1198 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001199do_execreg(
1200 int regname,
1201 int colon, /* insert ':' before each line */
1202 int addcr, /* always add '\n' to end of line */
1203 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 long i;
1206 char_u *p;
1207 int retval = OK;
1208 int remap;
1209
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001210 // repeat previous one
1211 if (regname == '@')
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001212 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 emsg(_("E748: No previously used register"));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001216 return FAIL;
1217 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001218 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001219 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001220 // check for valid regname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001222 {
1223 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001225 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001226 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228#ifdef FEAT_CLIPBOARD
1229 regname = may_get_selection(regname);
1230#endif
1231
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001232 // black hole: don't stuff anything
1233 if (regname == '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 return OK;
1235
1236#ifdef FEAT_CMDHIST
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001237 // use last command line
1238 if (regname == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 if (last_cmdline == NULL)
1241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001242 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 return FAIL;
1244 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001245 // don't keep the cmdline containing @:
1246 VIM_CLEAR(new_last_cmdline);
1247 // Escape all control characters with a CTRL-V
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001248 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001249 (char_u *)"\001\002\003\004\005\006\007"
1250 "\010\011\012\013\014\015\016\017"
1251 "\020\021\022\023\024\025\026\027"
1252 "\030\031\032\033\034\035\036\037",
1253 Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001254 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001255 {
1256 /* When in Visual mode "'<,'>" will be prepended to the command.
1257 * Remove it when it's already there. */
1258 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001259 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001260 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001261 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001262 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001263 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265#endif
1266#ifdef FEAT_EVAL
1267 else if (regname == '=')
1268 {
1269 p = get_expr_line();
1270 if (p == NULL)
1271 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001272 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 vim_free(p);
1274 }
1275#endif
1276 else if (regname == '.') /* use last inserted text */
1277 {
1278 p = get_last_insert_save();
1279 if (p == NULL)
1280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001281 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 return FAIL;
1283 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001284 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 vim_free(p);
1286 }
1287 else
1288 {
1289 get_yank_register(regname, FALSE);
1290 if (y_current->y_array == NULL)
1291 return FAIL;
1292
1293 /* Disallow remaping for ":@r". */
1294 remap = colon ? REMAP_NONE : REMAP_YES;
1295
1296 /*
1297 * Insert lines into typeahead buffer, from last one to first one.
1298 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001299 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 for (i = y_current->y_size; --i >= 0; )
1301 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001302 char_u *escaped;
1303
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 /* insert NL between lines and after last line if type is MLINE */
1305 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1306 || addcr)
1307 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001308 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 return FAIL;
1310 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001311 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1312 if (escaped == NULL)
1313 return FAIL;
1314 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1315 vim_free(escaped);
1316 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001318 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 == FAIL)
1320 return FAIL;
1321 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001322 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324 return retval;
1325}
1326
1327/*
1328 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1329 * used only after other typeahead has been processed.
1330 */
1331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001332put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 char_u buf[3];
1335
1336 if (restart_edit != NUL)
1337 {
1338 if (restart_edit == 'V')
1339 {
1340 buf[0] = 'g';
1341 buf[1] = 'R';
1342 buf[2] = NUL;
1343 }
1344 else
1345 {
1346 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1347 buf[1] = NUL;
1348 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001349 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 restart_edit = NUL;
1351 }
1352}
1353
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001354/*
1355 * Insert register contents "s" into the typeahead buffer, so that it will be
1356 * executed again.
1357 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1358 * no remapping.
1359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001361put_in_typebuf(
1362 char_u *s,
1363 int esc,
1364 int colon, /* add ':' before the line */
1365 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
1367 int retval = OK;
1368
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001369 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001371 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001373 {
1374 char_u *p;
1375
1376 if (esc)
1377 p = vim_strsave_escape_csi(s);
1378 else
1379 p = s;
1380 if (p == NULL)
1381 retval = FAIL;
1382 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001383 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1384 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001385 if (esc)
1386 vim_free(p);
1387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001389 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return retval;
1391}
1392
1393/*
1394 * Insert a yank register: copy it into the Read buffer.
1395 * Used by CTRL-R command and middle mouse button in insert mode.
1396 *
1397 * return FAIL for failure, OK otherwise
1398 */
1399 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001400insert_reg(
1401 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001402 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 long i;
1405 int retval = OK;
1406 char_u *arg;
1407 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001408 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 /*
1411 * It is possible to get into an endless loop by having CTRL-R a in
1412 * register a and then, in insert mode, doing CTRL-R a.
1413 * If you hit CTRL-C, the loop will be broken here.
1414 */
1415 ui_breakcheck();
1416 if (got_int)
1417 return FAIL;
1418
1419 /* check for valid regname */
1420 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1421 return FAIL;
1422
1423#ifdef FEAT_CLIPBOARD
1424 regname = may_get_selection(regname);
1425#endif
1426
1427 if (regname == '.') /* insert last inserted text */
1428 retval = stuff_inserted(NUL, 1L, TRUE);
1429 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1430 {
1431 if (arg == NULL)
1432 return FAIL;
1433 stuffescaped(arg, literally);
1434 if (allocated)
1435 vim_free(arg);
1436 }
1437 else /* name or number register */
1438 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001439 if (get_yank_register(regname, FALSE))
1440 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (y_current->y_array == NULL)
1442 retval = FAIL;
1443 else
1444 {
1445 for (i = 0; i < y_current->y_size; ++i)
1446 {
1447 stuffescaped(y_current->y_array[i], literally);
1448 /*
1449 * Insert a newline between lines and after last line if
1450 * y_type is MLINE.
1451 */
1452 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1453 stuffcharReadbuff('\n');
1454 }
1455 }
1456 }
1457
1458 return retval;
1459}
1460
1461/*
1462 * Stuff a string into the typeahead buffer, such that edit() will insert it
1463 * literally ("literally" TRUE) or interpret is as typed characters.
1464 */
1465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001466stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 int c;
1469 char_u *start;
1470
1471 while (*arg != NUL)
1472 {
1473 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1474 * stuff K_SPECIAL to get the effect of a special key when "literally"
1475 * is TRUE. */
1476 start = arg;
1477 while ((*arg >= ' '
1478#ifndef EBCDIC
1479 && *arg < DEL /* EBCDIC: chars above space are normal */
1480#endif
1481 )
1482 || (*arg == K_SPECIAL && !literally))
1483 ++arg;
1484 if (arg > start)
1485 stuffReadbuffLen(start, (long)(arg - start));
1486
1487 /* stuff a single special character */
1488 if (*arg != NUL)
1489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001491 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 c = *arg++;
1494 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1495 stuffcharReadbuff(Ctrl_V);
1496 stuffcharReadbuff(c);
1497 }
1498 }
1499}
1500
1501/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001502 * If "regname" is a special register, return TRUE and store a pointer to its
1503 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001506get_spec_reg(
1507 int regname,
1508 char_u **argp,
1509 int *allocated, /* return: TRUE when value was allocated */
1510 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 int cnt;
1513
1514 *argp = NULL;
1515 *allocated = FALSE;
1516 switch (regname)
1517 {
1518 case '%': /* file name */
1519 if (errmsg)
1520 check_fname(); /* will give emsg if not set */
1521 *argp = curbuf->b_fname;
1522 return TRUE;
1523
1524 case '#': /* alternate file name */
1525 *argp = getaltfname(errmsg); /* may give emsg if not set */
1526 return TRUE;
1527
1528#ifdef FEAT_EVAL
1529 case '=': /* result of expression */
1530 *argp = get_expr_line();
1531 *allocated = TRUE;
1532 return TRUE;
1533#endif
1534
1535 case ':': /* last command line */
1536 if (last_cmdline == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001537 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 *argp = last_cmdline;
1539 return TRUE;
1540
1541 case '/': /* last search-pattern */
1542 if (last_search_pat() == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 *argp = last_search_pat();
1545 return TRUE;
1546
1547 case '.': /* last inserted text */
1548 *argp = get_last_insert_save();
1549 *allocated = TRUE;
1550 if (*argp == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return TRUE;
1553
1554#ifdef FEAT_SEARCHPATH
1555 case Ctrl_F: /* Filename under cursor */
1556 case Ctrl_P: /* Path under cursor, expand via "path" */
1557 if (!errmsg)
1558 return FALSE;
1559 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001560 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 *allocated = TRUE;
1562 return TRUE;
1563#endif
1564
1565 case Ctrl_W: /* word under cursor */
1566 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1567 if (!errmsg)
1568 return FALSE;
1569 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1570 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1571 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1572 *allocated = TRUE;
1573 return TRUE;
1574
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001575 case Ctrl_L: /* Line under cursor */
1576 if (!errmsg)
1577 return FALSE;
1578
1579 *argp = ml_get_buf(curwin->w_buffer,
1580 curwin->w_cursor.lnum, FALSE);
1581 return TRUE;
1582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 case '_': /* black hole: always empty */
1584 *argp = (char_u *)"";
1585 return TRUE;
1586 }
1587
1588 return FALSE;
1589}
1590
1591/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001592 * Paste a yank register into the command line.
1593 * Only for non-special registers.
1594 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * insert_reg() can't be used here, because special characters from the
1596 * register contents will be interpreted as commands.
1597 *
1598 * return FAIL for failure, OK otherwise
1599 */
1600 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001601cmdline_paste_reg(
1602 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001603 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001604 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
1606 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001607 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001609 if (get_yank_register(regname, FALSE))
1610 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (y_current->y_array == NULL)
1612 return FAIL;
1613
1614 for (i = 0; i < y_current->y_size; ++i)
1615 {
1616 cmdline_paste_str(y_current->y_array[i], literally);
1617
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001618 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001619 * Don't do this when "remcr" is TRUE. */
1620 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 cmdline_paste_str((char_u *)"\r", literally);
1622
1623 /* Check for CTRL-C, in case someone tries to paste a few thousand
1624 * lines and gets bored. */
1625 ui_breakcheck();
1626 if (got_int)
1627 return FAIL;
1628 }
1629 return OK;
1630}
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1633/*
1634 * Adjust the register name pointed to with "rp" for the clipboard being
1635 * used always and the clipboard being available.
1636 */
1637 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001638adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001640 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1641 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001642 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1643 {
1644 if (clip_unnamed != 0)
1645 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001646 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001647 else
1648 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1649 ? '+' : '*';
1650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (!clip_star.available && *rp == '*')
1652 *rp = 0;
1653 if (!clip_plus.available && *rp == '+')
1654 *rp = 0;
1655}
1656#endif
1657
1658/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001659 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1660 */
1661 void
1662shift_delete_registers()
1663{
1664 int n;
1665
1666 y_current = &y_regs[9];
1667 free_yank_all(); /* free register nine */
1668 for (n = 9; n > 1; --n)
1669 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001670 y_current = &y_regs[1];
1671 if (!y_append)
1672 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001673 y_regs[1].y_array = NULL; /* set register one to empty */
1674}
1675
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001676#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001677 static void
1678yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1679{
1680 static int recursive = FALSE;
1681 dict_T *v_event;
1682 list_T *list;
1683 int n;
1684 char_u buf[NUMBUFLEN + 2];
1685 long reglen = 0;
1686
1687 if (recursive)
1688 return;
1689
1690 v_event = get_vim_var_dict(VV_EVENT);
1691
1692 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001693 if (list == NULL)
1694 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001695 for (n = 0; n < reg->y_size; n++)
1696 list_append_string(list, reg->y_array[n], -1);
1697 list->lv_lock = VAR_FIXED;
1698 dict_add_list(v_event, "regcontents", list);
1699
1700 buf[0] = (char_u)oap->regname;
1701 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001702 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001703
1704 buf[0] = get_op_char(oap->op_type);
1705 buf[1] = get_extra_op_char(oap->op_type);
1706 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001707 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001708
1709 buf[0] = NUL;
1710 buf[1] = NUL;
1711 switch (get_reg_type(oap->regname, &reglen))
1712 {
1713 case MLINE: buf[0] = 'V'; break;
1714 case MCHAR: buf[0] = 'v'; break;
1715 case MBLOCK:
1716 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1717 reglen + 1);
1718 break;
1719 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001720 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001721
1722 /* Lock the dictionary and its keys */
1723 dict_set_items_ro(v_event);
1724
1725 recursive = TRUE;
1726 textlock++;
1727 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1728 textlock--;
1729 recursive = FALSE;
1730
1731 /* Empty the dictionary, v:event is still valid */
1732 dict_free_contents(v_event);
1733 hash_init(&v_event->dv_hashtab);
1734}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001735#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001736
Bram Moolenaara1891842017-02-04 21:34:31 +01001737/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001738 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001740 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 */
1742 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001743op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
1745 int n;
1746 linenr_T lnum;
1747 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 char_u *newp, *oldp;
1749 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1751 int did_yank = FALSE;
1752
1753 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1754 return OK;
1755
1756 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1757 if (oap->empty)
1758 return u_save_cursor();
1759
1760 if (!curbuf->b_p_ma)
1761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001762 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 return FAIL;
1764 }
1765
1766#ifdef FEAT_CLIPBOARD
1767 adjust_clip_reg(&oap->regname);
1768#endif
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (has_mbyte)
1771 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Bram Moolenaard04b7502010-07-08 22:27:55 +02001773 /*
1774 * Imitate the strange Vi behaviour: If the delete spans more than one
1775 * line and motion_type == MCHAR and the result is a blank line, make the
1776 * delete linewise. Don't do this for the change command or Visual mode.
1777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001780 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001782 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 && oap->op_type == OP_DELETE)
1784 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001785 ptr = ml_get(oap->end.lnum) + oap->end.col;
1786 if (*ptr != NUL)
1787 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 ptr = skipwhite(ptr);
1789 if (*ptr == NUL && inindent(0))
1790 oap->motion_type = MLINE;
1791 }
1792
Bram Moolenaard04b7502010-07-08 22:27:55 +02001793 /*
1794 * Check for trying to delete (e.g. "D") in an empty line.
1795 * Note: For the change operator it is ok.
1796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if ( oap->motion_type == MCHAR
1798 && oap->line_count == 1
1799 && oap->op_type == OP_DELETE
1800 && *ml_get(oap->start.lnum) == NUL)
1801 {
1802 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001803 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 * 'cpoptions' (Vi compatible).
1805 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001806 if (virtual_op)
1807 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1808 * marks as if it happened. */
1809 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1811 beep_flush();
1812 return OK;
1813 }
1814
Bram Moolenaard04b7502010-07-08 22:27:55 +02001815 /*
1816 * Do a yank of whatever we're about to delete.
1817 * If a yank register was specified, put the deleted text into that
1818 * register. For the black hole register '_' don't yank anything.
1819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (oap->regname != '_')
1821 {
1822 if (oap->regname != 0)
1823 {
1824 /* check for read-only register */
1825 if (!valid_yank_reg(oap->regname, TRUE))
1826 {
1827 beep_flush();
1828 return OK;
1829 }
1830 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1831 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1832 did_yank = TRUE;
1833 }
1834
1835 /*
1836 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001837 * delete contains a line break, or when using a specific operator (Vi
1838 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +02001839 * Use the register name from before adjust_clip_reg() may have
1840 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001842 if (oap->motion_type == MLINE || oap->line_count > 1
1843 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001845 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (op_yank(oap, TRUE, FALSE) == OK)
1847 did_yank = TRUE;
1848 }
1849
Bram Moolenaar84298db2012-04-20 13:46:08 +02001850 /* Yank into small delete register when no named register specified
1851 * and the delete is within one line. */
1852 if ((
1853#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001854 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1855 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001856#endif
1857 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 && oap->line_count == 1)
1859 {
1860 oap->regname = '-';
1861 get_yank_register(oap->regname, TRUE);
1862 if (op_yank(oap, TRUE, FALSE) == OK)
1863 did_yank = TRUE;
1864 oap->regname = 0;
1865 }
1866
1867 /*
1868 * If there's too much stuff to fit in the yank register, then get a
1869 * confirmation before doing the delete. This is crude, but simple.
1870 * And it avoids doing a delete of something we can't put back if we
1871 * want.
1872 */
1873 if (!did_yank)
1874 {
1875 int msg_silent_save = msg_silent;
1876
1877 msg_silent = 0; /* must display the prompt */
1878 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1879 msg_silent = msg_silent_save;
1880 if (n != 'y')
1881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 return FAIL;
1884 }
1885 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001886
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001887#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001888 if (did_yank && has_textyankpost())
1889 yank_do_autocmd(oap, y_current);
1890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
1892
Bram Moolenaard04b7502010-07-08 22:27:55 +02001893 /*
1894 * block mode delete
1895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (oap->block_mode)
1897 {
1898 if (u_save((linenr_T)(oap->start.lnum - 1),
1899 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1900 return FAIL;
1901
1902 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1903 {
1904 block_prep(oap, &bd, lnum, TRUE);
1905 if (bd.textlen == 0) /* nothing to delete */
1906 continue;
1907
1908 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1909 if (lnum == curwin->w_cursor.lnum)
1910 {
1911 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 }
1914
Bram Moolenaar8055d172019-05-17 22:57:26 +02001915 // "n" == number of chars deleted
1916 // If we delete a TAB, it may be replaced by several characters.
1917 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 n = bd.textlen - bd.startspaces - bd.endspaces;
1919 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001920 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if (newp == NULL)
1922 continue;
1923 /* copy up to deleted part */
1924 mch_memmove(newp, oldp, (size_t)bd.textcol);
1925 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001926 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 (size_t)(bd.startspaces + bd.endspaces));
1928 /* copy the part after the deleted part */
1929 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001930 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 /* replace the line */
1932 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001933
1934#ifdef FEAT_TEXT_PROP
1935 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001936 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939
1940 check_cursor_col();
1941 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1942 oap->end.lnum + 1, 0L);
1943 oap->line_count = 0; /* no lines deleted */
1944 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001945 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 if (oap->op_type == OP_CHANGE)
1948 {
1949 /* Delete the lines except the first one. Temporarily move the
1950 * cursor to the next line. Save the current line number, if the
1951 * last line is deleted it may be changed.
1952 */
1953 if (oap->line_count > 1)
1954 {
1955 lnum = curwin->w_cursor.lnum;
1956 ++curwin->w_cursor.lnum;
1957 del_lines((long)(oap->line_count - 1), TRUE);
1958 curwin->w_cursor.lnum = lnum;
1959 }
1960 if (u_save_cursor() == FAIL)
1961 return FAIL;
1962 if (curbuf->b_p_ai) /* don't delete indent */
1963 {
1964 beginline(BL_WHITE); /* cursor on first non-white */
1965 did_ai = TRUE; /* delete the indent when ESC hit */
1966 ai_col = curwin->w_cursor.col;
1967 }
1968 else
1969 beginline(0); /* cursor in column 0 */
1970 truncate_line(FALSE); /* delete the rest of the line */
1971 /* leave cursor past last char in line */
1972 if (oap->line_count > 1)
1973 u_clearline(); /* "U" command not possible after "2cc" */
1974 }
1975 else
1976 {
1977 del_lines(oap->line_count, TRUE);
1978 beginline(BL_WHITE | BL_FIX);
1979 u_clearline(); /* "U" command not possible after "dd" */
1980 }
1981 }
1982 else
1983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (virtual_op)
1985 {
1986 int endcol = 0;
1987
1988 /* For virtualedit: break the tabs that are partly included. */
1989 if (gchar_pos(&oap->start) == '\t')
1990 {
1991 if (u_save_cursor() == FAIL) /* save first line for undo */
1992 return FAIL;
1993 if (oap->line_count == 1)
1994 endcol = getviscol2(oap->end.col, oap->end.coladd);
1995 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1996 oap->start = curwin->w_cursor;
1997 if (oap->line_count == 1)
1998 {
1999 coladvance(endcol);
2000 oap->end.col = curwin->w_cursor.col;
2001 oap->end.coladd = curwin->w_cursor.coladd;
2002 curwin->w_cursor = oap->start;
2003 }
2004 }
2005
2006 /* Break a tab only when it's included in the area. */
2007 if (gchar_pos(&oap->end) == '\t'
2008 && (int)oap->end.coladd < oap->inclusive)
2009 {
2010 /* save last line for undo */
2011 if (u_save((linenr_T)(oap->end.lnum - 1),
2012 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2013 return FAIL;
2014 curwin->w_cursor = oap->end;
2015 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2016 oap->end = curwin->w_cursor;
2017 curwin->w_cursor = oap->start;
2018 }
2019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 if (oap->line_count == 1) /* delete characters within one line */
2022 {
2023 if (u_save_cursor() == FAIL) /* save line for undo */
2024 return FAIL;
2025
2026 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002027 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 && oap->op_type == OP_CHANGE
2029 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002030 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 display_dollar(oap->end.col - !oap->inclusive);
2032
2033 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (virtual_op)
2036 {
2037 /* fix up things for virtualedit-delete:
2038 * break the tabs which are going to get in our way
2039 */
2040 char_u *curline = ml_get_curline();
2041 int len = (int)STRLEN(curline);
2042
2043 if (oap->end.coladd != 0
2044 && (int)oap->end.col >= len - 1
2045 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2046 n++;
2047 /* Delete at least one char (e.g, when on a control char). */
2048 if (n == 0 && oap->start.coladd != oap->end.coladd)
2049 n = 1;
2050
2051 /* When deleted a char in the line, reset coladd. */
2052 if (gchar_cursor() != NUL)
2053 curwin->w_cursor.coladd = 0;
2054 }
Bram Moolenaard009e862015-06-09 20:20:03 +02002055 (void)del_bytes((long)n, !virtual_op,
2056 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058 else /* delete characters between lines */
2059 {
2060 pos_T curpos;
2061
2062 /* save deleted and changed lines for undo */
2063 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2064 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2065 return FAIL;
2066
2067 truncate_line(TRUE); /* delete from cursor to end of line */
2068
2069 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2070 ++curwin->w_cursor.lnum;
2071 del_lines((long)(oap->line_count - 2), FALSE);
2072
Bram Moolenaard009e862015-06-09 20:20:03 +02002073 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002074 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002075 curwin->w_cursor.col = 0;
2076 (void)del_bytes((long)n, !virtual_op,
2077 oap->op_type == OP_DELETE && !oap->is_VIsual);
2078 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2079 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081 }
2082
2083 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2084
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002085setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (oap->block_mode)
2087 {
2088 curbuf->b_op_end.lnum = oap->end.lnum;
2089 curbuf->b_op_end.col = oap->start.col;
2090 }
2091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 curbuf->b_op_end = oap->start;
2093 curbuf->b_op_start = oap->start;
2094
2095 return OK;
2096}
2097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098/*
2099 * Adjust end of operating area for ending on a multi-byte character.
2100 * Used for deletion.
2101 */
2102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002103mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 char_u *p;
2106
2107 if (oap->inclusive)
2108 {
2109 p = ml_get(oap->end.lnum);
2110 oap->end.col += mb_tail_off(p, p + oap->end.col);
2111 }
2112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaar630afe82018-06-28 19:26:28 +02002114/*
2115 * Replace the character under the cursor with "c".
2116 * This takes care of multi-byte characters.
2117 */
2118 static void
2119replace_character(int c)
2120{
2121 int n = State;
2122
2123 State = REPLACE;
2124 ins_char(c);
2125 State = n;
2126 /* Backup to the replaced character. */
2127 dec_cursor();
2128}
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130/*
2131 * Replace a whole area with one character.
2132 */
2133 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 char_u *newp, *oldp;
2139 size_t oldlen;
2140 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002141 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002142 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
2144 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2145 return OK; /* nothing to do */
2146
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002147 if (c == REPLACE_CR_NCHAR)
2148 {
2149 had_ctrl_v_cr = TRUE;
2150 c = CAR;
2151 }
2152 else if (c == REPLACE_NL_NCHAR)
2153 {
2154 had_ctrl_v_cr = TRUE;
2155 c = NL;
2156 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (has_mbyte)
2159 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161 if (u_save((linenr_T)(oap->start.lnum - 1),
2162 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2163 return FAIL;
2164
2165 /*
2166 * block mode replace
2167 */
2168 if (oap->block_mode)
2169 {
2170 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2171 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2172 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002173 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2175 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2176 continue; /* nothing to replace */
2177
2178 /* n == number of extra chars required
2179 * If we split a TAB, it may be replaced by several characters.
2180 * Thus the number of characters may increase!
2181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 /* If the range starts in virtual space, count the initial
2183 * coladd offset as part of "startspaces" */
2184 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2185 {
2186 pos_T vpos;
2187
Bram Moolenaara1381de2009-11-03 15:44:21 +00002188 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 getvpos(&vpos, oap->start_vcol);
2190 bd.startspaces += vpos.coladd;
2191 n = bd.startspaces;
2192 }
2193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /* allow for pre spaces */
2195 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2196
2197 /* allow for post spp */
2198 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2201 /* Figure out how many characters to replace. */
2202 numc = oap->end_vcol - oap->start_vcol + 1;
2203 if (bd.is_short && (!virtual_op || bd.is_MAX))
2204 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /* A double-wide character can be replaced only up to half the
2207 * times. */
2208 if ((*mb_char2cells)(c) > 1)
2209 {
2210 if ((numc & 1) && !bd.is_short)
2211 {
2212 ++bd.endspaces;
2213 ++n;
2214 }
2215 numc = numc / 2;
2216 }
2217
2218 /* Compute bytes needed, move character count to num_chars. */
2219 num_chars = numc;
2220 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 /* oldlen includes textlen, so don't double count */
2222 n += numc - bd.textlen;
2223
2224 oldp = ml_get_curline();
2225 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002226 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (newp == NULL)
2228 continue;
2229 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2230 /* copy up to deleted part */
2231 mch_memmove(newp, oldp, (size_t)bd.textcol);
2232 oldp += bd.textcol + bd.textlen;
2233 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002234 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002236 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2237 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002238 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002240 if (has_mbyte)
2241 {
2242 n = (int)STRLEN(newp);
2243 while (--num_chars >= 0)
2244 n += (*mb_char2bytes)(c, newp + n);
2245 }
2246 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002247 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002248 if (!bd.is_short)
2249 {
2250 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002251 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002252 /* copy the part after the changed part */
2253 STRMOVE(newp + STRLEN(newp), oldp);
2254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002258 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002259 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01002260 if (after_p != NULL)
2261 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263 /* replace the line */
2264 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002265 if (after_p != NULL)
2266 {
2267 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2268 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2269 oap->end.lnum++;
2270 vim_free(after_p);
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273 }
2274 else
2275 {
2276 /*
2277 * MCHAR and MLINE motion replace.
2278 */
2279 if (oap->motion_type == MLINE)
2280 {
2281 oap->start.col = 0;
2282 curwin->w_cursor.col = 0;
2283 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2284 if (oap->end.col)
2285 --oap->end.col;
2286 }
2287 else if (!oap->inclusive)
2288 dec(&(oap->end));
2289
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002290 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 n = gchar_cursor();
2293 if (n != NUL)
2294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2296 {
2297 /* This is slow, but it handles replacing a single-byte
2298 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002299 if (curwin->w_cursor.lnum == oap->end.lnum)
2300 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002301 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (n == TAB)
2306 {
2307 int end_vcol = 0;
2308
2309 if (curwin->w_cursor.lnum == oap->end.lnum)
2310 {
2311 /* oap->end has to be recalculated when
2312 * the tab breaks */
2313 end_vcol = getviscol2(oap->end.col,
2314 oap->end.coladd);
2315 }
2316 coladvance_force(getviscol());
2317 if (curwin->w_cursor.lnum == oap->end.lnum)
2318 getvpos(&oap->end, end_vcol);
2319 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02002320 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
2322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2324 {
2325 int virtcols = oap->end.coladd;
2326
2327 if (curwin->w_cursor.lnum == oap->start.lnum
2328 && oap->start.col == oap->end.col && oap->start.coladd)
2329 virtcols -= oap->start.coladd;
2330
2331 /* oap->end has been trimmed so it's effectively inclusive;
2332 * as a result an extra +1 must be counted so we don't
2333 * trample the NUL byte. */
2334 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2335 curwin->w_cursor.col -= (virtcols + 1);
2336 for (; virtcols >= 0; virtcols--)
2337 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02002338 if ((*mb_char2len)(c) > 1)
2339 replace_character(c);
2340 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002341 PBYTE(curwin->w_cursor, c);
2342 if (inc(&curwin->w_cursor) == -1)
2343 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 /* Advance to next character, stop at the end of the file. */
2348 if (inc_cursor() == -1)
2349 break;
2350 }
2351 }
2352
2353 curwin->w_cursor = oap->start;
2354 check_cursor();
2355 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2356
2357 /* Set "'[" and "']" marks. */
2358 curbuf->b_op_start = oap->start;
2359 curbuf->b_op_end = oap->end;
2360
2361 return OK;
2362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002364static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366/*
2367 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2368 */
2369 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002370op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002374 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 if (u_save((linenr_T)(oap->start.lnum - 1),
2377 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2378 return;
2379
2380 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (oap->block_mode) /* Visual block mode */
2382 {
2383 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2384 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002385 int one_change;
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 block_prep(oap, &bd, pos.lnum, FALSE);
2388 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002389 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2390 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002391
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002392#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002393 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
2395 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2396
Bram Moolenaar009b2592004-10-24 19:18:58 +00002397 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2398 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002400 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 if (did_change)
2405 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2406 }
2407 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
2409 if (oap->motion_type == MLINE)
2410 {
2411 oap->start.col = 0;
2412 pos.col = 0;
2413 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2414 if (oap->end.col)
2415 --oap->end.col;
2416 }
2417 else if (!oap->inclusive)
2418 dec(&(oap->end));
2419
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002420 if (pos.lnum == oap->end.lnum)
2421 did_change = swapchars(oap->op_type, &pos,
2422 oap->end.col - pos.col + 1);
2423 else
2424 for (;;)
2425 {
2426 did_change |= swapchars(oap->op_type, &pos,
2427 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2428 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002429 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002430 break;
2431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (did_change)
2433 {
2434 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2435 0L);
2436#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002437 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
2439 char_u *ptr;
2440 int count;
2441
2442 pos = oap->start;
2443 while (pos.lnum < oap->end.lnum)
2444 {
2445 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002446 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002447 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002449 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 pos.col = 0;
2451 pos.lnum++;
2452 }
2453 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2454 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002455 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002457 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459#endif
2460 }
2461 }
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (!did_change && oap->is_VIsual)
2464 /* No change: need to remove the Visual selection */
2465 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 /*
2468 * Set '[ and '] marks.
2469 */
2470 curbuf->b_op_start = oap->start;
2471 curbuf->b_op_end = oap->end;
2472
2473 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002474 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002475 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476}
2477
2478/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002479 * Invoke swapchar() on "length" bytes at position "pos".
2480 * "pos" is advanced to just after the changed characters.
2481 * "length" is rounded up to include the whole last multi-byte character.
2482 * Also works correctly when the number of bytes changes.
2483 * Returns TRUE if some character was changed.
2484 */
2485 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002486swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002487{
2488 int todo;
2489 int did_change = 0;
2490
2491 for (todo = length; todo > 0; --todo)
2492 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002493 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002494 {
2495 int len = (*mb_ptr2len)(ml_get_pos(pos));
2496
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002497 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002498 if (len > 0)
2499 todo -= len - 1;
2500 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002501 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002502 if (inc(pos) == -1) /* at end of file */
2503 break;
2504 }
2505 return did_change;
2506}
2507
2508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 * If op_type == OP_UPPER: make uppercase,
2510 * if op_type == OP_LOWER: make lowercase,
2511 * if op_type == OP_ROT13: do rot13 encoding,
2512 * else swap case of character at 'pos'
2513 * returns TRUE when something actually changed.
2514 */
2515 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002516swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
2518 int c;
2519 int nc;
2520
2521 c = gchar_pos(pos);
2522
2523 /* Only do rot13 encoding for ASCII characters. */
2524 if (c >= 0x80 && op_type == OP_ROT13)
2525 return FALSE;
2526
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002527 if (op_type == OP_UPPER && c == 0xdf
2528 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002529 {
2530 pos_T sp = curwin->w_cursor;
2531
2532 /* Special handling of German sharp s: change to "SS". */
2533 curwin->w_cursor = *pos;
2534 del_char(FALSE);
2535 ins_char('S');
2536 ins_char('S');
2537 curwin->w_cursor = sp;
2538 inc(pos);
2539 }
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2542 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 nc = c;
2544 if (MB_ISLOWER(c))
2545 {
2546 if (op_type == OP_ROT13)
2547 nc = ROT13(c, 'a');
2548 else if (op_type != OP_LOWER)
2549 nc = MB_TOUPPER(c);
2550 }
2551 else if (MB_ISUPPER(c))
2552 {
2553 if (op_type == OP_ROT13)
2554 nc = ROT13(c, 'A');
2555 else if (op_type != OP_UPPER)
2556 nc = MB_TOLOWER(c);
2557 }
2558 if (nc != c)
2559 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2561 {
2562 pos_T sp = curwin->w_cursor;
2563
2564 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002565 /* don't use del_char(), it also removes composing chars */
2566 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 ins_char(nc);
2568 curwin->w_cursor = sp;
2569 }
2570 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002571 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 return TRUE;
2573 }
2574 return FALSE;
2575}
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577/*
2578 * op_insert - Insert and append operators for Visual mode.
2579 */
2580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002581op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 long ins_len, pre_textlen = 0;
2584 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002585 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 struct block_def bd;
2587 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002588 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590 /* edit() changes this - record it for OP_APPEND */
2591 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2592
2593 /* vis block is still marked. Get rid of it now. */
2594 curwin->w_cursor.lnum = oap->start.lnum;
2595 update_screen(INVERTED);
2596
2597 if (oap->block_mode)
2598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 /* When 'virtualedit' is used, need to insert the extra spaces before
2600 * doing block_prep(). When only "block" is used, virtual edit is
2601 * already disabled, but still need it when calling
2602 * coladvance_force(). */
2603 if (curwin->w_cursor.coladd > 0)
2604 {
2605 int old_ve_flags = ve_flags;
2606
2607 ve_flags = VE_ALL;
2608 if (u_save_cursor() == FAIL)
2609 return;
2610 coladvance_force(oap->op_type == OP_APPEND
2611 ? oap->end_vcol + 1 : getviscol());
2612 if (oap->op_type == OP_APPEND)
2613 --curwin->w_cursor.col;
2614 ve_flags = old_ve_flags;
2615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 /* Get the info about the block before entering the text */
2617 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002618 /* Get indent information */
2619 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002621
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (oap->op_type == OP_APPEND)
2623 firstline += bd.textlen;
2624 pre_textlen = (long)STRLEN(firstline);
2625 }
2626
2627 if (oap->op_type == OP_APPEND)
2628 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002629 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 /* Move the cursor to the character right of the block. */
2632 curwin->w_set_curswant = TRUE;
2633 while (*ml_get_cursor() != NUL
2634 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2635 ++curwin->w_cursor.col;
2636 if (bd.is_short && !bd.is_MAX)
2637 {
2638 /* First line was too short, make it longer and adjust the
2639 * values in "bd". */
2640 if (u_save_cursor() == FAIL)
2641 return;
2642 for (i = 0; i < bd.endspaces; ++i)
2643 ins_char(' ');
2644 bd.textlen += bd.endspaces;
2645 }
2646 }
2647 else
2648 {
2649 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002650 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002653 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 && oap->start_vcol != oap->end_vcol)
2655 inc_cursor();
2656 }
2657 }
2658
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002659 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002660 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002662 /* When a tab was inserted, and the characters in front of the tab
2663 * have been converted to a tab as well, the column of the cursor
2664 * might have actually been reduced, so need to adjust here. */
2665 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002666 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002667 oap->start = curbuf->b_op_start_orig;
2668
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002669 /* If user has moved off this line, we don't know what to do, so do
2670 * nothing.
2671 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2672 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 return;
2674
2675 if (oap->block_mode)
2676 {
2677 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002678 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002679 size_t len;
2680 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002682 /* If indent kicked in, the firstline might have changed
2683 * but only do that, if the indent actually increased. */
2684 ind_post = (colnr_T)getwhitecols_curline();
2685 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2686 {
2687 bd.textcol += ind_post - ind_pre;
2688 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002689 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002690 }
2691
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002692 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002693 * to adjust the block for that. But only do it, if the difference
2694 * does not come from indent kicking in. */
2695 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2696 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002697 {
2698 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002699 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002700 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002701 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002702 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002703 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002704 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002705 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002706 pre_textlen -= t - oap->start_vcol;
2707 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002708 }
2709 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002710 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002711 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002712 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002713 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002714 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002715 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002716 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002717 /* reset pre_textlen to the value of OP_INSERT */
2718 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002719 pre_textlen -= t - oap->start_vcol;
2720 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002721 oap->op_type = OP_INSERT;
2722 }
2723 }
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 /*
2726 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002727 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 * Don't do this when "$" used, end-of-line will have changed.
2729 */
2730 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2731 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2732 {
2733 if (oap->op_type == OP_APPEND)
2734 {
2735 pre_textlen += bd2.textlen - bd.textlen;
2736 if (bd2.endspaces)
2737 --bd2.textlen;
2738 }
2739 bd.textcol = bd2.textcol;
2740 bd.textlen = bd2.textlen;
2741 }
2742
2743 /*
2744 * Subsequent calls to ml_get() flush the firstline data - take a
2745 * copy of the required string.
2746 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002747 firstline = ml_get(oap->start.lnum);
2748 len = STRLEN(firstline);
2749 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002751 add += bd.textlen;
2752 if ((size_t)add > len)
2753 firstline += len; // short line, point to the NUL
2754 else
2755 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002756 if (pre_textlen >= 0
2757 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
2759 ins_text = vim_strnsave(firstline, (int)ins_len);
2760 if (ins_text != NULL)
2761 {
2762 /* block handled here */
2763 if (u_save(oap->start.lnum,
2764 (linenr_T)(oap->end.lnum + 1)) == OK)
2765 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2766 &bd);
2767
2768 curwin->w_cursor.col = oap->start.col;
2769 check_cursor();
2770 vim_free(ins_text);
2771 }
2772 }
2773 }
2774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776/*
2777 * op_change - handle a change operation
2778 *
2779 * return TRUE if edit() returns because of a CTRL-O command
2780 */
2781 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002782op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 colnr_T l;
2785 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 long offset;
2787 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002788 long ins_len;
2789 long pre_textlen = 0;
2790 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 char_u *firstline;
2792 char_u *ins_text, *newp, *oldp;
2793 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 l = oap->start.col;
2796 if (oap->motion_type == MLINE)
2797 {
2798 l = 0;
2799#ifdef FEAT_SMARTINDENT
2800 if (!p_paste && curbuf->b_p_si
2801# ifdef FEAT_CINDENT
2802 && !curbuf->b_p_cin
2803# endif
2804 )
2805 can_si = TRUE; /* It's like opening a new line, do si */
2806#endif
2807 }
2808
2809 /* First delete the text in the region. In an empty buffer only need to
2810 * save for undo */
2811 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2812 {
2813 if (u_save_cursor() == FAIL)
2814 return FALSE;
2815 }
2816 else if (op_delete(oap) == FAIL)
2817 return FALSE;
2818
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002819 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 && !virtual_op)
2821 inc_cursor();
2822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 /* check for still on same line (<CR> in inserted text meaningless) */
2824 /* skip blank lines too */
2825 if (oap->block_mode)
2826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 /* Add spaces before getting the current line length. */
2828 if (virtual_op && (curwin->w_cursor.coladd > 0
2829 || gchar_cursor() == NUL))
2830 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00002831 firstline = ml_get(oap->start.lnum);
2832 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002833 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 bd.textcol = curwin->w_cursor.col;
2835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
2837#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2838 if (oap->motion_type == MLINE)
2839 fix_indent();
2840#endif
2841
2842 retval = edit(NUL, FALSE, (linenr_T)1);
2843
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002845 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002847 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002849 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002851 /* Auto-indenting may have changed the indent. If the cursor was past
2852 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002854 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002856 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002857
2858 pre_textlen += new_indent - pre_indent;
2859 bd.textcol += new_indent - pre_indent;
2860 }
2861
2862 ins_len = (long)STRLEN(firstline) - pre_textlen;
2863 if (ins_len > 0)
2864 {
2865 /* Subsequent calls to ml_get() flush the firstline data - take a
2866 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002867 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002869 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2871 linenr++)
2872 {
2873 block_prep(oap, &bd, linenr, TRUE);
2874 if (!bd.is_short || virtual_op)
2875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 pos_T vpos;
2877
2878 /* If the block starts in virtual space, count the
2879 * initial coladd offset as part of "startspaces" */
2880 if (bd.is_short)
2881 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002882 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 else
2886 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002888 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 if (newp == NULL)
2890 continue;
2891 /* copy up to block start */
2892 mch_memmove(newp, oldp, (size_t)bd.textcol);
2893 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002894 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2897 offset += ins_len;
2898 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002899 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 ml_replace(linenr, newp, FALSE);
2901 }
2902 }
2903 check_cursor();
2904
2905 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2906 }
2907 vim_free(ins_text);
2908 }
2909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 return retval;
2912}
2913
2914/*
2915 * set all the yank registers to empty (called from main())
2916 */
2917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002918init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
2920 int i;
2921
2922 for (i = 0; i < NUM_REGISTERS; ++i)
2923 y_regs[i].y_array = NULL;
2924}
2925
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002926#if defined(EXITFREE) || defined(PROTO)
2927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002928clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002929{
2930 int i;
2931
2932 for (i = 0; i < NUM_REGISTERS; ++i)
2933 {
2934 y_current = &y_regs[i];
2935 if (y_current->y_array != NULL)
2936 free_yank_all();
2937 }
2938}
2939#endif
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941/*
2942 * Free "n" lines from the current yank register.
2943 * Called for normal freeing and in case of error.
2944 */
2945 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002946free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 if (y_current->y_array != NULL)
2949 {
2950 long i;
2951
2952 for (i = n; --i >= 0; )
2953 {
2954#ifdef AMIGA /* only for very slow machines */
2955 if ((i & 1023) == 1023) /* this may take a while */
2956 {
2957 /*
2958 * This message should never cause a hit-return message.
2959 * Overwrite this message with any next message.
2960 */
2961 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002962 smsg(_("freeing %ld lines"), i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 --no_wait_return;
2964 msg_didout = FALSE;
2965 msg_col = 0;
2966 }
2967#endif
2968 vim_free(y_current->y_array[i]);
2969 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002970 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#ifdef AMIGA
2972 if (n >= 1000)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002973 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#endif
2975 }
2976}
2977
2978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002979free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 free_yank(y_current->y_size);
2982}
2983
2984/*
2985 * Yank the text between "oap->start" and "oap->end" into a yank register.
2986 * If we are to append (uppercase register), we first yank into a new yank
2987 * register and then concatenate the old and the new one (so we keep the old
2988 * one in case of out-of-memory).
2989 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002990 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 */
2992 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002993op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002996 yankreg_T *curr; /* copy of y_current */
2997 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 char_u **new_ptr;
2999 linenr_T lnum; /* current line number */
3000 long j;
3001 int yanktype = oap->motion_type;
3002 long yanklines = oap->line_count;
3003 linenr_T yankendlnum = oap->end.lnum;
3004 char_u *p;
3005 char_u *pnew;
3006 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003007#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003008 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 /* check for read-only register */
3012 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3013 {
3014 beep_flush();
3015 return FAIL;
3016 }
3017 if (oap->regname == '_') /* black hole: nothing to do */
3018 return OK;
3019
3020#ifdef FEAT_CLIPBOARD
3021 if (!clip_star.available && oap->regname == '*')
3022 oap->regname = 0;
3023 else if (!clip_plus.available && oap->regname == '+')
3024 oap->regname = 0;
3025#endif
3026
3027 if (!deleting) /* op_delete() already set y_current */
3028 get_yank_register(oap->regname, TRUE);
3029
3030 curr = y_current;
3031 /* append to existing contents */
3032 if (y_append && y_current->y_array != NULL)
3033 y_current = &newreg;
3034 else
3035 free_yank_all(); /* free previously yanked lines */
3036
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003037 /*
3038 * If the cursor was in column 1 before and after the movement, and the
3039 * operator is not inclusive, the yank is always linewise.
3040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if ( oap->motion_type == MCHAR
3042 && oap->start.col == 0
3043 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003045 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 && oap->end.col == 0
3047 && yanklines > 1)
3048 {
3049 yanktype = MLINE;
3050 --yankendlnum;
3051 --yanklines;
3052 }
3053
3054 y_current->y_size = yanklines;
3055 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 y_current->y_width = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003057 y_current->y_array = lalloc_clear(sizeof(char_u *) * yanklines, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 if (y_current->y_array == NULL)
3059 {
3060 y_current = curr;
3061 return FAIL;
3062 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003063#ifdef FEAT_VIMINFO
3064 y_current->y_time_set = vim_time();
3065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
3067 y_idx = 0;
3068 lnum = oap->start.lnum;
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (oap->block_mode)
3071 {
3072 /* Visual block mode */
3073 y_current->y_type = MBLOCK; /* set the yank register type */
3074 y_current->y_width = oap->end_vcol - oap->start_vcol;
3075
3076 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3077 y_current->y_width--;
3078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3081 {
3082 switch (y_current->y_type)
3083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 case MBLOCK:
3085 block_prep(oap, &bd, lnum, FALSE);
3086 if (yank_copy_line(&bd, y_idx) == FAIL)
3087 goto fail;
3088 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 case MLINE:
3091 if ((y_current->y_array[y_idx] =
3092 vim_strsave(ml_get(lnum))) == NULL)
3093 goto fail;
3094 break;
3095
3096 case MCHAR:
3097 {
3098 colnr_T startcol = 0, endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 int is_oneChar = FALSE;
3100 colnr_T cs, ce;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 p = ml_get(lnum);
3103 bd.startspaces = 0;
3104 bd.endspaces = 0;
3105
3106 if (lnum == oap->start.lnum)
3107 {
3108 startcol = oap->start.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (virtual_op)
3110 {
3111 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3112 if (ce != cs && oap->start.coladd > 0)
3113 {
3114 /* Part of a tab selected -- but don't
3115 * double-count it. */
3116 bd.startspaces = (ce - cs + 1)
3117 - oap->start.coladd;
3118 startcol++;
3119 }
3120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122
3123 if (lnum == oap->end.lnum)
3124 {
3125 endcol = oap->end.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (virtual_op)
3127 {
3128 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3129 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 /* Don't add space for double-wide
3131 * char; endcol will be on last byte
3132 * of multi-byte char. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003133 && (*mb_head_off)(p, p + endcol) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
3135 if (oap->start.lnum == oap->end.lnum
3136 && oap->start.col == oap->end.col)
3137 {
3138 /* Special case: inside a single char */
3139 is_oneChar = TRUE;
3140 bd.startspaces = oap->end.coladd
3141 - oap->start.coladd + oap->inclusive;
3142 endcol = startcol;
3143 }
3144 else
3145 {
3146 bd.endspaces = oap->end.coladd
3147 + oap->inclusive;
3148 endcol -= oap->inclusive;
3149 }
3150 }
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003153 if (endcol == MAXCOL)
3154 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003155 if (startcol > endcol || is_oneChar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 bd.textlen = 0;
3157 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 bd.textlen = endcol - startcol + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 bd.textstart = p + startcol;
3160 if (yank_copy_line(&bd, y_idx) == FAIL)
3161 goto fail;
3162 break;
3163 }
3164 /* NOTREACHED */
3165 }
3166 }
3167
3168 if (curr != y_current) /* append the new block to the old block */
3169 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003170 new_ptr = ALLOC_MULT(char_u *, curr->y_size + y_current->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (new_ptr == NULL)
3172 goto fail;
3173 for (j = 0; j < curr->y_size; ++j)
3174 new_ptr[j] = curr->y_array[j];
3175 vim_free(curr->y_array);
3176 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003177#ifdef FEAT_VIMINFO
3178 curr->y_time_set = vim_time();
3179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3182 curr->y_type = MLINE;
3183
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003184 /* Concatenate the last line of the old block with the first line of
3185 * the new block, unless being Vi compatible. */
3186 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02003188 pnew = alloc(STRLEN(curr->y_array[curr->y_size - 1])
3189 + STRLEN(y_current->y_array[0]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (pnew == NULL)
3191 {
3192 y_idx = y_current->y_size - 1;
3193 goto fail;
3194 }
3195 STRCPY(pnew, curr->y_array[--j]);
3196 STRCAT(pnew, y_current->y_array[0]);
3197 vim_free(curr->y_array[j]);
3198 vim_free(y_current->y_array[0]);
3199 curr->y_array[j++] = pnew;
3200 y_idx = 1;
3201 }
3202 else
3203 y_idx = 0;
3204 while (y_idx < y_current->y_size)
3205 curr->y_array[j++] = y_current->y_array[y_idx++];
3206 curr->y_size = j;
3207 vim_free(y_current->y_array);
3208 y_current = curr;
3209 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003210 if (curwin->w_p_rnu)
3211 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (mess) /* Display message about yank? */
3213 {
3214 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 && yanklines == 1)
3217 yanklines = 0;
3218 /* Some versions of Vi use ">=" here, some don't... */
3219 if (yanklines > p_report)
3220 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003221 char namebuf[100];
3222
3223 if (oap->regname == NUL)
3224 *namebuf = NUL;
3225 else
3226 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003227 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 /* redisplay now, so message is not deleted */
3230 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003231 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003233 smsg(NGETTEXT("block of %ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003234 "block of %ld lines yanked%s", yanklines),
3235 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003239 smsg(NGETTEXT("%ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003240 "%ld lines yanked%s", yanklines),
3241 yanklines, namebuf);
3242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244 }
3245
3246 /*
3247 * Set "'[" and "']" marks.
3248 */
3249 curbuf->b_op_start = oap->start;
3250 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003251 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003252 {
3253 curbuf->b_op_start.col = 0;
3254 curbuf->b_op_end.col = MAXCOL;
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
3257#ifdef FEAT_CLIPBOARD
3258 /*
3259 * If we were yanking to the '*' register, send result to clipboard.
3260 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3261 * to the '*' register.
3262 */
3263 if (clip_star.available
3264 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003265 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003266 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 if (curr != &(y_regs[STAR_REGISTER]))
3269 /* Copy the text from register 0 to the clipboard register. */
3270 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3271
3272 clip_own_selection(&clip_star);
3273 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003274# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003275 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278
3279# ifdef FEAT_X11
3280 /*
3281 * If we were yanking to the '+' register, send result to selection.
3282 * Also copy to the '*' register, in case auto-select is off.
3283 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003284 if (clip_plus.available
3285 && (curr == &(y_regs[PLUS_REGISTER])
3286 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003287 && ((clip_unnamed | clip_unnamed_saved) &
3288 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003290 if (curr != &(y_regs[PLUS_REGISTER]))
3291 /* Copy the text from register 0 to the clipboard register. */
3292 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 clip_own_selection(&clip_plus);
3295 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003296 if (!clip_isautosel_star() && !clip_isautosel_plus()
3297 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
3299 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3300 clip_own_selection(&clip_star);
3301 clip_gen_set_selection(&clip_star);
3302 }
3303 }
3304# endif
3305#endif
3306
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003307#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003308 if (!deleting && has_textyankpost())
3309 yank_do_autocmd(oap, y_current);
3310#endif
3311
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 return OK;
3313
3314fail: /* free the allocated lines */
3315 free_yank(y_idx + 1);
3316 y_current = curr;
3317 return FAIL;
3318}
3319
3320 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003321yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 char_u *pnew;
3324
3325 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3326 == NULL)
3327 return FAIL;
3328 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003329 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 pnew += bd->startspaces;
3331 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3332 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003333 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 pnew += bd->endspaces;
3335 *pnew = NUL;
3336 return OK;
3337}
3338
3339#ifdef FEAT_CLIPBOARD
3340/*
3341 * Make a copy of the y_current register to register "reg".
3342 */
3343 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003344copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003346 yankreg_T *curr = y_current;
3347 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
3349 y_current = reg;
3350 free_yank_all();
3351 *y_current = *curr;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003352 y_current->y_array = lalloc_clear(
Bram Moolenaar51e14382019-05-25 20:21:28 +02003353 sizeof(char_u *) * y_current->y_size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (y_current->y_array == NULL)
3355 y_current->y_size = 0;
3356 else
3357 for (j = 0; j < y_current->y_size; ++j)
3358 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3359 {
3360 free_yank(j);
3361 y_current->y_size = 0;
3362 break;
3363 }
3364 y_current = curr;
3365}
3366#endif
3367
3368/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003369 * Put contents of register "regname" into the text.
3370 * Caller must check "regname" to be valid!
3371 * "flags": PUT_FIXINDENT make indent look nice
3372 * PUT_CURSEND leave cursor after end of new text
3373 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 */
3375 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003376do_put(
3377 int regname,
3378 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3379 long count,
3380 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 char_u *ptr;
3383 char_u *newp, *oldp;
3384 int yanklen;
3385 int totlen = 0; /* init for gcc */
3386 linenr_T lnum;
3387 colnr_T col;
3388 long i; /* index in y_array[] */
3389 int y_type;
3390 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 int oldlen;
3392 long y_width = 0;
3393 colnr_T vcol;
3394 int delcount;
3395 int incr = 0;
3396 long j;
3397 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 char_u **y_array = NULL;
3399 long nr_lines = 0;
3400 pos_T new_cursor;
3401 int indent;
3402 int orig_indent = 0; /* init for gcc */
3403 int indent_diff = 0; /* init for gcc */
3404 int first_indent = TRUE;
3405 int lendiff = 0;
3406 pos_T old_pos;
3407 char_u *insert_string = NULL;
3408 int allocated = FALSE;
3409 long cnt;
3410
3411#ifdef FEAT_CLIPBOARD
3412 /* Adjust register name for "unnamed" in 'clipboard'. */
3413 adjust_clip_reg(&regname);
3414 (void)may_get_selection(regname);
3415#endif
3416
3417 if (flags & PUT_FIXINDENT)
3418 orig_indent = get_indent();
3419
3420 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3421 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3422
3423 /*
3424 * Using inserted text works differently, because the register includes
3425 * special characters (newlines, etc.).
3426 */
3427 if (regname == '.')
3428 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003429 if (VIsual_active)
3430 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3432 (count == -1 ? 'O' : 'i')), count, FALSE);
3433 /* Putting the text is done later, so can't really move the cursor to
3434 * the next character. Use "l" to simulate it. */
3435 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3436 stuffcharReadbuff('l');
3437 return;
3438 }
3439
3440 /*
3441 * For special registers '%' (file name), '#' (alternate file name) and
3442 * ':' (last command line), etc. we have to create a fake yank register.
3443 */
3444 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3445 {
3446 if (insert_string == NULL)
3447 return;
3448 }
3449
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003450 /* Autocommands may be executed when saving lines for undo. This might
3451 * make "y_array" invalid, so we start undo now to avoid that. */
3452 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3453 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (insert_string != NULL)
3456 {
3457 y_type = MCHAR;
3458#ifdef FEAT_EVAL
3459 if (regname == '=')
3460 {
3461 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003462 * characters.
3463 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 for (;;)
3465 {
3466 y_size = 0;
3467 ptr = insert_string;
3468 while (ptr != NULL)
3469 {
3470 if (y_array != NULL)
3471 y_array[y_size] = ptr;
3472 ++y_size;
3473 ptr = vim_strchr(ptr, '\n');
3474 if (ptr != NULL)
3475 {
3476 if (y_array != NULL)
3477 *ptr = NUL;
3478 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003479 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (*ptr == NUL)
3481 {
3482 y_type = MLINE;
3483 break;
3484 }
3485 }
3486 }
3487 if (y_array != NULL)
3488 break;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003489 y_array = ALLOC_MULT(char_u *, y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (y_array == NULL)
3491 goto end;
3492 }
3493 }
3494 else
3495#endif
3496 {
3497 y_size = 1; /* use fake one-line yank register */
3498 y_array = &insert_string;
3499 }
3500 }
3501 else
3502 {
3503 get_yank_register(regname, FALSE);
3504
3505 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 y_size = y_current->y_size;
3508 y_array = y_current->y_array;
3509 }
3510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (y_type == MLINE)
3512 {
3513 if (flags & PUT_LINE_SPLIT)
3514 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003515 char_u *p;
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 /* "p" or "P" in Visual mode: split the lines to put the text in
3518 * between. */
3519 if (u_save_cursor() == FAIL)
3520 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003521 p = ml_get_cursor();
3522 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003523 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003524 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 if (ptr == NULL)
3526 goto end;
3527 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3528 vim_free(ptr);
3529
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003530 oldp = ml_get_curline();
3531 p = oldp + curwin->w_cursor.col;
3532 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003533 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003534 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (ptr == NULL)
3536 goto end;
3537 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3538 ++nr_lines;
3539 dir = FORWARD;
3540 }
3541 if (flags & PUT_LINE_FORWARD)
3542 {
3543 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003544 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 dir = FORWARD;
3546 }
3547 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3548 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3552 y_type = MLINE;
3553
3554 if (y_size == 0 || y_array == NULL)
3555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003556 semsg(_("E353: Nothing in register %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 regname == 0 ? (char_u *)"\"" : transchar(regname));
3558 goto end;
3559 }
3560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (y_type == MBLOCK)
3562 {
3563 lnum = curwin->w_cursor.lnum + y_size + 1;
3564 if (lnum > curbuf->b_ml.ml_line_count)
3565 lnum = curbuf->b_ml.ml_line_count + 1;
3566 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3567 goto end;
3568 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003569 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 lnum = curwin->w_cursor.lnum;
3572#ifdef FEAT_FOLDING
3573 /* Correct line number for closed fold. Don't move the cursor yet,
3574 * u_save() uses it. */
3575 if (dir == BACKWARD)
3576 (void)hasFolding(lnum, &lnum, NULL);
3577 else
3578 (void)hasFolding(lnum, NULL, &lnum);
3579#endif
3580 if (dir == FORWARD)
3581 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003582 /* In an empty buffer the empty line is going to be replaced, include
3583 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003584 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 goto end;
3586#ifdef FEAT_FOLDING
3587 if (dir == FORWARD)
3588 curwin->w_cursor.lnum = lnum - 1;
3589 else
3590 curwin->w_cursor.lnum = lnum;
3591 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3592#endif
3593 }
3594 else if (u_save_cursor() == FAIL)
3595 goto end;
3596
3597 yanklen = (int)STRLEN(y_array[0]);
3598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (ve_flags == VE_ALL && y_type == MCHAR)
3600 {
3601 if (gchar_cursor() == TAB)
3602 {
3603 /* Don't need to insert spaces when "p" on the last position of a
3604 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003605#ifdef FEAT_VARTABS
3606 int viscol = getviscol();
3607 if (dir == FORWARD
3608 ? tabstop_padding(viscol, curbuf->b_p_ts,
3609 curbuf->b_p_vts_array) != 1
3610 : curwin->w_cursor.coladd > 0)
3611 coladvance_force(viscol);
3612#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (dir == FORWARD
3614 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3615 : curwin->w_cursor.coladd > 0)
3616 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 else
3619 curwin->w_cursor.coladd = 0;
3620 }
3621 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3622 coladvance_force(getviscol() + (dir == FORWARD));
3623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
3625 lnum = curwin->w_cursor.lnum;
3626 col = curwin->w_cursor.col;
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 /*
3629 * Block mode
3630 */
3631 if (y_type == MBLOCK)
3632 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003633 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 colnr_T endcol2 = 0;
3635
3636 if (dir == FORWARD && c != NUL)
3637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if (ve_flags == VE_ALL)
3639 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (has_mbyte)
3644 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003645 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (c != TAB || ve_flags != VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 ++curwin->w_cursor.col;
3649 ++col;
3650 }
3651 else
3652 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003655 if (ve_flags == VE_ALL
3656 && (curwin->w_cursor.coladd > 0
3657 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 if (dir == FORWARD && c == NUL)
3660 ++col;
3661 if (dir != FORWARD && c != NUL)
3662 ++curwin->w_cursor.col;
3663 if (c == TAB)
3664 {
3665 if (dir == BACKWARD && curwin->w_cursor.col)
3666 curwin->w_cursor.col--;
3667 if (dir == FORWARD && col - 1 == endcol2)
3668 curwin->w_cursor.col++;
3669 }
3670 }
3671 curwin->w_cursor.coladd = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003672 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 for (i = 0; i < y_size; ++i)
3674 {
3675 int spaces;
3676 char shortline;
3677
3678 bd.startspaces = 0;
3679 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 vcol = 0;
3681 delcount = 0;
3682
3683 /* add a new line */
3684 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3685 {
3686 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3687 (colnr_T)1, FALSE) == FAIL)
3688 break;
3689 ++nr_lines;
3690 }
3691 /* get the old line and advance to the position to insert at */
3692 oldp = ml_get_curline();
3693 oldlen = (int)STRLEN(oldp);
3694 for (ptr = oldp; vcol < col && *ptr; )
3695 {
3696 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003697 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 vcol += incr;
3699 }
3700 bd.textcol = (colnr_T)(ptr - oldp);
3701
3702 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3703
3704 if (vcol < col) /* line too short, padd with spaces */
3705 bd.startspaces = col - vcol;
3706 else if (vcol > col)
3707 {
3708 bd.endspaces = vcol - col;
3709 bd.startspaces = incr - bd.endspaces;
3710 --bd.textcol;
3711 delcount = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 if (has_mbyte)
3713 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (oldp[bd.textcol] != TAB)
3715 {
3716 /* Only a Tab can be split into spaces. Other
3717 * characters will have to be moved to after the
3718 * block, causing misalignment. */
3719 delcount = 0;
3720 bd.endspaces = 0;
3721 }
3722 }
3723
3724 yanklen = (int)STRLEN(y_array[i]);
3725
3726 /* calculate number of spaces required to fill right side of block*/
3727 spaces = y_width + 1;
3728 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003729 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (spaces < 0)
3731 spaces = 0;
3732
3733 /* insert the new text */
3734 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003735 newp = alloc(totlen + oldlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (newp == NULL)
3737 break;
3738 /* copy part up to cursor to new line */
3739 ptr = newp;
3740 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3741 ptr += bd.textcol;
3742 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003743 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 ptr += bd.startspaces;
3745 /* insert the new text */
3746 for (j = 0; j < count; ++j)
3747 {
3748 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3749 ptr += yanklen;
3750
3751 /* insert block's trailing spaces only if there's text behind */
3752 if ((j < count - 1 || !shortline) && spaces)
3753 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003754 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 ptr += spaces;
3756 }
3757 }
3758 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003759 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 ptr += bd.endspaces;
3761 /* move the text after the cursor to the end of the line. */
3762 mch_memmove(ptr, oldp + bd.textcol + delcount,
3763 (size_t)(oldlen - bd.textcol - delcount + 1));
3764 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3765
3766 ++curwin->w_cursor.lnum;
3767 if (i == 0)
3768 curwin->w_cursor.col += bd.startspaces;
3769 }
3770
3771 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3772
3773 /* Set '[ mark. */
3774 curbuf->b_op_start = curwin->w_cursor;
3775 curbuf->b_op_start.lnum = lnum;
3776
3777 /* adjust '] mark */
3778 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3779 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 curbuf->b_op_end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (flags & PUT_CURSEND)
3782 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003783 colnr_T len;
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 curwin->w_cursor = curbuf->b_op_end;
3786 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003787
3788 /* in Insert mode we might be after the NUL, correct for that */
3789 len = (colnr_T)STRLEN(ml_get_curline());
3790 if (curwin->w_cursor.col > len)
3791 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 else
3794 curwin->w_cursor.lnum = lnum;
3795 }
3796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
3798 /*
3799 * Character or Line mode
3800 */
3801 if (y_type == MCHAR)
3802 {
3803 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3804 * char */
3805 if (dir == FORWARD && gchar_cursor() != NUL)
3806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (has_mbyte)
3808 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003809 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 /* put it on the next of the multi-byte character. */
3812 col += bytelen;
3813 if (yanklen)
3814 {
3815 curwin->w_cursor.col += bytelen;
3816 curbuf->b_op_end.col += bytelen;
3817 }
3818 }
3819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
3821 ++col;
3822 if (yanklen)
3823 {
3824 ++curwin->w_cursor.col;
3825 ++curbuf->b_op_end.col;
3826 }
3827 }
3828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 curbuf->b_op_start = curwin->w_cursor;
3830 }
3831 /*
3832 * Line mode: BACKWARD is the same as FORWARD on the previous line
3833 */
3834 else if (dir == BACKWARD)
3835 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003836 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
3838 /*
3839 * simple case: insert into current line
3840 */
3841 if (y_type == MCHAR && y_size == 1)
3842 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003843 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003844
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003845 if (VIsual_active)
3846 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003847 end_lnum = curbuf->b_visual.vi_end.lnum;
3848 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3849 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003850 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003851
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003852 do {
3853 totlen = count * yanklen;
3854 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003856 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003857 if (VIsual_active && col > (int)STRLEN(oldp))
3858 {
3859 lnum++;
3860 continue;
3861 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02003862 newp = alloc(STRLEN(oldp) + totlen + 1);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003863 if (newp == NULL)
3864 goto end; /* alloc() gave an error message */
3865 mch_memmove(newp, oldp, (size_t)col);
3866 ptr = newp + col;
3867 for (i = 0; i < count; ++i)
3868 {
3869 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3870 ptr += yanklen;
3871 }
3872 STRMOVE(ptr, oldp + col);
3873 ml_replace(lnum, newp, FALSE);
3874 /* Place cursor on last putted char. */
3875 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003876 {
3877 /* make sure curwin->w_virtcol is updated */
3878 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003879 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003882 if (VIsual_active)
3883 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003884 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003885
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003886 if (VIsual_active) /* reset lnum to the last visual line */
3887 lnum--;
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 curbuf->b_op_end = curwin->w_cursor;
3890 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3891 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3892 ++curwin->w_cursor.col;
3893 changed_bytes(lnum, col);
3894 }
3895 else
3896 {
3897 /*
3898 * Insert at least one line. When y_type is MCHAR, break the first
3899 * line in two.
3900 */
3901 for (cnt = 1; cnt <= count; ++cnt)
3902 {
3903 i = 0;
3904 if (y_type == MCHAR)
3905 {
3906 /*
3907 * Split the current line in two at the insert position.
3908 * First insert y_array[size - 1] in front of second line.
3909 * Then append y_array[0] to first line.
3910 */
3911 lnum = new_cursor.lnum;
3912 ptr = ml_get(lnum) + col;
3913 totlen = (int)STRLEN(y_array[y_size - 1]);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003914 newp = alloc(STRLEN(ptr) + totlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (newp == NULL)
3916 goto error;
3917 STRCPY(newp, y_array[y_size - 1]);
3918 STRCAT(newp, ptr);
3919 /* insert second line */
3920 ml_append(lnum, newp, (colnr_T)0, FALSE);
3921 vim_free(newp);
3922
3923 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003924 newp = alloc(col + yanklen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if (newp == NULL)
3926 goto error;
3927 /* copy first part of line */
3928 mch_memmove(newp, oldp, (size_t)col);
3929 /* append to first line */
3930 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3931 ml_replace(lnum, newp, FALSE);
3932
3933 curwin->w_cursor.lnum = lnum;
3934 i = 1;
3935 }
3936
3937 for (; i < y_size; ++i)
3938 {
3939 if ((y_type != MCHAR || i < y_size - 1)
3940 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3941 == FAIL)
3942 goto error;
3943 lnum++;
3944 ++nr_lines;
3945 if (flags & PUT_FIXINDENT)
3946 {
3947 old_pos = curwin->w_cursor;
3948 curwin->w_cursor.lnum = lnum;
3949 ptr = ml_get(lnum);
3950 if (cnt == count && i == y_size - 1)
3951 lendiff = (int)STRLEN(ptr);
3952#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3953 if (*ptr == '#' && preprocs_left())
3954 indent = 0; /* Leave # lines at start */
3955 else
3956#endif
3957 if (*ptr == NUL)
3958 indent = 0; /* Ignore empty lines */
3959 else if (first_indent)
3960 {
3961 indent_diff = orig_indent - get_indent();
3962 indent = orig_indent;
3963 first_indent = FALSE;
3964 }
3965 else if ((indent = get_indent() + indent_diff) < 0)
3966 indent = 0;
3967 (void)set_indent(indent, 0);
3968 curwin->w_cursor = old_pos;
3969 /* remember how many chars were removed */
3970 if (cnt == count && i == y_size - 1)
3971 lendiff -= (int)STRLEN(ml_get(lnum));
3972 }
3973 }
3974 }
3975
3976error:
3977 /* Adjust marks. */
3978 if (y_type == MLINE)
3979 {
3980 curbuf->b_op_start.col = 0;
3981 if (dir == FORWARD)
3982 curbuf->b_op_start.lnum++;
3983 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003984 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003985 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02003986 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003987 < curbuf->b_ml.ml_line_count
3988#ifdef FEAT_DIFF
3989 || curwin->w_p_diff
3990#endif
3991 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003992 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 (linenr_T)MAXLNUM, nr_lines, 0L);
3994
3995 /* note changed text for displaying and folding */
3996 if (y_type == MCHAR)
3997 changed_lines(curwin->w_cursor.lnum, col,
3998 curwin->w_cursor.lnum + 1, nr_lines);
3999 else
4000 changed_lines(curbuf->b_op_start.lnum, 0,
4001 curbuf->b_op_start.lnum, nr_lines);
4002
4003 /* put '] mark at last inserted character */
4004 curbuf->b_op_end.lnum = lnum;
4005 /* correct length for change in indent */
4006 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4007 if (col > 1)
4008 curbuf->b_op_end.col = col - 1;
4009 else
4010 curbuf->b_op_end.col = 0;
4011
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004012 if (flags & PUT_CURSLINE)
4013 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004014 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004015 curwin->w_cursor.lnum = lnum;
4016 beginline(BL_WHITE | BL_FIX);
4017 }
4018 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
4020 /* put cursor after inserted text */
4021 if (y_type == MLINE)
4022 {
4023 if (lnum >= curbuf->b_ml.ml_line_count)
4024 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4025 else
4026 curwin->w_cursor.lnum = lnum + 1;
4027 curwin->w_cursor.col = 0;
4028 }
4029 else
4030 {
4031 curwin->w_cursor.lnum = lnum;
4032 curwin->w_cursor.col = col;
4033 }
4034 }
4035 else if (y_type == MLINE)
4036 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004037 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 curwin->w_cursor.col = 0;
4039 if (dir == FORWARD)
4040 ++curwin->w_cursor.lnum;
4041 beginline(BL_WHITE | BL_FIX);
4042 }
4043 else /* put cursor on first inserted character */
4044 curwin->w_cursor = new_cursor;
4045 }
4046 }
4047
4048 msgmore(nr_lines);
4049 curwin->w_set_curswant = TRUE;
4050
4051end:
4052 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004054 if (regname == '=')
4055 vim_free(y_array);
4056
Bram Moolenaar033d8882013-09-25 23:24:57 +02004057 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004058
Bram Moolenaar677ee682005-01-27 14:41:15 +00004059 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004060 adjust_cursor_eol();
4061}
4062
4063/*
4064 * When the cursor is on the NUL past the end of the line and it should not be
4065 * there move it left.
4066 */
4067 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004068adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004069{
4070 if (curwin->w_cursor.col > 0
4071 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004072 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 && !(restart_edit || (State & INSERT)))
4074 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004075 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004076 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004079 {
4080 colnr_T scol, ecol;
4081
4082 /* Coladd is set to the width of the last character. */
4083 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4084 curwin->w_cursor.coladd = ecol - scol + 1;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087}
4088
4089#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4090/*
4091 * Return TRUE if lines starting with '#' should be left aligned.
4092 */
4093 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004094preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
4096 return
4097# ifdef FEAT_SMARTINDENT
4098# ifdef FEAT_CINDENT
4099 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4100# else
4101 curbuf->b_p_si
4102# endif
4103# endif
4104# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004105 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4106 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107# endif
4108 ;
4109}
4110#endif
4111
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004112/*
4113 * Return the character name of the register with the given number.
4114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004116get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
4118 if (num == -1)
4119 return '"';
4120 else if (num < 10)
4121 return num + '0';
4122 else if (num == DELETION_REGISTER)
4123 return '-';
4124#ifdef FEAT_CLIPBOARD
4125 else if (num == STAR_REGISTER)
4126 return '*';
4127 else if (num == PLUS_REGISTER)
4128 return '+';
4129#endif
4130 else
4131 {
4132#ifdef EBCDIC
4133 int i;
4134
4135 /* EBCDIC is really braindead ... */
4136 i = 'a' + (num - 10);
4137 if (i > 'i')
4138 i += 7;
4139 if (i > 'r')
4140 i += 8;
4141 return i;
4142#else
4143 return num + 'a' - 10;
4144#endif
4145 }
4146}
4147
4148/*
4149 * ":dis" and ":registers": Display the contents of the yank registers.
4150 */
4151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004152ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004154 int i, n;
4155 long j;
4156 char_u *p;
4157 yankreg_T *yb;
4158 int name;
4159 int attr;
4160 char_u *arg = eap->arg;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004161 int clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
4163 if (arg != NULL && *arg == NUL)
4164 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004165 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
4167 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004168 msg_puts_title(_("\n--- Registers ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4170 {
4171 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004172 if (arg != NULL && vim_strchr(arg, name) == NULL
4173#ifdef ONE_CLIPBOARD
4174 /* Star register and plus register contain the same thing. */
4175 && (name != '*' || vim_strchr(arg, '+') == NULL)
4176#endif
4177 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 continue; /* did not ask for this register */
4179
4180#ifdef FEAT_CLIPBOARD
4181 /* Adjust register name for "unnamed" in 'clipboard'.
4182 * When it's a clipboard register, fill it with the current contents
4183 * of the clipboard. */
4184 adjust_clip_reg(&name);
4185 (void)may_get_selection(name);
4186#endif
4187
4188 if (i == -1)
4189 {
4190 if (y_previous != NULL)
4191 yb = y_previous;
4192 else
4193 yb = &(y_regs[0]);
4194 }
4195 else
4196 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004197
4198#ifdef FEAT_EVAL
4199 if (name == MB_TOLOWER(redir_reg)
4200 || (redir_reg == '"' && yb == y_previous))
4201 continue; /* do not list register being written to, the
4202 * pointer can be freed */
4203#endif
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (yb->y_array != NULL)
4206 {
4207 msg_putchar('\n');
4208 msg_putchar('"');
4209 msg_putchar(name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004210 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212 n = (int)Columns - 6;
4213 for (j = 0; j < yb->y_size && n > 1; ++j)
4214 {
4215 if (j)
4216 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004217 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 n -= 2;
4219 }
4220 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4221 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004222 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004223 msg_outtrans_len(p, clen);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004224 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 }
4227 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004228 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 out_flush(); /* show one line at a time */
4230 }
4231 ui_breakcheck();
4232 }
4233
4234 /*
4235 * display last inserted text
4236 */
4237 if ((p = get_last_insert()) != NULL
4238 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4239 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004240 msg_puts("\n\". ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 dis_msg(p, TRUE);
4242 }
4243
4244 /*
4245 * display last command line
4246 */
4247 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4248 && !got_int)
4249 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004250 msg_puts("\n\": ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 dis_msg(last_cmdline, FALSE);
4252 }
4253
4254 /*
4255 * display current file name
4256 */
4257 if (curbuf->b_fname != NULL
4258 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4259 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004260 msg_puts("\n\"% ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 dis_msg(curbuf->b_fname, FALSE);
4262 }
4263
4264 /*
4265 * display alternate file name
4266 */
4267 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4268 {
4269 char_u *fname;
4270 linenr_T dummy;
4271
4272 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4273 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004274 msg_puts("\n\"# ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 dis_msg(fname, FALSE);
4276 }
4277 }
4278
4279 /*
4280 * display last search pattern
4281 */
4282 if (last_search_pat() != NULL
4283 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4284 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004285 msg_puts("\n\"/ ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 dis_msg(last_search_pat(), FALSE);
4287 }
4288
4289#ifdef FEAT_EVAL
4290 /*
4291 * display last used expression
4292 */
4293 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4294 && !got_int)
4295 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004296 msg_puts("\n\"= ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 dis_msg(expr_line, FALSE);
4298 }
4299#endif
4300}
4301
4302/*
4303 * display a string for do_dis()
4304 * truncate at end of screen line
4305 */
4306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004307dis_msg(
4308 char_u *p,
4309 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
4311 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 n = (int)Columns - 6;
4315 while (*p != NUL
4316 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4317 && (n -= ptr2cells(p)) >= 0)
4318 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004319 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 msg_outtrans_len(p, l);
4322 p += l;
4323 }
4324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 msg_outtrans_len(p++, 1);
4326 }
4327 ui_breakcheck();
4328}
4329
Bram Moolenaar81340392012-06-06 16:12:59 +02004330#if defined(FEAT_COMMENTS) || defined(PROTO)
4331/*
4332 * If "process" is TRUE and the line begins with a comment leader (possibly
4333 * after some white space), return a pointer to the text after it. Put a boolean
4334 * value indicating whether the line ends with an unclosed comment in
4335 * "is_comment".
4336 * line - line to be processed,
4337 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004338 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004339 * include_space - whether to also skip space following the comment leader,
4340 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004341 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004342 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004343 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004344skip_comment(
4345 char_u *line,
4346 int process,
4347 int include_space,
4348 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004349{
4350 char_u *comment_flags = NULL;
4351 int lead_len;
4352 int leader_offset = get_last_leader_offset(line, &comment_flags);
4353
4354 *is_comment = FALSE;
4355 if (leader_offset != -1)
4356 {
4357 /* Let's check whether the line ends with an unclosed comment.
4358 * If the last comment leader has COM_END in flags, there's no comment.
4359 */
4360 while (*comment_flags)
4361 {
4362 if (*comment_flags == COM_END
4363 || *comment_flags == ':')
4364 break;
4365 ++comment_flags;
4366 }
4367 if (*comment_flags != COM_END)
4368 *is_comment = TRUE;
4369 }
4370
4371 if (process == FALSE)
4372 return line;
4373
4374 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4375
4376 if (lead_len == 0)
4377 return line;
4378
4379 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004380 * - COM_END,
4381 * - colon,
4382 * whichever comes first.
4383 */
4384 while (*comment_flags)
4385 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004386 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004387 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02004388 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02004389 ++comment_flags;
4390 }
4391
4392 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004393 * starting with a closing part of a three-part comment. That's good,
4394 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004395 */
4396 if (*comment_flags == ':' || *comment_flags == NUL)
4397 line += lead_len;
4398
4399 return line;
4400}
4401#endif
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004404 * Join 'count' lines (minimal 2) at cursor position.
4405 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004406 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4407 * leaders should not be removed.
4408 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4409 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004411 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 */
4413 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004414do_join(
4415 long count,
4416 int insert_space,
4417 int save_undo,
4418 int use_formatoptions UNUSED,
4419 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004421 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004422 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004423 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004425 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004426 int endcurr1 = NUL;
4427 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004428 int currsize = 0; /* size of the current line */
4429 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004431 colnr_T col = 0;
4432 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004433#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004434 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004435 int remove_comments = (use_formatoptions == TRUE)
4436 && has_format_option(FO_REMOVE_COMS);
4437 int prev_was_comment;
4438#endif
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004439#ifdef FEAT_TEXT_PROP
4440 textprop_T **prop_lines = NULL;
4441 int *prop_lengths = NULL;
4442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004444 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4445 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004448 /* Allocate an array to store the number of spaces inserted before each
4449 * line. We will use it to pre-compute the length of the new line and the
4450 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004451 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004452 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004454#if defined(FEAT_COMMENTS) || defined(PROTO)
4455 if (remove_comments)
4456 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004457 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02004458 if (comments == NULL)
4459 {
4460 vim_free(spaces);
4461 return FAIL;
4462 }
4463 }
4464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004467 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004468 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004469 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004471 for (t = 0; t < count; ++t)
4472 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004473 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004474 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004475 {
4476 /* Set the '[ mark. */
4477 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4478 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4479 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004480#if defined(FEAT_COMMENTS) || defined(PROTO)
4481 if (remove_comments)
4482 {
4483 /* We don't want to remove the comment leader if the
4484 * previous line is not a comment. */
4485 if (t > 0 && prev_was_comment)
4486 {
4487
4488 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4489 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004490 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004491 curr = new_curr;
4492 }
4493 else
4494 curr = skip_comment(curr, FALSE, insert_space,
4495 &prev_was_comment);
4496 }
4497#endif
4498
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004499 if (insert_space && t > 0)
4500 {
4501 curr = skipwhite(curr);
4502 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004503 && (!has_format_option(FO_MBYTE_JOIN)
4504 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4505 && (!has_format_option(FO_MBYTE_JOIN2)
4506 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004507 )
4508 {
4509 /* don't add a space if the line is ending in a space */
4510 if (endcurr1 == ' ')
4511 endcurr1 = endcurr2;
4512 else
4513 ++spaces[t];
4514 /* extra space when 'joinspaces' set and line ends in '.' */
4515 if ( p_js
4516 && (endcurr1 == '.'
4517 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4518 && (endcurr1 == '?' || endcurr1 == '!'))))
4519 ++spaces[t];
4520 }
4521 }
4522 currsize = (int)STRLEN(curr);
4523 sumsize += currsize + spaces[t];
4524 endcurr1 = endcurr2 = NUL;
4525 if (insert_space && currsize > 0)
4526 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004527 if (has_mbyte)
4528 {
4529 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004530 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004531 endcurr1 = (*mb_ptr2char)(cend);
4532 if (cend > curr)
4533 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004534 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004535 endcurr2 = (*mb_ptr2char)(cend);
4536 }
4537 }
4538 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004539 {
4540 endcurr1 = *(curr + currsize - 1);
4541 if (currsize > 1)
4542 endcurr2 = *(curr + currsize - 2);
4543 }
4544 }
4545 line_breakcheck();
4546 if (got_int)
4547 {
4548 ret = FAIL;
4549 goto theend;
4550 }
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004553 /* store the column position before last line */
4554 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004556 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004557 newp = alloc(sumsize + 1);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004558 cend = newp + sumsize;
4559 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004561#ifdef FEAT_TEXT_PROP
4562 // We need to move properties of the lines that are going to be deleted to
4563 // the new long one.
4564 if (curbuf->b_has_textprop && !text_prop_frozen)
4565 {
4566 // Allocate an array to copy the text properties of joined lines into.
4567 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004568 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
4569 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004570 if (prop_lengths == NULL)
4571 VIM_CLEAR(prop_lines);
4572 }
4573#endif
4574
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004575 /*
4576 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004577 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004578 *
4579 * Move marks from each deleted line to the joined line, adjusting the
4580 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4581 * should not really be a problem.
4582 */
4583 for (t = count - 1; ; --t)
4584 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004585 int spaces_removed;
4586
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004587 cend -= currsize;
4588 mch_memmove(cend, curr, (size_t)currsize);
4589 if (spaces[t] > 0)
4590 {
4591 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004592 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004593 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004594
4595 // If deleting more spaces than adding, the cursor moves no more than
4596 // what is added if it is inside these spaces.
4597 spaces_removed = (curr - curr_start) - spaces[t];
4598
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004599 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004600 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004601 if (t == 0)
4602 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004603#ifdef FEAT_TEXT_PROP
4604 if (prop_lines != NULL)
4605 adjust_props_for_join(curwin->w_cursor.lnum + t,
4606 prop_lines + t - 1, prop_lengths + t - 1,
4607 (long)(cend - newp - spaces_removed), spaces_removed);
4608#endif
4609
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004610 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004611#if defined(FEAT_COMMENTS)
Bram Moolenaar81340392012-06-06 16:12:59 +02004612 if (remove_comments)
4613 curr += comments[t - 1];
4614#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004615 if (insert_space && t > 1)
4616 curr = skipwhite(curr);
4617 currsize = (int)STRLEN(curr);
4618 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004619
4620#ifdef FEAT_TEXT_PROP
4621 if (prop_lines != NULL)
4622 join_prop_lines(curwin->w_cursor.lnum, newp,
4623 prop_lines, prop_lengths, count);
4624 else
4625#endif
4626 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004628 if (setmark)
4629 {
4630 /* Set the '] mark. */
4631 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02004632 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004633 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 /* Only report the change in the first line here, del_lines() will report
4636 * the deleted line. */
4637 changed_lines(curwin->w_cursor.lnum, currsize,
4638 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004640 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 * briefly, and then move it back. After del_lines() the cursor may
4642 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 */
4644 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004646 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 curwin->w_cursor.lnum = t;
4648
4649 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004650 * Set the cursor column:
4651 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004652 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004654 curwin->w_cursor.col =
4655 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 curwin->w_set_curswant = TRUE;
4660
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004661theend:
4662 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004663#if defined(FEAT_COMMENTS) || defined(PROTO)
4664 if (remove_comments)
4665 vim_free(comments);
4666#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004667 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668}
4669
4670#ifdef FEAT_COMMENTS
4671/*
4672 * Return TRUE if the two comment leaders given are the same. "lnum" is
4673 * the first line. White-space is ignored. Note that the whole of
4674 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4675 */
4676 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004677same_leader(
4678 linenr_T lnum,
4679 int leader1_len,
4680 char_u *leader1_flags,
4681 int leader2_len,
4682 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684 int idx1 = 0, idx2 = 0;
4685 char_u *p;
4686 char_u *line1;
4687 char_u *line2;
4688
4689 if (leader1_len == 0)
4690 return (leader2_len == 0);
4691
4692 /*
4693 * If first leader has 'f' flag, the lines can be joined only if the
4694 * second line does not have a leader.
4695 * If first leader has 'e' flag, the lines can never be joined.
4696 * If fist leader has 's' flag, the lines can only be joined if there is
4697 * some text after it and the second line has the 'm' flag.
4698 */
4699 if (leader1_flags != NULL)
4700 {
4701 for (p = leader1_flags; *p && *p != ':'; ++p)
4702 {
4703 if (*p == COM_FIRST)
4704 return (leader2_len == 0);
4705 if (*p == COM_END)
4706 return FALSE;
4707 if (*p == COM_START)
4708 {
4709 if (*(ml_get(lnum) + leader1_len) == NUL)
4710 return FALSE;
4711 if (leader2_flags == NULL || leader2_len == 0)
4712 return FALSE;
4713 for (p = leader2_flags; *p && *p != ':'; ++p)
4714 if (*p == COM_MIDDLE)
4715 return TRUE;
4716 return FALSE;
4717 }
4718 }
4719 }
4720
4721 /*
4722 * Get current line and next line, compare the leaders.
4723 * The first line has to be saved, only one line can be locked at a time.
4724 */
4725 line1 = vim_strsave(ml_get(lnum));
4726 if (line1 != NULL)
4727 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004728 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 ;
4730 line2 = ml_get(lnum + 1);
4731 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4732 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004733 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 if (line1[idx1++] != line2[idx2])
4736 break;
4737 }
4738 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004739 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 ++idx1;
4741 }
4742 vim_free(line1);
4743 }
4744 return (idx2 == leader2_len && idx1 == leader1_len);
4745}
4746#endif
4747
4748/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004749 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 */
4751 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004752op_format(
4753 oparg_T *oap,
4754 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 long old_line_count = curbuf->b_ml.ml_line_count;
4757
4758 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4759 * can put it back there. */
4760 curwin->w_cursor = oap->cursor_start;
4761
4762 if (u_save((linenr_T)(oap->start.lnum - 1),
4763 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4764 return;
4765 curwin->w_cursor = oap->start;
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (oap->is_VIsual)
4768 /* When there is no change: need to remove the Visual selection */
4769 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
4771 /* Set '[ mark at the start of the formatted area */
4772 curbuf->b_op_start = oap->start;
4773
4774 /* For "gw" remember the cursor position and put it back below (adjusted
4775 * for joined and split lines). */
4776 if (keep_cursor)
4777 saved_cursor = oap->cursor_start;
4778
Bram Moolenaar81a82092008-03-12 16:27:00 +00004779 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
4781 /*
4782 * Leave the cursor at the first non-blank of the last formatted line.
4783 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4784 * line, so "." will do the next lines.
4785 */
4786 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4787 ++curwin->w_cursor.lnum;
4788 beginline(BL_WHITE | BL_FIX);
4789 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4790 msgmore(old_line_count);
4791
4792 /* put '] mark on the end of the formatted area */
4793 curbuf->b_op_end = curwin->w_cursor;
4794
4795 if (keep_cursor)
4796 {
4797 curwin->w_cursor = saved_cursor;
4798 saved_cursor.lnum = 0;
4799 }
4800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (oap->is_VIsual)
4802 {
4803 win_T *wp;
4804
4805 FOR_ALL_WINDOWS(wp)
4806 {
4807 if (wp->w_old_cursor_lnum != 0)
4808 {
4809 /* When lines have been inserted or deleted, adjust the end of
4810 * the Visual area to be redrawn. */
4811 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4812 wp->w_old_cursor_lnum += old_line_count;
4813 else
4814 wp->w_old_visual_lnum += old_line_count;
4815 }
4816 }
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818}
4819
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004820#if defined(FEAT_EVAL) || defined(PROTO)
4821/*
4822 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4823 */
4824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004825op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004826{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004827 if (oap->is_VIsual)
4828 /* When there is no change: need to remove the Visual selection */
4829 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004830
Bram Moolenaar700303e2010-07-11 17:35:50 +02004831 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4832 /* As documented: when 'formatexpr' returns non-zero fall back to
4833 * internal formatting. */
4834 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004835}
4836
4837 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004838fex_format(
4839 linenr_T lnum,
4840 long count,
4841 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004842{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004843 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4844 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004845 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004846 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004847
4848 /*
4849 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004850 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004851 */
4852 set_vim_var_nr(VV_LNUM, lnum);
4853 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004854 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004855
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004856 /* Make a copy, the option could be changed while calling it. */
4857 fex = vim_strsave(curbuf->b_p_fex);
4858 if (fex == NULL)
4859 return 0;
4860
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004861 /*
4862 * Evaluate the function.
4863 */
4864 if (use_sandbox)
4865 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004866 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004867 if (use_sandbox)
4868 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004869
4870 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004871 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004872
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004873 return r;
4874}
4875#endif
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877/*
4878 * Format "line_count" lines, starting at the cursor position.
4879 * When "line_count" is negative, format until the end of the paragraph.
4880 * Lines after the cursor line are saved for undo, caller must have saved the
4881 * first line.
4882 */
4883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004884format_lines(
4885 linenr_T line_count,
4886 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
4888 int max_len;
4889 int is_not_par; /* current line not part of parag. */
4890 int next_is_not_par; /* next line not part of paragraph */
4891 int is_end_par; /* at end of paragraph */
4892 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4893 int next_is_start_par = FALSE;
4894#ifdef FEAT_COMMENTS
4895 int leader_len = 0; /* leader len of current line */
4896 int next_leader_len; /* leader len of next line */
4897 char_u *leader_flags = NULL; /* flags for leader of current line */
4898 char_u *next_leader_flags; /* flags for leader of next line */
4899 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004900 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
4902 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004903 int second_indent = -1; /* indent for second line (comment
4904 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 int do_second_indent;
4906 int do_number_indent;
4907 int do_trail_white;
4908 int first_par_line = TRUE;
4909 int smd_save;
4910 long count;
4911 int need_set_indent = TRUE; /* set indent of next paragraph */
4912 int force_format = FALSE;
4913 int old_State = State;
4914
4915 /* length of a line to force formatting: 3 * 'tw' */
4916 max_len = comp_textwidth(TRUE) * 3;
4917
4918 /* check for 'q', '2' and '1' in 'formatoptions' */
4919#ifdef FEAT_COMMENTS
4920 do_comments = has_format_option(FO_Q_COMS);
4921#endif
4922 do_second_indent = has_format_option(FO_Q_SECOND);
4923 do_number_indent = has_format_option(FO_Q_NUMBER);
4924 do_trail_white = has_format_option(FO_WHITE_PAR);
4925
4926 /*
4927 * Get info about the previous and current line.
4928 */
4929 if (curwin->w_cursor.lnum > 1)
4930 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4931#ifdef FEAT_COMMENTS
4932 , &leader_len, &leader_flags, do_comments
4933#endif
4934 );
4935 else
4936 is_not_par = TRUE;
4937 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4938#ifdef FEAT_COMMENTS
4939 , &next_leader_len, &next_leader_flags, do_comments
4940#endif
4941 );
4942 is_end_par = (is_not_par || next_is_not_par);
4943 if (!is_end_par && do_trail_white)
4944 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4945
4946 curwin->w_cursor.lnum--;
4947 for (count = line_count; count != 0 && !got_int; --count)
4948 {
4949 /*
4950 * Advance to next paragraph.
4951 */
4952 if (advance)
4953 {
4954 curwin->w_cursor.lnum++;
4955 prev_is_end_par = is_end_par;
4956 is_not_par = next_is_not_par;
4957#ifdef FEAT_COMMENTS
4958 leader_len = next_leader_len;
4959 leader_flags = next_leader_flags;
4960#endif
4961 }
4962
4963 /*
4964 * The last line to be formatted.
4965 */
4966 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4967 {
4968 next_is_not_par = TRUE;
4969#ifdef FEAT_COMMENTS
4970 next_leader_len = 0;
4971 next_leader_flags = NULL;
4972#endif
4973 }
4974 else
4975 {
4976 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4977#ifdef FEAT_COMMENTS
4978 , &next_leader_len, &next_leader_flags, do_comments
4979#endif
4980 );
4981 if (do_number_indent)
4982 next_is_start_par =
4983 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4984 }
4985 advance = TRUE;
4986 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4987 if (!is_end_par && do_trail_white)
4988 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4989
4990 /*
4991 * Skip lines that are not in a paragraph.
4992 */
4993 if (is_not_par)
4994 {
4995 if (line_count < 0)
4996 break;
4997 }
4998 else
4999 {
5000 /*
5001 * For the first line of a paragraph, check indent of second line.
5002 * Don't do this for comments and empty lines.
5003 */
5004 if (first_par_line
5005 && (do_second_indent || do_number_indent)
5006 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005007 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005009 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005010 {
5011#ifdef FEAT_COMMENTS
5012 if (leader_len == 0 && next_leader_len == 0)
5013 {
5014 /* no comment found */
5015#endif
5016 second_indent =
5017 get_indent_lnum(curwin->w_cursor.lnum + 1);
5018#ifdef FEAT_COMMENTS
5019 }
5020 else
5021 {
5022 second_indent = next_leader_len;
5023 do_comments_list = 1;
5024 }
5025#endif
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005028 {
5029#ifdef FEAT_COMMENTS
5030 if (leader_len == 0 && next_leader_len == 0)
5031 {
5032 /* no comment found */
5033#endif
5034 second_indent =
5035 get_number_indent(curwin->w_cursor.lnum);
5036#ifdef FEAT_COMMENTS
5037 }
5038 else
5039 {
5040 /* get_number_indent() is now "comment aware"... */
5041 second_indent =
5042 get_number_indent(curwin->w_cursor.lnum);
5043 do_comments_list = 1;
5044 }
5045#endif
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048
5049 /*
5050 * When the comment leader changes, it's the end of the paragraph.
5051 */
5052 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5053#ifdef FEAT_COMMENTS
5054 || !same_leader(curwin->w_cursor.lnum,
5055 leader_len, leader_flags,
5056 next_leader_len, next_leader_flags)
5057#endif
5058 )
5059 is_end_par = TRUE;
5060
5061 /*
5062 * If we have got to the end of a paragraph, or the line is
5063 * getting long, format it.
5064 */
5065 if (is_end_par || force_format)
5066 {
5067 if (need_set_indent)
5068 /* replace indent in first line with minimal number of
5069 * tabs and spaces, according to current options */
5070 (void)set_indent(get_indent(), SIN_CHANGED);
5071
5072 /* put cursor on last non-space */
5073 State = NORMAL; /* don't go past end-of-line */
5074 coladvance((colnr_T)MAXCOL);
5075 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5076 dec_cursor();
5077
5078 /* do the formatting, without 'showmode' */
5079 State = INSERT; /* for open_line() */
5080 smd_save = p_smd;
5081 p_smd = FALSE;
5082 insertchar(NUL, INSCHAR_FORMAT
5083#ifdef FEAT_COMMENTS
5084 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005085 + (do_comments && do_comments_list
5086 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005088 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 State = old_State;
5090 p_smd = smd_save;
5091 second_indent = -1;
5092 /* at end of par.: need to set indent of next par. */
5093 need_set_indent = is_end_par;
5094 if (is_end_par)
5095 {
5096 /* When called with a negative line count, break at the
5097 * end of the paragraph. */
5098 if (line_count < 0)
5099 break;
5100 first_par_line = TRUE;
5101 }
5102 force_format = FALSE;
5103 }
5104
5105 /*
5106 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005107 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
5109 if (!is_end_par)
5110 {
5111 advance = FALSE;
5112 curwin->w_cursor.lnum++;
5113 curwin->w_cursor.col = 0;
5114 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005115 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005118 {
5119 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005121 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005122 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005124 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5125 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005126 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005127
5128 if (indent > 0)
5129 {
5130 (void)del_bytes(indent, FALSE, FALSE);
5131 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005132 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005133 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005136 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 beep_flush();
5139 break;
5140 }
5141 first_par_line = FALSE;
5142 /* If the line is getting long, format it next time */
5143 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5144 force_format = TRUE;
5145 else
5146 force_format = FALSE;
5147 }
5148 }
5149 line_breakcheck();
5150 }
5151}
5152
5153/*
5154 * Return TRUE if line "lnum" ends in a white character.
5155 */
5156 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005157ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
5159 char_u *s = ml_get(lnum);
5160 size_t l;
5161
5162 if (*s == NUL)
5163 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005164 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 * invocation may call function multiple times". */
5166 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005167 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168}
5169
5170/*
5171 * Blank lines, and lines containing only the comment leader, are left
5172 * untouched by the formatting. The function returns TRUE in this
5173 * case. It also returns TRUE when a line starts with the end of a comment
5174 * ('e' in comment flags), so that this line is skipped, and not joined to the
5175 * previous line. A new paragraph starts after a blank line, or when the
5176 * comment leader changes -- webb.
5177 */
5178#ifdef FEAT_COMMENTS
5179 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005180fmt_check_par(
5181 linenr_T lnum,
5182 int *leader_len,
5183 char_u **leader_flags,
5184 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 char_u *flags = NULL; /* init for GCC */
5187 char_u *ptr;
5188
5189 ptr = ml_get(lnum);
5190 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005191 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 else
5193 *leader_len = 0;
5194
5195 if (*leader_len > 0)
5196 {
5197 /*
5198 * Search for 'e' flag in comment leader flags.
5199 */
5200 flags = *leader_flags;
5201 while (*flags && *flags != ':' && *flags != COM_END)
5202 ++flags;
5203 }
5204
5205 return (*skipwhite(ptr + *leader_len) == NUL
5206 || (*leader_len > 0 && *flags == COM_END)
5207 || startPS(lnum, NUL, FALSE));
5208}
5209#else
5210 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
5213 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5214}
5215#endif
5216
5217/*
5218 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5219 * previous line is in the same paragraph. Used for auto-formatting.
5220 */
5221 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 char_u *p;
5225#ifdef FEAT_COMMENTS
5226 int leader_len = 0; /* leader len of current line */
5227 char_u *leader_flags = NULL; /* flags for leader of current line */
5228 int next_leader_len; /* leader len of next line */
5229 char_u *next_leader_flags; /* flags for leader of next line */
5230 int do_comments; /* format comments */
5231#endif
5232
5233 if (lnum <= 1)
5234 return TRUE; /* start of the file */
5235
5236 p = ml_get(lnum - 1);
5237 if (*p == NUL)
5238 return TRUE; /* after empty line */
5239
5240#ifdef FEAT_COMMENTS
5241 do_comments = has_format_option(FO_Q_COMS);
5242#endif
5243 if (fmt_check_par(lnum - 1
5244#ifdef FEAT_COMMENTS
5245 , &leader_len, &leader_flags, do_comments
5246#endif
5247 ))
5248 return TRUE; /* after non-paragraph line */
5249
5250 if (fmt_check_par(lnum
5251#ifdef FEAT_COMMENTS
5252 , &next_leader_len, &next_leader_flags, do_comments
5253#endif
5254 ))
5255 return TRUE; /* "lnum" is not a paragraph line */
5256
5257 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5258 return TRUE; /* missing trailing space in previous line. */
5259
5260 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5261 return TRUE; /* numbered item starts in "lnum". */
5262
5263#ifdef FEAT_COMMENTS
5264 if (!same_leader(lnum - 1, leader_len, leader_flags,
5265 next_leader_len, next_leader_flags))
5266 return TRUE; /* change of comment leader. */
5267#endif
5268
5269 return FALSE;
5270}
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272/*
5273 * prepare a few things for block mode yank/delete/tilde
5274 *
5275 * for delete:
5276 * - textlen includes the first/last char to be (partly) deleted
5277 * - start/endspaces is the number of columns that are taken by the
5278 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005279 * deleted.
5280 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 * - textlen includes the first/last char to be wholly yanked
5282 * - start/endspaces is the number of columns of the first/last yanked char
5283 * that are to be yanked.
5284 */
5285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286block_prep(
5287 oparg_T *oap,
5288 struct block_def *bdp,
5289 linenr_T lnum,
5290 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 int incr = 0;
5293 char_u *pend;
5294 char_u *pstart;
5295 char_u *line;
5296 char_u *prev_pstart;
5297 char_u *prev_pend;
5298
5299 bdp->startspaces = 0;
5300 bdp->endspaces = 0;
5301 bdp->textlen = 0;
5302 bdp->start_vcol = 0;
5303 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 bdp->is_short = FALSE;
5305 bdp->is_oneChar = FALSE;
5306 bdp->pre_whitesp = 0;
5307 bdp->pre_whitesp_c = 0;
5308 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 bdp->start_char_vcols = 0;
5310
5311 line = ml_get(lnum);
5312 pstart = line;
5313 prev_pstart = line;
5314 while (bdp->start_vcol < oap->start_vcol && *pstart)
5315 {
5316 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005317 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005319 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
5321 bdp->pre_whitesp += incr;
5322 bdp->pre_whitesp_c++;
5323 }
5324 else
5325 {
5326 bdp->pre_whitesp = 0;
5327 bdp->pre_whitesp_c = 0;
5328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005330 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332 bdp->start_char_vcols = incr;
5333 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5334 {
5335 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (!is_del || oap->op_type == OP_APPEND)
5338 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5339 }
5340 else
5341 {
5342 /* notice: this converts partly selected Multibyte characters to
5343 * spaces, too. */
5344 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5345 if (is_del && bdp->startspaces)
5346 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5347 pend = pstart;
5348 bdp->end_vcol = bdp->start_vcol;
5349 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 if (oap->op_type == OP_INSERT)
5353 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5354 else if (oap->op_type == OP_APPEND)
5355 {
5356 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5357 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5358 }
5359 else
5360 {
5361 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5362 if (is_del && oap->op_type != OP_LSHIFT)
5363 {
5364 /* just putting the sum of those two into
5365 * bdp->startspaces doesn't work for Visual replace,
5366 * so we have to split the tab in two */
5367 bdp->startspaces = bdp->start_char_vcols
5368 - (bdp->start_vcol - oap->start_vcol);
5369 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5370 }
5371 }
5372 }
5373 else
5374 {
5375 prev_pend = pend;
5376 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5377 {
5378 /* Count a tab for what it's worth (if list mode not on) */
5379 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005380 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 bdp->end_vcol += incr;
5382 }
5383 if (bdp->end_vcol <= oap->end_vcol
5384 && (!is_del
5385 || oap->op_type == OP_APPEND
5386 || oap->op_type == OP_REPLACE)) /* line too short */
5387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 /* Alternative: include spaces to fill up the block.
5390 * Disadvantage: can lead to trailing spaces when the line is
5391 * short where the text is put */
5392 /* if (!is_del || oap->op_type == OP_APPEND) */
5393 if (oap->op_type == OP_APPEND || virtual_op)
5394 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005395 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 else
5397 bdp->endspaces = 0; /* replace doesn't add characters */
5398 }
5399 else if (bdp->end_vcol > oap->end_vcol)
5400 {
5401 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5402 if (!is_del && bdp->endspaces)
5403 {
5404 bdp->endspaces = incr - bdp->endspaces;
5405 if (pend != pstart)
5406 pend = prev_pend;
5407 }
5408 }
5409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 if (is_del && bdp->startspaces)
5412 pstart = prev_pstart;
5413 bdp->textlen = (int)(pend - pstart);
5414 }
5415 bdp->textcol = (colnr_T) (pstart - line);
5416 bdp->textstart = pstart;
5417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005420 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005422 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005423op_addsub(
5424 oparg_T *oap,
5425 linenr_T Prenum1, /* Amount of add/subtract */
5426 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005428 pos_T pos;
5429 struct block_def bd;
5430 int change_cnt = 0;
5431 linenr_T amount = Prenum1;
5432
Bram Moolenaar6b731882018-11-16 20:54:47 +01005433 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01005434 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01005435 // the call to changed_lines().
5436#ifdef FEAT_FOLDING
5437 disable_fold_update++;
5438#endif
5439
Bram Moolenaard79e5502016-01-10 22:13:02 +01005440 if (!VIsual_active)
5441 {
5442 pos = curwin->w_cursor;
5443 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005444 {
5445#ifdef FEAT_FOLDING
5446 disable_fold_update--;
5447#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005448 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005449 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005450 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005451#ifdef FEAT_FOLDING
5452 disable_fold_update--;
5453#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005454 if (change_cnt)
5455 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5456 }
5457 else
5458 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005459 int one_change;
5460 int length;
5461 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005462
5463 if (u_save((linenr_T)(oap->start.lnum - 1),
5464 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005465 {
5466#ifdef FEAT_FOLDING
5467 disable_fold_update--;
5468#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005469 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005470 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005471
5472 pos = oap->start;
5473 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5474 {
5475 if (oap->block_mode) /* Visual block mode */
5476 {
5477 block_prep(oap, &bd, pos.lnum, FALSE);
5478 pos.col = bd.textcol;
5479 length = bd.textlen;
5480 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005481 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005482 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005483 curwin->w_cursor.col = 0;
5484 pos.col = 0;
5485 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5486 }
5487 else /* oap->motion_type == MCHAR */
5488 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005489 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005490 dec(&(oap->end));
5491 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5492 pos.col = 0;
5493 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005494 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005495 pos.col += oap->start.col;
5496 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005497 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005498 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005499 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005500 length = (int)STRLEN(ml_get(oap->end.lnum));
5501 if (oap->end.col >= length)
5502 oap->end.col = length - 1;
5503 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005504 }
5505 }
5506 one_change = do_addsub(oap->op_type, &pos, length, amount);
5507 if (one_change)
5508 {
5509 /* Remember the start position of the first change. */
5510 if (change_cnt == 0)
5511 startpos = curbuf->b_op_start;
5512 ++change_cnt;
5513 }
5514
5515#ifdef FEAT_NETBEANS_INTG
5516 if (netbeans_active() && one_change)
5517 {
5518 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5519
5520 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5521 netbeans_inserted(curbuf, pos.lnum, pos.col,
5522 &ptr[pos.col], length);
5523 }
5524#endif
5525 if (g_cmd && one_change)
5526 amount += Prenum1;
5527 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005528
5529#ifdef FEAT_FOLDING
5530 disable_fold_update--;
5531#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005532 if (change_cnt)
5533 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5534
5535 if (!change_cnt && oap->is_VIsual)
5536 /* No change: need to remove the Visual selection */
5537 redraw_curbuf_later(INVERTED);
5538
5539 /* Set '[ mark if something changed. Keep the last end
5540 * position from do_addsub(). */
5541 if (change_cnt > 0)
5542 curbuf->b_op_start = startpos;
5543
5544 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005545 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005546 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005547 }
5548}
5549
5550/*
5551 * Add or subtract 'Prenum1' from a number in a line
5552 * op_type is OP_NR_ADD or OP_NR_SUB
5553 *
5554 * Returns TRUE if some character was changed.
5555 */
5556 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005557do_addsub(
5558 int op_type,
5559 pos_T *pos,
5560 int length,
5561 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 int col;
5564 char_u *buf1;
5565 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005566 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005568 uvarnumber_T n;
5569 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 char_u *ptr;
5571 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 int todel;
5573 int dohex;
5574 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005575 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 int doalp;
5577 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005579 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005580 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005581 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005582 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005583 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005584 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005585 pos_T startpos;
5586 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5589 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005590 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5592
Bram Moolenaard79e5502016-01-10 22:13:02 +01005593 curwin->w_cursor = *pos;
5594 ptr = ml_get(pos->lnum);
5595 col = pos->col;
5596
5597 if (*ptr == NUL)
5598 goto theend;
5599
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 /*
5601 * First check if we are on a hexadecimal number, after the "0x".
5602 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005603 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005605 if (dobin)
5606 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005607 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005608 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005609 if (has_mbyte)
5610 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005611 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005612
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005613 if (dohex)
5614 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005615 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005616 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005617 if (has_mbyte)
5618 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005619 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005620
5621 if ( dobin
5622 && dohex
5623 && ! ((col > 0
5624 && (ptr[col] == 'X'
5625 || ptr[col] == 'x')
5626 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005627 && (!has_mbyte ||
5628 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005629 && vim_isxdigit(ptr[col + 1]))))
5630 {
5631
5632 /* In case of binary/hexadecimal pattern overlap match, rescan */
5633
Bram Moolenaard79e5502016-01-10 22:13:02 +01005634 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005635
5636 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005637 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005638 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005639 if (has_mbyte)
5640 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005641 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005642 }
5643
5644 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005645 && col > 0
5646 && (ptr[col] == 'X'
5647 || ptr[col] == 'x')
5648 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005649 && (!has_mbyte ||
5650 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005651 && vim_isxdigit(ptr[col + 1])) ||
5652 ( dobin
5653 && col > 0
5654 && (ptr[col] == 'B'
5655 || ptr[col] == 'b')
5656 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005657 && (!has_mbyte ||
5658 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005659 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005660 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005661 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005662 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005663 if (has_mbyte)
5664 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005665 }
5666 else
5667 {
5668 /*
5669 * Search forward and then backward to find the start of number.
5670 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005671 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005672
5673 while (ptr[col] != NUL
5674 && !vim_isdigit(ptr[col])
5675 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005676 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005677
5678 while (col > 0
5679 && vim_isdigit(ptr[col - 1])
5680 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005681 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005682 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005683 if (has_mbyte)
5684 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005685 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005686 }
5687 }
5688
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005689 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005690 {
5691 while (ptr[col] != NUL && length > 0
5692 && !vim_isdigit(ptr[col])
5693 && !(doalp && ASCII_ISALPHA(ptr[col])))
5694 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005695 int mb_len = MB_PTR2LEN(ptr + col);
5696
5697 col += mb_len;
5698 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005699 }
5700
5701 if (length == 0)
5702 goto theend;
5703
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005704 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005705 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005706 {
5707 negative = TRUE;
5708 was_positive = FALSE;
5709 }
5710 }
5711
5712 /*
5713 * If a number was found, and saving for undo works, replace the number.
5714 */
5715 firstdigit = ptr[col];
5716 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5717 {
5718 beep_flush();
5719 goto theend;
5720 }
5721
5722 if (doalp && ASCII_ISALPHA(firstdigit))
5723 {
5724 /* decrement or increment alphabetic character */
5725 if (op_type == OP_NR_SUB)
5726 {
5727 if (CharOrd(firstdigit) < Prenum1)
5728 {
5729 if (isupper(firstdigit))
5730 firstdigit = 'A';
5731 else
5732 firstdigit = 'a';
5733 }
5734 else
5735#ifdef EBCDIC
5736 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5737#else
5738 firstdigit -= Prenum1;
5739#endif
5740 }
5741 else
5742 {
5743 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5744 {
5745 if (isupper(firstdigit))
5746 firstdigit = 'Z';
5747 else
5748 firstdigit = 'z';
5749 }
5750 else
5751#ifdef EBCDIC
5752 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5753#else
5754 firstdigit += Prenum1;
5755#endif
5756 }
5757 curwin->w_cursor.col = col;
5758 if (!did_change)
5759 startpos = curwin->w_cursor;
5760 did_change = TRUE;
5761 (void)del_char(FALSE);
5762 ins_char(firstdigit);
5763 endpos = curwin->w_cursor;
5764 curwin->w_cursor.col = col;
5765 }
5766 else
5767 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005768 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005769 && (!has_mbyte ||
5770 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005771 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005772 {
5773 /* negative number */
5774 --col;
5775 negative = TRUE;
5776 }
5777 /* get the number value (unsigned) */
5778 if (visual && VIsual_mode != 'V')
5779 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5780 ? (int)STRLEN(ptr) - col
5781 : length);
5782
5783 vim_str2nr(ptr + col, &pre, &length,
5784 0 + (dobin ? STR2NR_BIN : 0)
5785 + (dooct ? STR2NR_OCT : 0)
5786 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005787 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005788
5789 /* ignore leading '-' for hex and octal and bin numbers */
5790 if (pre && negative)
5791 {
5792 ++col;
5793 --length;
5794 negative = FALSE;
5795 }
5796 /* add or subtract */
5797 subtract = FALSE;
5798 if (op_type == OP_NR_SUB)
5799 subtract ^= TRUE;
5800 if (negative)
5801 subtract ^= TRUE;
5802
5803 oldn = n;
5804 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005805 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005806 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005807 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005808 /* handle wraparound for decimal numbers */
5809 if (!pre)
5810 {
5811 if (subtract)
5812 {
5813 if (n > oldn)
5814 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005815 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005816 negative ^= TRUE;
5817 }
5818 }
5819 else
5820 {
5821 /* add */
5822 if (n < oldn)
5823 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005824 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005825 negative ^= TRUE;
5826 }
5827 }
5828 if (n == 0)
5829 negative = FALSE;
5830 }
5831
5832 if (visual && !was_positive && !negative && col > 0)
5833 {
5834 /* need to remove the '-' */
5835 col--;
5836 length++;
5837 }
5838
5839 /*
5840 * Delete the old number.
5841 */
5842 curwin->w_cursor.col = col;
5843 if (!did_change)
5844 startpos = curwin->w_cursor;
5845 did_change = TRUE;
5846 todel = length;
5847 c = gchar_cursor();
5848 /*
5849 * Don't include the '-' in the length, only the length of the
5850 * part after it is kept the same.
5851 */
5852 if (c == '-')
5853 --length;
5854 while (todel-- > 0)
5855 {
5856 if (c < 0x100 && isalpha(c))
5857 {
5858 if (isupper(c))
5859 hexupper = TRUE;
5860 else
5861 hexupper = FALSE;
5862 }
5863 /* del_char() will mark line needing displaying */
5864 (void)del_char(FALSE);
5865 c = gchar_cursor();
5866 }
5867
5868 /*
5869 * Prepare the leading characters in buf1[].
5870 * When there are many leading zeros it could be very long.
5871 * Allocate a bit too much.
5872 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005873 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005874 if (buf1 == NULL)
5875 goto theend;
5876 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005877 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005878 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01005879 if (pre)
5880 {
5881 *ptr++ = '0';
5882 --length;
5883 }
5884 if (pre == 'b' || pre == 'B' ||
5885 pre == 'x' || pre == 'X')
5886 {
5887 *ptr++ = pre;
5888 --length;
5889 }
5890
5891 /*
5892 * Put the number characters in buf2[].
5893 */
5894 if (pre == 'b' || pre == 'B')
5895 {
5896 int i;
5897 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005898 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005899
5900 /* leading zeros */
5901 for (bit = bits; bit > 0; bit--)
5902 if ((n >> (bit - 1)) & 0x1) break;
5903
5904 for (i = 0; bit > 0; bit--)
5905 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5906
5907 buf2[i] = '\0';
5908 }
5909 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02005910 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005911 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005912 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02005913 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005914 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005915 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02005916 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005917 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005918 else
Bram Moolenaarea391762018-04-08 13:07:22 +02005919 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005920 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005921 length -= (int)STRLEN(buf2);
5922
5923 /*
5924 * Adjust number of zeros to the new number of digits, so the
5925 * total length of the number remains the same.
5926 * Don't do this when
5927 * the result may look like an octal number.
5928 */
5929 if (firstdigit == '0' && !(dooct && pre == 0))
5930 while (length-- > 0)
5931 *ptr++ = '0';
5932 *ptr = NUL;
5933 STRCAT(buf1, buf2);
5934 ins_str(buf1); /* insert the new number */
5935 vim_free(buf1);
5936 endpos = curwin->w_cursor;
5937 if (did_change && curwin->w_cursor.col)
5938 --curwin->w_cursor.col;
5939 }
5940
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005941 if (did_change)
5942 {
5943 /* set the '[ and '] marks */
5944 curbuf->b_op_start = startpos;
5945 curbuf->b_op_end = endpos;
5946 if (curbuf->b_op_end.col > 0)
5947 --curbuf->b_op_end.col;
5948 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005949
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005950theend:
5951 if (visual)
5952 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005953 else if (did_change)
5954 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005955
Bram Moolenaard79e5502016-01-10 22:13:02 +01005956 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957}
5958
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5960/*
5961 * SELECTION / PRIMARY ('*')
5962 *
5963 * Text selection stuff that uses the GUI selection register '*'. When using a
5964 * GUI this may be text from another window, otherwise it is the last text we
5965 * had highlighted with VIsual mode. With mouse support, clicking the middle
5966 * button performs the paste, otherwise you will need to do <"*p>. "
5967 * If not under X, it is synonymous with the clipboard register '+'.
5968 *
5969 * X CLIPBOARD ('+')
5970 *
5971 * Text selection stuff that uses the GUI clipboard register '+'.
5972 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5973 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5974 * otherwise you will need to do <"+p>. "
5975 * If not under X, it is synonymous with the selection register '*'.
5976 */
5977
5978/*
5979 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01005980 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 * only exist while the owning application exists, so we write to the
5982 * permanent (while X runs) store CUT_BUFFER0.
5983 * Dump the CLIPBOARD selection if we own it (it's logically the more
5984 * 'permanent' of the two), otherwise the PRIMARY one.
5985 * For now, use a hard-coded sanity limit of 1Mb of data.
5986 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02005987#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005989x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 Display *dpy;
5992 char_u *str = NULL;
5993 long_u len = 0;
5994 int motion_type = -1;
5995
5996# ifdef FEAT_GUI
5997 if (gui.in_use)
5998 dpy = X_DISPLAY;
5999 else
6000# endif
6001# ifdef FEAT_XCLIPBOARD
6002 dpy = xterm_dpy;
6003# else
6004 return;
6005# endif
6006
6007 /* Get selection to export */
6008 if (clip_plus.owned)
6009 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6010 else if (clip_star.owned)
6011 motion_type = clip_convert_selection(&str, &len, &clip_star);
6012
6013 /* Check it's OK */
6014 if (dpy != NULL && str != NULL && motion_type >= 0
6015 && len < 1024*1024 && len > 0)
6016 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006017 int ok = TRUE;
6018
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006019 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6020 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6021 * encoding conversion usually doesn't work, so keep the text as-is.
6022 */
6023 if (has_mbyte)
6024 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006025 vimconv_T vc;
6026
6027 vc.vc_type = CONV_NONE;
6028 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6029 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006030 int intlen = len;
6031 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006032
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006033 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006034 conv_str = string_convert(&vc, str, &intlen);
6035 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006036 if (conv_str != NULL)
6037 {
6038 vim_free(str);
6039 str = conv_str;
6040 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006041 else
6042 {
6043 ok = FALSE;
6044 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006045 convert_setup(&vc, NULL, NULL);
6046 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006047 else
6048 {
6049 ok = FALSE;
6050 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006051 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006052
6053 /* Do not store the string if conversion failed. Better to use any
6054 * other selection than garbled text. */
6055 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006056 {
6057 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6058 XFlush(dpy);
6059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061
6062 vim_free(str);
6063}
6064#endif
6065
6066 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006067clip_free_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006069 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071 if (cbd == &clip_plus)
6072 y_current = &y_regs[PLUS_REGISTER];
6073 else
6074 y_current = &y_regs[STAR_REGISTER];
6075 free_yank_all();
6076 y_current->y_size = 0;
6077 y_current = y_ptr;
6078}
6079
6080/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006081 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 */
6083 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006084clip_get_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006086 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 pos_T old_visual;
6089 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 colnr_T old_curswant;
6091 int old_set_curswant;
6092 pos_T old_op_start, old_op_end;
6093 oparg_T oa;
6094 cmdarg_T ca;
6095
6096 if (cbd->owned)
6097 {
6098 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6099 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6100 return;
6101
6102 /* Get the text between clip_star.start & clip_star.end */
6103 old_y_previous = y_previous;
6104 old_y_current = y_current;
6105 old_cursor = curwin->w_cursor;
6106 old_curswant = curwin->w_curswant;
6107 old_set_curswant = curwin->w_set_curswant;
6108 old_op_start = curbuf->b_op_start;
6109 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 old_visual = VIsual;
6111 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 clear_oparg(&oa);
6113 oa.regname = (cbd == &clip_plus ? '+' : '*');
6114 oa.op_type = OP_YANK;
6115 vim_memset(&ca, 0, sizeof(ca));
6116 ca.oap = &oa;
6117 ca.cmdchar = 'y';
6118 ca.count1 = 1;
6119 ca.retval = CA_NO_ADJ_OP_END;
6120 do_pending_operator(&ca, 0, TRUE);
6121 y_previous = old_y_previous;
6122 y_current = old_y_current;
6123 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006124 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 curwin->w_curswant = old_curswant;
6126 curwin->w_set_curswant = old_set_curswant;
6127 curbuf->b_op_start = old_op_start;
6128 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 VIsual = old_visual;
6130 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006132 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
6134 clip_free_selection(cbd);
6135
6136 /* Try to get selected text from another window */
6137 clip_gen_request_selection(cbd);
6138 }
6139}
6140
Bram Moolenaard44347f2011-06-19 01:14:29 +02006141/*
6142 * Convert from the GUI selection string into the '*'/'+' register.
6143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006145clip_yank_selection(
6146 int type,
6147 char_u *str,
6148 long len,
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006149 Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006151 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153 if (cbd == &clip_plus)
6154 y_ptr = &y_regs[PLUS_REGISTER];
6155 else
6156 y_ptr = &y_regs[STAR_REGISTER];
6157
6158 clip_free_selection(cbd);
6159
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006160 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161}
6162
6163/*
6164 * Convert the '*'/'+' register into a GUI selection string returned in *str
6165 * with length *len.
6166 * Returns the motion type, or -1 for failure.
6167 */
6168 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006169clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 char_u *p;
6172 int lnum;
6173 int i, j;
6174 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006175 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
6177 if (cbd == &clip_plus)
6178 y_ptr = &y_regs[PLUS_REGISTER];
6179 else
6180 y_ptr = &y_regs[STAR_REGISTER];
6181
6182#ifdef USE_CRNL
6183 eolsize = 2;
6184#else
6185 eolsize = 1;
6186#endif
6187
6188 *str = NULL;
6189 *len = 0;
6190 if (y_ptr->y_array == NULL)
6191 return -1;
6192
6193 for (i = 0; i < y_ptr->y_size; i++)
6194 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6195
6196 /*
6197 * Don't want newline character at end of last line if we're in MCHAR mode.
6198 */
6199 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6200 *len -= eolsize;
6201
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006202 p = *str = alloc(*len + 1); // add one to avoid zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (p == NULL)
6204 return -1;
6205 lnum = 0;
6206 for (i = 0, j = 0; i < (int)*len; i++, j++)
6207 {
6208 if (y_ptr->y_array[lnum][j] == '\n')
6209 p[i] = NUL;
6210 else if (y_ptr->y_array[lnum][j] == NUL)
6211 {
6212#ifdef USE_CRNL
6213 p[i++] = '\r';
6214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 p[i] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 lnum++;
6217 j = -1;
6218 }
6219 else
6220 p[i] = y_ptr->y_array[lnum][j];
6221 }
6222 return y_ptr->y_type;
6223}
6224
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226/*
6227 * If we have written to a clipboard register, send the text to the clipboard.
6228 */
6229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006230may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6233 {
6234 clip_own_selection(&clip_star);
6235 clip_gen_set_selection(&clip_star);
6236 }
6237 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6238 {
6239 clip_own_selection(&clip_plus);
6240 clip_gen_set_selection(&clip_plus);
6241 }
6242}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
6244#endif /* FEAT_CLIPBOARD || PROTO */
6245
6246
6247#if defined(FEAT_DND) || defined(PROTO)
6248/*
6249 * Replace the contents of the '~' register with str.
6250 */
6251 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006252dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006254 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
6256 curr = y_current;
6257 y_current = &y_regs[TILDE_REGISTER];
6258 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006259 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 y_current = curr;
6261}
6262#endif
6263
6264
6265#if defined(FEAT_EVAL) || defined(PROTO)
6266/*
6267 * Return the type of a register.
6268 * Used for getregtype()
6269 * Returns MAUTO for error.
6270 */
6271 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006272get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 switch (regname)
6275 {
6276 case '%': /* file name */
6277 case '#': /* alternate file name */
6278 case '=': /* expression */
6279 case ':': /* last command line */
6280 case '/': /* last search-pattern */
6281 case '.': /* last inserted text */
6282#ifdef FEAT_SEARCHPATH
6283 case Ctrl_F: /* Filename under cursor */
6284 case Ctrl_P: /* Path under cursor, expand via "path" */
6285#endif
6286 case Ctrl_W: /* word under cursor */
6287 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6288 case '_': /* black hole: always empty */
6289 return MCHAR;
6290 }
6291
6292#ifdef FEAT_CLIPBOARD
6293 regname = may_get_selection(regname);
6294#endif
6295
Bram Moolenaar32b92012014-01-14 12:33:36 +01006296 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006297 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 get_yank_register(regname, FALSE);
6300
6301 if (y_current->y_array != NULL)
6302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (reglen != NULL && y_current->y_type == MBLOCK)
6304 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 return y_current->y_type;
6306 }
6307 return MAUTO;
6308}
6309
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006310/*
6311 * When "flags" has GREG_LIST return a list with text "s".
6312 * Otherwise just return "s".
6313 */
6314 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006315getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006316{
6317 if (flags & GREG_LIST)
6318 {
6319 list_T *list = list_alloc();
6320
6321 if (list != NULL)
6322 {
6323 if (list_append_string(list, NULL, -1) == FAIL)
6324 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006325 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006326 return NULL;
6327 }
6328 list->lv_first->li_tv.vval.v_string = s;
6329 }
6330 return (char_u *)list;
6331 }
6332 return s;
6333}
6334
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335/*
6336 * Return the contents of a register as a single allocated string.
6337 * Used for "@r" in expressions and for getreg().
6338 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006339 * Flags:
6340 * GREG_NO_EXPR Do not allow expression register
6341 * GREG_EXPR_SRC For the expression register: return expression itself,
6342 * not the result of its evaluation.
6343 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 */
6345 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006346get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 long i;
6349 char_u *retval;
6350 int allocated;
6351 long len;
6352
6353 /* Don't allow using an expression register inside an expression */
6354 if (regname == '=')
6355 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006356 if (flags & GREG_NO_EXPR)
6357 return NULL;
6358 if (flags & GREG_EXPR_SRC)
6359 return getreg_wrap_one_line(get_expr_line_src(), flags);
6360 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362
6363 if (regname == '@') /* "@@" is used for unnamed register */
6364 regname = '"';
6365
6366 /* check for valid regname */
6367 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6368 return NULL;
6369
6370#ifdef FEAT_CLIPBOARD
6371 regname = may_get_selection(regname);
6372#endif
6373
6374 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6375 {
6376 if (retval == NULL)
6377 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006378 if (allocated)
6379 return getreg_wrap_one_line(retval, flags);
6380 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382
6383 get_yank_register(regname, FALSE);
6384 if (y_current->y_array == NULL)
6385 return NULL;
6386
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006387 if (flags & GREG_LIST)
6388 {
6389 list_T *list = list_alloc();
6390 int error = FALSE;
6391
6392 if (list == NULL)
6393 return NULL;
6394 for (i = 0; i < y_current->y_size; ++i)
6395 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6396 error = TRUE;
6397 if (error)
6398 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006399 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006400 return NULL;
6401 }
6402 return (char_u *)list;
6403 }
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 /*
6406 * Compute length of resulting string.
6407 */
6408 len = 0;
6409 for (i = 0; i < y_current->y_size; ++i)
6410 {
6411 len += (long)STRLEN(y_current->y_array[i]);
6412 /*
6413 * Insert a newline between lines and after last line if
6414 * y_type is MLINE.
6415 */
6416 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6417 ++len;
6418 }
6419
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006420 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 /*
6423 * Copy the lines of the yank register into the string.
6424 */
6425 if (retval != NULL)
6426 {
6427 len = 0;
6428 for (i = 0; i < y_current->y_size; ++i)
6429 {
6430 STRCPY(retval + len, y_current->y_array[i]);
6431 len += (long)STRLEN(retval + len);
6432
6433 /*
6434 * Insert a NL between lines and after the last line if y_type is
6435 * MLINE.
6436 */
6437 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6438 retval[len++] = '\n';
6439 }
6440 retval[len] = NUL;
6441 }
6442
6443 return retval;
6444}
6445
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006446 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006447init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006448 int name,
6449 yankreg_T **old_y_previous,
6450 yankreg_T **old_y_current,
6451 int must_append,
6452 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006453{
6454 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6455 {
6456 emsg_invreg(name);
6457 return FAIL;
6458 }
6459
6460 /* Don't want to change the current (unnamed) register */
6461 *old_y_previous = y_previous;
6462 *old_y_current = y_current;
6463
6464 get_yank_register(name, TRUE);
6465 if (!y_append && !must_append)
6466 free_yank_all();
6467 return OK;
6468}
6469
6470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006471finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006472 int name,
6473 yankreg_T *old_y_previous,
6474 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006475{
6476# ifdef FEAT_CLIPBOARD
6477 /* Send text of clipboard register to the clipboard. */
6478 may_set_selection();
6479# endif
6480
6481 /* ':let @" = "val"' should change the meaning of the "" register */
6482 if (name != '"')
6483 y_previous = old_y_previous;
6484 y_current = old_y_current;
6485}
6486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487/*
6488 * Store string "str" in register "name".
6489 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6490 * If "must_append" is TRUE, always append to the register. Otherwise append
6491 * if "name" is an uppercase letter.
6492 * Note: "maxlen" and "must_append" don't work for the "/" register.
6493 * Careful: 'str' is modified, you may have to use a copy!
6494 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6495 */
6496 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006497write_reg_contents(
6498 int name,
6499 char_u *str,
6500 int maxlen,
6501 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6504}
6505
6506 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006507write_reg_contents_lst(
6508 int name,
6509 char_u **strings,
6510 int maxlen UNUSED,
6511 int must_append,
6512 int yank_type,
6513 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006514{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006515 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006516
6517 if (name == '/'
6518#ifdef FEAT_EVAL
6519 || name == '='
6520#endif
6521 )
6522 {
6523 char_u *s;
6524
6525 if (strings[0] == NULL)
6526 s = (char_u *)"";
6527 else if (strings[1] != NULL)
6528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006529 emsg(_("E883: search pattern and expression register may not "
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006530 "contain two or more lines"));
6531 return;
6532 }
6533 else
6534 s = strings[0];
6535 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6536 return;
6537 }
6538
6539 if (name == '_') /* black hole: nothing to do */
6540 return;
6541
6542 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6543 &yank_type) == FAIL)
6544 return;
6545
6546 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6547
6548 finish_write_reg(name, old_y_previous, old_y_current);
6549}
6550
6551 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006552write_reg_contents_ex(
6553 int name,
6554 char_u *str,
6555 int maxlen,
6556 int must_append,
6557 int yank_type,
6558 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006560 yankreg_T *old_y_previous, *old_y_current;
6561 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562
Bram Moolenaare7566042005-06-17 22:00:15 +00006563 if (maxlen >= 0)
6564 len = maxlen;
6565 else
6566 len = (long)STRLEN(str);
6567
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 /* Special case: '/' search pattern */
6569 if (name == '/')
6570 {
6571 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6572 return;
6573 }
6574
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006575 if (name == '#')
6576 {
6577 buf_T *buf;
6578
6579 if (VIM_ISDIGIT(*str))
6580 {
6581 int num = atoi((char *)str);
6582
6583 buf = buflist_findnr(num);
6584 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006585 semsg(_(e_nobufnr), (long)num);
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006586 }
6587 else
6588 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6589 TRUE, FALSE, FALSE));
6590 if (buf == NULL)
6591 return;
6592 curwin->w_alt_fnum = buf->b_fnum;
6593 return;
6594 }
6595
Bram Moolenaare7566042005-06-17 22:00:15 +00006596#ifdef FEAT_EVAL
6597 if (name == '=')
6598 {
6599 char_u *p, *s;
6600
6601 p = vim_strnsave(str, (int)len);
6602 if (p == NULL)
6603 return;
6604 if (must_append)
6605 {
6606 s = concat_str(get_expr_line_src(), p);
6607 vim_free(p);
6608 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006609 }
6610 set_expr_line(p);
6611 return;
6612 }
6613#endif
6614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (name == '_') /* black hole: nothing to do */
6616 return;
6617
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006618 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6619 &yank_type) == FAIL)
6620 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006622 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006624 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
6626#endif /* FEAT_EVAL */
6627
6628#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6629/*
6630 * Put a string into a register. When the register is not empty, the string
6631 * is appended.
6632 */
6633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006634str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006635 yankreg_T *y_ptr, /* pointer to yank register */
6636 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6637 char_u *str, /* string to put in register */
6638 long len, /* length of string */
6639 long blocklen, /* width of Visual block */
6640 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006642 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 int lnum;
6644 long start;
6645 long i;
6646 int extra;
6647 int newlines; /* number of lines added */
6648 int extraline = 0; /* extra line at the end */
6649 int append = FALSE; /* append to last line in register */
6650 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006651 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006655 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 y_ptr->y_size = 0;
6657
Bram Moolenaard44347f2011-06-19 01:14:29 +02006658 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006659 type = ((str_list || (len > 0 && (str[len - 1] == NL
6660 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006661 ? MLINE : MCHAR);
6662 else
6663 type = yank_type;
6664
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 /*
6666 * Count the number of lines within the string
6667 */
6668 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006669 if (str_list)
6670 {
6671 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006676 for (i = 0; i < len; i++)
6677 if (str[i] == '\n')
6678 ++newlines;
6679 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6680 {
6681 extraline = 1;
6682 ++newlines; /* count extra newline at the end */
6683 }
6684 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6685 {
6686 append = TRUE;
6687 --newlines; /* uncount newline when appending first line */
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006691 /* Without any lines make the register empty. */
6692 if (y_ptr->y_size + newlines == 0)
6693 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006694 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006695 return;
6696 }
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 /*
6699 * Allocate an array to hold the pointers to the new register lines.
6700 * If the register was not empty, move the existing lines to the new array.
6701 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006702 pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 if (pp == NULL) /* out of memory */
6704 return;
6705 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6706 pp[lnum] = y_ptr->y_array[lnum];
6707 vim_free(y_ptr->y_array);
6708 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
6711 /*
6712 * Find the end of each line and save it into the array.
6713 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006714 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006716 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6717 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006718 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006719 pp[lnum] = vim_strnsave(*ss, i);
6720 if (i > maxlen)
6721 maxlen = i;
6722 }
6723 }
6724 else
6725 {
6726 for (start = 0; start < len + extraline; start += i + 1)
6727 {
6728 for (i = start; i < len; ++i) /* find the end of the line */
6729 if (str[i] == '\n')
6730 break;
6731 i -= start; /* i is now length of line */
6732 if (i > maxlen)
6733 maxlen = i;
6734 if (append)
6735 {
6736 --lnum;
6737 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6738 }
6739 else
6740 extra = 0;
Bram Moolenaar964b3742019-05-24 18:54:09 +02006741 s = alloc(i + extra + 1);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006742 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006744 if (extra)
6745 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6746 if (append)
6747 vim_free(y_ptr->y_array[lnum]);
6748 if (i)
6749 mch_memmove(s + extra, str + start, (size_t)i);
6750 extra += i;
6751 s[extra] = NUL;
6752 y_ptr->y_array[lnum++] = s;
6753 while (--extra >= 0)
6754 {
6755 if (*s == NUL)
6756 *s = '\n'; /* replace NUL with newline */
6757 ++s;
6758 }
6759 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
6762 y_ptr->y_type = type;
6763 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (type == MBLOCK)
6765 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6766 else
6767 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006768#ifdef FEAT_VIMINFO
6769 y_ptr->y_time_set = vim_time();
6770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6773
6774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006775clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 vim_memset(oap, 0, sizeof(oparg_T));
6778}
6779
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006781 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 *
6783 * "Words" are counted by looking for boundaries between non-space and
6784 * space characters. (it seems to produce results that match 'wc'.)
6785 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006786 * Return value is byte count; word count for the line is added to "*wc".
6787 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 *
6789 * The function will only examine the first "limit" characters in the
6790 * line, stopping if it encounters an end-of-line (NUL byte). In that
6791 * case, eol_size will be added to the character count to account for
6792 * the size of the EOL character.
6793 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006794 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006795line_count_info(
6796 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006797 varnumber_T *wc,
6798 varnumber_T *cc,
6799 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01006800 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006802 varnumber_T i;
6803 varnumber_T words = 0;
6804 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 int is_word = 0;
6806
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006807 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 if (is_word)
6810 {
6811 if (vim_isspace(line[i]))
6812 {
6813 words++;
6814 is_word = 0;
6815 }
6816 }
6817 else if (!vim_isspace(line[i]))
6818 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006819 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006820 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822
6823 if (is_word)
6824 words++;
6825 *wc += words;
6826
6827 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006828 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006831 chars += eol_size;
6832 }
6833 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 return i;
6835}
6836
6837/*
6838 * Give some info about the position of the cursor (for "g CTRL-G").
6839 * In Visual mode, give some info about the selected region. (In this case,
6840 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01006841 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 */
6843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006844cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006847 char_u buf1[50];
6848 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006850 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006851 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006852 varnumber_T byte_count_cursor = 0;
6853 varnumber_T char_count = 0;
6854 varnumber_T char_count_cursor = 0;
6855 varnumber_T word_count = 0;
6856 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006857 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006858 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 long line_count_selected = 0;
6860 pos_T min_pos, max_pos;
6861 oparg_T oparg;
6862 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
6864 /*
6865 * Compute the length of the file in characters.
6866 */
6867 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6868 {
Bram Moolenaared767a22016-01-03 22:49:16 +01006869 if (dict == NULL)
6870 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006871 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01006872 return;
6873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875 else
6876 {
6877 if (get_fileformat(curbuf) == EOL_DOS)
6878 eol_size = 2;
6879 else
6880 eol_size = 1;
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 if (VIsual_active)
6883 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006884 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 min_pos = VIsual;
6887 max_pos = curwin->w_cursor;
6888 }
6889 else
6890 {
6891 min_pos = curwin->w_cursor;
6892 max_pos = VIsual;
6893 }
6894 if (*p_sel == 'e' && max_pos.col > 0)
6895 --max_pos.col;
6896
6897 if (VIsual_mode == Ctrl_V)
6898 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006899#ifdef FEAT_LINEBREAK
6900 char_u * saved_sbr = p_sbr;
6901
6902 /* Make 'sbr' empty for a moment to get the correct size. */
6903 p_sbr = empty_option;
6904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 oparg.is_VIsual = 1;
6906 oparg.block_mode = TRUE;
6907 oparg.op_type = OP_NOP;
6908 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006909 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006910#ifdef FEAT_LINEBREAK
6911 p_sbr = saved_sbr;
6912#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006913 if (curwin->w_curswant == MAXCOL)
6914 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 /* Swap the start, end vcol if needed */
6916 if (oparg.end_vcol < oparg.start_vcol)
6917 {
6918 oparg.end_vcol += oparg.start_vcol;
6919 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6920 oparg.end_vcol -= oparg.start_vcol;
6921 }
6922 }
6923 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6927 {
6928 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006929 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
6931 ui_breakcheck();
6932 if (got_int)
6933 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006934 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 /* Do extra processing for VIsual mode. */
6938 if (VIsual_active
6939 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6940 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006941 char_u *s = NULL;
6942 long len = 0L;
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 switch (VIsual_mode)
6945 {
6946 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006950 s = bd.textstart;
6951 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 break;
6953 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006954 s = ml_get(lnum);
6955 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 break;
6957 case 'v':
6958 {
6959 colnr_T start_col = (lnum == min_pos.lnum)
6960 ? min_pos.col : 0;
6961 colnr_T end_col = (lnum == max_pos.lnum)
6962 ? max_pos.col - start_col + 1 : MAXCOL;
6963
Bram Moolenaardef9e822004-12-31 20:58:58 +00006964 s = ml_get(lnum) + start_col;
6965 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
6967 break;
6968 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006969 if (s != NULL)
6970 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006971 byte_count_cursor += line_count_info(s, &word_count_cursor,
6972 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006973 if (lnum == curbuf->b_ml.ml_line_count
6974 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006975 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006976 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006977 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
6982 /* In non-visual mode, check for the line the cursor is on */
6983 if (lnum == curwin->w_cursor.lnum)
6984 {
6985 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006986 char_count_cursor += char_count;
6987 byte_count_cursor = byte_count +
6988 line_count_info(ml_get(lnum),
6989 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006990 (varnumber_T)(curwin->w_cursor.col + 1),
6991 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 }
6993 }
6994 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006995 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006996 &char_count, (varnumber_T)MAXCOL,
6997 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 }
6999
7000 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007001 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007002 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
Bram Moolenaared767a22016-01-03 22:49:16 +01007004 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007006 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007008 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7009 {
7010 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7011 &max_pos.col);
7012 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7013 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7014 }
7015 else
7016 buf1[0] = NUL;
7017
7018 if (char_count_cursor == byte_count_cursor
7019 && char_count == byte_count)
7020 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007021 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007022 buf1, line_count_selected,
7023 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007024 (long_long_T)word_count_cursor,
7025 (long_long_T)word_count,
7026 (long_long_T)byte_count_cursor,
7027 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007028 else
7029 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007030 _("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 +01007031 buf1, line_count_selected,
7032 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007033 (long_long_T)word_count_cursor,
7034 (long_long_T)word_count,
7035 (long_long_T)char_count_cursor,
7036 (long_long_T)char_count,
7037 (long_long_T)byte_count_cursor,
7038 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
7040 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007041 {
7042 p = ml_get_curline();
7043 validate_virtcol();
7044 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7045 (int)curwin->w_virtcol + 1);
7046 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7047 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
Bram Moolenaared767a22016-01-03 22:49:16 +01007049 if (char_count_cursor == byte_count_cursor
7050 && char_count == byte_count)
7051 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007052 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007053 (char *)buf1, (char *)buf2,
7054 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007056 (long_long_T)word_count_cursor, (long_long_T)word_count,
7057 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007058 else
7059 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007060 _("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 +01007061 (char *)buf1, (char *)buf2,
7062 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007063 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007064 (long_long_T)word_count_cursor, (long_long_T)word_count,
7065 (long_long_T)char_count_cursor, (long_long_T)char_count,
7066 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007067 }
7068 }
7069
Bram Moolenaared767a22016-01-03 22:49:16 +01007070 bom_count = bomb_size();
7071 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007072 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007073 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007074 if (dict == NULL)
7075 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007076 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007077 p = p_shm;
7078 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01007079 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01007080 p_shm = p;
7081 }
7082 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007083#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007084 if (dict != NULL)
7085 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007086 dict_add_number(dict, "words", word_count);
7087 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007088 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007089 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7090 byte_count_cursor);
7091 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7092 char_count_cursor);
7093 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7094 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097}