blob: d3e97f07f01c99ea0d01524db358d3df03386faa [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];
1639 y_previous = y_current = &y_regs[1];
1640 y_regs[1].y_array = NULL; /* set register one to empty */
1641}
1642
1643/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001644 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001646 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
1648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001649op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651 int n;
1652 linenr_T lnum;
1653 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 char_u *newp, *oldp;
1655 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1657 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001658 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1661 return OK;
1662
1663 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1664 if (oap->empty)
1665 return u_save_cursor();
1666
1667 if (!curbuf->b_p_ma)
1668 {
1669 EMSG(_(e_modifiable));
1670 return FAIL;
1671 }
1672
1673#ifdef FEAT_CLIPBOARD
1674 adjust_clip_reg(&oap->regname);
1675#endif
1676
1677#ifdef FEAT_MBYTE
1678 if (has_mbyte)
1679 mb_adjust_opend(oap);
1680#endif
1681
Bram Moolenaard04b7502010-07-08 22:27:55 +02001682 /*
1683 * Imitate the strange Vi behaviour: If the delete spans more than one
1684 * line and motion_type == MCHAR and the result is a blank line, make the
1685 * delete linewise. Don't do this for the change command or Visual mode.
1686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001689 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001691 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 && oap->op_type == OP_DELETE)
1693 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001694 ptr = ml_get(oap->end.lnum) + oap->end.col;
1695 if (*ptr != NUL)
1696 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 ptr = skipwhite(ptr);
1698 if (*ptr == NUL && inindent(0))
1699 oap->motion_type = MLINE;
1700 }
1701
Bram Moolenaard04b7502010-07-08 22:27:55 +02001702 /*
1703 * Check for trying to delete (e.g. "D") in an empty line.
1704 * Note: For the change operator it is ok.
1705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if ( oap->motion_type == MCHAR
1707 && oap->line_count == 1
1708 && oap->op_type == OP_DELETE
1709 && *ml_get(oap->start.lnum) == NUL)
1710 {
1711 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001712 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 * 'cpoptions' (Vi compatible).
1714 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001715#ifdef FEAT_VIRTUALEDIT
1716 if (virtual_op)
1717 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1718 * marks as if it happened. */
1719 goto setmarks;
1720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1722 beep_flush();
1723 return OK;
1724 }
1725
Bram Moolenaard04b7502010-07-08 22:27:55 +02001726 /*
1727 * Do a yank of whatever we're about to delete.
1728 * If a yank register was specified, put the deleted text into that
1729 * register. For the black hole register '_' don't yank anything.
1730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (oap->regname != '_')
1732 {
1733 if (oap->regname != 0)
1734 {
1735 /* check for read-only register */
1736 if (!valid_yank_reg(oap->regname, TRUE))
1737 {
1738 beep_flush();
1739 return OK;
1740 }
1741 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1742 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1743 did_yank = TRUE;
1744 }
1745
1746 /*
1747 * Put deleted text into register 1 and shift number registers if the
1748 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001749 * Use the register name from before adjust_clip_reg() may have
1750 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001752 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 || oap->line_count > 1 || oap->use_reg_one)
1754 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001755 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (op_yank(oap, TRUE, FALSE) == OK)
1757 did_yank = TRUE;
1758 }
1759
Bram Moolenaar84298db2012-04-20 13:46:08 +02001760 /* Yank into small delete register when no named register specified
1761 * and the delete is within one line. */
1762 if ((
1763#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001764 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1765 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001766#endif
1767 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && oap->line_count == 1)
1769 {
1770 oap->regname = '-';
1771 get_yank_register(oap->regname, TRUE);
1772 if (op_yank(oap, TRUE, FALSE) == OK)
1773 did_yank = TRUE;
1774 oap->regname = 0;
1775 }
1776
1777 /*
1778 * If there's too much stuff to fit in the yank register, then get a
1779 * confirmation before doing the delete. This is crude, but simple.
1780 * And it avoids doing a delete of something we can't put back if we
1781 * want.
1782 */
1783 if (!did_yank)
1784 {
1785 int msg_silent_save = msg_silent;
1786
1787 msg_silent = 0; /* must display the prompt */
1788 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1789 msg_silent = msg_silent_save;
1790 if (n != 'y')
1791 {
1792 EMSG(_(e_abort));
1793 return FAIL;
1794 }
1795 }
1796 }
1797
Bram Moolenaard04b7502010-07-08 22:27:55 +02001798 /*
1799 * block mode delete
1800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (oap->block_mode)
1802 {
1803 if (u_save((linenr_T)(oap->start.lnum - 1),
1804 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1805 return FAIL;
1806
1807 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1808 {
1809 block_prep(oap, &bd, lnum, TRUE);
1810 if (bd.textlen == 0) /* nothing to delete */
1811 continue;
1812
1813 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1814 if (lnum == curwin->w_cursor.lnum)
1815 {
1816 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1817# ifdef FEAT_VIRTUALEDIT
1818 curwin->w_cursor.coladd = 0;
1819# endif
1820 }
1821
1822 /* n == number of chars deleted
1823 * If we delete a TAB, it may be replaced by several characters.
1824 * Thus the number of characters may increase!
1825 */
1826 n = bd.textlen - bd.startspaces - bd.endspaces;
1827 oldp = ml_get(lnum);
1828 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1829 if (newp == NULL)
1830 continue;
1831 /* copy up to deleted part */
1832 mch_memmove(newp, oldp, (size_t)bd.textcol);
1833 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001834 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 (size_t)(bd.startspaces + bd.endspaces));
1836 /* copy the part after the deleted part */
1837 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001838 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /* replace the line */
1840 ml_replace(lnum, newp, FALSE);
1841 }
1842
1843 check_cursor_col();
1844 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1845 oap->end.lnum + 1, 0L);
1846 oap->line_count = 0; /* no lines deleted */
1847 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001848 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 if (oap->op_type == OP_CHANGE)
1851 {
1852 /* Delete the lines except the first one. Temporarily move the
1853 * cursor to the next line. Save the current line number, if the
1854 * last line is deleted it may be changed.
1855 */
1856 if (oap->line_count > 1)
1857 {
1858 lnum = curwin->w_cursor.lnum;
1859 ++curwin->w_cursor.lnum;
1860 del_lines((long)(oap->line_count - 1), TRUE);
1861 curwin->w_cursor.lnum = lnum;
1862 }
1863 if (u_save_cursor() == FAIL)
1864 return FAIL;
1865 if (curbuf->b_p_ai) /* don't delete indent */
1866 {
1867 beginline(BL_WHITE); /* cursor on first non-white */
1868 did_ai = TRUE; /* delete the indent when ESC hit */
1869 ai_col = curwin->w_cursor.col;
1870 }
1871 else
1872 beginline(0); /* cursor in column 0 */
1873 truncate_line(FALSE); /* delete the rest of the line */
1874 /* leave cursor past last char in line */
1875 if (oap->line_count > 1)
1876 u_clearline(); /* "U" command not possible after "2cc" */
1877 }
1878 else
1879 {
1880 del_lines(oap->line_count, TRUE);
1881 beginline(BL_WHITE | BL_FIX);
1882 u_clearline(); /* "U" command not possible after "dd" */
1883 }
1884 }
1885 else
1886 {
1887#ifdef FEAT_VIRTUALEDIT
1888 if (virtual_op)
1889 {
1890 int endcol = 0;
1891
1892 /* For virtualedit: break the tabs that are partly included. */
1893 if (gchar_pos(&oap->start) == '\t')
1894 {
1895 if (u_save_cursor() == FAIL) /* save first line for undo */
1896 return FAIL;
1897 if (oap->line_count == 1)
1898 endcol = getviscol2(oap->end.col, oap->end.coladd);
1899 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1900 oap->start = curwin->w_cursor;
1901 if (oap->line_count == 1)
1902 {
1903 coladvance(endcol);
1904 oap->end.col = curwin->w_cursor.col;
1905 oap->end.coladd = curwin->w_cursor.coladd;
1906 curwin->w_cursor = oap->start;
1907 }
1908 }
1909
1910 /* Break a tab only when it's included in the area. */
1911 if (gchar_pos(&oap->end) == '\t'
1912 && (int)oap->end.coladd < oap->inclusive)
1913 {
1914 /* save last line for undo */
1915 if (u_save((linenr_T)(oap->end.lnum - 1),
1916 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1917 return FAIL;
1918 curwin->w_cursor = oap->end;
1919 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1920 oap->end = curwin->w_cursor;
1921 curwin->w_cursor = oap->start;
1922 }
1923 }
1924#endif
1925
1926 if (oap->line_count == 1) /* delete characters within one line */
1927 {
1928 if (u_save_cursor() == FAIL) /* save line for undo */
1929 return FAIL;
1930
1931 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001932 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 && oap->op_type == OP_CHANGE
1934 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001935 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 display_dollar(oap->end.col - !oap->inclusive);
1937
1938 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1939
1940#ifdef FEAT_VIRTUALEDIT
1941 if (virtual_op)
1942 {
1943 /* fix up things for virtualedit-delete:
1944 * break the tabs which are going to get in our way
1945 */
1946 char_u *curline = ml_get_curline();
1947 int len = (int)STRLEN(curline);
1948
1949 if (oap->end.coladd != 0
1950 && (int)oap->end.col >= len - 1
1951 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1952 n++;
1953 /* Delete at least one char (e.g, when on a control char). */
1954 if (n == 0 && oap->start.coladd != oap->end.coladd)
1955 n = 1;
1956
1957 /* When deleted a char in the line, reset coladd. */
1958 if (gchar_cursor() != NUL)
1959 curwin->w_cursor.coladd = 0;
1960 }
1961#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001962 (void)del_bytes((long)n, !virtual_op,
1963 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 else /* delete characters between lines */
1966 {
1967 pos_T curpos;
1968
1969 /* save deleted and changed lines for undo */
1970 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1971 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1972 return FAIL;
1973
1974 truncate_line(TRUE); /* delete from cursor to end of line */
1975
1976 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1977 ++curwin->w_cursor.lnum;
1978 del_lines((long)(oap->line_count - 2), FALSE);
1979
Bram Moolenaard009e862015-06-09 20:20:03 +02001980 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001981 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001982 curwin->w_cursor.col = 0;
1983 (void)del_bytes((long)n, !virtual_op,
1984 oap->op_type == OP_DELETE && !oap->is_VIsual);
1985 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1986 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988 }
1989
1990 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992#ifdef FEAT_VIRTUALEDIT
1993setmarks:
1994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (oap->block_mode)
1996 {
1997 curbuf->b_op_end.lnum = oap->end.lnum;
1998 curbuf->b_op_end.col = oap->start.col;
1999 }
2000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 curbuf->b_op_end = oap->start;
2002 curbuf->b_op_start = oap->start;
2003
2004 return OK;
2005}
2006
2007#ifdef FEAT_MBYTE
2008/*
2009 * Adjust end of operating area for ending on a multi-byte character.
2010 * Used for deletion.
2011 */
2012 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002013mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014{
2015 char_u *p;
2016
2017 if (oap->inclusive)
2018 {
2019 p = ml_get(oap->end.lnum);
2020 oap->end.col += mb_tail_off(p, p + oap->end.col);
2021 }
2022}
2023#endif
2024
2025#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2026/*
2027 * Replace a whole area with one character.
2028 */
2029 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 int n, numc;
2033#ifdef FEAT_MBYTE
2034 int num_chars;
2035#endif
2036 char_u *newp, *oldp;
2037 size_t oldlen;
2038 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002039 char_u *after_p = NULL;
2040 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2043 return OK; /* nothing to do */
2044
Bram Moolenaard9820532013-11-04 01:41:17 +01002045 if (had_ctrl_v_cr)
2046 c = (c == -1 ? '\r' : '\n');
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048#ifdef FEAT_MBYTE
2049 if (has_mbyte)
2050 mb_adjust_opend(oap);
2051#endif
2052
2053 if (u_save((linenr_T)(oap->start.lnum - 1),
2054 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2055 return FAIL;
2056
2057 /*
2058 * block mode replace
2059 */
2060 if (oap->block_mode)
2061 {
2062 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2063 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2064 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002065 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2067 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2068 continue; /* nothing to replace */
2069
2070 /* n == number of extra chars required
2071 * If we split a TAB, it may be replaced by several characters.
2072 * Thus the number of characters may increase!
2073 */
2074#ifdef FEAT_VIRTUALEDIT
2075 /* If the range starts in virtual space, count the initial
2076 * coladd offset as part of "startspaces" */
2077 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2078 {
2079 pos_T vpos;
2080
Bram Moolenaara1381de2009-11-03 15:44:21 +00002081 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 getvpos(&vpos, oap->start_vcol);
2083 bd.startspaces += vpos.coladd;
2084 n = bd.startspaces;
2085 }
2086 else
2087#endif
2088 /* allow for pre spaces */
2089 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2090
2091 /* allow for post spp */
2092 n += (bd.endspaces
2093#ifdef FEAT_VIRTUALEDIT
2094 && !bd.is_oneChar
2095#endif
2096 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2097 /* Figure out how many characters to replace. */
2098 numc = oap->end_vcol - oap->start_vcol + 1;
2099 if (bd.is_short && (!virtual_op || bd.is_MAX))
2100 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2101
2102#ifdef FEAT_MBYTE
2103 /* A double-wide character can be replaced only up to half the
2104 * times. */
2105 if ((*mb_char2cells)(c) > 1)
2106 {
2107 if ((numc & 1) && !bd.is_short)
2108 {
2109 ++bd.endspaces;
2110 ++n;
2111 }
2112 numc = numc / 2;
2113 }
2114
2115 /* Compute bytes needed, move character count to num_chars. */
2116 num_chars = numc;
2117 numc *= (*mb_char2len)(c);
2118#endif
2119 /* oldlen includes textlen, so don't double count */
2120 n += numc - bd.textlen;
2121
2122 oldp = ml_get_curline();
2123 oldlen = STRLEN(oldp);
2124 newp = alloc_check((unsigned)oldlen + 1 + n);
2125 if (newp == NULL)
2126 continue;
2127 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2128 /* copy up to deleted part */
2129 mch_memmove(newp, oldp, (size_t)bd.textcol);
2130 oldp += bd.textcol + bd.textlen;
2131 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002132 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002134 /* -1/-2 is used for entering CR literally. */
2135 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002137#ifdef FEAT_MBYTE
2138 if (has_mbyte)
2139 {
2140 n = (int)STRLEN(newp);
2141 while (--num_chars >= 0)
2142 n += (*mb_char2bytes)(c, newp + n);
2143 }
2144 else
2145#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002146 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002147 if (!bd.is_short)
2148 {
2149 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002150 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002151 /* copy the part after the changed part */
2152 STRMOVE(newp + STRLEN(newp), oldp);
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
2155 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002157 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002158 after_p = alloc_check(
2159 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002160 if (after_p != NULL)
2161 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163 /* replace the line */
2164 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002165 if (after_p != NULL)
2166 {
2167 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2168 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2169 oap->end.lnum++;
2170 vim_free(after_p);
2171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173 }
2174 else
2175 {
2176 /*
2177 * MCHAR and MLINE motion replace.
2178 */
2179 if (oap->motion_type == MLINE)
2180 {
2181 oap->start.col = 0;
2182 curwin->w_cursor.col = 0;
2183 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2184 if (oap->end.col)
2185 --oap->end.col;
2186 }
2187 else if (!oap->inclusive)
2188 dec(&(oap->end));
2189
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002190 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192 n = gchar_cursor();
2193 if (n != NUL)
2194 {
2195#ifdef FEAT_MBYTE
2196 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2197 {
2198 /* This is slow, but it handles replacing a single-byte
2199 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002200 if (curwin->w_cursor.lnum == oap->end.lnum)
2201 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 n = State;
2203 State = REPLACE;
2204 ins_char(c);
2205 State = n;
2206 /* Backup to the replaced character. */
2207 dec_cursor();
2208 }
2209 else
2210#endif
2211 {
2212#ifdef FEAT_VIRTUALEDIT
2213 if (n == TAB)
2214 {
2215 int end_vcol = 0;
2216
2217 if (curwin->w_cursor.lnum == oap->end.lnum)
2218 {
2219 /* oap->end has to be recalculated when
2220 * the tab breaks */
2221 end_vcol = getviscol2(oap->end.col,
2222 oap->end.coladd);
2223 }
2224 coladvance_force(getviscol());
2225 if (curwin->w_cursor.lnum == oap->end.lnum)
2226 getvpos(&oap->end, end_vcol);
2227 }
2228#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002229 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231 }
2232#ifdef FEAT_VIRTUALEDIT
2233 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2234 {
2235 int virtcols = oap->end.coladd;
2236
2237 if (curwin->w_cursor.lnum == oap->start.lnum
2238 && oap->start.col == oap->end.col && oap->start.coladd)
2239 virtcols -= oap->start.coladd;
2240
2241 /* oap->end has been trimmed so it's effectively inclusive;
2242 * as a result an extra +1 must be counted so we don't
2243 * trample the NUL byte. */
2244 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2245 curwin->w_cursor.col -= (virtcols + 1);
2246 for (; virtcols >= 0; virtcols--)
2247 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002248 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (inc(&curwin->w_cursor) == -1)
2250 break;
2251 }
2252 }
2253#endif
2254
2255 /* Advance to next character, stop at the end of the file. */
2256 if (inc_cursor() == -1)
2257 break;
2258 }
2259 }
2260
2261 curwin->w_cursor = oap->start;
2262 check_cursor();
2263 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2264
2265 /* Set "'[" and "']" marks. */
2266 curbuf->b_op_start = oap->start;
2267 curbuf->b_op_end = oap->end;
2268
2269 return OK;
2270}
2271#endif
2272
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002273static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275/*
2276 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2277 */
2278 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002279op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002283 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 if (u_save((linenr_T)(oap->start.lnum - 1),
2286 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2287 return;
2288
2289 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (oap->block_mode) /* Visual block mode */
2291 {
2292 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2293 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002294 int one_change;
2295
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 block_prep(oap, &bd, pos.lnum, FALSE);
2297 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002298 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2299 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002300
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002301#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002302 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2305
Bram Moolenaar009b2592004-10-24 19:18:58 +00002306 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2307 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002309 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313 if (did_change)
2314 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2315 }
2316 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318 if (oap->motion_type == MLINE)
2319 {
2320 oap->start.col = 0;
2321 pos.col = 0;
2322 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2323 if (oap->end.col)
2324 --oap->end.col;
2325 }
2326 else if (!oap->inclusive)
2327 dec(&(oap->end));
2328
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002329 if (pos.lnum == oap->end.lnum)
2330 did_change = swapchars(oap->op_type, &pos,
2331 oap->end.col - pos.col + 1);
2332 else
2333 for (;;)
2334 {
2335 did_change |= swapchars(oap->op_type, &pos,
2336 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2337 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002338 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002339 break;
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (did_change)
2342 {
2343 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2344 0L);
2345#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002346 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 char_u *ptr;
2349 int count;
2350
2351 pos = oap->start;
2352 while (pos.lnum < oap->end.lnum)
2353 {
2354 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002355 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002356 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002358 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 pos.col = 0;
2360 pos.lnum++;
2361 }
2362 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2363 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002364 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002366 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368#endif
2369 }
2370 }
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (!did_change && oap->is_VIsual)
2373 /* No change: need to remove the Visual selection */
2374 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 /*
2377 * Set '[ and '] marks.
2378 */
2379 curbuf->b_op_start = oap->start;
2380 curbuf->b_op_end = oap->end;
2381
2382 if (oap->line_count > p_report)
2383 {
2384 if (oap->line_count == 1)
2385 MSG(_("1 line changed"));
2386 else
2387 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2388 }
2389}
2390
2391/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002392 * Invoke swapchar() on "length" bytes at position "pos".
2393 * "pos" is advanced to just after the changed characters.
2394 * "length" is rounded up to include the whole last multi-byte character.
2395 * Also works correctly when the number of bytes changes.
2396 * Returns TRUE if some character was changed.
2397 */
2398 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002399swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002400{
2401 int todo;
2402 int did_change = 0;
2403
2404 for (todo = length; todo > 0; --todo)
2405 {
2406# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002407 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002408 {
2409 int len = (*mb_ptr2len)(ml_get_pos(pos));
2410
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002411 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002412 if (len > 0)
2413 todo -= len - 1;
2414 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002415# endif
2416 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002417 if (inc(pos) == -1) /* at end of file */
2418 break;
2419 }
2420 return did_change;
2421}
2422
2423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 * If op_type == OP_UPPER: make uppercase,
2425 * if op_type == OP_LOWER: make lowercase,
2426 * if op_type == OP_ROT13: do rot13 encoding,
2427 * else swap case of character at 'pos'
2428 * returns TRUE when something actually changed.
2429 */
2430 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002431swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 int c;
2434 int nc;
2435
2436 c = gchar_pos(pos);
2437
2438 /* Only do rot13 encoding for ASCII characters. */
2439 if (c >= 0x80 && op_type == OP_ROT13)
2440 return FALSE;
2441
2442#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002443 if (op_type == OP_UPPER && c == 0xdf
2444 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002445 {
2446 pos_T sp = curwin->w_cursor;
2447
2448 /* Special handling of German sharp s: change to "SS". */
2449 curwin->w_cursor = *pos;
2450 del_char(FALSE);
2451 ins_char('S');
2452 ins_char('S');
2453 curwin->w_cursor = sp;
2454 inc(pos);
2455 }
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2458 return FALSE;
2459#endif
2460 nc = c;
2461 if (MB_ISLOWER(c))
2462 {
2463 if (op_type == OP_ROT13)
2464 nc = ROT13(c, 'a');
2465 else if (op_type != OP_LOWER)
2466 nc = MB_TOUPPER(c);
2467 }
2468 else if (MB_ISUPPER(c))
2469 {
2470 if (op_type == OP_ROT13)
2471 nc = ROT13(c, 'A');
2472 else if (op_type != OP_UPPER)
2473 nc = MB_TOLOWER(c);
2474 }
2475 if (nc != c)
2476 {
2477#ifdef FEAT_MBYTE
2478 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2479 {
2480 pos_T sp = curwin->w_cursor;
2481
2482 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002483 /* don't use del_char(), it also removes composing chars */
2484 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 ins_char(nc);
2486 curwin->w_cursor = sp;
2487 }
2488 else
2489#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002490 PCHAR(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 return TRUE;
2492 }
2493 return FALSE;
2494}
2495
2496#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2497/*
2498 * op_insert - Insert and append operators for Visual mode.
2499 */
2500 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002501op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 long ins_len, pre_textlen = 0;
2504 char_u *firstline, *ins_text;
2505 struct block_def bd;
2506 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002507 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 /* edit() changes this - record it for OP_APPEND */
2510 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2511
2512 /* vis block is still marked. Get rid of it now. */
2513 curwin->w_cursor.lnum = oap->start.lnum;
2514 update_screen(INVERTED);
2515
2516 if (oap->block_mode)
2517 {
2518#ifdef FEAT_VIRTUALEDIT
2519 /* When 'virtualedit' is used, need to insert the extra spaces before
2520 * doing block_prep(). When only "block" is used, virtual edit is
2521 * already disabled, but still need it when calling
2522 * coladvance_force(). */
2523 if (curwin->w_cursor.coladd > 0)
2524 {
2525 int old_ve_flags = ve_flags;
2526
2527 ve_flags = VE_ALL;
2528 if (u_save_cursor() == FAIL)
2529 return;
2530 coladvance_force(oap->op_type == OP_APPEND
2531 ? oap->end_vcol + 1 : getviscol());
2532 if (oap->op_type == OP_APPEND)
2533 --curwin->w_cursor.col;
2534 ve_flags = old_ve_flags;
2535 }
2536#endif
2537 /* Get the info about the block before entering the text */
2538 block_prep(oap, &bd, oap->start.lnum, TRUE);
2539 firstline = ml_get(oap->start.lnum) + bd.textcol;
2540 if (oap->op_type == OP_APPEND)
2541 firstline += bd.textlen;
2542 pre_textlen = (long)STRLEN(firstline);
2543 }
2544
2545 if (oap->op_type == OP_APPEND)
2546 {
2547 if (oap->block_mode
2548#ifdef FEAT_VIRTUALEDIT
2549 && curwin->w_cursor.coladd == 0
2550#endif
2551 )
2552 {
2553 /* Move the cursor to the character right of the block. */
2554 curwin->w_set_curswant = TRUE;
2555 while (*ml_get_cursor() != NUL
2556 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2557 ++curwin->w_cursor.col;
2558 if (bd.is_short && !bd.is_MAX)
2559 {
2560 /* First line was too short, make it longer and adjust the
2561 * values in "bd". */
2562 if (u_save_cursor() == FAIL)
2563 return;
2564 for (i = 0; i < bd.endspaces; ++i)
2565 ins_char(' ');
2566 bd.textlen += bd.endspaces;
2567 }
2568 }
2569 else
2570 {
2571 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002572 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002575 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 && oap->start_vcol != oap->end_vcol)
2577 inc_cursor();
2578 }
2579 }
2580
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002581 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002582 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002584 /* When a tab was inserted, and the characters in front of the tab
2585 * have been converted to a tab as well, the column of the cursor
2586 * might have actually been reduced, so need to adjust here. */
2587 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002588 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002589 oap->start = curbuf->b_op_start_orig;
2590
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002591 /* If user has moved off this line, we don't know what to do, so do
2592 * nothing.
2593 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2594 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return;
2596
2597 if (oap->block_mode)
2598 {
2599 struct block_def bd2;
2600
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002601 /* The user may have moved the cursor before inserting something, try
2602 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002603 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002604 {
2605 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002606 && oap->start.col
2607#ifdef FEAT_VIRTUALEDIT
2608 + oap->start.coladd
2609#endif
2610 != curbuf->b_op_start_orig.col
2611#ifdef FEAT_VIRTUALEDIT
2612 + curbuf->b_op_start_orig.coladd
2613#endif
2614 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002615 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002616 int t = getviscol2(curbuf->b_op_start_orig.col,
2617 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002618 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002619 pre_textlen -= t - oap->start_vcol;
2620 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002621 }
2622 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002623 && oap->end.col
2624#ifdef FEAT_VIRTUALEDIT
2625 + oap->end.coladd
2626#endif
2627 >= curbuf->b_op_start_orig.col
2628#ifdef FEAT_VIRTUALEDIT
2629 + curbuf->b_op_start_orig.coladd
2630#endif
2631 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002632 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002633 int t = getviscol2(curbuf->b_op_start_orig.col,
2634 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002635 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002636 /* reset pre_textlen to the value of OP_INSERT */
2637 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002638 pre_textlen -= t - oap->start_vcol;
2639 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002640 oap->op_type = OP_INSERT;
2641 }
2642 }
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /*
2645 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002646 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * Don't do this when "$" used, end-of-line will have changed.
2648 */
2649 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2650 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2651 {
2652 if (oap->op_type == OP_APPEND)
2653 {
2654 pre_textlen += bd2.textlen - bd.textlen;
2655 if (bd2.endspaces)
2656 --bd2.textlen;
2657 }
2658 bd.textcol = bd2.textcol;
2659 bd.textlen = bd2.textlen;
2660 }
2661
2662 /*
2663 * Subsequent calls to ml_get() flush the firstline data - take a
2664 * copy of the required string.
2665 */
2666 firstline = ml_get(oap->start.lnum) + bd.textcol;
2667 if (oap->op_type == OP_APPEND)
2668 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002669 if (pre_textlen >= 0
2670 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672 ins_text = vim_strnsave(firstline, (int)ins_len);
2673 if (ins_text != NULL)
2674 {
2675 /* block handled here */
2676 if (u_save(oap->start.lnum,
2677 (linenr_T)(oap->end.lnum + 1)) == OK)
2678 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2679 &bd);
2680
2681 curwin->w_cursor.col = oap->start.col;
2682 check_cursor();
2683 vim_free(ins_text);
2684 }
2685 }
2686 }
2687}
2688#endif
2689
2690/*
2691 * op_change - handle a change operation
2692 *
2693 * return TRUE if edit() returns because of a CTRL-O command
2694 */
2695 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002696op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 colnr_T l;
2699 int retval;
2700#ifdef FEAT_VISUALEXTRA
2701 long offset;
2702 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002703 long ins_len;
2704 long pre_textlen = 0;
2705 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 char_u *firstline;
2707 char_u *ins_text, *newp, *oldp;
2708 struct block_def bd;
2709#endif
2710
2711 l = oap->start.col;
2712 if (oap->motion_type == MLINE)
2713 {
2714 l = 0;
2715#ifdef FEAT_SMARTINDENT
2716 if (!p_paste && curbuf->b_p_si
2717# ifdef FEAT_CINDENT
2718 && !curbuf->b_p_cin
2719# endif
2720 )
2721 can_si = TRUE; /* It's like opening a new line, do si */
2722#endif
2723 }
2724
2725 /* First delete the text in the region. In an empty buffer only need to
2726 * save for undo */
2727 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2728 {
2729 if (u_save_cursor() == FAIL)
2730 return FALSE;
2731 }
2732 else if (op_delete(oap) == FAIL)
2733 return FALSE;
2734
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002735 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 && !virtual_op)
2737 inc_cursor();
2738
2739#ifdef FEAT_VISUALEXTRA
2740 /* check for still on same line (<CR> in inserted text meaningless) */
2741 /* skip blank lines too */
2742 if (oap->block_mode)
2743 {
2744# ifdef FEAT_VIRTUALEDIT
2745 /* Add spaces before getting the current line length. */
2746 if (virtual_op && (curwin->w_cursor.coladd > 0
2747 || gchar_cursor() == NUL))
2748 coladvance_force(getviscol());
2749# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002750 firstline = ml_get(oap->start.lnum);
2751 pre_textlen = (long)STRLEN(firstline);
2752 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 bd.textcol = curwin->w_cursor.col;
2754 }
2755#endif
2756
2757#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2758 if (oap->motion_type == MLINE)
2759 fix_indent();
2760#endif
2761
2762 retval = edit(NUL, FALSE, (linenr_T)1);
2763
2764#ifdef FEAT_VISUALEXTRA
2765 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002766 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002768 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002770 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002772 /* Auto-indenting may have changed the indent. If the cursor was past
2773 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002775 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002777 long new_indent = (long)(skipwhite(firstline) - firstline);
2778
2779 pre_textlen += new_indent - pre_indent;
2780 bd.textcol += new_indent - pre_indent;
2781 }
2782
2783 ins_len = (long)STRLEN(firstline) - pre_textlen;
2784 if (ins_len > 0)
2785 {
2786 /* Subsequent calls to ml_get() flush the firstline data - take a
2787 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2789 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002790 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2792 linenr++)
2793 {
2794 block_prep(oap, &bd, linenr, TRUE);
2795 if (!bd.is_short || virtual_op)
2796 {
2797# ifdef FEAT_VIRTUALEDIT
2798 pos_T vpos;
2799
2800 /* If the block starts in virtual space, count the
2801 * initial coladd offset as part of "startspaces" */
2802 if (bd.is_short)
2803 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002804 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 else
2808 vpos.coladd = 0;
2809# endif
2810 oldp = ml_get(linenr);
2811 newp = alloc_check((unsigned)(STRLEN(oldp)
2812# ifdef FEAT_VIRTUALEDIT
2813 + vpos.coladd
2814# endif
2815 + ins_len + 1));
2816 if (newp == NULL)
2817 continue;
2818 /* copy up to block start */
2819 mch_memmove(newp, oldp, (size_t)bd.textcol);
2820 offset = bd.textcol;
2821# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002822 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 offset += vpos.coladd;
2824# endif
2825 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2826 offset += ins_len;
2827 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002828 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 ml_replace(linenr, newp, FALSE);
2830 }
2831 }
2832 check_cursor();
2833
2834 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2835 }
2836 vim_free(ins_text);
2837 }
2838 }
2839#endif
2840
2841 return retval;
2842}
2843
2844/*
2845 * set all the yank registers to empty (called from main())
2846 */
2847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002848init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850 int i;
2851
2852 for (i = 0; i < NUM_REGISTERS; ++i)
2853 y_regs[i].y_array = NULL;
2854}
2855
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002856#if defined(EXITFREE) || defined(PROTO)
2857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002858clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002859{
2860 int i;
2861
2862 for (i = 0; i < NUM_REGISTERS; ++i)
2863 {
2864 y_current = &y_regs[i];
2865 if (y_current->y_array != NULL)
2866 free_yank_all();
2867 }
2868}
2869#endif
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871/*
2872 * Free "n" lines from the current yank register.
2873 * Called for normal freeing and in case of error.
2874 */
2875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002876free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
2878 if (y_current->y_array != NULL)
2879 {
2880 long i;
2881
2882 for (i = n; --i >= 0; )
2883 {
2884#ifdef AMIGA /* only for very slow machines */
2885 if ((i & 1023) == 1023) /* this may take a while */
2886 {
2887 /*
2888 * This message should never cause a hit-return message.
2889 * Overwrite this message with any next message.
2890 */
2891 ++no_wait_return;
2892 smsg((char_u *)_("freeing %ld lines"), i + 1);
2893 --no_wait_return;
2894 msg_didout = FALSE;
2895 msg_col = 0;
2896 }
2897#endif
2898 vim_free(y_current->y_array[i]);
2899 }
2900 vim_free(y_current->y_array);
2901 y_current->y_array = NULL;
2902#ifdef AMIGA
2903 if (n >= 1000)
2904 MSG("");
2905#endif
2906 }
2907}
2908
2909 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002910free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
2912 free_yank(y_current->y_size);
2913}
2914
2915/*
2916 * Yank the text between "oap->start" and "oap->end" into a yank register.
2917 * If we are to append (uppercase register), we first yank into a new yank
2918 * register and then concatenate the old and the new one (so we keep the old
2919 * one in case of out-of-memory).
2920 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002921 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 */
2923 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002924op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002927 yankreg_T *curr; /* copy of y_current */
2928 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 char_u **new_ptr;
2930 linenr_T lnum; /* current line number */
2931 long j;
2932 int yanktype = oap->motion_type;
2933 long yanklines = oap->line_count;
2934 linenr_T yankendlnum = oap->end.lnum;
2935 char_u *p;
2936 char_u *pnew;
2937 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002938#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002939 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 /* check for read-only register */
2943 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2944 {
2945 beep_flush();
2946 return FAIL;
2947 }
2948 if (oap->regname == '_') /* black hole: nothing to do */
2949 return OK;
2950
2951#ifdef FEAT_CLIPBOARD
2952 if (!clip_star.available && oap->regname == '*')
2953 oap->regname = 0;
2954 else if (!clip_plus.available && oap->regname == '+')
2955 oap->regname = 0;
2956#endif
2957
2958 if (!deleting) /* op_delete() already set y_current */
2959 get_yank_register(oap->regname, TRUE);
2960
2961 curr = y_current;
2962 /* append to existing contents */
2963 if (y_append && y_current->y_array != NULL)
2964 y_current = &newreg;
2965 else
2966 free_yank_all(); /* free previously yanked lines */
2967
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002968 /*
2969 * If the cursor was in column 1 before and after the movement, and the
2970 * operator is not inclusive, the yank is always linewise.
2971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if ( oap->motion_type == MCHAR
2973 && oap->start.col == 0
2974 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002976 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 && oap->end.col == 0
2978 && yanklines > 1)
2979 {
2980 yanktype = MLINE;
2981 --yankendlnum;
2982 --yanklines;
2983 }
2984
2985 y_current->y_size = yanklines;
2986 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2989 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (y_current->y_array == NULL)
2991 {
2992 y_current = curr;
2993 return FAIL;
2994 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002995#ifdef FEAT_VIMINFO
2996 y_current->y_time_set = vim_time();
2997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
2999 y_idx = 0;
3000 lnum = oap->start.lnum;
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (oap->block_mode)
3003 {
3004 /* Visual block mode */
3005 y_current->y_type = MBLOCK; /* set the yank register type */
3006 y_current->y_width = oap->end_vcol - oap->start_vcol;
3007
3008 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3009 y_current->y_width--;
3010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3013 {
3014 switch (y_current->y_type)
3015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 case MBLOCK:
3017 block_prep(oap, &bd, lnum, FALSE);
3018 if (yank_copy_line(&bd, y_idx) == FAIL)
3019 goto fail;
3020 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 case MLINE:
3023 if ((y_current->y_array[y_idx] =
3024 vim_strsave(ml_get(lnum))) == NULL)
3025 goto fail;
3026 break;
3027
3028 case MCHAR:
3029 {
3030 colnr_T startcol = 0, endcol = MAXCOL;
3031#ifdef FEAT_VIRTUALEDIT
3032 int is_oneChar = FALSE;
3033 colnr_T cs, ce;
3034#endif
3035 p = ml_get(lnum);
3036 bd.startspaces = 0;
3037 bd.endspaces = 0;
3038
3039 if (lnum == oap->start.lnum)
3040 {
3041 startcol = oap->start.col;
3042#ifdef FEAT_VIRTUALEDIT
3043 if (virtual_op)
3044 {
3045 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3046 if (ce != cs && oap->start.coladd > 0)
3047 {
3048 /* Part of a tab selected -- but don't
3049 * double-count it. */
3050 bd.startspaces = (ce - cs + 1)
3051 - oap->start.coladd;
3052 startcol++;
3053 }
3054 }
3055#endif
3056 }
3057
3058 if (lnum == oap->end.lnum)
3059 {
3060 endcol = oap->end.col;
3061#ifdef FEAT_VIRTUALEDIT
3062 if (virtual_op)
3063 {
3064 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3065 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3066# ifdef FEAT_MBYTE
3067 /* Don't add space for double-wide
3068 * char; endcol will be on last byte
3069 * of multi-byte char. */
3070 && (*mb_head_off)(p, p + endcol) == 0
3071# endif
3072 ))
3073 {
3074 if (oap->start.lnum == oap->end.lnum
3075 && oap->start.col == oap->end.col)
3076 {
3077 /* Special case: inside a single char */
3078 is_oneChar = TRUE;
3079 bd.startspaces = oap->end.coladd
3080 - oap->start.coladd + oap->inclusive;
3081 endcol = startcol;
3082 }
3083 else
3084 {
3085 bd.endspaces = oap->end.coladd
3086 + oap->inclusive;
3087 endcol -= oap->inclusive;
3088 }
3089 }
3090 }
3091#endif
3092 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003093 if (endcol == MAXCOL)
3094 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 if (startcol > endcol
3096#ifdef FEAT_VIRTUALEDIT
3097 || is_oneChar
3098#endif
3099 )
3100 bd.textlen = 0;
3101 else
3102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 bd.textlen = endcol - startcol + oap->inclusive;
3104 }
3105 bd.textstart = p + startcol;
3106 if (yank_copy_line(&bd, y_idx) == FAIL)
3107 goto fail;
3108 break;
3109 }
3110 /* NOTREACHED */
3111 }
3112 }
3113
3114 if (curr != y_current) /* append the new block to the old block */
3115 {
3116 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3117 (curr->y_size + y_current->y_size)), TRUE);
3118 if (new_ptr == NULL)
3119 goto fail;
3120 for (j = 0; j < curr->y_size; ++j)
3121 new_ptr[j] = curr->y_array[j];
3122 vim_free(curr->y_array);
3123 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003124#ifdef FEAT_VIMINFO
3125 curr->y_time_set = vim_time();
3126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3129 curr->y_type = MLINE;
3130
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003131 /* Concatenate the last line of the old block with the first line of
3132 * the new block, unless being Vi compatible. */
3133 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
3135 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3136 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3137 if (pnew == NULL)
3138 {
3139 y_idx = y_current->y_size - 1;
3140 goto fail;
3141 }
3142 STRCPY(pnew, curr->y_array[--j]);
3143 STRCAT(pnew, y_current->y_array[0]);
3144 vim_free(curr->y_array[j]);
3145 vim_free(y_current->y_array[0]);
3146 curr->y_array[j++] = pnew;
3147 y_idx = 1;
3148 }
3149 else
3150 y_idx = 0;
3151 while (y_idx < y_current->y_size)
3152 curr->y_array[j++] = y_current->y_array[y_idx++];
3153 curr->y_size = j;
3154 vim_free(y_current->y_array);
3155 y_current = curr;
3156 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003157 if (curwin->w_p_rnu)
3158 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (mess) /* Display message about yank? */
3160 {
3161 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 && yanklines == 1)
3164 yanklines = 0;
3165 /* Some versions of Vi use ">=" here, some don't... */
3166 if (yanklines > p_report)
3167 {
3168 /* redisplay now, so message is not deleted */
3169 update_topline_redraw();
3170 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003171 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003172 if (oap->block_mode)
3173 MSG(_("block of 1 line yanked"));
3174 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003175 MSG(_("1 line yanked"));
3176 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003177 else if (oap->block_mode)
3178 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
3180 smsg((char_u *)_("%ld lines yanked"), yanklines);
3181 }
3182 }
3183
3184 /*
3185 * Set "'[" and "']" marks.
3186 */
3187 curbuf->b_op_start = oap->start;
3188 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003189 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003190 {
3191 curbuf->b_op_start.col = 0;
3192 curbuf->b_op_end.col = MAXCOL;
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195#ifdef FEAT_CLIPBOARD
3196 /*
3197 * If we were yanking to the '*' register, send result to clipboard.
3198 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3199 * to the '*' register.
3200 */
3201 if (clip_star.available
3202 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003203 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003204 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 if (curr != &(y_regs[STAR_REGISTER]))
3207 /* Copy the text from register 0 to the clipboard register. */
3208 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3209
3210 clip_own_selection(&clip_star);
3211 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003212# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003213 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216
3217# ifdef FEAT_X11
3218 /*
3219 * If we were yanking to the '+' register, send result to selection.
3220 * Also copy to the '*' register, in case auto-select is off.
3221 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003222 if (clip_plus.available
3223 && (curr == &(y_regs[PLUS_REGISTER])
3224 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003225 && ((clip_unnamed | clip_unnamed_saved) &
3226 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003228 if (curr != &(y_regs[PLUS_REGISTER]))
3229 /* Copy the text from register 0 to the clipboard register. */
3230 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 clip_own_selection(&clip_plus);
3233 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003234 if (!clip_isautosel_star() && !did_star
3235 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
3237 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3238 clip_own_selection(&clip_star);
3239 clip_gen_set_selection(&clip_star);
3240 }
3241 }
3242# endif
3243#endif
3244
3245 return OK;
3246
3247fail: /* free the allocated lines */
3248 free_yank(y_idx + 1);
3249 y_current = curr;
3250 return FAIL;
3251}
3252
3253 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 char_u *pnew;
3257
3258 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3259 == NULL)
3260 return FAIL;
3261 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003262 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 pnew += bd->startspaces;
3264 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3265 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003266 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 pnew += bd->endspaces;
3268 *pnew = NUL;
3269 return OK;
3270}
3271
3272#ifdef FEAT_CLIPBOARD
3273/*
3274 * Make a copy of the y_current register to register "reg".
3275 */
3276 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003277copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003279 yankreg_T *curr = y_current;
3280 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
3282 y_current = reg;
3283 free_yank_all();
3284 *y_current = *curr;
3285 y_current->y_array = (char_u **)lalloc_clear(
3286 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3287 if (y_current->y_array == NULL)
3288 y_current->y_size = 0;
3289 else
3290 for (j = 0; j < y_current->y_size; ++j)
3291 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3292 {
3293 free_yank(j);
3294 y_current->y_size = 0;
3295 break;
3296 }
3297 y_current = curr;
3298}
3299#endif
3300
3301/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003302 * Put contents of register "regname" into the text.
3303 * Caller must check "regname" to be valid!
3304 * "flags": PUT_FIXINDENT make indent look nice
3305 * PUT_CURSEND leave cursor after end of new text
3306 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 */
3308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003309do_put(
3310 int regname,
3311 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3312 long count,
3313 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 char_u *ptr;
3316 char_u *newp, *oldp;
3317 int yanklen;
3318 int totlen = 0; /* init for gcc */
3319 linenr_T lnum;
3320 colnr_T col;
3321 long i; /* index in y_array[] */
3322 int y_type;
3323 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int oldlen;
3325 long y_width = 0;
3326 colnr_T vcol;
3327 int delcount;
3328 int incr = 0;
3329 long j;
3330 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 char_u **y_array = NULL;
3332 long nr_lines = 0;
3333 pos_T new_cursor;
3334 int indent;
3335 int orig_indent = 0; /* init for gcc */
3336 int indent_diff = 0; /* init for gcc */
3337 int first_indent = TRUE;
3338 int lendiff = 0;
3339 pos_T old_pos;
3340 char_u *insert_string = NULL;
3341 int allocated = FALSE;
3342 long cnt;
3343
3344#ifdef FEAT_CLIPBOARD
3345 /* Adjust register name for "unnamed" in 'clipboard'. */
3346 adjust_clip_reg(&regname);
3347 (void)may_get_selection(regname);
3348#endif
3349
3350 if (flags & PUT_FIXINDENT)
3351 orig_indent = get_indent();
3352
3353 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3354 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3355
3356 /*
3357 * Using inserted text works differently, because the register includes
3358 * special characters (newlines, etc.).
3359 */
3360 if (regname == '.')
3361 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003362 if (VIsual_active)
3363 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3365 (count == -1 ? 'O' : 'i')), count, FALSE);
3366 /* Putting the text is done later, so can't really move the cursor to
3367 * the next character. Use "l" to simulate it. */
3368 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3369 stuffcharReadbuff('l');
3370 return;
3371 }
3372
3373 /*
3374 * For special registers '%' (file name), '#' (alternate file name) and
3375 * ':' (last command line), etc. we have to create a fake yank register.
3376 */
3377 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3378 {
3379 if (insert_string == NULL)
3380 return;
3381 }
3382
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003383#ifdef FEAT_AUTOCMD
3384 /* Autocommands may be executed when saving lines for undo, which may make
3385 * y_array invalid. Start undo now to avoid that. */
3386 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3387#endif
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 if (insert_string != NULL)
3390 {
3391 y_type = MCHAR;
3392#ifdef FEAT_EVAL
3393 if (regname == '=')
3394 {
3395 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003396 * characters.
3397 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 for (;;)
3399 {
3400 y_size = 0;
3401 ptr = insert_string;
3402 while (ptr != NULL)
3403 {
3404 if (y_array != NULL)
3405 y_array[y_size] = ptr;
3406 ++y_size;
3407 ptr = vim_strchr(ptr, '\n');
3408 if (ptr != NULL)
3409 {
3410 if (y_array != NULL)
3411 *ptr = NUL;
3412 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003413 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (*ptr == NUL)
3415 {
3416 y_type = MLINE;
3417 break;
3418 }
3419 }
3420 }
3421 if (y_array != NULL)
3422 break;
3423 y_array = (char_u **)alloc((unsigned)
3424 (y_size * sizeof(char_u *)));
3425 if (y_array == NULL)
3426 goto end;
3427 }
3428 }
3429 else
3430#endif
3431 {
3432 y_size = 1; /* use fake one-line yank register */
3433 y_array = &insert_string;
3434 }
3435 }
3436 else
3437 {
3438 get_yank_register(regname, FALSE);
3439
3440 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 y_size = y_current->y_size;
3443 y_array = y_current->y_array;
3444 }
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (y_type == MLINE)
3447 {
3448 if (flags & PUT_LINE_SPLIT)
3449 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003450 char_u *p;
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 /* "p" or "P" in Visual mode: split the lines to put the text in
3453 * between. */
3454 if (u_save_cursor() == FAIL)
3455 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003456 p = ml_get_cursor();
3457 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003458 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003459 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (ptr == NULL)
3461 goto end;
3462 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3463 vim_free(ptr);
3464
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003465 oldp = ml_get_curline();
3466 p = oldp + curwin->w_cursor.col;
3467 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003468 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003469 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (ptr == NULL)
3471 goto end;
3472 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3473 ++nr_lines;
3474 dir = FORWARD;
3475 }
3476 if (flags & PUT_LINE_FORWARD)
3477 {
3478 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003479 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 dir = FORWARD;
3481 }
3482 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3483 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3487 y_type = MLINE;
3488
3489 if (y_size == 0 || y_array == NULL)
3490 {
3491 EMSG2(_("E353: Nothing in register %s"),
3492 regname == 0 ? (char_u *)"\"" : transchar(regname));
3493 goto end;
3494 }
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (y_type == MBLOCK)
3497 {
3498 lnum = curwin->w_cursor.lnum + y_size + 1;
3499 if (lnum > curbuf->b_ml.ml_line_count)
3500 lnum = curbuf->b_ml.ml_line_count + 1;
3501 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3502 goto end;
3503 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003504 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 lnum = curwin->w_cursor.lnum;
3507#ifdef FEAT_FOLDING
3508 /* Correct line number for closed fold. Don't move the cursor yet,
3509 * u_save() uses it. */
3510 if (dir == BACKWARD)
3511 (void)hasFolding(lnum, &lnum, NULL);
3512 else
3513 (void)hasFolding(lnum, NULL, &lnum);
3514#endif
3515 if (dir == FORWARD)
3516 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003517 /* In an empty buffer the empty line is going to be replaced, include
3518 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003519 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 goto end;
3521#ifdef FEAT_FOLDING
3522 if (dir == FORWARD)
3523 curwin->w_cursor.lnum = lnum - 1;
3524 else
3525 curwin->w_cursor.lnum = lnum;
3526 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3527#endif
3528 }
3529 else if (u_save_cursor() == FAIL)
3530 goto end;
3531
3532 yanklen = (int)STRLEN(y_array[0]);
3533
3534#ifdef FEAT_VIRTUALEDIT
3535 if (ve_flags == VE_ALL && y_type == MCHAR)
3536 {
3537 if (gchar_cursor() == TAB)
3538 {
3539 /* Don't need to insert spaces when "p" on the last position of a
3540 * tab or "P" on the first position. */
3541 if (dir == FORWARD
3542 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3543 : curwin->w_cursor.coladd > 0)
3544 coladvance_force(getviscol());
3545 else
3546 curwin->w_cursor.coladd = 0;
3547 }
3548 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3549 coladvance_force(getviscol() + (dir == FORWARD));
3550 }
3551#endif
3552
3553 lnum = curwin->w_cursor.lnum;
3554 col = curwin->w_cursor.col;
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /*
3557 * Block mode
3558 */
3559 if (y_type == MBLOCK)
3560 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003561 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 colnr_T endcol2 = 0;
3563
3564 if (dir == FORWARD && c != NUL)
3565 {
3566#ifdef FEAT_VIRTUALEDIT
3567 if (ve_flags == VE_ALL)
3568 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3569 else
3570#endif
3571 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3572
3573#ifdef FEAT_MBYTE
3574 if (has_mbyte)
3575 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003576 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 else
3578#endif
3579#ifdef FEAT_VIRTUALEDIT
3580 if (c != TAB || ve_flags != VE_ALL)
3581#endif
3582 ++curwin->w_cursor.col;
3583 ++col;
3584 }
3585 else
3586 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3587
3588#ifdef FEAT_VIRTUALEDIT
3589 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003590 if (ve_flags == VE_ALL
3591 && (curwin->w_cursor.coladd > 0
3592 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 if (dir == FORWARD && c == NUL)
3595 ++col;
3596 if (dir != FORWARD && c != NUL)
3597 ++curwin->w_cursor.col;
3598 if (c == TAB)
3599 {
3600 if (dir == BACKWARD && curwin->w_cursor.col)
3601 curwin->w_cursor.col--;
3602 if (dir == FORWARD && col - 1 == endcol2)
3603 curwin->w_cursor.col++;
3604 }
3605 }
3606 curwin->w_cursor.coladd = 0;
3607#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003608 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 for (i = 0; i < y_size; ++i)
3610 {
3611 int spaces;
3612 char shortline;
3613
3614 bd.startspaces = 0;
3615 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 vcol = 0;
3617 delcount = 0;
3618
3619 /* add a new line */
3620 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3621 {
3622 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3623 (colnr_T)1, FALSE) == FAIL)
3624 break;
3625 ++nr_lines;
3626 }
3627 /* get the old line and advance to the position to insert at */
3628 oldp = ml_get_curline();
3629 oldlen = (int)STRLEN(oldp);
3630 for (ptr = oldp; vcol < col && *ptr; )
3631 {
3632 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003633 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 vcol += incr;
3635 }
3636 bd.textcol = (colnr_T)(ptr - oldp);
3637
3638 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3639
3640 if (vcol < col) /* line too short, padd with spaces */
3641 bd.startspaces = col - vcol;
3642 else if (vcol > col)
3643 {
3644 bd.endspaces = vcol - col;
3645 bd.startspaces = incr - bd.endspaces;
3646 --bd.textcol;
3647 delcount = 1;
3648#ifdef FEAT_MBYTE
3649 if (has_mbyte)
3650 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3651#endif
3652 if (oldp[bd.textcol] != TAB)
3653 {
3654 /* Only a Tab can be split into spaces. Other
3655 * characters will have to be moved to after the
3656 * block, causing misalignment. */
3657 delcount = 0;
3658 bd.endspaces = 0;
3659 }
3660 }
3661
3662 yanklen = (int)STRLEN(y_array[i]);
3663
3664 /* calculate number of spaces required to fill right side of block*/
3665 spaces = y_width + 1;
3666 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003667 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (spaces < 0)
3669 spaces = 0;
3670
3671 /* insert the new text */
3672 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3673 newp = alloc_check((unsigned)totlen + oldlen + 1);
3674 if (newp == NULL)
3675 break;
3676 /* copy part up to cursor to new line */
3677 ptr = newp;
3678 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3679 ptr += bd.textcol;
3680 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003681 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 ptr += bd.startspaces;
3683 /* insert the new text */
3684 for (j = 0; j < count; ++j)
3685 {
3686 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3687 ptr += yanklen;
3688
3689 /* insert block's trailing spaces only if there's text behind */
3690 if ((j < count - 1 || !shortline) && spaces)
3691 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003692 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 ptr += spaces;
3694 }
3695 }
3696 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003697 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 ptr += bd.endspaces;
3699 /* move the text after the cursor to the end of the line. */
3700 mch_memmove(ptr, oldp + bd.textcol + delcount,
3701 (size_t)(oldlen - bd.textcol - delcount + 1));
3702 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3703
3704 ++curwin->w_cursor.lnum;
3705 if (i == 0)
3706 curwin->w_cursor.col += bd.startspaces;
3707 }
3708
3709 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3710
3711 /* Set '[ mark. */
3712 curbuf->b_op_start = curwin->w_cursor;
3713 curbuf->b_op_start.lnum = lnum;
3714
3715 /* adjust '] mark */
3716 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3717 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003718# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (flags & PUT_CURSEND)
3722 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003723 colnr_T len;
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 curwin->w_cursor = curbuf->b_op_end;
3726 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003727
3728 /* in Insert mode we might be after the NUL, correct for that */
3729 len = (colnr_T)STRLEN(ml_get_curline());
3730 if (curwin->w_cursor.col > len)
3731 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733 else
3734 curwin->w_cursor.lnum = lnum;
3735 }
3736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
3738 /*
3739 * Character or Line mode
3740 */
3741 if (y_type == MCHAR)
3742 {
3743 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3744 * char */
3745 if (dir == FORWARD && gchar_cursor() != NUL)
3746 {
3747#ifdef FEAT_MBYTE
3748 if (has_mbyte)
3749 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003750 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 /* put it on the next of the multi-byte character. */
3753 col += bytelen;
3754 if (yanklen)
3755 {
3756 curwin->w_cursor.col += bytelen;
3757 curbuf->b_op_end.col += bytelen;
3758 }
3759 }
3760 else
3761#endif
3762 {
3763 ++col;
3764 if (yanklen)
3765 {
3766 ++curwin->w_cursor.col;
3767 ++curbuf->b_op_end.col;
3768 }
3769 }
3770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 curbuf->b_op_start = curwin->w_cursor;
3772 }
3773 /*
3774 * Line mode: BACKWARD is the same as FORWARD on the previous line
3775 */
3776 else if (dir == BACKWARD)
3777 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003778 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 /*
3781 * simple case: insert into current line
3782 */
3783 if (y_type == MCHAR && y_size == 1)
3784 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003785 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003786
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003787 if (VIsual_active)
3788 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003789 end_lnum = curbuf->b_visual.vi_end.lnum;
3790 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3791 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003792 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003793
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003794 do {
3795 totlen = count * yanklen;
3796 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003798 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003799 if (VIsual_active && col > (int)STRLEN(oldp))
3800 {
3801 lnum++;
3802 continue;
3803 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003804 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3805 if (newp == NULL)
3806 goto end; /* alloc() gave an error message */
3807 mch_memmove(newp, oldp, (size_t)col);
3808 ptr = newp + col;
3809 for (i = 0; i < count; ++i)
3810 {
3811 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3812 ptr += yanklen;
3813 }
3814 STRMOVE(ptr, oldp + col);
3815 ml_replace(lnum, newp, FALSE);
3816 /* Place cursor on last putted char. */
3817 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003818 {
3819 /* make sure curwin->w_virtcol is updated */
3820 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003821 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003824 if (VIsual_active)
3825 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003826 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003827
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003828 if (VIsual_active) /* reset lnum to the last visual line */
3829 lnum--;
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 curbuf->b_op_end = curwin->w_cursor;
3832 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3833 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3834 ++curwin->w_cursor.col;
3835 changed_bytes(lnum, col);
3836 }
3837 else
3838 {
3839 /*
3840 * Insert at least one line. When y_type is MCHAR, break the first
3841 * line in two.
3842 */
3843 for (cnt = 1; cnt <= count; ++cnt)
3844 {
3845 i = 0;
3846 if (y_type == MCHAR)
3847 {
3848 /*
3849 * Split the current line in two at the insert position.
3850 * First insert y_array[size - 1] in front of second line.
3851 * Then append y_array[0] to first line.
3852 */
3853 lnum = new_cursor.lnum;
3854 ptr = ml_get(lnum) + col;
3855 totlen = (int)STRLEN(y_array[y_size - 1]);
3856 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3857 if (newp == NULL)
3858 goto error;
3859 STRCPY(newp, y_array[y_size - 1]);
3860 STRCAT(newp, ptr);
3861 /* insert second line */
3862 ml_append(lnum, newp, (colnr_T)0, FALSE);
3863 vim_free(newp);
3864
3865 oldp = ml_get(lnum);
3866 newp = alloc_check((unsigned)(col + yanklen + 1));
3867 if (newp == NULL)
3868 goto error;
3869 /* copy first part of line */
3870 mch_memmove(newp, oldp, (size_t)col);
3871 /* append to first line */
3872 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3873 ml_replace(lnum, newp, FALSE);
3874
3875 curwin->w_cursor.lnum = lnum;
3876 i = 1;
3877 }
3878
3879 for (; i < y_size; ++i)
3880 {
3881 if ((y_type != MCHAR || i < y_size - 1)
3882 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3883 == FAIL)
3884 goto error;
3885 lnum++;
3886 ++nr_lines;
3887 if (flags & PUT_FIXINDENT)
3888 {
3889 old_pos = curwin->w_cursor;
3890 curwin->w_cursor.lnum = lnum;
3891 ptr = ml_get(lnum);
3892 if (cnt == count && i == y_size - 1)
3893 lendiff = (int)STRLEN(ptr);
3894#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3895 if (*ptr == '#' && preprocs_left())
3896 indent = 0; /* Leave # lines at start */
3897 else
3898#endif
3899 if (*ptr == NUL)
3900 indent = 0; /* Ignore empty lines */
3901 else if (first_indent)
3902 {
3903 indent_diff = orig_indent - get_indent();
3904 indent = orig_indent;
3905 first_indent = FALSE;
3906 }
3907 else if ((indent = get_indent() + indent_diff) < 0)
3908 indent = 0;
3909 (void)set_indent(indent, 0);
3910 curwin->w_cursor = old_pos;
3911 /* remember how many chars were removed */
3912 if (cnt == count && i == y_size - 1)
3913 lendiff -= (int)STRLEN(ml_get(lnum));
3914 }
3915 }
3916 }
3917
3918error:
3919 /* Adjust marks. */
3920 if (y_type == MLINE)
3921 {
3922 curbuf->b_op_start.col = 0;
3923 if (dir == FORWARD)
3924 curbuf->b_op_start.lnum++;
3925 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003926 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003927 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02003928 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003929 < curbuf->b_ml.ml_line_count
3930#ifdef FEAT_DIFF
3931 || curwin->w_p_diff
3932#endif
3933 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003934 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 (linenr_T)MAXLNUM, nr_lines, 0L);
3936
3937 /* note changed text for displaying and folding */
3938 if (y_type == MCHAR)
3939 changed_lines(curwin->w_cursor.lnum, col,
3940 curwin->w_cursor.lnum + 1, nr_lines);
3941 else
3942 changed_lines(curbuf->b_op_start.lnum, 0,
3943 curbuf->b_op_start.lnum, nr_lines);
3944
3945 /* put '] mark at last inserted character */
3946 curbuf->b_op_end.lnum = lnum;
3947 /* correct length for change in indent */
3948 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3949 if (col > 1)
3950 curbuf->b_op_end.col = col - 1;
3951 else
3952 curbuf->b_op_end.col = 0;
3953
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003954 if (flags & PUT_CURSLINE)
3955 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003956 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003957 curwin->w_cursor.lnum = lnum;
3958 beginline(BL_WHITE | BL_FIX);
3959 }
3960 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
3962 /* put cursor after inserted text */
3963 if (y_type == MLINE)
3964 {
3965 if (lnum >= curbuf->b_ml.ml_line_count)
3966 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3967 else
3968 curwin->w_cursor.lnum = lnum + 1;
3969 curwin->w_cursor.col = 0;
3970 }
3971 else
3972 {
3973 curwin->w_cursor.lnum = lnum;
3974 curwin->w_cursor.col = col;
3975 }
3976 }
3977 else if (y_type == MLINE)
3978 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003979 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 curwin->w_cursor.col = 0;
3981 if (dir == FORWARD)
3982 ++curwin->w_cursor.lnum;
3983 beginline(BL_WHITE | BL_FIX);
3984 }
3985 else /* put cursor on first inserted character */
3986 curwin->w_cursor = new_cursor;
3987 }
3988 }
3989
3990 msgmore(nr_lines);
3991 curwin->w_set_curswant = TRUE;
3992
3993end:
3994 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003996 if (regname == '=')
3997 vim_free(y_array);
3998
Bram Moolenaar033d8882013-09-25 23:24:57 +02003999 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004000
Bram Moolenaar677ee682005-01-27 14:41:15 +00004001 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004002 adjust_cursor_eol();
4003}
4004
4005/*
4006 * When the cursor is on the NUL past the end of the line and it should not be
4007 * there move it left.
4008 */
4009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004010adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004011{
4012 if (curwin->w_cursor.col > 0
4013 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004014#ifdef FEAT_VIRTUALEDIT
4015 && (ve_flags & VE_ONEMORE) == 0
4016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 && !(restart_edit || (State & INSERT)))
4018 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004019 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004020 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#ifdef FEAT_VIRTUALEDIT
4023 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004024 {
4025 colnr_T scol, ecol;
4026
4027 /* Coladd is set to the width of the last character. */
4028 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4029 curwin->w_cursor.coladd = ecol - scol + 1;
4030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031#endif
4032 }
4033}
4034
4035#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4036/*
4037 * Return TRUE if lines starting with '#' should be left aligned.
4038 */
4039 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004040preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
4042 return
4043# ifdef FEAT_SMARTINDENT
4044# ifdef FEAT_CINDENT
4045 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4046# else
4047 curbuf->b_p_si
4048# endif
4049# endif
4050# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004051 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4052 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053# endif
4054 ;
4055}
4056#endif
4057
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004058/*
4059 * Return the character name of the register with the given number.
4060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004062get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063{
4064 if (num == -1)
4065 return '"';
4066 else if (num < 10)
4067 return num + '0';
4068 else if (num == DELETION_REGISTER)
4069 return '-';
4070#ifdef FEAT_CLIPBOARD
4071 else if (num == STAR_REGISTER)
4072 return '*';
4073 else if (num == PLUS_REGISTER)
4074 return '+';
4075#endif
4076 else
4077 {
4078#ifdef EBCDIC
4079 int i;
4080
4081 /* EBCDIC is really braindead ... */
4082 i = 'a' + (num - 10);
4083 if (i > 'i')
4084 i += 7;
4085 if (i > 'r')
4086 i += 8;
4087 return i;
4088#else
4089 return num + 'a' - 10;
4090#endif
4091 }
4092}
4093
4094/*
4095 * ":dis" and ":registers": Display the contents of the yank registers.
4096 */
4097 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004098ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004100 int i, n;
4101 long j;
4102 char_u *p;
4103 yankreg_T *yb;
4104 int name;
4105 int attr;
4106 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004107#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004108 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004109#else
4110# define clen 1
4111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 if (arg != NULL && *arg == NUL)
4114 arg = NULL;
4115 attr = hl_attr(HLF_8);
4116
4117 /* Highlight title */
4118 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4119 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4120 {
4121 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004122 if (arg != NULL && vim_strchr(arg, name) == NULL
4123#ifdef ONE_CLIPBOARD
4124 /* Star register and plus register contain the same thing. */
4125 && (name != '*' || vim_strchr(arg, '+') == NULL)
4126#endif
4127 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 continue; /* did not ask for this register */
4129
4130#ifdef FEAT_CLIPBOARD
4131 /* Adjust register name for "unnamed" in 'clipboard'.
4132 * When it's a clipboard register, fill it with the current contents
4133 * of the clipboard. */
4134 adjust_clip_reg(&name);
4135 (void)may_get_selection(name);
4136#endif
4137
4138 if (i == -1)
4139 {
4140 if (y_previous != NULL)
4141 yb = y_previous;
4142 else
4143 yb = &(y_regs[0]);
4144 }
4145 else
4146 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004147
4148#ifdef FEAT_EVAL
4149 if (name == MB_TOLOWER(redir_reg)
4150 || (redir_reg == '"' && yb == y_previous))
4151 continue; /* do not list register being written to, the
4152 * pointer can be freed */
4153#endif
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 if (yb->y_array != NULL)
4156 {
4157 msg_putchar('\n');
4158 msg_putchar('"');
4159 msg_putchar(name);
4160 MSG_PUTS(" ");
4161
4162 n = (int)Columns - 6;
4163 for (j = 0; j < yb->y_size && n > 1; ++j)
4164 {
4165 if (j)
4166 {
4167 MSG_PUTS_ATTR("^J", attr);
4168 n -= 2;
4169 }
4170 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004173 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004174#endif
4175 msg_outtrans_len(p, clen);
4176#ifdef FEAT_MBYTE
4177 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#endif
4179 }
4180 }
4181 if (n > 1 && yb->y_type == MLINE)
4182 MSG_PUTS_ATTR("^J", attr);
4183 out_flush(); /* show one line at a time */
4184 }
4185 ui_breakcheck();
4186 }
4187
4188 /*
4189 * display last inserted text
4190 */
4191 if ((p = get_last_insert()) != NULL
4192 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4193 {
4194 MSG_PUTS("\n\". ");
4195 dis_msg(p, TRUE);
4196 }
4197
4198 /*
4199 * display last command line
4200 */
4201 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4202 && !got_int)
4203 {
4204 MSG_PUTS("\n\": ");
4205 dis_msg(last_cmdline, FALSE);
4206 }
4207
4208 /*
4209 * display current file name
4210 */
4211 if (curbuf->b_fname != NULL
4212 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4213 {
4214 MSG_PUTS("\n\"% ");
4215 dis_msg(curbuf->b_fname, FALSE);
4216 }
4217
4218 /*
4219 * display alternate file name
4220 */
4221 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4222 {
4223 char_u *fname;
4224 linenr_T dummy;
4225
4226 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4227 {
4228 MSG_PUTS("\n\"# ");
4229 dis_msg(fname, FALSE);
4230 }
4231 }
4232
4233 /*
4234 * display last search pattern
4235 */
4236 if (last_search_pat() != NULL
4237 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4238 {
4239 MSG_PUTS("\n\"/ ");
4240 dis_msg(last_search_pat(), FALSE);
4241 }
4242
4243#ifdef FEAT_EVAL
4244 /*
4245 * display last used expression
4246 */
4247 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4248 && !got_int)
4249 {
4250 MSG_PUTS("\n\"= ");
4251 dis_msg(expr_line, FALSE);
4252 }
4253#endif
4254}
4255
4256/*
4257 * display a string for do_dis()
4258 * truncate at end of screen line
4259 */
4260 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004261dis_msg(
4262 char_u *p,
4263 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264{
4265 int n;
4266#ifdef FEAT_MBYTE
4267 int l;
4268#endif
4269
4270 n = (int)Columns - 6;
4271 while (*p != NUL
4272 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4273 && (n -= ptr2cells(p)) >= 0)
4274 {
4275#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004276 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
4278 msg_outtrans_len(p, l);
4279 p += l;
4280 }
4281 else
4282#endif
4283 msg_outtrans_len(p++, 1);
4284 }
4285 ui_breakcheck();
4286}
4287
Bram Moolenaar81340392012-06-06 16:12:59 +02004288#if defined(FEAT_COMMENTS) || defined(PROTO)
4289/*
4290 * If "process" is TRUE and the line begins with a comment leader (possibly
4291 * after some white space), return a pointer to the text after it. Put a boolean
4292 * value indicating whether the line ends with an unclosed comment in
4293 * "is_comment".
4294 * line - line to be processed,
4295 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004296 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004297 * include_space - whether to also skip space following the comment leader,
4298 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004299 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004300 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004301 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004302skip_comment(
4303 char_u *line,
4304 int process,
4305 int include_space,
4306 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004307{
4308 char_u *comment_flags = NULL;
4309 int lead_len;
4310 int leader_offset = get_last_leader_offset(line, &comment_flags);
4311
4312 *is_comment = FALSE;
4313 if (leader_offset != -1)
4314 {
4315 /* Let's check whether the line ends with an unclosed comment.
4316 * If the last comment leader has COM_END in flags, there's no comment.
4317 */
4318 while (*comment_flags)
4319 {
4320 if (*comment_flags == COM_END
4321 || *comment_flags == ':')
4322 break;
4323 ++comment_flags;
4324 }
4325 if (*comment_flags != COM_END)
4326 *is_comment = TRUE;
4327 }
4328
4329 if (process == FALSE)
4330 return line;
4331
4332 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4333
4334 if (lead_len == 0)
4335 return line;
4336
4337 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004338 * - COM_END,
4339 * - colon,
4340 * whichever comes first.
4341 */
4342 while (*comment_flags)
4343 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004344 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004345 || *comment_flags == ':')
4346 {
4347 break;
4348 }
4349 ++comment_flags;
4350 }
4351
4352 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004353 * starting with a closing part of a three-part comment. That's good,
4354 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004355 */
4356 if (*comment_flags == ':' || *comment_flags == NUL)
4357 line += lead_len;
4358
4359 return line;
4360}
4361#endif
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004364 * Join 'count' lines (minimal 2) at cursor position.
4365 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004366 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4367 * leaders should not be removed.
4368 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4369 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004371 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004374do_join(
4375 long count,
4376 int insert_space,
4377 int save_undo,
4378 int use_formatoptions UNUSED,
4379 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004381 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004382 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004383 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004385 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004386 int endcurr1 = NUL;
4387 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004388 int currsize = 0; /* size of the current line */
4389 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004391 colnr_T col = 0;
4392 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004393#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004394 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004395 int remove_comments = (use_formatoptions == TRUE)
4396 && has_format_option(FO_REMOVE_COMS);
4397 int prev_was_comment;
4398#endif
4399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004401 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4402 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4403 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004405 /* Allocate an array to store the number of spaces inserted before each
4406 * line. We will use it to pre-compute the length of the new line and the
4407 * proper placement of each original line in the new one. */
4408 spaces = lalloc_clear((long_u)count, TRUE);
4409 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004411#if defined(FEAT_COMMENTS) || defined(PROTO)
4412 if (remove_comments)
4413 {
4414 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4415 if (comments == NULL)
4416 {
4417 vim_free(spaces);
4418 return FAIL;
4419 }
4420 }
4421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
4423 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004424 * Don't move anything, just compute the final line length
4425 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004427 for (t = 0; t < count; ++t)
4428 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004429 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004430 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004431 {
4432 /* Set the '[ mark. */
4433 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4434 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4435 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004436#if defined(FEAT_COMMENTS) || defined(PROTO)
4437 if (remove_comments)
4438 {
4439 /* We don't want to remove the comment leader if the
4440 * previous line is not a comment. */
4441 if (t > 0 && prev_was_comment)
4442 {
4443
4444 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4445 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004446 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004447 curr = new_curr;
4448 }
4449 else
4450 curr = skip_comment(curr, FALSE, insert_space,
4451 &prev_was_comment);
4452 }
4453#endif
4454
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004455 if (insert_space && t > 0)
4456 {
4457 curr = skipwhite(curr);
4458 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4459#ifdef FEAT_MBYTE
4460 && (!has_format_option(FO_MBYTE_JOIN)
4461 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4462 && (!has_format_option(FO_MBYTE_JOIN2)
4463 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4464#endif
4465 )
4466 {
4467 /* don't add a space if the line is ending in a space */
4468 if (endcurr1 == ' ')
4469 endcurr1 = endcurr2;
4470 else
4471 ++spaces[t];
4472 /* extra space when 'joinspaces' set and line ends in '.' */
4473 if ( p_js
4474 && (endcurr1 == '.'
4475 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4476 && (endcurr1 == '?' || endcurr1 == '!'))))
4477 ++spaces[t];
4478 }
4479 }
4480 currsize = (int)STRLEN(curr);
4481 sumsize += currsize + spaces[t];
4482 endcurr1 = endcurr2 = NUL;
4483 if (insert_space && currsize > 0)
4484 {
4485#ifdef FEAT_MBYTE
4486 if (has_mbyte)
4487 {
4488 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004489 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004490 endcurr1 = (*mb_ptr2char)(cend);
4491 if (cend > curr)
4492 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004493 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004494 endcurr2 = (*mb_ptr2char)(cend);
4495 }
4496 }
4497 else
4498#endif
4499 {
4500 endcurr1 = *(curr + currsize - 1);
4501 if (currsize > 1)
4502 endcurr2 = *(curr + currsize - 2);
4503 }
4504 }
4505 line_breakcheck();
4506 if (got_int)
4507 {
4508 ret = FAIL;
4509 goto theend;
4510 }
4511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004513 /* store the column position before last line */
4514 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004516 /* allocate the space for the new line */
4517 newp = alloc_check((unsigned)(sumsize + 1));
4518 cend = newp + sumsize;
4519 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004521 /*
4522 * Move affected lines to the new long one.
4523 *
4524 * Move marks from each deleted line to the joined line, adjusting the
4525 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4526 * should not really be a problem.
4527 */
4528 for (t = count - 1; ; --t)
4529 {
4530 cend -= currsize;
4531 mch_memmove(cend, curr, (size_t)currsize);
4532 if (spaces[t] > 0)
4533 {
4534 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004535 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004536 }
4537 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004538 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004539 if (t == 0)
4540 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004541 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004542#if defined(FEAT_COMMENTS) || defined(PROTO)
4543 if (remove_comments)
4544 curr += comments[t - 1];
4545#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004546 if (insert_space && t > 1)
4547 curr = skipwhite(curr);
4548 currsize = (int)STRLEN(curr);
4549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4551
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004552 if (setmark)
4553 {
4554 /* Set the '] mark. */
4555 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4556 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4557 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 /* Only report the change in the first line here, del_lines() will report
4560 * the deleted line. */
4561 changed_lines(curwin->w_cursor.lnum, currsize,
4562 curwin->w_cursor.lnum + 1, 0L);
4563
4564 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004565 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 * briefly, and then move it back. After del_lines() the cursor may
4567 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 */
4569 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004571 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 curwin->w_cursor.lnum = t;
4573
4574 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004575 * Set the cursor column:
4576 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004577 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004579 curwin->w_cursor.col =
4580 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583#ifdef FEAT_VIRTUALEDIT
4584 curwin->w_cursor.coladd = 0;
4585#endif
4586 curwin->w_set_curswant = TRUE;
4587
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004588theend:
4589 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004590#if defined(FEAT_COMMENTS) || defined(PROTO)
4591 if (remove_comments)
4592 vim_free(comments);
4593#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004594 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595}
4596
4597#ifdef FEAT_COMMENTS
4598/*
4599 * Return TRUE if the two comment leaders given are the same. "lnum" is
4600 * the first line. White-space is ignored. Note that the whole of
4601 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4602 */
4603 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004604same_leader(
4605 linenr_T lnum,
4606 int leader1_len,
4607 char_u *leader1_flags,
4608 int leader2_len,
4609 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 int idx1 = 0, idx2 = 0;
4612 char_u *p;
4613 char_u *line1;
4614 char_u *line2;
4615
4616 if (leader1_len == 0)
4617 return (leader2_len == 0);
4618
4619 /*
4620 * If first leader has 'f' flag, the lines can be joined only if the
4621 * second line does not have a leader.
4622 * If first leader has 'e' flag, the lines can never be joined.
4623 * If fist leader has 's' flag, the lines can only be joined if there is
4624 * some text after it and the second line has the 'm' flag.
4625 */
4626 if (leader1_flags != NULL)
4627 {
4628 for (p = leader1_flags; *p && *p != ':'; ++p)
4629 {
4630 if (*p == COM_FIRST)
4631 return (leader2_len == 0);
4632 if (*p == COM_END)
4633 return FALSE;
4634 if (*p == COM_START)
4635 {
4636 if (*(ml_get(lnum) + leader1_len) == NUL)
4637 return FALSE;
4638 if (leader2_flags == NULL || leader2_len == 0)
4639 return FALSE;
4640 for (p = leader2_flags; *p && *p != ':'; ++p)
4641 if (*p == COM_MIDDLE)
4642 return TRUE;
4643 return FALSE;
4644 }
4645 }
4646 }
4647
4648 /*
4649 * Get current line and next line, compare the leaders.
4650 * The first line has to be saved, only one line can be locked at a time.
4651 */
4652 line1 = vim_strsave(ml_get(lnum));
4653 if (line1 != NULL)
4654 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004655 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 ;
4657 line2 = ml_get(lnum + 1);
4658 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4659 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004660 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
4662 if (line1[idx1++] != line2[idx2])
4663 break;
4664 }
4665 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004666 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 ++idx1;
4668 }
4669 vim_free(line1);
4670 }
4671 return (idx2 == leader2_len && idx1 == leader1_len);
4672}
4673#endif
4674
4675/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004676 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 */
4678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004679op_format(
4680 oparg_T *oap,
4681 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
4683 long old_line_count = curbuf->b_ml.ml_line_count;
4684
4685 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4686 * can put it back there. */
4687 curwin->w_cursor = oap->cursor_start;
4688
4689 if (u_save((linenr_T)(oap->start.lnum - 1),
4690 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4691 return;
4692 curwin->w_cursor = oap->start;
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (oap->is_VIsual)
4695 /* When there is no change: need to remove the Visual selection */
4696 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
4698 /* Set '[ mark at the start of the formatted area */
4699 curbuf->b_op_start = oap->start;
4700
4701 /* For "gw" remember the cursor position and put it back below (adjusted
4702 * for joined and split lines). */
4703 if (keep_cursor)
4704 saved_cursor = oap->cursor_start;
4705
Bram Moolenaar81a82092008-03-12 16:27:00 +00004706 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 /*
4709 * Leave the cursor at the first non-blank of the last formatted line.
4710 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4711 * line, so "." will do the next lines.
4712 */
4713 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4714 ++curwin->w_cursor.lnum;
4715 beginline(BL_WHITE | BL_FIX);
4716 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4717 msgmore(old_line_count);
4718
4719 /* put '] mark on the end of the formatted area */
4720 curbuf->b_op_end = curwin->w_cursor;
4721
4722 if (keep_cursor)
4723 {
4724 curwin->w_cursor = saved_cursor;
4725 saved_cursor.lnum = 0;
4726 }
4727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (oap->is_VIsual)
4729 {
4730 win_T *wp;
4731
4732 FOR_ALL_WINDOWS(wp)
4733 {
4734 if (wp->w_old_cursor_lnum != 0)
4735 {
4736 /* When lines have been inserted or deleted, adjust the end of
4737 * the Visual area to be redrawn. */
4738 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4739 wp->w_old_cursor_lnum += old_line_count;
4740 else
4741 wp->w_old_visual_lnum += old_line_count;
4742 }
4743 }
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745}
4746
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004747#if defined(FEAT_EVAL) || defined(PROTO)
4748/*
4749 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4750 */
4751 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004752op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004753{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004754 if (oap->is_VIsual)
4755 /* When there is no change: need to remove the Visual selection */
4756 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004757
Bram Moolenaar700303e2010-07-11 17:35:50 +02004758 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4759 /* As documented: when 'formatexpr' returns non-zero fall back to
4760 * internal formatting. */
4761 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004762}
4763
4764 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004765fex_format(
4766 linenr_T lnum,
4767 long count,
4768 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004769{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004770 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4771 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004772 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004773 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004774
4775 /*
4776 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004777 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004778 */
4779 set_vim_var_nr(VV_LNUM, lnum);
4780 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004781 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004782
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004783 /* Make a copy, the option could be changed while calling it. */
4784 fex = vim_strsave(curbuf->b_p_fex);
4785 if (fex == NULL)
4786 return 0;
4787
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004788 /*
4789 * Evaluate the function.
4790 */
4791 if (use_sandbox)
4792 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004793 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004794 if (use_sandbox)
4795 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004796
4797 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004798 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004799
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004800 return r;
4801}
4802#endif
4803
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804/*
4805 * Format "line_count" lines, starting at the cursor position.
4806 * When "line_count" is negative, format until the end of the paragraph.
4807 * Lines after the cursor line are saved for undo, caller must have saved the
4808 * first line.
4809 */
4810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004811format_lines(
4812 linenr_T line_count,
4813 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
4815 int max_len;
4816 int is_not_par; /* current line not part of parag. */
4817 int next_is_not_par; /* next line not part of paragraph */
4818 int is_end_par; /* at end of paragraph */
4819 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4820 int next_is_start_par = FALSE;
4821#ifdef FEAT_COMMENTS
4822 int leader_len = 0; /* leader len of current line */
4823 int next_leader_len; /* leader len of next line */
4824 char_u *leader_flags = NULL; /* flags for leader of current line */
4825 char_u *next_leader_flags; /* flags for leader of next line */
4826 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004827 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#endif
4829 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004830 int second_indent = -1; /* indent for second line (comment
4831 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 int do_second_indent;
4833 int do_number_indent;
4834 int do_trail_white;
4835 int first_par_line = TRUE;
4836 int smd_save;
4837 long count;
4838 int need_set_indent = TRUE; /* set indent of next paragraph */
4839 int force_format = FALSE;
4840 int old_State = State;
4841
4842 /* length of a line to force formatting: 3 * 'tw' */
4843 max_len = comp_textwidth(TRUE) * 3;
4844
4845 /* check for 'q', '2' and '1' in 'formatoptions' */
4846#ifdef FEAT_COMMENTS
4847 do_comments = has_format_option(FO_Q_COMS);
4848#endif
4849 do_second_indent = has_format_option(FO_Q_SECOND);
4850 do_number_indent = has_format_option(FO_Q_NUMBER);
4851 do_trail_white = has_format_option(FO_WHITE_PAR);
4852
4853 /*
4854 * Get info about the previous and current line.
4855 */
4856 if (curwin->w_cursor.lnum > 1)
4857 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4858#ifdef FEAT_COMMENTS
4859 , &leader_len, &leader_flags, do_comments
4860#endif
4861 );
4862 else
4863 is_not_par = TRUE;
4864 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4865#ifdef FEAT_COMMENTS
4866 , &next_leader_len, &next_leader_flags, do_comments
4867#endif
4868 );
4869 is_end_par = (is_not_par || next_is_not_par);
4870 if (!is_end_par && do_trail_white)
4871 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4872
4873 curwin->w_cursor.lnum--;
4874 for (count = line_count; count != 0 && !got_int; --count)
4875 {
4876 /*
4877 * Advance to next paragraph.
4878 */
4879 if (advance)
4880 {
4881 curwin->w_cursor.lnum++;
4882 prev_is_end_par = is_end_par;
4883 is_not_par = next_is_not_par;
4884#ifdef FEAT_COMMENTS
4885 leader_len = next_leader_len;
4886 leader_flags = next_leader_flags;
4887#endif
4888 }
4889
4890 /*
4891 * The last line to be formatted.
4892 */
4893 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4894 {
4895 next_is_not_par = TRUE;
4896#ifdef FEAT_COMMENTS
4897 next_leader_len = 0;
4898 next_leader_flags = NULL;
4899#endif
4900 }
4901 else
4902 {
4903 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4904#ifdef FEAT_COMMENTS
4905 , &next_leader_len, &next_leader_flags, do_comments
4906#endif
4907 );
4908 if (do_number_indent)
4909 next_is_start_par =
4910 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4911 }
4912 advance = TRUE;
4913 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4914 if (!is_end_par && do_trail_white)
4915 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4916
4917 /*
4918 * Skip lines that are not in a paragraph.
4919 */
4920 if (is_not_par)
4921 {
4922 if (line_count < 0)
4923 break;
4924 }
4925 else
4926 {
4927 /*
4928 * For the first line of a paragraph, check indent of second line.
4929 * Don't do this for comments and empty lines.
4930 */
4931 if (first_par_line
4932 && (do_second_indent || do_number_indent)
4933 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004934 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004936 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004937 {
4938#ifdef FEAT_COMMENTS
4939 if (leader_len == 0 && next_leader_len == 0)
4940 {
4941 /* no comment found */
4942#endif
4943 second_indent =
4944 get_indent_lnum(curwin->w_cursor.lnum + 1);
4945#ifdef FEAT_COMMENTS
4946 }
4947 else
4948 {
4949 second_indent = next_leader_len;
4950 do_comments_list = 1;
4951 }
4952#endif
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004955 {
4956#ifdef FEAT_COMMENTS
4957 if (leader_len == 0 && next_leader_len == 0)
4958 {
4959 /* no comment found */
4960#endif
4961 second_indent =
4962 get_number_indent(curwin->w_cursor.lnum);
4963#ifdef FEAT_COMMENTS
4964 }
4965 else
4966 {
4967 /* get_number_indent() is now "comment aware"... */
4968 second_indent =
4969 get_number_indent(curwin->w_cursor.lnum);
4970 do_comments_list = 1;
4971 }
4972#endif
4973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
4975
4976 /*
4977 * When the comment leader changes, it's the end of the paragraph.
4978 */
4979 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4980#ifdef FEAT_COMMENTS
4981 || !same_leader(curwin->w_cursor.lnum,
4982 leader_len, leader_flags,
4983 next_leader_len, next_leader_flags)
4984#endif
4985 )
4986 is_end_par = TRUE;
4987
4988 /*
4989 * If we have got to the end of a paragraph, or the line is
4990 * getting long, format it.
4991 */
4992 if (is_end_par || force_format)
4993 {
4994 if (need_set_indent)
4995 /* replace indent in first line with minimal number of
4996 * tabs and spaces, according to current options */
4997 (void)set_indent(get_indent(), SIN_CHANGED);
4998
4999 /* put cursor on last non-space */
5000 State = NORMAL; /* don't go past end-of-line */
5001 coladvance((colnr_T)MAXCOL);
5002 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5003 dec_cursor();
5004
5005 /* do the formatting, without 'showmode' */
5006 State = INSERT; /* for open_line() */
5007 smd_save = p_smd;
5008 p_smd = FALSE;
5009 insertchar(NUL, INSCHAR_FORMAT
5010#ifdef FEAT_COMMENTS
5011 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005012 + (do_comments && do_comments_list
5013 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005015 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 State = old_State;
5017 p_smd = smd_save;
5018 second_indent = -1;
5019 /* at end of par.: need to set indent of next par. */
5020 need_set_indent = is_end_par;
5021 if (is_end_par)
5022 {
5023 /* When called with a negative line count, break at the
5024 * end of the paragraph. */
5025 if (line_count < 0)
5026 break;
5027 first_par_line = TRUE;
5028 }
5029 force_format = FALSE;
5030 }
5031
5032 /*
5033 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005034 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 */
5036 if (!is_end_par)
5037 {
5038 advance = FALSE;
5039 curwin->w_cursor.lnum++;
5040 curwin->w_cursor.col = 0;
5041 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005042 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005045 {
5046 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5048 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005049 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005051 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5052 {
5053 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005054 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005055
5056 if (indent > 0)
5057 {
5058 (void)del_bytes(indent, FALSE, FALSE);
5059 mark_col_adjust(curwin->w_cursor.lnum,
5060 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005061 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005064 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 beep_flush();
5067 break;
5068 }
5069 first_par_line = FALSE;
5070 /* If the line is getting long, format it next time */
5071 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5072 force_format = TRUE;
5073 else
5074 force_format = FALSE;
5075 }
5076 }
5077 line_breakcheck();
5078 }
5079}
5080
5081/*
5082 * Return TRUE if line "lnum" ends in a white character.
5083 */
5084 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005085ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 char_u *s = ml_get(lnum);
5088 size_t l;
5089
5090 if (*s == NUL)
5091 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005092 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 * invocation may call function multiple times". */
5094 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005095 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096}
5097
5098/*
5099 * Blank lines, and lines containing only the comment leader, are left
5100 * untouched by the formatting. The function returns TRUE in this
5101 * case. It also returns TRUE when a line starts with the end of a comment
5102 * ('e' in comment flags), so that this line is skipped, and not joined to the
5103 * previous line. A new paragraph starts after a blank line, or when the
5104 * comment leader changes -- webb.
5105 */
5106#ifdef FEAT_COMMENTS
5107 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005108fmt_check_par(
5109 linenr_T lnum,
5110 int *leader_len,
5111 char_u **leader_flags,
5112 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
5114 char_u *flags = NULL; /* init for GCC */
5115 char_u *ptr;
5116
5117 ptr = ml_get(lnum);
5118 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005119 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 else
5121 *leader_len = 0;
5122
5123 if (*leader_len > 0)
5124 {
5125 /*
5126 * Search for 'e' flag in comment leader flags.
5127 */
5128 flags = *leader_flags;
5129 while (*flags && *flags != ':' && *flags != COM_END)
5130 ++flags;
5131 }
5132
5133 return (*skipwhite(ptr + *leader_len) == NUL
5134 || (*leader_len > 0 && *flags == COM_END)
5135 || startPS(lnum, NUL, FALSE));
5136}
5137#else
5138 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005139fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5142}
5143#endif
5144
5145/*
5146 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5147 * previous line is in the same paragraph. Used for auto-formatting.
5148 */
5149 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005150paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 char_u *p;
5153#ifdef FEAT_COMMENTS
5154 int leader_len = 0; /* leader len of current line */
5155 char_u *leader_flags = NULL; /* flags for leader of current line */
5156 int next_leader_len; /* leader len of next line */
5157 char_u *next_leader_flags; /* flags for leader of next line */
5158 int do_comments; /* format comments */
5159#endif
5160
5161 if (lnum <= 1)
5162 return TRUE; /* start of the file */
5163
5164 p = ml_get(lnum - 1);
5165 if (*p == NUL)
5166 return TRUE; /* after empty line */
5167
5168#ifdef FEAT_COMMENTS
5169 do_comments = has_format_option(FO_Q_COMS);
5170#endif
5171 if (fmt_check_par(lnum - 1
5172#ifdef FEAT_COMMENTS
5173 , &leader_len, &leader_flags, do_comments
5174#endif
5175 ))
5176 return TRUE; /* after non-paragraph line */
5177
5178 if (fmt_check_par(lnum
5179#ifdef FEAT_COMMENTS
5180 , &next_leader_len, &next_leader_flags, do_comments
5181#endif
5182 ))
5183 return TRUE; /* "lnum" is not a paragraph line */
5184
5185 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5186 return TRUE; /* missing trailing space in previous line. */
5187
5188 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5189 return TRUE; /* numbered item starts in "lnum". */
5190
5191#ifdef FEAT_COMMENTS
5192 if (!same_leader(lnum - 1, leader_len, leader_flags,
5193 next_leader_len, next_leader_flags))
5194 return TRUE; /* change of comment leader. */
5195#endif
5196
5197 return FALSE;
5198}
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200/*
5201 * prepare a few things for block mode yank/delete/tilde
5202 *
5203 * for delete:
5204 * - textlen includes the first/last char to be (partly) deleted
5205 * - start/endspaces is the number of columns that are taken by the
5206 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005207 * deleted.
5208 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 * - textlen includes the first/last char to be wholly yanked
5210 * - start/endspaces is the number of columns of the first/last yanked char
5211 * that are to be yanked.
5212 */
5213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005214block_prep(
5215 oparg_T *oap,
5216 struct block_def *bdp,
5217 linenr_T lnum,
5218 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219{
5220 int incr = 0;
5221 char_u *pend;
5222 char_u *pstart;
5223 char_u *line;
5224 char_u *prev_pstart;
5225 char_u *prev_pend;
5226
5227 bdp->startspaces = 0;
5228 bdp->endspaces = 0;
5229 bdp->textlen = 0;
5230 bdp->start_vcol = 0;
5231 bdp->end_vcol = 0;
5232#ifdef FEAT_VISUALEXTRA
5233 bdp->is_short = FALSE;
5234 bdp->is_oneChar = FALSE;
5235 bdp->pre_whitesp = 0;
5236 bdp->pre_whitesp_c = 0;
5237 bdp->end_char_vcols = 0;
5238#endif
5239 bdp->start_char_vcols = 0;
5240
5241 line = ml_get(lnum);
5242 pstart = line;
5243 prev_pstart = line;
5244 while (bdp->start_vcol < oap->start_vcol && *pstart)
5245 {
5246 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005247 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 bdp->start_vcol += incr;
5249#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005250 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 bdp->pre_whitesp += incr;
5253 bdp->pre_whitesp_c++;
5254 }
5255 else
5256 {
5257 bdp->pre_whitesp = 0;
5258 bdp->pre_whitesp_c = 0;
5259 }
5260#endif
5261 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005262 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264 bdp->start_char_vcols = incr;
5265 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5266 {
5267 bdp->end_vcol = bdp->start_vcol;
5268#ifdef FEAT_VISUALEXTRA
5269 bdp->is_short = TRUE;
5270#endif
5271 if (!is_del || oap->op_type == OP_APPEND)
5272 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5273 }
5274 else
5275 {
5276 /* notice: this converts partly selected Multibyte characters to
5277 * spaces, too. */
5278 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5279 if (is_del && bdp->startspaces)
5280 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5281 pend = pstart;
5282 bdp->end_vcol = bdp->start_vcol;
5283 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5284 {
5285#ifdef FEAT_VISUALEXTRA
5286 bdp->is_oneChar = TRUE;
5287#endif
5288 if (oap->op_type == OP_INSERT)
5289 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5290 else if (oap->op_type == OP_APPEND)
5291 {
5292 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5293 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5294 }
5295 else
5296 {
5297 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5298 if (is_del && oap->op_type != OP_LSHIFT)
5299 {
5300 /* just putting the sum of those two into
5301 * bdp->startspaces doesn't work for Visual replace,
5302 * so we have to split the tab in two */
5303 bdp->startspaces = bdp->start_char_vcols
5304 - (bdp->start_vcol - oap->start_vcol);
5305 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5306 }
5307 }
5308 }
5309 else
5310 {
5311 prev_pend = pend;
5312 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5313 {
5314 /* Count a tab for what it's worth (if list mode not on) */
5315 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005316 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 bdp->end_vcol += incr;
5318 }
5319 if (bdp->end_vcol <= oap->end_vcol
5320 && (!is_del
5321 || oap->op_type == OP_APPEND
5322 || oap->op_type == OP_REPLACE)) /* line too short */
5323 {
5324#ifdef FEAT_VISUALEXTRA
5325 bdp->is_short = TRUE;
5326#endif
5327 /* Alternative: include spaces to fill up the block.
5328 * Disadvantage: can lead to trailing spaces when the line is
5329 * short where the text is put */
5330 /* if (!is_del || oap->op_type == OP_APPEND) */
5331 if (oap->op_type == OP_APPEND || virtual_op)
5332 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005333 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 else
5335 bdp->endspaces = 0; /* replace doesn't add characters */
5336 }
5337 else if (bdp->end_vcol > oap->end_vcol)
5338 {
5339 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5340 if (!is_del && bdp->endspaces)
5341 {
5342 bdp->endspaces = incr - bdp->endspaces;
5343 if (pend != pstart)
5344 pend = prev_pend;
5345 }
5346 }
5347 }
5348#ifdef FEAT_VISUALEXTRA
5349 bdp->end_char_vcols = incr;
5350#endif
5351 if (is_del && bdp->startspaces)
5352 pstart = prev_pstart;
5353 bdp->textlen = (int)(pend - pstart);
5354 }
5355 bdp->textcol = (colnr_T) (pstart - line);
5356 bdp->textstart = pstart;
5357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005360 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005362 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005363op_addsub(
5364 oparg_T *oap,
5365 linenr_T Prenum1, /* Amount of add/subtract */
5366 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005368 pos_T pos;
5369 struct block_def bd;
5370 int change_cnt = 0;
5371 linenr_T amount = Prenum1;
5372
5373 if (!VIsual_active)
5374 {
5375 pos = curwin->w_cursor;
5376 if (u_save_cursor() == FAIL)
5377 return;
5378 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5379 if (change_cnt)
5380 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5381 }
5382 else
5383 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005384 int one_change;
5385 int length;
5386 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005387
5388 if (u_save((linenr_T)(oap->start.lnum - 1),
5389 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5390 return;
5391
5392 pos = oap->start;
5393 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5394 {
5395 if (oap->block_mode) /* Visual block mode */
5396 {
5397 block_prep(oap, &bd, pos.lnum, FALSE);
5398 pos.col = bd.textcol;
5399 length = bd.textlen;
5400 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005401 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005402 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005403 curwin->w_cursor.col = 0;
5404 pos.col = 0;
5405 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5406 }
5407 else /* oap->motion_type == MCHAR */
5408 {
5409 if (!oap->inclusive)
5410 dec(&(oap->end));
5411 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5412 pos.col = 0;
5413 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005414 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005415 pos.col += oap->start.col;
5416 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005417 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005418 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005419 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005420 length = (int)STRLEN(ml_get(oap->end.lnum));
5421 if (oap->end.col >= length)
5422 oap->end.col = length - 1;
5423 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005424 }
5425 }
5426 one_change = do_addsub(oap->op_type, &pos, length, amount);
5427 if (one_change)
5428 {
5429 /* Remember the start position of the first change. */
5430 if (change_cnt == 0)
5431 startpos = curbuf->b_op_start;
5432 ++change_cnt;
5433 }
5434
5435#ifdef FEAT_NETBEANS_INTG
5436 if (netbeans_active() && one_change)
5437 {
5438 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5439
5440 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5441 netbeans_inserted(curbuf, pos.lnum, pos.col,
5442 &ptr[pos.col], length);
5443 }
5444#endif
5445 if (g_cmd && one_change)
5446 amount += Prenum1;
5447 }
5448 if (change_cnt)
5449 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5450
5451 if (!change_cnt && oap->is_VIsual)
5452 /* No change: need to remove the Visual selection */
5453 redraw_curbuf_later(INVERTED);
5454
5455 /* Set '[ mark if something changed. Keep the last end
5456 * position from do_addsub(). */
5457 if (change_cnt > 0)
5458 curbuf->b_op_start = startpos;
5459
5460 if (change_cnt > p_report)
5461 {
5462 if (change_cnt == 1)
5463 MSG(_("1 line changed"));
5464 else
5465 smsg((char_u *)_("%ld lines changed"), change_cnt);
5466 }
5467 }
5468}
5469
5470/*
5471 * Add or subtract 'Prenum1' from a number in a line
5472 * op_type is OP_NR_ADD or OP_NR_SUB
5473 *
5474 * Returns TRUE if some character was changed.
5475 */
5476 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005477do_addsub(
5478 int op_type,
5479 pos_T *pos,
5480 int length,
5481 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005482{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 int col;
5484 char_u *buf1;
5485 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005486 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005488 uvarnumber_T n;
5489 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 char_u *ptr;
5491 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 int todel;
5493 int dohex;
5494 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005495 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int doalp;
5497 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005499 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005500 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005501 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005502 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005503 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005504 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005505 pos_T startpos;
5506 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5509 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005510 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5512
Bram Moolenaard79e5502016-01-10 22:13:02 +01005513 curwin->w_cursor = *pos;
5514 ptr = ml_get(pos->lnum);
5515 col = pos->col;
5516
5517 if (*ptr == NUL)
5518 goto theend;
5519
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 /*
5521 * First check if we are on a hexadecimal number, after the "0x".
5522 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005523 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005525 if (dobin)
5526 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005527 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005528 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005529#ifdef FEAT_MBYTE
5530 if (has_mbyte)
5531 col -= (*mb_head_off)(ptr, ptr + col);
5532#endif
5533 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005534
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005535 if (dohex)
5536 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005537 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005538 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005539#ifdef FEAT_MBYTE
5540 if (has_mbyte)
5541 col -= (*mb_head_off)(ptr, ptr + col);
5542#endif
5543 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005544
5545 if ( dobin
5546 && dohex
5547 && ! ((col > 0
5548 && (ptr[col] == 'X'
5549 || ptr[col] == 'x')
5550 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005551#ifdef FEAT_MBYTE
5552 && (!has_mbyte ||
5553 !(*mb_head_off)(ptr, ptr + col - 1))
5554#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005555 && vim_isxdigit(ptr[col + 1]))))
5556 {
5557
5558 /* In case of binary/hexadecimal pattern overlap match, rescan */
5559
Bram Moolenaard79e5502016-01-10 22:13:02 +01005560 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005561
5562 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005563 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005564 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005565#ifdef FEAT_MBYTE
5566 if (has_mbyte)
5567 col -= (*mb_head_off)(ptr, ptr + col);
5568#endif
5569 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005570 }
5571
5572 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005573 && col > 0
5574 && (ptr[col] == 'X'
5575 || ptr[col] == 'x')
5576 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005577#ifdef FEAT_MBYTE
5578 && (!has_mbyte ||
5579 !(*mb_head_off)(ptr, ptr + col - 1))
5580#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005581 && vim_isxdigit(ptr[col + 1])) ||
5582 ( dobin
5583 && col > 0
5584 && (ptr[col] == 'B'
5585 || ptr[col] == 'b')
5586 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005587#ifdef FEAT_MBYTE
5588 && (!has_mbyte ||
5589 !(*mb_head_off)(ptr, ptr + col - 1))
5590#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005591 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005592 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005593 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005594 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005595#ifdef FEAT_MBYTE
5596 if (has_mbyte)
5597 col -= (*mb_head_off)(ptr, ptr + col);
5598#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005599 }
5600 else
5601 {
5602 /*
5603 * Search forward and then backward to find the start of number.
5604 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005605 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005606
5607 while (ptr[col] != NUL
5608 && !vim_isdigit(ptr[col])
5609 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005610 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005611
5612 while (col > 0
5613 && vim_isdigit(ptr[col - 1])
5614 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005615 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005616 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005617#ifdef FEAT_MBYTE
5618 if (has_mbyte)
5619 col -= (*mb_head_off)(ptr, ptr + col);
5620#endif
5621 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005622 }
5623 }
5624
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005625 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005626 {
5627 while (ptr[col] != NUL && length > 0
5628 && !vim_isdigit(ptr[col])
5629 && !(doalp && ASCII_ISALPHA(ptr[col])))
5630 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005631 int mb_len = MB_PTR2LEN(ptr + col);
5632
5633 col += mb_len;
5634 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005635 }
5636
5637 if (length == 0)
5638 goto theend;
5639
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005640 if (col > pos->col && ptr[col - 1] == '-'
5641#ifdef FEAT_MBYTE
5642 && (!has_mbyte ||
5643 !(*mb_head_off)(ptr, ptr + col - 1))
5644#endif
5645 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005646 {
5647 negative = TRUE;
5648 was_positive = FALSE;
5649 }
5650 }
5651
5652 /*
5653 * If a number was found, and saving for undo works, replace the number.
5654 */
5655 firstdigit = ptr[col];
5656 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5657 {
5658 beep_flush();
5659 goto theend;
5660 }
5661
5662 if (doalp && ASCII_ISALPHA(firstdigit))
5663 {
5664 /* decrement or increment alphabetic character */
5665 if (op_type == OP_NR_SUB)
5666 {
5667 if (CharOrd(firstdigit) < Prenum1)
5668 {
5669 if (isupper(firstdigit))
5670 firstdigit = 'A';
5671 else
5672 firstdigit = 'a';
5673 }
5674 else
5675#ifdef EBCDIC
5676 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5677#else
5678 firstdigit -= Prenum1;
5679#endif
5680 }
5681 else
5682 {
5683 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5684 {
5685 if (isupper(firstdigit))
5686 firstdigit = 'Z';
5687 else
5688 firstdigit = 'z';
5689 }
5690 else
5691#ifdef EBCDIC
5692 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5693#else
5694 firstdigit += Prenum1;
5695#endif
5696 }
5697 curwin->w_cursor.col = col;
5698 if (!did_change)
5699 startpos = curwin->w_cursor;
5700 did_change = TRUE;
5701 (void)del_char(FALSE);
5702 ins_char(firstdigit);
5703 endpos = curwin->w_cursor;
5704 curwin->w_cursor.col = col;
5705 }
5706 else
5707 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005708 if (col > 0 && ptr[col - 1] == '-'
5709#ifdef FEAT_MBYTE
5710 && (!has_mbyte ||
5711 !(*mb_head_off)(ptr, ptr + col - 1))
5712#endif
5713 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005714 {
5715 /* negative number */
5716 --col;
5717 negative = TRUE;
5718 }
5719 /* get the number value (unsigned) */
5720 if (visual && VIsual_mode != 'V')
5721 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5722 ? (int)STRLEN(ptr) - col
5723 : length);
5724
5725 vim_str2nr(ptr + col, &pre, &length,
5726 0 + (dobin ? STR2NR_BIN : 0)
5727 + (dooct ? STR2NR_OCT : 0)
5728 + (dohex ? STR2NR_HEX : 0),
5729 NULL, &n, maxlen);
5730
5731 /* ignore leading '-' for hex and octal and bin numbers */
5732 if (pre && negative)
5733 {
5734 ++col;
5735 --length;
5736 negative = FALSE;
5737 }
5738 /* add or subtract */
5739 subtract = FALSE;
5740 if (op_type == OP_NR_SUB)
5741 subtract ^= TRUE;
5742 if (negative)
5743 subtract ^= TRUE;
5744
5745 oldn = n;
5746 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005747 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005748 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005749 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005750 /* handle wraparound for decimal numbers */
5751 if (!pre)
5752 {
5753 if (subtract)
5754 {
5755 if (n > oldn)
5756 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005757 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005758 negative ^= TRUE;
5759 }
5760 }
5761 else
5762 {
5763 /* add */
5764 if (n < oldn)
5765 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005766 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005767 negative ^= TRUE;
5768 }
5769 }
5770 if (n == 0)
5771 negative = FALSE;
5772 }
5773
5774 if (visual && !was_positive && !negative && col > 0)
5775 {
5776 /* need to remove the '-' */
5777 col--;
5778 length++;
5779 }
5780
5781 /*
5782 * Delete the old number.
5783 */
5784 curwin->w_cursor.col = col;
5785 if (!did_change)
5786 startpos = curwin->w_cursor;
5787 did_change = TRUE;
5788 todel = length;
5789 c = gchar_cursor();
5790 /*
5791 * Don't include the '-' in the length, only the length of the
5792 * part after it is kept the same.
5793 */
5794 if (c == '-')
5795 --length;
5796 while (todel-- > 0)
5797 {
5798 if (c < 0x100 && isalpha(c))
5799 {
5800 if (isupper(c))
5801 hexupper = TRUE;
5802 else
5803 hexupper = FALSE;
5804 }
5805 /* del_char() will mark line needing displaying */
5806 (void)del_char(FALSE);
5807 c = gchar_cursor();
5808 }
5809
5810 /*
5811 * Prepare the leading characters in buf1[].
5812 * When there are many leading zeros it could be very long.
5813 * Allocate a bit too much.
5814 */
5815 buf1 = alloc((unsigned)length + NUMBUFLEN);
5816 if (buf1 == NULL)
5817 goto theend;
5818 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005819 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005820 {
5821 *ptr++ = '-';
5822 }
5823 if (pre)
5824 {
5825 *ptr++ = '0';
5826 --length;
5827 }
5828 if (pre == 'b' || pre == 'B' ||
5829 pre == 'x' || pre == 'X')
5830 {
5831 *ptr++ = pre;
5832 --length;
5833 }
5834
5835 /*
5836 * Put the number characters in buf2[].
5837 */
5838 if (pre == 'b' || pre == 'B')
5839 {
5840 int i;
5841 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005842 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005843
5844 /* leading zeros */
5845 for (bit = bits; bit > 0; bit--)
5846 if ((n >> (bit - 1)) & 0x1) break;
5847
5848 for (i = 0; bit > 0; bit--)
5849 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5850
5851 buf2[i] = '\0';
5852 }
5853 else if (pre == 0)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005854 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005855 else if (pre == '0')
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005856 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005857 else if (pre && hexupper)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005858 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005859 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005860 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005861 length -= (int)STRLEN(buf2);
5862
5863 /*
5864 * Adjust number of zeros to the new number of digits, so the
5865 * total length of the number remains the same.
5866 * Don't do this when
5867 * the result may look like an octal number.
5868 */
5869 if (firstdigit == '0' && !(dooct && pre == 0))
5870 while (length-- > 0)
5871 *ptr++ = '0';
5872 *ptr = NUL;
5873 STRCAT(buf1, buf2);
5874 ins_str(buf1); /* insert the new number */
5875 vim_free(buf1);
5876 endpos = curwin->w_cursor;
5877 if (did_change && curwin->w_cursor.col)
5878 --curwin->w_cursor.col;
5879 }
5880
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005881 if (did_change)
5882 {
5883 /* set the '[ and '] marks */
5884 curbuf->b_op_start = startpos;
5885 curbuf->b_op_end = endpos;
5886 if (curbuf->b_op_end.col > 0)
5887 --curbuf->b_op_end.col;
5888 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005889
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005890theend:
5891 if (visual)
5892 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005893 else if (did_change)
5894 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005895
Bram Moolenaard79e5502016-01-10 22:13:02 +01005896 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898
5899#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005900
5901static yankreg_T *y_read_regs = NULL;
5902
5903#define REG_PREVIOUS 1
5904#define REG_EXEC 2
5905
5906/*
5907 * Prepare for reading viminfo registers when writing viminfo later.
5908 */
5909 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005910prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005911{
5912 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
5913 * (int)sizeof(yankreg_T));
5914}
5915
5916 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005917finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005918{
5919 int i;
5920 int j;
5921
5922 if (y_read_regs != NULL)
5923 {
5924 for (i = 0; i < NUM_REGISTERS; ++i)
5925 if (y_read_regs[i].y_array != NULL)
5926 {
5927 for (j = 0; j < y_read_regs[i].y_size; j++)
5928 vim_free(y_read_regs[i].y_array[j]);
5929 vim_free(y_read_regs[i].y_array);
5930 }
5931 vim_free(y_read_regs);
5932 y_read_regs = NULL;
5933 }
5934}
5935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005937read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 int eof;
5940 int do_it = TRUE;
5941 int size;
5942 int limit;
5943 int i;
5944 int set_prev = FALSE;
5945 char_u *str;
5946 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005947 int new_type = MCHAR; /* init to shut up compiler */
5948 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950 /* We only get here (hopefully) if line[0] == '"' */
5951 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005952
5953 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 if (*str == '"')
5955 {
5956 set_prev = TRUE;
5957 str++;
5958 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (!ASCII_ISALNUM(*str) && *str != '-')
5961 {
5962 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5963 return TRUE; /* too many errors, pretend end-of-file */
5964 do_it = FALSE;
5965 }
5966 get_yank_register(*str++, FALSE);
5967 if (!force && y_current->y_array != NULL)
5968 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005969
5970 if (*str == '@')
5971 {
5972 /* "x@: register x used for @@ */
5973 if (force || execreg_lastc == NUL)
5974 execreg_lastc = str[-1];
5975 }
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 size = 0;
5978 limit = 100; /* Optimized for registers containing <= 100 lines */
5979 if (do_it)
5980 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005981 /*
5982 * Build the new register in array[].
5983 * y_array is kept as-is until done.
5984 * The "do_it" flag is reset when something is wrong, in which case
5985 * array[] needs to be freed.
5986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 if (set_prev)
5988 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005989 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005990 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005992 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005994 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005996 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 /* get the block width; if it's missing we get a zero, which is OK */
5998 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005999 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001
6002 while (!(eof = viminfo_readline(virp))
6003 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6004 {
6005 if (do_it)
6006 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006007 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006009 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006011
6012 if (new_array == NULL)
6013 {
6014 do_it = FALSE;
6015 break;
6016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006018 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006020 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 }
6023 str = viminfo_readstring(virp, 1, TRUE);
6024 if (str != NULL)
6025 array[size++] = str;
6026 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006027 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 do_it = FALSE;
6029 }
6030 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (do_it)
6033 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006034 /* free y_array[] */
6035 for (i = 0; i < y_current->y_size; i++)
6036 vim_free(y_current->y_array[i]);
6037 vim_free(y_current->y_array);
6038
6039 y_current->y_type = new_type;
6040 y_current->y_width = new_width;
6041 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006042 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 if (size == 0)
6044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 y_current->y_array = NULL;
6046 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006047 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006049 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 y_current->y_array =
6051 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6052 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006053 {
6054 if (y_current->y_array == NULL)
6055 vim_free(array[i]);
6056 else
6057 y_current->y_array[i] = array[i];
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006061 else
6062 {
6063 /* Free array[] if it was filled. */
6064 for (i = 0; i < size; i++)
6065 vim_free(array[i]);
6066 }
6067 vim_free(array);
6068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 return eof;
6070}
6071
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006072/*
6073 * Accept a new style register line from the viminfo, store it when it's new.
6074 */
6075 void
6076handle_viminfo_register(garray_T *values, int force)
6077{
6078 bval_T *vp = (bval_T *)values->ga_data;
6079 int flags;
6080 int name;
6081 int type;
6082 int linecount;
6083 int width;
6084 time_t timestamp;
6085 yankreg_T *y_ptr;
6086 int i;
6087
6088 /* Check the format:
6089 * |{bartype},{flags},{name},{type},
6090 * {linecount},{width},{timestamp},"line1","line2"
6091 */
6092 if (values->ga_len < 6
6093 || vp[0].bv_type != BVAL_NR
6094 || vp[1].bv_type != BVAL_NR
6095 || vp[2].bv_type != BVAL_NR
6096 || vp[3].bv_type != BVAL_NR
6097 || vp[4].bv_type != BVAL_NR
6098 || vp[5].bv_type != BVAL_NR)
6099 return;
6100 flags = vp[0].bv_nr;
6101 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006102 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006103 return;
6104 type = vp[2].bv_nr;
6105 if (type != MCHAR && type != MLINE && type != MBLOCK)
6106 return;
6107 linecount = vp[3].bv_nr;
6108 if (values->ga_len < 6 + linecount)
6109 return;
6110 width = vp[4].bv_nr;
6111 if (width < 0)
6112 return;
6113
6114 if (y_read_regs != NULL)
6115 /* Reading viminfo for merging and writing. Store the register
6116 * content, don't update the current registers. */
6117 y_ptr = &y_read_regs[name];
6118 else
6119 y_ptr = &y_regs[name];
6120
6121 /* Do not overwrite unless forced or the timestamp is newer. */
6122 timestamp = (time_t)vp[5].bv_nr;
6123 if (y_ptr->y_array != NULL && !force
6124 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6125 return;
6126
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006127 if (y_ptr->y_array != NULL)
6128 for (i = 0; i < y_ptr->y_size; i++)
6129 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006130 vim_free(y_ptr->y_array);
6131
6132 if (y_read_regs == NULL)
6133 {
6134 if (flags & REG_PREVIOUS)
6135 y_previous = y_ptr;
6136 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6137 execreg_lastc = get_register_name(name);
6138 }
6139 y_ptr->y_type = type;
6140 y_ptr->y_width = width;
6141 y_ptr->y_size = linecount;
6142 y_ptr->y_time_set = timestamp;
6143 if (linecount == 0)
6144 y_ptr->y_array = NULL;
6145 else
6146 {
6147 y_ptr->y_array =
6148 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6149 for (i = 0; i < linecount; i++)
6150 {
6151 if (vp[i + 6].bv_allocated)
6152 {
6153 y_ptr->y_array[i] = vp[i + 6].bv_string;
6154 vp[i + 6].bv_string = NULL;
6155 }
6156 else
6157 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6158 }
6159 }
6160}
6161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006163write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006165 int i, j;
6166 char_u *type;
6167 char_u c;
6168 int num_lines;
6169 int max_num_lines;
6170 int max_kbyte;
6171 long len;
6172 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaar64404472010-06-26 06:24:45 +02006174 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
6176 /* Get '<' value, use old '"' value if '<' is not found. */
6177 max_num_lines = get_viminfo_parameter('<');
6178 if (max_num_lines < 0)
6179 max_num_lines = get_viminfo_parameter('"');
6180 if (max_num_lines == 0)
6181 return;
6182 max_kbyte = get_viminfo_parameter('s');
6183 if (max_kbyte == 0)
6184 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006185
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 for (i = 0; i < NUM_REGISTERS; i++)
6187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188#ifdef FEAT_CLIPBOARD
6189 /* Skip '*'/'+' register, we don't want them back next time */
6190 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6191 continue;
6192#endif
6193#ifdef FEAT_DND
6194 /* Neither do we want the '~' register */
6195 if (i == TILDE_REGISTER)
6196 continue;
6197#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006198 /* When reading viminfo for merging and writing: Use the register from
6199 * viminfo if it's newer. */
6200 if (y_read_regs != NULL
6201 && y_read_regs[i].y_array != NULL
6202 && (y_regs[i].y_array == NULL ||
6203 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6204 y_ptr = &y_read_regs[i];
6205 else if (y_regs[i].y_array == NULL)
6206 continue;
6207 else
6208 y_ptr = &y_regs[i];
6209
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006210 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006211 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006212 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006213 || (num_lines == 1 && y_ptr->y_type == MCHAR
6214 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006215 continue;
6216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (max_kbyte > 0)
6218 {
6219 /* Skip register if there is more text than the maximum size. */
6220 len = 0;
6221 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006222 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (len > (long)max_kbyte * 1024L)
6224 continue;
6225 }
6226
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006227 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
6229 case MLINE:
6230 type = (char_u *)"LINE";
6231 break;
6232 case MCHAR:
6233 type = (char_u *)"CHAR";
6234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 case MBLOCK:
6236 type = (char_u *)"BLOCK";
6237 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 default:
6239 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006240 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 emsg(IObuff);
6242 type = (char_u *)"LINE";
6243 break;
6244 }
6245 if (y_previous == &y_regs[i])
6246 fprintf(fp, "\"");
6247 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006248 fprintf(fp, "\"%c", c);
6249 if (c == execreg_lastc)
6250 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006251 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253 /* If max_num_lines < 0, then we save ALL the lines in the register */
6254 if (max_num_lines > 0 && num_lines > max_num_lines)
6255 num_lines = max_num_lines;
6256 for (j = 0; j < num_lines; j++)
6257 {
6258 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006259 viminfo_writestring(fp, y_ptr->y_array[j]);
6260 }
6261
6262 {
6263 int flags = 0;
6264 int remaining;
6265
6266 /* New style with a bar line. Format:
6267 * |{bartype},{flags},{name},{type},
6268 * {linecount},{width},{timestamp},"line1","line2"
6269 * flags: REG_PREVIOUS - register is y_previous
6270 * REG_EXEC - used for @@
6271 */
6272 if (y_previous == &y_regs[i])
6273 flags |= REG_PREVIOUS;
6274 if (c == execreg_lastc)
6275 flags |= REG_EXEC;
6276 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6277 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6278 (long)y_ptr->y_time_set);
6279 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6280 remaining = LSIZE - 71;
6281 for (j = 0; j < num_lines; j++)
6282 {
6283 putc(',', fp);
6284 --remaining;
6285 remaining = barline_writestring(fp, y_ptr->y_array[j],
6286 remaining);
6287 }
6288 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290 }
6291}
6292#endif /* FEAT_VIMINFO */
6293
6294#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6295/*
6296 * SELECTION / PRIMARY ('*')
6297 *
6298 * Text selection stuff that uses the GUI selection register '*'. When using a
6299 * GUI this may be text from another window, otherwise it is the last text we
6300 * had highlighted with VIsual mode. With mouse support, clicking the middle
6301 * button performs the paste, otherwise you will need to do <"*p>. "
6302 * If not under X, it is synonymous with the clipboard register '+'.
6303 *
6304 * X CLIPBOARD ('+')
6305 *
6306 * Text selection stuff that uses the GUI clipboard register '+'.
6307 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6308 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6309 * otherwise you will need to do <"+p>. "
6310 * If not under X, it is synonymous with the selection register '*'.
6311 */
6312
6313/*
6314 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006315 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 * only exist while the owning application exists, so we write to the
6317 * permanent (while X runs) store CUT_BUFFER0.
6318 * Dump the CLIPBOARD selection if we own it (it's logically the more
6319 * 'permanent' of the two), otherwise the PRIMARY one.
6320 * For now, use a hard-coded sanity limit of 1Mb of data.
6321 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006322#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006324x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 Display *dpy;
6327 char_u *str = NULL;
6328 long_u len = 0;
6329 int motion_type = -1;
6330
6331# ifdef FEAT_GUI
6332 if (gui.in_use)
6333 dpy = X_DISPLAY;
6334 else
6335# endif
6336# ifdef FEAT_XCLIPBOARD
6337 dpy = xterm_dpy;
6338# else
6339 return;
6340# endif
6341
6342 /* Get selection to export */
6343 if (clip_plus.owned)
6344 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6345 else if (clip_star.owned)
6346 motion_type = clip_convert_selection(&str, &len, &clip_star);
6347
6348 /* Check it's OK */
6349 if (dpy != NULL && str != NULL && motion_type >= 0
6350 && len < 1024*1024 && len > 0)
6351 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006352#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006353 int ok = TRUE;
6354
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006355 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6356 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6357 * encoding conversion usually doesn't work, so keep the text as-is.
6358 */
6359 if (has_mbyte)
6360 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006361 vimconv_T vc;
6362
6363 vc.vc_type = CONV_NONE;
6364 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6365 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006366 int intlen = len;
6367 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006368
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006369 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006370 conv_str = string_convert(&vc, str, &intlen);
6371 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006372 if (conv_str != NULL)
6373 {
6374 vim_free(str);
6375 str = conv_str;
6376 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006377 else
6378 {
6379 ok = FALSE;
6380 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006381 convert_setup(&vc, NULL, NULL);
6382 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006383 else
6384 {
6385 ok = FALSE;
6386 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006387 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006388
6389 /* Do not store the string if conversion failed. Better to use any
6390 * other selection than garbled text. */
6391 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006392#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006393 {
6394 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6395 XFlush(dpy);
6396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 }
6398
6399 vim_free(str);
6400}
6401#endif
6402
6403 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006404clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006406 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408 if (cbd == &clip_plus)
6409 y_current = &y_regs[PLUS_REGISTER];
6410 else
6411 y_current = &y_regs[STAR_REGISTER];
6412 free_yank_all();
6413 y_current->y_size = 0;
6414 y_current = y_ptr;
6415}
6416
6417/*
6418 * Get the selected text and put it in the gui selection register '*' or '+'.
6419 */
6420 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006421clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006423 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 pos_T old_visual;
6426 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 colnr_T old_curswant;
6428 int old_set_curswant;
6429 pos_T old_op_start, old_op_end;
6430 oparg_T oa;
6431 cmdarg_T ca;
6432
6433 if (cbd->owned)
6434 {
6435 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6436 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6437 return;
6438
6439 /* Get the text between clip_star.start & clip_star.end */
6440 old_y_previous = y_previous;
6441 old_y_current = y_current;
6442 old_cursor = curwin->w_cursor;
6443 old_curswant = curwin->w_curswant;
6444 old_set_curswant = curwin->w_set_curswant;
6445 old_op_start = curbuf->b_op_start;
6446 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 old_visual = VIsual;
6448 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 clear_oparg(&oa);
6450 oa.regname = (cbd == &clip_plus ? '+' : '*');
6451 oa.op_type = OP_YANK;
6452 vim_memset(&ca, 0, sizeof(ca));
6453 ca.oap = &oa;
6454 ca.cmdchar = 'y';
6455 ca.count1 = 1;
6456 ca.retval = CA_NO_ADJ_OP_END;
6457 do_pending_operator(&ca, 0, TRUE);
6458 y_previous = old_y_previous;
6459 y_current = old_y_current;
6460 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006461 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 curwin->w_curswant = old_curswant;
6463 curwin->w_set_curswant = old_set_curswant;
6464 curbuf->b_op_start = old_op_start;
6465 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 VIsual = old_visual;
6467 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 }
6469 else
6470 {
6471 clip_free_selection(cbd);
6472
6473 /* Try to get selected text from another window */
6474 clip_gen_request_selection(cbd);
6475 }
6476}
6477
Bram Moolenaard44347f2011-06-19 01:14:29 +02006478/*
6479 * Convert from the GUI selection string into the '*'/'+' register.
6480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006482clip_yank_selection(
6483 int type,
6484 char_u *str,
6485 long len,
6486 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006488 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 if (cbd == &clip_plus)
6491 y_ptr = &y_regs[PLUS_REGISTER];
6492 else
6493 y_ptr = &y_regs[STAR_REGISTER];
6494
6495 clip_free_selection(cbd);
6496
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006497 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498}
6499
6500/*
6501 * Convert the '*'/'+' register into a GUI selection string returned in *str
6502 * with length *len.
6503 * Returns the motion type, or -1 for failure.
6504 */
6505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006506clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 char_u *p;
6509 int lnum;
6510 int i, j;
6511 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006512 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
6514 if (cbd == &clip_plus)
6515 y_ptr = &y_regs[PLUS_REGISTER];
6516 else
6517 y_ptr = &y_regs[STAR_REGISTER];
6518
6519#ifdef USE_CRNL
6520 eolsize = 2;
6521#else
6522 eolsize = 1;
6523#endif
6524
6525 *str = NULL;
6526 *len = 0;
6527 if (y_ptr->y_array == NULL)
6528 return -1;
6529
6530 for (i = 0; i < y_ptr->y_size; i++)
6531 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6532
6533 /*
6534 * Don't want newline character at end of last line if we're in MCHAR mode.
6535 */
6536 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6537 *len -= eolsize;
6538
6539 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6540 if (p == NULL)
6541 return -1;
6542 lnum = 0;
6543 for (i = 0, j = 0; i < (int)*len; i++, j++)
6544 {
6545 if (y_ptr->y_array[lnum][j] == '\n')
6546 p[i] = NUL;
6547 else if (y_ptr->y_array[lnum][j] == NUL)
6548 {
6549#ifdef USE_CRNL
6550 p[i++] = '\r';
6551#endif
6552#ifdef USE_CR
6553 p[i] = '\r';
6554#else
6555 p[i] = '\n';
6556#endif
6557 lnum++;
6558 j = -1;
6559 }
6560 else
6561 p[i] = y_ptr->y_array[lnum][j];
6562 }
6563 return y_ptr->y_type;
6564}
6565
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567/*
6568 * If we have written to a clipboard register, send the text to the clipboard.
6569 */
6570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006571may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6574 {
6575 clip_own_selection(&clip_star);
6576 clip_gen_set_selection(&clip_star);
6577 }
6578 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6579 {
6580 clip_own_selection(&clip_plus);
6581 clip_gen_set_selection(&clip_plus);
6582 }
6583}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
6585#endif /* FEAT_CLIPBOARD || PROTO */
6586
6587
6588#if defined(FEAT_DND) || defined(PROTO)
6589/*
6590 * Replace the contents of the '~' register with str.
6591 */
6592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006593dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006595 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596
6597 curr = y_current;
6598 y_current = &y_regs[TILDE_REGISTER];
6599 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006600 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 y_current = curr;
6602}
6603#endif
6604
6605
6606#if defined(FEAT_EVAL) || defined(PROTO)
6607/*
6608 * Return the type of a register.
6609 * Used for getregtype()
6610 * Returns MAUTO for error.
6611 */
6612 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006613get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 switch (regname)
6616 {
6617 case '%': /* file name */
6618 case '#': /* alternate file name */
6619 case '=': /* expression */
6620 case ':': /* last command line */
6621 case '/': /* last search-pattern */
6622 case '.': /* last inserted text */
6623#ifdef FEAT_SEARCHPATH
6624 case Ctrl_F: /* Filename under cursor */
6625 case Ctrl_P: /* Path under cursor, expand via "path" */
6626#endif
6627 case Ctrl_W: /* word under cursor */
6628 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6629 case '_': /* black hole: always empty */
6630 return MCHAR;
6631 }
6632
6633#ifdef FEAT_CLIPBOARD
6634 regname = may_get_selection(regname);
6635#endif
6636
Bram Moolenaar32b92012014-01-14 12:33:36 +01006637 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006638 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006639
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 get_yank_register(regname, FALSE);
6641
6642 if (y_current->y_array != NULL)
6643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (reglen != NULL && y_current->y_type == MBLOCK)
6645 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 return y_current->y_type;
6647 }
6648 return MAUTO;
6649}
6650
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006651static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006652
6653/*
6654 * When "flags" has GREG_LIST return a list with text "s".
6655 * Otherwise just return "s".
6656 */
6657 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006658getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006659{
6660 if (flags & GREG_LIST)
6661 {
6662 list_T *list = list_alloc();
6663
6664 if (list != NULL)
6665 {
6666 if (list_append_string(list, NULL, -1) == FAIL)
6667 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006668 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006669 return NULL;
6670 }
6671 list->lv_first->li_tv.vval.v_string = s;
6672 }
6673 return (char_u *)list;
6674 }
6675 return s;
6676}
6677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678/*
6679 * Return the contents of a register as a single allocated string.
6680 * Used for "@r" in expressions and for getreg().
6681 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006682 * Flags:
6683 * GREG_NO_EXPR Do not allow expression register
6684 * GREG_EXPR_SRC For the expression register: return expression itself,
6685 * not the result of its evaluation.
6686 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 */
6688 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006689get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 long i;
6692 char_u *retval;
6693 int allocated;
6694 long len;
6695
6696 /* Don't allow using an expression register inside an expression */
6697 if (regname == '=')
6698 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006699 if (flags & GREG_NO_EXPR)
6700 return NULL;
6701 if (flags & GREG_EXPR_SRC)
6702 return getreg_wrap_one_line(get_expr_line_src(), flags);
6703 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705
6706 if (regname == '@') /* "@@" is used for unnamed register */
6707 regname = '"';
6708
6709 /* check for valid regname */
6710 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6711 return NULL;
6712
6713#ifdef FEAT_CLIPBOARD
6714 regname = may_get_selection(regname);
6715#endif
6716
6717 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6718 {
6719 if (retval == NULL)
6720 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006721 if (allocated)
6722 return getreg_wrap_one_line(retval, flags);
6723 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
6725
6726 get_yank_register(regname, FALSE);
6727 if (y_current->y_array == NULL)
6728 return NULL;
6729
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006730 if (flags & GREG_LIST)
6731 {
6732 list_T *list = list_alloc();
6733 int error = FALSE;
6734
6735 if (list == NULL)
6736 return NULL;
6737 for (i = 0; i < y_current->y_size; ++i)
6738 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6739 error = TRUE;
6740 if (error)
6741 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006742 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006743 return NULL;
6744 }
6745 return (char_u *)list;
6746 }
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 /*
6749 * Compute length of resulting string.
6750 */
6751 len = 0;
6752 for (i = 0; i < y_current->y_size; ++i)
6753 {
6754 len += (long)STRLEN(y_current->y_array[i]);
6755 /*
6756 * Insert a newline between lines and after last line if
6757 * y_type is MLINE.
6758 */
6759 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6760 ++len;
6761 }
6762
6763 retval = lalloc(len + 1, TRUE);
6764
6765 /*
6766 * Copy the lines of the yank register into the string.
6767 */
6768 if (retval != NULL)
6769 {
6770 len = 0;
6771 for (i = 0; i < y_current->y_size; ++i)
6772 {
6773 STRCPY(retval + len, y_current->y_array[i]);
6774 len += (long)STRLEN(retval + len);
6775
6776 /*
6777 * Insert a NL between lines and after the last line if y_type is
6778 * MLINE.
6779 */
6780 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6781 retval[len++] = '\n';
6782 }
6783 retval[len] = NUL;
6784 }
6785
6786 return retval;
6787}
6788
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006789 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006790init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006791 int name,
6792 yankreg_T **old_y_previous,
6793 yankreg_T **old_y_current,
6794 int must_append,
6795 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006796{
6797 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6798 {
6799 emsg_invreg(name);
6800 return FAIL;
6801 }
6802
6803 /* Don't want to change the current (unnamed) register */
6804 *old_y_previous = y_previous;
6805 *old_y_current = y_current;
6806
6807 get_yank_register(name, TRUE);
6808 if (!y_append && !must_append)
6809 free_yank_all();
6810 return OK;
6811}
6812
6813 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006814finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006815 int name,
6816 yankreg_T *old_y_previous,
6817 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006818{
6819# ifdef FEAT_CLIPBOARD
6820 /* Send text of clipboard register to the clipboard. */
6821 may_set_selection();
6822# endif
6823
6824 /* ':let @" = "val"' should change the meaning of the "" register */
6825 if (name != '"')
6826 y_previous = old_y_previous;
6827 y_current = old_y_current;
6828}
6829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830/*
6831 * Store string "str" in register "name".
6832 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6833 * If "must_append" is TRUE, always append to the register. Otherwise append
6834 * if "name" is an uppercase letter.
6835 * Note: "maxlen" and "must_append" don't work for the "/" register.
6836 * Careful: 'str' is modified, you may have to use a copy!
6837 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6838 */
6839 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006840write_reg_contents(
6841 int name,
6842 char_u *str,
6843 int maxlen,
6844 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6847}
6848
6849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006850write_reg_contents_lst(
6851 int name,
6852 char_u **strings,
6853 int maxlen UNUSED,
6854 int must_append,
6855 int yank_type,
6856 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006857{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006858 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006859
6860 if (name == '/'
6861#ifdef FEAT_EVAL
6862 || name == '='
6863#endif
6864 )
6865 {
6866 char_u *s;
6867
6868 if (strings[0] == NULL)
6869 s = (char_u *)"";
6870 else if (strings[1] != NULL)
6871 {
6872 EMSG(_("E883: search pattern and expression register may not "
6873 "contain two or more lines"));
6874 return;
6875 }
6876 else
6877 s = strings[0];
6878 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6879 return;
6880 }
6881
6882 if (name == '_') /* black hole: nothing to do */
6883 return;
6884
6885 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6886 &yank_type) == FAIL)
6887 return;
6888
6889 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6890
6891 finish_write_reg(name, old_y_previous, old_y_current);
6892}
6893
6894 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006895write_reg_contents_ex(
6896 int name,
6897 char_u *str,
6898 int maxlen,
6899 int must_append,
6900 int yank_type,
6901 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006903 yankreg_T *old_y_previous, *old_y_current;
6904 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaare7566042005-06-17 22:00:15 +00006906 if (maxlen >= 0)
6907 len = maxlen;
6908 else
6909 len = (long)STRLEN(str);
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 /* Special case: '/' search pattern */
6912 if (name == '/')
6913 {
6914 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6915 return;
6916 }
6917
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006918 if (name == '#')
6919 {
6920 buf_T *buf;
6921
6922 if (VIM_ISDIGIT(*str))
6923 {
6924 int num = atoi((char *)str);
6925
6926 buf = buflist_findnr(num);
6927 if (buf == NULL)
6928 EMSGN(_(e_nobufnr), (long)num);
6929 }
6930 else
6931 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6932 TRUE, FALSE, FALSE));
6933 if (buf == NULL)
6934 return;
6935 curwin->w_alt_fnum = buf->b_fnum;
6936 return;
6937 }
6938
Bram Moolenaare7566042005-06-17 22:00:15 +00006939#ifdef FEAT_EVAL
6940 if (name == '=')
6941 {
6942 char_u *p, *s;
6943
6944 p = vim_strnsave(str, (int)len);
6945 if (p == NULL)
6946 return;
6947 if (must_append)
6948 {
6949 s = concat_str(get_expr_line_src(), p);
6950 vim_free(p);
6951 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006952 }
6953 set_expr_line(p);
6954 return;
6955 }
6956#endif
6957
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if (name == '_') /* black hole: nothing to do */
6959 return;
6960
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006961 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6962 &yank_type) == FAIL)
6963 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006965 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006967 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968}
6969#endif /* FEAT_EVAL */
6970
6971#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6972/*
6973 * Put a string into a register. When the register is not empty, the string
6974 * is appended.
6975 */
6976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006977str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006978 yankreg_T *y_ptr, /* pointer to yank register */
6979 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6980 char_u *str, /* string to put in register */
6981 long len, /* length of string */
6982 long blocklen, /* width of Visual block */
6983 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006985 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 int lnum;
6987 long start;
6988 long i;
6989 int extra;
6990 int newlines; /* number of lines added */
6991 int extraline = 0; /* extra line at the end */
6992 int append = FALSE; /* append to last line in register */
6993 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006994 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006998 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 y_ptr->y_size = 0;
7000
Bram Moolenaard44347f2011-06-19 01:14:29 +02007001 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007002 type = ((str_list || (len > 0 && (str[len - 1] == NL
7003 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007004 ? MLINE : MCHAR);
7005 else
7006 type = yank_type;
7007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 /*
7009 * Count the number of lines within the string
7010 */
7011 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007012 if (str_list)
7013 {
7014 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007017 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007019 for (i = 0; i < len; i++)
7020 if (str[i] == '\n')
7021 ++newlines;
7022 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7023 {
7024 extraline = 1;
7025 ++newlines; /* count extra newline at the end */
7026 }
7027 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7028 {
7029 append = TRUE;
7030 --newlines; /* uncount newline when appending first line */
7031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007034 /* Without any lines make the register empty. */
7035 if (y_ptr->y_size + newlines == 0)
7036 {
7037 vim_free(y_ptr->y_array);
7038 y_ptr->y_array = NULL;
7039 return;
7040 }
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 /*
7043 * Allocate an array to hold the pointers to the new register lines.
7044 * If the register was not empty, move the existing lines to the new array.
7045 */
7046 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7047 * sizeof(char_u *), TRUE);
7048 if (pp == NULL) /* out of memory */
7049 return;
7050 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7051 pp[lnum] = y_ptr->y_array[lnum];
7052 vim_free(y_ptr->y_array);
7053 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 /*
7057 * Find the end of each line and save it into the array.
7058 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007059 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007061 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7062 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007063 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007064 pp[lnum] = vim_strnsave(*ss, i);
7065 if (i > maxlen)
7066 maxlen = i;
7067 }
7068 }
7069 else
7070 {
7071 for (start = 0; start < len + extraline; start += i + 1)
7072 {
7073 for (i = start; i < len; ++i) /* find the end of the line */
7074 if (str[i] == '\n')
7075 break;
7076 i -= start; /* i is now length of line */
7077 if (i > maxlen)
7078 maxlen = i;
7079 if (append)
7080 {
7081 --lnum;
7082 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7083 }
7084 else
7085 extra = 0;
7086 s = alloc((unsigned)(i + extra + 1));
7087 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007089 if (extra)
7090 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7091 if (append)
7092 vim_free(y_ptr->y_array[lnum]);
7093 if (i)
7094 mch_memmove(s + extra, str + start, (size_t)i);
7095 extra += i;
7096 s[extra] = NUL;
7097 y_ptr->y_array[lnum++] = s;
7098 while (--extra >= 0)
7099 {
7100 if (*s == NUL)
7101 *s = '\n'; /* replace NUL with newline */
7102 ++s;
7103 }
7104 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 }
7107 y_ptr->y_type = type;
7108 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 if (type == MBLOCK)
7110 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7111 else
7112 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007113#ifdef FEAT_VIMINFO
7114 y_ptr->y_time_set = vim_time();
7115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116}
7117#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7118
7119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007120clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 vim_memset(oap, 0, sizeof(oparg_T));
7123}
7124
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007125static 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 +00007126
7127/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007128 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 *
7130 * "Words" are counted by looking for boundaries between non-space and
7131 * space characters. (it seems to produce results that match 'wc'.)
7132 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007133 * Return value is byte count; word count for the line is added to "*wc".
7134 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 *
7136 * The function will only examine the first "limit" characters in the
7137 * line, stopping if it encounters an end-of-line (NUL byte). In that
7138 * case, eol_size will be added to the character count to account for
7139 * the size of the EOL character.
7140 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007141 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007142line_count_info(
7143 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007144 varnumber_T *wc,
7145 varnumber_T *cc,
7146 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007147 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007149 varnumber_T i;
7150 varnumber_T words = 0;
7151 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 int is_word = 0;
7153
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007154 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
7156 if (is_word)
7157 {
7158 if (vim_isspace(line[i]))
7159 {
7160 words++;
7161 is_word = 0;
7162 }
7163 }
7164 else if (!vim_isspace(line[i]))
7165 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007166 ++chars;
7167#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007168 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007169#else
7170 ++i;
7171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 }
7173
7174 if (is_word)
7175 words++;
7176 *wc += words;
7177
7178 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007179 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007182 chars += eol_size;
7183 }
7184 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 return i;
7186}
7187
7188/*
7189 * Give some info about the position of the cursor (for "g CTRL-G").
7190 * In Visual mode, give some info about the selected region. (In this case,
7191 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007192 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 */
7194 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007195cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
7197 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007198 char_u buf1[50];
7199 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007201 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007202#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007203 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007204#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007205 varnumber_T byte_count_cursor = 0;
7206 varnumber_T char_count = 0;
7207 varnumber_T char_count_cursor = 0;
7208 varnumber_T word_count = 0;
7209 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007210 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007211 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 long line_count_selected = 0;
7213 pos_T min_pos, max_pos;
7214 oparg_T oparg;
7215 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217 /*
7218 * Compute the length of the file in characters.
7219 */
7220 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7221 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007222 if (dict == NULL)
7223 {
7224 MSG(_(no_lines_msg));
7225 return;
7226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
7228 else
7229 {
7230 if (get_fileformat(curbuf) == EOL_DOS)
7231 eol_size = 2;
7232 else
7233 eol_size = 1;
7234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 if (VIsual_active)
7236 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007237 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
7239 min_pos = VIsual;
7240 max_pos = curwin->w_cursor;
7241 }
7242 else
7243 {
7244 min_pos = curwin->w_cursor;
7245 max_pos = VIsual;
7246 }
7247 if (*p_sel == 'e' && max_pos.col > 0)
7248 --max_pos.col;
7249
7250 if (VIsual_mode == Ctrl_V)
7251 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007252#ifdef FEAT_LINEBREAK
7253 char_u * saved_sbr = p_sbr;
7254
7255 /* Make 'sbr' empty for a moment to get the correct size. */
7256 p_sbr = empty_option;
7257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 oparg.is_VIsual = 1;
7259 oparg.block_mode = TRUE;
7260 oparg.op_type = OP_NOP;
7261 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007262 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007263#ifdef FEAT_LINEBREAK
7264 p_sbr = saved_sbr;
7265#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007266 if (curwin->w_curswant == MAXCOL)
7267 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 /* Swap the start, end vcol if needed */
7269 if (oparg.end_vcol < oparg.start_vcol)
7270 {
7271 oparg.end_vcol += oparg.start_vcol;
7272 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7273 oparg.end_vcol -= oparg.start_vcol;
7274 }
7275 }
7276 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
7279 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7280 {
7281 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007282 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
7284 ui_breakcheck();
7285 if (got_int)
7286 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007287 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 }
7289
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 /* Do extra processing for VIsual mode. */
7291 if (VIsual_active
7292 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7293 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007294 char_u *s = NULL;
7295 long len = 0L;
7296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 switch (VIsual_mode)
7298 {
7299 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007300#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007304#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007306#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007307 s = bd.textstart;
7308 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 break;
7310 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007311 s = ml_get(lnum);
7312 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 break;
7314 case 'v':
7315 {
7316 colnr_T start_col = (lnum == min_pos.lnum)
7317 ? min_pos.col : 0;
7318 colnr_T end_col = (lnum == max_pos.lnum)
7319 ? max_pos.col - start_col + 1 : MAXCOL;
7320
Bram Moolenaardef9e822004-12-31 20:58:58 +00007321 s = ml_get(lnum) + start_col;
7322 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 }
7324 break;
7325 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007326 if (s != NULL)
7327 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007328 byte_count_cursor += line_count_info(s, &word_count_cursor,
7329 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007330 if (lnum == curbuf->b_ml.ml_line_count
7331 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007332 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007333 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007334 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
7337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 {
7339 /* In non-visual mode, check for the line the cursor is on */
7340 if (lnum == curwin->w_cursor.lnum)
7341 {
7342 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007343 char_count_cursor += char_count;
7344 byte_count_cursor = byte_count +
7345 line_count_info(ml_get(lnum),
7346 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007347 (varnumber_T)(curwin->w_cursor.col + 1),
7348 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350 }
7351 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007352 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007353 &char_count, (varnumber_T)MAXCOL,
7354 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
7356
7357 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007358 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007359 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
Bram Moolenaared767a22016-01-03 22:49:16 +01007361 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007363 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007365 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7366 {
7367 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7368 &max_pos.col);
7369 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7370 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7371 }
7372 else
7373 buf1[0] = NUL;
7374
7375 if (char_count_cursor == byte_count_cursor
7376 && char_count == byte_count)
7377 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007378 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007379 buf1, line_count_selected,
7380 (long)curbuf->b_ml.ml_line_count,
7381 word_count_cursor, word_count,
7382 byte_count_cursor, byte_count);
7383 else
7384 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007385 _("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 +01007386 buf1, line_count_selected,
7387 (long)curbuf->b_ml.ml_line_count,
7388 word_count_cursor, word_count,
7389 char_count_cursor, char_count,
7390 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 }
7392 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007393 {
7394 p = ml_get_curline();
7395 validate_virtcol();
7396 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7397 (int)curwin->w_virtcol + 1);
7398 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7399 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaared767a22016-01-03 22:49:16 +01007401 if (char_count_cursor == byte_count_cursor
7402 && char_count == byte_count)
7403 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007404 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007405 (char *)buf1, (char *)buf2,
7406 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 (long)curbuf->b_ml.ml_line_count,
7408 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007409 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007410 else
7411 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007412 _("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 +01007413 (char *)buf1, (char *)buf2,
7414 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007415 (long)curbuf->b_ml.ml_line_count,
7416 word_count_cursor, word_count,
7417 char_count_cursor, char_count,
7418 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007419 }
7420 }
7421
Bram Moolenaared767a22016-01-03 22:49:16 +01007422#ifdef FEAT_MBYTE
7423 bom_count = bomb_size();
7424 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007425 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
7426 _("(+%ld for BOM)"), bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007427#endif
7428 if (dict == NULL)
7429 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007430 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007431 p = p_shm;
7432 p_shm = (char_u *)"";
7433 msg(IObuff);
7434 p_shm = p;
7435 }
7436 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007437#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007438 if (dict != NULL)
7439 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007440 dict_add_nr_str(dict, "words", word_count, NULL);
7441 dict_add_nr_str(dict, "chars", char_count, NULL);
7442 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007443# ifdef FEAT_MBYTE
7444 + bom_count
7445# endif
7446 , NULL);
7447 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007448 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007449 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007450 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007451 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007452 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}