blob: 004d5093c213b622a3e1ab9cb78c429bdef64252 [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 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020053 * Each yank register has an array of pointers to lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020055typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 colnr_T y_width; /* only set if y_type == MBLOCK */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020061#ifdef FEAT_VIMINFO
62 time_t y_time_set;
63#endif
64} yankreg_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020066static yankreg_T y_regs[NUM_REGISTERS];
67
68static yankreg_T *y_current; /* ptr to current yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int y_append; /* TRUE when appending */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020070static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
73 * structure used by block_prep, op_delete and op_yank for blockwise operators
74 * also op_change, op_shift, op_insert, op_replace - AKelly
75 */
76struct block_def
77{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000078 int startspaces; /* 'extra' cols before first char */
79 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000081 char_u *textstart; /* pointer to 1st char (partially) in block */
82 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 colnr_T start_vcol; /* start col of 1st char wholly inside block */
84 colnr_T end_vcol; /* start col of 1st char wholly after block */
85#ifdef FEAT_VISUALEXTRA
86 int is_short; /* TRUE if line is too short to fit in block */
87 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
88 int is_oneChar; /* TRUE if block within one character */
89 int pre_whitesp; /* screen cols of ws before block */
90 int pre_whitesp_c; /* chars of ws before block */
91 colnr_T end_char_vcols; /* number of vcols of post-block char */
92#endif
93 colnr_T start_char_vcols; /* number of vcols of pre-block char */
94};
95
96#ifdef FEAT_VISUALEXTRA
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010097static void shift_block(oparg_T *oap, int amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010099static int stuff_yank(int, char_u *);
100static void put_reedit_in_typebuf(int silent);
101static int put_in_typebuf(char_u *s, int esc, int colon,
102 int silent);
103static void stuffescaped(char_u *arg, int literally);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100105static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100107static void free_yank_all(void);
108static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200110static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100111static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100113static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100114static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
115static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200117static 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 +0000118#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100121static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#endif
125
Bram Moolenaarf2732452018-06-03 14:47:35 +0200126// Flags for third item in "opchars".
127#define OPF_LINES 1 // operator always works on lines
128#define OPF_CHANGE 2 // operator changes text
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130/*
131 * The names of operators.
132 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +0200133 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
135static char opchars[][3] =
136{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200137 {NUL, NUL, 0}, // OP_NOP
138 {'d', NUL, OPF_CHANGE}, // OP_DELETE
139 {'y', NUL, 0}, // OP_YANK
140 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
141 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
142 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
143 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
144 {'g', '~', OPF_CHANGE}, // OP_TILDE
145 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
146 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
147 {':', NUL, OPF_LINES}, // OP_COLON
148 {'g', 'U', OPF_CHANGE}, // OP_UPPER
149 {'g', 'u', OPF_CHANGE}, // OP_LOWER
150 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
151 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
152 {'g', '?', OPF_CHANGE}, // OP_ROT13
153 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
154 {'I', NUL, OPF_CHANGE}, // OP_INSERT
155 {'A', NUL, OPF_CHANGE}, // OP_APPEND
156 {'z', 'f', OPF_LINES}, // OP_FOLD
157 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
158 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
159 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
160 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
161 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
162 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
163 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
164 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
165 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
166 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167};
168
169/*
170 * Translate a command name into an operator type.
171 * Must only be called with a valid operator name!
172 */
173 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100174get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175{
176 int i;
177
178 if (char1 == 'r') /* ignore second character */
179 return OP_REPLACE;
180 if (char1 == '~') /* when tilde is an operator */
181 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100182 if (char1 == 'g' && char2 == Ctrl_A) /* add */
183 return OP_NR_ADD;
184 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
185 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (opchars[i][0] == char1 && opchars[i][1] == char2)
189 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100190 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
191 {
192 internal_error("get_op_type()");
193 break;
194 }
195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 return i;
197}
198
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199/*
200 * Return TRUE if operator "op" always works on whole lines.
201 */
202 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100203op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200205 return opchars[op][2] & OPF_LINES;
206}
207
208/*
209 * Return TRUE if operator "op" changes text.
210 */
211 int
212op_is_change(int op)
213{
214 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
217/*
218 * Get first operator command character.
219 * Returns 'g' or 'z' if there is another command character.
220 */
221 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100222get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 return opchars[optype][0];
225}
226
227/*
228 * Get second operator command character.
229 */
230 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100231get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
233 return opchars[optype][1];
234}
235
236/*
237 * op_shift - handle a shift operation
238 */
239 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100240op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241{
242 long i;
243 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245
246 if (u_save((linenr_T)(oap->start.lnum - 1),
247 (linenr_T)(oap->end.lnum + 1)) == FAIL)
248 return;
249
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 if (oap->block_mode)
251 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253 for (i = oap->line_count; --i >= 0; )
254 {
255 first_char = *ml_get_curline();
256 if (first_char == NUL) /* empty line */
257 curwin->w_cursor.col = 0;
258#ifdef FEAT_VISUALEXTRA
259 else if (oap->block_mode)
260 shift_block(oap, amount);
261#endif
262 else
263 /* Move the line right if it doesn't start with '#', 'smartindent'
264 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
265#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
266 if (first_char != '#' || !preprocs_left())
267#endif
268 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000269 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 }
271 ++curwin->w_cursor.lnum;
272 }
273
274 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 if (oap->block_mode)
276 {
277 curwin->w_cursor.lnum = oap->start.lnum;
278 curwin->w_cursor.col = block_col;
279 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100280 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 {
282 curwin->w_cursor.lnum = oap->start.lnum;
283 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
284 }
285 else
286 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
287
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100288#ifdef FEAT_FOLDING
289 /* The cursor line is not in a closed fold */
290 foldOpenCursor();
291#endif
292
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if (oap->line_count > p_report)
295 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200296 char *op;
297 char *msg_line_single;
298 char *msg_line_plural;
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200301 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200303 op = "<";
304 msg_line_single = NGETTEXT("%ld line %sed %d time",
305 "%ld line %sed %d times", amount);
306 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
307 "%ld lines %sed %d times", amount);
308 vim_snprintf((char *)IObuff, IOSIZE,
309 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
310 oap->line_count, op, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 msg(IObuff);
312 }
313
314 /*
315 * Set "'[" and "']" marks.
316 */
317 curbuf->b_op_start = oap->start;
318 curbuf->b_op_end.lnum = oap->end.lnum;
319 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
320 if (curbuf->b_op_end.col > 0)
321 --curbuf->b_op_end.col;
322}
323
324/*
325 * shift the current line one shiftwidth left (if left != 0) or right
326 * leaves cursor on first blank in the line
327 */
328 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100329shift_line(
330 int left,
331 int round,
332 int amount,
333 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
335 int count;
336 int i, j;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100337 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
339 count = get_indent(); /* get current indent */
340
341 if (round) /* round off indent */
342 {
343 i = count / p_sw; /* number of p_sw rounded down */
344 j = count % p_sw; /* extra spaces */
345 if (j && left) /* first remove extra spaces */
346 --amount;
347 if (left)
348 {
349 i -= amount;
350 if (i < 0)
351 i = 0;
352 }
353 else
354 i += amount;
355 count = i * p_sw;
356 }
357 else /* original vi indent */
358 {
359 if (left)
360 {
361 count -= p_sw * amount;
362 if (count < 0)
363 count = 0;
364 }
365 else
366 count += p_sw * amount;
367 }
368
369 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000371 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000373 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374}
375
376#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
377/*
378 * Shift one line of the current block one shiftwidth right or left.
379 * Leaves cursor on first character in block.
380 */
381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100382shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383{
384 int left = (oap->op_type == OP_LSHIFT);
385 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000386 int total;
387 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 int oldcol = curwin->w_cursor.col;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100389 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200390#ifdef FEAT_VARTABS
391 int *p_vts = curbuf->b_p_vts_array;
392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 int p_ts = (int)curbuf->b_p_ts;
394 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000396 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 int i = 0, j = 0;
398 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#ifdef FEAT_RIGHTLEFT
400 int old_p_ri = p_ri;
401
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200402 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
404
405 State = INSERT; /* don't want REPLACE for State */
406 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
407 if (bd.is_short)
408 return;
409
410 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200411 total = (int)((unsigned)amount * (unsigned)p_sw);
412 if ((total / p_sw) != amount)
413 return; /* multiplication overflow */
414
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 oldp = ml_get_curline();
416
417 if (!left)
418 {
419 /*
420 * 1. Get start vcol
421 * 2. Total ws vcols
422 * 3. Divvy into TABs & spp
423 * 4. Construct new string
424 */
425 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
426 ws_vcol = bd.start_vcol - bd.pre_whitesp;
427 if (bd.startspaces)
428 {
429#ifdef FEAT_MBYTE
430 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100431 {
432 if ((*mb_ptr2len)(bd.textstart) == 1)
433 ++bd.textstart;
434 else
435 {
436 ws_vcol = 0;
437 bd.startspaces = 0;
438 }
439 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000442 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100444 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200446 /* TODO: is passing bd.textstart for start of the line OK? */
447 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
448 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 total += incr;
450 bd.start_vcol += incr;
451 }
452 /* OK, now total=all the VWS reqd, and textstart points at the 1st
453 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200454#ifdef FEAT_VARTABS
455 if (!curbuf->b_p_et)
456 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
457 else
458 j = total;
459#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 if (!curbuf->b_p_et)
461 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
462 if (i)
463 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
464 else
465 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 /* if we're splitting a TAB, allow for it */
468 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
469 len = (int)STRLEN(bd.textstart) + 1;
470 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
471 if (newp == NULL)
472 return;
473 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
474 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200475 vim_memset(newp + bd.textcol, TAB, (size_t)i);
476 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 /* the end */
478 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
479 }
480 else /* left */
481 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000482 colnr_T destination_col; /* column to which text in block will
483 be shifted */
484 char_u *verbatim_copy_end; /* end of the part of the line which is
485 copied verbatim */
486 colnr_T verbatim_copy_width;/* the (displayed) width of this part
487 of line */
488 unsigned fill; /* nr of spaces that replace a TAB */
489 unsigned new_line_len; /* the length of the line after the
490 block shift */
491 size_t block_space_width;
492 size_t shift_amount;
493 char_u *non_white = bd.textstart;
494 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000496 /*
497 * Firstly, let's find the first non-whitespace character that is
498 * displayed after the block's start column and the character's column
499 * number. Also, let's calculate the width of all the whitespace
500 * characters that are displayed in the block and precede the searched
501 * non-whitespace character.
502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000504 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
505 * the part of which is displayed at the block's beginning. Let's start
506 * searching from the next character. */
507 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100508 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000509
510 /* The character's column is in "bd.start_vcol". */
511 non_white_col = bd.start_vcol;
512
Bram Moolenaar1c465442017-03-12 20:10:05 +0100513 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000514 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200515 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000516 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000519 block_space_width = non_white_col - oap->start_vcol;
520 /* We will shift by "total" or "block_space_width", whichever is less.
521 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000522 shift_amount = (block_space_width < (size_t)total
523 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000524
525 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000526 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000527
528 /* Now let's find out how much of the beginning of the line we can
529 * reuse without modification. */
530 verbatim_copy_end = bd.textstart;
531 verbatim_copy_width = bd.start_vcol;
532
533 /* If "bd.startspaces" is set, "bd.textstart" points to the character
534 * preceding the block. We have to subtract its width to obtain its
535 * column number. */
536 if (bd.startspaces)
537 verbatim_copy_width -= bd.start_char_vcols;
538 while (verbatim_copy_width < destination_col)
539 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200540 char_u *line = verbatim_copy_end;
541
542 /* TODO: is passing verbatim_copy_end for start of the line OK? */
543 incr = lbr_chartabsize(line, verbatim_copy_end,
544 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000545 if (verbatim_copy_width + incr > destination_col)
546 break;
547 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100548 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000549 }
550
551 /* If "destination_col" is different from the width of the initial
552 * part of the line that will be copied, it means we encountered a tab
553 * character, which we will have to partly replace with spaces. */
554 fill = destination_col - verbatim_copy_width;
555
556 /* The replacement line will consist of:
557 * - the beginning of the original line up to "verbatim_copy_end",
558 * - "fill" number of spaces,
559 * - the rest of the line, pointed to by non_white. */
560 new_line_len = (unsigned)(verbatim_copy_end - oldp)
561 + fill
562 + (unsigned)STRLEN(non_white) + 1;
563
564 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (newp == NULL)
566 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000567 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200568 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000569 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
571 /* replace the line */
572 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
573 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
574 State = oldstate;
575 curwin->w_cursor.col = oldcol;
576#ifdef FEAT_RIGHTLEFT
577 p_ri = old_p_ri;
578#endif
579}
580#endif
581
582#ifdef FEAT_VISUALEXTRA
583/*
584 * Insert string "s" (b_insert ? before : after) block :AKelly
585 * Caller must prepare for undo.
586 */
587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100588block_insert(
589 oparg_T *oap,
590 char_u *s,
591 int b_insert,
592 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 int p_ts;
595 int count = 0; /* extra spaces to replace a cut TAB */
596 int spaces = 0; /* non-zero if cutting a TAB */
597 colnr_T offset; /* pointer along new line */
598 unsigned s_len; /* STRLEN(s) */
599 char_u *newp, *oldp; /* new, old lines */
600 linenr_T lnum; /* loop var */
601 int oldstate = State;
602
603 State = INSERT; /* don't want REPLACE for State */
604 s_len = (unsigned)STRLEN(s);
605
606 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
607 {
608 block_prep(oap, bdp, lnum, TRUE);
609 if (bdp->is_short && b_insert)
610 continue; /* OP_INSERT, line ends before block start */
611
612 oldp = ml_get(lnum);
613
614 if (b_insert)
615 {
616 p_ts = bdp->start_char_vcols;
617 spaces = bdp->startspaces;
618 if (spaces != 0)
619 count = p_ts - 1; /* we're cutting a TAB */
620 offset = bdp->textcol;
621 }
622 else /* append */
623 {
624 p_ts = bdp->end_char_vcols;
625 if (!bdp->is_short) /* spaces = padding after block */
626 {
627 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
628 if (spaces != 0)
629 count = p_ts - 1; /* we're cutting a TAB */
630 offset = bdp->textcol + bdp->textlen - (spaces != 0);
631 }
632 else /* spaces = padding to block edge */
633 {
634 /* if $ used, just append to EOL (ie spaces==0) */
635 if (!bdp->is_MAX)
636 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
637 count = spaces;
638 offset = bdp->textcol + bdp->textlen;
639 }
640 }
641
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200642#ifdef FEAT_MBYTE
643 if (has_mbyte && spaces > 0)
644 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100645 int off;
646
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200647 /* Avoid starting halfway a multi-byte character. */
648 if (b_insert)
649 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100650 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200651 }
652 else
653 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100654 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200655 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200656 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100657 spaces -= off;
658 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200659 }
660#endif
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
663 if (newp == NULL)
664 continue;
665
666 /* copy up to shifted part */
667 mch_memmove(newp, oldp, (size_t)(offset));
668 oldp += offset;
669
670 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200671 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
673 /* copy the new text */
674 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
675 offset += s_len;
676
677 if (spaces && !bdp->is_short)
678 {
679 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200680 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 /* We're splitting a TAB, don't copy it. */
682 oldp++;
683 /* We allowed for that TAB, remember this now */
684 count++;
685 }
686
687 if (spaces > 0)
688 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000689 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 ml_replace(lnum, newp, FALSE);
692
693 if (lnum == oap->end.lnum)
694 {
695 /* Set "']" mark to the end of the block instead of the end of
696 * the insert in the first line. */
697 curbuf->b_op_end.lnum = oap->end.lnum;
698 curbuf->b_op_end.col = offset;
699 }
700 } /* for all lnum */
701
702 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
703
704 State = oldstate;
705}
706#endif
707
708#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
709/*
710 * op_reindent - handle reindenting a block of lines.
711 */
712 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100713op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 long i;
716 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200717 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 linenr_T first_changed = 0;
719 linenr_T last_changed = 0;
720 linenr_T start_lnum = curwin->w_cursor.lnum;
721
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000722 /* Don't even try when 'modifiable' is off. */
723 if (!curbuf->b_p_ma)
724 {
725 EMSG(_(e_modifiable));
726 return;
727 }
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 for (i = oap->line_count; --i >= 0 && !got_int; )
730 {
731 /* it's a slow thing to do, so give feedback so there's no worry that
732 * the computer's just hung. */
733
734 if (i > 1
735 && (i % 50 == 0 || i == oap->line_count - 1)
736 && oap->line_count > p_report)
737 smsg((char_u *)_("%ld lines to indent... "), i);
738
739 /*
740 * Be vi-compatible: For lisp indenting the first line is not
741 * indented, unless there is only one line.
742 */
743#ifdef FEAT_LISP
744 if (i != oap->line_count - 1 || oap->line_count == 1
745 || how != get_lisp_indent)
746#endif
747 {
748 l = skipwhite(ml_get_curline());
749 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200750 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200752 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200754 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 /* did change the indent, call changed_lines() later */
757 if (first_changed == 0)
758 first_changed = curwin->w_cursor.lnum;
759 last_changed = curwin->w_cursor.lnum;
760 }
761 }
762 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000763 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 }
765
766 /* put cursor on first non-blank of indented line */
767 curwin->w_cursor.lnum = start_lnum;
768 beginline(BL_SOL | BL_FIX);
769
770 /* Mark changed lines so that they will be redrawn. When Visual
771 * highlighting was present, need to continue until the last line. When
772 * there is no change still need to remove the Visual highlighting. */
773 if (last_changed != 0)
774 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 else if (oap->is_VIsual)
778 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 if (oap->line_count > p_report)
781 {
782 i = oap->line_count - (i + 1);
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200783 smsg((char_u *)NGETTEXT("%ld line indented ",
784 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 /* set '[ and '] marks */
787 curbuf->b_op_start = oap->start;
788 curbuf->b_op_end = oap->end;
789}
790#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
791
792#if defined(FEAT_EVAL) || defined(PROTO)
793/*
794 * Keep the last expression line here, for repeating.
795 */
796static char_u *expr_line = NULL;
797
798/*
799 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
800 * Returns '=' when OK, NUL otherwise.
801 */
802 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 char_u *new_line;
806
807 new_line = getcmdline('=', 0L, 0);
808 if (new_line == NULL)
809 return NUL;
810 if (*new_line == NUL) /* use previous line */
811 vim_free(new_line);
812 else
813 set_expr_line(new_line);
814 return '=';
815}
816
817/*
818 * Set the expression for the '=' register.
819 * Argument must be an allocated string.
820 */
821 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100822set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
824 vim_free(expr_line);
825 expr_line = new_line;
826}
827
828/*
829 * Get the result of the '=' register expression.
830 * Returns a pointer to allocated memory, or NULL for failure.
831 */
832 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100833get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 char_u *expr_copy;
836 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000837 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 if (expr_line == NULL)
840 return NULL;
841
842 /* Make a copy of the expression, because evaluating it may cause it to be
843 * changed. */
844 expr_copy = vim_strsave(expr_line);
845 if (expr_copy == NULL)
846 return NULL;
847
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000848 /* When we are invoked recursively limit the evaluation to 10 levels.
849 * Then return the string as-is. */
850 if (nested >= 10)
851 return expr_copy;
852
853 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000854 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000855 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 vim_free(expr_copy);
857 return rv;
858}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000859
860/*
861 * Get the '=' register expression itself, without evaluating it.
862 */
863 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100864get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000865{
866 if (expr_line == NULL)
867 return NULL;
868 return vim_strsave(expr_line);
869}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#endif /* FEAT_EVAL */
871
872/*
873 * Check if 'regname' is a valid name of a yank register.
874 * Note: There is no check for 0 (default register), caller should do this
875 */
876 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877valid_yank_reg(
878 int regname,
879 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
881 if ( (regname > 0 && ASCII_ISALNUM(regname))
882 || (!writing && vim_strchr((char_u *)
883#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100884 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100886 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100889 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 || regname == '"'
891 || regname == '-'
892 || regname == '_'
893#ifdef FEAT_CLIPBOARD
894 || regname == '*'
895 || regname == '+'
896#endif
897#ifdef FEAT_DND
898 || (!writing && regname == '~')
899#endif
900 )
901 return TRUE;
902 return FALSE;
903}
904
905/*
906 * Set y_current and y_append, according to the value of "regname".
907 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000908 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 *
910 * If regname is 0 and writing, use register 0
911 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100912 *
913 * Return TRUE when the register should be inserted literally (selection or
914 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100916 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100917get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100920 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921
922 y_append = FALSE;
923 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
924 {
925 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100926 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928 i = regname;
929 if (VIM_ISDIGIT(i))
930 i -= '0';
931 else if (ASCII_ISLOWER(i))
932 i = CharOrdLow(i) + 10;
933 else if (ASCII_ISUPPER(i))
934 {
935 i = CharOrdUp(i) + 10;
936 y_append = TRUE;
937 }
938 else if (regname == '-')
939 i = DELETION_REGISTER;
940#ifdef FEAT_CLIPBOARD
941 /* When selection is not available, use register 0 instead of '*' */
942 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100945 ret = TRUE;
946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 /* When clipboard is not available, use register 0 instead of '+' */
948 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100951 ret = TRUE;
952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953#endif
954#ifdef FEAT_DND
955 else if (!writing && regname == '~')
956 i = TILDE_REGISTER;
957#endif
958 else /* not 0-9, a-z, A-Z or '-': use register 0 */
959 i = 0;
960 y_current = &(y_regs[i]);
961 if (writing) /* remember the register we write into for do_put() */
962 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100963 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
Bram Moolenaar8299df92004-07-10 09:47:34 +0000966#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967/*
968 * When "regname" is a clipboard register, obtain the selection. If it's not
969 * available return zero, otherwise return "regname".
970 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000971 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100972may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 if (regname == '*')
975 {
976 if (!clip_star.available)
977 regname = 0;
978 else
979 clip_get_selection(&clip_star);
980 }
981 else if (regname == '+')
982 {
983 if (!clip_plus.available)
984 regname = 0;
985 else
986 clip_get_selection(&clip_plus);
987 }
988 return regname;
989}
990#endif
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
993 * Obtain the contents of a "normal" register. The register is made empty.
994 * The returned pointer has allocated memory, use put_register() later.
995 */
996 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100997get_register(
998 int name,
999 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001001 yankreg_T *reg;
1002 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004#ifdef FEAT_CLIPBOARD
1005 /* When Visual area changed, may have to update selection. Obtain the
1006 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +00001007 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001009 if (clip_isautosel_star())
1010 clip_update_selection(&clip_star);
1011 may_get_selection(name);
1012 }
1013 if (name == '+' && clip_plus.available)
1014 {
1015 if (clip_isautosel_plus())
1016 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 may_get_selection(name);
1018 }
1019#endif
1020
1021 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001022 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (reg != NULL)
1024 {
1025 *reg = *y_current;
1026 if (copy)
1027 {
1028 /* If we run out of memory some or all of the lines are empty. */
1029 if (reg->y_size == 0)
1030 reg->y_array = NULL;
1031 else
1032 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1033 * reg->y_size));
1034 if (reg->y_array != NULL)
1035 {
1036 for (i = 0; i < reg->y_size; ++i)
1037 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1038 }
1039 }
1040 else
1041 y_current->y_array = NULL;
1042 }
1043 return (void *)reg;
1044}
1045
1046/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001047 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 */
1049 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001050put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 get_yank_register(name, 0);
1053 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001054 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001055 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001057#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 /* Send text written to clipboard register to the clipboard. */
1059 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001062
1063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001064free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001065{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001066 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001067
1068 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001069 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001070 free_yank_all();
1071 vim_free(reg);
1072 *y_current = tmp;
1073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075#if defined(FEAT_MOUSE) || defined(PROTO)
1076/*
1077 * return TRUE if the current yank register has type MLINE
1078 */
1079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001080yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1083 return FALSE;
1084 if (regname == '_') /* black hole is always empty */
1085 return FALSE;
1086 get_yank_register(regname, FALSE);
1087 return (y_current->y_type == MLINE);
1088}
1089#endif
1090
1091/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001092 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001094 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
1096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001097do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
Bram Moolenaard55de222007-05-06 13:38:48 +00001099 char_u *p;
1100 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001101 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001102 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001104 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001106 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1108 retval = FAIL;
1109 else
1110 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001111 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 showmode();
1113 regname = c;
1114 retval = OK;
1115 }
1116 }
1117 else /* stop recording */
1118 {
1119 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001120 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1121 * needs to be removed again to put it in a register. exec_reg then
1122 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001124 reg_recording = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 MSG("");
1126 p = get_recorded();
1127 if (p == NULL)
1128 retval = FAIL;
1129 else
1130 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001131 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1132 vim_unescape_csi(p);
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 /*
1135 * We don't want to change the default register here, so save and
1136 * restore the current register name.
1137 */
1138 old_y_previous = y_previous;
1139 old_y_current = y_current;
1140
1141 retval = stuff_yank(regname, p);
1142
1143 y_previous = old_y_previous;
1144 y_current = old_y_current;
1145 }
1146 }
1147 return retval;
1148}
1149
1150/*
1151 * Stuff string "p" into yank register "regname" as a single line (append if
1152 * uppercase). "p" must have been alloced.
1153 *
1154 * return FAIL for failure, OK otherwise
1155 */
1156 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001157stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 char_u *lp;
1160 char_u **pp;
1161
1162 /* check for read-only register */
1163 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1164 {
1165 vim_free(p);
1166 return FAIL;
1167 }
1168 if (regname == '_') /* black hole: don't do anything */
1169 {
1170 vim_free(p);
1171 return OK;
1172 }
1173 get_yank_register(regname, TRUE);
1174 if (y_append && y_current->y_array != NULL)
1175 {
1176 pp = &(y_current->y_array[y_current->y_size - 1]);
1177 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1178 if (lp == NULL)
1179 {
1180 vim_free(p);
1181 return FAIL;
1182 }
1183 STRCPY(lp, *pp);
1184 STRCAT(lp, p);
1185 vim_free(p);
1186 vim_free(*pp);
1187 *pp = lp;
1188 }
1189 else
1190 {
1191 free_yank_all();
1192 if ((y_current->y_array =
1193 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1194 {
1195 vim_free(p);
1196 return FAIL;
1197 }
1198 y_current->y_array[0] = p;
1199 y_current->y_size = 1;
1200 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001201#ifdef FEAT_VIMINFO
1202 y_current->y_time_set = vim_time();
1203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 return OK;
1206}
1207
Bram Moolenaar42b94362009-05-26 16:12:37 +00001208static int execreg_lastc = NUL;
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001211 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001213 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
1215 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001216do_execreg(
1217 int regname,
1218 int colon, /* insert ':' before each line */
1219 int addcr, /* always add '\n' to end of line */
1220 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 long i;
1223 char_u *p;
1224 int retval = OK;
1225 int remap;
1226
1227 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001228 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001229 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001230 {
1231 EMSG(_("E748: No previously used register"));
1232 return FAIL;
1233 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001234 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 /* check for valid regname */
1237 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001238 {
1239 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001241 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001242 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
1244#ifdef FEAT_CLIPBOARD
1245 regname = may_get_selection(regname);
1246#endif
1247
1248 if (regname == '_') /* black hole: don't stuff anything */
1249 return OK;
1250
1251#ifdef FEAT_CMDHIST
1252 if (regname == ':') /* use last command line */
1253 {
1254 if (last_cmdline == NULL)
1255 {
1256 EMSG(_(e_nolastcmd));
1257 return FAIL;
1258 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001259 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001260 /* Escape all control characters with a CTRL-V */
1261 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001262 (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001263 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001264 {
1265 /* When in Visual mode "'<,'>" will be prepended to the command.
1266 * Remove it when it's already there. */
1267 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001268 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001269 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001270 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001271 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001272 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274#endif
1275#ifdef FEAT_EVAL
1276 else if (regname == '=')
1277 {
1278 p = get_expr_line();
1279 if (p == NULL)
1280 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001281 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 vim_free(p);
1283 }
1284#endif
1285 else if (regname == '.') /* use last inserted text */
1286 {
1287 p = get_last_insert_save();
1288 if (p == NULL)
1289 {
1290 EMSG(_(e_noinstext));
1291 return FAIL;
1292 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001293 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 vim_free(p);
1295 }
1296 else
1297 {
1298 get_yank_register(regname, FALSE);
1299 if (y_current->y_array == NULL)
1300 return FAIL;
1301
1302 /* Disallow remaping for ":@r". */
1303 remap = colon ? REMAP_NONE : REMAP_YES;
1304
1305 /*
1306 * Insert lines into typeahead buffer, from last one to first one.
1307 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001308 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 for (i = y_current->y_size; --i >= 0; )
1310 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001311 char_u *escaped;
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 /* insert NL between lines and after last line if type is MLINE */
1314 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1315 || addcr)
1316 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001317 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 return FAIL;
1319 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001320 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1321 if (escaped == NULL)
1322 return FAIL;
1323 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1324 vim_free(escaped);
1325 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001327 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 == FAIL)
1329 return FAIL;
1330 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001331 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333 return retval;
1334}
1335
1336/*
1337 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1338 * used only after other typeahead has been processed.
1339 */
1340 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001341put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 char_u buf[3];
1344
1345 if (restart_edit != NUL)
1346 {
1347 if (restart_edit == 'V')
1348 {
1349 buf[0] = 'g';
1350 buf[1] = 'R';
1351 buf[2] = NUL;
1352 }
1353 else
1354 {
1355 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1356 buf[1] = NUL;
1357 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001358 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 restart_edit = NUL;
1360 }
1361}
1362
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001363/*
1364 * Insert register contents "s" into the typeahead buffer, so that it will be
1365 * executed again.
1366 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1367 * no remapping.
1368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001370put_in_typebuf(
1371 char_u *s,
1372 int esc,
1373 int colon, /* add ':' before the line */
1374 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 int retval = OK;
1377
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001378 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001380 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001382 {
1383 char_u *p;
1384
1385 if (esc)
1386 p = vim_strsave_escape_csi(s);
1387 else
1388 p = s;
1389 if (p == NULL)
1390 retval = FAIL;
1391 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001392 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1393 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001394 if (esc)
1395 vim_free(p);
1396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001398 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 return retval;
1400}
1401
1402/*
1403 * Insert a yank register: copy it into the Read buffer.
1404 * Used by CTRL-R command and middle mouse button in insert mode.
1405 *
1406 * return FAIL for failure, OK otherwise
1407 */
1408 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001409insert_reg(
1410 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001411 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 long i;
1414 int retval = OK;
1415 char_u *arg;
1416 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001417 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419 /*
1420 * It is possible to get into an endless loop by having CTRL-R a in
1421 * register a and then, in insert mode, doing CTRL-R a.
1422 * If you hit CTRL-C, the loop will be broken here.
1423 */
1424 ui_breakcheck();
1425 if (got_int)
1426 return FAIL;
1427
1428 /* check for valid regname */
1429 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1430 return FAIL;
1431
1432#ifdef FEAT_CLIPBOARD
1433 regname = may_get_selection(regname);
1434#endif
1435
1436 if (regname == '.') /* insert last inserted text */
1437 retval = stuff_inserted(NUL, 1L, TRUE);
1438 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1439 {
1440 if (arg == NULL)
1441 return FAIL;
1442 stuffescaped(arg, literally);
1443 if (allocated)
1444 vim_free(arg);
1445 }
1446 else /* name or number register */
1447 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001448 if (get_yank_register(regname, FALSE))
1449 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 if (y_current->y_array == NULL)
1451 retval = FAIL;
1452 else
1453 {
1454 for (i = 0; i < y_current->y_size; ++i)
1455 {
1456 stuffescaped(y_current->y_array[i], literally);
1457 /*
1458 * Insert a newline between lines and after last line if
1459 * y_type is MLINE.
1460 */
1461 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1462 stuffcharReadbuff('\n');
1463 }
1464 }
1465 }
1466
1467 return retval;
1468}
1469
1470/*
1471 * Stuff a string into the typeahead buffer, such that edit() will insert it
1472 * literally ("literally" TRUE) or interpret is as typed characters.
1473 */
1474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001475stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 int c;
1478 char_u *start;
1479
1480 while (*arg != NUL)
1481 {
1482 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1483 * stuff K_SPECIAL to get the effect of a special key when "literally"
1484 * is TRUE. */
1485 start = arg;
1486 while ((*arg >= ' '
1487#ifndef EBCDIC
1488 && *arg < DEL /* EBCDIC: chars above space are normal */
1489#endif
1490 )
1491 || (*arg == K_SPECIAL && !literally))
1492 ++arg;
1493 if (arg > start)
1494 stuffReadbuffLen(start, (long)(arg - start));
1495
1496 /* stuff a single special character */
1497 if (*arg != NUL)
1498 {
1499#ifdef FEAT_MBYTE
1500 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001501 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 else
1503#endif
1504 c = *arg++;
1505 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1506 stuffcharReadbuff(Ctrl_V);
1507 stuffcharReadbuff(c);
1508 }
1509 }
1510}
1511
1512/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001513 * If "regname" is a special register, return TRUE and store a pointer to its
1514 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001516 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517get_spec_reg(
1518 int regname,
1519 char_u **argp,
1520 int *allocated, /* return: TRUE when value was allocated */
1521 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 int cnt;
1524
1525 *argp = NULL;
1526 *allocated = FALSE;
1527 switch (regname)
1528 {
1529 case '%': /* file name */
1530 if (errmsg)
1531 check_fname(); /* will give emsg if not set */
1532 *argp = curbuf->b_fname;
1533 return TRUE;
1534
1535 case '#': /* alternate file name */
1536 *argp = getaltfname(errmsg); /* may give emsg if not set */
1537 return TRUE;
1538
1539#ifdef FEAT_EVAL
1540 case '=': /* result of expression */
1541 *argp = get_expr_line();
1542 *allocated = TRUE;
1543 return TRUE;
1544#endif
1545
1546 case ':': /* last command line */
1547 if (last_cmdline == NULL && errmsg)
1548 EMSG(_(e_nolastcmd));
1549 *argp = last_cmdline;
1550 return TRUE;
1551
1552 case '/': /* last search-pattern */
1553 if (last_search_pat() == NULL && errmsg)
1554 EMSG(_(e_noprevre));
1555 *argp = last_search_pat();
1556 return TRUE;
1557
1558 case '.': /* last inserted text */
1559 *argp = get_last_insert_save();
1560 *allocated = TRUE;
1561 if (*argp == NULL && errmsg)
1562 EMSG(_(e_noinstext));
1563 return TRUE;
1564
1565#ifdef FEAT_SEARCHPATH
1566 case Ctrl_F: /* Filename under cursor */
1567 case Ctrl_P: /* Path under cursor, expand via "path" */
1568 if (!errmsg)
1569 return FALSE;
1570 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001571 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 *allocated = TRUE;
1573 return TRUE;
1574#endif
1575
1576 case Ctrl_W: /* word under cursor */
1577 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1578 if (!errmsg)
1579 return FALSE;
1580 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1581 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1582 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1583 *allocated = TRUE;
1584 return TRUE;
1585
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001586 case Ctrl_L: /* Line under cursor */
1587 if (!errmsg)
1588 return FALSE;
1589
1590 *argp = ml_get_buf(curwin->w_buffer,
1591 curwin->w_cursor.lnum, FALSE);
1592 return TRUE;
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 case '_': /* black hole: always empty */
1595 *argp = (char_u *)"";
1596 return TRUE;
1597 }
1598
1599 return FALSE;
1600}
1601
1602/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001603 * Paste a yank register into the command line.
1604 * Only for non-special registers.
1605 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 * insert_reg() can't be used here, because special characters from the
1607 * register contents will be interpreted as commands.
1608 *
1609 * return FAIL for failure, OK otherwise
1610 */
1611 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001612cmdline_paste_reg(
1613 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001614 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001615 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001618 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001620 if (get_yank_register(regname, FALSE))
1621 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (y_current->y_array == NULL)
1623 return FAIL;
1624
1625 for (i = 0; i < y_current->y_size; ++i)
1626 {
1627 cmdline_paste_str(y_current->y_array[i], literally);
1628
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001629 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001630 * Don't do this when "remcr" is TRUE. */
1631 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 cmdline_paste_str((char_u *)"\r", literally);
1633
1634 /* Check for CTRL-C, in case someone tries to paste a few thousand
1635 * lines and gets bored. */
1636 ui_breakcheck();
1637 if (got_int)
1638 return FAIL;
1639 }
1640 return OK;
1641}
1642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1644/*
1645 * Adjust the register name pointed to with "rp" for the clipboard being
1646 * used always and the clipboard being available.
1647 */
1648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001649adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001651 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1652 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001653 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1654 {
1655 if (clip_unnamed != 0)
1656 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001657 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001658 else
1659 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1660 ? '+' : '*';
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (!clip_star.available && *rp == '*')
1663 *rp = 0;
1664 if (!clip_plus.available && *rp == '+')
1665 *rp = 0;
1666}
1667#endif
1668
1669/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001670 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1671 */
1672 void
1673shift_delete_registers()
1674{
1675 int n;
1676
1677 y_current = &y_regs[9];
1678 free_yank_all(); /* free register nine */
1679 for (n = 9; n > 1; --n)
1680 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001681 y_current = &y_regs[1];
1682 if (!y_append)
1683 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001684 y_regs[1].y_array = NULL; /* set register one to empty */
1685}
1686
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001687#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001688 static void
1689yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1690{
1691 static int recursive = FALSE;
1692 dict_T *v_event;
1693 list_T *list;
1694 int n;
1695 char_u buf[NUMBUFLEN + 2];
1696 long reglen = 0;
1697
1698 if (recursive)
1699 return;
1700
1701 v_event = get_vim_var_dict(VV_EVENT);
1702
1703 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001704 if (list == NULL)
1705 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001706 for (n = 0; n < reg->y_size; n++)
1707 list_append_string(list, reg->y_array[n], -1);
1708 list->lv_lock = VAR_FIXED;
1709 dict_add_list(v_event, "regcontents", list);
1710
1711 buf[0] = (char_u)oap->regname;
1712 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001713 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001714
1715 buf[0] = get_op_char(oap->op_type);
1716 buf[1] = get_extra_op_char(oap->op_type);
1717 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001718 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001719
1720 buf[0] = NUL;
1721 buf[1] = NUL;
1722 switch (get_reg_type(oap->regname, &reglen))
1723 {
1724 case MLINE: buf[0] = 'V'; break;
1725 case MCHAR: buf[0] = 'v'; break;
1726 case MBLOCK:
1727 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1728 reglen + 1);
1729 break;
1730 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001731 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001732
1733 /* Lock the dictionary and its keys */
1734 dict_set_items_ro(v_event);
1735
1736 recursive = TRUE;
1737 textlock++;
1738 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1739 textlock--;
1740 recursive = FALSE;
1741
1742 /* Empty the dictionary, v:event is still valid */
1743 dict_free_contents(v_event);
1744 hash_init(&v_event->dv_hashtab);
1745}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001746#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001747
Bram Moolenaara1891842017-02-04 21:34:31 +01001748/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001749 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001751 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 */
1753 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001754op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
1756 int n;
1757 linenr_T lnum;
1758 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 char_u *newp, *oldp;
1760 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1762 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001763 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1766 return OK;
1767
1768 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1769 if (oap->empty)
1770 return u_save_cursor();
1771
1772 if (!curbuf->b_p_ma)
1773 {
1774 EMSG(_(e_modifiable));
1775 return FAIL;
1776 }
1777
1778#ifdef FEAT_CLIPBOARD
1779 adjust_clip_reg(&oap->regname);
1780#endif
1781
1782#ifdef FEAT_MBYTE
1783 if (has_mbyte)
1784 mb_adjust_opend(oap);
1785#endif
1786
Bram Moolenaard04b7502010-07-08 22:27:55 +02001787 /*
1788 * Imitate the strange Vi behaviour: If the delete spans more than one
1789 * line and motion_type == MCHAR and the result is a blank line, make the
1790 * delete linewise. Don't do this for the change command or Visual mode.
1791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001794 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001796 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 && oap->op_type == OP_DELETE)
1798 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001799 ptr = ml_get(oap->end.lnum) + oap->end.col;
1800 if (*ptr != NUL)
1801 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 ptr = skipwhite(ptr);
1803 if (*ptr == NUL && inindent(0))
1804 oap->motion_type = MLINE;
1805 }
1806
Bram Moolenaard04b7502010-07-08 22:27:55 +02001807 /*
1808 * Check for trying to delete (e.g. "D") in an empty line.
1809 * Note: For the change operator it is ok.
1810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if ( oap->motion_type == MCHAR
1812 && oap->line_count == 1
1813 && oap->op_type == OP_DELETE
1814 && *ml_get(oap->start.lnum) == NUL)
1815 {
1816 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001817 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 * 'cpoptions' (Vi compatible).
1819 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001820#ifdef FEAT_VIRTUALEDIT
1821 if (virtual_op)
1822 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1823 * marks as if it happened. */
1824 goto setmarks;
1825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1827 beep_flush();
1828 return OK;
1829 }
1830
Bram Moolenaard04b7502010-07-08 22:27:55 +02001831 /*
1832 * Do a yank of whatever we're about to delete.
1833 * If a yank register was specified, put the deleted text into that
1834 * register. For the black hole register '_' don't yank anything.
1835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (oap->regname != '_')
1837 {
1838 if (oap->regname != 0)
1839 {
1840 /* check for read-only register */
1841 if (!valid_yank_reg(oap->regname, TRUE))
1842 {
1843 beep_flush();
1844 return OK;
1845 }
1846 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1847 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1848 did_yank = TRUE;
1849 }
1850
1851 /*
1852 * Put deleted text into register 1 and shift number registers if the
1853 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001854 * Use the register name from before adjust_clip_reg() may have
1855 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001857 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 || oap->line_count > 1 || oap->use_reg_one)
1859 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001860 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (op_yank(oap, TRUE, FALSE) == OK)
1862 did_yank = TRUE;
1863 }
1864
Bram Moolenaar84298db2012-04-20 13:46:08 +02001865 /* Yank into small delete register when no named register specified
1866 * and the delete is within one line. */
1867 if ((
1868#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001869 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1870 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001871#endif
1872 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 && oap->line_count == 1)
1874 {
1875 oap->regname = '-';
1876 get_yank_register(oap->regname, TRUE);
1877 if (op_yank(oap, TRUE, FALSE) == OK)
1878 did_yank = TRUE;
1879 oap->regname = 0;
1880 }
1881
1882 /*
1883 * If there's too much stuff to fit in the yank register, then get a
1884 * confirmation before doing the delete. This is crude, but simple.
1885 * And it avoids doing a delete of something we can't put back if we
1886 * want.
1887 */
1888 if (!did_yank)
1889 {
1890 int msg_silent_save = msg_silent;
1891
1892 msg_silent = 0; /* must display the prompt */
1893 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1894 msg_silent = msg_silent_save;
1895 if (n != 'y')
1896 {
1897 EMSG(_(e_abort));
1898 return FAIL;
1899 }
1900 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001901
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001902#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001903 if (did_yank && has_textyankpost())
1904 yank_do_autocmd(oap, y_current);
1905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907
Bram Moolenaard04b7502010-07-08 22:27:55 +02001908 /*
1909 * block mode delete
1910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (oap->block_mode)
1912 {
1913 if (u_save((linenr_T)(oap->start.lnum - 1),
1914 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1915 return FAIL;
1916
1917 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1918 {
1919 block_prep(oap, &bd, lnum, TRUE);
1920 if (bd.textlen == 0) /* nothing to delete */
1921 continue;
1922
1923 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1924 if (lnum == curwin->w_cursor.lnum)
1925 {
1926 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1927# ifdef FEAT_VIRTUALEDIT
1928 curwin->w_cursor.coladd = 0;
1929# endif
1930 }
1931
1932 /* n == number of chars deleted
1933 * If we delete a TAB, it may be replaced by several characters.
1934 * Thus the number of characters may increase!
1935 */
1936 n = bd.textlen - bd.startspaces - bd.endspaces;
1937 oldp = ml_get(lnum);
1938 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1939 if (newp == NULL)
1940 continue;
1941 /* copy up to deleted part */
1942 mch_memmove(newp, oldp, (size_t)bd.textcol);
1943 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001944 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 (size_t)(bd.startspaces + bd.endspaces));
1946 /* copy the part after the deleted part */
1947 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001948 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 /* replace the line */
1950 ml_replace(lnum, newp, FALSE);
1951 }
1952
1953 check_cursor_col();
1954 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1955 oap->end.lnum + 1, 0L);
1956 oap->line_count = 0; /* no lines deleted */
1957 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001958 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 if (oap->op_type == OP_CHANGE)
1961 {
1962 /* Delete the lines except the first one. Temporarily move the
1963 * cursor to the next line. Save the current line number, if the
1964 * last line is deleted it may be changed.
1965 */
1966 if (oap->line_count > 1)
1967 {
1968 lnum = curwin->w_cursor.lnum;
1969 ++curwin->w_cursor.lnum;
1970 del_lines((long)(oap->line_count - 1), TRUE);
1971 curwin->w_cursor.lnum = lnum;
1972 }
1973 if (u_save_cursor() == FAIL)
1974 return FAIL;
1975 if (curbuf->b_p_ai) /* don't delete indent */
1976 {
1977 beginline(BL_WHITE); /* cursor on first non-white */
1978 did_ai = TRUE; /* delete the indent when ESC hit */
1979 ai_col = curwin->w_cursor.col;
1980 }
1981 else
1982 beginline(0); /* cursor in column 0 */
1983 truncate_line(FALSE); /* delete the rest of the line */
1984 /* leave cursor past last char in line */
1985 if (oap->line_count > 1)
1986 u_clearline(); /* "U" command not possible after "2cc" */
1987 }
1988 else
1989 {
1990 del_lines(oap->line_count, TRUE);
1991 beginline(BL_WHITE | BL_FIX);
1992 u_clearline(); /* "U" command not possible after "dd" */
1993 }
1994 }
1995 else
1996 {
1997#ifdef FEAT_VIRTUALEDIT
1998 if (virtual_op)
1999 {
2000 int endcol = 0;
2001
2002 /* For virtualedit: break the tabs that are partly included. */
2003 if (gchar_pos(&oap->start) == '\t')
2004 {
2005 if (u_save_cursor() == FAIL) /* save first line for undo */
2006 return FAIL;
2007 if (oap->line_count == 1)
2008 endcol = getviscol2(oap->end.col, oap->end.coladd);
2009 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
2010 oap->start = curwin->w_cursor;
2011 if (oap->line_count == 1)
2012 {
2013 coladvance(endcol);
2014 oap->end.col = curwin->w_cursor.col;
2015 oap->end.coladd = curwin->w_cursor.coladd;
2016 curwin->w_cursor = oap->start;
2017 }
2018 }
2019
2020 /* Break a tab only when it's included in the area. */
2021 if (gchar_pos(&oap->end) == '\t'
2022 && (int)oap->end.coladd < oap->inclusive)
2023 {
2024 /* save last line for undo */
2025 if (u_save((linenr_T)(oap->end.lnum - 1),
2026 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2027 return FAIL;
2028 curwin->w_cursor = oap->end;
2029 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2030 oap->end = curwin->w_cursor;
2031 curwin->w_cursor = oap->start;
2032 }
2033 }
2034#endif
2035
2036 if (oap->line_count == 1) /* delete characters within one line */
2037 {
2038 if (u_save_cursor() == FAIL) /* save line for undo */
2039 return FAIL;
2040
2041 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002042 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 && oap->op_type == OP_CHANGE
2044 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002045 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 display_dollar(oap->end.col - !oap->inclusive);
2047
2048 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2049
2050#ifdef FEAT_VIRTUALEDIT
2051 if (virtual_op)
2052 {
2053 /* fix up things for virtualedit-delete:
2054 * break the tabs which are going to get in our way
2055 */
2056 char_u *curline = ml_get_curline();
2057 int len = (int)STRLEN(curline);
2058
2059 if (oap->end.coladd != 0
2060 && (int)oap->end.col >= len - 1
2061 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2062 n++;
2063 /* Delete at least one char (e.g, when on a control char). */
2064 if (n == 0 && oap->start.coladd != oap->end.coladd)
2065 n = 1;
2066
2067 /* When deleted a char in the line, reset coladd. */
2068 if (gchar_cursor() != NUL)
2069 curwin->w_cursor.coladd = 0;
2070 }
2071#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002072 (void)del_bytes((long)n, !virtual_op,
2073 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 else /* delete characters between lines */
2076 {
2077 pos_T curpos;
2078
2079 /* save deleted and changed lines for undo */
2080 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2081 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2082 return FAIL;
2083
2084 truncate_line(TRUE); /* delete from cursor to end of line */
2085
2086 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2087 ++curwin->w_cursor.lnum;
2088 del_lines((long)(oap->line_count - 2), FALSE);
2089
Bram Moolenaard009e862015-06-09 20:20:03 +02002090 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002091 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002092 curwin->w_cursor.col = 0;
2093 (void)del_bytes((long)n, !virtual_op,
2094 oap->op_type == OP_DELETE && !oap->is_VIsual);
2095 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2096 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
2098 }
2099
2100 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2101
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002102#ifdef FEAT_VIRTUALEDIT
2103setmarks:
2104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (oap->block_mode)
2106 {
2107 curbuf->b_op_end.lnum = oap->end.lnum;
2108 curbuf->b_op_end.col = oap->start.col;
2109 }
2110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 curbuf->b_op_end = oap->start;
2112 curbuf->b_op_start = oap->start;
2113
2114 return OK;
2115}
2116
2117#ifdef FEAT_MBYTE
2118/*
2119 * Adjust end of operating area for ending on a multi-byte character.
2120 * Used for deletion.
2121 */
2122 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 char_u *p;
2126
2127 if (oap->inclusive)
2128 {
2129 p = ml_get(oap->end.lnum);
2130 oap->end.col += mb_tail_off(p, p + oap->end.col);
2131 }
2132}
2133#endif
2134
2135#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
Bram Moolenaar630afe82018-06-28 19:26:28 +02002136
2137# ifdef FEAT_MBYTE
2138/*
2139 * Replace the character under the cursor with "c".
2140 * This takes care of multi-byte characters.
2141 */
2142 static void
2143replace_character(int c)
2144{
2145 int n = State;
2146
2147 State = REPLACE;
2148 ins_char(c);
2149 State = n;
2150 /* Backup to the replaced character. */
2151 dec_cursor();
2152}
2153
2154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155/*
2156 * Replace a whole area with one character.
2157 */
2158 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002159op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 int n, numc;
2162#ifdef FEAT_MBYTE
2163 int num_chars;
2164#endif
2165 char_u *newp, *oldp;
2166 size_t oldlen;
2167 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002168 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002169 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2172 return OK; /* nothing to do */
2173
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002174 if (c == REPLACE_CR_NCHAR)
2175 {
2176 had_ctrl_v_cr = TRUE;
2177 c = CAR;
2178 }
2179 else if (c == REPLACE_NL_NCHAR)
2180 {
2181 had_ctrl_v_cr = TRUE;
2182 c = NL;
2183 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185#ifdef FEAT_MBYTE
2186 if (has_mbyte)
2187 mb_adjust_opend(oap);
2188#endif
2189
2190 if (u_save((linenr_T)(oap->start.lnum - 1),
2191 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2192 return FAIL;
2193
2194 /*
2195 * block mode replace
2196 */
2197 if (oap->block_mode)
2198 {
2199 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2200 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2201 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002202 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2204 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2205 continue; /* nothing to replace */
2206
2207 /* n == number of extra chars required
2208 * If we split a TAB, it may be replaced by several characters.
2209 * Thus the number of characters may increase!
2210 */
2211#ifdef FEAT_VIRTUALEDIT
2212 /* If the range starts in virtual space, count the initial
2213 * coladd offset as part of "startspaces" */
2214 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2215 {
2216 pos_T vpos;
2217
Bram Moolenaara1381de2009-11-03 15:44:21 +00002218 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 getvpos(&vpos, oap->start_vcol);
2220 bd.startspaces += vpos.coladd;
2221 n = bd.startspaces;
2222 }
2223 else
2224#endif
2225 /* allow for pre spaces */
2226 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2227
2228 /* allow for post spp */
2229 n += (bd.endspaces
2230#ifdef FEAT_VIRTUALEDIT
2231 && !bd.is_oneChar
2232#endif
2233 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2234 /* Figure out how many characters to replace. */
2235 numc = oap->end_vcol - oap->start_vcol + 1;
2236 if (bd.is_short && (!virtual_op || bd.is_MAX))
2237 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2238
2239#ifdef FEAT_MBYTE
2240 /* A double-wide character can be replaced only up to half the
2241 * times. */
2242 if ((*mb_char2cells)(c) > 1)
2243 {
2244 if ((numc & 1) && !bd.is_short)
2245 {
2246 ++bd.endspaces;
2247 ++n;
2248 }
2249 numc = numc / 2;
2250 }
2251
2252 /* Compute bytes needed, move character count to num_chars. */
2253 num_chars = numc;
2254 numc *= (*mb_char2len)(c);
2255#endif
2256 /* oldlen includes textlen, so don't double count */
2257 n += numc - bd.textlen;
2258
2259 oldp = ml_get_curline();
2260 oldlen = STRLEN(oldp);
2261 newp = alloc_check((unsigned)oldlen + 1 + n);
2262 if (newp == NULL)
2263 continue;
2264 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2265 /* copy up to deleted part */
2266 mch_memmove(newp, oldp, (size_t)bd.textcol);
2267 oldp += bd.textcol + bd.textlen;
2268 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002269 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002271 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2272 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002273 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002275#ifdef FEAT_MBYTE
2276 if (has_mbyte)
2277 {
2278 n = (int)STRLEN(newp);
2279 while (--num_chars >= 0)
2280 n += (*mb_char2bytes)(c, newp + n);
2281 }
2282 else
2283#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002284 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002285 if (!bd.is_short)
2286 {
2287 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002288 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002289 /* copy the part after the changed part */
2290 STRMOVE(newp + STRLEN(newp), oldp);
2291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002295 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002296 after_p = alloc_check(
2297 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002298 if (after_p != NULL)
2299 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 /* replace the line */
2302 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002303 if (after_p != NULL)
2304 {
2305 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2306 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2307 oap->end.lnum++;
2308 vim_free(after_p);
2309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311 }
2312 else
2313 {
2314 /*
2315 * MCHAR and MLINE motion replace.
2316 */
2317 if (oap->motion_type == MLINE)
2318 {
2319 oap->start.col = 0;
2320 curwin->w_cursor.col = 0;
2321 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2322 if (oap->end.col)
2323 --oap->end.col;
2324 }
2325 else if (!oap->inclusive)
2326 dec(&(oap->end));
2327
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002328 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 n = gchar_cursor();
2331 if (n != NUL)
2332 {
2333#ifdef FEAT_MBYTE
2334 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2335 {
2336 /* This is slow, but it handles replacing a single-byte
2337 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002338 if (curwin->w_cursor.lnum == oap->end.lnum)
2339 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002340 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 else
2343#endif
2344 {
2345#ifdef FEAT_VIRTUALEDIT
2346 if (n == TAB)
2347 {
2348 int end_vcol = 0;
2349
2350 if (curwin->w_cursor.lnum == oap->end.lnum)
2351 {
2352 /* oap->end has to be recalculated when
2353 * the tab breaks */
2354 end_vcol = getviscol2(oap->end.col,
2355 oap->end.coladd);
2356 }
2357 coladvance_force(getviscol());
2358 if (curwin->w_cursor.lnum == oap->end.lnum)
2359 getvpos(&oap->end, end_vcol);
2360 }
2361#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002362 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364 }
2365#ifdef FEAT_VIRTUALEDIT
2366 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2367 {
2368 int virtcols = oap->end.coladd;
2369
2370 if (curwin->w_cursor.lnum == oap->start.lnum
2371 && oap->start.col == oap->end.col && oap->start.coladd)
2372 virtcols -= oap->start.coladd;
2373
2374 /* oap->end has been trimmed so it's effectively inclusive;
2375 * as a result an extra +1 must be counted so we don't
2376 * trample the NUL byte. */
2377 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2378 curwin->w_cursor.col -= (virtcols + 1);
2379 for (; virtcols >= 0; virtcols--)
2380 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02002381#ifdef FEAT_MBYTE
2382 if ((*mb_char2len)(c) > 1)
2383 replace_character(c);
2384 else
2385 #endif
2386 PBYTE(curwin->w_cursor, c);
2387 if (inc(&curwin->w_cursor) == -1)
2388 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 }
2391#endif
2392
2393 /* Advance to next character, stop at the end of the file. */
2394 if (inc_cursor() == -1)
2395 break;
2396 }
2397 }
2398
2399 curwin->w_cursor = oap->start;
2400 check_cursor();
2401 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2402
2403 /* Set "'[" and "']" marks. */
2404 curbuf->b_op_start = oap->start;
2405 curbuf->b_op_end = oap->end;
2406
2407 return OK;
2408}
2409#endif
2410
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002411static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413/*
2414 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2415 */
2416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002421 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 if (u_save((linenr_T)(oap->start.lnum - 1),
2424 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2425 return;
2426
2427 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 if (oap->block_mode) /* Visual block mode */
2429 {
2430 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2431 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002432 int one_change;
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 block_prep(oap, &bd, pos.lnum, FALSE);
2435 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002436 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2437 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002438
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002439#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002440 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
2442 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2443
Bram Moolenaar009b2592004-10-24 19:18:58 +00002444 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2445 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002447 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451 if (did_change)
2452 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2453 }
2454 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
2456 if (oap->motion_type == MLINE)
2457 {
2458 oap->start.col = 0;
2459 pos.col = 0;
2460 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2461 if (oap->end.col)
2462 --oap->end.col;
2463 }
2464 else if (!oap->inclusive)
2465 dec(&(oap->end));
2466
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002467 if (pos.lnum == oap->end.lnum)
2468 did_change = swapchars(oap->op_type, &pos,
2469 oap->end.col - pos.col + 1);
2470 else
2471 for (;;)
2472 {
2473 did_change |= swapchars(oap->op_type, &pos,
2474 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2475 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002476 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002477 break;
2478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 if (did_change)
2480 {
2481 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2482 0L);
2483#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002484 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 char_u *ptr;
2487 int count;
2488
2489 pos = oap->start;
2490 while (pos.lnum < oap->end.lnum)
2491 {
2492 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002493 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002494 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002496 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 pos.col = 0;
2498 pos.lnum++;
2499 }
2500 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2501 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002502 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002504 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506#endif
2507 }
2508 }
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (!did_change && oap->is_VIsual)
2511 /* No change: need to remove the Visual selection */
2512 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514 /*
2515 * Set '[ and '] marks.
2516 */
2517 curbuf->b_op_start = oap->start;
2518 curbuf->b_op_end = oap->end;
2519
2520 if (oap->line_count > p_report)
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002521 smsg((char_u *)NGETTEXT("%ld line changed", "%ld lines changed",
2522 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523}
2524
2525/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002526 * Invoke swapchar() on "length" bytes at position "pos".
2527 * "pos" is advanced to just after the changed characters.
2528 * "length" is rounded up to include the whole last multi-byte character.
2529 * Also works correctly when the number of bytes changes.
2530 * Returns TRUE if some character was changed.
2531 */
2532 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002533swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002534{
2535 int todo;
2536 int did_change = 0;
2537
2538 for (todo = length; todo > 0; --todo)
2539 {
2540# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002541 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002542 {
2543 int len = (*mb_ptr2len)(ml_get_pos(pos));
2544
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002545 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002546 if (len > 0)
2547 todo -= len - 1;
2548 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002549# endif
2550 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002551 if (inc(pos) == -1) /* at end of file */
2552 break;
2553 }
2554 return did_change;
2555}
2556
2557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 * If op_type == OP_UPPER: make uppercase,
2559 * if op_type == OP_LOWER: make lowercase,
2560 * if op_type == OP_ROT13: do rot13 encoding,
2561 * else swap case of character at 'pos'
2562 * returns TRUE when something actually changed.
2563 */
2564 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002565swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
2567 int c;
2568 int nc;
2569
2570 c = gchar_pos(pos);
2571
2572 /* Only do rot13 encoding for ASCII characters. */
2573 if (c >= 0x80 && op_type == OP_ROT13)
2574 return FALSE;
2575
2576#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002577 if (op_type == OP_UPPER && c == 0xdf
2578 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002579 {
2580 pos_T sp = curwin->w_cursor;
2581
2582 /* Special handling of German sharp s: change to "SS". */
2583 curwin->w_cursor = *pos;
2584 del_char(FALSE);
2585 ins_char('S');
2586 ins_char('S');
2587 curwin->w_cursor = sp;
2588 inc(pos);
2589 }
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2592 return FALSE;
2593#endif
2594 nc = c;
2595 if (MB_ISLOWER(c))
2596 {
2597 if (op_type == OP_ROT13)
2598 nc = ROT13(c, 'a');
2599 else if (op_type != OP_LOWER)
2600 nc = MB_TOUPPER(c);
2601 }
2602 else if (MB_ISUPPER(c))
2603 {
2604 if (op_type == OP_ROT13)
2605 nc = ROT13(c, 'A');
2606 else if (op_type != OP_UPPER)
2607 nc = MB_TOLOWER(c);
2608 }
2609 if (nc != c)
2610 {
2611#ifdef FEAT_MBYTE
2612 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2613 {
2614 pos_T sp = curwin->w_cursor;
2615
2616 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002617 /* don't use del_char(), it also removes composing chars */
2618 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ins_char(nc);
2620 curwin->w_cursor = sp;
2621 }
2622 else
2623#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002624 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return TRUE;
2626 }
2627 return FALSE;
2628}
2629
2630#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2631/*
2632 * op_insert - Insert and append operators for Visual mode.
2633 */
2634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002635op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 long ins_len, pre_textlen = 0;
2638 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002639 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 struct block_def bd;
2641 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002642 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 /* edit() changes this - record it for OP_APPEND */
2645 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2646
2647 /* vis block is still marked. Get rid of it now. */
2648 curwin->w_cursor.lnum = oap->start.lnum;
2649 update_screen(INVERTED);
2650
2651 if (oap->block_mode)
2652 {
2653#ifdef FEAT_VIRTUALEDIT
2654 /* When 'virtualedit' is used, need to insert the extra spaces before
2655 * doing block_prep(). When only "block" is used, virtual edit is
2656 * already disabled, but still need it when calling
2657 * coladvance_force(). */
2658 if (curwin->w_cursor.coladd > 0)
2659 {
2660 int old_ve_flags = ve_flags;
2661
2662 ve_flags = VE_ALL;
2663 if (u_save_cursor() == FAIL)
2664 return;
2665 coladvance_force(oap->op_type == OP_APPEND
2666 ? oap->end_vcol + 1 : getviscol());
2667 if (oap->op_type == OP_APPEND)
2668 --curwin->w_cursor.col;
2669 ve_flags = old_ve_flags;
2670 }
2671#endif
2672 /* Get the info about the block before entering the text */
2673 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002674 /* Get indent information */
2675 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (oap->op_type == OP_APPEND)
2679 firstline += bd.textlen;
2680 pre_textlen = (long)STRLEN(firstline);
2681 }
2682
2683 if (oap->op_type == OP_APPEND)
2684 {
2685 if (oap->block_mode
2686#ifdef FEAT_VIRTUALEDIT
2687 && curwin->w_cursor.coladd == 0
2688#endif
2689 )
2690 {
2691 /* Move the cursor to the character right of the block. */
2692 curwin->w_set_curswant = TRUE;
2693 while (*ml_get_cursor() != NUL
2694 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2695 ++curwin->w_cursor.col;
2696 if (bd.is_short && !bd.is_MAX)
2697 {
2698 /* First line was too short, make it longer and adjust the
2699 * values in "bd". */
2700 if (u_save_cursor() == FAIL)
2701 return;
2702 for (i = 0; i < bd.endspaces; ++i)
2703 ins_char(' ');
2704 bd.textlen += bd.endspaces;
2705 }
2706 }
2707 else
2708 {
2709 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002710 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002713 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 && oap->start_vcol != oap->end_vcol)
2715 inc_cursor();
2716 }
2717 }
2718
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002719 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002720 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002722 /* When a tab was inserted, and the characters in front of the tab
2723 * have been converted to a tab as well, the column of the cursor
2724 * might have actually been reduced, so need to adjust here. */
2725 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002726 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002727 oap->start = curbuf->b_op_start_orig;
2728
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002729 /* If user has moved off this line, we don't know what to do, so do
2730 * nothing.
2731 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2732 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return;
2734
2735 if (oap->block_mode)
2736 {
2737 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002738 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002739 size_t len;
2740 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002742 /* If indent kicked in, the firstline might have changed
2743 * but only do that, if the indent actually increased. */
2744 ind_post = (colnr_T)getwhitecols_curline();
2745 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2746 {
2747 bd.textcol += ind_post - ind_pre;
2748 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002749 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002750 }
2751
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002752 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002753 * to adjust the block for that. But only do it, if the difference
2754 * does not come from indent kicking in. */
2755 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2756 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002757 {
2758 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002759 && oap->start.col
2760#ifdef FEAT_VIRTUALEDIT
2761 + oap->start.coladd
2762#endif
2763 != curbuf->b_op_start_orig.col
2764#ifdef FEAT_VIRTUALEDIT
2765 + curbuf->b_op_start_orig.coladd
2766#endif
2767 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002768 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002769 int t = getviscol2(curbuf->b_op_start_orig.col,
2770 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002771 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002772 pre_textlen -= t - oap->start_vcol;
2773 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002774 }
2775 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002776 && oap->end.col
2777#ifdef FEAT_VIRTUALEDIT
2778 + oap->end.coladd
2779#endif
2780 >= curbuf->b_op_start_orig.col
2781#ifdef FEAT_VIRTUALEDIT
2782 + curbuf->b_op_start_orig.coladd
2783#endif
2784 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002785 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002786 int t = getviscol2(curbuf->b_op_start_orig.col,
2787 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002788 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002789 /* reset pre_textlen to the value of OP_INSERT */
2790 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002791 pre_textlen -= t - oap->start_vcol;
2792 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002793 oap->op_type = OP_INSERT;
2794 }
2795 }
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 /*
2798 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002799 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 * Don't do this when "$" used, end-of-line will have changed.
2801 */
2802 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2803 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2804 {
2805 if (oap->op_type == OP_APPEND)
2806 {
2807 pre_textlen += bd2.textlen - bd.textlen;
2808 if (bd2.endspaces)
2809 --bd2.textlen;
2810 }
2811 bd.textcol = bd2.textcol;
2812 bd.textlen = bd2.textlen;
2813 }
2814
2815 /*
2816 * Subsequent calls to ml_get() flush the firstline data - take a
2817 * copy of the required string.
2818 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002819 firstline = ml_get(oap->start.lnum);
2820 len = STRLEN(firstline);
2821 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002823 add += bd.textlen;
2824 if ((size_t)add > len)
2825 firstline += len; // short line, point to the NUL
2826 else
2827 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002828 if (pre_textlen >= 0
2829 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
2831 ins_text = vim_strnsave(firstline, (int)ins_len);
2832 if (ins_text != NULL)
2833 {
2834 /* block handled here */
2835 if (u_save(oap->start.lnum,
2836 (linenr_T)(oap->end.lnum + 1)) == OK)
2837 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2838 &bd);
2839
2840 curwin->w_cursor.col = oap->start.col;
2841 check_cursor();
2842 vim_free(ins_text);
2843 }
2844 }
2845 }
2846}
2847#endif
2848
2849/*
2850 * op_change - handle a change operation
2851 *
2852 * return TRUE if edit() returns because of a CTRL-O command
2853 */
2854 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002855op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 colnr_T l;
2858 int retval;
2859#ifdef FEAT_VISUALEXTRA
2860 long offset;
2861 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002862 long ins_len;
2863 long pre_textlen = 0;
2864 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 char_u *firstline;
2866 char_u *ins_text, *newp, *oldp;
2867 struct block_def bd;
2868#endif
2869
2870 l = oap->start.col;
2871 if (oap->motion_type == MLINE)
2872 {
2873 l = 0;
2874#ifdef FEAT_SMARTINDENT
2875 if (!p_paste && curbuf->b_p_si
2876# ifdef FEAT_CINDENT
2877 && !curbuf->b_p_cin
2878# endif
2879 )
2880 can_si = TRUE; /* It's like opening a new line, do si */
2881#endif
2882 }
2883
2884 /* First delete the text in the region. In an empty buffer only need to
2885 * save for undo */
2886 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2887 {
2888 if (u_save_cursor() == FAIL)
2889 return FALSE;
2890 }
2891 else if (op_delete(oap) == FAIL)
2892 return FALSE;
2893
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002894 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 && !virtual_op)
2896 inc_cursor();
2897
2898#ifdef FEAT_VISUALEXTRA
2899 /* check for still on same line (<CR> in inserted text meaningless) */
2900 /* skip blank lines too */
2901 if (oap->block_mode)
2902 {
2903# ifdef FEAT_VIRTUALEDIT
2904 /* Add spaces before getting the current line length. */
2905 if (virtual_op && (curwin->w_cursor.coladd > 0
2906 || gchar_cursor() == NUL))
2907 coladvance_force(getviscol());
2908# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002909 firstline = ml_get(oap->start.lnum);
2910 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002911 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 bd.textcol = curwin->w_cursor.col;
2913 }
2914#endif
2915
2916#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2917 if (oap->motion_type == MLINE)
2918 fix_indent();
2919#endif
2920
2921 retval = edit(NUL, FALSE, (linenr_T)1);
2922
2923#ifdef FEAT_VISUALEXTRA
2924 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002925 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002927 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002929 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002931 /* Auto-indenting may have changed the indent. If the cursor was past
2932 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002934 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002936 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002937
2938 pre_textlen += new_indent - pre_indent;
2939 bd.textcol += new_indent - pre_indent;
2940 }
2941
2942 ins_len = (long)STRLEN(firstline) - pre_textlen;
2943 if (ins_len > 0)
2944 {
2945 /* Subsequent calls to ml_get() flush the firstline data - take a
2946 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2948 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002949 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2951 linenr++)
2952 {
2953 block_prep(oap, &bd, linenr, TRUE);
2954 if (!bd.is_short || virtual_op)
2955 {
2956# ifdef FEAT_VIRTUALEDIT
2957 pos_T vpos;
2958
2959 /* If the block starts in virtual space, count the
2960 * initial coladd offset as part of "startspaces" */
2961 if (bd.is_short)
2962 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002963 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966 else
2967 vpos.coladd = 0;
2968# endif
2969 oldp = ml_get(linenr);
2970 newp = alloc_check((unsigned)(STRLEN(oldp)
2971# ifdef FEAT_VIRTUALEDIT
2972 + vpos.coladd
2973# endif
2974 + ins_len + 1));
2975 if (newp == NULL)
2976 continue;
2977 /* copy up to block start */
2978 mch_memmove(newp, oldp, (size_t)bd.textcol);
2979 offset = bd.textcol;
2980# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002981 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 offset += vpos.coladd;
2983# endif
2984 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2985 offset += ins_len;
2986 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002987 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 ml_replace(linenr, newp, FALSE);
2989 }
2990 }
2991 check_cursor();
2992
2993 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2994 }
2995 vim_free(ins_text);
2996 }
2997 }
2998#endif
2999
3000 return retval;
3001}
3002
3003/*
3004 * set all the yank registers to empty (called from main())
3005 */
3006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003007init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 int i;
3010
3011 for (i = 0; i < NUM_REGISTERS; ++i)
3012 y_regs[i].y_array = NULL;
3013}
3014
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003015#if defined(EXITFREE) || defined(PROTO)
3016 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003017clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003018{
3019 int i;
3020
3021 for (i = 0; i < NUM_REGISTERS; ++i)
3022 {
3023 y_current = &y_regs[i];
3024 if (y_current->y_array != NULL)
3025 free_yank_all();
3026 }
3027}
3028#endif
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030/*
3031 * Free "n" lines from the current yank register.
3032 * Called for normal freeing and in case of error.
3033 */
3034 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003035free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
3037 if (y_current->y_array != NULL)
3038 {
3039 long i;
3040
3041 for (i = n; --i >= 0; )
3042 {
3043#ifdef AMIGA /* only for very slow machines */
3044 if ((i & 1023) == 1023) /* this may take a while */
3045 {
3046 /*
3047 * This message should never cause a hit-return message.
3048 * Overwrite this message with any next message.
3049 */
3050 ++no_wait_return;
3051 smsg((char_u *)_("freeing %ld lines"), i + 1);
3052 --no_wait_return;
3053 msg_didout = FALSE;
3054 msg_col = 0;
3055 }
3056#endif
3057 vim_free(y_current->y_array[i]);
3058 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003059 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#ifdef AMIGA
3061 if (n >= 1000)
3062 MSG("");
3063#endif
3064 }
3065}
3066
3067 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003068free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 free_yank(y_current->y_size);
3071}
3072
3073/*
3074 * Yank the text between "oap->start" and "oap->end" into a yank register.
3075 * If we are to append (uppercase register), we first yank into a new yank
3076 * register and then concatenate the old and the new one (so we keep the old
3077 * one in case of out-of-memory).
3078 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003079 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 */
3081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003082op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003085 yankreg_T *curr; /* copy of y_current */
3086 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 char_u **new_ptr;
3088 linenr_T lnum; /* current line number */
3089 long j;
3090 int yanktype = oap->motion_type;
3091 long yanklines = oap->line_count;
3092 linenr_T yankendlnum = oap->end.lnum;
3093 char_u *p;
3094 char_u *pnew;
3095 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003096#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003097 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100 /* check for read-only register */
3101 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3102 {
3103 beep_flush();
3104 return FAIL;
3105 }
3106 if (oap->regname == '_') /* black hole: nothing to do */
3107 return OK;
3108
3109#ifdef FEAT_CLIPBOARD
3110 if (!clip_star.available && oap->regname == '*')
3111 oap->regname = 0;
3112 else if (!clip_plus.available && oap->regname == '+')
3113 oap->regname = 0;
3114#endif
3115
3116 if (!deleting) /* op_delete() already set y_current */
3117 get_yank_register(oap->regname, TRUE);
3118
3119 curr = y_current;
3120 /* append to existing contents */
3121 if (y_append && y_current->y_array != NULL)
3122 y_current = &newreg;
3123 else
3124 free_yank_all(); /* free previously yanked lines */
3125
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003126 /*
3127 * If the cursor was in column 1 before and after the movement, and the
3128 * operator is not inclusive, the yank is always linewise.
3129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if ( oap->motion_type == MCHAR
3131 && oap->start.col == 0
3132 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003134 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 && oap->end.col == 0
3136 && yanklines > 1)
3137 {
3138 yanktype = MLINE;
3139 --yankendlnum;
3140 --yanklines;
3141 }
3142
3143 y_current->y_size = yanklines;
3144 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3147 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (y_current->y_array == NULL)
3149 {
3150 y_current = curr;
3151 return FAIL;
3152 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003153#ifdef FEAT_VIMINFO
3154 y_current->y_time_set = vim_time();
3155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 y_idx = 0;
3158 lnum = oap->start.lnum;
3159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (oap->block_mode)
3161 {
3162 /* Visual block mode */
3163 y_current->y_type = MBLOCK; /* set the yank register type */
3164 y_current->y_width = oap->end_vcol - oap->start_vcol;
3165
3166 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3167 y_current->y_width--;
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3171 {
3172 switch (y_current->y_type)
3173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 case MBLOCK:
3175 block_prep(oap, &bd, lnum, FALSE);
3176 if (yank_copy_line(&bd, y_idx) == FAIL)
3177 goto fail;
3178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 case MLINE:
3181 if ((y_current->y_array[y_idx] =
3182 vim_strsave(ml_get(lnum))) == NULL)
3183 goto fail;
3184 break;
3185
3186 case MCHAR:
3187 {
3188 colnr_T startcol = 0, endcol = MAXCOL;
3189#ifdef FEAT_VIRTUALEDIT
3190 int is_oneChar = FALSE;
3191 colnr_T cs, ce;
3192#endif
3193 p = ml_get(lnum);
3194 bd.startspaces = 0;
3195 bd.endspaces = 0;
3196
3197 if (lnum == oap->start.lnum)
3198 {
3199 startcol = oap->start.col;
3200#ifdef FEAT_VIRTUALEDIT
3201 if (virtual_op)
3202 {
3203 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3204 if (ce != cs && oap->start.coladd > 0)
3205 {
3206 /* Part of a tab selected -- but don't
3207 * double-count it. */
3208 bd.startspaces = (ce - cs + 1)
3209 - oap->start.coladd;
3210 startcol++;
3211 }
3212 }
3213#endif
3214 }
3215
3216 if (lnum == oap->end.lnum)
3217 {
3218 endcol = oap->end.col;
3219#ifdef FEAT_VIRTUALEDIT
3220 if (virtual_op)
3221 {
3222 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3223 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3224# ifdef FEAT_MBYTE
3225 /* Don't add space for double-wide
3226 * char; endcol will be on last byte
3227 * of multi-byte char. */
3228 && (*mb_head_off)(p, p + endcol) == 0
3229# endif
3230 ))
3231 {
3232 if (oap->start.lnum == oap->end.lnum
3233 && oap->start.col == oap->end.col)
3234 {
3235 /* Special case: inside a single char */
3236 is_oneChar = TRUE;
3237 bd.startspaces = oap->end.coladd
3238 - oap->start.coladd + oap->inclusive;
3239 endcol = startcol;
3240 }
3241 else
3242 {
3243 bd.endspaces = oap->end.coladd
3244 + oap->inclusive;
3245 endcol -= oap->inclusive;
3246 }
3247 }
3248 }
3249#endif
3250 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003251 if (endcol == MAXCOL)
3252 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (startcol > endcol
3254#ifdef FEAT_VIRTUALEDIT
3255 || is_oneChar
3256#endif
3257 )
3258 bd.textlen = 0;
3259 else
3260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 bd.textlen = endcol - startcol + oap->inclusive;
3262 }
3263 bd.textstart = p + startcol;
3264 if (yank_copy_line(&bd, y_idx) == FAIL)
3265 goto fail;
3266 break;
3267 }
3268 /* NOTREACHED */
3269 }
3270 }
3271
3272 if (curr != y_current) /* append the new block to the old block */
3273 {
3274 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3275 (curr->y_size + y_current->y_size)), TRUE);
3276 if (new_ptr == NULL)
3277 goto fail;
3278 for (j = 0; j < curr->y_size; ++j)
3279 new_ptr[j] = curr->y_array[j];
3280 vim_free(curr->y_array);
3281 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003282#ifdef FEAT_VIMINFO
3283 curr->y_time_set = vim_time();
3284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
3286 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3287 curr->y_type = MLINE;
3288
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003289 /* Concatenate the last line of the old block with the first line of
3290 * the new block, unless being Vi compatible. */
3291 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3294 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3295 if (pnew == NULL)
3296 {
3297 y_idx = y_current->y_size - 1;
3298 goto fail;
3299 }
3300 STRCPY(pnew, curr->y_array[--j]);
3301 STRCAT(pnew, y_current->y_array[0]);
3302 vim_free(curr->y_array[j]);
3303 vim_free(y_current->y_array[0]);
3304 curr->y_array[j++] = pnew;
3305 y_idx = 1;
3306 }
3307 else
3308 y_idx = 0;
3309 while (y_idx < y_current->y_size)
3310 curr->y_array[j++] = y_current->y_array[y_idx++];
3311 curr->y_size = j;
3312 vim_free(y_current->y_array);
3313 y_current = curr;
3314 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003315 if (curwin->w_p_rnu)
3316 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (mess) /* Display message about yank? */
3318 {
3319 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 && yanklines == 1)
3322 yanklines = 0;
3323 /* Some versions of Vi use ">=" here, some don't... */
3324 if (yanklines > p_report)
3325 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003326 char namebuf[100];
3327
3328 if (oap->regname == NUL)
3329 *namebuf = NUL;
3330 else
3331 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003332 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 /* redisplay now, so message is not deleted */
3335 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003336 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003337 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003338 smsg((char_u *)NGETTEXT("block of %ld line yanked%s",
3339 "block of %ld lines yanked%s", yanklines),
3340 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003343 {
3344 smsg((char_u *)NGETTEXT("%ld line yanked%s",
3345 "%ld lines yanked%s", yanklines),
3346 yanklines, namebuf);
3347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
3349 }
3350
3351 /*
3352 * Set "'[" and "']" marks.
3353 */
3354 curbuf->b_op_start = oap->start;
3355 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003356 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003357 {
3358 curbuf->b_op_start.col = 0;
3359 curbuf->b_op_end.col = MAXCOL;
3360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
3362#ifdef FEAT_CLIPBOARD
3363 /*
3364 * If we were yanking to the '*' register, send result to clipboard.
3365 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3366 * to the '*' register.
3367 */
3368 if (clip_star.available
3369 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003370 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003371 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 if (curr != &(y_regs[STAR_REGISTER]))
3374 /* Copy the text from register 0 to the clipboard register. */
3375 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3376
3377 clip_own_selection(&clip_star);
3378 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003379# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003380 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383
3384# ifdef FEAT_X11
3385 /*
3386 * If we were yanking to the '+' register, send result to selection.
3387 * Also copy to the '*' register, in case auto-select is off.
3388 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003389 if (clip_plus.available
3390 && (curr == &(y_regs[PLUS_REGISTER])
3391 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003392 && ((clip_unnamed | clip_unnamed_saved) &
3393 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003395 if (curr != &(y_regs[PLUS_REGISTER]))
3396 /* Copy the text from register 0 to the clipboard register. */
3397 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 clip_own_selection(&clip_plus);
3400 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003401 if (!clip_isautosel_star() && !clip_isautosel_plus()
3402 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3405 clip_own_selection(&clip_star);
3406 clip_gen_set_selection(&clip_star);
3407 }
3408 }
3409# endif
3410#endif
3411
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003412#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003413 if (!deleting && has_textyankpost())
3414 yank_do_autocmd(oap, y_current);
3415#endif
3416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return OK;
3418
3419fail: /* free the allocated lines */
3420 free_yank(y_idx + 1);
3421 y_current = curr;
3422 return FAIL;
3423}
3424
3425 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003426yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 char_u *pnew;
3429
3430 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3431 == NULL)
3432 return FAIL;
3433 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003434 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 pnew += bd->startspaces;
3436 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3437 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003438 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 pnew += bd->endspaces;
3440 *pnew = NUL;
3441 return OK;
3442}
3443
3444#ifdef FEAT_CLIPBOARD
3445/*
3446 * Make a copy of the y_current register to register "reg".
3447 */
3448 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003449copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003451 yankreg_T *curr = y_current;
3452 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 y_current = reg;
3455 free_yank_all();
3456 *y_current = *curr;
3457 y_current->y_array = (char_u **)lalloc_clear(
3458 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3459 if (y_current->y_array == NULL)
3460 y_current->y_size = 0;
3461 else
3462 for (j = 0; j < y_current->y_size; ++j)
3463 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3464 {
3465 free_yank(j);
3466 y_current->y_size = 0;
3467 break;
3468 }
3469 y_current = curr;
3470}
3471#endif
3472
3473/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003474 * Put contents of register "regname" into the text.
3475 * Caller must check "regname" to be valid!
3476 * "flags": PUT_FIXINDENT make indent look nice
3477 * PUT_CURSEND leave cursor after end of new text
3478 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 */
3480 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003481do_put(
3482 int regname,
3483 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3484 long count,
3485 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 char_u *ptr;
3488 char_u *newp, *oldp;
3489 int yanklen;
3490 int totlen = 0; /* init for gcc */
3491 linenr_T lnum;
3492 colnr_T col;
3493 long i; /* index in y_array[] */
3494 int y_type;
3495 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 int oldlen;
3497 long y_width = 0;
3498 colnr_T vcol;
3499 int delcount;
3500 int incr = 0;
3501 long j;
3502 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 char_u **y_array = NULL;
3504 long nr_lines = 0;
3505 pos_T new_cursor;
3506 int indent;
3507 int orig_indent = 0; /* init for gcc */
3508 int indent_diff = 0; /* init for gcc */
3509 int first_indent = TRUE;
3510 int lendiff = 0;
3511 pos_T old_pos;
3512 char_u *insert_string = NULL;
3513 int allocated = FALSE;
3514 long cnt;
3515
3516#ifdef FEAT_CLIPBOARD
3517 /* Adjust register name for "unnamed" in 'clipboard'. */
3518 adjust_clip_reg(&regname);
3519 (void)may_get_selection(regname);
3520#endif
3521
3522 if (flags & PUT_FIXINDENT)
3523 orig_indent = get_indent();
3524
3525 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3526 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3527
3528 /*
3529 * Using inserted text works differently, because the register includes
3530 * special characters (newlines, etc.).
3531 */
3532 if (regname == '.')
3533 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003534 if (VIsual_active)
3535 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3537 (count == -1 ? 'O' : 'i')), count, FALSE);
3538 /* Putting the text is done later, so can't really move the cursor to
3539 * the next character. Use "l" to simulate it. */
3540 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3541 stuffcharReadbuff('l');
3542 return;
3543 }
3544
3545 /*
3546 * For special registers '%' (file name), '#' (alternate file name) and
3547 * ':' (last command line), etc. we have to create a fake yank register.
3548 */
3549 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3550 {
3551 if (insert_string == NULL)
3552 return;
3553 }
3554
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003555 /* Autocommands may be executed when saving lines for undo. This might
3556 * make "y_array" invalid, so we start undo now to avoid that. */
3557 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3558 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (insert_string != NULL)
3561 {
3562 y_type = MCHAR;
3563#ifdef FEAT_EVAL
3564 if (regname == '=')
3565 {
3566 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003567 * characters.
3568 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 for (;;)
3570 {
3571 y_size = 0;
3572 ptr = insert_string;
3573 while (ptr != NULL)
3574 {
3575 if (y_array != NULL)
3576 y_array[y_size] = ptr;
3577 ++y_size;
3578 ptr = vim_strchr(ptr, '\n');
3579 if (ptr != NULL)
3580 {
3581 if (y_array != NULL)
3582 *ptr = NUL;
3583 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003584 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (*ptr == NUL)
3586 {
3587 y_type = MLINE;
3588 break;
3589 }
3590 }
3591 }
3592 if (y_array != NULL)
3593 break;
3594 y_array = (char_u **)alloc((unsigned)
3595 (y_size * sizeof(char_u *)));
3596 if (y_array == NULL)
3597 goto end;
3598 }
3599 }
3600 else
3601#endif
3602 {
3603 y_size = 1; /* use fake one-line yank register */
3604 y_array = &insert_string;
3605 }
3606 }
3607 else
3608 {
3609 get_yank_register(regname, FALSE);
3610
3611 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 y_size = y_current->y_size;
3614 y_array = y_current->y_array;
3615 }
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 if (y_type == MLINE)
3618 {
3619 if (flags & PUT_LINE_SPLIT)
3620 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003621 char_u *p;
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 /* "p" or "P" in Visual mode: split the lines to put the text in
3624 * between. */
3625 if (u_save_cursor() == FAIL)
3626 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003627 p = ml_get_cursor();
3628 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003629 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003630 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (ptr == NULL)
3632 goto end;
3633 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3634 vim_free(ptr);
3635
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003636 oldp = ml_get_curline();
3637 p = oldp + curwin->w_cursor.col;
3638 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003639 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003640 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (ptr == NULL)
3642 goto end;
3643 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3644 ++nr_lines;
3645 dir = FORWARD;
3646 }
3647 if (flags & PUT_LINE_FORWARD)
3648 {
3649 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003650 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 dir = FORWARD;
3652 }
3653 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3654 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
3657 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3658 y_type = MLINE;
3659
3660 if (y_size == 0 || y_array == NULL)
3661 {
3662 EMSG2(_("E353: Nothing in register %s"),
3663 regname == 0 ? (char_u *)"\"" : transchar(regname));
3664 goto end;
3665 }
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (y_type == MBLOCK)
3668 {
3669 lnum = curwin->w_cursor.lnum + y_size + 1;
3670 if (lnum > curbuf->b_ml.ml_line_count)
3671 lnum = curbuf->b_ml.ml_line_count + 1;
3672 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3673 goto end;
3674 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003675 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
3677 lnum = curwin->w_cursor.lnum;
3678#ifdef FEAT_FOLDING
3679 /* Correct line number for closed fold. Don't move the cursor yet,
3680 * u_save() uses it. */
3681 if (dir == BACKWARD)
3682 (void)hasFolding(lnum, &lnum, NULL);
3683 else
3684 (void)hasFolding(lnum, NULL, &lnum);
3685#endif
3686 if (dir == FORWARD)
3687 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003688 /* In an empty buffer the empty line is going to be replaced, include
3689 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003690 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 goto end;
3692#ifdef FEAT_FOLDING
3693 if (dir == FORWARD)
3694 curwin->w_cursor.lnum = lnum - 1;
3695 else
3696 curwin->w_cursor.lnum = lnum;
3697 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3698#endif
3699 }
3700 else if (u_save_cursor() == FAIL)
3701 goto end;
3702
3703 yanklen = (int)STRLEN(y_array[0]);
3704
3705#ifdef FEAT_VIRTUALEDIT
3706 if (ve_flags == VE_ALL && y_type == MCHAR)
3707 {
3708 if (gchar_cursor() == TAB)
3709 {
3710 /* Don't need to insert spaces when "p" on the last position of a
3711 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003712#ifdef FEAT_VARTABS
3713 int viscol = getviscol();
3714 if (dir == FORWARD
3715 ? tabstop_padding(viscol, curbuf->b_p_ts,
3716 curbuf->b_p_vts_array) != 1
3717 : curwin->w_cursor.coladd > 0)
3718 coladvance_force(viscol);
3719#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (dir == FORWARD
3721 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3722 : curwin->w_cursor.coladd > 0)
3723 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 else
3726 curwin->w_cursor.coladd = 0;
3727 }
3728 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3729 coladvance_force(getviscol() + (dir == FORWARD));
3730 }
3731#endif
3732
3733 lnum = curwin->w_cursor.lnum;
3734 col = curwin->w_cursor.col;
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 /*
3737 * Block mode
3738 */
3739 if (y_type == MBLOCK)
3740 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003741 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 colnr_T endcol2 = 0;
3743
3744 if (dir == FORWARD && c != NUL)
3745 {
3746#ifdef FEAT_VIRTUALEDIT
3747 if (ve_flags == VE_ALL)
3748 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3749 else
3750#endif
3751 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3752
3753#ifdef FEAT_MBYTE
3754 if (has_mbyte)
3755 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003756 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 else
3758#endif
3759#ifdef FEAT_VIRTUALEDIT
3760 if (c != TAB || ve_flags != VE_ALL)
3761#endif
3762 ++curwin->w_cursor.col;
3763 ++col;
3764 }
3765 else
3766 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3767
3768#ifdef FEAT_VIRTUALEDIT
3769 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003770 if (ve_flags == VE_ALL
3771 && (curwin->w_cursor.coladd > 0
3772 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
3774 if (dir == FORWARD && c == NUL)
3775 ++col;
3776 if (dir != FORWARD && c != NUL)
3777 ++curwin->w_cursor.col;
3778 if (c == TAB)
3779 {
3780 if (dir == BACKWARD && curwin->w_cursor.col)
3781 curwin->w_cursor.col--;
3782 if (dir == FORWARD && col - 1 == endcol2)
3783 curwin->w_cursor.col++;
3784 }
3785 }
3786 curwin->w_cursor.coladd = 0;
3787#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003788 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 for (i = 0; i < y_size; ++i)
3790 {
3791 int spaces;
3792 char shortline;
3793
3794 bd.startspaces = 0;
3795 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 vcol = 0;
3797 delcount = 0;
3798
3799 /* add a new line */
3800 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3801 {
3802 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3803 (colnr_T)1, FALSE) == FAIL)
3804 break;
3805 ++nr_lines;
3806 }
3807 /* get the old line and advance to the position to insert at */
3808 oldp = ml_get_curline();
3809 oldlen = (int)STRLEN(oldp);
3810 for (ptr = oldp; vcol < col && *ptr; )
3811 {
3812 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003813 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 vcol += incr;
3815 }
3816 bd.textcol = (colnr_T)(ptr - oldp);
3817
3818 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3819
3820 if (vcol < col) /* line too short, padd with spaces */
3821 bd.startspaces = col - vcol;
3822 else if (vcol > col)
3823 {
3824 bd.endspaces = vcol - col;
3825 bd.startspaces = incr - bd.endspaces;
3826 --bd.textcol;
3827 delcount = 1;
3828#ifdef FEAT_MBYTE
3829 if (has_mbyte)
3830 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3831#endif
3832 if (oldp[bd.textcol] != TAB)
3833 {
3834 /* Only a Tab can be split into spaces. Other
3835 * characters will have to be moved to after the
3836 * block, causing misalignment. */
3837 delcount = 0;
3838 bd.endspaces = 0;
3839 }
3840 }
3841
3842 yanklen = (int)STRLEN(y_array[i]);
3843
3844 /* calculate number of spaces required to fill right side of block*/
3845 spaces = y_width + 1;
3846 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003847 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 if (spaces < 0)
3849 spaces = 0;
3850
3851 /* insert the new text */
3852 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3853 newp = alloc_check((unsigned)totlen + oldlen + 1);
3854 if (newp == NULL)
3855 break;
3856 /* copy part up to cursor to new line */
3857 ptr = newp;
3858 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3859 ptr += bd.textcol;
3860 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003861 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 ptr += bd.startspaces;
3863 /* insert the new text */
3864 for (j = 0; j < count; ++j)
3865 {
3866 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3867 ptr += yanklen;
3868
3869 /* insert block's trailing spaces only if there's text behind */
3870 if ((j < count - 1 || !shortline) && spaces)
3871 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003872 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 ptr += spaces;
3874 }
3875 }
3876 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003877 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 ptr += bd.endspaces;
3879 /* move the text after the cursor to the end of the line. */
3880 mch_memmove(ptr, oldp + bd.textcol + delcount,
3881 (size_t)(oldlen - bd.textcol - delcount + 1));
3882 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3883
3884 ++curwin->w_cursor.lnum;
3885 if (i == 0)
3886 curwin->w_cursor.col += bd.startspaces;
3887 }
3888
3889 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3890
3891 /* Set '[ mark. */
3892 curbuf->b_op_start = curwin->w_cursor;
3893 curbuf->b_op_start.lnum = lnum;
3894
3895 /* adjust '] mark */
3896 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3897 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003898# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (flags & PUT_CURSEND)
3902 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003903 colnr_T len;
3904
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 curwin->w_cursor = curbuf->b_op_end;
3906 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003907
3908 /* in Insert mode we might be after the NUL, correct for that */
3909 len = (colnr_T)STRLEN(ml_get_curline());
3910 if (curwin->w_cursor.col > len)
3911 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 else
3914 curwin->w_cursor.lnum = lnum;
3915 }
3916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
3918 /*
3919 * Character or Line mode
3920 */
3921 if (y_type == MCHAR)
3922 {
3923 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3924 * char */
3925 if (dir == FORWARD && gchar_cursor() != NUL)
3926 {
3927#ifdef FEAT_MBYTE
3928 if (has_mbyte)
3929 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003930 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 /* put it on the next of the multi-byte character. */
3933 col += bytelen;
3934 if (yanklen)
3935 {
3936 curwin->w_cursor.col += bytelen;
3937 curbuf->b_op_end.col += bytelen;
3938 }
3939 }
3940 else
3941#endif
3942 {
3943 ++col;
3944 if (yanklen)
3945 {
3946 ++curwin->w_cursor.col;
3947 ++curbuf->b_op_end.col;
3948 }
3949 }
3950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 curbuf->b_op_start = curwin->w_cursor;
3952 }
3953 /*
3954 * Line mode: BACKWARD is the same as FORWARD on the previous line
3955 */
3956 else if (dir == BACKWARD)
3957 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003958 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960 /*
3961 * simple case: insert into current line
3962 */
3963 if (y_type == MCHAR && y_size == 1)
3964 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003965 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003966
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003967 if (VIsual_active)
3968 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003969 end_lnum = curbuf->b_visual.vi_end.lnum;
3970 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3971 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003972 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003973
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003974 do {
3975 totlen = count * yanklen;
3976 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003978 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003979 if (VIsual_active && col > (int)STRLEN(oldp))
3980 {
3981 lnum++;
3982 continue;
3983 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003984 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3985 if (newp == NULL)
3986 goto end; /* alloc() gave an error message */
3987 mch_memmove(newp, oldp, (size_t)col);
3988 ptr = newp + col;
3989 for (i = 0; i < count; ++i)
3990 {
3991 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3992 ptr += yanklen;
3993 }
3994 STRMOVE(ptr, oldp + col);
3995 ml_replace(lnum, newp, FALSE);
3996 /* Place cursor on last putted char. */
3997 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003998 {
3999 /* make sure curwin->w_virtcol is updated */
4000 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004001 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01004002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004004 if (VIsual_active)
4005 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01004006 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004007
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01004008 if (VIsual_active) /* reset lnum to the last visual line */
4009 lnum--;
4010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 curbuf->b_op_end = curwin->w_cursor;
4012 /* For "CTRL-O p" in Insert mode, put cursor after last char */
4013 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
4014 ++curwin->w_cursor.col;
4015 changed_bytes(lnum, col);
4016 }
4017 else
4018 {
4019 /*
4020 * Insert at least one line. When y_type is MCHAR, break the first
4021 * line in two.
4022 */
4023 for (cnt = 1; cnt <= count; ++cnt)
4024 {
4025 i = 0;
4026 if (y_type == MCHAR)
4027 {
4028 /*
4029 * Split the current line in two at the insert position.
4030 * First insert y_array[size - 1] in front of second line.
4031 * Then append y_array[0] to first line.
4032 */
4033 lnum = new_cursor.lnum;
4034 ptr = ml_get(lnum) + col;
4035 totlen = (int)STRLEN(y_array[y_size - 1]);
4036 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4037 if (newp == NULL)
4038 goto error;
4039 STRCPY(newp, y_array[y_size - 1]);
4040 STRCAT(newp, ptr);
4041 /* insert second line */
4042 ml_append(lnum, newp, (colnr_T)0, FALSE);
4043 vim_free(newp);
4044
4045 oldp = ml_get(lnum);
4046 newp = alloc_check((unsigned)(col + yanklen + 1));
4047 if (newp == NULL)
4048 goto error;
4049 /* copy first part of line */
4050 mch_memmove(newp, oldp, (size_t)col);
4051 /* append to first line */
4052 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4053 ml_replace(lnum, newp, FALSE);
4054
4055 curwin->w_cursor.lnum = lnum;
4056 i = 1;
4057 }
4058
4059 for (; i < y_size; ++i)
4060 {
4061 if ((y_type != MCHAR || i < y_size - 1)
4062 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4063 == FAIL)
4064 goto error;
4065 lnum++;
4066 ++nr_lines;
4067 if (flags & PUT_FIXINDENT)
4068 {
4069 old_pos = curwin->w_cursor;
4070 curwin->w_cursor.lnum = lnum;
4071 ptr = ml_get(lnum);
4072 if (cnt == count && i == y_size - 1)
4073 lendiff = (int)STRLEN(ptr);
4074#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4075 if (*ptr == '#' && preprocs_left())
4076 indent = 0; /* Leave # lines at start */
4077 else
4078#endif
4079 if (*ptr == NUL)
4080 indent = 0; /* Ignore empty lines */
4081 else if (first_indent)
4082 {
4083 indent_diff = orig_indent - get_indent();
4084 indent = orig_indent;
4085 first_indent = FALSE;
4086 }
4087 else if ((indent = get_indent() + indent_diff) < 0)
4088 indent = 0;
4089 (void)set_indent(indent, 0);
4090 curwin->w_cursor = old_pos;
4091 /* remember how many chars were removed */
4092 if (cnt == count && i == y_size - 1)
4093 lendiff -= (int)STRLEN(ml_get(lnum));
4094 }
4095 }
4096 }
4097
4098error:
4099 /* Adjust marks. */
4100 if (y_type == MLINE)
4101 {
4102 curbuf->b_op_start.col = 0;
4103 if (dir == FORWARD)
4104 curbuf->b_op_start.lnum++;
4105 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004106 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004107 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004108 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004109 < curbuf->b_ml.ml_line_count
4110#ifdef FEAT_DIFF
4111 || curwin->w_p_diff
4112#endif
4113 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004114 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 (linenr_T)MAXLNUM, nr_lines, 0L);
4116
4117 /* note changed text for displaying and folding */
4118 if (y_type == MCHAR)
4119 changed_lines(curwin->w_cursor.lnum, col,
4120 curwin->w_cursor.lnum + 1, nr_lines);
4121 else
4122 changed_lines(curbuf->b_op_start.lnum, 0,
4123 curbuf->b_op_start.lnum, nr_lines);
4124
4125 /* put '] mark at last inserted character */
4126 curbuf->b_op_end.lnum = lnum;
4127 /* correct length for change in indent */
4128 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4129 if (col > 1)
4130 curbuf->b_op_end.col = col - 1;
4131 else
4132 curbuf->b_op_end.col = 0;
4133
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004134 if (flags & PUT_CURSLINE)
4135 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004136 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004137 curwin->w_cursor.lnum = lnum;
4138 beginline(BL_WHITE | BL_FIX);
4139 }
4140 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 /* put cursor after inserted text */
4143 if (y_type == MLINE)
4144 {
4145 if (lnum >= curbuf->b_ml.ml_line_count)
4146 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4147 else
4148 curwin->w_cursor.lnum = lnum + 1;
4149 curwin->w_cursor.col = 0;
4150 }
4151 else
4152 {
4153 curwin->w_cursor.lnum = lnum;
4154 curwin->w_cursor.col = col;
4155 }
4156 }
4157 else if (y_type == MLINE)
4158 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004159 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 curwin->w_cursor.col = 0;
4161 if (dir == FORWARD)
4162 ++curwin->w_cursor.lnum;
4163 beginline(BL_WHITE | BL_FIX);
4164 }
4165 else /* put cursor on first inserted character */
4166 curwin->w_cursor = new_cursor;
4167 }
4168 }
4169
4170 msgmore(nr_lines);
4171 curwin->w_set_curswant = TRUE;
4172
4173end:
4174 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004176 if (regname == '=')
4177 vim_free(y_array);
4178
Bram Moolenaar033d8882013-09-25 23:24:57 +02004179 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004180
Bram Moolenaar677ee682005-01-27 14:41:15 +00004181 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004182 adjust_cursor_eol();
4183}
4184
4185/*
4186 * When the cursor is on the NUL past the end of the line and it should not be
4187 * there move it left.
4188 */
4189 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004190adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004191{
4192 if (curwin->w_cursor.col > 0
4193 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004194#ifdef FEAT_VIRTUALEDIT
4195 && (ve_flags & VE_ONEMORE) == 0
4196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 && !(restart_edit || (State & INSERT)))
4198 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004199 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004200 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202#ifdef FEAT_VIRTUALEDIT
4203 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004204 {
4205 colnr_T scol, ecol;
4206
4207 /* Coladd is set to the width of the last character. */
4208 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4209 curwin->w_cursor.coladd = ecol - scol + 1;
4210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#endif
4212 }
4213}
4214
4215#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4216/*
4217 * Return TRUE if lines starting with '#' should be left aligned.
4218 */
4219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004220preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 return
4223# ifdef FEAT_SMARTINDENT
4224# ifdef FEAT_CINDENT
4225 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4226# else
4227 curbuf->b_p_si
4228# endif
4229# endif
4230# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004231 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4232 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233# endif
4234 ;
4235}
4236#endif
4237
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004238/*
4239 * Return the character name of the register with the given number.
4240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004242get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243{
4244 if (num == -1)
4245 return '"';
4246 else if (num < 10)
4247 return num + '0';
4248 else if (num == DELETION_REGISTER)
4249 return '-';
4250#ifdef FEAT_CLIPBOARD
4251 else if (num == STAR_REGISTER)
4252 return '*';
4253 else if (num == PLUS_REGISTER)
4254 return '+';
4255#endif
4256 else
4257 {
4258#ifdef EBCDIC
4259 int i;
4260
4261 /* EBCDIC is really braindead ... */
4262 i = 'a' + (num - 10);
4263 if (i > 'i')
4264 i += 7;
4265 if (i > 'r')
4266 i += 8;
4267 return i;
4268#else
4269 return num + 'a' - 10;
4270#endif
4271 }
4272}
4273
4274/*
4275 * ":dis" and ":registers": Display the contents of the yank registers.
4276 */
4277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004278ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004280 int i, n;
4281 long j;
4282 char_u *p;
4283 yankreg_T *yb;
4284 int name;
4285 int attr;
4286 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004287#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004288 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004289#else
4290# define clen 1
4291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 if (arg != NULL && *arg == NUL)
4294 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004295 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 /* Highlight title */
4298 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4299 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4300 {
4301 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004302 if (arg != NULL && vim_strchr(arg, name) == NULL
4303#ifdef ONE_CLIPBOARD
4304 /* Star register and plus register contain the same thing. */
4305 && (name != '*' || vim_strchr(arg, '+') == NULL)
4306#endif
4307 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 continue; /* did not ask for this register */
4309
4310#ifdef FEAT_CLIPBOARD
4311 /* Adjust register name for "unnamed" in 'clipboard'.
4312 * When it's a clipboard register, fill it with the current contents
4313 * of the clipboard. */
4314 adjust_clip_reg(&name);
4315 (void)may_get_selection(name);
4316#endif
4317
4318 if (i == -1)
4319 {
4320 if (y_previous != NULL)
4321 yb = y_previous;
4322 else
4323 yb = &(y_regs[0]);
4324 }
4325 else
4326 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004327
4328#ifdef FEAT_EVAL
4329 if (name == MB_TOLOWER(redir_reg)
4330 || (redir_reg == '"' && yb == y_previous))
4331 continue; /* do not list register being written to, the
4332 * pointer can be freed */
4333#endif
4334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (yb->y_array != NULL)
4336 {
4337 msg_putchar('\n');
4338 msg_putchar('"');
4339 msg_putchar(name);
4340 MSG_PUTS(" ");
4341
4342 n = (int)Columns - 6;
4343 for (j = 0; j < yb->y_size && n > 1; ++j)
4344 {
4345 if (j)
4346 {
4347 MSG_PUTS_ATTR("^J", attr);
4348 n -= 2;
4349 }
4350 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004353 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004354#endif
4355 msg_outtrans_len(p, clen);
4356#ifdef FEAT_MBYTE
4357 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#endif
4359 }
4360 }
4361 if (n > 1 && yb->y_type == MLINE)
4362 MSG_PUTS_ATTR("^J", attr);
4363 out_flush(); /* show one line at a time */
4364 }
4365 ui_breakcheck();
4366 }
4367
4368 /*
4369 * display last inserted text
4370 */
4371 if ((p = get_last_insert()) != NULL
4372 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4373 {
4374 MSG_PUTS("\n\". ");
4375 dis_msg(p, TRUE);
4376 }
4377
4378 /*
4379 * display last command line
4380 */
4381 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4382 && !got_int)
4383 {
4384 MSG_PUTS("\n\": ");
4385 dis_msg(last_cmdline, FALSE);
4386 }
4387
4388 /*
4389 * display current file name
4390 */
4391 if (curbuf->b_fname != NULL
4392 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4393 {
4394 MSG_PUTS("\n\"% ");
4395 dis_msg(curbuf->b_fname, FALSE);
4396 }
4397
4398 /*
4399 * display alternate file name
4400 */
4401 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4402 {
4403 char_u *fname;
4404 linenr_T dummy;
4405
4406 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4407 {
4408 MSG_PUTS("\n\"# ");
4409 dis_msg(fname, FALSE);
4410 }
4411 }
4412
4413 /*
4414 * display last search pattern
4415 */
4416 if (last_search_pat() != NULL
4417 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4418 {
4419 MSG_PUTS("\n\"/ ");
4420 dis_msg(last_search_pat(), FALSE);
4421 }
4422
4423#ifdef FEAT_EVAL
4424 /*
4425 * display last used expression
4426 */
4427 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4428 && !got_int)
4429 {
4430 MSG_PUTS("\n\"= ");
4431 dis_msg(expr_line, FALSE);
4432 }
4433#endif
4434}
4435
4436/*
4437 * display a string for do_dis()
4438 * truncate at end of screen line
4439 */
4440 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004441dis_msg(
4442 char_u *p,
4443 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444{
4445 int n;
4446#ifdef FEAT_MBYTE
4447 int l;
4448#endif
4449
4450 n = (int)Columns - 6;
4451 while (*p != NUL
4452 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4453 && (n -= ptr2cells(p)) >= 0)
4454 {
4455#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004456 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 msg_outtrans_len(p, l);
4459 p += l;
4460 }
4461 else
4462#endif
4463 msg_outtrans_len(p++, 1);
4464 }
4465 ui_breakcheck();
4466}
4467
Bram Moolenaar81340392012-06-06 16:12:59 +02004468#if defined(FEAT_COMMENTS) || defined(PROTO)
4469/*
4470 * If "process" is TRUE and the line begins with a comment leader (possibly
4471 * after some white space), return a pointer to the text after it. Put a boolean
4472 * value indicating whether the line ends with an unclosed comment in
4473 * "is_comment".
4474 * line - line to be processed,
4475 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004476 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004477 * include_space - whether to also skip space following the comment leader,
4478 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004479 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004480 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004481 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004482skip_comment(
4483 char_u *line,
4484 int process,
4485 int include_space,
4486 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004487{
4488 char_u *comment_flags = NULL;
4489 int lead_len;
4490 int leader_offset = get_last_leader_offset(line, &comment_flags);
4491
4492 *is_comment = FALSE;
4493 if (leader_offset != -1)
4494 {
4495 /* Let's check whether the line ends with an unclosed comment.
4496 * If the last comment leader has COM_END in flags, there's no comment.
4497 */
4498 while (*comment_flags)
4499 {
4500 if (*comment_flags == COM_END
4501 || *comment_flags == ':')
4502 break;
4503 ++comment_flags;
4504 }
4505 if (*comment_flags != COM_END)
4506 *is_comment = TRUE;
4507 }
4508
4509 if (process == FALSE)
4510 return line;
4511
4512 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4513
4514 if (lead_len == 0)
4515 return line;
4516
4517 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004518 * - COM_END,
4519 * - colon,
4520 * whichever comes first.
4521 */
4522 while (*comment_flags)
4523 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004524 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004525 || *comment_flags == ':')
4526 {
4527 break;
4528 }
4529 ++comment_flags;
4530 }
4531
4532 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004533 * starting with a closing part of a three-part comment. That's good,
4534 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004535 */
4536 if (*comment_flags == ':' || *comment_flags == NUL)
4537 line += lead_len;
4538
4539 return line;
4540}
4541#endif
4542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004544 * Join 'count' lines (minimal 2) at cursor position.
4545 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004546 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4547 * leaders should not be removed.
4548 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4549 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004551 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 */
4553 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004554do_join(
4555 long count,
4556 int insert_space,
4557 int save_undo,
4558 int use_formatoptions UNUSED,
4559 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004561 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004562 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004563 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004565 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004566 int endcurr1 = NUL;
4567 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004568 int currsize = 0; /* size of the current line */
4569 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004571 colnr_T col = 0;
4572 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004573#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004574 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004575 int remove_comments = (use_formatoptions == TRUE)
4576 && has_format_option(FO_REMOVE_COMS);
4577 int prev_was_comment;
4578#endif
4579
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004581 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4582 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4583 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004585 /* Allocate an array to store the number of spaces inserted before each
4586 * line. We will use it to pre-compute the length of the new line and the
4587 * proper placement of each original line in the new one. */
4588 spaces = lalloc_clear((long_u)count, TRUE);
4589 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004591#if defined(FEAT_COMMENTS) || defined(PROTO)
4592 if (remove_comments)
4593 {
4594 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4595 if (comments == NULL)
4596 {
4597 vim_free(spaces);
4598 return FAIL;
4599 }
4600 }
4601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004604 * Don't move anything, just compute the final line length
4605 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004607 for (t = 0; t < count; ++t)
4608 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004609 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004610 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004611 {
4612 /* Set the '[ mark. */
4613 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4614 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4615 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004616#if defined(FEAT_COMMENTS) || defined(PROTO)
4617 if (remove_comments)
4618 {
4619 /* We don't want to remove the comment leader if the
4620 * previous line is not a comment. */
4621 if (t > 0 && prev_was_comment)
4622 {
4623
4624 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4625 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004626 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004627 curr = new_curr;
4628 }
4629 else
4630 curr = skip_comment(curr, FALSE, insert_space,
4631 &prev_was_comment);
4632 }
4633#endif
4634
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004635 if (insert_space && t > 0)
4636 {
4637 curr = skipwhite(curr);
4638 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4639#ifdef FEAT_MBYTE
4640 && (!has_format_option(FO_MBYTE_JOIN)
4641 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4642 && (!has_format_option(FO_MBYTE_JOIN2)
4643 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4644#endif
4645 )
4646 {
4647 /* don't add a space if the line is ending in a space */
4648 if (endcurr1 == ' ')
4649 endcurr1 = endcurr2;
4650 else
4651 ++spaces[t];
4652 /* extra space when 'joinspaces' set and line ends in '.' */
4653 if ( p_js
4654 && (endcurr1 == '.'
4655 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4656 && (endcurr1 == '?' || endcurr1 == '!'))))
4657 ++spaces[t];
4658 }
4659 }
4660 currsize = (int)STRLEN(curr);
4661 sumsize += currsize + spaces[t];
4662 endcurr1 = endcurr2 = NUL;
4663 if (insert_space && currsize > 0)
4664 {
4665#ifdef FEAT_MBYTE
4666 if (has_mbyte)
4667 {
4668 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004669 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004670 endcurr1 = (*mb_ptr2char)(cend);
4671 if (cend > curr)
4672 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004673 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004674 endcurr2 = (*mb_ptr2char)(cend);
4675 }
4676 }
4677 else
4678#endif
4679 {
4680 endcurr1 = *(curr + currsize - 1);
4681 if (currsize > 1)
4682 endcurr2 = *(curr + currsize - 2);
4683 }
4684 }
4685 line_breakcheck();
4686 if (got_int)
4687 {
4688 ret = FAIL;
4689 goto theend;
4690 }
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004693 /* store the column position before last line */
4694 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004696 /* allocate the space for the new line */
4697 newp = alloc_check((unsigned)(sumsize + 1));
4698 cend = newp + sumsize;
4699 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004701 /*
4702 * Move affected lines to the new long one.
4703 *
4704 * Move marks from each deleted line to the joined line, adjusting the
4705 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4706 * should not really be a problem.
4707 */
4708 for (t = count - 1; ; --t)
4709 {
4710 cend -= currsize;
4711 mch_memmove(cend, curr, (size_t)currsize);
4712 if (spaces[t] > 0)
4713 {
4714 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004715 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004716 }
4717 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004718 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004719 if (t == 0)
4720 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004721 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004722#if defined(FEAT_COMMENTS) || defined(PROTO)
4723 if (remove_comments)
4724 curr += comments[t - 1];
4725#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004726 if (insert_space && t > 1)
4727 curr = skipwhite(curr);
4728 currsize = (int)STRLEN(curr);
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4731
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004732 if (setmark)
4733 {
4734 /* Set the '] mark. */
4735 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4736 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4737 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 /* Only report the change in the first line here, del_lines() will report
4740 * the deleted line. */
4741 changed_lines(curwin->w_cursor.lnum, currsize,
4742 curwin->w_cursor.lnum + 1, 0L);
4743
4744 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004745 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 * briefly, and then move it back. After del_lines() the cursor may
4747 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
4749 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004751 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 curwin->w_cursor.lnum = t;
4753
4754 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004755 * Set the cursor column:
4756 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004757 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004759 curwin->w_cursor.col =
4760 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#ifdef FEAT_VIRTUALEDIT
4764 curwin->w_cursor.coladd = 0;
4765#endif
4766 curwin->w_set_curswant = TRUE;
4767
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004768theend:
4769 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004770#if defined(FEAT_COMMENTS) || defined(PROTO)
4771 if (remove_comments)
4772 vim_free(comments);
4773#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004774 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775}
4776
4777#ifdef FEAT_COMMENTS
4778/*
4779 * Return TRUE if the two comment leaders given are the same. "lnum" is
4780 * the first line. White-space is ignored. Note that the whole of
4781 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4782 */
4783 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004784same_leader(
4785 linenr_T lnum,
4786 int leader1_len,
4787 char_u *leader1_flags,
4788 int leader2_len,
4789 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 int idx1 = 0, idx2 = 0;
4792 char_u *p;
4793 char_u *line1;
4794 char_u *line2;
4795
4796 if (leader1_len == 0)
4797 return (leader2_len == 0);
4798
4799 /*
4800 * If first leader has 'f' flag, the lines can be joined only if the
4801 * second line does not have a leader.
4802 * If first leader has 'e' flag, the lines can never be joined.
4803 * If fist leader has 's' flag, the lines can only be joined if there is
4804 * some text after it and the second line has the 'm' flag.
4805 */
4806 if (leader1_flags != NULL)
4807 {
4808 for (p = leader1_flags; *p && *p != ':'; ++p)
4809 {
4810 if (*p == COM_FIRST)
4811 return (leader2_len == 0);
4812 if (*p == COM_END)
4813 return FALSE;
4814 if (*p == COM_START)
4815 {
4816 if (*(ml_get(lnum) + leader1_len) == NUL)
4817 return FALSE;
4818 if (leader2_flags == NULL || leader2_len == 0)
4819 return FALSE;
4820 for (p = leader2_flags; *p && *p != ':'; ++p)
4821 if (*p == COM_MIDDLE)
4822 return TRUE;
4823 return FALSE;
4824 }
4825 }
4826 }
4827
4828 /*
4829 * Get current line and next line, compare the leaders.
4830 * The first line has to be saved, only one line can be locked at a time.
4831 */
4832 line1 = vim_strsave(ml_get(lnum));
4833 if (line1 != NULL)
4834 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004835 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 ;
4837 line2 = ml_get(lnum + 1);
4838 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4839 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004840 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 if (line1[idx1++] != line2[idx2])
4843 break;
4844 }
4845 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004846 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 ++idx1;
4848 }
4849 vim_free(line1);
4850 }
4851 return (idx2 == leader2_len && idx1 == leader1_len);
4852}
4853#endif
4854
4855/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004856 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 */
4858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004859op_format(
4860 oparg_T *oap,
4861 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 long old_line_count = curbuf->b_ml.ml_line_count;
4864
4865 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4866 * can put it back there. */
4867 curwin->w_cursor = oap->cursor_start;
4868
4869 if (u_save((linenr_T)(oap->start.lnum - 1),
4870 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4871 return;
4872 curwin->w_cursor = oap->start;
4873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (oap->is_VIsual)
4875 /* When there is no change: need to remove the Visual selection */
4876 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878 /* Set '[ mark at the start of the formatted area */
4879 curbuf->b_op_start = oap->start;
4880
4881 /* For "gw" remember the cursor position and put it back below (adjusted
4882 * for joined and split lines). */
4883 if (keep_cursor)
4884 saved_cursor = oap->cursor_start;
4885
Bram Moolenaar81a82092008-03-12 16:27:00 +00004886 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 /*
4889 * Leave the cursor at the first non-blank of the last formatted line.
4890 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4891 * line, so "." will do the next lines.
4892 */
4893 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4894 ++curwin->w_cursor.lnum;
4895 beginline(BL_WHITE | BL_FIX);
4896 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4897 msgmore(old_line_count);
4898
4899 /* put '] mark on the end of the formatted area */
4900 curbuf->b_op_end = curwin->w_cursor;
4901
4902 if (keep_cursor)
4903 {
4904 curwin->w_cursor = saved_cursor;
4905 saved_cursor.lnum = 0;
4906 }
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (oap->is_VIsual)
4909 {
4910 win_T *wp;
4911
4912 FOR_ALL_WINDOWS(wp)
4913 {
4914 if (wp->w_old_cursor_lnum != 0)
4915 {
4916 /* When lines have been inserted or deleted, adjust the end of
4917 * the Visual area to be redrawn. */
4918 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4919 wp->w_old_cursor_lnum += old_line_count;
4920 else
4921 wp->w_old_visual_lnum += old_line_count;
4922 }
4923 }
4924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925}
4926
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004927#if defined(FEAT_EVAL) || defined(PROTO)
4928/*
4929 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4930 */
4931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004932op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004933{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004934 if (oap->is_VIsual)
4935 /* When there is no change: need to remove the Visual selection */
4936 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004937
Bram Moolenaar700303e2010-07-11 17:35:50 +02004938 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4939 /* As documented: when 'formatexpr' returns non-zero fall back to
4940 * internal formatting. */
4941 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004942}
4943
4944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004945fex_format(
4946 linenr_T lnum,
4947 long count,
4948 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004949{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004950 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4951 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004952 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004953 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004954
4955 /*
4956 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004957 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004958 */
4959 set_vim_var_nr(VV_LNUM, lnum);
4960 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004961 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004962
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004963 /* Make a copy, the option could be changed while calling it. */
4964 fex = vim_strsave(curbuf->b_p_fex);
4965 if (fex == NULL)
4966 return 0;
4967
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004968 /*
4969 * Evaluate the function.
4970 */
4971 if (use_sandbox)
4972 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004973 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004974 if (use_sandbox)
4975 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004976
4977 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004978 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004979
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004980 return r;
4981}
4982#endif
4983
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984/*
4985 * Format "line_count" lines, starting at the cursor position.
4986 * When "line_count" is negative, format until the end of the paragraph.
4987 * Lines after the cursor line are saved for undo, caller must have saved the
4988 * first line.
4989 */
4990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004991format_lines(
4992 linenr_T line_count,
4993 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
4995 int max_len;
4996 int is_not_par; /* current line not part of parag. */
4997 int next_is_not_par; /* next line not part of paragraph */
4998 int is_end_par; /* at end of paragraph */
4999 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
5000 int next_is_start_par = FALSE;
5001#ifdef FEAT_COMMENTS
5002 int leader_len = 0; /* leader len of current line */
5003 int next_leader_len; /* leader len of next line */
5004 char_u *leader_flags = NULL; /* flags for leader of current line */
5005 char_u *next_leader_flags; /* flags for leader of next line */
5006 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005007 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008#endif
5009 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005010 int second_indent = -1; /* indent for second line (comment
5011 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 int do_second_indent;
5013 int do_number_indent;
5014 int do_trail_white;
5015 int first_par_line = TRUE;
5016 int smd_save;
5017 long count;
5018 int need_set_indent = TRUE; /* set indent of next paragraph */
5019 int force_format = FALSE;
5020 int old_State = State;
5021
5022 /* length of a line to force formatting: 3 * 'tw' */
5023 max_len = comp_textwidth(TRUE) * 3;
5024
5025 /* check for 'q', '2' and '1' in 'formatoptions' */
5026#ifdef FEAT_COMMENTS
5027 do_comments = has_format_option(FO_Q_COMS);
5028#endif
5029 do_second_indent = has_format_option(FO_Q_SECOND);
5030 do_number_indent = has_format_option(FO_Q_NUMBER);
5031 do_trail_white = has_format_option(FO_WHITE_PAR);
5032
5033 /*
5034 * Get info about the previous and current line.
5035 */
5036 if (curwin->w_cursor.lnum > 1)
5037 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5038#ifdef FEAT_COMMENTS
5039 , &leader_len, &leader_flags, do_comments
5040#endif
5041 );
5042 else
5043 is_not_par = TRUE;
5044 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5045#ifdef FEAT_COMMENTS
5046 , &next_leader_len, &next_leader_flags, do_comments
5047#endif
5048 );
5049 is_end_par = (is_not_par || next_is_not_par);
5050 if (!is_end_par && do_trail_white)
5051 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5052
5053 curwin->w_cursor.lnum--;
5054 for (count = line_count; count != 0 && !got_int; --count)
5055 {
5056 /*
5057 * Advance to next paragraph.
5058 */
5059 if (advance)
5060 {
5061 curwin->w_cursor.lnum++;
5062 prev_is_end_par = is_end_par;
5063 is_not_par = next_is_not_par;
5064#ifdef FEAT_COMMENTS
5065 leader_len = next_leader_len;
5066 leader_flags = next_leader_flags;
5067#endif
5068 }
5069
5070 /*
5071 * The last line to be formatted.
5072 */
5073 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5074 {
5075 next_is_not_par = TRUE;
5076#ifdef FEAT_COMMENTS
5077 next_leader_len = 0;
5078 next_leader_flags = NULL;
5079#endif
5080 }
5081 else
5082 {
5083 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5084#ifdef FEAT_COMMENTS
5085 , &next_leader_len, &next_leader_flags, do_comments
5086#endif
5087 );
5088 if (do_number_indent)
5089 next_is_start_par =
5090 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5091 }
5092 advance = TRUE;
5093 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5094 if (!is_end_par && do_trail_white)
5095 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5096
5097 /*
5098 * Skip lines that are not in a paragraph.
5099 */
5100 if (is_not_par)
5101 {
5102 if (line_count < 0)
5103 break;
5104 }
5105 else
5106 {
5107 /*
5108 * For the first line of a paragraph, check indent of second line.
5109 * Don't do this for comments and empty lines.
5110 */
5111 if (first_par_line
5112 && (do_second_indent || do_number_indent)
5113 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005114 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005116 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005117 {
5118#ifdef FEAT_COMMENTS
5119 if (leader_len == 0 && next_leader_len == 0)
5120 {
5121 /* no comment found */
5122#endif
5123 second_indent =
5124 get_indent_lnum(curwin->w_cursor.lnum + 1);
5125#ifdef FEAT_COMMENTS
5126 }
5127 else
5128 {
5129 second_indent = next_leader_len;
5130 do_comments_list = 1;
5131 }
5132#endif
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005135 {
5136#ifdef FEAT_COMMENTS
5137 if (leader_len == 0 && next_leader_len == 0)
5138 {
5139 /* no comment found */
5140#endif
5141 second_indent =
5142 get_number_indent(curwin->w_cursor.lnum);
5143#ifdef FEAT_COMMENTS
5144 }
5145 else
5146 {
5147 /* get_number_indent() is now "comment aware"... */
5148 second_indent =
5149 get_number_indent(curwin->w_cursor.lnum);
5150 do_comments_list = 1;
5151 }
5152#endif
5153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155
5156 /*
5157 * When the comment leader changes, it's the end of the paragraph.
5158 */
5159 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5160#ifdef FEAT_COMMENTS
5161 || !same_leader(curwin->w_cursor.lnum,
5162 leader_len, leader_flags,
5163 next_leader_len, next_leader_flags)
5164#endif
5165 )
5166 is_end_par = TRUE;
5167
5168 /*
5169 * If we have got to the end of a paragraph, or the line is
5170 * getting long, format it.
5171 */
5172 if (is_end_par || force_format)
5173 {
5174 if (need_set_indent)
5175 /* replace indent in first line with minimal number of
5176 * tabs and spaces, according to current options */
5177 (void)set_indent(get_indent(), SIN_CHANGED);
5178
5179 /* put cursor on last non-space */
5180 State = NORMAL; /* don't go past end-of-line */
5181 coladvance((colnr_T)MAXCOL);
5182 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5183 dec_cursor();
5184
5185 /* do the formatting, without 'showmode' */
5186 State = INSERT; /* for open_line() */
5187 smd_save = p_smd;
5188 p_smd = FALSE;
5189 insertchar(NUL, INSCHAR_FORMAT
5190#ifdef FEAT_COMMENTS
5191 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005192 + (do_comments && do_comments_list
5193 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005195 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 State = old_State;
5197 p_smd = smd_save;
5198 second_indent = -1;
5199 /* at end of par.: need to set indent of next par. */
5200 need_set_indent = is_end_par;
5201 if (is_end_par)
5202 {
5203 /* When called with a negative line count, break at the
5204 * end of the paragraph. */
5205 if (line_count < 0)
5206 break;
5207 first_par_line = TRUE;
5208 }
5209 force_format = FALSE;
5210 }
5211
5212 /*
5213 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005214 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 */
5216 if (!is_end_par)
5217 {
5218 advance = FALSE;
5219 curwin->w_cursor.lnum++;
5220 curwin->w_cursor.col = 0;
5221 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005222 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005225 {
5226 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5228 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005229 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005231 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5232 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005233 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005234
5235 if (indent > 0)
5236 {
5237 (void)del_bytes(indent, FALSE, FALSE);
5238 mark_col_adjust(curwin->w_cursor.lnum,
5239 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005240 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005243 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
5245 beep_flush();
5246 break;
5247 }
5248 first_par_line = FALSE;
5249 /* If the line is getting long, format it next time */
5250 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5251 force_format = TRUE;
5252 else
5253 force_format = FALSE;
5254 }
5255 }
5256 line_breakcheck();
5257 }
5258}
5259
5260/*
5261 * Return TRUE if line "lnum" ends in a white character.
5262 */
5263 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005264ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 char_u *s = ml_get(lnum);
5267 size_t l;
5268
5269 if (*s == NUL)
5270 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005271 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 * invocation may call function multiple times". */
5273 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005274 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275}
5276
5277/*
5278 * Blank lines, and lines containing only the comment leader, are left
5279 * untouched by the formatting. The function returns TRUE in this
5280 * case. It also returns TRUE when a line starts with the end of a comment
5281 * ('e' in comment flags), so that this line is skipped, and not joined to the
5282 * previous line. A new paragraph starts after a blank line, or when the
5283 * comment leader changes -- webb.
5284 */
5285#ifdef FEAT_COMMENTS
5286 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005287fmt_check_par(
5288 linenr_T lnum,
5289 int *leader_len,
5290 char_u **leader_flags,
5291 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293 char_u *flags = NULL; /* init for GCC */
5294 char_u *ptr;
5295
5296 ptr = ml_get(lnum);
5297 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005298 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 else
5300 *leader_len = 0;
5301
5302 if (*leader_len > 0)
5303 {
5304 /*
5305 * Search for 'e' flag in comment leader flags.
5306 */
5307 flags = *leader_flags;
5308 while (*flags && *flags != ':' && *flags != COM_END)
5309 ++flags;
5310 }
5311
5312 return (*skipwhite(ptr + *leader_len) == NUL
5313 || (*leader_len > 0 && *flags == COM_END)
5314 || startPS(lnum, NUL, FALSE));
5315}
5316#else
5317 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005318fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5321}
5322#endif
5323
5324/*
5325 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5326 * previous line is in the same paragraph. Used for auto-formatting.
5327 */
5328 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005329paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
5331 char_u *p;
5332#ifdef FEAT_COMMENTS
5333 int leader_len = 0; /* leader len of current line */
5334 char_u *leader_flags = NULL; /* flags for leader of current line */
5335 int next_leader_len; /* leader len of next line */
5336 char_u *next_leader_flags; /* flags for leader of next line */
5337 int do_comments; /* format comments */
5338#endif
5339
5340 if (lnum <= 1)
5341 return TRUE; /* start of the file */
5342
5343 p = ml_get(lnum - 1);
5344 if (*p == NUL)
5345 return TRUE; /* after empty line */
5346
5347#ifdef FEAT_COMMENTS
5348 do_comments = has_format_option(FO_Q_COMS);
5349#endif
5350 if (fmt_check_par(lnum - 1
5351#ifdef FEAT_COMMENTS
5352 , &leader_len, &leader_flags, do_comments
5353#endif
5354 ))
5355 return TRUE; /* after non-paragraph line */
5356
5357 if (fmt_check_par(lnum
5358#ifdef FEAT_COMMENTS
5359 , &next_leader_len, &next_leader_flags, do_comments
5360#endif
5361 ))
5362 return TRUE; /* "lnum" is not a paragraph line */
5363
5364 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5365 return TRUE; /* missing trailing space in previous line. */
5366
5367 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5368 return TRUE; /* numbered item starts in "lnum". */
5369
5370#ifdef FEAT_COMMENTS
5371 if (!same_leader(lnum - 1, leader_len, leader_flags,
5372 next_leader_len, next_leader_flags))
5373 return TRUE; /* change of comment leader. */
5374#endif
5375
5376 return FALSE;
5377}
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379/*
5380 * prepare a few things for block mode yank/delete/tilde
5381 *
5382 * for delete:
5383 * - textlen includes the first/last char to be (partly) deleted
5384 * - start/endspaces is the number of columns that are taken by the
5385 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005386 * deleted.
5387 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 * - textlen includes the first/last char to be wholly yanked
5389 * - start/endspaces is the number of columns of the first/last yanked char
5390 * that are to be yanked.
5391 */
5392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005393block_prep(
5394 oparg_T *oap,
5395 struct block_def *bdp,
5396 linenr_T lnum,
5397 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 int incr = 0;
5400 char_u *pend;
5401 char_u *pstart;
5402 char_u *line;
5403 char_u *prev_pstart;
5404 char_u *prev_pend;
5405
5406 bdp->startspaces = 0;
5407 bdp->endspaces = 0;
5408 bdp->textlen = 0;
5409 bdp->start_vcol = 0;
5410 bdp->end_vcol = 0;
5411#ifdef FEAT_VISUALEXTRA
5412 bdp->is_short = FALSE;
5413 bdp->is_oneChar = FALSE;
5414 bdp->pre_whitesp = 0;
5415 bdp->pre_whitesp_c = 0;
5416 bdp->end_char_vcols = 0;
5417#endif
5418 bdp->start_char_vcols = 0;
5419
5420 line = ml_get(lnum);
5421 pstart = line;
5422 prev_pstart = line;
5423 while (bdp->start_vcol < oap->start_vcol && *pstart)
5424 {
5425 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005426 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 bdp->start_vcol += incr;
5428#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005429 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 bdp->pre_whitesp += incr;
5432 bdp->pre_whitesp_c++;
5433 }
5434 else
5435 {
5436 bdp->pre_whitesp = 0;
5437 bdp->pre_whitesp_c = 0;
5438 }
5439#endif
5440 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005441 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 }
5443 bdp->start_char_vcols = incr;
5444 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5445 {
5446 bdp->end_vcol = bdp->start_vcol;
5447#ifdef FEAT_VISUALEXTRA
5448 bdp->is_short = TRUE;
5449#endif
5450 if (!is_del || oap->op_type == OP_APPEND)
5451 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5452 }
5453 else
5454 {
5455 /* notice: this converts partly selected Multibyte characters to
5456 * spaces, too. */
5457 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5458 if (is_del && bdp->startspaces)
5459 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5460 pend = pstart;
5461 bdp->end_vcol = bdp->start_vcol;
5462 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5463 {
5464#ifdef FEAT_VISUALEXTRA
5465 bdp->is_oneChar = TRUE;
5466#endif
5467 if (oap->op_type == OP_INSERT)
5468 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5469 else if (oap->op_type == OP_APPEND)
5470 {
5471 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5472 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5473 }
5474 else
5475 {
5476 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5477 if (is_del && oap->op_type != OP_LSHIFT)
5478 {
5479 /* just putting the sum of those two into
5480 * bdp->startspaces doesn't work for Visual replace,
5481 * so we have to split the tab in two */
5482 bdp->startspaces = bdp->start_char_vcols
5483 - (bdp->start_vcol - oap->start_vcol);
5484 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5485 }
5486 }
5487 }
5488 else
5489 {
5490 prev_pend = pend;
5491 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5492 {
5493 /* Count a tab for what it's worth (if list mode not on) */
5494 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005495 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 bdp->end_vcol += incr;
5497 }
5498 if (bdp->end_vcol <= oap->end_vcol
5499 && (!is_del
5500 || oap->op_type == OP_APPEND
5501 || oap->op_type == OP_REPLACE)) /* line too short */
5502 {
5503#ifdef FEAT_VISUALEXTRA
5504 bdp->is_short = TRUE;
5505#endif
5506 /* Alternative: include spaces to fill up the block.
5507 * Disadvantage: can lead to trailing spaces when the line is
5508 * short where the text is put */
5509 /* if (!is_del || oap->op_type == OP_APPEND) */
5510 if (oap->op_type == OP_APPEND || virtual_op)
5511 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005512 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 else
5514 bdp->endspaces = 0; /* replace doesn't add characters */
5515 }
5516 else if (bdp->end_vcol > oap->end_vcol)
5517 {
5518 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5519 if (!is_del && bdp->endspaces)
5520 {
5521 bdp->endspaces = incr - bdp->endspaces;
5522 if (pend != pstart)
5523 pend = prev_pend;
5524 }
5525 }
5526 }
5527#ifdef FEAT_VISUALEXTRA
5528 bdp->end_char_vcols = incr;
5529#endif
5530 if (is_del && bdp->startspaces)
5531 pstart = prev_pstart;
5532 bdp->textlen = (int)(pend - pstart);
5533 }
5534 bdp->textcol = (colnr_T) (pstart - line);
5535 bdp->textstart = pstart;
5536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005539 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005542op_addsub(
5543 oparg_T *oap,
5544 linenr_T Prenum1, /* Amount of add/subtract */
5545 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005547 pos_T pos;
5548 struct block_def bd;
5549 int change_cnt = 0;
5550 linenr_T amount = Prenum1;
5551
Bram Moolenaar6b731882018-11-16 20:54:47 +01005552 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
5553 // buffer is not completly updated yet. Postpone updating folds until before
5554 // the call to changed_lines().
5555#ifdef FEAT_FOLDING
5556 disable_fold_update++;
5557#endif
5558
Bram Moolenaard79e5502016-01-10 22:13:02 +01005559 if (!VIsual_active)
5560 {
5561 pos = curwin->w_cursor;
5562 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005563 {
5564#ifdef FEAT_FOLDING
5565 disable_fold_update--;
5566#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005567 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005568 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005569 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005570#ifdef FEAT_FOLDING
5571 disable_fold_update--;
5572#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005573 if (change_cnt)
5574 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5575 }
5576 else
5577 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005578 int one_change;
5579 int length;
5580 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005581
5582 if (u_save((linenr_T)(oap->start.lnum - 1),
5583 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005584 {
5585#ifdef FEAT_FOLDING
5586 disable_fold_update--;
5587#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005588 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005589 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005590
5591 pos = oap->start;
5592 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5593 {
5594 if (oap->block_mode) /* Visual block mode */
5595 {
5596 block_prep(oap, &bd, pos.lnum, FALSE);
5597 pos.col = bd.textcol;
5598 length = bd.textlen;
5599 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005600 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005601 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005602 curwin->w_cursor.col = 0;
5603 pos.col = 0;
5604 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5605 }
5606 else /* oap->motion_type == MCHAR */
5607 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005608 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005609 dec(&(oap->end));
5610 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5611 pos.col = 0;
5612 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005613 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005614 pos.col += oap->start.col;
5615 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005616 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005617 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005618 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005619 length = (int)STRLEN(ml_get(oap->end.lnum));
5620 if (oap->end.col >= length)
5621 oap->end.col = length - 1;
5622 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005623 }
5624 }
5625 one_change = do_addsub(oap->op_type, &pos, length, amount);
5626 if (one_change)
5627 {
5628 /* Remember the start position of the first change. */
5629 if (change_cnt == 0)
5630 startpos = curbuf->b_op_start;
5631 ++change_cnt;
5632 }
5633
5634#ifdef FEAT_NETBEANS_INTG
5635 if (netbeans_active() && one_change)
5636 {
5637 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5638
5639 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5640 netbeans_inserted(curbuf, pos.lnum, pos.col,
5641 &ptr[pos.col], length);
5642 }
5643#endif
5644 if (g_cmd && one_change)
5645 amount += Prenum1;
5646 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005647
5648#ifdef FEAT_FOLDING
5649 disable_fold_update--;
5650#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005651 if (change_cnt)
5652 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5653
5654 if (!change_cnt && oap->is_VIsual)
5655 /* No change: need to remove the Visual selection */
5656 redraw_curbuf_later(INVERTED);
5657
5658 /* Set '[ mark if something changed. Keep the last end
5659 * position from do_addsub(). */
5660 if (change_cnt > 0)
5661 curbuf->b_op_start = startpos;
5662
5663 if (change_cnt > p_report)
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005664 smsg((char_u *)NGETTEXT("%ld line changed", "%ld lines changed",
5665 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005666 }
5667}
5668
5669/*
5670 * Add or subtract 'Prenum1' from a number in a line
5671 * op_type is OP_NR_ADD or OP_NR_SUB
5672 *
5673 * Returns TRUE if some character was changed.
5674 */
5675 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005676do_addsub(
5677 int op_type,
5678 pos_T *pos,
5679 int length,
5680 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005681{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 int col;
5683 char_u *buf1;
5684 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005685 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005687 uvarnumber_T n;
5688 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 char_u *ptr;
5690 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 int todel;
5692 int dohex;
5693 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005694 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 int doalp;
5696 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005698 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005699 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005700 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005701 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005702 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005703 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005704 pos_T startpos;
5705 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706
5707 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5708 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005709 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5711
Bram Moolenaard79e5502016-01-10 22:13:02 +01005712 curwin->w_cursor = *pos;
5713 ptr = ml_get(pos->lnum);
5714 col = pos->col;
5715
5716 if (*ptr == NUL)
5717 goto theend;
5718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 /*
5720 * First check if we are on a hexadecimal number, after the "0x".
5721 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005722 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005724 if (dobin)
5725 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005726 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005727 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005728#ifdef FEAT_MBYTE
5729 if (has_mbyte)
5730 col -= (*mb_head_off)(ptr, ptr + col);
5731#endif
5732 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005733
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005734 if (dohex)
5735 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005736 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005737 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005738#ifdef FEAT_MBYTE
5739 if (has_mbyte)
5740 col -= (*mb_head_off)(ptr, ptr + col);
5741#endif
5742 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005743
5744 if ( dobin
5745 && dohex
5746 && ! ((col > 0
5747 && (ptr[col] == 'X'
5748 || ptr[col] == 'x')
5749 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005750#ifdef FEAT_MBYTE
5751 && (!has_mbyte ||
5752 !(*mb_head_off)(ptr, ptr + col - 1))
5753#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005754 && vim_isxdigit(ptr[col + 1]))))
5755 {
5756
5757 /* In case of binary/hexadecimal pattern overlap match, rescan */
5758
Bram Moolenaard79e5502016-01-10 22:13:02 +01005759 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005760
5761 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005762 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005763 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005764#ifdef FEAT_MBYTE
5765 if (has_mbyte)
5766 col -= (*mb_head_off)(ptr, ptr + col);
5767#endif
5768 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005769 }
5770
5771 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005772 && col > 0
5773 && (ptr[col] == 'X'
5774 || ptr[col] == 'x')
5775 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005776#ifdef FEAT_MBYTE
5777 && (!has_mbyte ||
5778 !(*mb_head_off)(ptr, ptr + col - 1))
5779#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005780 && vim_isxdigit(ptr[col + 1])) ||
5781 ( dobin
5782 && col > 0
5783 && (ptr[col] == 'B'
5784 || ptr[col] == 'b')
5785 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005786#ifdef FEAT_MBYTE
5787 && (!has_mbyte ||
5788 !(*mb_head_off)(ptr, ptr + col - 1))
5789#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005790 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005791 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005792 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005793 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005794#ifdef FEAT_MBYTE
5795 if (has_mbyte)
5796 col -= (*mb_head_off)(ptr, ptr + col);
5797#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005798 }
5799 else
5800 {
5801 /*
5802 * Search forward and then backward to find the start of number.
5803 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005804 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005805
5806 while (ptr[col] != NUL
5807 && !vim_isdigit(ptr[col])
5808 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005809 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005810
5811 while (col > 0
5812 && vim_isdigit(ptr[col - 1])
5813 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005814 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005815 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005816#ifdef FEAT_MBYTE
5817 if (has_mbyte)
5818 col -= (*mb_head_off)(ptr, ptr + col);
5819#endif
5820 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005821 }
5822 }
5823
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005824 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005825 {
5826 while (ptr[col] != NUL && length > 0
5827 && !vim_isdigit(ptr[col])
5828 && !(doalp && ASCII_ISALPHA(ptr[col])))
5829 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005830 int mb_len = MB_PTR2LEN(ptr + col);
5831
5832 col += mb_len;
5833 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005834 }
5835
5836 if (length == 0)
5837 goto theend;
5838
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005839 if (col > pos->col && ptr[col - 1] == '-'
5840#ifdef FEAT_MBYTE
5841 && (!has_mbyte ||
5842 !(*mb_head_off)(ptr, ptr + col - 1))
5843#endif
5844 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005845 {
5846 negative = TRUE;
5847 was_positive = FALSE;
5848 }
5849 }
5850
5851 /*
5852 * If a number was found, and saving for undo works, replace the number.
5853 */
5854 firstdigit = ptr[col];
5855 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5856 {
5857 beep_flush();
5858 goto theend;
5859 }
5860
5861 if (doalp && ASCII_ISALPHA(firstdigit))
5862 {
5863 /* decrement or increment alphabetic character */
5864 if (op_type == OP_NR_SUB)
5865 {
5866 if (CharOrd(firstdigit) < Prenum1)
5867 {
5868 if (isupper(firstdigit))
5869 firstdigit = 'A';
5870 else
5871 firstdigit = 'a';
5872 }
5873 else
5874#ifdef EBCDIC
5875 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5876#else
5877 firstdigit -= Prenum1;
5878#endif
5879 }
5880 else
5881 {
5882 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5883 {
5884 if (isupper(firstdigit))
5885 firstdigit = 'Z';
5886 else
5887 firstdigit = 'z';
5888 }
5889 else
5890#ifdef EBCDIC
5891 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5892#else
5893 firstdigit += Prenum1;
5894#endif
5895 }
5896 curwin->w_cursor.col = col;
5897 if (!did_change)
5898 startpos = curwin->w_cursor;
5899 did_change = TRUE;
5900 (void)del_char(FALSE);
5901 ins_char(firstdigit);
5902 endpos = curwin->w_cursor;
5903 curwin->w_cursor.col = col;
5904 }
5905 else
5906 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005907 if (col > 0 && ptr[col - 1] == '-'
5908#ifdef FEAT_MBYTE
5909 && (!has_mbyte ||
5910 !(*mb_head_off)(ptr, ptr + col - 1))
5911#endif
5912 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005913 {
5914 /* negative number */
5915 --col;
5916 negative = TRUE;
5917 }
5918 /* get the number value (unsigned) */
5919 if (visual && VIsual_mode != 'V')
5920 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5921 ? (int)STRLEN(ptr) - col
5922 : length);
5923
5924 vim_str2nr(ptr + col, &pre, &length,
5925 0 + (dobin ? STR2NR_BIN : 0)
5926 + (dooct ? STR2NR_OCT : 0)
5927 + (dohex ? STR2NR_HEX : 0),
5928 NULL, &n, maxlen);
5929
5930 /* ignore leading '-' for hex and octal and bin numbers */
5931 if (pre && negative)
5932 {
5933 ++col;
5934 --length;
5935 negative = FALSE;
5936 }
5937 /* add or subtract */
5938 subtract = FALSE;
5939 if (op_type == OP_NR_SUB)
5940 subtract ^= TRUE;
5941 if (negative)
5942 subtract ^= TRUE;
5943
5944 oldn = n;
5945 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005946 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005947 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005948 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005949 /* handle wraparound for decimal numbers */
5950 if (!pre)
5951 {
5952 if (subtract)
5953 {
5954 if (n > oldn)
5955 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005956 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005957 negative ^= TRUE;
5958 }
5959 }
5960 else
5961 {
5962 /* add */
5963 if (n < oldn)
5964 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005965 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005966 negative ^= TRUE;
5967 }
5968 }
5969 if (n == 0)
5970 negative = FALSE;
5971 }
5972
5973 if (visual && !was_positive && !negative && col > 0)
5974 {
5975 /* need to remove the '-' */
5976 col--;
5977 length++;
5978 }
5979
5980 /*
5981 * Delete the old number.
5982 */
5983 curwin->w_cursor.col = col;
5984 if (!did_change)
5985 startpos = curwin->w_cursor;
5986 did_change = TRUE;
5987 todel = length;
5988 c = gchar_cursor();
5989 /*
5990 * Don't include the '-' in the length, only the length of the
5991 * part after it is kept the same.
5992 */
5993 if (c == '-')
5994 --length;
5995 while (todel-- > 0)
5996 {
5997 if (c < 0x100 && isalpha(c))
5998 {
5999 if (isupper(c))
6000 hexupper = TRUE;
6001 else
6002 hexupper = FALSE;
6003 }
6004 /* del_char() will mark line needing displaying */
6005 (void)del_char(FALSE);
6006 c = gchar_cursor();
6007 }
6008
6009 /*
6010 * Prepare the leading characters in buf1[].
6011 * When there are many leading zeros it could be very long.
6012 * Allocate a bit too much.
6013 */
6014 buf1 = alloc((unsigned)length + NUMBUFLEN);
6015 if (buf1 == NULL)
6016 goto theend;
6017 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02006018 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01006019 {
6020 *ptr++ = '-';
6021 }
6022 if (pre)
6023 {
6024 *ptr++ = '0';
6025 --length;
6026 }
6027 if (pre == 'b' || pre == 'B' ||
6028 pre == 'x' || pre == 'X')
6029 {
6030 *ptr++ = pre;
6031 --length;
6032 }
6033
6034 /*
6035 * Put the number characters in buf2[].
6036 */
6037 if (pre == 'b' || pre == 'B')
6038 {
6039 int i;
6040 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006041 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006042
6043 /* leading zeros */
6044 for (bit = bits; bit > 0; bit--)
6045 if ((n >> (bit - 1)) & 0x1) break;
6046
6047 for (i = 0; bit > 0; bit--)
6048 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6049
6050 buf2[i] = '\0';
6051 }
6052 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006053 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6054 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006055 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006056 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6057 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006058 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006059 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6060 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006061 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006062 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6063 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006064 length -= (int)STRLEN(buf2);
6065
6066 /*
6067 * Adjust number of zeros to the new number of digits, so the
6068 * total length of the number remains the same.
6069 * Don't do this when
6070 * the result may look like an octal number.
6071 */
6072 if (firstdigit == '0' && !(dooct && pre == 0))
6073 while (length-- > 0)
6074 *ptr++ = '0';
6075 *ptr = NUL;
6076 STRCAT(buf1, buf2);
6077 ins_str(buf1); /* insert the new number */
6078 vim_free(buf1);
6079 endpos = curwin->w_cursor;
6080 if (did_change && curwin->w_cursor.col)
6081 --curwin->w_cursor.col;
6082 }
6083
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006084 if (did_change)
6085 {
6086 /* set the '[ and '] marks */
6087 curbuf->b_op_start = startpos;
6088 curbuf->b_op_end = endpos;
6089 if (curbuf->b_op_end.col > 0)
6090 --curbuf->b_op_end.col;
6091 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006092
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006093theend:
6094 if (visual)
6095 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006096 else if (did_change)
6097 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006098
Bram Moolenaard79e5502016-01-10 22:13:02 +01006099 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100}
6101
6102#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006103
6104static yankreg_T *y_read_regs = NULL;
6105
6106#define REG_PREVIOUS 1
6107#define REG_EXEC 2
6108
6109/*
6110 * Prepare for reading viminfo registers when writing viminfo later.
6111 */
6112 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006113prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006114{
6115 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6116 * (int)sizeof(yankreg_T));
6117}
6118
6119 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006120finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006121{
6122 int i;
6123 int j;
6124
6125 if (y_read_regs != NULL)
6126 {
6127 for (i = 0; i < NUM_REGISTERS; ++i)
6128 if (y_read_regs[i].y_array != NULL)
6129 {
6130 for (j = 0; j < y_read_regs[i].y_size; j++)
6131 vim_free(y_read_regs[i].y_array[j]);
6132 vim_free(y_read_regs[i].y_array);
6133 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006134 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006135 }
6136}
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006139read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
6141 int eof;
6142 int do_it = TRUE;
6143 int size;
6144 int limit;
6145 int i;
6146 int set_prev = FALSE;
6147 char_u *str;
6148 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006149 int new_type = MCHAR; /* init to shut up compiler */
6150 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 /* We only get here (hopefully) if line[0] == '"' */
6153 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006154
6155 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (*str == '"')
6157 {
6158 set_prev = TRUE;
6159 str++;
6160 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (!ASCII_ISALNUM(*str) && *str != '-')
6163 {
6164 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6165 return TRUE; /* too many errors, pretend end-of-file */
6166 do_it = FALSE;
6167 }
6168 get_yank_register(*str++, FALSE);
6169 if (!force && y_current->y_array != NULL)
6170 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006171
6172 if (*str == '@')
6173 {
6174 /* "x@: register x used for @@ */
6175 if (force || execreg_lastc == NUL)
6176 execreg_lastc = str[-1];
6177 }
6178
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 size = 0;
6180 limit = 100; /* Optimized for registers containing <= 100 lines */
6181 if (do_it)
6182 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006183 /*
6184 * Build the new register in array[].
6185 * y_array is kept as-is until done.
6186 * The "do_it" flag is reset when something is wrong, in which case
6187 * array[] needs to be freed.
6188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (set_prev)
6190 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006191 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006192 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006194 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006196 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006198 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 /* get the block width; if it's missing we get a zero, which is OK */
6200 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006201 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203
6204 while (!(eof = viminfo_readline(virp))
6205 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6206 {
6207 if (do_it)
6208 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006209 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006211 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006213
6214 if (new_array == NULL)
6215 {
6216 do_it = FALSE;
6217 break;
6218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006220 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006222 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
6225 str = viminfo_readstring(virp, 1, TRUE);
6226 if (str != NULL)
6227 array[size++] = str;
6228 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006229 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 do_it = FALSE;
6231 }
6232 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (do_it)
6235 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006236 /* free y_array[] */
6237 for (i = 0; i < y_current->y_size; i++)
6238 vim_free(y_current->y_array[i]);
6239 vim_free(y_current->y_array);
6240
6241 y_current->y_type = new_type;
6242 y_current->y_width = new_width;
6243 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006244 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 if (size == 0)
6246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 y_current->y_array = NULL;
6248 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006251 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 y_current->y_array =
6253 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6254 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006255 {
6256 if (y_current->y_array == NULL)
6257 vim_free(array[i]);
6258 else
6259 y_current->y_array[i] = array[i];
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006263 else
6264 {
6265 /* Free array[] if it was filled. */
6266 for (i = 0; i < size; i++)
6267 vim_free(array[i]);
6268 }
6269 vim_free(array);
6270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 return eof;
6272}
6273
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006274/*
6275 * Accept a new style register line from the viminfo, store it when it's new.
6276 */
6277 void
6278handle_viminfo_register(garray_T *values, int force)
6279{
6280 bval_T *vp = (bval_T *)values->ga_data;
6281 int flags;
6282 int name;
6283 int type;
6284 int linecount;
6285 int width;
6286 time_t timestamp;
6287 yankreg_T *y_ptr;
6288 int i;
6289
6290 /* Check the format:
6291 * |{bartype},{flags},{name},{type},
6292 * {linecount},{width},{timestamp},"line1","line2"
6293 */
6294 if (values->ga_len < 6
6295 || vp[0].bv_type != BVAL_NR
6296 || vp[1].bv_type != BVAL_NR
6297 || vp[2].bv_type != BVAL_NR
6298 || vp[3].bv_type != BVAL_NR
6299 || vp[4].bv_type != BVAL_NR
6300 || vp[5].bv_type != BVAL_NR)
6301 return;
6302 flags = vp[0].bv_nr;
6303 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006304 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006305 return;
6306 type = vp[2].bv_nr;
6307 if (type != MCHAR && type != MLINE && type != MBLOCK)
6308 return;
6309 linecount = vp[3].bv_nr;
6310 if (values->ga_len < 6 + linecount)
6311 return;
6312 width = vp[4].bv_nr;
6313 if (width < 0)
6314 return;
6315
6316 if (y_read_regs != NULL)
6317 /* Reading viminfo for merging and writing. Store the register
6318 * content, don't update the current registers. */
6319 y_ptr = &y_read_regs[name];
6320 else
6321 y_ptr = &y_regs[name];
6322
6323 /* Do not overwrite unless forced or the timestamp is newer. */
6324 timestamp = (time_t)vp[5].bv_nr;
6325 if (y_ptr->y_array != NULL && !force
6326 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6327 return;
6328
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006329 if (y_ptr->y_array != NULL)
6330 for (i = 0; i < y_ptr->y_size; i++)
6331 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006332 vim_free(y_ptr->y_array);
6333
6334 if (y_read_regs == NULL)
6335 {
6336 if (flags & REG_PREVIOUS)
6337 y_previous = y_ptr;
6338 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6339 execreg_lastc = get_register_name(name);
6340 }
6341 y_ptr->y_type = type;
6342 y_ptr->y_width = width;
6343 y_ptr->y_size = linecount;
6344 y_ptr->y_time_set = timestamp;
6345 if (linecount == 0)
6346 y_ptr->y_array = NULL;
6347 else
6348 {
6349 y_ptr->y_array =
6350 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6351 for (i = 0; i < linecount; i++)
6352 {
6353 if (vp[i + 6].bv_allocated)
6354 {
6355 y_ptr->y_array[i] = vp[i + 6].bv_string;
6356 vp[i + 6].bv_string = NULL;
6357 }
6358 else
6359 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6360 }
6361 }
6362}
6363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006367 int i, j;
6368 char_u *type;
6369 char_u c;
6370 int num_lines;
6371 int max_num_lines;
6372 int max_kbyte;
6373 long len;
6374 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
Bram Moolenaar64404472010-06-26 06:24:45 +02006376 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 /* Get '<' value, use old '"' value if '<' is not found. */
6379 max_num_lines = get_viminfo_parameter('<');
6380 if (max_num_lines < 0)
6381 max_num_lines = get_viminfo_parameter('"');
6382 if (max_num_lines == 0)
6383 return;
6384 max_kbyte = get_viminfo_parameter('s');
6385 if (max_kbyte == 0)
6386 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006387
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 for (i = 0; i < NUM_REGISTERS; i++)
6389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390#ifdef FEAT_CLIPBOARD
6391 /* Skip '*'/'+' register, we don't want them back next time */
6392 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6393 continue;
6394#endif
6395#ifdef FEAT_DND
6396 /* Neither do we want the '~' register */
6397 if (i == TILDE_REGISTER)
6398 continue;
6399#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006400 /* When reading viminfo for merging and writing: Use the register from
6401 * viminfo if it's newer. */
6402 if (y_read_regs != NULL
6403 && y_read_regs[i].y_array != NULL
6404 && (y_regs[i].y_array == NULL ||
6405 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6406 y_ptr = &y_read_regs[i];
6407 else if (y_regs[i].y_array == NULL)
6408 continue;
6409 else
6410 y_ptr = &y_regs[i];
6411
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006412 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006413 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006414 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006415 || (num_lines == 1 && y_ptr->y_type == MCHAR
6416 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006417 continue;
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (max_kbyte > 0)
6420 {
6421 /* Skip register if there is more text than the maximum size. */
6422 len = 0;
6423 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006424 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 if (len > (long)max_kbyte * 1024L)
6426 continue;
6427 }
6428
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006429 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
6431 case MLINE:
6432 type = (char_u *)"LINE";
6433 break;
6434 case MCHAR:
6435 type = (char_u *)"CHAR";
6436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 case MBLOCK:
6438 type = (char_u *)"BLOCK";
6439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 default:
6441 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006442 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 emsg(IObuff);
6444 type = (char_u *)"LINE";
6445 break;
6446 }
6447 if (y_previous == &y_regs[i])
6448 fprintf(fp, "\"");
6449 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006450 fprintf(fp, "\"%c", c);
6451 if (c == execreg_lastc)
6452 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006453 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455 /* If max_num_lines < 0, then we save ALL the lines in the register */
6456 if (max_num_lines > 0 && num_lines > max_num_lines)
6457 num_lines = max_num_lines;
6458 for (j = 0; j < num_lines; j++)
6459 {
6460 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006461 viminfo_writestring(fp, y_ptr->y_array[j]);
6462 }
6463
6464 {
6465 int flags = 0;
6466 int remaining;
6467
6468 /* New style with a bar line. Format:
6469 * |{bartype},{flags},{name},{type},
6470 * {linecount},{width},{timestamp},"line1","line2"
6471 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006472 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006473 */
6474 if (y_previous == &y_regs[i])
6475 flags |= REG_PREVIOUS;
6476 if (c == execreg_lastc)
6477 flags |= REG_EXEC;
6478 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6479 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6480 (long)y_ptr->y_time_set);
6481 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6482 remaining = LSIZE - 71;
6483 for (j = 0; j < num_lines; j++)
6484 {
6485 putc(',', fp);
6486 --remaining;
6487 remaining = barline_writestring(fp, y_ptr->y_array[j],
6488 remaining);
6489 }
6490 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 }
6493}
6494#endif /* FEAT_VIMINFO */
6495
6496#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6497/*
6498 * SELECTION / PRIMARY ('*')
6499 *
6500 * Text selection stuff that uses the GUI selection register '*'. When using a
6501 * GUI this may be text from another window, otherwise it is the last text we
6502 * had highlighted with VIsual mode. With mouse support, clicking the middle
6503 * button performs the paste, otherwise you will need to do <"*p>. "
6504 * If not under X, it is synonymous with the clipboard register '+'.
6505 *
6506 * X CLIPBOARD ('+')
6507 *
6508 * Text selection stuff that uses the GUI clipboard register '+'.
6509 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6510 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6511 * otherwise you will need to do <"+p>. "
6512 * If not under X, it is synonymous with the selection register '*'.
6513 */
6514
6515/*
6516 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006517 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 * only exist while the owning application exists, so we write to the
6519 * permanent (while X runs) store CUT_BUFFER0.
6520 * Dump the CLIPBOARD selection if we own it (it's logically the more
6521 * 'permanent' of the two), otherwise the PRIMARY one.
6522 * For now, use a hard-coded sanity limit of 1Mb of data.
6523 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006524#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006526x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528 Display *dpy;
6529 char_u *str = NULL;
6530 long_u len = 0;
6531 int motion_type = -1;
6532
6533# ifdef FEAT_GUI
6534 if (gui.in_use)
6535 dpy = X_DISPLAY;
6536 else
6537# endif
6538# ifdef FEAT_XCLIPBOARD
6539 dpy = xterm_dpy;
6540# else
6541 return;
6542# endif
6543
6544 /* Get selection to export */
6545 if (clip_plus.owned)
6546 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6547 else if (clip_star.owned)
6548 motion_type = clip_convert_selection(&str, &len, &clip_star);
6549
6550 /* Check it's OK */
6551 if (dpy != NULL && str != NULL && motion_type >= 0
6552 && len < 1024*1024 && len > 0)
6553 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006554#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006555 int ok = TRUE;
6556
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006557 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6558 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6559 * encoding conversion usually doesn't work, so keep the text as-is.
6560 */
6561 if (has_mbyte)
6562 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006563 vimconv_T vc;
6564
6565 vc.vc_type = CONV_NONE;
6566 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6567 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006568 int intlen = len;
6569 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006570
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006571 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006572 conv_str = string_convert(&vc, str, &intlen);
6573 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006574 if (conv_str != NULL)
6575 {
6576 vim_free(str);
6577 str = conv_str;
6578 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006579 else
6580 {
6581 ok = FALSE;
6582 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006583 convert_setup(&vc, NULL, NULL);
6584 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006585 else
6586 {
6587 ok = FALSE;
6588 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006589 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006590
6591 /* Do not store the string if conversion failed. Better to use any
6592 * other selection than garbled text. */
6593 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006594#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006595 {
6596 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6597 XFlush(dpy);
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600
6601 vim_free(str);
6602}
6603#endif
6604
6605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006606clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006608 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
6610 if (cbd == &clip_plus)
6611 y_current = &y_regs[PLUS_REGISTER];
6612 else
6613 y_current = &y_regs[STAR_REGISTER];
6614 free_yank_all();
6615 y_current->y_size = 0;
6616 y_current = y_ptr;
6617}
6618
6619/*
6620 * Get the selected text and put it in the gui selection register '*' or '+'.
6621 */
6622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006623clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006625 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 pos_T old_visual;
6628 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 colnr_T old_curswant;
6630 int old_set_curswant;
6631 pos_T old_op_start, old_op_end;
6632 oparg_T oa;
6633 cmdarg_T ca;
6634
6635 if (cbd->owned)
6636 {
6637 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6638 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6639 return;
6640
6641 /* Get the text between clip_star.start & clip_star.end */
6642 old_y_previous = y_previous;
6643 old_y_current = y_current;
6644 old_cursor = curwin->w_cursor;
6645 old_curswant = curwin->w_curswant;
6646 old_set_curswant = curwin->w_set_curswant;
6647 old_op_start = curbuf->b_op_start;
6648 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 old_visual = VIsual;
6650 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 clear_oparg(&oa);
6652 oa.regname = (cbd == &clip_plus ? '+' : '*');
6653 oa.op_type = OP_YANK;
6654 vim_memset(&ca, 0, sizeof(ca));
6655 ca.oap = &oa;
6656 ca.cmdchar = 'y';
6657 ca.count1 = 1;
6658 ca.retval = CA_NO_ADJ_OP_END;
6659 do_pending_operator(&ca, 0, TRUE);
6660 y_previous = old_y_previous;
6661 y_current = old_y_current;
6662 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006663 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 curwin->w_curswant = old_curswant;
6665 curwin->w_set_curswant = old_set_curswant;
6666 curbuf->b_op_start = old_op_start;
6667 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 VIsual = old_visual;
6669 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006671 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
6673 clip_free_selection(cbd);
6674
6675 /* Try to get selected text from another window */
6676 clip_gen_request_selection(cbd);
6677 }
6678}
6679
Bram Moolenaard44347f2011-06-19 01:14:29 +02006680/*
6681 * Convert from the GUI selection string into the '*'/'+' register.
6682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006684clip_yank_selection(
6685 int type,
6686 char_u *str,
6687 long len,
6688 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006690 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 if (cbd == &clip_plus)
6693 y_ptr = &y_regs[PLUS_REGISTER];
6694 else
6695 y_ptr = &y_regs[STAR_REGISTER];
6696
6697 clip_free_selection(cbd);
6698
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006699 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700}
6701
6702/*
6703 * Convert the '*'/'+' register into a GUI selection string returned in *str
6704 * with length *len.
6705 * Returns the motion type, or -1 for failure.
6706 */
6707 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006708clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 char_u *p;
6711 int lnum;
6712 int i, j;
6713 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006714 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716 if (cbd == &clip_plus)
6717 y_ptr = &y_regs[PLUS_REGISTER];
6718 else
6719 y_ptr = &y_regs[STAR_REGISTER];
6720
6721#ifdef USE_CRNL
6722 eolsize = 2;
6723#else
6724 eolsize = 1;
6725#endif
6726
6727 *str = NULL;
6728 *len = 0;
6729 if (y_ptr->y_array == NULL)
6730 return -1;
6731
6732 for (i = 0; i < y_ptr->y_size; i++)
6733 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6734
6735 /*
6736 * Don't want newline character at end of last line if we're in MCHAR mode.
6737 */
6738 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6739 *len -= eolsize;
6740
6741 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6742 if (p == NULL)
6743 return -1;
6744 lnum = 0;
6745 for (i = 0, j = 0; i < (int)*len; i++, j++)
6746 {
6747 if (y_ptr->y_array[lnum][j] == '\n')
6748 p[i] = NUL;
6749 else if (y_ptr->y_array[lnum][j] == NUL)
6750 {
6751#ifdef USE_CRNL
6752 p[i++] = '\r';
6753#endif
6754#ifdef USE_CR
6755 p[i] = '\r';
6756#else
6757 p[i] = '\n';
6758#endif
6759 lnum++;
6760 j = -1;
6761 }
6762 else
6763 p[i] = y_ptr->y_array[lnum][j];
6764 }
6765 return y_ptr->y_type;
6766}
6767
6768
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769/*
6770 * If we have written to a clipboard register, send the text to the clipboard.
6771 */
6772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006773may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6776 {
6777 clip_own_selection(&clip_star);
6778 clip_gen_set_selection(&clip_star);
6779 }
6780 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6781 {
6782 clip_own_selection(&clip_plus);
6783 clip_gen_set_selection(&clip_plus);
6784 }
6785}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787#endif /* FEAT_CLIPBOARD || PROTO */
6788
6789
6790#if defined(FEAT_DND) || defined(PROTO)
6791/*
6792 * Replace the contents of the '~' register with str.
6793 */
6794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006795dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006797 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799 curr = y_current;
6800 y_current = &y_regs[TILDE_REGISTER];
6801 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006802 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 y_current = curr;
6804}
6805#endif
6806
6807
6808#if defined(FEAT_EVAL) || defined(PROTO)
6809/*
6810 * Return the type of a register.
6811 * Used for getregtype()
6812 * Returns MAUTO for error.
6813 */
6814 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006815get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 switch (regname)
6818 {
6819 case '%': /* file name */
6820 case '#': /* alternate file name */
6821 case '=': /* expression */
6822 case ':': /* last command line */
6823 case '/': /* last search-pattern */
6824 case '.': /* last inserted text */
6825#ifdef FEAT_SEARCHPATH
6826 case Ctrl_F: /* Filename under cursor */
6827 case Ctrl_P: /* Path under cursor, expand via "path" */
6828#endif
6829 case Ctrl_W: /* word under cursor */
6830 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6831 case '_': /* black hole: always empty */
6832 return MCHAR;
6833 }
6834
6835#ifdef FEAT_CLIPBOARD
6836 regname = may_get_selection(regname);
6837#endif
6838
Bram Moolenaar32b92012014-01-14 12:33:36 +01006839 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006840 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 get_yank_register(regname, FALSE);
6843
6844 if (y_current->y_array != NULL)
6845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (reglen != NULL && y_current->y_type == MBLOCK)
6847 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 return y_current->y_type;
6849 }
6850 return MAUTO;
6851}
6852
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006853/*
6854 * When "flags" has GREG_LIST return a list with text "s".
6855 * Otherwise just return "s".
6856 */
6857 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006858getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006859{
6860 if (flags & GREG_LIST)
6861 {
6862 list_T *list = list_alloc();
6863
6864 if (list != NULL)
6865 {
6866 if (list_append_string(list, NULL, -1) == FAIL)
6867 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006868 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006869 return NULL;
6870 }
6871 list->lv_first->li_tv.vval.v_string = s;
6872 }
6873 return (char_u *)list;
6874 }
6875 return s;
6876}
6877
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878/*
6879 * Return the contents of a register as a single allocated string.
6880 * Used for "@r" in expressions and for getreg().
6881 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006882 * Flags:
6883 * GREG_NO_EXPR Do not allow expression register
6884 * GREG_EXPR_SRC For the expression register: return expression itself,
6885 * not the result of its evaluation.
6886 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 */
6888 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 long i;
6892 char_u *retval;
6893 int allocated;
6894 long len;
6895
6896 /* Don't allow using an expression register inside an expression */
6897 if (regname == '=')
6898 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006899 if (flags & GREG_NO_EXPR)
6900 return NULL;
6901 if (flags & GREG_EXPR_SRC)
6902 return getreg_wrap_one_line(get_expr_line_src(), flags);
6903 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905
6906 if (regname == '@') /* "@@" is used for unnamed register */
6907 regname = '"';
6908
6909 /* check for valid regname */
6910 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6911 return NULL;
6912
6913#ifdef FEAT_CLIPBOARD
6914 regname = may_get_selection(regname);
6915#endif
6916
6917 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6918 {
6919 if (retval == NULL)
6920 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006921 if (allocated)
6922 return getreg_wrap_one_line(retval, flags);
6923 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
6925
6926 get_yank_register(regname, FALSE);
6927 if (y_current->y_array == NULL)
6928 return NULL;
6929
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006930 if (flags & GREG_LIST)
6931 {
6932 list_T *list = list_alloc();
6933 int error = FALSE;
6934
6935 if (list == NULL)
6936 return NULL;
6937 for (i = 0; i < y_current->y_size; ++i)
6938 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6939 error = TRUE;
6940 if (error)
6941 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006942 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006943 return NULL;
6944 }
6945 return (char_u *)list;
6946 }
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 /*
6949 * Compute length of resulting string.
6950 */
6951 len = 0;
6952 for (i = 0; i < y_current->y_size; ++i)
6953 {
6954 len += (long)STRLEN(y_current->y_array[i]);
6955 /*
6956 * Insert a newline between lines and after last line if
6957 * y_type is MLINE.
6958 */
6959 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6960 ++len;
6961 }
6962
6963 retval = lalloc(len + 1, TRUE);
6964
6965 /*
6966 * Copy the lines of the yank register into the string.
6967 */
6968 if (retval != NULL)
6969 {
6970 len = 0;
6971 for (i = 0; i < y_current->y_size; ++i)
6972 {
6973 STRCPY(retval + len, y_current->y_array[i]);
6974 len += (long)STRLEN(retval + len);
6975
6976 /*
6977 * Insert a NL between lines and after the last line if y_type is
6978 * MLINE.
6979 */
6980 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6981 retval[len++] = '\n';
6982 }
6983 retval[len] = NUL;
6984 }
6985
6986 return retval;
6987}
6988
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006989 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006990init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006991 int name,
6992 yankreg_T **old_y_previous,
6993 yankreg_T **old_y_current,
6994 int must_append,
6995 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006996{
6997 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6998 {
6999 emsg_invreg(name);
7000 return FAIL;
7001 }
7002
7003 /* Don't want to change the current (unnamed) register */
7004 *old_y_previous = y_previous;
7005 *old_y_current = y_current;
7006
7007 get_yank_register(name, TRUE);
7008 if (!y_append && !must_append)
7009 free_yank_all();
7010 return OK;
7011}
7012
7013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007014finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007015 int name,
7016 yankreg_T *old_y_previous,
7017 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007018{
7019# ifdef FEAT_CLIPBOARD
7020 /* Send text of clipboard register to the clipboard. */
7021 may_set_selection();
7022# endif
7023
7024 /* ':let @" = "val"' should change the meaning of the "" register */
7025 if (name != '"')
7026 y_previous = old_y_previous;
7027 y_current = old_y_current;
7028}
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030/*
7031 * Store string "str" in register "name".
7032 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
7033 * If "must_append" is TRUE, always append to the register. Otherwise append
7034 * if "name" is an uppercase letter.
7035 * Note: "maxlen" and "must_append" don't work for the "/" register.
7036 * Careful: 'str' is modified, you may have to use a copy!
7037 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
7038 */
7039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007040write_reg_contents(
7041 int name,
7042 char_u *str,
7043 int maxlen,
7044 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7047}
7048
7049 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007050write_reg_contents_lst(
7051 int name,
7052 char_u **strings,
7053 int maxlen UNUSED,
7054 int must_append,
7055 int yank_type,
7056 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007057{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007058 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007059
7060 if (name == '/'
7061#ifdef FEAT_EVAL
7062 || name == '='
7063#endif
7064 )
7065 {
7066 char_u *s;
7067
7068 if (strings[0] == NULL)
7069 s = (char_u *)"";
7070 else if (strings[1] != NULL)
7071 {
7072 EMSG(_("E883: search pattern and expression register may not "
7073 "contain two or more lines"));
7074 return;
7075 }
7076 else
7077 s = strings[0];
7078 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7079 return;
7080 }
7081
7082 if (name == '_') /* black hole: nothing to do */
7083 return;
7084
7085 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7086 &yank_type) == FAIL)
7087 return;
7088
7089 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7090
7091 finish_write_reg(name, old_y_previous, old_y_current);
7092}
7093
7094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007095write_reg_contents_ex(
7096 int name,
7097 char_u *str,
7098 int maxlen,
7099 int must_append,
7100 int yank_type,
7101 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007103 yankreg_T *old_y_previous, *old_y_current;
7104 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaare7566042005-06-17 22:00:15 +00007106 if (maxlen >= 0)
7107 len = maxlen;
7108 else
7109 len = (long)STRLEN(str);
7110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 /* Special case: '/' search pattern */
7112 if (name == '/')
7113 {
7114 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7115 return;
7116 }
7117
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007118 if (name == '#')
7119 {
7120 buf_T *buf;
7121
7122 if (VIM_ISDIGIT(*str))
7123 {
7124 int num = atoi((char *)str);
7125
7126 buf = buflist_findnr(num);
7127 if (buf == NULL)
7128 EMSGN(_(e_nobufnr), (long)num);
7129 }
7130 else
7131 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7132 TRUE, FALSE, FALSE));
7133 if (buf == NULL)
7134 return;
7135 curwin->w_alt_fnum = buf->b_fnum;
7136 return;
7137 }
7138
Bram Moolenaare7566042005-06-17 22:00:15 +00007139#ifdef FEAT_EVAL
7140 if (name == '=')
7141 {
7142 char_u *p, *s;
7143
7144 p = vim_strnsave(str, (int)len);
7145 if (p == NULL)
7146 return;
7147 if (must_append)
7148 {
7149 s = concat_str(get_expr_line_src(), p);
7150 vim_free(p);
7151 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007152 }
7153 set_expr_line(p);
7154 return;
7155 }
7156#endif
7157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (name == '_') /* black hole: nothing to do */
7159 return;
7160
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007161 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7162 &yank_type) == FAIL)
7163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007165 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007167 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169#endif /* FEAT_EVAL */
7170
7171#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7172/*
7173 * Put a string into a register. When the register is not empty, the string
7174 * is appended.
7175 */
7176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007177str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007178 yankreg_T *y_ptr, /* pointer to yank register */
7179 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7180 char_u *str, /* string to put in register */
7181 long len, /* length of string */
7182 long blocklen, /* width of Visual block */
7183 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007185 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int lnum;
7187 long start;
7188 long i;
7189 int extra;
7190 int newlines; /* number of lines added */
7191 int extraline = 0; /* extra line at the end */
7192 int append = FALSE; /* append to last line in register */
7193 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007194 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007198 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 y_ptr->y_size = 0;
7200
Bram Moolenaard44347f2011-06-19 01:14:29 +02007201 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007202 type = ((str_list || (len > 0 && (str[len - 1] == NL
7203 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007204 ? MLINE : MCHAR);
7205 else
7206 type = yank_type;
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 /*
7209 * Count the number of lines within the string
7210 */
7211 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007212 if (str_list)
7213 {
7214 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007219 for (i = 0; i < len; i++)
7220 if (str[i] == '\n')
7221 ++newlines;
7222 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7223 {
7224 extraline = 1;
7225 ++newlines; /* count extra newline at the end */
7226 }
7227 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7228 {
7229 append = TRUE;
7230 --newlines; /* uncount newline when appending first line */
7231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
7233
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007234 /* Without any lines make the register empty. */
7235 if (y_ptr->y_size + newlines == 0)
7236 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007237 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007238 return;
7239 }
7240
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 /*
7242 * Allocate an array to hold the pointers to the new register lines.
7243 * If the register was not empty, move the existing lines to the new array.
7244 */
7245 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7246 * sizeof(char_u *), TRUE);
7247 if (pp == NULL) /* out of memory */
7248 return;
7249 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7250 pp[lnum] = y_ptr->y_array[lnum];
7251 vim_free(y_ptr->y_array);
7252 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
7255 /*
7256 * Find the end of each line and save it into the array.
7257 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007258 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007260 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7261 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007262 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007263 pp[lnum] = vim_strnsave(*ss, i);
7264 if (i > maxlen)
7265 maxlen = i;
7266 }
7267 }
7268 else
7269 {
7270 for (start = 0; start < len + extraline; start += i + 1)
7271 {
7272 for (i = start; i < len; ++i) /* find the end of the line */
7273 if (str[i] == '\n')
7274 break;
7275 i -= start; /* i is now length of line */
7276 if (i > maxlen)
7277 maxlen = i;
7278 if (append)
7279 {
7280 --lnum;
7281 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7282 }
7283 else
7284 extra = 0;
7285 s = alloc((unsigned)(i + extra + 1));
7286 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007288 if (extra)
7289 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7290 if (append)
7291 vim_free(y_ptr->y_array[lnum]);
7292 if (i)
7293 mch_memmove(s + extra, str + start, (size_t)i);
7294 extra += i;
7295 s[extra] = NUL;
7296 y_ptr->y_array[lnum++] = s;
7297 while (--extra >= 0)
7298 {
7299 if (*s == NUL)
7300 *s = '\n'; /* replace NUL with newline */
7301 ++s;
7302 }
7303 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 }
7306 y_ptr->y_type = type;
7307 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 if (type == MBLOCK)
7309 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7310 else
7311 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007312#ifdef FEAT_VIMINFO
7313 y_ptr->y_time_set = vim_time();
7314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315}
7316#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7317
7318 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007319clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 vim_memset(oap, 0, sizeof(oparg_T));
7322}
7323
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007325 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 *
7327 * "Words" are counted by looking for boundaries between non-space and
7328 * space characters. (it seems to produce results that match 'wc'.)
7329 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007330 * Return value is byte count; word count for the line is added to "*wc".
7331 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 *
7333 * The function will only examine the first "limit" characters in the
7334 * line, stopping if it encounters an end-of-line (NUL byte). In that
7335 * case, eol_size will be added to the character count to account for
7336 * the size of the EOL character.
7337 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007338 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007339line_count_info(
7340 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007341 varnumber_T *wc,
7342 varnumber_T *cc,
7343 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007344 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007346 varnumber_T i;
7347 varnumber_T words = 0;
7348 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 int is_word = 0;
7350
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007351 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 {
7353 if (is_word)
7354 {
7355 if (vim_isspace(line[i]))
7356 {
7357 words++;
7358 is_word = 0;
7359 }
7360 }
7361 else if (!vim_isspace(line[i]))
7362 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007363 ++chars;
7364#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007365 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007366#else
7367 ++i;
7368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 }
7370
7371 if (is_word)
7372 words++;
7373 *wc += words;
7374
7375 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007376 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007379 chars += eol_size;
7380 }
7381 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 return i;
7383}
7384
7385/*
7386 * Give some info about the position of the cursor (for "g CTRL-G").
7387 * In Visual mode, give some info about the selected region. (In this case,
7388 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007389 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 */
7391 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007392cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007395 char_u buf1[50];
7396 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007398 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007399#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007400 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007401#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007402 varnumber_T byte_count_cursor = 0;
7403 varnumber_T char_count = 0;
7404 varnumber_T char_count_cursor = 0;
7405 varnumber_T word_count = 0;
7406 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007407 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007408 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 long line_count_selected = 0;
7410 pos_T min_pos, max_pos;
7411 oparg_T oparg;
7412 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
7414 /*
7415 * Compute the length of the file in characters.
7416 */
7417 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7418 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007419 if (dict == NULL)
7420 {
7421 MSG(_(no_lines_msg));
7422 return;
7423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425 else
7426 {
7427 if (get_fileformat(curbuf) == EOL_DOS)
7428 eol_size = 2;
7429 else
7430 eol_size = 1;
7431
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (VIsual_active)
7433 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007434 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
7436 min_pos = VIsual;
7437 max_pos = curwin->w_cursor;
7438 }
7439 else
7440 {
7441 min_pos = curwin->w_cursor;
7442 max_pos = VIsual;
7443 }
7444 if (*p_sel == 'e' && max_pos.col > 0)
7445 --max_pos.col;
7446
7447 if (VIsual_mode == Ctrl_V)
7448 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007449#ifdef FEAT_LINEBREAK
7450 char_u * saved_sbr = p_sbr;
7451
7452 /* Make 'sbr' empty for a moment to get the correct size. */
7453 p_sbr = empty_option;
7454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 oparg.is_VIsual = 1;
7456 oparg.block_mode = TRUE;
7457 oparg.op_type = OP_NOP;
7458 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007459 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007460#ifdef FEAT_LINEBREAK
7461 p_sbr = saved_sbr;
7462#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007463 if (curwin->w_curswant == MAXCOL)
7464 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 /* Swap the start, end vcol if needed */
7466 if (oparg.end_vcol < oparg.start_vcol)
7467 {
7468 oparg.end_vcol += oparg.start_vcol;
7469 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7470 oparg.end_vcol -= oparg.start_vcol;
7471 }
7472 }
7473 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7477 {
7478 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007479 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 {
7481 ui_breakcheck();
7482 if (got_int)
7483 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007484 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 /* Do extra processing for VIsual mode. */
7488 if (VIsual_active
7489 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7490 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007491 char_u *s = NULL;
7492 long len = 0L;
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 switch (VIsual_mode)
7495 {
7496 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007497#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007501#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007503#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007504 s = bd.textstart;
7505 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 break;
7507 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007508 s = ml_get(lnum);
7509 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 break;
7511 case 'v':
7512 {
7513 colnr_T start_col = (lnum == min_pos.lnum)
7514 ? min_pos.col : 0;
7515 colnr_T end_col = (lnum == max_pos.lnum)
7516 ? max_pos.col - start_col + 1 : MAXCOL;
7517
Bram Moolenaardef9e822004-12-31 20:58:58 +00007518 s = ml_get(lnum) + start_col;
7519 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 }
7521 break;
7522 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007523 if (s != NULL)
7524 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007525 byte_count_cursor += line_count_info(s, &word_count_cursor,
7526 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007527 if (lnum == curbuf->b_ml.ml_line_count
7528 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007529 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007530 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007531 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
7536 /* In non-visual mode, check for the line the cursor is on */
7537 if (lnum == curwin->w_cursor.lnum)
7538 {
7539 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007540 char_count_cursor += char_count;
7541 byte_count_cursor = byte_count +
7542 line_count_info(ml_get(lnum),
7543 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007544 (varnumber_T)(curwin->w_cursor.col + 1),
7545 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
7547 }
7548 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007549 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007550 &char_count, (varnumber_T)MAXCOL,
7551 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553
7554 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007555 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007556 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaared767a22016-01-03 22:49:16 +01007558 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007560 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007562 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7563 {
7564 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7565 &max_pos.col);
7566 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7567 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7568 }
7569 else
7570 buf1[0] = NUL;
7571
7572 if (char_count_cursor == byte_count_cursor
7573 && char_count == byte_count)
7574 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007575 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007576 buf1, line_count_selected,
7577 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007578 (long long)word_count_cursor,
7579 (long long)word_count,
7580 (long long)byte_count_cursor,
7581 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007582 else
7583 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007584 _("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 +01007585 buf1, line_count_selected,
7586 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007587 (long long)word_count_cursor,
7588 (long long)word_count,
7589 (long long)char_count_cursor,
7590 (long long)char_count,
7591 (long long)byte_count_cursor,
7592 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 }
7594 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007595 {
7596 p = ml_get_curline();
7597 validate_virtcol();
7598 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7599 (int)curwin->w_virtcol + 1);
7600 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7601 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602
Bram Moolenaared767a22016-01-03 22:49:16 +01007603 if (char_count_cursor == byte_count_cursor
7604 && char_count == byte_count)
7605 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007606 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007607 (char *)buf1, (char *)buf2,
7608 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007610 (long long)word_count_cursor, (long long)word_count,
7611 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007612 else
7613 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007614 _("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 +01007615 (char *)buf1, (char *)buf2,
7616 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007617 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007618 (long long)word_count_cursor, (long long)word_count,
7619 (long long)char_count_cursor, (long long)char_count,
7620 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007621 }
7622 }
7623
Bram Moolenaared767a22016-01-03 22:49:16 +01007624#ifdef FEAT_MBYTE
7625 bom_count = bomb_size();
7626 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007627 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007628 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007629#endif
7630 if (dict == NULL)
7631 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007632 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007633 p = p_shm;
7634 p_shm = (char_u *)"";
7635 msg(IObuff);
7636 p_shm = p;
7637 }
7638 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007639#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007640 if (dict != NULL)
7641 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007642 dict_add_number(dict, "words", word_count);
7643 dict_add_number(dict, "chars", char_count);
7644 dict_add_number(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007645# ifdef FEAT_MBYTE
7646 + bom_count
7647# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02007648 );
7649 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7650 byte_count_cursor);
7651 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7652 char_count_cursor);
7653 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7654 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657}