blob: 2aec5d4383bf0e4a122b07853052eed880734d0d [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);
98static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static int stuff_yank(int, char_u *);
101static void put_reedit_in_typebuf(int silent);
102static int put_in_typebuf(char_u *s, int esc, int colon,
103 int silent);
104static void stuffescaped(char_u *arg, int literally);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100106static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100108static void free_yank(long);
109static void free_yank_all(void);
110static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200112static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100113static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100116static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
117static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200119static 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 +0000120#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100121static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static int same_leader(linenr_T lnum, int, char_u *, int, char_u *);
124static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
128
129/*
130 * The names of operators.
131 * IMPORTANT: Index must correspond with defines in vim.h!!!
132 * The third field indicates whether the operator always works on lines.
133 */
134static char opchars[][3] =
135{
136 {NUL, NUL, FALSE}, /* OP_NOP */
137 {'d', NUL, FALSE}, /* OP_DELETE */
138 {'y', NUL, FALSE}, /* OP_YANK */
139 {'c', NUL, FALSE}, /* OP_CHANGE */
140 {'<', NUL, TRUE}, /* OP_LSHIFT */
141 {'>', NUL, TRUE}, /* OP_RSHIFT */
142 {'!', NUL, TRUE}, /* OP_FILTER */
143 {'g', '~', FALSE}, /* OP_TILDE */
144 {'=', NUL, TRUE}, /* OP_INDENT */
145 {'g', 'q', TRUE}, /* OP_FORMAT */
146 {':', NUL, TRUE}, /* OP_COLON */
147 {'g', 'U', FALSE}, /* OP_UPPER */
148 {'g', 'u', FALSE}, /* OP_LOWER */
149 {'J', NUL, TRUE}, /* DO_JOIN */
150 {'g', 'J', TRUE}, /* DO_JOIN_NS */
151 {'g', '?', FALSE}, /* OP_ROT13 */
152 {'r', NUL, FALSE}, /* OP_REPLACE */
153 {'I', NUL, FALSE}, /* OP_INSERT */
154 {'A', NUL, FALSE}, /* OP_APPEND */
155 {'z', 'f', TRUE}, /* OP_FOLD */
156 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
157 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
158 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
159 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
160 {'z', 'd', TRUE}, /* OP_FOLDDEL */
161 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
162 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000163 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaard79e5502016-01-10 22:13:02 +0100164 {Ctrl_A, NUL, FALSE}, /* OP_NR_ADD */
165 {Ctrl_X, NUL, FALSE}, /* OP_NR_SUB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166};
167
168/*
169 * Translate a command name into an operator type.
170 * Must only be called with a valid operator name!
171 */
172 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100173get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174{
175 int i;
176
177 if (char1 == 'r') /* ignore second character */
178 return OP_REPLACE;
179 if (char1 == '~') /* when tilde is an operator */
180 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100181 if (char1 == 'g' && char2 == Ctrl_A) /* add */
182 return OP_NR_ADD;
183 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
184 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 for (i = 0; ; ++i)
186 if (opchars[i][0] == char1 && opchars[i][1] == char2)
187 break;
188 return i;
189}
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Return TRUE if operator "op" always works on whole lines.
193 */
194 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100195op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
197 return opchars[op][2];
198}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200/*
201 * Get first operator command character.
202 * Returns 'g' or 'z' if there is another command character.
203 */
204 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100205get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
207 return opchars[optype][0];
208}
209
210/*
211 * Get second operator command character.
212 */
213 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100214get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216 return opchars[optype][1];
217}
218
219/*
220 * op_shift - handle a shift operation
221 */
222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100223op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224{
225 long i;
226 int first_char;
227 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
230 if (u_save((linenr_T)(oap->start.lnum - 1),
231 (linenr_T)(oap->end.lnum + 1)) == FAIL)
232 return;
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (oap->block_mode)
235 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237 for (i = oap->line_count; --i >= 0; )
238 {
239 first_char = *ml_get_curline();
240 if (first_char == NUL) /* empty line */
241 curwin->w_cursor.col = 0;
242#ifdef FEAT_VISUALEXTRA
243 else if (oap->block_mode)
244 shift_block(oap, amount);
245#endif
246 else
247 /* Move the line right if it doesn't start with '#', 'smartindent'
248 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
249#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
250 if (first_char != '#' || !preprocs_left())
251#endif
252 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000253 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 }
255 ++curwin->w_cursor.lnum;
256 }
257
258 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (oap->block_mode)
260 {
261 curwin->w_cursor.lnum = oap->start.lnum;
262 curwin->w_cursor.col = block_col;
263 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100264 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {
266 curwin->w_cursor.lnum = oap->start.lnum;
267 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
268 }
269 else
270 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
271
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100272#ifdef FEAT_FOLDING
273 /* The cursor line is not in a closed fold */
274 foldOpenCursor();
275#endif
276
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 if (oap->line_count > p_report)
279 {
280 if (oap->op_type == OP_RSHIFT)
281 s = (char_u *)">";
282 else
283 s = (char_u *)"<";
284 if (oap->line_count == 1)
285 {
286 if (amount == 1)
287 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
288 else
289 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
290 }
291 else
292 {
293 if (amount == 1)
294 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
295 oap->line_count, s);
296 else
297 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
298 oap->line_count, s, amount);
299 }
300 msg(IObuff);
301 }
302
303 /*
304 * Set "'[" and "']" marks.
305 */
306 curbuf->b_op_start = oap->start;
307 curbuf->b_op_end.lnum = oap->end.lnum;
308 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
309 if (curbuf->b_op_end.col > 0)
310 --curbuf->b_op_end.col;
311}
312
313/*
314 * shift the current line one shiftwidth left (if left != 0) or right
315 * leaves cursor on first blank in the line
316 */
317 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100318shift_line(
319 int left,
320 int round,
321 int amount,
322 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323{
324 int count;
325 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100326 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
328 count = get_indent(); /* get current indent */
329
330 if (round) /* round off indent */
331 {
332 i = count / p_sw; /* number of p_sw rounded down */
333 j = count % p_sw; /* extra spaces */
334 if (j && left) /* first remove extra spaces */
335 --amount;
336 if (left)
337 {
338 i -= amount;
339 if (i < 0)
340 i = 0;
341 }
342 else
343 i += amount;
344 count = i * p_sw;
345 }
346 else /* original vi indent */
347 {
348 if (left)
349 {
350 count -= p_sw * amount;
351 if (count < 0)
352 count = 0;
353 }
354 else
355 count += p_sw * amount;
356 }
357
358 /* Set new indent */
359#ifdef FEAT_VREPLACE
360 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000361 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 else
363#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000364 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365}
366
367#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
368/*
369 * Shift one line of the current block one shiftwidth right or left.
370 * Leaves cursor on first character in block.
371 */
372 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100373shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374{
375 int left = (oap->op_type == OP_LSHIFT);
376 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000377 int total;
378 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100380 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int p_ts = (int)curbuf->b_p_ts;
382 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int i = 0, j = 0;
386 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_RIGHTLEFT
388 int old_p_ri = p_ri;
389
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200390 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392
393 State = INSERT; /* don't want REPLACE for State */
394 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
395 if (bd.is_short)
396 return;
397
398 /* total is number of screen columns to be inserted/removed */
399 total = amount * p_sw;
400 oldp = ml_get_curline();
401
402 if (!left)
403 {
404 /*
405 * 1. Get start vcol
406 * 2. Total ws vcols
407 * 3. Divvy into TABs & spp
408 * 4. Construct new string
409 */
410 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
411 ws_vcol = bd.start_vcol - bd.pre_whitesp;
412 if (bd.startspaces)
413 {
414#ifdef FEAT_MBYTE
415 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100416 {
417 if ((*mb_ptr2len)(bd.textstart) == 1)
418 ++bd.textstart;
419 else
420 {
421 ws_vcol = 0;
422 bd.startspaces = 0;
423 }
424 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000427 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100429 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200431 /* TODO: is passing bd.textstart for start of the line OK? */
432 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
433 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 total += incr;
435 bd.start_vcol += incr;
436 }
437 /* OK, now total=all the VWS reqd, and textstart points at the 1st
438 * non-ws char in the block. */
439 if (!curbuf->b_p_et)
440 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
441 if (i)
442 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
443 else
444 j = total;
445 /* if we're splitting a TAB, allow for it */
446 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
447 len = (int)STRLEN(bd.textstart) + 1;
448 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
449 if (newp == NULL)
450 return;
451 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
452 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200453 vim_memset(newp + bd.textcol, TAB, (size_t)i);
454 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 /* the end */
456 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
457 }
458 else /* left */
459 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000460 colnr_T destination_col; /* column to which text in block will
461 be shifted */
462 char_u *verbatim_copy_end; /* end of the part of the line which is
463 copied verbatim */
464 colnr_T verbatim_copy_width;/* the (displayed) width of this part
465 of line */
466 unsigned fill; /* nr of spaces that replace a TAB */
467 unsigned new_line_len; /* the length of the line after the
468 block shift */
469 size_t block_space_width;
470 size_t shift_amount;
471 char_u *non_white = bd.textstart;
472 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000474 /*
475 * Firstly, let's find the first non-whitespace character that is
476 * displayed after the block's start column and the character's column
477 * number. Also, let's calculate the width of all the whitespace
478 * characters that are displayed in the block and precede the searched
479 * non-whitespace character.
480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000482 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
483 * the part of which is displayed at the block's beginning. Let's start
484 * searching from the next character. */
485 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100486 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000487
488 /* The character's column is in "bd.start_vcol". */
489 non_white_col = bd.start_vcol;
490
Bram Moolenaar1c465442017-03-12 20:10:05 +0100491 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000492 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200493 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 }
496
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497 block_space_width = non_white_col - oap->start_vcol;
498 /* We will shift by "total" or "block_space_width", whichever is less.
499 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000500 shift_amount = (block_space_width < (size_t)total
501 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502
503 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000504 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000505
506 /* Now let's find out how much of the beginning of the line we can
507 * reuse without modification. */
508 verbatim_copy_end = bd.textstart;
509 verbatim_copy_width = bd.start_vcol;
510
511 /* If "bd.startspaces" is set, "bd.textstart" points to the character
512 * preceding the block. We have to subtract its width to obtain its
513 * column number. */
514 if (bd.startspaces)
515 verbatim_copy_width -= bd.start_char_vcols;
516 while (verbatim_copy_width < destination_col)
517 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200518 char_u *line = verbatim_copy_end;
519
520 /* TODO: is passing verbatim_copy_end for start of the line OK? */
521 incr = lbr_chartabsize(line, verbatim_copy_end,
522 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000523 if (verbatim_copy_width + incr > destination_col)
524 break;
525 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100526 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000527 }
528
529 /* If "destination_col" is different from the width of the initial
530 * part of the line that will be copied, it means we encountered a tab
531 * character, which we will have to partly replace with spaces. */
532 fill = destination_col - verbatim_copy_width;
533
534 /* The replacement line will consist of:
535 * - the beginning of the original line up to "verbatim_copy_end",
536 * - "fill" number of spaces,
537 * - the rest of the line, pointed to by non_white. */
538 new_line_len = (unsigned)(verbatim_copy_end - oldp)
539 + fill
540 + (unsigned)STRLEN(non_white) + 1;
541
542 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 if (newp == NULL)
544 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000545 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200546 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000547 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549 /* replace the line */
550 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
551 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
552 State = oldstate;
553 curwin->w_cursor.col = oldcol;
554#ifdef FEAT_RIGHTLEFT
555 p_ri = old_p_ri;
556#endif
557}
558#endif
559
560#ifdef FEAT_VISUALEXTRA
561/*
562 * Insert string "s" (b_insert ? before : after) block :AKelly
563 * Caller must prepare for undo.
564 */
565 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100566block_insert(
567 oparg_T *oap,
568 char_u *s,
569 int b_insert,
570 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
572 int p_ts;
573 int count = 0; /* extra spaces to replace a cut TAB */
574 int spaces = 0; /* non-zero if cutting a TAB */
575 colnr_T offset; /* pointer along new line */
576 unsigned s_len; /* STRLEN(s) */
577 char_u *newp, *oldp; /* new, old lines */
578 linenr_T lnum; /* loop var */
579 int oldstate = State;
580
581 State = INSERT; /* don't want REPLACE for State */
582 s_len = (unsigned)STRLEN(s);
583
584 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
585 {
586 block_prep(oap, bdp, lnum, TRUE);
587 if (bdp->is_short && b_insert)
588 continue; /* OP_INSERT, line ends before block start */
589
590 oldp = ml_get(lnum);
591
592 if (b_insert)
593 {
594 p_ts = bdp->start_char_vcols;
595 spaces = bdp->startspaces;
596 if (spaces != 0)
597 count = p_ts - 1; /* we're cutting a TAB */
598 offset = bdp->textcol;
599 }
600 else /* append */
601 {
602 p_ts = bdp->end_char_vcols;
603 if (!bdp->is_short) /* spaces = padding after block */
604 {
605 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
606 if (spaces != 0)
607 count = p_ts - 1; /* we're cutting a TAB */
608 offset = bdp->textcol + bdp->textlen - (spaces != 0);
609 }
610 else /* spaces = padding to block edge */
611 {
612 /* if $ used, just append to EOL (ie spaces==0) */
613 if (!bdp->is_MAX)
614 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
615 count = spaces;
616 offset = bdp->textcol + bdp->textlen;
617 }
618 }
619
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200620#ifdef FEAT_MBYTE
621 if (has_mbyte && spaces > 0)
622 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100623 int off;
624
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200625 /* Avoid starting halfway a multi-byte character. */
626 if (b_insert)
627 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100628 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200629 }
630 else
631 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100632 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200633 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200634 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100635 spaces -= off;
636 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200637 }
638#endif
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
641 if (newp == NULL)
642 continue;
643
644 /* copy up to shifted part */
645 mch_memmove(newp, oldp, (size_t)(offset));
646 oldp += offset;
647
648 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200649 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
651 /* copy the new text */
652 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
653 offset += s_len;
654
655 if (spaces && !bdp->is_short)
656 {
657 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200658 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 /* We're splitting a TAB, don't copy it. */
660 oldp++;
661 /* We allowed for that TAB, remember this now */
662 count++;
663 }
664
665 if (spaces > 0)
666 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000667 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 ml_replace(lnum, newp, FALSE);
670
671 if (lnum == oap->end.lnum)
672 {
673 /* Set "']" mark to the end of the block instead of the end of
674 * the insert in the first line. */
675 curbuf->b_op_end.lnum = oap->end.lnum;
676 curbuf->b_op_end.col = offset;
677 }
678 } /* for all lnum */
679
680 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
681
682 State = oldstate;
683}
684#endif
685
686#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
687/*
688 * op_reindent - handle reindenting a block of lines.
689 */
690 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100691op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 long i;
694 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200695 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 linenr_T first_changed = 0;
697 linenr_T last_changed = 0;
698 linenr_T start_lnum = curwin->w_cursor.lnum;
699
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000700 /* Don't even try when 'modifiable' is off. */
701 if (!curbuf->b_p_ma)
702 {
703 EMSG(_(e_modifiable));
704 return;
705 }
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 for (i = oap->line_count; --i >= 0 && !got_int; )
708 {
709 /* it's a slow thing to do, so give feedback so there's no worry that
710 * the computer's just hung. */
711
712 if (i > 1
713 && (i % 50 == 0 || i == oap->line_count - 1)
714 && oap->line_count > p_report)
715 smsg((char_u *)_("%ld lines to indent... "), i);
716
717 /*
718 * Be vi-compatible: For lisp indenting the first line is not
719 * indented, unless there is only one line.
720 */
721#ifdef FEAT_LISP
722 if (i != oap->line_count - 1 || oap->line_count == 1
723 || how != get_lisp_indent)
724#endif
725 {
726 l = skipwhite(ml_get_curline());
727 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200728 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200730 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200732 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {
734 /* did change the indent, call changed_lines() later */
735 if (first_changed == 0)
736 first_changed = curwin->w_cursor.lnum;
737 last_changed = curwin->w_cursor.lnum;
738 }
739 }
740 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000741 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743
744 /* put cursor on first non-blank of indented line */
745 curwin->w_cursor.lnum = start_lnum;
746 beginline(BL_SOL | BL_FIX);
747
748 /* Mark changed lines so that they will be redrawn. When Visual
749 * highlighting was present, need to continue until the last line. When
750 * there is no change still need to remove the Visual highlighting. */
751 if (last_changed != 0)
752 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 else if (oap->is_VIsual)
756 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 if (oap->line_count > p_report)
759 {
760 i = oap->line_count - (i + 1);
761 if (i == 1)
762 MSG(_("1 line indented "));
763 else
764 smsg((char_u *)_("%ld lines indented "), i);
765 }
766 /* set '[ and '] marks */
767 curbuf->b_op_start = oap->start;
768 curbuf->b_op_end = oap->end;
769}
770#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
771
772#if defined(FEAT_EVAL) || defined(PROTO)
773/*
774 * Keep the last expression line here, for repeating.
775 */
776static char_u *expr_line = NULL;
777
778/*
779 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
780 * Returns '=' when OK, NUL otherwise.
781 */
782 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100783get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785 char_u *new_line;
786
787 new_line = getcmdline('=', 0L, 0);
788 if (new_line == NULL)
789 return NUL;
790 if (*new_line == NUL) /* use previous line */
791 vim_free(new_line);
792 else
793 set_expr_line(new_line);
794 return '=';
795}
796
797/*
798 * Set the expression for the '=' register.
799 * Argument must be an allocated string.
800 */
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 vim_free(expr_line);
805 expr_line = new_line;
806}
807
808/*
809 * Get the result of the '=' register expression.
810 * Returns a pointer to allocated memory, or NULL for failure.
811 */
812 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100813get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 char_u *expr_copy;
816 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000817 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 if (expr_line == NULL)
820 return NULL;
821
822 /* Make a copy of the expression, because evaluating it may cause it to be
823 * changed. */
824 expr_copy = vim_strsave(expr_line);
825 if (expr_copy == NULL)
826 return NULL;
827
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000828 /* When we are invoked recursively limit the evaluation to 10 levels.
829 * Then return the string as-is. */
830 if (nested >= 10)
831 return expr_copy;
832
833 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000835 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 vim_free(expr_copy);
837 return rv;
838}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000839
840/*
841 * Get the '=' register expression itself, without evaluating it.
842 */
843 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000845{
846 if (expr_line == NULL)
847 return NULL;
848 return vim_strsave(expr_line);
849}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#endif /* FEAT_EVAL */
851
852/*
853 * Check if 'regname' is a valid name of a yank register.
854 * Note: There is no check for 0 (default register), caller should do this
855 */
856 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100857valid_yank_reg(
858 int regname,
859 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
861 if ( (regname > 0 && ASCII_ISALNUM(regname))
862 || (!writing && vim_strchr((char_u *)
863#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100864 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100866 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#endif
868 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100869 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 || regname == '"'
871 || regname == '-'
872 || regname == '_'
873#ifdef FEAT_CLIPBOARD
874 || regname == '*'
875 || regname == '+'
876#endif
877#ifdef FEAT_DND
878 || (!writing && regname == '~')
879#endif
880 )
881 return TRUE;
882 return FALSE;
883}
884
885/*
886 * Set y_current and y_append, according to the value of "regname".
887 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000888 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 *
890 * If regname is 0 and writing, use register 0
891 * If regname is 0 and reading, use previous register
892 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100894get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 int i;
897
898 y_append = FALSE;
899 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
900 {
901 y_current = y_previous;
902 return;
903 }
904 i = regname;
905 if (VIM_ISDIGIT(i))
906 i -= '0';
907 else if (ASCII_ISLOWER(i))
908 i = CharOrdLow(i) + 10;
909 else if (ASCII_ISUPPER(i))
910 {
911 i = CharOrdUp(i) + 10;
912 y_append = TRUE;
913 }
914 else if (regname == '-')
915 i = DELETION_REGISTER;
916#ifdef FEAT_CLIPBOARD
917 /* When selection is not available, use register 0 instead of '*' */
918 else if (clip_star.available && regname == '*')
919 i = STAR_REGISTER;
920 /* When clipboard is not available, use register 0 instead of '+' */
921 else if (clip_plus.available && regname == '+')
922 i = PLUS_REGISTER;
923#endif
924#ifdef FEAT_DND
925 else if (!writing && regname == '~')
926 i = TILDE_REGISTER;
927#endif
928 else /* not 0-9, a-z, A-Z or '-': use register 0 */
929 i = 0;
930 y_current = &(y_regs[i]);
931 if (writing) /* remember the register we write into for do_put() */
932 y_previous = y_current;
933}
934
Bram Moolenaar8299df92004-07-10 09:47:34 +0000935#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936/*
937 * When "regname" is a clipboard register, obtain the selection. If it's not
938 * available return zero, otherwise return "regname".
939 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000940 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100941may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 if (regname == '*')
944 {
945 if (!clip_star.available)
946 regname = 0;
947 else
948 clip_get_selection(&clip_star);
949 }
950 else if (regname == '+')
951 {
952 if (!clip_plus.available)
953 regname = 0;
954 else
955 clip_get_selection(&clip_plus);
956 }
957 return regname;
958}
959#endif
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961/*
962 * Obtain the contents of a "normal" register. The register is made empty.
963 * The returned pointer has allocated memory, use put_register() later.
964 */
965 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100966get_register(
967 int name,
968 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200970 yankreg_T *reg;
971 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973#ifdef FEAT_CLIPBOARD
974 /* When Visual area changed, may have to update selection. Obtain the
975 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000976 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200978 if (clip_isautosel_star())
979 clip_update_selection(&clip_star);
980 may_get_selection(name);
981 }
982 if (name == '+' && clip_plus.available)
983 {
984 if (clip_isautosel_plus())
985 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 may_get_selection(name);
987 }
988#endif
989
990 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200991 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (reg != NULL)
993 {
994 *reg = *y_current;
995 if (copy)
996 {
997 /* If we run out of memory some or all of the lines are empty. */
998 if (reg->y_size == 0)
999 reg->y_array = NULL;
1000 else
1001 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1002 * reg->y_size));
1003 if (reg->y_array != NULL)
1004 {
1005 for (i = 0; i < reg->y_size; ++i)
1006 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1007 }
1008 }
1009 else
1010 y_current->y_array = NULL;
1011 }
1012 return (void *)reg;
1013}
1014
1015/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001016 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
1018 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001019put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 get_yank_register(name, 0);
1022 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001023 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001024 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001026#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /* Send text written to clipboard register to the clipboard. */
1028 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001031
1032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001033free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001034{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001035 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001036
1037 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001038 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001039 free_yank_all();
1040 vim_free(reg);
1041 *y_current = tmp;
1042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044#if defined(FEAT_MOUSE) || defined(PROTO)
1045/*
1046 * return TRUE if the current yank register has type MLINE
1047 */
1048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001049yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
1051 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1052 return FALSE;
1053 if (regname == '_') /* black hole is always empty */
1054 return FALSE;
1055 get_yank_register(regname, FALSE);
1056 return (y_current->y_type == MLINE);
1057}
1058#endif
1059
1060/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001061 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001063 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 */
1065 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001066do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaard55de222007-05-06 13:38:48 +00001068 char_u *p;
1069 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001070 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
1073 if (Recording == FALSE) /* start recording */
1074 {
1075 /* registers 0-9, a-z and " are allowed */
1076 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1077 retval = FAIL;
1078 else
1079 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01001080 Recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 showmode();
1082 regname = c;
1083 retval = OK;
1084 }
1085 }
1086 else /* stop recording */
1087 {
1088 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001089 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1090 * needs to be removed again to put it in a register. exec_reg then
1091 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
1093 Recording = FALSE;
1094 MSG("");
1095 p = get_recorded();
1096 if (p == NULL)
1097 retval = FAIL;
1098 else
1099 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001100 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1101 vim_unescape_csi(p);
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 /*
1104 * We don't want to change the default register here, so save and
1105 * restore the current register name.
1106 */
1107 old_y_previous = y_previous;
1108 old_y_current = y_current;
1109
1110 retval = stuff_yank(regname, p);
1111
1112 y_previous = old_y_previous;
1113 y_current = old_y_current;
1114 }
1115 }
1116 return retval;
1117}
1118
1119/*
1120 * Stuff string "p" into yank register "regname" as a single line (append if
1121 * uppercase). "p" must have been alloced.
1122 *
1123 * return FAIL for failure, OK otherwise
1124 */
1125 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001126stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 char_u *lp;
1129 char_u **pp;
1130
1131 /* check for read-only register */
1132 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1133 {
1134 vim_free(p);
1135 return FAIL;
1136 }
1137 if (regname == '_') /* black hole: don't do anything */
1138 {
1139 vim_free(p);
1140 return OK;
1141 }
1142 get_yank_register(regname, TRUE);
1143 if (y_append && y_current->y_array != NULL)
1144 {
1145 pp = &(y_current->y_array[y_current->y_size - 1]);
1146 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1147 if (lp == NULL)
1148 {
1149 vim_free(p);
1150 return FAIL;
1151 }
1152 STRCPY(lp, *pp);
1153 STRCAT(lp, p);
1154 vim_free(p);
1155 vim_free(*pp);
1156 *pp = lp;
1157 }
1158 else
1159 {
1160 free_yank_all();
1161 if ((y_current->y_array =
1162 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1163 {
1164 vim_free(p);
1165 return FAIL;
1166 }
1167 y_current->y_array[0] = p;
1168 y_current->y_size = 1;
1169 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001170#ifdef FEAT_VIMINFO
1171 y_current->y_time_set = vim_time();
1172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 return OK;
1175}
1176
Bram Moolenaar42b94362009-05-26 16:12:37 +00001177static int execreg_lastc = NUL;
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001180 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001182 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 */
1184 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001185do_execreg(
1186 int regname,
1187 int colon, /* insert ':' before each line */
1188 int addcr, /* always add '\n' to end of line */
1189 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 long i;
1192 char_u *p;
1193 int retval = OK;
1194 int remap;
1195
1196 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001197 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001198 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001199 {
1200 EMSG(_("E748: No previously used register"));
1201 return FAIL;
1202 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001203 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 /* check for valid regname */
1206 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001207 {
1208 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001210 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001211 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
1213#ifdef FEAT_CLIPBOARD
1214 regname = may_get_selection(regname);
1215#endif
1216
1217 if (regname == '_') /* black hole: don't stuff anything */
1218 return OK;
1219
1220#ifdef FEAT_CMDHIST
1221 if (regname == ':') /* use last command line */
1222 {
1223 if (last_cmdline == NULL)
1224 {
1225 EMSG(_(e_nolastcmd));
1226 return FAIL;
1227 }
1228 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1229 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001230 /* Escape all control characters with a CTRL-V */
1231 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001232 (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 +00001233 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001234 {
1235 /* When in Visual mode "'<,'>" will be prepended to the command.
1236 * Remove it when it's already there. */
1237 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001238 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001239 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001240 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001241 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001242 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244#endif
1245#ifdef FEAT_EVAL
1246 else if (regname == '=')
1247 {
1248 p = get_expr_line();
1249 if (p == NULL)
1250 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001251 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 vim_free(p);
1253 }
1254#endif
1255 else if (regname == '.') /* use last inserted text */
1256 {
1257 p = get_last_insert_save();
1258 if (p == NULL)
1259 {
1260 EMSG(_(e_noinstext));
1261 return FAIL;
1262 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001263 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 vim_free(p);
1265 }
1266 else
1267 {
1268 get_yank_register(regname, FALSE);
1269 if (y_current->y_array == NULL)
1270 return FAIL;
1271
1272 /* Disallow remaping for ":@r". */
1273 remap = colon ? REMAP_NONE : REMAP_YES;
1274
1275 /*
1276 * Insert lines into typeahead buffer, from last one to first one.
1277 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001278 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 for (i = y_current->y_size; --i >= 0; )
1280 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001281 char_u *escaped;
1282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 /* insert NL between lines and after last line if type is MLINE */
1284 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1285 || addcr)
1286 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001287 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 return FAIL;
1289 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001290 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1291 if (escaped == NULL)
1292 return FAIL;
1293 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1294 vim_free(escaped);
1295 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001297 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 == FAIL)
1299 return FAIL;
1300 }
1301 Exec_reg = TRUE; /* disable the 'q' command */
1302 }
1303 return retval;
1304}
1305
1306/*
1307 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1308 * used only after other typeahead has been processed.
1309 */
1310 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001311put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 char_u buf[3];
1314
1315 if (restart_edit != NUL)
1316 {
1317 if (restart_edit == 'V')
1318 {
1319 buf[0] = 'g';
1320 buf[1] = 'R';
1321 buf[2] = NUL;
1322 }
1323 else
1324 {
1325 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1326 buf[1] = NUL;
1327 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001328 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 restart_edit = NUL;
1330 }
1331}
1332
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001333/*
1334 * Insert register contents "s" into the typeahead buffer, so that it will be
1335 * executed again.
1336 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1337 * no remapping.
1338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001340put_in_typebuf(
1341 char_u *s,
1342 int esc,
1343 int colon, /* add ':' before the line */
1344 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346 int retval = OK;
1347
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001348 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001350 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001352 {
1353 char_u *p;
1354
1355 if (esc)
1356 p = vim_strsave_escape_csi(s);
1357 else
1358 p = s;
1359 if (p == NULL)
1360 retval = FAIL;
1361 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001362 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1363 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001364 if (esc)
1365 vim_free(p);
1366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001368 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return retval;
1370}
1371
1372/*
1373 * Insert a yank register: copy it into the Read buffer.
1374 * Used by CTRL-R command and middle mouse button in insert mode.
1375 *
1376 * return FAIL for failure, OK otherwise
1377 */
1378 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001379insert_reg(
1380 int regname,
1381 int literally) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 long i;
1384 int retval = OK;
1385 char_u *arg;
1386 int allocated;
1387
1388 /*
1389 * It is possible to get into an endless loop by having CTRL-R a in
1390 * register a and then, in insert mode, doing CTRL-R a.
1391 * If you hit CTRL-C, the loop will be broken here.
1392 */
1393 ui_breakcheck();
1394 if (got_int)
1395 return FAIL;
1396
1397 /* check for valid regname */
1398 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1399 return FAIL;
1400
1401#ifdef FEAT_CLIPBOARD
1402 regname = may_get_selection(regname);
1403#endif
1404
1405 if (regname == '.') /* insert last inserted text */
1406 retval = stuff_inserted(NUL, 1L, TRUE);
1407 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1408 {
1409 if (arg == NULL)
1410 return FAIL;
1411 stuffescaped(arg, literally);
1412 if (allocated)
1413 vim_free(arg);
1414 }
1415 else /* name or number register */
1416 {
1417 get_yank_register(regname, FALSE);
1418 if (y_current->y_array == NULL)
1419 retval = FAIL;
1420 else
1421 {
1422 for (i = 0; i < y_current->y_size; ++i)
1423 {
1424 stuffescaped(y_current->y_array[i], literally);
1425 /*
1426 * Insert a newline between lines and after last line if
1427 * y_type is MLINE.
1428 */
1429 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1430 stuffcharReadbuff('\n');
1431 }
1432 }
1433 }
1434
1435 return retval;
1436}
1437
1438/*
1439 * Stuff a string into the typeahead buffer, such that edit() will insert it
1440 * literally ("literally" TRUE) or interpret is as typed characters.
1441 */
1442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001443stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444{
1445 int c;
1446 char_u *start;
1447
1448 while (*arg != NUL)
1449 {
1450 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1451 * stuff K_SPECIAL to get the effect of a special key when "literally"
1452 * is TRUE. */
1453 start = arg;
1454 while ((*arg >= ' '
1455#ifndef EBCDIC
1456 && *arg < DEL /* EBCDIC: chars above space are normal */
1457#endif
1458 )
1459 || (*arg == K_SPECIAL && !literally))
1460 ++arg;
1461 if (arg > start)
1462 stuffReadbuffLen(start, (long)(arg - start));
1463
1464 /* stuff a single special character */
1465 if (*arg != NUL)
1466 {
1467#ifdef FEAT_MBYTE
1468 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001469 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else
1471#endif
1472 c = *arg++;
1473 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1474 stuffcharReadbuff(Ctrl_V);
1475 stuffcharReadbuff(c);
1476 }
1477 }
1478}
1479
1480/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001481 * If "regname" is a special register, return TRUE and store a pointer to its
1482 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001484 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001485get_spec_reg(
1486 int regname,
1487 char_u **argp,
1488 int *allocated, /* return: TRUE when value was allocated */
1489 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 int cnt;
1492
1493 *argp = NULL;
1494 *allocated = FALSE;
1495 switch (regname)
1496 {
1497 case '%': /* file name */
1498 if (errmsg)
1499 check_fname(); /* will give emsg if not set */
1500 *argp = curbuf->b_fname;
1501 return TRUE;
1502
1503 case '#': /* alternate file name */
1504 *argp = getaltfname(errmsg); /* may give emsg if not set */
1505 return TRUE;
1506
1507#ifdef FEAT_EVAL
1508 case '=': /* result of expression */
1509 *argp = get_expr_line();
1510 *allocated = TRUE;
1511 return TRUE;
1512#endif
1513
1514 case ':': /* last command line */
1515 if (last_cmdline == NULL && errmsg)
1516 EMSG(_(e_nolastcmd));
1517 *argp = last_cmdline;
1518 return TRUE;
1519
1520 case '/': /* last search-pattern */
1521 if (last_search_pat() == NULL && errmsg)
1522 EMSG(_(e_noprevre));
1523 *argp = last_search_pat();
1524 return TRUE;
1525
1526 case '.': /* last inserted text */
1527 *argp = get_last_insert_save();
1528 *allocated = TRUE;
1529 if (*argp == NULL && errmsg)
1530 EMSG(_(e_noinstext));
1531 return TRUE;
1532
1533#ifdef FEAT_SEARCHPATH
1534 case Ctrl_F: /* Filename under cursor */
1535 case Ctrl_P: /* Path under cursor, expand via "path" */
1536 if (!errmsg)
1537 return FALSE;
1538 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001539 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 *allocated = TRUE;
1541 return TRUE;
1542#endif
1543
1544 case Ctrl_W: /* word under cursor */
1545 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1546 if (!errmsg)
1547 return FALSE;
1548 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1549 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1550 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1551 *allocated = TRUE;
1552 return TRUE;
1553
1554 case '_': /* black hole: always empty */
1555 *argp = (char_u *)"";
1556 return TRUE;
1557 }
1558
1559 return FALSE;
1560}
1561
1562/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001563 * Paste a yank register into the command line.
1564 * Only for non-special registers.
1565 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 * insert_reg() can't be used here, because special characters from the
1567 * register contents will be interpreted as commands.
1568 *
1569 * return FAIL for failure, OK otherwise
1570 */
1571 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001572cmdline_paste_reg(
1573 int regname,
1574 int literally, /* Insert text literally instead of "as typed" */
1575 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 get_yank_register(regname, FALSE);
1580 if (y_current->y_array == NULL)
1581 return FAIL;
1582
1583 for (i = 0; i < y_current->y_size; ++i)
1584 {
1585 cmdline_paste_str(y_current->y_array[i], literally);
1586
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001587 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001588 * Don't do this when "remcr" is TRUE. */
1589 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 cmdline_paste_str((char_u *)"\r", literally);
1591
1592 /* Check for CTRL-C, in case someone tries to paste a few thousand
1593 * lines and gets bored. */
1594 ui_breakcheck();
1595 if (got_int)
1596 return FAIL;
1597 }
1598 return OK;
1599}
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1602/*
1603 * Adjust the register name pointed to with "rp" for the clipboard being
1604 * used always and the clipboard being available.
1605 */
1606 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001607adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001609 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1610 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001611 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1612 {
1613 if (clip_unnamed != 0)
1614 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001615 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001616 else
1617 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1618 ? '+' : '*';
1619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (!clip_star.available && *rp == '*')
1621 *rp = 0;
1622 if (!clip_plus.available && *rp == '+')
1623 *rp = 0;
1624}
1625#endif
1626
1627/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001628 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1629 */
1630 void
1631shift_delete_registers()
1632{
1633 int n;
1634
1635 y_current = &y_regs[9];
1636 free_yank_all(); /* free register nine */
1637 for (n = 9; n > 1; --n)
1638 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001639 y_current = &y_regs[1];
1640 if (!y_append)
1641 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001642 y_regs[1].y_array = NULL; /* set register one to empty */
1643}
1644
1645/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001646 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001648 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 */
1650 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001651op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653 int n;
1654 linenr_T lnum;
1655 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 char_u *newp, *oldp;
1657 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1659 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001660 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
1662 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1663 return OK;
1664
1665 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1666 if (oap->empty)
1667 return u_save_cursor();
1668
1669 if (!curbuf->b_p_ma)
1670 {
1671 EMSG(_(e_modifiable));
1672 return FAIL;
1673 }
1674
1675#ifdef FEAT_CLIPBOARD
1676 adjust_clip_reg(&oap->regname);
1677#endif
1678
1679#ifdef FEAT_MBYTE
1680 if (has_mbyte)
1681 mb_adjust_opend(oap);
1682#endif
1683
Bram Moolenaard04b7502010-07-08 22:27:55 +02001684 /*
1685 * Imitate the strange Vi behaviour: If the delete spans more than one
1686 * line and motion_type == MCHAR and the result is a blank line, make the
1687 * delete linewise. Don't do this for the change command or Visual mode.
1688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001691 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001693 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 && oap->op_type == OP_DELETE)
1695 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001696 ptr = ml_get(oap->end.lnum) + oap->end.col;
1697 if (*ptr != NUL)
1698 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 ptr = skipwhite(ptr);
1700 if (*ptr == NUL && inindent(0))
1701 oap->motion_type = MLINE;
1702 }
1703
Bram Moolenaard04b7502010-07-08 22:27:55 +02001704 /*
1705 * Check for trying to delete (e.g. "D") in an empty line.
1706 * Note: For the change operator it is ok.
1707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if ( oap->motion_type == MCHAR
1709 && oap->line_count == 1
1710 && oap->op_type == OP_DELETE
1711 && *ml_get(oap->start.lnum) == NUL)
1712 {
1713 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001714 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 * 'cpoptions' (Vi compatible).
1716 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001717#ifdef FEAT_VIRTUALEDIT
1718 if (virtual_op)
1719 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1720 * marks as if it happened. */
1721 goto setmarks;
1722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1724 beep_flush();
1725 return OK;
1726 }
1727
Bram Moolenaard04b7502010-07-08 22:27:55 +02001728 /*
1729 * Do a yank of whatever we're about to delete.
1730 * If a yank register was specified, put the deleted text into that
1731 * register. For the black hole register '_' don't yank anything.
1732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (oap->regname != '_')
1734 {
1735 if (oap->regname != 0)
1736 {
1737 /* check for read-only register */
1738 if (!valid_yank_reg(oap->regname, TRUE))
1739 {
1740 beep_flush();
1741 return OK;
1742 }
1743 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1744 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1745 did_yank = TRUE;
1746 }
1747
1748 /*
1749 * Put deleted text into register 1 and shift number registers if the
1750 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001751 * Use the register name from before adjust_clip_reg() may have
1752 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001754 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 || oap->line_count > 1 || oap->use_reg_one)
1756 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001757 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if (op_yank(oap, TRUE, FALSE) == OK)
1759 did_yank = TRUE;
1760 }
1761
Bram Moolenaar84298db2012-04-20 13:46:08 +02001762 /* Yank into small delete register when no named register specified
1763 * and the delete is within one line. */
1764 if ((
1765#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001766 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1767 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001768#endif
1769 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 && oap->line_count == 1)
1771 {
1772 oap->regname = '-';
1773 get_yank_register(oap->regname, TRUE);
1774 if (op_yank(oap, TRUE, FALSE) == OK)
1775 did_yank = TRUE;
1776 oap->regname = 0;
1777 }
1778
1779 /*
1780 * If there's too much stuff to fit in the yank register, then get a
1781 * confirmation before doing the delete. This is crude, but simple.
1782 * And it avoids doing a delete of something we can't put back if we
1783 * want.
1784 */
1785 if (!did_yank)
1786 {
1787 int msg_silent_save = msg_silent;
1788
1789 msg_silent = 0; /* must display the prompt */
1790 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1791 msg_silent = msg_silent_save;
1792 if (n != 'y')
1793 {
1794 EMSG(_(e_abort));
1795 return FAIL;
1796 }
1797 }
1798 }
1799
Bram Moolenaard04b7502010-07-08 22:27:55 +02001800 /*
1801 * block mode delete
1802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (oap->block_mode)
1804 {
1805 if (u_save((linenr_T)(oap->start.lnum - 1),
1806 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1807 return FAIL;
1808
1809 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1810 {
1811 block_prep(oap, &bd, lnum, TRUE);
1812 if (bd.textlen == 0) /* nothing to delete */
1813 continue;
1814
1815 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1816 if (lnum == curwin->w_cursor.lnum)
1817 {
1818 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1819# ifdef FEAT_VIRTUALEDIT
1820 curwin->w_cursor.coladd = 0;
1821# endif
1822 }
1823
1824 /* n == number of chars deleted
1825 * If we delete a TAB, it may be replaced by several characters.
1826 * Thus the number of characters may increase!
1827 */
1828 n = bd.textlen - bd.startspaces - bd.endspaces;
1829 oldp = ml_get(lnum);
1830 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1831 if (newp == NULL)
1832 continue;
1833 /* copy up to deleted part */
1834 mch_memmove(newp, oldp, (size_t)bd.textcol);
1835 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001836 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 (size_t)(bd.startspaces + bd.endspaces));
1838 /* copy the part after the deleted part */
1839 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001840 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 /* replace the line */
1842 ml_replace(lnum, newp, FALSE);
1843 }
1844
1845 check_cursor_col();
1846 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1847 oap->end.lnum + 1, 0L);
1848 oap->line_count = 0; /* no lines deleted */
1849 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001850 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 if (oap->op_type == OP_CHANGE)
1853 {
1854 /* Delete the lines except the first one. Temporarily move the
1855 * cursor to the next line. Save the current line number, if the
1856 * last line is deleted it may be changed.
1857 */
1858 if (oap->line_count > 1)
1859 {
1860 lnum = curwin->w_cursor.lnum;
1861 ++curwin->w_cursor.lnum;
1862 del_lines((long)(oap->line_count - 1), TRUE);
1863 curwin->w_cursor.lnum = lnum;
1864 }
1865 if (u_save_cursor() == FAIL)
1866 return FAIL;
1867 if (curbuf->b_p_ai) /* don't delete indent */
1868 {
1869 beginline(BL_WHITE); /* cursor on first non-white */
1870 did_ai = TRUE; /* delete the indent when ESC hit */
1871 ai_col = curwin->w_cursor.col;
1872 }
1873 else
1874 beginline(0); /* cursor in column 0 */
1875 truncate_line(FALSE); /* delete the rest of the line */
1876 /* leave cursor past last char in line */
1877 if (oap->line_count > 1)
1878 u_clearline(); /* "U" command not possible after "2cc" */
1879 }
1880 else
1881 {
1882 del_lines(oap->line_count, TRUE);
1883 beginline(BL_WHITE | BL_FIX);
1884 u_clearline(); /* "U" command not possible after "dd" */
1885 }
1886 }
1887 else
1888 {
1889#ifdef FEAT_VIRTUALEDIT
1890 if (virtual_op)
1891 {
1892 int endcol = 0;
1893
1894 /* For virtualedit: break the tabs that are partly included. */
1895 if (gchar_pos(&oap->start) == '\t')
1896 {
1897 if (u_save_cursor() == FAIL) /* save first line for undo */
1898 return FAIL;
1899 if (oap->line_count == 1)
1900 endcol = getviscol2(oap->end.col, oap->end.coladd);
1901 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1902 oap->start = curwin->w_cursor;
1903 if (oap->line_count == 1)
1904 {
1905 coladvance(endcol);
1906 oap->end.col = curwin->w_cursor.col;
1907 oap->end.coladd = curwin->w_cursor.coladd;
1908 curwin->w_cursor = oap->start;
1909 }
1910 }
1911
1912 /* Break a tab only when it's included in the area. */
1913 if (gchar_pos(&oap->end) == '\t'
1914 && (int)oap->end.coladd < oap->inclusive)
1915 {
1916 /* save last line for undo */
1917 if (u_save((linenr_T)(oap->end.lnum - 1),
1918 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1919 return FAIL;
1920 curwin->w_cursor = oap->end;
1921 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1922 oap->end = curwin->w_cursor;
1923 curwin->w_cursor = oap->start;
1924 }
1925 }
1926#endif
1927
1928 if (oap->line_count == 1) /* delete characters within one line */
1929 {
1930 if (u_save_cursor() == FAIL) /* save line for undo */
1931 return FAIL;
1932
1933 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001934 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 && oap->op_type == OP_CHANGE
1936 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001937 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 display_dollar(oap->end.col - !oap->inclusive);
1939
1940 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1941
1942#ifdef FEAT_VIRTUALEDIT
1943 if (virtual_op)
1944 {
1945 /* fix up things for virtualedit-delete:
1946 * break the tabs which are going to get in our way
1947 */
1948 char_u *curline = ml_get_curline();
1949 int len = (int)STRLEN(curline);
1950
1951 if (oap->end.coladd != 0
1952 && (int)oap->end.col >= len - 1
1953 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1954 n++;
1955 /* Delete at least one char (e.g, when on a control char). */
1956 if (n == 0 && oap->start.coladd != oap->end.coladd)
1957 n = 1;
1958
1959 /* When deleted a char in the line, reset coladd. */
1960 if (gchar_cursor() != NUL)
1961 curwin->w_cursor.coladd = 0;
1962 }
1963#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001964 (void)del_bytes((long)n, !virtual_op,
1965 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967 else /* delete characters between lines */
1968 {
1969 pos_T curpos;
1970
1971 /* save deleted and changed lines for undo */
1972 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1973 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1974 return FAIL;
1975
1976 truncate_line(TRUE); /* delete from cursor to end of line */
1977
1978 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1979 ++curwin->w_cursor.lnum;
1980 del_lines((long)(oap->line_count - 2), FALSE);
1981
Bram Moolenaard009e862015-06-09 20:20:03 +02001982 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001983 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001984 curwin->w_cursor.col = 0;
1985 (void)del_bytes((long)n, !virtual_op,
1986 oap->op_type == OP_DELETE && !oap->is_VIsual);
1987 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1988 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 }
1991
1992 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1993
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001994#ifdef FEAT_VIRTUALEDIT
1995setmarks:
1996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (oap->block_mode)
1998 {
1999 curbuf->b_op_end.lnum = oap->end.lnum;
2000 curbuf->b_op_end.col = oap->start.col;
2001 }
2002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 curbuf->b_op_end = oap->start;
2004 curbuf->b_op_start = oap->start;
2005
2006 return OK;
2007}
2008
2009#ifdef FEAT_MBYTE
2010/*
2011 * Adjust end of operating area for ending on a multi-byte character.
2012 * Used for deletion.
2013 */
2014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002015mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 char_u *p;
2018
2019 if (oap->inclusive)
2020 {
2021 p = ml_get(oap->end.lnum);
2022 oap->end.col += mb_tail_off(p, p + oap->end.col);
2023 }
2024}
2025#endif
2026
2027#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2028/*
2029 * Replace a whole area with one character.
2030 */
2031 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002032op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034 int n, numc;
2035#ifdef FEAT_MBYTE
2036 int num_chars;
2037#endif
2038 char_u *newp, *oldp;
2039 size_t oldlen;
2040 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002041 char_u *after_p = NULL;
2042 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2045 return OK; /* nothing to do */
2046
Bram Moolenaard9820532013-11-04 01:41:17 +01002047 if (had_ctrl_v_cr)
2048 c = (c == -1 ? '\r' : '\n');
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050#ifdef FEAT_MBYTE
2051 if (has_mbyte)
2052 mb_adjust_opend(oap);
2053#endif
2054
2055 if (u_save((linenr_T)(oap->start.lnum - 1),
2056 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2057 return FAIL;
2058
2059 /*
2060 * block mode replace
2061 */
2062 if (oap->block_mode)
2063 {
2064 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2065 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2066 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002067 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2069 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2070 continue; /* nothing to replace */
2071
2072 /* n == number of extra chars required
2073 * If we split a TAB, it may be replaced by several characters.
2074 * Thus the number of characters may increase!
2075 */
2076#ifdef FEAT_VIRTUALEDIT
2077 /* If the range starts in virtual space, count the initial
2078 * coladd offset as part of "startspaces" */
2079 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2080 {
2081 pos_T vpos;
2082
Bram Moolenaara1381de2009-11-03 15:44:21 +00002083 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 getvpos(&vpos, oap->start_vcol);
2085 bd.startspaces += vpos.coladd;
2086 n = bd.startspaces;
2087 }
2088 else
2089#endif
2090 /* allow for pre spaces */
2091 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2092
2093 /* allow for post spp */
2094 n += (bd.endspaces
2095#ifdef FEAT_VIRTUALEDIT
2096 && !bd.is_oneChar
2097#endif
2098 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2099 /* Figure out how many characters to replace. */
2100 numc = oap->end_vcol - oap->start_vcol + 1;
2101 if (bd.is_short && (!virtual_op || bd.is_MAX))
2102 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2103
2104#ifdef FEAT_MBYTE
2105 /* A double-wide character can be replaced only up to half the
2106 * times. */
2107 if ((*mb_char2cells)(c) > 1)
2108 {
2109 if ((numc & 1) && !bd.is_short)
2110 {
2111 ++bd.endspaces;
2112 ++n;
2113 }
2114 numc = numc / 2;
2115 }
2116
2117 /* Compute bytes needed, move character count to num_chars. */
2118 num_chars = numc;
2119 numc *= (*mb_char2len)(c);
2120#endif
2121 /* oldlen includes textlen, so don't double count */
2122 n += numc - bd.textlen;
2123
2124 oldp = ml_get_curline();
2125 oldlen = STRLEN(oldp);
2126 newp = alloc_check((unsigned)oldlen + 1 + n);
2127 if (newp == NULL)
2128 continue;
2129 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2130 /* copy up to deleted part */
2131 mch_memmove(newp, oldp, (size_t)bd.textcol);
2132 oldp += bd.textcol + bd.textlen;
2133 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002134 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002136 /* -1/-2 is used for entering CR literally. */
2137 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002139#ifdef FEAT_MBYTE
2140 if (has_mbyte)
2141 {
2142 n = (int)STRLEN(newp);
2143 while (--num_chars >= 0)
2144 n += (*mb_char2bytes)(c, newp + n);
2145 }
2146 else
2147#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002148 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002149 if (!bd.is_short)
2150 {
2151 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002152 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002153 /* copy the part after the changed part */
2154 STRMOVE(newp + STRLEN(newp), oldp);
2155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
2157 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002159 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002160 after_p = alloc_check(
2161 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002162 if (after_p != NULL)
2163 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165 /* replace the line */
2166 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002167 if (after_p != NULL)
2168 {
2169 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2170 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2171 oap->end.lnum++;
2172 vim_free(after_p);
2173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175 }
2176 else
2177 {
2178 /*
2179 * MCHAR and MLINE motion replace.
2180 */
2181 if (oap->motion_type == MLINE)
2182 {
2183 oap->start.col = 0;
2184 curwin->w_cursor.col = 0;
2185 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2186 if (oap->end.col)
2187 --oap->end.col;
2188 }
2189 else if (!oap->inclusive)
2190 dec(&(oap->end));
2191
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002192 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 n = gchar_cursor();
2195 if (n != NUL)
2196 {
2197#ifdef FEAT_MBYTE
2198 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2199 {
2200 /* This is slow, but it handles replacing a single-byte
2201 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002202 if (curwin->w_cursor.lnum == oap->end.lnum)
2203 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 n = State;
2205 State = REPLACE;
2206 ins_char(c);
2207 State = n;
2208 /* Backup to the replaced character. */
2209 dec_cursor();
2210 }
2211 else
2212#endif
2213 {
2214#ifdef FEAT_VIRTUALEDIT
2215 if (n == TAB)
2216 {
2217 int end_vcol = 0;
2218
2219 if (curwin->w_cursor.lnum == oap->end.lnum)
2220 {
2221 /* oap->end has to be recalculated when
2222 * the tab breaks */
2223 end_vcol = getviscol2(oap->end.col,
2224 oap->end.coladd);
2225 }
2226 coladvance_force(getviscol());
2227 if (curwin->w_cursor.lnum == oap->end.lnum)
2228 getvpos(&oap->end, end_vcol);
2229 }
2230#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002231 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233 }
2234#ifdef FEAT_VIRTUALEDIT
2235 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2236 {
2237 int virtcols = oap->end.coladd;
2238
2239 if (curwin->w_cursor.lnum == oap->start.lnum
2240 && oap->start.col == oap->end.col && oap->start.coladd)
2241 virtcols -= oap->start.coladd;
2242
2243 /* oap->end has been trimmed so it's effectively inclusive;
2244 * as a result an extra +1 must be counted so we don't
2245 * trample the NUL byte. */
2246 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2247 curwin->w_cursor.col -= (virtcols + 1);
2248 for (; virtcols >= 0; virtcols--)
2249 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002250 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 if (inc(&curwin->w_cursor) == -1)
2252 break;
2253 }
2254 }
2255#endif
2256
2257 /* Advance to next character, stop at the end of the file. */
2258 if (inc_cursor() == -1)
2259 break;
2260 }
2261 }
2262
2263 curwin->w_cursor = oap->start;
2264 check_cursor();
2265 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2266
2267 /* Set "'[" and "']" marks. */
2268 curbuf->b_op_start = oap->start;
2269 curbuf->b_op_end = oap->end;
2270
2271 return OK;
2272}
2273#endif
2274
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002275static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277/*
2278 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2279 */
2280 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002281op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002285 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 if (u_save((linenr_T)(oap->start.lnum - 1),
2288 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2289 return;
2290
2291 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (oap->block_mode) /* Visual block mode */
2293 {
2294 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2295 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002296 int one_change;
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 block_prep(oap, &bd, pos.lnum, FALSE);
2299 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002300 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2301 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002302
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002303#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002304 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2307
Bram Moolenaar009b2592004-10-24 19:18:58 +00002308 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2309 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002311 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315 if (did_change)
2316 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2317 }
2318 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 if (oap->motion_type == MLINE)
2321 {
2322 oap->start.col = 0;
2323 pos.col = 0;
2324 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2325 if (oap->end.col)
2326 --oap->end.col;
2327 }
2328 else if (!oap->inclusive)
2329 dec(&(oap->end));
2330
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002331 if (pos.lnum == oap->end.lnum)
2332 did_change = swapchars(oap->op_type, &pos,
2333 oap->end.col - pos.col + 1);
2334 else
2335 for (;;)
2336 {
2337 did_change |= swapchars(oap->op_type, &pos,
2338 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2339 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002340 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002341 break;
2342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (did_change)
2344 {
2345 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2346 0L);
2347#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002348 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 char_u *ptr;
2351 int count;
2352
2353 pos = oap->start;
2354 while (pos.lnum < oap->end.lnum)
2355 {
2356 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002357 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002358 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002360 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 pos.col = 0;
2362 pos.lnum++;
2363 }
2364 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2365 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002366 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002368 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370#endif
2371 }
2372 }
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (!did_change && oap->is_VIsual)
2375 /* No change: need to remove the Visual selection */
2376 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 /*
2379 * Set '[ and '] marks.
2380 */
2381 curbuf->b_op_start = oap->start;
2382 curbuf->b_op_end = oap->end;
2383
2384 if (oap->line_count > p_report)
2385 {
2386 if (oap->line_count == 1)
2387 MSG(_("1 line changed"));
2388 else
2389 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2390 }
2391}
2392
2393/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002394 * Invoke swapchar() on "length" bytes at position "pos".
2395 * "pos" is advanced to just after the changed characters.
2396 * "length" is rounded up to include the whole last multi-byte character.
2397 * Also works correctly when the number of bytes changes.
2398 * Returns TRUE if some character was changed.
2399 */
2400 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002401swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002402{
2403 int todo;
2404 int did_change = 0;
2405
2406 for (todo = length; todo > 0; --todo)
2407 {
2408# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002409 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002410 {
2411 int len = (*mb_ptr2len)(ml_get_pos(pos));
2412
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002413 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002414 if (len > 0)
2415 todo -= len - 1;
2416 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002417# endif
2418 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002419 if (inc(pos) == -1) /* at end of file */
2420 break;
2421 }
2422 return did_change;
2423}
2424
2425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 * If op_type == OP_UPPER: make uppercase,
2427 * if op_type == OP_LOWER: make lowercase,
2428 * if op_type == OP_ROT13: do rot13 encoding,
2429 * else swap case of character at 'pos'
2430 * returns TRUE when something actually changed.
2431 */
2432 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002433swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434{
2435 int c;
2436 int nc;
2437
2438 c = gchar_pos(pos);
2439
2440 /* Only do rot13 encoding for ASCII characters. */
2441 if (c >= 0x80 && op_type == OP_ROT13)
2442 return FALSE;
2443
2444#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002445 if (op_type == OP_UPPER && c == 0xdf
2446 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002447 {
2448 pos_T sp = curwin->w_cursor;
2449
2450 /* Special handling of German sharp s: change to "SS". */
2451 curwin->w_cursor = *pos;
2452 del_char(FALSE);
2453 ins_char('S');
2454 ins_char('S');
2455 curwin->w_cursor = sp;
2456 inc(pos);
2457 }
2458
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2460 return FALSE;
2461#endif
2462 nc = c;
2463 if (MB_ISLOWER(c))
2464 {
2465 if (op_type == OP_ROT13)
2466 nc = ROT13(c, 'a');
2467 else if (op_type != OP_LOWER)
2468 nc = MB_TOUPPER(c);
2469 }
2470 else if (MB_ISUPPER(c))
2471 {
2472 if (op_type == OP_ROT13)
2473 nc = ROT13(c, 'A');
2474 else if (op_type != OP_UPPER)
2475 nc = MB_TOLOWER(c);
2476 }
2477 if (nc != c)
2478 {
2479#ifdef FEAT_MBYTE
2480 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2481 {
2482 pos_T sp = curwin->w_cursor;
2483
2484 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002485 /* don't use del_char(), it also removes composing chars */
2486 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 ins_char(nc);
2488 curwin->w_cursor = sp;
2489 }
2490 else
2491#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002492 PCHAR(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 return TRUE;
2494 }
2495 return FALSE;
2496}
2497
2498#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2499/*
2500 * op_insert - Insert and append operators for Visual mode.
2501 */
2502 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002503op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 long ins_len, pre_textlen = 0;
2506 char_u *firstline, *ins_text;
2507 struct block_def bd;
2508 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002509 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511 /* edit() changes this - record it for OP_APPEND */
2512 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2513
2514 /* vis block is still marked. Get rid of it now. */
2515 curwin->w_cursor.lnum = oap->start.lnum;
2516 update_screen(INVERTED);
2517
2518 if (oap->block_mode)
2519 {
2520#ifdef FEAT_VIRTUALEDIT
2521 /* When 'virtualedit' is used, need to insert the extra spaces before
2522 * doing block_prep(). When only "block" is used, virtual edit is
2523 * already disabled, but still need it when calling
2524 * coladvance_force(). */
2525 if (curwin->w_cursor.coladd > 0)
2526 {
2527 int old_ve_flags = ve_flags;
2528
2529 ve_flags = VE_ALL;
2530 if (u_save_cursor() == FAIL)
2531 return;
2532 coladvance_force(oap->op_type == OP_APPEND
2533 ? oap->end_vcol + 1 : getviscol());
2534 if (oap->op_type == OP_APPEND)
2535 --curwin->w_cursor.col;
2536 ve_flags = old_ve_flags;
2537 }
2538#endif
2539 /* Get the info about the block before entering the text */
2540 block_prep(oap, &bd, oap->start.lnum, TRUE);
2541 firstline = ml_get(oap->start.lnum) + bd.textcol;
2542 if (oap->op_type == OP_APPEND)
2543 firstline += bd.textlen;
2544 pre_textlen = (long)STRLEN(firstline);
2545 }
2546
2547 if (oap->op_type == OP_APPEND)
2548 {
2549 if (oap->block_mode
2550#ifdef FEAT_VIRTUALEDIT
2551 && curwin->w_cursor.coladd == 0
2552#endif
2553 )
2554 {
2555 /* Move the cursor to the character right of the block. */
2556 curwin->w_set_curswant = TRUE;
2557 while (*ml_get_cursor() != NUL
2558 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2559 ++curwin->w_cursor.col;
2560 if (bd.is_short && !bd.is_MAX)
2561 {
2562 /* First line was too short, make it longer and adjust the
2563 * values in "bd". */
2564 if (u_save_cursor() == FAIL)
2565 return;
2566 for (i = 0; i < bd.endspaces; ++i)
2567 ins_char(' ');
2568 bd.textlen += bd.endspaces;
2569 }
2570 }
2571 else
2572 {
2573 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002574 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002577 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 && oap->start_vcol != oap->end_vcol)
2579 inc_cursor();
2580 }
2581 }
2582
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002583 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002584 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002586 /* When a tab was inserted, and the characters in front of the tab
2587 * have been converted to a tab as well, the column of the cursor
2588 * might have actually been reduced, so need to adjust here. */
2589 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002590 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002591 oap->start = curbuf->b_op_start_orig;
2592
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002593 /* If user has moved off this line, we don't know what to do, so do
2594 * nothing.
2595 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2596 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return;
2598
2599 if (oap->block_mode)
2600 {
2601 struct block_def bd2;
2602
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002603 /* The user may have moved the cursor before inserting something, try
2604 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002605 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002606 {
2607 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002608 && oap->start.col
2609#ifdef FEAT_VIRTUALEDIT
2610 + oap->start.coladd
2611#endif
2612 != curbuf->b_op_start_orig.col
2613#ifdef FEAT_VIRTUALEDIT
2614 + curbuf->b_op_start_orig.coladd
2615#endif
2616 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002617 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002618 int t = getviscol2(curbuf->b_op_start_orig.col,
2619 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002620 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002621 pre_textlen -= t - oap->start_vcol;
2622 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002623 }
2624 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002625 && oap->end.col
2626#ifdef FEAT_VIRTUALEDIT
2627 + oap->end.coladd
2628#endif
2629 >= curbuf->b_op_start_orig.col
2630#ifdef FEAT_VIRTUALEDIT
2631 + curbuf->b_op_start_orig.coladd
2632#endif
2633 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002634 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002635 int t = getviscol2(curbuf->b_op_start_orig.col,
2636 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002637 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002638 /* reset pre_textlen to the value of OP_INSERT */
2639 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002640 pre_textlen -= t - oap->start_vcol;
2641 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002642 oap->op_type = OP_INSERT;
2643 }
2644 }
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 /*
2647 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002648 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 * Don't do this when "$" used, end-of-line will have changed.
2650 */
2651 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2652 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2653 {
2654 if (oap->op_type == OP_APPEND)
2655 {
2656 pre_textlen += bd2.textlen - bd.textlen;
2657 if (bd2.endspaces)
2658 --bd2.textlen;
2659 }
2660 bd.textcol = bd2.textcol;
2661 bd.textlen = bd2.textlen;
2662 }
2663
2664 /*
2665 * Subsequent calls to ml_get() flush the firstline data - take a
2666 * copy of the required string.
2667 */
2668 firstline = ml_get(oap->start.lnum) + bd.textcol;
2669 if (oap->op_type == OP_APPEND)
2670 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002671 if (pre_textlen >= 0
2672 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 ins_text = vim_strnsave(firstline, (int)ins_len);
2675 if (ins_text != NULL)
2676 {
2677 /* block handled here */
2678 if (u_save(oap->start.lnum,
2679 (linenr_T)(oap->end.lnum + 1)) == OK)
2680 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2681 &bd);
2682
2683 curwin->w_cursor.col = oap->start.col;
2684 check_cursor();
2685 vim_free(ins_text);
2686 }
2687 }
2688 }
2689}
2690#endif
2691
2692/*
2693 * op_change - handle a change operation
2694 *
2695 * return TRUE if edit() returns because of a CTRL-O command
2696 */
2697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002698op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
2700 colnr_T l;
2701 int retval;
2702#ifdef FEAT_VISUALEXTRA
2703 long offset;
2704 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002705 long ins_len;
2706 long pre_textlen = 0;
2707 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 char_u *firstline;
2709 char_u *ins_text, *newp, *oldp;
2710 struct block_def bd;
2711#endif
2712
2713 l = oap->start.col;
2714 if (oap->motion_type == MLINE)
2715 {
2716 l = 0;
2717#ifdef FEAT_SMARTINDENT
2718 if (!p_paste && curbuf->b_p_si
2719# ifdef FEAT_CINDENT
2720 && !curbuf->b_p_cin
2721# endif
2722 )
2723 can_si = TRUE; /* It's like opening a new line, do si */
2724#endif
2725 }
2726
2727 /* First delete the text in the region. In an empty buffer only need to
2728 * save for undo */
2729 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2730 {
2731 if (u_save_cursor() == FAIL)
2732 return FALSE;
2733 }
2734 else if (op_delete(oap) == FAIL)
2735 return FALSE;
2736
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002737 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 && !virtual_op)
2739 inc_cursor();
2740
2741#ifdef FEAT_VISUALEXTRA
2742 /* check for still on same line (<CR> in inserted text meaningless) */
2743 /* skip blank lines too */
2744 if (oap->block_mode)
2745 {
2746# ifdef FEAT_VIRTUALEDIT
2747 /* Add spaces before getting the current line length. */
2748 if (virtual_op && (curwin->w_cursor.coladd > 0
2749 || gchar_cursor() == NUL))
2750 coladvance_force(getviscol());
2751# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002752 firstline = ml_get(oap->start.lnum);
2753 pre_textlen = (long)STRLEN(firstline);
2754 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 bd.textcol = curwin->w_cursor.col;
2756 }
2757#endif
2758
2759#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2760 if (oap->motion_type == MLINE)
2761 fix_indent();
2762#endif
2763
2764 retval = edit(NUL, FALSE, (linenr_T)1);
2765
2766#ifdef FEAT_VISUALEXTRA
2767 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002768 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002770 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002772 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002774 /* Auto-indenting may have changed the indent. If the cursor was past
2775 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002777 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002779 long new_indent = (long)(skipwhite(firstline) - firstline);
2780
2781 pre_textlen += new_indent - pre_indent;
2782 bd.textcol += new_indent - pre_indent;
2783 }
2784
2785 ins_len = (long)STRLEN(firstline) - pre_textlen;
2786 if (ins_len > 0)
2787 {
2788 /* Subsequent calls to ml_get() flush the firstline data - take a
2789 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2791 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002792 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2794 linenr++)
2795 {
2796 block_prep(oap, &bd, linenr, TRUE);
2797 if (!bd.is_short || virtual_op)
2798 {
2799# ifdef FEAT_VIRTUALEDIT
2800 pos_T vpos;
2801
2802 /* If the block starts in virtual space, count the
2803 * initial coladd offset as part of "startspaces" */
2804 if (bd.is_short)
2805 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002806 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809 else
2810 vpos.coladd = 0;
2811# endif
2812 oldp = ml_get(linenr);
2813 newp = alloc_check((unsigned)(STRLEN(oldp)
2814# ifdef FEAT_VIRTUALEDIT
2815 + vpos.coladd
2816# endif
2817 + ins_len + 1));
2818 if (newp == NULL)
2819 continue;
2820 /* copy up to block start */
2821 mch_memmove(newp, oldp, (size_t)bd.textcol);
2822 offset = bd.textcol;
2823# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002824 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 offset += vpos.coladd;
2826# endif
2827 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2828 offset += ins_len;
2829 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002830 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 ml_replace(linenr, newp, FALSE);
2832 }
2833 }
2834 check_cursor();
2835
2836 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2837 }
2838 vim_free(ins_text);
2839 }
2840 }
2841#endif
2842
2843 return retval;
2844}
2845
2846/*
2847 * set all the yank registers to empty (called from main())
2848 */
2849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002850init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
2852 int i;
2853
2854 for (i = 0; i < NUM_REGISTERS; ++i)
2855 y_regs[i].y_array = NULL;
2856}
2857
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002858#if defined(EXITFREE) || defined(PROTO)
2859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002860clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002861{
2862 int i;
2863
2864 for (i = 0; i < NUM_REGISTERS; ++i)
2865 {
2866 y_current = &y_regs[i];
2867 if (y_current->y_array != NULL)
2868 free_yank_all();
2869 }
2870}
2871#endif
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873/*
2874 * Free "n" lines from the current yank register.
2875 * Called for normal freeing and in case of error.
2876 */
2877 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 if (y_current->y_array != NULL)
2881 {
2882 long i;
2883
2884 for (i = n; --i >= 0; )
2885 {
2886#ifdef AMIGA /* only for very slow machines */
2887 if ((i & 1023) == 1023) /* this may take a while */
2888 {
2889 /*
2890 * This message should never cause a hit-return message.
2891 * Overwrite this message with any next message.
2892 */
2893 ++no_wait_return;
2894 smsg((char_u *)_("freeing %ld lines"), i + 1);
2895 --no_wait_return;
2896 msg_didout = FALSE;
2897 msg_col = 0;
2898 }
2899#endif
2900 vim_free(y_current->y_array[i]);
2901 }
2902 vim_free(y_current->y_array);
2903 y_current->y_array = NULL;
2904#ifdef AMIGA
2905 if (n >= 1000)
2906 MSG("");
2907#endif
2908 }
2909}
2910
2911 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002912free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 free_yank(y_current->y_size);
2915}
2916
2917/*
2918 * Yank the text between "oap->start" and "oap->end" into a yank register.
2919 * If we are to append (uppercase register), we first yank into a new yank
2920 * register and then concatenate the old and the new one (so we keep the old
2921 * one in case of out-of-memory).
2922 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002923 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 */
2925 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002926op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002929 yankreg_T *curr; /* copy of y_current */
2930 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 char_u **new_ptr;
2932 linenr_T lnum; /* current line number */
2933 long j;
2934 int yanktype = oap->motion_type;
2935 long yanklines = oap->line_count;
2936 linenr_T yankendlnum = oap->end.lnum;
2937 char_u *p;
2938 char_u *pnew;
2939 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002940#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002941 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 /* check for read-only register */
2945 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2946 {
2947 beep_flush();
2948 return FAIL;
2949 }
2950 if (oap->regname == '_') /* black hole: nothing to do */
2951 return OK;
2952
2953#ifdef FEAT_CLIPBOARD
2954 if (!clip_star.available && oap->regname == '*')
2955 oap->regname = 0;
2956 else if (!clip_plus.available && oap->regname == '+')
2957 oap->regname = 0;
2958#endif
2959
2960 if (!deleting) /* op_delete() already set y_current */
2961 get_yank_register(oap->regname, TRUE);
2962
2963 curr = y_current;
2964 /* append to existing contents */
2965 if (y_append && y_current->y_array != NULL)
2966 y_current = &newreg;
2967 else
2968 free_yank_all(); /* free previously yanked lines */
2969
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002970 /*
2971 * If the cursor was in column 1 before and after the movement, and the
2972 * operator is not inclusive, the yank is always linewise.
2973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if ( oap->motion_type == MCHAR
2975 && oap->start.col == 0
2976 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002978 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 && oap->end.col == 0
2980 && yanklines > 1)
2981 {
2982 yanktype = MLINE;
2983 --yankendlnum;
2984 --yanklines;
2985 }
2986
2987 y_current->y_size = yanklines;
2988 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2991 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (y_current->y_array == NULL)
2993 {
2994 y_current = curr;
2995 return FAIL;
2996 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002997#ifdef FEAT_VIMINFO
2998 y_current->y_time_set = vim_time();
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 y_idx = 0;
3002 lnum = oap->start.lnum;
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 if (oap->block_mode)
3005 {
3006 /* Visual block mode */
3007 y_current->y_type = MBLOCK; /* set the yank register type */
3008 y_current->y_width = oap->end_vcol - oap->start_vcol;
3009
3010 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3011 y_current->y_width--;
3012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
3014 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3015 {
3016 switch (y_current->y_type)
3017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 case MBLOCK:
3019 block_prep(oap, &bd, lnum, FALSE);
3020 if (yank_copy_line(&bd, y_idx) == FAIL)
3021 goto fail;
3022 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024 case MLINE:
3025 if ((y_current->y_array[y_idx] =
3026 vim_strsave(ml_get(lnum))) == NULL)
3027 goto fail;
3028 break;
3029
3030 case MCHAR:
3031 {
3032 colnr_T startcol = 0, endcol = MAXCOL;
3033#ifdef FEAT_VIRTUALEDIT
3034 int is_oneChar = FALSE;
3035 colnr_T cs, ce;
3036#endif
3037 p = ml_get(lnum);
3038 bd.startspaces = 0;
3039 bd.endspaces = 0;
3040
3041 if (lnum == oap->start.lnum)
3042 {
3043 startcol = oap->start.col;
3044#ifdef FEAT_VIRTUALEDIT
3045 if (virtual_op)
3046 {
3047 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3048 if (ce != cs && oap->start.coladd > 0)
3049 {
3050 /* Part of a tab selected -- but don't
3051 * double-count it. */
3052 bd.startspaces = (ce - cs + 1)
3053 - oap->start.coladd;
3054 startcol++;
3055 }
3056 }
3057#endif
3058 }
3059
3060 if (lnum == oap->end.lnum)
3061 {
3062 endcol = oap->end.col;
3063#ifdef FEAT_VIRTUALEDIT
3064 if (virtual_op)
3065 {
3066 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3067 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3068# ifdef FEAT_MBYTE
3069 /* Don't add space for double-wide
3070 * char; endcol will be on last byte
3071 * of multi-byte char. */
3072 && (*mb_head_off)(p, p + endcol) == 0
3073# endif
3074 ))
3075 {
3076 if (oap->start.lnum == oap->end.lnum
3077 && oap->start.col == oap->end.col)
3078 {
3079 /* Special case: inside a single char */
3080 is_oneChar = TRUE;
3081 bd.startspaces = oap->end.coladd
3082 - oap->start.coladd + oap->inclusive;
3083 endcol = startcol;
3084 }
3085 else
3086 {
3087 bd.endspaces = oap->end.coladd
3088 + oap->inclusive;
3089 endcol -= oap->inclusive;
3090 }
3091 }
3092 }
3093#endif
3094 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003095 if (endcol == MAXCOL)
3096 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (startcol > endcol
3098#ifdef FEAT_VIRTUALEDIT
3099 || is_oneChar
3100#endif
3101 )
3102 bd.textlen = 0;
3103 else
3104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 bd.textlen = endcol - startcol + oap->inclusive;
3106 }
3107 bd.textstart = p + startcol;
3108 if (yank_copy_line(&bd, y_idx) == FAIL)
3109 goto fail;
3110 break;
3111 }
3112 /* NOTREACHED */
3113 }
3114 }
3115
3116 if (curr != y_current) /* append the new block to the old block */
3117 {
3118 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3119 (curr->y_size + y_current->y_size)), TRUE);
3120 if (new_ptr == NULL)
3121 goto fail;
3122 for (j = 0; j < curr->y_size; ++j)
3123 new_ptr[j] = curr->y_array[j];
3124 vim_free(curr->y_array);
3125 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003126#ifdef FEAT_VIMINFO
3127 curr->y_time_set = vim_time();
3128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
3130 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3131 curr->y_type = MLINE;
3132
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003133 /* Concatenate the last line of the old block with the first line of
3134 * the new block, unless being Vi compatible. */
3135 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
3137 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3138 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3139 if (pnew == NULL)
3140 {
3141 y_idx = y_current->y_size - 1;
3142 goto fail;
3143 }
3144 STRCPY(pnew, curr->y_array[--j]);
3145 STRCAT(pnew, y_current->y_array[0]);
3146 vim_free(curr->y_array[j]);
3147 vim_free(y_current->y_array[0]);
3148 curr->y_array[j++] = pnew;
3149 y_idx = 1;
3150 }
3151 else
3152 y_idx = 0;
3153 while (y_idx < y_current->y_size)
3154 curr->y_array[j++] = y_current->y_array[y_idx++];
3155 curr->y_size = j;
3156 vim_free(y_current->y_array);
3157 y_current = curr;
3158 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003159 if (curwin->w_p_rnu)
3160 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (mess) /* Display message about yank? */
3162 {
3163 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 && yanklines == 1)
3166 yanklines = 0;
3167 /* Some versions of Vi use ">=" here, some don't... */
3168 if (yanklines > p_report)
3169 {
3170 /* redisplay now, so message is not deleted */
3171 update_topline_redraw();
3172 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003173 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003174 if (oap->block_mode)
3175 MSG(_("block of 1 line yanked"));
3176 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003177 MSG(_("1 line yanked"));
3178 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003179 else if (oap->block_mode)
3180 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 else
3182 smsg((char_u *)_("%ld lines yanked"), yanklines);
3183 }
3184 }
3185
3186 /*
3187 * Set "'[" and "']" marks.
3188 */
3189 curbuf->b_op_start = oap->start;
3190 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003191 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003192 {
3193 curbuf->b_op_start.col = 0;
3194 curbuf->b_op_end.col = MAXCOL;
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197#ifdef FEAT_CLIPBOARD
3198 /*
3199 * If we were yanking to the '*' register, send result to clipboard.
3200 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3201 * to the '*' register.
3202 */
3203 if (clip_star.available
3204 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003205 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003206 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 if (curr != &(y_regs[STAR_REGISTER]))
3209 /* Copy the text from register 0 to the clipboard register. */
3210 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3211
3212 clip_own_selection(&clip_star);
3213 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003214# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003215 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218
3219# ifdef FEAT_X11
3220 /*
3221 * If we were yanking to the '+' register, send result to selection.
3222 * Also copy to the '*' register, in case auto-select is off.
3223 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003224 if (clip_plus.available
3225 && (curr == &(y_regs[PLUS_REGISTER])
3226 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003227 && ((clip_unnamed | clip_unnamed_saved) &
3228 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003230 if (curr != &(y_regs[PLUS_REGISTER]))
3231 /* Copy the text from register 0 to the clipboard register. */
3232 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 clip_own_selection(&clip_plus);
3235 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003236 if (!clip_isautosel_star() && !did_star
3237 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3240 clip_own_selection(&clip_star);
3241 clip_gen_set_selection(&clip_star);
3242 }
3243 }
3244# endif
3245#endif
3246
3247 return OK;
3248
3249fail: /* free the allocated lines */
3250 free_yank(y_idx + 1);
3251 y_current = curr;
3252 return FAIL;
3253}
3254
3255 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003256yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 char_u *pnew;
3259
3260 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3261 == NULL)
3262 return FAIL;
3263 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003264 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 pnew += bd->startspaces;
3266 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3267 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003268 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 pnew += bd->endspaces;
3270 *pnew = NUL;
3271 return OK;
3272}
3273
3274#ifdef FEAT_CLIPBOARD
3275/*
3276 * Make a copy of the y_current register to register "reg".
3277 */
3278 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003279copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003281 yankreg_T *curr = y_current;
3282 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 y_current = reg;
3285 free_yank_all();
3286 *y_current = *curr;
3287 y_current->y_array = (char_u **)lalloc_clear(
3288 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3289 if (y_current->y_array == NULL)
3290 y_current->y_size = 0;
3291 else
3292 for (j = 0; j < y_current->y_size; ++j)
3293 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3294 {
3295 free_yank(j);
3296 y_current->y_size = 0;
3297 break;
3298 }
3299 y_current = curr;
3300}
3301#endif
3302
3303/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003304 * Put contents of register "regname" into the text.
3305 * Caller must check "regname" to be valid!
3306 * "flags": PUT_FIXINDENT make indent look nice
3307 * PUT_CURSEND leave cursor after end of new text
3308 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
3310 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003311do_put(
3312 int regname,
3313 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3314 long count,
3315 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 char_u *ptr;
3318 char_u *newp, *oldp;
3319 int yanklen;
3320 int totlen = 0; /* init for gcc */
3321 linenr_T lnum;
3322 colnr_T col;
3323 long i; /* index in y_array[] */
3324 int y_type;
3325 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 int oldlen;
3327 long y_width = 0;
3328 colnr_T vcol;
3329 int delcount;
3330 int incr = 0;
3331 long j;
3332 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 char_u **y_array = NULL;
3334 long nr_lines = 0;
3335 pos_T new_cursor;
3336 int indent;
3337 int orig_indent = 0; /* init for gcc */
3338 int indent_diff = 0; /* init for gcc */
3339 int first_indent = TRUE;
3340 int lendiff = 0;
3341 pos_T old_pos;
3342 char_u *insert_string = NULL;
3343 int allocated = FALSE;
3344 long cnt;
3345
3346#ifdef FEAT_CLIPBOARD
3347 /* Adjust register name for "unnamed" in 'clipboard'. */
3348 adjust_clip_reg(&regname);
3349 (void)may_get_selection(regname);
3350#endif
3351
3352 if (flags & PUT_FIXINDENT)
3353 orig_indent = get_indent();
3354
3355 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3356 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3357
3358 /*
3359 * Using inserted text works differently, because the register includes
3360 * special characters (newlines, etc.).
3361 */
3362 if (regname == '.')
3363 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003364 if (VIsual_active)
3365 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3367 (count == -1 ? 'O' : 'i')), count, FALSE);
3368 /* Putting the text is done later, so can't really move the cursor to
3369 * the next character. Use "l" to simulate it. */
3370 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3371 stuffcharReadbuff('l');
3372 return;
3373 }
3374
3375 /*
3376 * For special registers '%' (file name), '#' (alternate file name) and
3377 * ':' (last command line), etc. we have to create a fake yank register.
3378 */
3379 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3380 {
3381 if (insert_string == NULL)
3382 return;
3383 }
3384
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003385#ifdef FEAT_AUTOCMD
3386 /* Autocommands may be executed when saving lines for undo, which may make
3387 * y_array invalid. Start undo now to avoid that. */
3388 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3389#endif
3390
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (insert_string != NULL)
3392 {
3393 y_type = MCHAR;
3394#ifdef FEAT_EVAL
3395 if (regname == '=')
3396 {
3397 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003398 * characters.
3399 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 for (;;)
3401 {
3402 y_size = 0;
3403 ptr = insert_string;
3404 while (ptr != NULL)
3405 {
3406 if (y_array != NULL)
3407 y_array[y_size] = ptr;
3408 ++y_size;
3409 ptr = vim_strchr(ptr, '\n');
3410 if (ptr != NULL)
3411 {
3412 if (y_array != NULL)
3413 *ptr = NUL;
3414 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003415 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (*ptr == NUL)
3417 {
3418 y_type = MLINE;
3419 break;
3420 }
3421 }
3422 }
3423 if (y_array != NULL)
3424 break;
3425 y_array = (char_u **)alloc((unsigned)
3426 (y_size * sizeof(char_u *)));
3427 if (y_array == NULL)
3428 goto end;
3429 }
3430 }
3431 else
3432#endif
3433 {
3434 y_size = 1; /* use fake one-line yank register */
3435 y_array = &insert_string;
3436 }
3437 }
3438 else
3439 {
3440 get_yank_register(regname, FALSE);
3441
3442 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 y_size = y_current->y_size;
3445 y_array = y_current->y_array;
3446 }
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (y_type == MLINE)
3449 {
3450 if (flags & PUT_LINE_SPLIT)
3451 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003452 char_u *p;
3453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 /* "p" or "P" in Visual mode: split the lines to put the text in
3455 * between. */
3456 if (u_save_cursor() == FAIL)
3457 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003458 p = ml_get_cursor();
3459 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003460 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003461 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (ptr == NULL)
3463 goto end;
3464 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3465 vim_free(ptr);
3466
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003467 oldp = ml_get_curline();
3468 p = oldp + curwin->w_cursor.col;
3469 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003470 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003471 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (ptr == NULL)
3473 goto end;
3474 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3475 ++nr_lines;
3476 dir = FORWARD;
3477 }
3478 if (flags & PUT_LINE_FORWARD)
3479 {
3480 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003481 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 dir = FORWARD;
3483 }
3484 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3485 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3489 y_type = MLINE;
3490
3491 if (y_size == 0 || y_array == NULL)
3492 {
3493 EMSG2(_("E353: Nothing in register %s"),
3494 regname == 0 ? (char_u *)"\"" : transchar(regname));
3495 goto end;
3496 }
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (y_type == MBLOCK)
3499 {
3500 lnum = curwin->w_cursor.lnum + y_size + 1;
3501 if (lnum > curbuf->b_ml.ml_line_count)
3502 lnum = curbuf->b_ml.ml_line_count + 1;
3503 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3504 goto end;
3505 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003506 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 lnum = curwin->w_cursor.lnum;
3509#ifdef FEAT_FOLDING
3510 /* Correct line number for closed fold. Don't move the cursor yet,
3511 * u_save() uses it. */
3512 if (dir == BACKWARD)
3513 (void)hasFolding(lnum, &lnum, NULL);
3514 else
3515 (void)hasFolding(lnum, NULL, &lnum);
3516#endif
3517 if (dir == FORWARD)
3518 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003519 /* In an empty buffer the empty line is going to be replaced, include
3520 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003521 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 goto end;
3523#ifdef FEAT_FOLDING
3524 if (dir == FORWARD)
3525 curwin->w_cursor.lnum = lnum - 1;
3526 else
3527 curwin->w_cursor.lnum = lnum;
3528 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3529#endif
3530 }
3531 else if (u_save_cursor() == FAIL)
3532 goto end;
3533
3534 yanklen = (int)STRLEN(y_array[0]);
3535
3536#ifdef FEAT_VIRTUALEDIT
3537 if (ve_flags == VE_ALL && y_type == MCHAR)
3538 {
3539 if (gchar_cursor() == TAB)
3540 {
3541 /* Don't need to insert spaces when "p" on the last position of a
3542 * tab or "P" on the first position. */
3543 if (dir == FORWARD
3544 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3545 : curwin->w_cursor.coladd > 0)
3546 coladvance_force(getviscol());
3547 else
3548 curwin->w_cursor.coladd = 0;
3549 }
3550 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3551 coladvance_force(getviscol() + (dir == FORWARD));
3552 }
3553#endif
3554
3555 lnum = curwin->w_cursor.lnum;
3556 col = curwin->w_cursor.col;
3557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 /*
3559 * Block mode
3560 */
3561 if (y_type == MBLOCK)
3562 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003563 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 colnr_T endcol2 = 0;
3565
3566 if (dir == FORWARD && c != NUL)
3567 {
3568#ifdef FEAT_VIRTUALEDIT
3569 if (ve_flags == VE_ALL)
3570 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3571 else
3572#endif
3573 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3574
3575#ifdef FEAT_MBYTE
3576 if (has_mbyte)
3577 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003578 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 else
3580#endif
3581#ifdef FEAT_VIRTUALEDIT
3582 if (c != TAB || ve_flags != VE_ALL)
3583#endif
3584 ++curwin->w_cursor.col;
3585 ++col;
3586 }
3587 else
3588 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3589
3590#ifdef FEAT_VIRTUALEDIT
3591 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003592 if (ve_flags == VE_ALL
3593 && (curwin->w_cursor.coladd > 0
3594 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
3596 if (dir == FORWARD && c == NUL)
3597 ++col;
3598 if (dir != FORWARD && c != NUL)
3599 ++curwin->w_cursor.col;
3600 if (c == TAB)
3601 {
3602 if (dir == BACKWARD && curwin->w_cursor.col)
3603 curwin->w_cursor.col--;
3604 if (dir == FORWARD && col - 1 == endcol2)
3605 curwin->w_cursor.col++;
3606 }
3607 }
3608 curwin->w_cursor.coladd = 0;
3609#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003610 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 for (i = 0; i < y_size; ++i)
3612 {
3613 int spaces;
3614 char shortline;
3615
3616 bd.startspaces = 0;
3617 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 vcol = 0;
3619 delcount = 0;
3620
3621 /* add a new line */
3622 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3623 {
3624 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3625 (colnr_T)1, FALSE) == FAIL)
3626 break;
3627 ++nr_lines;
3628 }
3629 /* get the old line and advance to the position to insert at */
3630 oldp = ml_get_curline();
3631 oldlen = (int)STRLEN(oldp);
3632 for (ptr = oldp; vcol < col && *ptr; )
3633 {
3634 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003635 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 vcol += incr;
3637 }
3638 bd.textcol = (colnr_T)(ptr - oldp);
3639
3640 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3641
3642 if (vcol < col) /* line too short, padd with spaces */
3643 bd.startspaces = col - vcol;
3644 else if (vcol > col)
3645 {
3646 bd.endspaces = vcol - col;
3647 bd.startspaces = incr - bd.endspaces;
3648 --bd.textcol;
3649 delcount = 1;
3650#ifdef FEAT_MBYTE
3651 if (has_mbyte)
3652 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3653#endif
3654 if (oldp[bd.textcol] != TAB)
3655 {
3656 /* Only a Tab can be split into spaces. Other
3657 * characters will have to be moved to after the
3658 * block, causing misalignment. */
3659 delcount = 0;
3660 bd.endspaces = 0;
3661 }
3662 }
3663
3664 yanklen = (int)STRLEN(y_array[i]);
3665
3666 /* calculate number of spaces required to fill right side of block*/
3667 spaces = y_width + 1;
3668 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003669 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (spaces < 0)
3671 spaces = 0;
3672
3673 /* insert the new text */
3674 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3675 newp = alloc_check((unsigned)totlen + oldlen + 1);
3676 if (newp == NULL)
3677 break;
3678 /* copy part up to cursor to new line */
3679 ptr = newp;
3680 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3681 ptr += bd.textcol;
3682 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003683 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 ptr += bd.startspaces;
3685 /* insert the new text */
3686 for (j = 0; j < count; ++j)
3687 {
3688 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3689 ptr += yanklen;
3690
3691 /* insert block's trailing spaces only if there's text behind */
3692 if ((j < count - 1 || !shortline) && spaces)
3693 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003694 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 ptr += spaces;
3696 }
3697 }
3698 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003699 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 ptr += bd.endspaces;
3701 /* move the text after the cursor to the end of the line. */
3702 mch_memmove(ptr, oldp + bd.textcol + delcount,
3703 (size_t)(oldlen - bd.textcol - delcount + 1));
3704 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3705
3706 ++curwin->w_cursor.lnum;
3707 if (i == 0)
3708 curwin->w_cursor.col += bd.startspaces;
3709 }
3710
3711 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3712
3713 /* Set '[ mark. */
3714 curbuf->b_op_start = curwin->w_cursor;
3715 curbuf->b_op_start.lnum = lnum;
3716
3717 /* adjust '] mark */
3718 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3719 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003720# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (flags & PUT_CURSEND)
3724 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003725 colnr_T len;
3726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 curwin->w_cursor = curbuf->b_op_end;
3728 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003729
3730 /* in Insert mode we might be after the NUL, correct for that */
3731 len = (colnr_T)STRLEN(ml_get_curline());
3732 if (curwin->w_cursor.col > len)
3733 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735 else
3736 curwin->w_cursor.lnum = lnum;
3737 }
3738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
3740 /*
3741 * Character or Line mode
3742 */
3743 if (y_type == MCHAR)
3744 {
3745 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3746 * char */
3747 if (dir == FORWARD && gchar_cursor() != NUL)
3748 {
3749#ifdef FEAT_MBYTE
3750 if (has_mbyte)
3751 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003752 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
3754 /* put it on the next of the multi-byte character. */
3755 col += bytelen;
3756 if (yanklen)
3757 {
3758 curwin->w_cursor.col += bytelen;
3759 curbuf->b_op_end.col += bytelen;
3760 }
3761 }
3762 else
3763#endif
3764 {
3765 ++col;
3766 if (yanklen)
3767 {
3768 ++curwin->w_cursor.col;
3769 ++curbuf->b_op_end.col;
3770 }
3771 }
3772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 curbuf->b_op_start = curwin->w_cursor;
3774 }
3775 /*
3776 * Line mode: BACKWARD is the same as FORWARD on the previous line
3777 */
3778 else if (dir == BACKWARD)
3779 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003780 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /*
3783 * simple case: insert into current line
3784 */
3785 if (y_type == MCHAR && y_size == 1)
3786 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003787 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003788
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003789 if (VIsual_active)
3790 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003791 end_lnum = curbuf->b_visual.vi_end.lnum;
3792 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3793 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003794 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003795
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003796 do {
3797 totlen = count * yanklen;
3798 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003800 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003801 if (VIsual_active && col > (int)STRLEN(oldp))
3802 {
3803 lnum++;
3804 continue;
3805 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003806 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3807 if (newp == NULL)
3808 goto end; /* alloc() gave an error message */
3809 mch_memmove(newp, oldp, (size_t)col);
3810 ptr = newp + col;
3811 for (i = 0; i < count; ++i)
3812 {
3813 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3814 ptr += yanklen;
3815 }
3816 STRMOVE(ptr, oldp + col);
3817 ml_replace(lnum, newp, FALSE);
3818 /* Place cursor on last putted char. */
3819 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003820 {
3821 /* make sure curwin->w_virtcol is updated */
3822 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003823 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003826 if (VIsual_active)
3827 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003828 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003829
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003830 if (VIsual_active) /* reset lnum to the last visual line */
3831 lnum--;
3832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 curbuf->b_op_end = curwin->w_cursor;
3834 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3835 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3836 ++curwin->w_cursor.col;
3837 changed_bytes(lnum, col);
3838 }
3839 else
3840 {
3841 /*
3842 * Insert at least one line. When y_type is MCHAR, break the first
3843 * line in two.
3844 */
3845 for (cnt = 1; cnt <= count; ++cnt)
3846 {
3847 i = 0;
3848 if (y_type == MCHAR)
3849 {
3850 /*
3851 * Split the current line in two at the insert position.
3852 * First insert y_array[size - 1] in front of second line.
3853 * Then append y_array[0] to first line.
3854 */
3855 lnum = new_cursor.lnum;
3856 ptr = ml_get(lnum) + col;
3857 totlen = (int)STRLEN(y_array[y_size - 1]);
3858 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3859 if (newp == NULL)
3860 goto error;
3861 STRCPY(newp, y_array[y_size - 1]);
3862 STRCAT(newp, ptr);
3863 /* insert second line */
3864 ml_append(lnum, newp, (colnr_T)0, FALSE);
3865 vim_free(newp);
3866
3867 oldp = ml_get(lnum);
3868 newp = alloc_check((unsigned)(col + yanklen + 1));
3869 if (newp == NULL)
3870 goto error;
3871 /* copy first part of line */
3872 mch_memmove(newp, oldp, (size_t)col);
3873 /* append to first line */
3874 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3875 ml_replace(lnum, newp, FALSE);
3876
3877 curwin->w_cursor.lnum = lnum;
3878 i = 1;
3879 }
3880
3881 for (; i < y_size; ++i)
3882 {
3883 if ((y_type != MCHAR || i < y_size - 1)
3884 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3885 == FAIL)
3886 goto error;
3887 lnum++;
3888 ++nr_lines;
3889 if (flags & PUT_FIXINDENT)
3890 {
3891 old_pos = curwin->w_cursor;
3892 curwin->w_cursor.lnum = lnum;
3893 ptr = ml_get(lnum);
3894 if (cnt == count && i == y_size - 1)
3895 lendiff = (int)STRLEN(ptr);
3896#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3897 if (*ptr == '#' && preprocs_left())
3898 indent = 0; /* Leave # lines at start */
3899 else
3900#endif
3901 if (*ptr == NUL)
3902 indent = 0; /* Ignore empty lines */
3903 else if (first_indent)
3904 {
3905 indent_diff = orig_indent - get_indent();
3906 indent = orig_indent;
3907 first_indent = FALSE;
3908 }
3909 else if ((indent = get_indent() + indent_diff) < 0)
3910 indent = 0;
3911 (void)set_indent(indent, 0);
3912 curwin->w_cursor = old_pos;
3913 /* remember how many chars were removed */
3914 if (cnt == count && i == y_size - 1)
3915 lendiff -= (int)STRLEN(ml_get(lnum));
3916 }
3917 }
3918 }
3919
3920error:
3921 /* Adjust marks. */
3922 if (y_type == MLINE)
3923 {
3924 curbuf->b_op_start.col = 0;
3925 if (dir == FORWARD)
3926 curbuf->b_op_start.lnum++;
3927 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003928 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003929 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02003930 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003931 < curbuf->b_ml.ml_line_count
3932#ifdef FEAT_DIFF
3933 || curwin->w_p_diff
3934#endif
3935 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003936 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 (linenr_T)MAXLNUM, nr_lines, 0L);
3938
3939 /* note changed text for displaying and folding */
3940 if (y_type == MCHAR)
3941 changed_lines(curwin->w_cursor.lnum, col,
3942 curwin->w_cursor.lnum + 1, nr_lines);
3943 else
3944 changed_lines(curbuf->b_op_start.lnum, 0,
3945 curbuf->b_op_start.lnum, nr_lines);
3946
3947 /* put '] mark at last inserted character */
3948 curbuf->b_op_end.lnum = lnum;
3949 /* correct length for change in indent */
3950 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3951 if (col > 1)
3952 curbuf->b_op_end.col = col - 1;
3953 else
3954 curbuf->b_op_end.col = 0;
3955
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003956 if (flags & PUT_CURSLINE)
3957 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003958 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003959 curwin->w_cursor.lnum = lnum;
3960 beginline(BL_WHITE | BL_FIX);
3961 }
3962 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
3964 /* put cursor after inserted text */
3965 if (y_type == MLINE)
3966 {
3967 if (lnum >= curbuf->b_ml.ml_line_count)
3968 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3969 else
3970 curwin->w_cursor.lnum = lnum + 1;
3971 curwin->w_cursor.col = 0;
3972 }
3973 else
3974 {
3975 curwin->w_cursor.lnum = lnum;
3976 curwin->w_cursor.col = col;
3977 }
3978 }
3979 else if (y_type == MLINE)
3980 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003981 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 curwin->w_cursor.col = 0;
3983 if (dir == FORWARD)
3984 ++curwin->w_cursor.lnum;
3985 beginline(BL_WHITE | BL_FIX);
3986 }
3987 else /* put cursor on first inserted character */
3988 curwin->w_cursor = new_cursor;
3989 }
3990 }
3991
3992 msgmore(nr_lines);
3993 curwin->w_set_curswant = TRUE;
3994
3995end:
3996 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003998 if (regname == '=')
3999 vim_free(y_array);
4000
Bram Moolenaar033d8882013-09-25 23:24:57 +02004001 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004002
Bram Moolenaar677ee682005-01-27 14:41:15 +00004003 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004004 adjust_cursor_eol();
4005}
4006
4007/*
4008 * When the cursor is on the NUL past the end of the line and it should not be
4009 * there move it left.
4010 */
4011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004012adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004013{
4014 if (curwin->w_cursor.col > 0
4015 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004016#ifdef FEAT_VIRTUALEDIT
4017 && (ve_flags & VE_ONEMORE) == 0
4018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 && !(restart_edit || (State & INSERT)))
4020 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004021 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004022 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024#ifdef FEAT_VIRTUALEDIT
4025 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004026 {
4027 colnr_T scol, ecol;
4028
4029 /* Coladd is set to the width of the last character. */
4030 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4031 curwin->w_cursor.coladd = ecol - scol + 1;
4032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033#endif
4034 }
4035}
4036
4037#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4038/*
4039 * Return TRUE if lines starting with '#' should be left aligned.
4040 */
4041 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004042preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
4044 return
4045# ifdef FEAT_SMARTINDENT
4046# ifdef FEAT_CINDENT
4047 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4048# else
4049 curbuf->b_p_si
4050# endif
4051# endif
4052# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004053 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4054 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055# endif
4056 ;
4057}
4058#endif
4059
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004060/*
4061 * Return the character name of the register with the given number.
4062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004064get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
4066 if (num == -1)
4067 return '"';
4068 else if (num < 10)
4069 return num + '0';
4070 else if (num == DELETION_REGISTER)
4071 return '-';
4072#ifdef FEAT_CLIPBOARD
4073 else if (num == STAR_REGISTER)
4074 return '*';
4075 else if (num == PLUS_REGISTER)
4076 return '+';
4077#endif
4078 else
4079 {
4080#ifdef EBCDIC
4081 int i;
4082
4083 /* EBCDIC is really braindead ... */
4084 i = 'a' + (num - 10);
4085 if (i > 'i')
4086 i += 7;
4087 if (i > 'r')
4088 i += 8;
4089 return i;
4090#else
4091 return num + 'a' - 10;
4092#endif
4093 }
4094}
4095
4096/*
4097 * ":dis" and ":registers": Display the contents of the yank registers.
4098 */
4099 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004100ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004102 int i, n;
4103 long j;
4104 char_u *p;
4105 yankreg_T *yb;
4106 int name;
4107 int attr;
4108 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004109#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004110 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004111#else
4112# define clen 1
4113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 if (arg != NULL && *arg == NUL)
4116 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004117 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119 /* Highlight title */
4120 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4121 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4122 {
4123 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004124 if (arg != NULL && vim_strchr(arg, name) == NULL
4125#ifdef ONE_CLIPBOARD
4126 /* Star register and plus register contain the same thing. */
4127 && (name != '*' || vim_strchr(arg, '+') == NULL)
4128#endif
4129 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 continue; /* did not ask for this register */
4131
4132#ifdef FEAT_CLIPBOARD
4133 /* Adjust register name for "unnamed" in 'clipboard'.
4134 * When it's a clipboard register, fill it with the current contents
4135 * of the clipboard. */
4136 adjust_clip_reg(&name);
4137 (void)may_get_selection(name);
4138#endif
4139
4140 if (i == -1)
4141 {
4142 if (y_previous != NULL)
4143 yb = y_previous;
4144 else
4145 yb = &(y_regs[0]);
4146 }
4147 else
4148 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004149
4150#ifdef FEAT_EVAL
4151 if (name == MB_TOLOWER(redir_reg)
4152 || (redir_reg == '"' && yb == y_previous))
4153 continue; /* do not list register being written to, the
4154 * pointer can be freed */
4155#endif
4156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 if (yb->y_array != NULL)
4158 {
4159 msg_putchar('\n');
4160 msg_putchar('"');
4161 msg_putchar(name);
4162 MSG_PUTS(" ");
4163
4164 n = (int)Columns - 6;
4165 for (j = 0; j < yb->y_size && n > 1; ++j)
4166 {
4167 if (j)
4168 {
4169 MSG_PUTS_ATTR("^J", attr);
4170 n -= 2;
4171 }
4172 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004175 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004176#endif
4177 msg_outtrans_len(p, clen);
4178#ifdef FEAT_MBYTE
4179 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180#endif
4181 }
4182 }
4183 if (n > 1 && yb->y_type == MLINE)
4184 MSG_PUTS_ATTR("^J", attr);
4185 out_flush(); /* show one line at a time */
4186 }
4187 ui_breakcheck();
4188 }
4189
4190 /*
4191 * display last inserted text
4192 */
4193 if ((p = get_last_insert()) != NULL
4194 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4195 {
4196 MSG_PUTS("\n\". ");
4197 dis_msg(p, TRUE);
4198 }
4199
4200 /*
4201 * display last command line
4202 */
4203 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4204 && !got_int)
4205 {
4206 MSG_PUTS("\n\": ");
4207 dis_msg(last_cmdline, FALSE);
4208 }
4209
4210 /*
4211 * display current file name
4212 */
4213 if (curbuf->b_fname != NULL
4214 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4215 {
4216 MSG_PUTS("\n\"% ");
4217 dis_msg(curbuf->b_fname, FALSE);
4218 }
4219
4220 /*
4221 * display alternate file name
4222 */
4223 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4224 {
4225 char_u *fname;
4226 linenr_T dummy;
4227
4228 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4229 {
4230 MSG_PUTS("\n\"# ");
4231 dis_msg(fname, FALSE);
4232 }
4233 }
4234
4235 /*
4236 * display last search pattern
4237 */
4238 if (last_search_pat() != NULL
4239 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4240 {
4241 MSG_PUTS("\n\"/ ");
4242 dis_msg(last_search_pat(), FALSE);
4243 }
4244
4245#ifdef FEAT_EVAL
4246 /*
4247 * display last used expression
4248 */
4249 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4250 && !got_int)
4251 {
4252 MSG_PUTS("\n\"= ");
4253 dis_msg(expr_line, FALSE);
4254 }
4255#endif
4256}
4257
4258/*
4259 * display a string for do_dis()
4260 * truncate at end of screen line
4261 */
4262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004263dis_msg(
4264 char_u *p,
4265 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
4267 int n;
4268#ifdef FEAT_MBYTE
4269 int l;
4270#endif
4271
4272 n = (int)Columns - 6;
4273 while (*p != NUL
4274 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4275 && (n -= ptr2cells(p)) >= 0)
4276 {
4277#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004278 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
4280 msg_outtrans_len(p, l);
4281 p += l;
4282 }
4283 else
4284#endif
4285 msg_outtrans_len(p++, 1);
4286 }
4287 ui_breakcheck();
4288}
4289
Bram Moolenaar81340392012-06-06 16:12:59 +02004290#if defined(FEAT_COMMENTS) || defined(PROTO)
4291/*
4292 * If "process" is TRUE and the line begins with a comment leader (possibly
4293 * after some white space), return a pointer to the text after it. Put a boolean
4294 * value indicating whether the line ends with an unclosed comment in
4295 * "is_comment".
4296 * line - line to be processed,
4297 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004298 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004299 * include_space - whether to also skip space following the comment leader,
4300 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004301 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004302 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004303 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004304skip_comment(
4305 char_u *line,
4306 int process,
4307 int include_space,
4308 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004309{
4310 char_u *comment_flags = NULL;
4311 int lead_len;
4312 int leader_offset = get_last_leader_offset(line, &comment_flags);
4313
4314 *is_comment = FALSE;
4315 if (leader_offset != -1)
4316 {
4317 /* Let's check whether the line ends with an unclosed comment.
4318 * If the last comment leader has COM_END in flags, there's no comment.
4319 */
4320 while (*comment_flags)
4321 {
4322 if (*comment_flags == COM_END
4323 || *comment_flags == ':')
4324 break;
4325 ++comment_flags;
4326 }
4327 if (*comment_flags != COM_END)
4328 *is_comment = TRUE;
4329 }
4330
4331 if (process == FALSE)
4332 return line;
4333
4334 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4335
4336 if (lead_len == 0)
4337 return line;
4338
4339 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004340 * - COM_END,
4341 * - colon,
4342 * whichever comes first.
4343 */
4344 while (*comment_flags)
4345 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004346 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004347 || *comment_flags == ':')
4348 {
4349 break;
4350 }
4351 ++comment_flags;
4352 }
4353
4354 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004355 * starting with a closing part of a three-part comment. That's good,
4356 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004357 */
4358 if (*comment_flags == ':' || *comment_flags == NUL)
4359 line += lead_len;
4360
4361 return line;
4362}
4363#endif
4364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004366 * Join 'count' lines (minimal 2) at cursor position.
4367 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004368 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4369 * leaders should not be removed.
4370 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4371 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004373 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 */
4375 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004376do_join(
4377 long count,
4378 int insert_space,
4379 int save_undo,
4380 int use_formatoptions UNUSED,
4381 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004383 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004384 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004385 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004387 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004388 int endcurr1 = NUL;
4389 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004390 int currsize = 0; /* size of the current line */
4391 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004393 colnr_T col = 0;
4394 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004395#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004396 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004397 int remove_comments = (use_formatoptions == TRUE)
4398 && has_format_option(FO_REMOVE_COMS);
4399 int prev_was_comment;
4400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004403 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4404 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4405 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004407 /* Allocate an array to store the number of spaces inserted before each
4408 * line. We will use it to pre-compute the length of the new line and the
4409 * proper placement of each original line in the new one. */
4410 spaces = lalloc_clear((long_u)count, TRUE);
4411 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004413#if defined(FEAT_COMMENTS) || defined(PROTO)
4414 if (remove_comments)
4415 {
4416 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4417 if (comments == NULL)
4418 {
4419 vim_free(spaces);
4420 return FAIL;
4421 }
4422 }
4423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004426 * Don't move anything, just compute the final line length
4427 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004429 for (t = 0; t < count; ++t)
4430 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004431 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004432 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004433 {
4434 /* Set the '[ mark. */
4435 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4436 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4437 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004438#if defined(FEAT_COMMENTS) || defined(PROTO)
4439 if (remove_comments)
4440 {
4441 /* We don't want to remove the comment leader if the
4442 * previous line is not a comment. */
4443 if (t > 0 && prev_was_comment)
4444 {
4445
4446 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4447 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004448 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004449 curr = new_curr;
4450 }
4451 else
4452 curr = skip_comment(curr, FALSE, insert_space,
4453 &prev_was_comment);
4454 }
4455#endif
4456
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004457 if (insert_space && t > 0)
4458 {
4459 curr = skipwhite(curr);
4460 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4461#ifdef FEAT_MBYTE
4462 && (!has_format_option(FO_MBYTE_JOIN)
4463 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4464 && (!has_format_option(FO_MBYTE_JOIN2)
4465 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4466#endif
4467 )
4468 {
4469 /* don't add a space if the line is ending in a space */
4470 if (endcurr1 == ' ')
4471 endcurr1 = endcurr2;
4472 else
4473 ++spaces[t];
4474 /* extra space when 'joinspaces' set and line ends in '.' */
4475 if ( p_js
4476 && (endcurr1 == '.'
4477 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4478 && (endcurr1 == '?' || endcurr1 == '!'))))
4479 ++spaces[t];
4480 }
4481 }
4482 currsize = (int)STRLEN(curr);
4483 sumsize += currsize + spaces[t];
4484 endcurr1 = endcurr2 = NUL;
4485 if (insert_space && currsize > 0)
4486 {
4487#ifdef FEAT_MBYTE
4488 if (has_mbyte)
4489 {
4490 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004491 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004492 endcurr1 = (*mb_ptr2char)(cend);
4493 if (cend > curr)
4494 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004495 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004496 endcurr2 = (*mb_ptr2char)(cend);
4497 }
4498 }
4499 else
4500#endif
4501 {
4502 endcurr1 = *(curr + currsize - 1);
4503 if (currsize > 1)
4504 endcurr2 = *(curr + currsize - 2);
4505 }
4506 }
4507 line_breakcheck();
4508 if (got_int)
4509 {
4510 ret = FAIL;
4511 goto theend;
4512 }
4513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004515 /* store the column position before last line */
4516 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004518 /* allocate the space for the new line */
4519 newp = alloc_check((unsigned)(sumsize + 1));
4520 cend = newp + sumsize;
4521 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004523 /*
4524 * Move affected lines to the new long one.
4525 *
4526 * Move marks from each deleted line to the joined line, adjusting the
4527 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4528 * should not really be a problem.
4529 */
4530 for (t = count - 1; ; --t)
4531 {
4532 cend -= currsize;
4533 mch_memmove(cend, curr, (size_t)currsize);
4534 if (spaces[t] > 0)
4535 {
4536 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004537 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004538 }
4539 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004540 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004541 if (t == 0)
4542 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004543 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004544#if defined(FEAT_COMMENTS) || defined(PROTO)
4545 if (remove_comments)
4546 curr += comments[t - 1];
4547#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004548 if (insert_space && t > 1)
4549 curr = skipwhite(curr);
4550 currsize = (int)STRLEN(curr);
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4553
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004554 if (setmark)
4555 {
4556 /* Set the '] mark. */
4557 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4558 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4559 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 /* Only report the change in the first line here, del_lines() will report
4562 * the deleted line. */
4563 changed_lines(curwin->w_cursor.lnum, currsize,
4564 curwin->w_cursor.lnum + 1, 0L);
4565
4566 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004567 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 * briefly, and then move it back. After del_lines() the cursor may
4569 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 */
4571 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004573 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 curwin->w_cursor.lnum = t;
4575
4576 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004577 * Set the cursor column:
4578 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004579 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004581 curwin->w_cursor.col =
4582 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#ifdef FEAT_VIRTUALEDIT
4586 curwin->w_cursor.coladd = 0;
4587#endif
4588 curwin->w_set_curswant = TRUE;
4589
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004590theend:
4591 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004592#if defined(FEAT_COMMENTS) || defined(PROTO)
4593 if (remove_comments)
4594 vim_free(comments);
4595#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004596 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597}
4598
4599#ifdef FEAT_COMMENTS
4600/*
4601 * Return TRUE if the two comment leaders given are the same. "lnum" is
4602 * the first line. White-space is ignored. Note that the whole of
4603 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4604 */
4605 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004606same_leader(
4607 linenr_T lnum,
4608 int leader1_len,
4609 char_u *leader1_flags,
4610 int leader2_len,
4611 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
4613 int idx1 = 0, idx2 = 0;
4614 char_u *p;
4615 char_u *line1;
4616 char_u *line2;
4617
4618 if (leader1_len == 0)
4619 return (leader2_len == 0);
4620
4621 /*
4622 * If first leader has 'f' flag, the lines can be joined only if the
4623 * second line does not have a leader.
4624 * If first leader has 'e' flag, the lines can never be joined.
4625 * If fist leader has 's' flag, the lines can only be joined if there is
4626 * some text after it and the second line has the 'm' flag.
4627 */
4628 if (leader1_flags != NULL)
4629 {
4630 for (p = leader1_flags; *p && *p != ':'; ++p)
4631 {
4632 if (*p == COM_FIRST)
4633 return (leader2_len == 0);
4634 if (*p == COM_END)
4635 return FALSE;
4636 if (*p == COM_START)
4637 {
4638 if (*(ml_get(lnum) + leader1_len) == NUL)
4639 return FALSE;
4640 if (leader2_flags == NULL || leader2_len == 0)
4641 return FALSE;
4642 for (p = leader2_flags; *p && *p != ':'; ++p)
4643 if (*p == COM_MIDDLE)
4644 return TRUE;
4645 return FALSE;
4646 }
4647 }
4648 }
4649
4650 /*
4651 * Get current line and next line, compare the leaders.
4652 * The first line has to be saved, only one line can be locked at a time.
4653 */
4654 line1 = vim_strsave(ml_get(lnum));
4655 if (line1 != NULL)
4656 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004657 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 ;
4659 line2 = ml_get(lnum + 1);
4660 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4661 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004662 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
4664 if (line1[idx1++] != line2[idx2])
4665 break;
4666 }
4667 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004668 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 ++idx1;
4670 }
4671 vim_free(line1);
4672 }
4673 return (idx2 == leader2_len && idx1 == leader1_len);
4674}
4675#endif
4676
4677/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004678 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 */
4680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004681op_format(
4682 oparg_T *oap,
4683 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 long old_line_count = curbuf->b_ml.ml_line_count;
4686
4687 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4688 * can put it back there. */
4689 curwin->w_cursor = oap->cursor_start;
4690
4691 if (u_save((linenr_T)(oap->start.lnum - 1),
4692 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4693 return;
4694 curwin->w_cursor = oap->start;
4695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (oap->is_VIsual)
4697 /* When there is no change: need to remove the Visual selection */
4698 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
4700 /* Set '[ mark at the start of the formatted area */
4701 curbuf->b_op_start = oap->start;
4702
4703 /* For "gw" remember the cursor position and put it back below (adjusted
4704 * for joined and split lines). */
4705 if (keep_cursor)
4706 saved_cursor = oap->cursor_start;
4707
Bram Moolenaar81a82092008-03-12 16:27:00 +00004708 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
4710 /*
4711 * Leave the cursor at the first non-blank of the last formatted line.
4712 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4713 * line, so "." will do the next lines.
4714 */
4715 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4716 ++curwin->w_cursor.lnum;
4717 beginline(BL_WHITE | BL_FIX);
4718 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4719 msgmore(old_line_count);
4720
4721 /* put '] mark on the end of the formatted area */
4722 curbuf->b_op_end = curwin->w_cursor;
4723
4724 if (keep_cursor)
4725 {
4726 curwin->w_cursor = saved_cursor;
4727 saved_cursor.lnum = 0;
4728 }
4729
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (oap->is_VIsual)
4731 {
4732 win_T *wp;
4733
4734 FOR_ALL_WINDOWS(wp)
4735 {
4736 if (wp->w_old_cursor_lnum != 0)
4737 {
4738 /* When lines have been inserted or deleted, adjust the end of
4739 * the Visual area to be redrawn. */
4740 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4741 wp->w_old_cursor_lnum += old_line_count;
4742 else
4743 wp->w_old_visual_lnum += old_line_count;
4744 }
4745 }
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004749#if defined(FEAT_EVAL) || defined(PROTO)
4750/*
4751 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4752 */
4753 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004754op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004755{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004756 if (oap->is_VIsual)
4757 /* When there is no change: need to remove the Visual selection */
4758 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004759
Bram Moolenaar700303e2010-07-11 17:35:50 +02004760 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4761 /* As documented: when 'formatexpr' returns non-zero fall back to
4762 * internal formatting. */
4763 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004764}
4765
4766 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004767fex_format(
4768 linenr_T lnum,
4769 long count,
4770 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004771{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004772 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4773 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004774 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004775 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004776
4777 /*
4778 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004779 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004780 */
4781 set_vim_var_nr(VV_LNUM, lnum);
4782 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004783 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004784
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004785 /* Make a copy, the option could be changed while calling it. */
4786 fex = vim_strsave(curbuf->b_p_fex);
4787 if (fex == NULL)
4788 return 0;
4789
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004790 /*
4791 * Evaluate the function.
4792 */
4793 if (use_sandbox)
4794 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004795 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004796 if (use_sandbox)
4797 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004798
4799 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004800 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004801
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004802 return r;
4803}
4804#endif
4805
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806/*
4807 * Format "line_count" lines, starting at the cursor position.
4808 * When "line_count" is negative, format until the end of the paragraph.
4809 * Lines after the cursor line are saved for undo, caller must have saved the
4810 * first line.
4811 */
4812 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004813format_lines(
4814 linenr_T line_count,
4815 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817 int max_len;
4818 int is_not_par; /* current line not part of parag. */
4819 int next_is_not_par; /* next line not part of paragraph */
4820 int is_end_par; /* at end of paragraph */
4821 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4822 int next_is_start_par = FALSE;
4823#ifdef FEAT_COMMENTS
4824 int leader_len = 0; /* leader len of current line */
4825 int next_leader_len; /* leader len of next line */
4826 char_u *leader_flags = NULL; /* flags for leader of current line */
4827 char_u *next_leader_flags; /* flags for leader of next line */
4828 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004829 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#endif
4831 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004832 int second_indent = -1; /* indent for second line (comment
4833 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 int do_second_indent;
4835 int do_number_indent;
4836 int do_trail_white;
4837 int first_par_line = TRUE;
4838 int smd_save;
4839 long count;
4840 int need_set_indent = TRUE; /* set indent of next paragraph */
4841 int force_format = FALSE;
4842 int old_State = State;
4843
4844 /* length of a line to force formatting: 3 * 'tw' */
4845 max_len = comp_textwidth(TRUE) * 3;
4846
4847 /* check for 'q', '2' and '1' in 'formatoptions' */
4848#ifdef FEAT_COMMENTS
4849 do_comments = has_format_option(FO_Q_COMS);
4850#endif
4851 do_second_indent = has_format_option(FO_Q_SECOND);
4852 do_number_indent = has_format_option(FO_Q_NUMBER);
4853 do_trail_white = has_format_option(FO_WHITE_PAR);
4854
4855 /*
4856 * Get info about the previous and current line.
4857 */
4858 if (curwin->w_cursor.lnum > 1)
4859 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4860#ifdef FEAT_COMMENTS
4861 , &leader_len, &leader_flags, do_comments
4862#endif
4863 );
4864 else
4865 is_not_par = TRUE;
4866 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4867#ifdef FEAT_COMMENTS
4868 , &next_leader_len, &next_leader_flags, do_comments
4869#endif
4870 );
4871 is_end_par = (is_not_par || next_is_not_par);
4872 if (!is_end_par && do_trail_white)
4873 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4874
4875 curwin->w_cursor.lnum--;
4876 for (count = line_count; count != 0 && !got_int; --count)
4877 {
4878 /*
4879 * Advance to next paragraph.
4880 */
4881 if (advance)
4882 {
4883 curwin->w_cursor.lnum++;
4884 prev_is_end_par = is_end_par;
4885 is_not_par = next_is_not_par;
4886#ifdef FEAT_COMMENTS
4887 leader_len = next_leader_len;
4888 leader_flags = next_leader_flags;
4889#endif
4890 }
4891
4892 /*
4893 * The last line to be formatted.
4894 */
4895 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4896 {
4897 next_is_not_par = TRUE;
4898#ifdef FEAT_COMMENTS
4899 next_leader_len = 0;
4900 next_leader_flags = NULL;
4901#endif
4902 }
4903 else
4904 {
4905 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4906#ifdef FEAT_COMMENTS
4907 , &next_leader_len, &next_leader_flags, do_comments
4908#endif
4909 );
4910 if (do_number_indent)
4911 next_is_start_par =
4912 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4913 }
4914 advance = TRUE;
4915 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4916 if (!is_end_par && do_trail_white)
4917 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4918
4919 /*
4920 * Skip lines that are not in a paragraph.
4921 */
4922 if (is_not_par)
4923 {
4924 if (line_count < 0)
4925 break;
4926 }
4927 else
4928 {
4929 /*
4930 * For the first line of a paragraph, check indent of second line.
4931 * Don't do this for comments and empty lines.
4932 */
4933 if (first_par_line
4934 && (do_second_indent || do_number_indent)
4935 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004936 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004938 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004939 {
4940#ifdef FEAT_COMMENTS
4941 if (leader_len == 0 && next_leader_len == 0)
4942 {
4943 /* no comment found */
4944#endif
4945 second_indent =
4946 get_indent_lnum(curwin->w_cursor.lnum + 1);
4947#ifdef FEAT_COMMENTS
4948 }
4949 else
4950 {
4951 second_indent = next_leader_len;
4952 do_comments_list = 1;
4953 }
4954#endif
4955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004957 {
4958#ifdef FEAT_COMMENTS
4959 if (leader_len == 0 && next_leader_len == 0)
4960 {
4961 /* no comment found */
4962#endif
4963 second_indent =
4964 get_number_indent(curwin->w_cursor.lnum);
4965#ifdef FEAT_COMMENTS
4966 }
4967 else
4968 {
4969 /* get_number_indent() is now "comment aware"... */
4970 second_indent =
4971 get_number_indent(curwin->w_cursor.lnum);
4972 do_comments_list = 1;
4973 }
4974#endif
4975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977
4978 /*
4979 * When the comment leader changes, it's the end of the paragraph.
4980 */
4981 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4982#ifdef FEAT_COMMENTS
4983 || !same_leader(curwin->w_cursor.lnum,
4984 leader_len, leader_flags,
4985 next_leader_len, next_leader_flags)
4986#endif
4987 )
4988 is_end_par = TRUE;
4989
4990 /*
4991 * If we have got to the end of a paragraph, or the line is
4992 * getting long, format it.
4993 */
4994 if (is_end_par || force_format)
4995 {
4996 if (need_set_indent)
4997 /* replace indent in first line with minimal number of
4998 * tabs and spaces, according to current options */
4999 (void)set_indent(get_indent(), SIN_CHANGED);
5000
5001 /* put cursor on last non-space */
5002 State = NORMAL; /* don't go past end-of-line */
5003 coladvance((colnr_T)MAXCOL);
5004 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5005 dec_cursor();
5006
5007 /* do the formatting, without 'showmode' */
5008 State = INSERT; /* for open_line() */
5009 smd_save = p_smd;
5010 p_smd = FALSE;
5011 insertchar(NUL, INSCHAR_FORMAT
5012#ifdef FEAT_COMMENTS
5013 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005014 + (do_comments && do_comments_list
5015 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005017 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 State = old_State;
5019 p_smd = smd_save;
5020 second_indent = -1;
5021 /* at end of par.: need to set indent of next par. */
5022 need_set_indent = is_end_par;
5023 if (is_end_par)
5024 {
5025 /* When called with a negative line count, break at the
5026 * end of the paragraph. */
5027 if (line_count < 0)
5028 break;
5029 first_par_line = TRUE;
5030 }
5031 force_format = FALSE;
5032 }
5033
5034 /*
5035 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005036 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 */
5038 if (!is_end_par)
5039 {
5040 advance = FALSE;
5041 curwin->w_cursor.lnum++;
5042 curwin->w_cursor.col = 0;
5043 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005044 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005047 {
5048 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5050 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005051 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005053 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5054 {
5055 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005056 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005057
5058 if (indent > 0)
5059 {
5060 (void)del_bytes(indent, FALSE, FALSE);
5061 mark_col_adjust(curwin->w_cursor.lnum,
5062 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005063 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005066 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
5068 beep_flush();
5069 break;
5070 }
5071 first_par_line = FALSE;
5072 /* If the line is getting long, format it next time */
5073 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5074 force_format = TRUE;
5075 else
5076 force_format = FALSE;
5077 }
5078 }
5079 line_breakcheck();
5080 }
5081}
5082
5083/*
5084 * Return TRUE if line "lnum" ends in a white character.
5085 */
5086 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005087ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 char_u *s = ml_get(lnum);
5090 size_t l;
5091
5092 if (*s == NUL)
5093 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005094 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 * invocation may call function multiple times". */
5096 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005097 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098}
5099
5100/*
5101 * Blank lines, and lines containing only the comment leader, are left
5102 * untouched by the formatting. The function returns TRUE in this
5103 * case. It also returns TRUE when a line starts with the end of a comment
5104 * ('e' in comment flags), so that this line is skipped, and not joined to the
5105 * previous line. A new paragraph starts after a blank line, or when the
5106 * comment leader changes -- webb.
5107 */
5108#ifdef FEAT_COMMENTS
5109 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005110fmt_check_par(
5111 linenr_T lnum,
5112 int *leader_len,
5113 char_u **leader_flags,
5114 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115{
5116 char_u *flags = NULL; /* init for GCC */
5117 char_u *ptr;
5118
5119 ptr = ml_get(lnum);
5120 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005121 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 else
5123 *leader_len = 0;
5124
5125 if (*leader_len > 0)
5126 {
5127 /*
5128 * Search for 'e' flag in comment leader flags.
5129 */
5130 flags = *leader_flags;
5131 while (*flags && *flags != ':' && *flags != COM_END)
5132 ++flags;
5133 }
5134
5135 return (*skipwhite(ptr + *leader_len) == NUL
5136 || (*leader_len > 0 && *flags == COM_END)
5137 || startPS(lnum, NUL, FALSE));
5138}
5139#else
5140 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005141fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
5143 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5144}
5145#endif
5146
5147/*
5148 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5149 * previous line is in the same paragraph. Used for auto-formatting.
5150 */
5151 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005152paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 char_u *p;
5155#ifdef FEAT_COMMENTS
5156 int leader_len = 0; /* leader len of current line */
5157 char_u *leader_flags = NULL; /* flags for leader of current line */
5158 int next_leader_len; /* leader len of next line */
5159 char_u *next_leader_flags; /* flags for leader of next line */
5160 int do_comments; /* format comments */
5161#endif
5162
5163 if (lnum <= 1)
5164 return TRUE; /* start of the file */
5165
5166 p = ml_get(lnum - 1);
5167 if (*p == NUL)
5168 return TRUE; /* after empty line */
5169
5170#ifdef FEAT_COMMENTS
5171 do_comments = has_format_option(FO_Q_COMS);
5172#endif
5173 if (fmt_check_par(lnum - 1
5174#ifdef FEAT_COMMENTS
5175 , &leader_len, &leader_flags, do_comments
5176#endif
5177 ))
5178 return TRUE; /* after non-paragraph line */
5179
5180 if (fmt_check_par(lnum
5181#ifdef FEAT_COMMENTS
5182 , &next_leader_len, &next_leader_flags, do_comments
5183#endif
5184 ))
5185 return TRUE; /* "lnum" is not a paragraph line */
5186
5187 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5188 return TRUE; /* missing trailing space in previous line. */
5189
5190 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5191 return TRUE; /* numbered item starts in "lnum". */
5192
5193#ifdef FEAT_COMMENTS
5194 if (!same_leader(lnum - 1, leader_len, leader_flags,
5195 next_leader_len, next_leader_flags))
5196 return TRUE; /* change of comment leader. */
5197#endif
5198
5199 return FALSE;
5200}
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202/*
5203 * prepare a few things for block mode yank/delete/tilde
5204 *
5205 * for delete:
5206 * - textlen includes the first/last char to be (partly) deleted
5207 * - start/endspaces is the number of columns that are taken by the
5208 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005209 * deleted.
5210 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 * - textlen includes the first/last char to be wholly yanked
5212 * - start/endspaces is the number of columns of the first/last yanked char
5213 * that are to be yanked.
5214 */
5215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005216block_prep(
5217 oparg_T *oap,
5218 struct block_def *bdp,
5219 linenr_T lnum,
5220 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 int incr = 0;
5223 char_u *pend;
5224 char_u *pstart;
5225 char_u *line;
5226 char_u *prev_pstart;
5227 char_u *prev_pend;
5228
5229 bdp->startspaces = 0;
5230 bdp->endspaces = 0;
5231 bdp->textlen = 0;
5232 bdp->start_vcol = 0;
5233 bdp->end_vcol = 0;
5234#ifdef FEAT_VISUALEXTRA
5235 bdp->is_short = FALSE;
5236 bdp->is_oneChar = FALSE;
5237 bdp->pre_whitesp = 0;
5238 bdp->pre_whitesp_c = 0;
5239 bdp->end_char_vcols = 0;
5240#endif
5241 bdp->start_char_vcols = 0;
5242
5243 line = ml_get(lnum);
5244 pstart = line;
5245 prev_pstart = line;
5246 while (bdp->start_vcol < oap->start_vcol && *pstart)
5247 {
5248 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005249 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 bdp->start_vcol += incr;
5251#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005252 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
5254 bdp->pre_whitesp += incr;
5255 bdp->pre_whitesp_c++;
5256 }
5257 else
5258 {
5259 bdp->pre_whitesp = 0;
5260 bdp->pre_whitesp_c = 0;
5261 }
5262#endif
5263 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005264 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 bdp->start_char_vcols = incr;
5267 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5268 {
5269 bdp->end_vcol = bdp->start_vcol;
5270#ifdef FEAT_VISUALEXTRA
5271 bdp->is_short = TRUE;
5272#endif
5273 if (!is_del || oap->op_type == OP_APPEND)
5274 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5275 }
5276 else
5277 {
5278 /* notice: this converts partly selected Multibyte characters to
5279 * spaces, too. */
5280 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5281 if (is_del && bdp->startspaces)
5282 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5283 pend = pstart;
5284 bdp->end_vcol = bdp->start_vcol;
5285 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5286 {
5287#ifdef FEAT_VISUALEXTRA
5288 bdp->is_oneChar = TRUE;
5289#endif
5290 if (oap->op_type == OP_INSERT)
5291 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5292 else if (oap->op_type == OP_APPEND)
5293 {
5294 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5295 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5296 }
5297 else
5298 {
5299 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5300 if (is_del && oap->op_type != OP_LSHIFT)
5301 {
5302 /* just putting the sum of those two into
5303 * bdp->startspaces doesn't work for Visual replace,
5304 * so we have to split the tab in two */
5305 bdp->startspaces = bdp->start_char_vcols
5306 - (bdp->start_vcol - oap->start_vcol);
5307 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5308 }
5309 }
5310 }
5311 else
5312 {
5313 prev_pend = pend;
5314 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5315 {
5316 /* Count a tab for what it's worth (if list mode not on) */
5317 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005318 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 bdp->end_vcol += incr;
5320 }
5321 if (bdp->end_vcol <= oap->end_vcol
5322 && (!is_del
5323 || oap->op_type == OP_APPEND
5324 || oap->op_type == OP_REPLACE)) /* line too short */
5325 {
5326#ifdef FEAT_VISUALEXTRA
5327 bdp->is_short = TRUE;
5328#endif
5329 /* Alternative: include spaces to fill up the block.
5330 * Disadvantage: can lead to trailing spaces when the line is
5331 * short where the text is put */
5332 /* if (!is_del || oap->op_type == OP_APPEND) */
5333 if (oap->op_type == OP_APPEND || virtual_op)
5334 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005335 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 else
5337 bdp->endspaces = 0; /* replace doesn't add characters */
5338 }
5339 else if (bdp->end_vcol > oap->end_vcol)
5340 {
5341 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5342 if (!is_del && bdp->endspaces)
5343 {
5344 bdp->endspaces = incr - bdp->endspaces;
5345 if (pend != pstart)
5346 pend = prev_pend;
5347 }
5348 }
5349 }
5350#ifdef FEAT_VISUALEXTRA
5351 bdp->end_char_vcols = incr;
5352#endif
5353 if (is_del && bdp->startspaces)
5354 pstart = prev_pstart;
5355 bdp->textlen = (int)(pend - pstart);
5356 }
5357 bdp->textcol = (colnr_T) (pstart - line);
5358 bdp->textstart = pstart;
5359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005362 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005365op_addsub(
5366 oparg_T *oap,
5367 linenr_T Prenum1, /* Amount of add/subtract */
5368 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005370 pos_T pos;
5371 struct block_def bd;
5372 int change_cnt = 0;
5373 linenr_T amount = Prenum1;
5374
5375 if (!VIsual_active)
5376 {
5377 pos = curwin->w_cursor;
5378 if (u_save_cursor() == FAIL)
5379 return;
5380 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5381 if (change_cnt)
5382 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5383 }
5384 else
5385 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005386 int one_change;
5387 int length;
5388 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005389
5390 if (u_save((linenr_T)(oap->start.lnum - 1),
5391 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5392 return;
5393
5394 pos = oap->start;
5395 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5396 {
5397 if (oap->block_mode) /* Visual block mode */
5398 {
5399 block_prep(oap, &bd, pos.lnum, FALSE);
5400 pos.col = bd.textcol;
5401 length = bd.textlen;
5402 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005403 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005404 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005405 curwin->w_cursor.col = 0;
5406 pos.col = 0;
5407 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5408 }
5409 else /* oap->motion_type == MCHAR */
5410 {
5411 if (!oap->inclusive)
5412 dec(&(oap->end));
5413 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5414 pos.col = 0;
5415 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005416 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005417 pos.col += oap->start.col;
5418 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005419 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005420 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005421 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005422 length = (int)STRLEN(ml_get(oap->end.lnum));
5423 if (oap->end.col >= length)
5424 oap->end.col = length - 1;
5425 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005426 }
5427 }
5428 one_change = do_addsub(oap->op_type, &pos, length, amount);
5429 if (one_change)
5430 {
5431 /* Remember the start position of the first change. */
5432 if (change_cnt == 0)
5433 startpos = curbuf->b_op_start;
5434 ++change_cnt;
5435 }
5436
5437#ifdef FEAT_NETBEANS_INTG
5438 if (netbeans_active() && one_change)
5439 {
5440 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5441
5442 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5443 netbeans_inserted(curbuf, pos.lnum, pos.col,
5444 &ptr[pos.col], length);
5445 }
5446#endif
5447 if (g_cmd && one_change)
5448 amount += Prenum1;
5449 }
5450 if (change_cnt)
5451 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5452
5453 if (!change_cnt && oap->is_VIsual)
5454 /* No change: need to remove the Visual selection */
5455 redraw_curbuf_later(INVERTED);
5456
5457 /* Set '[ mark if something changed. Keep the last end
5458 * position from do_addsub(). */
5459 if (change_cnt > 0)
5460 curbuf->b_op_start = startpos;
5461
5462 if (change_cnt > p_report)
5463 {
5464 if (change_cnt == 1)
5465 MSG(_("1 line changed"));
5466 else
5467 smsg((char_u *)_("%ld lines changed"), change_cnt);
5468 }
5469 }
5470}
5471
5472/*
5473 * Add or subtract 'Prenum1' from a number in a line
5474 * op_type is OP_NR_ADD or OP_NR_SUB
5475 *
5476 * Returns TRUE if some character was changed.
5477 */
5478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005479do_addsub(
5480 int op_type,
5481 pos_T *pos,
5482 int length,
5483 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005484{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 int col;
5486 char_u *buf1;
5487 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005488 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005490 uvarnumber_T n;
5491 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 char_u *ptr;
5493 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 int todel;
5495 int dohex;
5496 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005497 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 int doalp;
5499 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005501 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005502 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005503 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005504 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005505 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005506 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005507 pos_T startpos;
5508 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
5510 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5511 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005512 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5514
Bram Moolenaard79e5502016-01-10 22:13:02 +01005515 curwin->w_cursor = *pos;
5516 ptr = ml_get(pos->lnum);
5517 col = pos->col;
5518
5519 if (*ptr == NUL)
5520 goto theend;
5521
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 /*
5523 * First check if we are on a hexadecimal number, after the "0x".
5524 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005525 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005527 if (dobin)
5528 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005529 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005530 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005531#ifdef FEAT_MBYTE
5532 if (has_mbyte)
5533 col -= (*mb_head_off)(ptr, ptr + col);
5534#endif
5535 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005536
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005537 if (dohex)
5538 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005539 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005540 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005541#ifdef FEAT_MBYTE
5542 if (has_mbyte)
5543 col -= (*mb_head_off)(ptr, ptr + col);
5544#endif
5545 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005546
5547 if ( dobin
5548 && dohex
5549 && ! ((col > 0
5550 && (ptr[col] == 'X'
5551 || ptr[col] == 'x')
5552 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005553#ifdef FEAT_MBYTE
5554 && (!has_mbyte ||
5555 !(*mb_head_off)(ptr, ptr + col - 1))
5556#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005557 && vim_isxdigit(ptr[col + 1]))))
5558 {
5559
5560 /* In case of binary/hexadecimal pattern overlap match, rescan */
5561
Bram Moolenaard79e5502016-01-10 22:13:02 +01005562 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005563
5564 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005565 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005566 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005567#ifdef FEAT_MBYTE
5568 if (has_mbyte)
5569 col -= (*mb_head_off)(ptr, ptr + col);
5570#endif
5571 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005572 }
5573
5574 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005575 && col > 0
5576 && (ptr[col] == 'X'
5577 || ptr[col] == 'x')
5578 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005579#ifdef FEAT_MBYTE
5580 && (!has_mbyte ||
5581 !(*mb_head_off)(ptr, ptr + col - 1))
5582#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005583 && vim_isxdigit(ptr[col + 1])) ||
5584 ( dobin
5585 && col > 0
5586 && (ptr[col] == 'B'
5587 || ptr[col] == 'b')
5588 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005589#ifdef FEAT_MBYTE
5590 && (!has_mbyte ||
5591 !(*mb_head_off)(ptr, ptr + col - 1))
5592#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005593 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005594 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005595 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005596 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005597#ifdef FEAT_MBYTE
5598 if (has_mbyte)
5599 col -= (*mb_head_off)(ptr, ptr + col);
5600#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005601 }
5602 else
5603 {
5604 /*
5605 * Search forward and then backward to find the start of number.
5606 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005607 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005608
5609 while (ptr[col] != NUL
5610 && !vim_isdigit(ptr[col])
5611 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005612 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005613
5614 while (col > 0
5615 && vim_isdigit(ptr[col - 1])
5616 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005617 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005618 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005619#ifdef FEAT_MBYTE
5620 if (has_mbyte)
5621 col -= (*mb_head_off)(ptr, ptr + col);
5622#endif
5623 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005624 }
5625 }
5626
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005627 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005628 {
5629 while (ptr[col] != NUL && length > 0
5630 && !vim_isdigit(ptr[col])
5631 && !(doalp && ASCII_ISALPHA(ptr[col])))
5632 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005633 int mb_len = MB_PTR2LEN(ptr + col);
5634
5635 col += mb_len;
5636 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005637 }
5638
5639 if (length == 0)
5640 goto theend;
5641
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005642 if (col > pos->col && ptr[col - 1] == '-'
5643#ifdef FEAT_MBYTE
5644 && (!has_mbyte ||
5645 !(*mb_head_off)(ptr, ptr + col - 1))
5646#endif
5647 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005648 {
5649 negative = TRUE;
5650 was_positive = FALSE;
5651 }
5652 }
5653
5654 /*
5655 * If a number was found, and saving for undo works, replace the number.
5656 */
5657 firstdigit = ptr[col];
5658 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5659 {
5660 beep_flush();
5661 goto theend;
5662 }
5663
5664 if (doalp && ASCII_ISALPHA(firstdigit))
5665 {
5666 /* decrement or increment alphabetic character */
5667 if (op_type == OP_NR_SUB)
5668 {
5669 if (CharOrd(firstdigit) < Prenum1)
5670 {
5671 if (isupper(firstdigit))
5672 firstdigit = 'A';
5673 else
5674 firstdigit = 'a';
5675 }
5676 else
5677#ifdef EBCDIC
5678 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5679#else
5680 firstdigit -= Prenum1;
5681#endif
5682 }
5683 else
5684 {
5685 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5686 {
5687 if (isupper(firstdigit))
5688 firstdigit = 'Z';
5689 else
5690 firstdigit = 'z';
5691 }
5692 else
5693#ifdef EBCDIC
5694 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5695#else
5696 firstdigit += Prenum1;
5697#endif
5698 }
5699 curwin->w_cursor.col = col;
5700 if (!did_change)
5701 startpos = curwin->w_cursor;
5702 did_change = TRUE;
5703 (void)del_char(FALSE);
5704 ins_char(firstdigit);
5705 endpos = curwin->w_cursor;
5706 curwin->w_cursor.col = col;
5707 }
5708 else
5709 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005710 if (col > 0 && ptr[col - 1] == '-'
5711#ifdef FEAT_MBYTE
5712 && (!has_mbyte ||
5713 !(*mb_head_off)(ptr, ptr + col - 1))
5714#endif
5715 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005716 {
5717 /* negative number */
5718 --col;
5719 negative = TRUE;
5720 }
5721 /* get the number value (unsigned) */
5722 if (visual && VIsual_mode != 'V')
5723 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5724 ? (int)STRLEN(ptr) - col
5725 : length);
5726
5727 vim_str2nr(ptr + col, &pre, &length,
5728 0 + (dobin ? STR2NR_BIN : 0)
5729 + (dooct ? STR2NR_OCT : 0)
5730 + (dohex ? STR2NR_HEX : 0),
5731 NULL, &n, maxlen);
5732
5733 /* ignore leading '-' for hex and octal and bin numbers */
5734 if (pre && negative)
5735 {
5736 ++col;
5737 --length;
5738 negative = FALSE;
5739 }
5740 /* add or subtract */
5741 subtract = FALSE;
5742 if (op_type == OP_NR_SUB)
5743 subtract ^= TRUE;
5744 if (negative)
5745 subtract ^= TRUE;
5746
5747 oldn = n;
5748 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005749 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005750 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005751 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005752 /* handle wraparound for decimal numbers */
5753 if (!pre)
5754 {
5755 if (subtract)
5756 {
5757 if (n > oldn)
5758 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005759 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005760 negative ^= TRUE;
5761 }
5762 }
5763 else
5764 {
5765 /* add */
5766 if (n < oldn)
5767 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005768 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005769 negative ^= TRUE;
5770 }
5771 }
5772 if (n == 0)
5773 negative = FALSE;
5774 }
5775
5776 if (visual && !was_positive && !negative && col > 0)
5777 {
5778 /* need to remove the '-' */
5779 col--;
5780 length++;
5781 }
5782
5783 /*
5784 * Delete the old number.
5785 */
5786 curwin->w_cursor.col = col;
5787 if (!did_change)
5788 startpos = curwin->w_cursor;
5789 did_change = TRUE;
5790 todel = length;
5791 c = gchar_cursor();
5792 /*
5793 * Don't include the '-' in the length, only the length of the
5794 * part after it is kept the same.
5795 */
5796 if (c == '-')
5797 --length;
5798 while (todel-- > 0)
5799 {
5800 if (c < 0x100 && isalpha(c))
5801 {
5802 if (isupper(c))
5803 hexupper = TRUE;
5804 else
5805 hexupper = FALSE;
5806 }
5807 /* del_char() will mark line needing displaying */
5808 (void)del_char(FALSE);
5809 c = gchar_cursor();
5810 }
5811
5812 /*
5813 * Prepare the leading characters in buf1[].
5814 * When there are many leading zeros it could be very long.
5815 * Allocate a bit too much.
5816 */
5817 buf1 = alloc((unsigned)length + NUMBUFLEN);
5818 if (buf1 == NULL)
5819 goto theend;
5820 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005821 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005822 {
5823 *ptr++ = '-';
5824 }
5825 if (pre)
5826 {
5827 *ptr++ = '0';
5828 --length;
5829 }
5830 if (pre == 'b' || pre == 'B' ||
5831 pre == 'x' || pre == 'X')
5832 {
5833 *ptr++ = pre;
5834 --length;
5835 }
5836
5837 /*
5838 * Put the number characters in buf2[].
5839 */
5840 if (pre == 'b' || pre == 'B')
5841 {
5842 int i;
5843 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005844 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005845
5846 /* leading zeros */
5847 for (bit = bits; bit > 0; bit--)
5848 if ((n >> (bit - 1)) & 0x1) break;
5849
5850 for (i = 0; bit > 0; bit--)
5851 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5852
5853 buf2[i] = '\0';
5854 }
5855 else if (pre == 0)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005856 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005857 else if (pre == '0')
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005858 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005859 else if (pre && hexupper)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005860 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005861 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005862 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005863 length -= (int)STRLEN(buf2);
5864
5865 /*
5866 * Adjust number of zeros to the new number of digits, so the
5867 * total length of the number remains the same.
5868 * Don't do this when
5869 * the result may look like an octal number.
5870 */
5871 if (firstdigit == '0' && !(dooct && pre == 0))
5872 while (length-- > 0)
5873 *ptr++ = '0';
5874 *ptr = NUL;
5875 STRCAT(buf1, buf2);
5876 ins_str(buf1); /* insert the new number */
5877 vim_free(buf1);
5878 endpos = curwin->w_cursor;
5879 if (did_change && curwin->w_cursor.col)
5880 --curwin->w_cursor.col;
5881 }
5882
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005883 if (did_change)
5884 {
5885 /* set the '[ and '] marks */
5886 curbuf->b_op_start = startpos;
5887 curbuf->b_op_end = endpos;
5888 if (curbuf->b_op_end.col > 0)
5889 --curbuf->b_op_end.col;
5890 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005891
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005892theend:
5893 if (visual)
5894 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005895 else if (did_change)
5896 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005897
Bram Moolenaard79e5502016-01-10 22:13:02 +01005898 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899}
5900
5901#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005902
5903static yankreg_T *y_read_regs = NULL;
5904
5905#define REG_PREVIOUS 1
5906#define REG_EXEC 2
5907
5908/*
5909 * Prepare for reading viminfo registers when writing viminfo later.
5910 */
5911 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005912prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005913{
5914 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
5915 * (int)sizeof(yankreg_T));
5916}
5917
5918 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005919finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005920{
5921 int i;
5922 int j;
5923
5924 if (y_read_regs != NULL)
5925 {
5926 for (i = 0; i < NUM_REGISTERS; ++i)
5927 if (y_read_regs[i].y_array != NULL)
5928 {
5929 for (j = 0; j < y_read_regs[i].y_size; j++)
5930 vim_free(y_read_regs[i].y_array[j]);
5931 vim_free(y_read_regs[i].y_array);
5932 }
5933 vim_free(y_read_regs);
5934 y_read_regs = NULL;
5935 }
5936}
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005939read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 int eof;
5942 int do_it = TRUE;
5943 int size;
5944 int limit;
5945 int i;
5946 int set_prev = FALSE;
5947 char_u *str;
5948 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005949 int new_type = MCHAR; /* init to shut up compiler */
5950 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
5952 /* We only get here (hopefully) if line[0] == '"' */
5953 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005954
5955 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (*str == '"')
5957 {
5958 set_prev = TRUE;
5959 str++;
5960 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if (!ASCII_ISALNUM(*str) && *str != '-')
5963 {
5964 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5965 return TRUE; /* too many errors, pretend end-of-file */
5966 do_it = FALSE;
5967 }
5968 get_yank_register(*str++, FALSE);
5969 if (!force && y_current->y_array != NULL)
5970 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005971
5972 if (*str == '@')
5973 {
5974 /* "x@: register x used for @@ */
5975 if (force || execreg_lastc == NUL)
5976 execreg_lastc = str[-1];
5977 }
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 size = 0;
5980 limit = 100; /* Optimized for registers containing <= 100 lines */
5981 if (do_it)
5982 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005983 /*
5984 * Build the new register in array[].
5985 * y_array is kept as-is until done.
5986 * The "do_it" flag is reset when something is wrong, in which case
5987 * array[] needs to be freed.
5988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 if (set_prev)
5990 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005991 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005992 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005994 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005996 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005998 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 /* get the block width; if it's missing we get a zero, which is OK */
6000 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006001 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003
6004 while (!(eof = viminfo_readline(virp))
6005 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6006 {
6007 if (do_it)
6008 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006009 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006011 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006013
6014 if (new_array == NULL)
6015 {
6016 do_it = FALSE;
6017 break;
6018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006020 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006022 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 }
6025 str = viminfo_readstring(virp, 1, TRUE);
6026 if (str != NULL)
6027 array[size++] = str;
6028 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006029 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 do_it = FALSE;
6031 }
6032 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (do_it)
6035 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006036 /* free y_array[] */
6037 for (i = 0; i < y_current->y_size; i++)
6038 vim_free(y_current->y_array[i]);
6039 vim_free(y_current->y_array);
6040
6041 y_current->y_type = new_type;
6042 y_current->y_width = new_width;
6043 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006044 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (size == 0)
6046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 y_current->y_array = NULL;
6048 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006049 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006051 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 y_current->y_array =
6053 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6054 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006055 {
6056 if (y_current->y_array == NULL)
6057 vim_free(array[i]);
6058 else
6059 y_current->y_array[i] = array[i];
6060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006063 else
6064 {
6065 /* Free array[] if it was filled. */
6066 for (i = 0; i < size; i++)
6067 vim_free(array[i]);
6068 }
6069 vim_free(array);
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 return eof;
6072}
6073
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006074/*
6075 * Accept a new style register line from the viminfo, store it when it's new.
6076 */
6077 void
6078handle_viminfo_register(garray_T *values, int force)
6079{
6080 bval_T *vp = (bval_T *)values->ga_data;
6081 int flags;
6082 int name;
6083 int type;
6084 int linecount;
6085 int width;
6086 time_t timestamp;
6087 yankreg_T *y_ptr;
6088 int i;
6089
6090 /* Check the format:
6091 * |{bartype},{flags},{name},{type},
6092 * {linecount},{width},{timestamp},"line1","line2"
6093 */
6094 if (values->ga_len < 6
6095 || vp[0].bv_type != BVAL_NR
6096 || vp[1].bv_type != BVAL_NR
6097 || vp[2].bv_type != BVAL_NR
6098 || vp[3].bv_type != BVAL_NR
6099 || vp[4].bv_type != BVAL_NR
6100 || vp[5].bv_type != BVAL_NR)
6101 return;
6102 flags = vp[0].bv_nr;
6103 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006104 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006105 return;
6106 type = vp[2].bv_nr;
6107 if (type != MCHAR && type != MLINE && type != MBLOCK)
6108 return;
6109 linecount = vp[3].bv_nr;
6110 if (values->ga_len < 6 + linecount)
6111 return;
6112 width = vp[4].bv_nr;
6113 if (width < 0)
6114 return;
6115
6116 if (y_read_regs != NULL)
6117 /* Reading viminfo for merging and writing. Store the register
6118 * content, don't update the current registers. */
6119 y_ptr = &y_read_regs[name];
6120 else
6121 y_ptr = &y_regs[name];
6122
6123 /* Do not overwrite unless forced or the timestamp is newer. */
6124 timestamp = (time_t)vp[5].bv_nr;
6125 if (y_ptr->y_array != NULL && !force
6126 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6127 return;
6128
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006129 if (y_ptr->y_array != NULL)
6130 for (i = 0; i < y_ptr->y_size; i++)
6131 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006132 vim_free(y_ptr->y_array);
6133
6134 if (y_read_regs == NULL)
6135 {
6136 if (flags & REG_PREVIOUS)
6137 y_previous = y_ptr;
6138 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6139 execreg_lastc = get_register_name(name);
6140 }
6141 y_ptr->y_type = type;
6142 y_ptr->y_width = width;
6143 y_ptr->y_size = linecount;
6144 y_ptr->y_time_set = timestamp;
6145 if (linecount == 0)
6146 y_ptr->y_array = NULL;
6147 else
6148 {
6149 y_ptr->y_array =
6150 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6151 for (i = 0; i < linecount; i++)
6152 {
6153 if (vp[i + 6].bv_allocated)
6154 {
6155 y_ptr->y_array[i] = vp[i + 6].bv_string;
6156 vp[i + 6].bv_string = NULL;
6157 }
6158 else
6159 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6160 }
6161 }
6162}
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006165write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006167 int i, j;
6168 char_u *type;
6169 char_u c;
6170 int num_lines;
6171 int max_num_lines;
6172 int max_kbyte;
6173 long len;
6174 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
Bram Moolenaar64404472010-06-26 06:24:45 +02006176 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 /* Get '<' value, use old '"' value if '<' is not found. */
6179 max_num_lines = get_viminfo_parameter('<');
6180 if (max_num_lines < 0)
6181 max_num_lines = get_viminfo_parameter('"');
6182 if (max_num_lines == 0)
6183 return;
6184 max_kbyte = get_viminfo_parameter('s');
6185 if (max_kbyte == 0)
6186 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 for (i = 0; i < NUM_REGISTERS; i++)
6189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190#ifdef FEAT_CLIPBOARD
6191 /* Skip '*'/'+' register, we don't want them back next time */
6192 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6193 continue;
6194#endif
6195#ifdef FEAT_DND
6196 /* Neither do we want the '~' register */
6197 if (i == TILDE_REGISTER)
6198 continue;
6199#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006200 /* When reading viminfo for merging and writing: Use the register from
6201 * viminfo if it's newer. */
6202 if (y_read_regs != NULL
6203 && y_read_regs[i].y_array != NULL
6204 && (y_regs[i].y_array == NULL ||
6205 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6206 y_ptr = &y_read_regs[i];
6207 else if (y_regs[i].y_array == NULL)
6208 continue;
6209 else
6210 y_ptr = &y_regs[i];
6211
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006212 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006213 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006214 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006215 || (num_lines == 1 && y_ptr->y_type == MCHAR
6216 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006217 continue;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (max_kbyte > 0)
6220 {
6221 /* Skip register if there is more text than the maximum size. */
6222 len = 0;
6223 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006224 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (len > (long)max_kbyte * 1024L)
6226 continue;
6227 }
6228
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006229 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
6231 case MLINE:
6232 type = (char_u *)"LINE";
6233 break;
6234 case MCHAR:
6235 type = (char_u *)"CHAR";
6236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 case MBLOCK:
6238 type = (char_u *)"BLOCK";
6239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 default:
6241 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006242 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 emsg(IObuff);
6244 type = (char_u *)"LINE";
6245 break;
6246 }
6247 if (y_previous == &y_regs[i])
6248 fprintf(fp, "\"");
6249 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006250 fprintf(fp, "\"%c", c);
6251 if (c == execreg_lastc)
6252 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006253 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 /* If max_num_lines < 0, then we save ALL the lines in the register */
6256 if (max_num_lines > 0 && num_lines > max_num_lines)
6257 num_lines = max_num_lines;
6258 for (j = 0; j < num_lines; j++)
6259 {
6260 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006261 viminfo_writestring(fp, y_ptr->y_array[j]);
6262 }
6263
6264 {
6265 int flags = 0;
6266 int remaining;
6267
6268 /* New style with a bar line. Format:
6269 * |{bartype},{flags},{name},{type},
6270 * {linecount},{width},{timestamp},"line1","line2"
6271 * flags: REG_PREVIOUS - register is y_previous
6272 * REG_EXEC - used for @@
6273 */
6274 if (y_previous == &y_regs[i])
6275 flags |= REG_PREVIOUS;
6276 if (c == execreg_lastc)
6277 flags |= REG_EXEC;
6278 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6279 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6280 (long)y_ptr->y_time_set);
6281 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6282 remaining = LSIZE - 71;
6283 for (j = 0; j < num_lines; j++)
6284 {
6285 putc(',', fp);
6286 --remaining;
6287 remaining = barline_writestring(fp, y_ptr->y_array[j],
6288 remaining);
6289 }
6290 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 }
6292 }
6293}
6294#endif /* FEAT_VIMINFO */
6295
6296#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6297/*
6298 * SELECTION / PRIMARY ('*')
6299 *
6300 * Text selection stuff that uses the GUI selection register '*'. When using a
6301 * GUI this may be text from another window, otherwise it is the last text we
6302 * had highlighted with VIsual mode. With mouse support, clicking the middle
6303 * button performs the paste, otherwise you will need to do <"*p>. "
6304 * If not under X, it is synonymous with the clipboard register '+'.
6305 *
6306 * X CLIPBOARD ('+')
6307 *
6308 * Text selection stuff that uses the GUI clipboard register '+'.
6309 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6310 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6311 * otherwise you will need to do <"+p>. "
6312 * If not under X, it is synonymous with the selection register '*'.
6313 */
6314
6315/*
6316 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006317 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 * only exist while the owning application exists, so we write to the
6319 * permanent (while X runs) store CUT_BUFFER0.
6320 * Dump the CLIPBOARD selection if we own it (it's logically the more
6321 * 'permanent' of the two), otherwise the PRIMARY one.
6322 * For now, use a hard-coded sanity limit of 1Mb of data.
6323 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006324#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006326x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 Display *dpy;
6329 char_u *str = NULL;
6330 long_u len = 0;
6331 int motion_type = -1;
6332
6333# ifdef FEAT_GUI
6334 if (gui.in_use)
6335 dpy = X_DISPLAY;
6336 else
6337# endif
6338# ifdef FEAT_XCLIPBOARD
6339 dpy = xterm_dpy;
6340# else
6341 return;
6342# endif
6343
6344 /* Get selection to export */
6345 if (clip_plus.owned)
6346 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6347 else if (clip_star.owned)
6348 motion_type = clip_convert_selection(&str, &len, &clip_star);
6349
6350 /* Check it's OK */
6351 if (dpy != NULL && str != NULL && motion_type >= 0
6352 && len < 1024*1024 && len > 0)
6353 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006354#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006355 int ok = TRUE;
6356
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006357 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6358 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6359 * encoding conversion usually doesn't work, so keep the text as-is.
6360 */
6361 if (has_mbyte)
6362 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006363 vimconv_T vc;
6364
6365 vc.vc_type = CONV_NONE;
6366 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6367 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006368 int intlen = len;
6369 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006370
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006371 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006372 conv_str = string_convert(&vc, str, &intlen);
6373 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006374 if (conv_str != NULL)
6375 {
6376 vim_free(str);
6377 str = conv_str;
6378 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006379 else
6380 {
6381 ok = FALSE;
6382 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006383 convert_setup(&vc, NULL, NULL);
6384 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006385 else
6386 {
6387 ok = FALSE;
6388 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006389 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006390
6391 /* Do not store the string if conversion failed. Better to use any
6392 * other selection than garbled text. */
6393 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006394#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006395 {
6396 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6397 XFlush(dpy);
6398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400
6401 vim_free(str);
6402}
6403#endif
6404
6405 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006406clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006408 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410 if (cbd == &clip_plus)
6411 y_current = &y_regs[PLUS_REGISTER];
6412 else
6413 y_current = &y_regs[STAR_REGISTER];
6414 free_yank_all();
6415 y_current->y_size = 0;
6416 y_current = y_ptr;
6417}
6418
6419/*
6420 * Get the selected text and put it in the gui selection register '*' or '+'.
6421 */
6422 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006425 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 pos_T old_visual;
6428 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 colnr_T old_curswant;
6430 int old_set_curswant;
6431 pos_T old_op_start, old_op_end;
6432 oparg_T oa;
6433 cmdarg_T ca;
6434
6435 if (cbd->owned)
6436 {
6437 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6438 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6439 return;
6440
6441 /* Get the text between clip_star.start & clip_star.end */
6442 old_y_previous = y_previous;
6443 old_y_current = y_current;
6444 old_cursor = curwin->w_cursor;
6445 old_curswant = curwin->w_curswant;
6446 old_set_curswant = curwin->w_set_curswant;
6447 old_op_start = curbuf->b_op_start;
6448 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 old_visual = VIsual;
6450 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 clear_oparg(&oa);
6452 oa.regname = (cbd == &clip_plus ? '+' : '*');
6453 oa.op_type = OP_YANK;
6454 vim_memset(&ca, 0, sizeof(ca));
6455 ca.oap = &oa;
6456 ca.cmdchar = 'y';
6457 ca.count1 = 1;
6458 ca.retval = CA_NO_ADJ_OP_END;
6459 do_pending_operator(&ca, 0, TRUE);
6460 y_previous = old_y_previous;
6461 y_current = old_y_current;
6462 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006463 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 curwin->w_curswant = old_curswant;
6465 curwin->w_set_curswant = old_set_curswant;
6466 curbuf->b_op_start = old_op_start;
6467 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 VIsual = old_visual;
6469 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006471 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 {
6473 clip_free_selection(cbd);
6474
6475 /* Try to get selected text from another window */
6476 clip_gen_request_selection(cbd);
6477 }
6478}
6479
Bram Moolenaard44347f2011-06-19 01:14:29 +02006480/*
6481 * Convert from the GUI selection string into the '*'/'+' register.
6482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006484clip_yank_selection(
6485 int type,
6486 char_u *str,
6487 long len,
6488 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006490 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492 if (cbd == &clip_plus)
6493 y_ptr = &y_regs[PLUS_REGISTER];
6494 else
6495 y_ptr = &y_regs[STAR_REGISTER];
6496
6497 clip_free_selection(cbd);
6498
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006499 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500}
6501
6502/*
6503 * Convert the '*'/'+' register into a GUI selection string returned in *str
6504 * with length *len.
6505 * Returns the motion type, or -1 for failure.
6506 */
6507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006508clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 char_u *p;
6511 int lnum;
6512 int i, j;
6513 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006514 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516 if (cbd == &clip_plus)
6517 y_ptr = &y_regs[PLUS_REGISTER];
6518 else
6519 y_ptr = &y_regs[STAR_REGISTER];
6520
6521#ifdef USE_CRNL
6522 eolsize = 2;
6523#else
6524 eolsize = 1;
6525#endif
6526
6527 *str = NULL;
6528 *len = 0;
6529 if (y_ptr->y_array == NULL)
6530 return -1;
6531
6532 for (i = 0; i < y_ptr->y_size; i++)
6533 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6534
6535 /*
6536 * Don't want newline character at end of last line if we're in MCHAR mode.
6537 */
6538 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6539 *len -= eolsize;
6540
6541 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6542 if (p == NULL)
6543 return -1;
6544 lnum = 0;
6545 for (i = 0, j = 0; i < (int)*len; i++, j++)
6546 {
6547 if (y_ptr->y_array[lnum][j] == '\n')
6548 p[i] = NUL;
6549 else if (y_ptr->y_array[lnum][j] == NUL)
6550 {
6551#ifdef USE_CRNL
6552 p[i++] = '\r';
6553#endif
6554#ifdef USE_CR
6555 p[i] = '\r';
6556#else
6557 p[i] = '\n';
6558#endif
6559 lnum++;
6560 j = -1;
6561 }
6562 else
6563 p[i] = y_ptr->y_array[lnum][j];
6564 }
6565 return y_ptr->y_type;
6566}
6567
6568
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569/*
6570 * If we have written to a clipboard register, send the text to the clipboard.
6571 */
6572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006573may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6576 {
6577 clip_own_selection(&clip_star);
6578 clip_gen_set_selection(&clip_star);
6579 }
6580 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6581 {
6582 clip_own_selection(&clip_plus);
6583 clip_gen_set_selection(&clip_plus);
6584 }
6585}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586
6587#endif /* FEAT_CLIPBOARD || PROTO */
6588
6589
6590#if defined(FEAT_DND) || defined(PROTO)
6591/*
6592 * Replace the contents of the '~' register with str.
6593 */
6594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006595dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006597 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598
6599 curr = y_current;
6600 y_current = &y_regs[TILDE_REGISTER];
6601 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006602 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 y_current = curr;
6604}
6605#endif
6606
6607
6608#if defined(FEAT_EVAL) || defined(PROTO)
6609/*
6610 * Return the type of a register.
6611 * Used for getregtype()
6612 * Returns MAUTO for error.
6613 */
6614 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006615get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 switch (regname)
6618 {
6619 case '%': /* file name */
6620 case '#': /* alternate file name */
6621 case '=': /* expression */
6622 case ':': /* last command line */
6623 case '/': /* last search-pattern */
6624 case '.': /* last inserted text */
6625#ifdef FEAT_SEARCHPATH
6626 case Ctrl_F: /* Filename under cursor */
6627 case Ctrl_P: /* Path under cursor, expand via "path" */
6628#endif
6629 case Ctrl_W: /* word under cursor */
6630 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6631 case '_': /* black hole: always empty */
6632 return MCHAR;
6633 }
6634
6635#ifdef FEAT_CLIPBOARD
6636 regname = may_get_selection(regname);
6637#endif
6638
Bram Moolenaar32b92012014-01-14 12:33:36 +01006639 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006640 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 get_yank_register(regname, FALSE);
6643
6644 if (y_current->y_array != NULL)
6645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (reglen != NULL && y_current->y_type == MBLOCK)
6647 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 return y_current->y_type;
6649 }
6650 return MAUTO;
6651}
6652
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006653static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006654
6655/*
6656 * When "flags" has GREG_LIST return a list with text "s".
6657 * Otherwise just return "s".
6658 */
6659 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006660getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006661{
6662 if (flags & GREG_LIST)
6663 {
6664 list_T *list = list_alloc();
6665
6666 if (list != NULL)
6667 {
6668 if (list_append_string(list, NULL, -1) == FAIL)
6669 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006670 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006671 return NULL;
6672 }
6673 list->lv_first->li_tv.vval.v_string = s;
6674 }
6675 return (char_u *)list;
6676 }
6677 return s;
6678}
6679
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680/*
6681 * Return the contents of a register as a single allocated string.
6682 * Used for "@r" in expressions and for getreg().
6683 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006684 * Flags:
6685 * GREG_NO_EXPR Do not allow expression register
6686 * GREG_EXPR_SRC For the expression register: return expression itself,
6687 * not the result of its evaluation.
6688 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 */
6690 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006691get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 long i;
6694 char_u *retval;
6695 int allocated;
6696 long len;
6697
6698 /* Don't allow using an expression register inside an expression */
6699 if (regname == '=')
6700 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006701 if (flags & GREG_NO_EXPR)
6702 return NULL;
6703 if (flags & GREG_EXPR_SRC)
6704 return getreg_wrap_one_line(get_expr_line_src(), flags);
6705 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 }
6707
6708 if (regname == '@') /* "@@" is used for unnamed register */
6709 regname = '"';
6710
6711 /* check for valid regname */
6712 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6713 return NULL;
6714
6715#ifdef FEAT_CLIPBOARD
6716 regname = may_get_selection(regname);
6717#endif
6718
6719 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6720 {
6721 if (retval == NULL)
6722 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006723 if (allocated)
6724 return getreg_wrap_one_line(retval, flags);
6725 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727
6728 get_yank_register(regname, FALSE);
6729 if (y_current->y_array == NULL)
6730 return NULL;
6731
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006732 if (flags & GREG_LIST)
6733 {
6734 list_T *list = list_alloc();
6735 int error = FALSE;
6736
6737 if (list == NULL)
6738 return NULL;
6739 for (i = 0; i < y_current->y_size; ++i)
6740 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6741 error = TRUE;
6742 if (error)
6743 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006744 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006745 return NULL;
6746 }
6747 return (char_u *)list;
6748 }
6749
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 /*
6751 * Compute length of resulting string.
6752 */
6753 len = 0;
6754 for (i = 0; i < y_current->y_size; ++i)
6755 {
6756 len += (long)STRLEN(y_current->y_array[i]);
6757 /*
6758 * Insert a newline between lines and after last line if
6759 * y_type is MLINE.
6760 */
6761 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6762 ++len;
6763 }
6764
6765 retval = lalloc(len + 1, TRUE);
6766
6767 /*
6768 * Copy the lines of the yank register into the string.
6769 */
6770 if (retval != NULL)
6771 {
6772 len = 0;
6773 for (i = 0; i < y_current->y_size; ++i)
6774 {
6775 STRCPY(retval + len, y_current->y_array[i]);
6776 len += (long)STRLEN(retval + len);
6777
6778 /*
6779 * Insert a NL between lines and after the last line if y_type is
6780 * MLINE.
6781 */
6782 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6783 retval[len++] = '\n';
6784 }
6785 retval[len] = NUL;
6786 }
6787
6788 return retval;
6789}
6790
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006791 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006792init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006793 int name,
6794 yankreg_T **old_y_previous,
6795 yankreg_T **old_y_current,
6796 int must_append,
6797 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006798{
6799 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6800 {
6801 emsg_invreg(name);
6802 return FAIL;
6803 }
6804
6805 /* Don't want to change the current (unnamed) register */
6806 *old_y_previous = y_previous;
6807 *old_y_current = y_current;
6808
6809 get_yank_register(name, TRUE);
6810 if (!y_append && !must_append)
6811 free_yank_all();
6812 return OK;
6813}
6814
6815 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006816finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006817 int name,
6818 yankreg_T *old_y_previous,
6819 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006820{
6821# ifdef FEAT_CLIPBOARD
6822 /* Send text of clipboard register to the clipboard. */
6823 may_set_selection();
6824# endif
6825
6826 /* ':let @" = "val"' should change the meaning of the "" register */
6827 if (name != '"')
6828 y_previous = old_y_previous;
6829 y_current = old_y_current;
6830}
6831
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832/*
6833 * Store string "str" in register "name".
6834 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6835 * If "must_append" is TRUE, always append to the register. Otherwise append
6836 * if "name" is an uppercase letter.
6837 * Note: "maxlen" and "must_append" don't work for the "/" register.
6838 * Careful: 'str' is modified, you may have to use a copy!
6839 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6840 */
6841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006842write_reg_contents(
6843 int name,
6844 char_u *str,
6845 int maxlen,
6846 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6849}
6850
6851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006852write_reg_contents_lst(
6853 int name,
6854 char_u **strings,
6855 int maxlen UNUSED,
6856 int must_append,
6857 int yank_type,
6858 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006859{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006860 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006861
6862 if (name == '/'
6863#ifdef FEAT_EVAL
6864 || name == '='
6865#endif
6866 )
6867 {
6868 char_u *s;
6869
6870 if (strings[0] == NULL)
6871 s = (char_u *)"";
6872 else if (strings[1] != NULL)
6873 {
6874 EMSG(_("E883: search pattern and expression register may not "
6875 "contain two or more lines"));
6876 return;
6877 }
6878 else
6879 s = strings[0];
6880 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6881 return;
6882 }
6883
6884 if (name == '_') /* black hole: nothing to do */
6885 return;
6886
6887 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6888 &yank_type) == FAIL)
6889 return;
6890
6891 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6892
6893 finish_write_reg(name, old_y_previous, old_y_current);
6894}
6895
6896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006897write_reg_contents_ex(
6898 int name,
6899 char_u *str,
6900 int maxlen,
6901 int must_append,
6902 int yank_type,
6903 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006905 yankreg_T *old_y_previous, *old_y_current;
6906 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
Bram Moolenaare7566042005-06-17 22:00:15 +00006908 if (maxlen >= 0)
6909 len = maxlen;
6910 else
6911 len = (long)STRLEN(str);
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 /* Special case: '/' search pattern */
6914 if (name == '/')
6915 {
6916 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6917 return;
6918 }
6919
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006920 if (name == '#')
6921 {
6922 buf_T *buf;
6923
6924 if (VIM_ISDIGIT(*str))
6925 {
6926 int num = atoi((char *)str);
6927
6928 buf = buflist_findnr(num);
6929 if (buf == NULL)
6930 EMSGN(_(e_nobufnr), (long)num);
6931 }
6932 else
6933 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6934 TRUE, FALSE, FALSE));
6935 if (buf == NULL)
6936 return;
6937 curwin->w_alt_fnum = buf->b_fnum;
6938 return;
6939 }
6940
Bram Moolenaare7566042005-06-17 22:00:15 +00006941#ifdef FEAT_EVAL
6942 if (name == '=')
6943 {
6944 char_u *p, *s;
6945
6946 p = vim_strnsave(str, (int)len);
6947 if (p == NULL)
6948 return;
6949 if (must_append)
6950 {
6951 s = concat_str(get_expr_line_src(), p);
6952 vim_free(p);
6953 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006954 }
6955 set_expr_line(p);
6956 return;
6957 }
6958#endif
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (name == '_') /* black hole: nothing to do */
6961 return;
6962
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006963 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6964 &yank_type) == FAIL)
6965 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006967 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006969 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970}
6971#endif /* FEAT_EVAL */
6972
6973#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6974/*
6975 * Put a string into a register. When the register is not empty, the string
6976 * is appended.
6977 */
6978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006979str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006980 yankreg_T *y_ptr, /* pointer to yank register */
6981 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6982 char_u *str, /* string to put in register */
6983 long len, /* length of string */
6984 long blocklen, /* width of Visual block */
6985 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006987 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 int lnum;
6989 long start;
6990 long i;
6991 int extra;
6992 int newlines; /* number of lines added */
6993 int extraline = 0; /* extra line at the end */
6994 int append = FALSE; /* append to last line in register */
6995 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006996 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007000 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 y_ptr->y_size = 0;
7002
Bram Moolenaard44347f2011-06-19 01:14:29 +02007003 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007004 type = ((str_list || (len > 0 && (str[len - 1] == NL
7005 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007006 ? MLINE : MCHAR);
7007 else
7008 type = yank_type;
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 /*
7011 * Count the number of lines within the string
7012 */
7013 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007014 if (str_list)
7015 {
7016 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007021 for (i = 0; i < len; i++)
7022 if (str[i] == '\n')
7023 ++newlines;
7024 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7025 {
7026 extraline = 1;
7027 ++newlines; /* count extra newline at the end */
7028 }
7029 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7030 {
7031 append = TRUE;
7032 --newlines; /* uncount newline when appending first line */
7033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007036 /* Without any lines make the register empty. */
7037 if (y_ptr->y_size + newlines == 0)
7038 {
7039 vim_free(y_ptr->y_array);
7040 y_ptr->y_array = NULL;
7041 return;
7042 }
7043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 /*
7045 * Allocate an array to hold the pointers to the new register lines.
7046 * If the register was not empty, move the existing lines to the new array.
7047 */
7048 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7049 * sizeof(char_u *), TRUE);
7050 if (pp == NULL) /* out of memory */
7051 return;
7052 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7053 pp[lnum] = y_ptr->y_array[lnum];
7054 vim_free(y_ptr->y_array);
7055 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
7058 /*
7059 * Find the end of each line and save it into the array.
7060 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007061 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007063 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7064 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007065 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007066 pp[lnum] = vim_strnsave(*ss, i);
7067 if (i > maxlen)
7068 maxlen = i;
7069 }
7070 }
7071 else
7072 {
7073 for (start = 0; start < len + extraline; start += i + 1)
7074 {
7075 for (i = start; i < len; ++i) /* find the end of the line */
7076 if (str[i] == '\n')
7077 break;
7078 i -= start; /* i is now length of line */
7079 if (i > maxlen)
7080 maxlen = i;
7081 if (append)
7082 {
7083 --lnum;
7084 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7085 }
7086 else
7087 extra = 0;
7088 s = alloc((unsigned)(i + extra + 1));
7089 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007091 if (extra)
7092 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7093 if (append)
7094 vim_free(y_ptr->y_array[lnum]);
7095 if (i)
7096 mch_memmove(s + extra, str + start, (size_t)i);
7097 extra += i;
7098 s[extra] = NUL;
7099 y_ptr->y_array[lnum++] = s;
7100 while (--extra >= 0)
7101 {
7102 if (*s == NUL)
7103 *s = '\n'; /* replace NUL with newline */
7104 ++s;
7105 }
7106 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
7109 y_ptr->y_type = type;
7110 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 if (type == MBLOCK)
7112 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7113 else
7114 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007115#ifdef FEAT_VIMINFO
7116 y_ptr->y_time_set = vim_time();
7117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
7119#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7120
7121 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007122clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
7124 vim_memset(oap, 0, sizeof(oparg_T));
7125}
7126
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007127static varnumber_T line_count_info(char_u *line, varnumber_T *wc, varnumber_T *cc, varnumber_T limit, int eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
7129/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007130 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 *
7132 * "Words" are counted by looking for boundaries between non-space and
7133 * space characters. (it seems to produce results that match 'wc'.)
7134 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007135 * Return value is byte count; word count for the line is added to "*wc".
7136 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 *
7138 * The function will only examine the first "limit" characters in the
7139 * line, stopping if it encounters an end-of-line (NUL byte). In that
7140 * case, eol_size will be added to the character count to account for
7141 * the size of the EOL character.
7142 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007143 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007144line_count_info(
7145 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007146 varnumber_T *wc,
7147 varnumber_T *cc,
7148 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007149 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007151 varnumber_T i;
7152 varnumber_T words = 0;
7153 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 int is_word = 0;
7155
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007156 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
7158 if (is_word)
7159 {
7160 if (vim_isspace(line[i]))
7161 {
7162 words++;
7163 is_word = 0;
7164 }
7165 }
7166 else if (!vim_isspace(line[i]))
7167 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007168 ++chars;
7169#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007170 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007171#else
7172 ++i;
7173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
7175
7176 if (is_word)
7177 words++;
7178 *wc += words;
7179
7180 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007181 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007184 chars += eol_size;
7185 }
7186 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 return i;
7188}
7189
7190/*
7191 * Give some info about the position of the cursor (for "g CTRL-G").
7192 * In Visual mode, give some info about the selected region. (In this case,
7193 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007194 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 */
7196 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007197cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007200 char_u buf1[50];
7201 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007203 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007204#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007205 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007206#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007207 varnumber_T byte_count_cursor = 0;
7208 varnumber_T char_count = 0;
7209 varnumber_T char_count_cursor = 0;
7210 varnumber_T word_count = 0;
7211 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007212 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007213 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 long line_count_selected = 0;
7215 pos_T min_pos, max_pos;
7216 oparg_T oparg;
7217 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219 /*
7220 * Compute the length of the file in characters.
7221 */
7222 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7223 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007224 if (dict == NULL)
7225 {
7226 MSG(_(no_lines_msg));
7227 return;
7228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
7230 else
7231 {
7232 if (get_fileformat(curbuf) == EOL_DOS)
7233 eol_size = 2;
7234 else
7235 eol_size = 1;
7236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (VIsual_active)
7238 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007239 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
7241 min_pos = VIsual;
7242 max_pos = curwin->w_cursor;
7243 }
7244 else
7245 {
7246 min_pos = curwin->w_cursor;
7247 max_pos = VIsual;
7248 }
7249 if (*p_sel == 'e' && max_pos.col > 0)
7250 --max_pos.col;
7251
7252 if (VIsual_mode == Ctrl_V)
7253 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007254#ifdef FEAT_LINEBREAK
7255 char_u * saved_sbr = p_sbr;
7256
7257 /* Make 'sbr' empty for a moment to get the correct size. */
7258 p_sbr = empty_option;
7259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 oparg.is_VIsual = 1;
7261 oparg.block_mode = TRUE;
7262 oparg.op_type = OP_NOP;
7263 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007264 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007265#ifdef FEAT_LINEBREAK
7266 p_sbr = saved_sbr;
7267#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007268 if (curwin->w_curswant == MAXCOL)
7269 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 /* Swap the start, end vcol if needed */
7271 if (oparg.end_vcol < oparg.start_vcol)
7272 {
7273 oparg.end_vcol += oparg.start_vcol;
7274 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7275 oparg.end_vcol -= oparg.start_vcol;
7276 }
7277 }
7278 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280
7281 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7282 {
7283 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007284 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
7286 ui_breakcheck();
7287 if (got_int)
7288 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007289 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 }
7291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 /* Do extra processing for VIsual mode. */
7293 if (VIsual_active
7294 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7295 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007296 char_u *s = NULL;
7297 long len = 0L;
7298
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 switch (VIsual_mode)
7300 {
7301 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007302#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007306#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007308#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007309 s = bd.textstart;
7310 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 break;
7312 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007313 s = ml_get(lnum);
7314 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 break;
7316 case 'v':
7317 {
7318 colnr_T start_col = (lnum == min_pos.lnum)
7319 ? min_pos.col : 0;
7320 colnr_T end_col = (lnum == max_pos.lnum)
7321 ? max_pos.col - start_col + 1 : MAXCOL;
7322
Bram Moolenaardef9e822004-12-31 20:58:58 +00007323 s = ml_get(lnum) + start_col;
7324 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 }
7326 break;
7327 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007328 if (s != NULL)
7329 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007330 byte_count_cursor += line_count_info(s, &word_count_cursor,
7331 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007332 if (lnum == curbuf->b_ml.ml_line_count
7333 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007334 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007335 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007336 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
7341 /* In non-visual mode, check for the line the cursor is on */
7342 if (lnum == curwin->w_cursor.lnum)
7343 {
7344 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007345 char_count_cursor += char_count;
7346 byte_count_cursor = byte_count +
7347 line_count_info(ml_get(lnum),
7348 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007349 (varnumber_T)(curwin->w_cursor.col + 1),
7350 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
7352 }
7353 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007354 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007355 &char_count, (varnumber_T)MAXCOL,
7356 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
7358
7359 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007360 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007361 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
Bram Moolenaared767a22016-01-03 22:49:16 +01007363 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007365 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007367 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7368 {
7369 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7370 &max_pos.col);
7371 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7372 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7373 }
7374 else
7375 buf1[0] = NUL;
7376
7377 if (char_count_cursor == byte_count_cursor
7378 && char_count == byte_count)
7379 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007380 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007381 buf1, line_count_selected,
7382 (long)curbuf->b_ml.ml_line_count,
7383 word_count_cursor, word_count,
7384 byte_count_cursor, byte_count);
7385 else
7386 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007387 _("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 +01007388 buf1, line_count_selected,
7389 (long)curbuf->b_ml.ml_line_count,
7390 word_count_cursor, word_count,
7391 char_count_cursor, char_count,
7392 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 }
7394 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007395 {
7396 p = ml_get_curline();
7397 validate_virtcol();
7398 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7399 (int)curwin->w_virtcol + 1);
7400 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7401 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
Bram Moolenaared767a22016-01-03 22:49:16 +01007403 if (char_count_cursor == byte_count_cursor
7404 && char_count == byte_count)
7405 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007406 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007407 (char *)buf1, (char *)buf2,
7408 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 (long)curbuf->b_ml.ml_line_count,
7410 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007411 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007412 else
7413 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007414 _("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 +01007415 (char *)buf1, (char *)buf2,
7416 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007417 (long)curbuf->b_ml.ml_line_count,
7418 word_count_cursor, word_count,
7419 char_count_cursor, char_count,
7420 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007421 }
7422 }
7423
Bram Moolenaared767a22016-01-03 22:49:16 +01007424#ifdef FEAT_MBYTE
7425 bom_count = bomb_size();
7426 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007427 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
7428 _("(+%ld for BOM)"), bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007429#endif
7430 if (dict == NULL)
7431 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007432 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007433 p = p_shm;
7434 p_shm = (char_u *)"";
7435 msg(IObuff);
7436 p_shm = p;
7437 }
7438 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007439#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007440 if (dict != NULL)
7441 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007442 dict_add_nr_str(dict, "words", word_count, NULL);
7443 dict_add_nr_str(dict, "chars", char_count, NULL);
7444 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007445# ifdef FEAT_MBYTE
7446 + bom_count
7447# endif
7448 , NULL);
7449 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007450 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007451 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007452 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007453 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007454 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457}