blob: 83c36bda67e2ab69fc6f37d115527f7071da0dcd [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)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (opchars[i][0] == char1 && opchars[i][1] == char2)
188 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100189 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
190 {
191 internal_error("get_op_type()");
192 break;
193 }
194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 return i;
196}
197
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198/*
199 * Return TRUE if operator "op" always works on whole lines.
200 */
201 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100202op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 return opchars[op][2];
205}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
208 * Get first operator command character.
209 * Returns 'g' or 'z' if there is another command character.
210 */
211 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100212get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 return opchars[optype][0];
215}
216
217/*
218 * Get second operator command character.
219 */
220 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100221get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 return opchars[optype][1];
224}
225
226/*
227 * op_shift - handle a shift operation
228 */
229 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100230op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 long i;
233 int first_char;
234 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237 if (u_save((linenr_T)(oap->start.lnum - 1),
238 (linenr_T)(oap->end.lnum + 1)) == FAIL)
239 return;
240
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 if (oap->block_mode)
242 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
244 for (i = oap->line_count; --i >= 0; )
245 {
246 first_char = *ml_get_curline();
247 if (first_char == NUL) /* empty line */
248 curwin->w_cursor.col = 0;
249#ifdef FEAT_VISUALEXTRA
250 else if (oap->block_mode)
251 shift_block(oap, amount);
252#endif
253 else
254 /* Move the line right if it doesn't start with '#', 'smartindent'
255 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
256#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
257 if (first_char != '#' || !preprocs_left())
258#endif
259 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000260 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262 ++curwin->w_cursor.lnum;
263 }
264
265 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 if (oap->block_mode)
267 {
268 curwin->w_cursor.lnum = oap->start.lnum;
269 curwin->w_cursor.col = block_col;
270 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100271 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 {
273 curwin->w_cursor.lnum = oap->start.lnum;
274 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
275 }
276 else
277 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
278
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100279#ifdef FEAT_FOLDING
280 /* The cursor line is not in a closed fold */
281 foldOpenCursor();
282#endif
283
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 if (oap->line_count > p_report)
286 {
287 if (oap->op_type == OP_RSHIFT)
288 s = (char_u *)">";
289 else
290 s = (char_u *)"<";
291 if (oap->line_count == 1)
292 {
293 if (amount == 1)
294 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
295 else
296 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
297 }
298 else
299 {
300 if (amount == 1)
301 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
302 oap->line_count, s);
303 else
304 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
305 oap->line_count, s, amount);
306 }
307 msg(IObuff);
308 }
309
310 /*
311 * Set "'[" and "']" marks.
312 */
313 curbuf->b_op_start = oap->start;
314 curbuf->b_op_end.lnum = oap->end.lnum;
315 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
316 if (curbuf->b_op_end.col > 0)
317 --curbuf->b_op_end.col;
318}
319
320/*
321 * shift the current line one shiftwidth left (if left != 0) or right
322 * leaves cursor on first blank in the line
323 */
324 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100325shift_line(
326 int left,
327 int round,
328 int amount,
329 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int count;
332 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100333 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
335 count = get_indent(); /* get current indent */
336
337 if (round) /* round off indent */
338 {
339 i = count / p_sw; /* number of p_sw rounded down */
340 j = count % p_sw; /* extra spaces */
341 if (j && left) /* first remove extra spaces */
342 --amount;
343 if (left)
344 {
345 i -= amount;
346 if (i < 0)
347 i = 0;
348 }
349 else
350 i += amount;
351 count = i * p_sw;
352 }
353 else /* original vi indent */
354 {
355 if (left)
356 {
357 count -= p_sw * amount;
358 if (count < 0)
359 count = 0;
360 }
361 else
362 count += p_sw * amount;
363 }
364
365 /* Set new indent */
366#ifdef FEAT_VREPLACE
367 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000368 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 else
370#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000371 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372}
373
374#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
375/*
376 * Shift one line of the current block one shiftwidth right or left.
377 * Leaves cursor on first character in block.
378 */
379 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100380shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
382 int left = (oap->op_type == OP_LSHIFT);
383 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 int total;
385 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100387 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 int p_ts = (int)curbuf->b_p_ts;
389 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000391 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 int i = 0, j = 0;
393 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#ifdef FEAT_RIGHTLEFT
395 int old_p_ri = p_ri;
396
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200397 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#endif
399
400 State = INSERT; /* don't want REPLACE for State */
401 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
402 if (bd.is_short)
403 return;
404
405 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200406 total = (int)((unsigned)amount * (unsigned)p_sw);
407 if ((total / p_sw) != amount)
408 return; /* multiplication overflow */
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 oldp = ml_get_curline();
411
412 if (!left)
413 {
414 /*
415 * 1. Get start vcol
416 * 2. Total ws vcols
417 * 3. Divvy into TABs & spp
418 * 4. Construct new string
419 */
420 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
421 ws_vcol = bd.start_vcol - bd.pre_whitesp;
422 if (bd.startspaces)
423 {
424#ifdef FEAT_MBYTE
425 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100426 {
427 if ((*mb_ptr2len)(bd.textstart) == 1)
428 ++bd.textstart;
429 else
430 {
431 ws_vcol = 0;
432 bd.startspaces = 0;
433 }
434 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000435 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000437 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100439 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200441 /* TODO: is passing bd.textstart for start of the line OK? */
442 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
443 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 total += incr;
445 bd.start_vcol += incr;
446 }
447 /* OK, now total=all the VWS reqd, and textstart points at the 1st
448 * non-ws char in the block. */
449 if (!curbuf->b_p_et)
450 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
451 if (i)
452 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
453 else
454 j = total;
455 /* if we're splitting a TAB, allow for it */
456 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
457 len = (int)STRLEN(bd.textstart) + 1;
458 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
459 if (newp == NULL)
460 return;
461 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
462 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200463 vim_memset(newp + bd.textcol, TAB, (size_t)i);
464 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 /* the end */
466 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
467 }
468 else /* left */
469 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000470 colnr_T destination_col; /* column to which text in block will
471 be shifted */
472 char_u *verbatim_copy_end; /* end of the part of the line which is
473 copied verbatim */
474 colnr_T verbatim_copy_width;/* the (displayed) width of this part
475 of line */
476 unsigned fill; /* nr of spaces that replace a TAB */
477 unsigned new_line_len; /* the length of the line after the
478 block shift */
479 size_t block_space_width;
480 size_t shift_amount;
481 char_u *non_white = bd.textstart;
482 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000484 /*
485 * Firstly, let's find the first non-whitespace character that is
486 * displayed after the block's start column and the character's column
487 * number. Also, let's calculate the width of all the whitespace
488 * characters that are displayed in the block and precede the searched
489 * non-whitespace character.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000492 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
493 * the part of which is displayed at the block's beginning. Let's start
494 * searching from the next character. */
495 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100496 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497
498 /* The character's column is in "bd.start_vcol". */
499 non_white_col = bd.start_vcol;
500
Bram Moolenaar1c465442017-03-12 20:10:05 +0100501 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200503 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000504 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000507 block_space_width = non_white_col - oap->start_vcol;
508 /* We will shift by "total" or "block_space_width", whichever is less.
509 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000510 shift_amount = (block_space_width < (size_t)total
511 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000512
513 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000514 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515
516 /* Now let's find out how much of the beginning of the line we can
517 * reuse without modification. */
518 verbatim_copy_end = bd.textstart;
519 verbatim_copy_width = bd.start_vcol;
520
521 /* If "bd.startspaces" is set, "bd.textstart" points to the character
522 * preceding the block. We have to subtract its width to obtain its
523 * column number. */
524 if (bd.startspaces)
525 verbatim_copy_width -= bd.start_char_vcols;
526 while (verbatim_copy_width < destination_col)
527 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200528 char_u *line = verbatim_copy_end;
529
530 /* TODO: is passing verbatim_copy_end for start of the line OK? */
531 incr = lbr_chartabsize(line, verbatim_copy_end,
532 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000533 if (verbatim_copy_width + incr > destination_col)
534 break;
535 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100536 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000537 }
538
539 /* If "destination_col" is different from the width of the initial
540 * part of the line that will be copied, it means we encountered a tab
541 * character, which we will have to partly replace with spaces. */
542 fill = destination_col - verbatim_copy_width;
543
544 /* The replacement line will consist of:
545 * - the beginning of the original line up to "verbatim_copy_end",
546 * - "fill" number of spaces,
547 * - the rest of the line, pointed to by non_white. */
548 new_line_len = (unsigned)(verbatim_copy_end - oldp)
549 + fill
550 + (unsigned)STRLEN(non_white) + 1;
551
552 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (newp == NULL)
554 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000555 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200556 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000557 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 }
559 /* replace the line */
560 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
561 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
562 State = oldstate;
563 curwin->w_cursor.col = oldcol;
564#ifdef FEAT_RIGHTLEFT
565 p_ri = old_p_ri;
566#endif
567}
568#endif
569
570#ifdef FEAT_VISUALEXTRA
571/*
572 * Insert string "s" (b_insert ? before : after) block :AKelly
573 * Caller must prepare for undo.
574 */
575 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100576block_insert(
577 oparg_T *oap,
578 char_u *s,
579 int b_insert,
580 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
582 int p_ts;
583 int count = 0; /* extra spaces to replace a cut TAB */
584 int spaces = 0; /* non-zero if cutting a TAB */
585 colnr_T offset; /* pointer along new line */
586 unsigned s_len; /* STRLEN(s) */
587 char_u *newp, *oldp; /* new, old lines */
588 linenr_T lnum; /* loop var */
589 int oldstate = State;
590
591 State = INSERT; /* don't want REPLACE for State */
592 s_len = (unsigned)STRLEN(s);
593
594 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
595 {
596 block_prep(oap, bdp, lnum, TRUE);
597 if (bdp->is_short && b_insert)
598 continue; /* OP_INSERT, line ends before block start */
599
600 oldp = ml_get(lnum);
601
602 if (b_insert)
603 {
604 p_ts = bdp->start_char_vcols;
605 spaces = bdp->startspaces;
606 if (spaces != 0)
607 count = p_ts - 1; /* we're cutting a TAB */
608 offset = bdp->textcol;
609 }
610 else /* append */
611 {
612 p_ts = bdp->end_char_vcols;
613 if (!bdp->is_short) /* spaces = padding after block */
614 {
615 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
616 if (spaces != 0)
617 count = p_ts - 1; /* we're cutting a TAB */
618 offset = bdp->textcol + bdp->textlen - (spaces != 0);
619 }
620 else /* spaces = padding to block edge */
621 {
622 /* if $ used, just append to EOL (ie spaces==0) */
623 if (!bdp->is_MAX)
624 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
625 count = spaces;
626 offset = bdp->textcol + bdp->textlen;
627 }
628 }
629
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200630#ifdef FEAT_MBYTE
631 if (has_mbyte && spaces > 0)
632 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100633 int off;
634
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200635 /* Avoid starting halfway a multi-byte character. */
636 if (b_insert)
637 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100638 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200639 }
640 else
641 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100642 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200643 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200644 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100645 spaces -= off;
646 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200647 }
648#endif
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
651 if (newp == NULL)
652 continue;
653
654 /* copy up to shifted part */
655 mch_memmove(newp, oldp, (size_t)(offset));
656 oldp += offset;
657
658 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200659 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 /* copy the new text */
662 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
663 offset += s_len;
664
665 if (spaces && !bdp->is_short)
666 {
667 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200668 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 /* We're splitting a TAB, don't copy it. */
670 oldp++;
671 /* We allowed for that TAB, remember this now */
672 count++;
673 }
674
675 if (spaces > 0)
676 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000677 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 ml_replace(lnum, newp, FALSE);
680
681 if (lnum == oap->end.lnum)
682 {
683 /* Set "']" mark to the end of the block instead of the end of
684 * the insert in the first line. */
685 curbuf->b_op_end.lnum = oap->end.lnum;
686 curbuf->b_op_end.col = offset;
687 }
688 } /* for all lnum */
689
690 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
691
692 State = oldstate;
693}
694#endif
695
696#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
697/*
698 * op_reindent - handle reindenting a block of lines.
699 */
700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100701op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 long i;
704 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200705 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 linenr_T first_changed = 0;
707 linenr_T last_changed = 0;
708 linenr_T start_lnum = curwin->w_cursor.lnum;
709
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000710 /* Don't even try when 'modifiable' is off. */
711 if (!curbuf->b_p_ma)
712 {
713 EMSG(_(e_modifiable));
714 return;
715 }
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 for (i = oap->line_count; --i >= 0 && !got_int; )
718 {
719 /* it's a slow thing to do, so give feedback so there's no worry that
720 * the computer's just hung. */
721
722 if (i > 1
723 && (i % 50 == 0 || i == oap->line_count - 1)
724 && oap->line_count > p_report)
725 smsg((char_u *)_("%ld lines to indent... "), i);
726
727 /*
728 * Be vi-compatible: For lisp indenting the first line is not
729 * indented, unless there is only one line.
730 */
731#ifdef FEAT_LISP
732 if (i != oap->line_count - 1 || oap->line_count == 1
733 || how != get_lisp_indent)
734#endif
735 {
736 l = skipwhite(ml_get_curline());
737 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200738 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200740 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200742 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
744 /* did change the indent, call changed_lines() later */
745 if (first_changed == 0)
746 first_changed = curwin->w_cursor.lnum;
747 last_changed = curwin->w_cursor.lnum;
748 }
749 }
750 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000751 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753
754 /* put cursor on first non-blank of indented line */
755 curwin->w_cursor.lnum = start_lnum;
756 beginline(BL_SOL | BL_FIX);
757
758 /* Mark changed lines so that they will be redrawn. When Visual
759 * highlighting was present, need to continue until the last line. When
760 * there is no change still need to remove the Visual highlighting. */
761 if (last_changed != 0)
762 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 else if (oap->is_VIsual)
766 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 if (oap->line_count > p_report)
769 {
770 i = oap->line_count - (i + 1);
771 if (i == 1)
772 MSG(_("1 line indented "));
773 else
774 smsg((char_u *)_("%ld lines indented "), i);
775 }
776 /* set '[ and '] marks */
777 curbuf->b_op_start = oap->start;
778 curbuf->b_op_end = oap->end;
779}
780#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
781
782#if defined(FEAT_EVAL) || defined(PROTO)
783/*
784 * Keep the last expression line here, for repeating.
785 */
786static char_u *expr_line = NULL;
787
788/*
789 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
790 * Returns '=' when OK, NUL otherwise.
791 */
792 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 char_u *new_line;
796
797 new_line = getcmdline('=', 0L, 0);
798 if (new_line == NULL)
799 return NUL;
800 if (*new_line == NUL) /* use previous line */
801 vim_free(new_line);
802 else
803 set_expr_line(new_line);
804 return '=';
805}
806
807/*
808 * Set the expression for the '=' register.
809 * Argument must be an allocated string.
810 */
811 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100812set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 vim_free(expr_line);
815 expr_line = new_line;
816}
817
818/*
819 * Get the result of the '=' register expression.
820 * Returns a pointer to allocated memory, or NULL for failure.
821 */
822 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 char_u *expr_copy;
826 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000827 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 if (expr_line == NULL)
830 return NULL;
831
832 /* Make a copy of the expression, because evaluating it may cause it to be
833 * changed. */
834 expr_copy = vim_strsave(expr_line);
835 if (expr_copy == NULL)
836 return NULL;
837
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000838 /* When we are invoked recursively limit the evaluation to 10 levels.
839 * Then return the string as-is. */
840 if (nested >= 10)
841 return expr_copy;
842
843 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000845 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 vim_free(expr_copy);
847 return rv;
848}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000849
850/*
851 * Get the '=' register expression itself, without evaluating it.
852 */
853 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000855{
856 if (expr_line == NULL)
857 return NULL;
858 return vim_strsave(expr_line);
859}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif /* FEAT_EVAL */
861
862/*
863 * Check if 'regname' is a valid name of a yank register.
864 * Note: There is no check for 0 (default register), caller should do this
865 */
866 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100867valid_yank_reg(
868 int regname,
869 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 if ( (regname > 0 && ASCII_ISALNUM(regname))
872 || (!writing && vim_strchr((char_u *)
873#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100874 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100876 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#endif
878 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100879 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 || regname == '"'
881 || regname == '-'
882 || regname == '_'
883#ifdef FEAT_CLIPBOARD
884 || regname == '*'
885 || regname == '+'
886#endif
887#ifdef FEAT_DND
888 || (!writing && regname == '~')
889#endif
890 )
891 return TRUE;
892 return FALSE;
893}
894
895/*
896 * Set y_current and y_append, according to the value of "regname".
897 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000898 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 *
900 * If regname is 0 and writing, use register 0
901 * If regname is 0 and reading, use previous register
902 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000903 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100904get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 int i;
907
908 y_append = FALSE;
909 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
910 {
911 y_current = y_previous;
912 return;
913 }
914 i = regname;
915 if (VIM_ISDIGIT(i))
916 i -= '0';
917 else if (ASCII_ISLOWER(i))
918 i = CharOrdLow(i) + 10;
919 else if (ASCII_ISUPPER(i))
920 {
921 i = CharOrdUp(i) + 10;
922 y_append = TRUE;
923 }
924 else if (regname == '-')
925 i = DELETION_REGISTER;
926#ifdef FEAT_CLIPBOARD
927 /* When selection is not available, use register 0 instead of '*' */
928 else if (clip_star.available && regname == '*')
929 i = STAR_REGISTER;
930 /* When clipboard is not available, use register 0 instead of '+' */
931 else if (clip_plus.available && regname == '+')
932 i = PLUS_REGISTER;
933#endif
934#ifdef FEAT_DND
935 else if (!writing && regname == '~')
936 i = TILDE_REGISTER;
937#endif
938 else /* not 0-9, a-z, A-Z or '-': use register 0 */
939 i = 0;
940 y_current = &(y_regs[i]);
941 if (writing) /* remember the register we write into for do_put() */
942 y_previous = y_current;
943}
944
Bram Moolenaar8299df92004-07-10 09:47:34 +0000945#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946/*
947 * When "regname" is a clipboard register, obtain the selection. If it's not
948 * available return zero, otherwise return "regname".
949 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100951may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
953 if (regname == '*')
954 {
955 if (!clip_star.available)
956 regname = 0;
957 else
958 clip_get_selection(&clip_star);
959 }
960 else if (regname == '+')
961 {
962 if (!clip_plus.available)
963 regname = 0;
964 else
965 clip_get_selection(&clip_plus);
966 }
967 return regname;
968}
969#endif
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Obtain the contents of a "normal" register. The register is made empty.
973 * The returned pointer has allocated memory, use put_register() later.
974 */
975 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100976get_register(
977 int name,
978 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200980 yankreg_T *reg;
981 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983#ifdef FEAT_CLIPBOARD
984 /* When Visual area changed, may have to update selection. Obtain the
985 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000986 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200988 if (clip_isautosel_star())
989 clip_update_selection(&clip_star);
990 may_get_selection(name);
991 }
992 if (name == '+' && clip_plus.available)
993 {
994 if (clip_isautosel_plus())
995 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 may_get_selection(name);
997 }
998#endif
999
1000 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001001 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (reg != NULL)
1003 {
1004 *reg = *y_current;
1005 if (copy)
1006 {
1007 /* If we run out of memory some or all of the lines are empty. */
1008 if (reg->y_size == 0)
1009 reg->y_array = NULL;
1010 else
1011 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1012 * reg->y_size));
1013 if (reg->y_array != NULL)
1014 {
1015 for (i = 0; i < reg->y_size; ++i)
1016 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1017 }
1018 }
1019 else
1020 y_current->y_array = NULL;
1021 }
1022 return (void *)reg;
1023}
1024
1025/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001026 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 */
1028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001029put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 get_yank_register(name, 0);
1032 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001033 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001034 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001036#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 /* Send text written to clipboard register to the clipboard. */
1038 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001041
1042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001043free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001044{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001045 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001046
1047 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001048 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001049 free_yank_all();
1050 vim_free(reg);
1051 *y_current = tmp;
1052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
1054#if defined(FEAT_MOUSE) || defined(PROTO)
1055/*
1056 * return TRUE if the current yank register has type MLINE
1057 */
1058 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
1061 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1062 return FALSE;
1063 if (regname == '_') /* black hole is always empty */
1064 return FALSE;
1065 get_yank_register(regname, FALSE);
1066 return (y_current->y_type == MLINE);
1067}
1068#endif
1069
1070/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001073 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaard55de222007-05-06 13:38:48 +00001078 char_u *p;
1079 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001080 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001081 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 if (Recording == FALSE) /* start recording */
1084 {
1085 /* registers 0-9, a-z and " are allowed */
1086 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1087 retval = FAIL;
1088 else
1089 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01001090 Recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 showmode();
1092 regname = c;
1093 retval = OK;
1094 }
1095 }
1096 else /* stop recording */
1097 {
1098 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001099 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1100 * needs to be removed again to put it in a register. exec_reg then
1101 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 */
1103 Recording = FALSE;
1104 MSG("");
1105 p = get_recorded();
1106 if (p == NULL)
1107 retval = FAIL;
1108 else
1109 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001110 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1111 vim_unescape_csi(p);
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 /*
1114 * We don't want to change the default register here, so save and
1115 * restore the current register name.
1116 */
1117 old_y_previous = y_previous;
1118 old_y_current = y_current;
1119
1120 retval = stuff_yank(regname, p);
1121
1122 y_previous = old_y_previous;
1123 y_current = old_y_current;
1124 }
1125 }
1126 return retval;
1127}
1128
1129/*
1130 * Stuff string "p" into yank register "regname" as a single line (append if
1131 * uppercase). "p" must have been alloced.
1132 *
1133 * return FAIL for failure, OK otherwise
1134 */
1135 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001136stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 char_u *lp;
1139 char_u **pp;
1140
1141 /* check for read-only register */
1142 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1143 {
1144 vim_free(p);
1145 return FAIL;
1146 }
1147 if (regname == '_') /* black hole: don't do anything */
1148 {
1149 vim_free(p);
1150 return OK;
1151 }
1152 get_yank_register(regname, TRUE);
1153 if (y_append && y_current->y_array != NULL)
1154 {
1155 pp = &(y_current->y_array[y_current->y_size - 1]);
1156 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1157 if (lp == NULL)
1158 {
1159 vim_free(p);
1160 return FAIL;
1161 }
1162 STRCPY(lp, *pp);
1163 STRCAT(lp, p);
1164 vim_free(p);
1165 vim_free(*pp);
1166 *pp = lp;
1167 }
1168 else
1169 {
1170 free_yank_all();
1171 if ((y_current->y_array =
1172 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1173 {
1174 vim_free(p);
1175 return FAIL;
1176 }
1177 y_current->y_array[0] = p;
1178 y_current->y_size = 1;
1179 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001180#ifdef FEAT_VIMINFO
1181 y_current->y_time_set = vim_time();
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 return OK;
1185}
1186
Bram Moolenaar42b94362009-05-26 16:12:37 +00001187static int execreg_lastc = NUL;
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001190 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001192 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 */
1194 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001195do_execreg(
1196 int regname,
1197 int colon, /* insert ':' before each line */
1198 int addcr, /* always add '\n' to end of line */
1199 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 long i;
1202 char_u *p;
1203 int retval = OK;
1204 int remap;
1205
1206 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001207 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001208 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
1210 EMSG(_("E748: No previously used register"));
1211 return FAIL;
1212 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 /* check for valid regname */
1216 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001217 {
1218 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001220 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001221 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
1223#ifdef FEAT_CLIPBOARD
1224 regname = may_get_selection(regname);
1225#endif
1226
1227 if (regname == '_') /* black hole: don't stuff anything */
1228 return OK;
1229
1230#ifdef FEAT_CMDHIST
1231 if (regname == ':') /* use last command line */
1232 {
1233 if (last_cmdline == NULL)
1234 {
1235 EMSG(_(e_nolastcmd));
1236 return FAIL;
1237 }
1238 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1239 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001240 /* Escape all control characters with a CTRL-V */
1241 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001242 (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 +00001243 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001244 {
1245 /* When in Visual mode "'<,'>" will be prepended to the command.
1246 * Remove it when it's already there. */
1247 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001248 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001249 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001250 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001251 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001252 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254#endif
1255#ifdef FEAT_EVAL
1256 else if (regname == '=')
1257 {
1258 p = get_expr_line();
1259 if (p == NULL)
1260 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001261 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 vim_free(p);
1263 }
1264#endif
1265 else if (regname == '.') /* use last inserted text */
1266 {
1267 p = get_last_insert_save();
1268 if (p == NULL)
1269 {
1270 EMSG(_(e_noinstext));
1271 return FAIL;
1272 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001273 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 vim_free(p);
1275 }
1276 else
1277 {
1278 get_yank_register(regname, FALSE);
1279 if (y_current->y_array == NULL)
1280 return FAIL;
1281
1282 /* Disallow remaping for ":@r". */
1283 remap = colon ? REMAP_NONE : REMAP_YES;
1284
1285 /*
1286 * Insert lines into typeahead buffer, from last one to first one.
1287 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001288 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 for (i = y_current->y_size; --i >= 0; )
1290 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001291 char_u *escaped;
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 /* insert NL between lines and after last line if type is MLINE */
1294 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1295 || addcr)
1296 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001297 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 return FAIL;
1299 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001300 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1301 if (escaped == NULL)
1302 return FAIL;
1303 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1304 vim_free(escaped);
1305 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001307 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 == FAIL)
1309 return FAIL;
1310 }
1311 Exec_reg = TRUE; /* disable the 'q' command */
1312 }
1313 return retval;
1314}
1315
1316/*
1317 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1318 * used only after other typeahead has been processed.
1319 */
1320 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001321put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 char_u buf[3];
1324
1325 if (restart_edit != NUL)
1326 {
1327 if (restart_edit == 'V')
1328 {
1329 buf[0] = 'g';
1330 buf[1] = 'R';
1331 buf[2] = NUL;
1332 }
1333 else
1334 {
1335 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1336 buf[1] = NUL;
1337 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001338 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 restart_edit = NUL;
1340 }
1341}
1342
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001343/*
1344 * Insert register contents "s" into the typeahead buffer, so that it will be
1345 * executed again.
1346 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1347 * no remapping.
1348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001350put_in_typebuf(
1351 char_u *s,
1352 int esc,
1353 int colon, /* add ':' before the line */
1354 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 int retval = OK;
1357
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001358 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001360 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001362 {
1363 char_u *p;
1364
1365 if (esc)
1366 p = vim_strsave_escape_csi(s);
1367 else
1368 p = s;
1369 if (p == NULL)
1370 retval = FAIL;
1371 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001372 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1373 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001374 if (esc)
1375 vim_free(p);
1376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001378 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 return retval;
1380}
1381
1382/*
1383 * Insert a yank register: copy it into the Read buffer.
1384 * Used by CTRL-R command and middle mouse button in insert mode.
1385 *
1386 * return FAIL for failure, OK otherwise
1387 */
1388 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001389insert_reg(
1390 int regname,
1391 int literally) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 long i;
1394 int retval = OK;
1395 char_u *arg;
1396 int allocated;
1397
1398 /*
1399 * It is possible to get into an endless loop by having CTRL-R a in
1400 * register a and then, in insert mode, doing CTRL-R a.
1401 * If you hit CTRL-C, the loop will be broken here.
1402 */
1403 ui_breakcheck();
1404 if (got_int)
1405 return FAIL;
1406
1407 /* check for valid regname */
1408 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1409 return FAIL;
1410
1411#ifdef FEAT_CLIPBOARD
1412 regname = may_get_selection(regname);
1413#endif
1414
1415 if (regname == '.') /* insert last inserted text */
1416 retval = stuff_inserted(NUL, 1L, TRUE);
1417 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1418 {
1419 if (arg == NULL)
1420 return FAIL;
1421 stuffescaped(arg, literally);
1422 if (allocated)
1423 vim_free(arg);
1424 }
1425 else /* name or number register */
1426 {
1427 get_yank_register(regname, FALSE);
1428 if (y_current->y_array == NULL)
1429 retval = FAIL;
1430 else
1431 {
1432 for (i = 0; i < y_current->y_size; ++i)
1433 {
1434 stuffescaped(y_current->y_array[i], literally);
1435 /*
1436 * Insert a newline between lines and after last line if
1437 * y_type is MLINE.
1438 */
1439 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1440 stuffcharReadbuff('\n');
1441 }
1442 }
1443 }
1444
1445 return retval;
1446}
1447
1448/*
1449 * Stuff a string into the typeahead buffer, such that edit() will insert it
1450 * literally ("literally" TRUE) or interpret is as typed characters.
1451 */
1452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001453stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 int c;
1456 char_u *start;
1457
1458 while (*arg != NUL)
1459 {
1460 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1461 * stuff K_SPECIAL to get the effect of a special key when "literally"
1462 * is TRUE. */
1463 start = arg;
1464 while ((*arg >= ' '
1465#ifndef EBCDIC
1466 && *arg < DEL /* EBCDIC: chars above space are normal */
1467#endif
1468 )
1469 || (*arg == K_SPECIAL && !literally))
1470 ++arg;
1471 if (arg > start)
1472 stuffReadbuffLen(start, (long)(arg - start));
1473
1474 /* stuff a single special character */
1475 if (*arg != NUL)
1476 {
1477#ifdef FEAT_MBYTE
1478 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001479 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 else
1481#endif
1482 c = *arg++;
1483 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1484 stuffcharReadbuff(Ctrl_V);
1485 stuffcharReadbuff(c);
1486 }
1487 }
1488}
1489
1490/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001491 * If "regname" is a special register, return TRUE and store a pointer to its
1492 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001495get_spec_reg(
1496 int regname,
1497 char_u **argp,
1498 int *allocated, /* return: TRUE when value was allocated */
1499 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 int cnt;
1502
1503 *argp = NULL;
1504 *allocated = FALSE;
1505 switch (regname)
1506 {
1507 case '%': /* file name */
1508 if (errmsg)
1509 check_fname(); /* will give emsg if not set */
1510 *argp = curbuf->b_fname;
1511 return TRUE;
1512
1513 case '#': /* alternate file name */
1514 *argp = getaltfname(errmsg); /* may give emsg if not set */
1515 return TRUE;
1516
1517#ifdef FEAT_EVAL
1518 case '=': /* result of expression */
1519 *argp = get_expr_line();
1520 *allocated = TRUE;
1521 return TRUE;
1522#endif
1523
1524 case ':': /* last command line */
1525 if (last_cmdline == NULL && errmsg)
1526 EMSG(_(e_nolastcmd));
1527 *argp = last_cmdline;
1528 return TRUE;
1529
1530 case '/': /* last search-pattern */
1531 if (last_search_pat() == NULL && errmsg)
1532 EMSG(_(e_noprevre));
1533 *argp = last_search_pat();
1534 return TRUE;
1535
1536 case '.': /* last inserted text */
1537 *argp = get_last_insert_save();
1538 *allocated = TRUE;
1539 if (*argp == NULL && errmsg)
1540 EMSG(_(e_noinstext));
1541 return TRUE;
1542
1543#ifdef FEAT_SEARCHPATH
1544 case Ctrl_F: /* Filename under cursor */
1545 case Ctrl_P: /* Path under cursor, expand via "path" */
1546 if (!errmsg)
1547 return FALSE;
1548 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001549 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 *allocated = TRUE;
1551 return TRUE;
1552#endif
1553
1554 case Ctrl_W: /* word under cursor */
1555 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1556 if (!errmsg)
1557 return FALSE;
1558 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1559 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1560 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1561 *allocated = TRUE;
1562 return TRUE;
1563
1564 case '_': /* black hole: always empty */
1565 *argp = (char_u *)"";
1566 return TRUE;
1567 }
1568
1569 return FALSE;
1570}
1571
1572/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001573 * Paste a yank register into the command line.
1574 * Only for non-special registers.
1575 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 * insert_reg() can't be used here, because special characters from the
1577 * register contents will be interpreted as commands.
1578 *
1579 * return FAIL for failure, OK otherwise
1580 */
1581 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001582cmdline_paste_reg(
1583 int regname,
1584 int literally, /* Insert text literally instead of "as typed" */
1585 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589 get_yank_register(regname, FALSE);
1590 if (y_current->y_array == NULL)
1591 return FAIL;
1592
1593 for (i = 0; i < y_current->y_size; ++i)
1594 {
1595 cmdline_paste_str(y_current->y_array[i], literally);
1596
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001597 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001598 * Don't do this when "remcr" is TRUE. */
1599 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 cmdline_paste_str((char_u *)"\r", literally);
1601
1602 /* Check for CTRL-C, in case someone tries to paste a few thousand
1603 * lines and gets bored. */
1604 ui_breakcheck();
1605 if (got_int)
1606 return FAIL;
1607 }
1608 return OK;
1609}
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1612/*
1613 * Adjust the register name pointed to with "rp" for the clipboard being
1614 * used always and the clipboard being available.
1615 */
1616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001617adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001619 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1620 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001621 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1622 {
1623 if (clip_unnamed != 0)
1624 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001625 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001626 else
1627 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1628 ? '+' : '*';
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (!clip_star.available && *rp == '*')
1631 *rp = 0;
1632 if (!clip_plus.available && *rp == '+')
1633 *rp = 0;
1634}
1635#endif
1636
1637/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001638 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1639 */
1640 void
1641shift_delete_registers()
1642{
1643 int n;
1644
1645 y_current = &y_regs[9];
1646 free_yank_all(); /* free register nine */
1647 for (n = 9; n > 1; --n)
1648 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001649 y_current = &y_regs[1];
1650 if (!y_append)
1651 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001652 y_regs[1].y_array = NULL; /* set register one to empty */
1653}
1654
Bram Moolenaaree219b02017-12-17 14:55:01 +01001655#ifdef FEAT_AUTOCMD
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001656 static void
1657yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1658{
1659 static int recursive = FALSE;
1660 dict_T *v_event;
1661 list_T *list;
1662 int n;
1663 char_u buf[NUMBUFLEN + 2];
1664 long reglen = 0;
1665
1666 if (recursive)
1667 return;
1668
1669 v_event = get_vim_var_dict(VV_EVENT);
1670
1671 list = list_alloc();
1672 for (n = 0; n < reg->y_size; n++)
1673 list_append_string(list, reg->y_array[n], -1);
1674 list->lv_lock = VAR_FIXED;
1675 dict_add_list(v_event, "regcontents", list);
1676
1677 buf[0] = (char_u)oap->regname;
1678 buf[1] = NUL;
1679 dict_add_nr_str(v_event, "regname", 0, buf);
1680
1681 buf[0] = get_op_char(oap->op_type);
1682 buf[1] = get_extra_op_char(oap->op_type);
1683 buf[2] = NUL;
1684 dict_add_nr_str(v_event, "operator", 0, buf);
1685
1686 buf[0] = NUL;
1687 buf[1] = NUL;
1688 switch (get_reg_type(oap->regname, &reglen))
1689 {
1690 case MLINE: buf[0] = 'V'; break;
1691 case MCHAR: buf[0] = 'v'; break;
1692 case MBLOCK:
1693 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1694 reglen + 1);
1695 break;
1696 }
1697 dict_add_nr_str(v_event, "regtype", 0, buf);
1698
1699 /* Lock the dictionary and its keys */
1700 dict_set_items_ro(v_event);
1701
1702 recursive = TRUE;
1703 textlock++;
1704 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1705 textlock--;
1706 recursive = FALSE;
1707
1708 /* Empty the dictionary, v:event is still valid */
1709 dict_free_contents(v_event);
1710 hash_init(&v_event->dv_hashtab);
1711}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001712#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001713
Bram Moolenaara1891842017-02-04 21:34:31 +01001714/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001715 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001717 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
1719 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001720op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722 int n;
1723 linenr_T lnum;
1724 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 char_u *newp, *oldp;
1726 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1728 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001729 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1732 return OK;
1733
1734 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1735 if (oap->empty)
1736 return u_save_cursor();
1737
1738 if (!curbuf->b_p_ma)
1739 {
1740 EMSG(_(e_modifiable));
1741 return FAIL;
1742 }
1743
1744#ifdef FEAT_CLIPBOARD
1745 adjust_clip_reg(&oap->regname);
1746#endif
1747
1748#ifdef FEAT_MBYTE
1749 if (has_mbyte)
1750 mb_adjust_opend(oap);
1751#endif
1752
Bram Moolenaard04b7502010-07-08 22:27:55 +02001753 /*
1754 * Imitate the strange Vi behaviour: If the delete spans more than one
1755 * line and motion_type == MCHAR and the result is a blank line, make the
1756 * delete linewise. Don't do this for the change command or Visual mode.
1757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001760 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001762 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 && oap->op_type == OP_DELETE)
1764 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001765 ptr = ml_get(oap->end.lnum) + oap->end.col;
1766 if (*ptr != NUL)
1767 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 ptr = skipwhite(ptr);
1769 if (*ptr == NUL && inindent(0))
1770 oap->motion_type = MLINE;
1771 }
1772
Bram Moolenaard04b7502010-07-08 22:27:55 +02001773 /*
1774 * Check for trying to delete (e.g. "D") in an empty line.
1775 * Note: For the change operator it is ok.
1776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 if ( oap->motion_type == MCHAR
1778 && oap->line_count == 1
1779 && oap->op_type == OP_DELETE
1780 && *ml_get(oap->start.lnum) == NUL)
1781 {
1782 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001783 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 * 'cpoptions' (Vi compatible).
1785 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001786#ifdef FEAT_VIRTUALEDIT
1787 if (virtual_op)
1788 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1789 * marks as if it happened. */
1790 goto setmarks;
1791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1793 beep_flush();
1794 return OK;
1795 }
1796
Bram Moolenaard04b7502010-07-08 22:27:55 +02001797 /*
1798 * Do a yank of whatever we're about to delete.
1799 * If a yank register was specified, put the deleted text into that
1800 * register. For the black hole register '_' don't yank anything.
1801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (oap->regname != '_')
1803 {
1804 if (oap->regname != 0)
1805 {
1806 /* check for read-only register */
1807 if (!valid_yank_reg(oap->regname, TRUE))
1808 {
1809 beep_flush();
1810 return OK;
1811 }
1812 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1813 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1814 did_yank = TRUE;
1815 }
1816
1817 /*
1818 * Put deleted text into register 1 and shift number registers if the
1819 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001820 * Use the register name from before adjust_clip_reg() may have
1821 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001823 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 || oap->line_count > 1 || oap->use_reg_one)
1825 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001826 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (op_yank(oap, TRUE, FALSE) == OK)
1828 did_yank = TRUE;
1829 }
1830
Bram Moolenaar84298db2012-04-20 13:46:08 +02001831 /* Yank into small delete register when no named register specified
1832 * and the delete is within one line. */
1833 if ((
1834#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001835 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1836 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001837#endif
1838 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 && oap->line_count == 1)
1840 {
1841 oap->regname = '-';
1842 get_yank_register(oap->regname, TRUE);
1843 if (op_yank(oap, TRUE, FALSE) == OK)
1844 did_yank = TRUE;
1845 oap->regname = 0;
1846 }
1847
1848 /*
1849 * If there's too much stuff to fit in the yank register, then get a
1850 * confirmation before doing the delete. This is crude, but simple.
1851 * And it avoids doing a delete of something we can't put back if we
1852 * want.
1853 */
1854 if (!did_yank)
1855 {
1856 int msg_silent_save = msg_silent;
1857
1858 msg_silent = 0; /* must display the prompt */
1859 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1860 msg_silent = msg_silent_save;
1861 if (n != 'y')
1862 {
1863 EMSG(_(e_abort));
1864 return FAIL;
1865 }
1866 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001867
1868#ifdef FEAT_AUTOCMD
1869 if (did_yank && has_textyankpost())
1870 yank_do_autocmd(oap, y_current);
1871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873
Bram Moolenaard04b7502010-07-08 22:27:55 +02001874 /*
1875 * block mode delete
1876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (oap->block_mode)
1878 {
1879 if (u_save((linenr_T)(oap->start.lnum - 1),
1880 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1881 return FAIL;
1882
1883 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1884 {
1885 block_prep(oap, &bd, lnum, TRUE);
1886 if (bd.textlen == 0) /* nothing to delete */
1887 continue;
1888
1889 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1890 if (lnum == curwin->w_cursor.lnum)
1891 {
1892 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1893# ifdef FEAT_VIRTUALEDIT
1894 curwin->w_cursor.coladd = 0;
1895# endif
1896 }
1897
1898 /* n == number of chars deleted
1899 * If we delete a TAB, it may be replaced by several characters.
1900 * Thus the number of characters may increase!
1901 */
1902 n = bd.textlen - bd.startspaces - bd.endspaces;
1903 oldp = ml_get(lnum);
1904 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1905 if (newp == NULL)
1906 continue;
1907 /* copy up to deleted part */
1908 mch_memmove(newp, oldp, (size_t)bd.textcol);
1909 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001910 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 (size_t)(bd.startspaces + bd.endspaces));
1912 /* copy the part after the deleted part */
1913 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001914 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 /* replace the line */
1916 ml_replace(lnum, newp, FALSE);
1917 }
1918
1919 check_cursor_col();
1920 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1921 oap->end.lnum + 1, 0L);
1922 oap->line_count = 0; /* no lines deleted */
1923 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001924 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 if (oap->op_type == OP_CHANGE)
1927 {
1928 /* Delete the lines except the first one. Temporarily move the
1929 * cursor to the next line. Save the current line number, if the
1930 * last line is deleted it may be changed.
1931 */
1932 if (oap->line_count > 1)
1933 {
1934 lnum = curwin->w_cursor.lnum;
1935 ++curwin->w_cursor.lnum;
1936 del_lines((long)(oap->line_count - 1), TRUE);
1937 curwin->w_cursor.lnum = lnum;
1938 }
1939 if (u_save_cursor() == FAIL)
1940 return FAIL;
1941 if (curbuf->b_p_ai) /* don't delete indent */
1942 {
1943 beginline(BL_WHITE); /* cursor on first non-white */
1944 did_ai = TRUE; /* delete the indent when ESC hit */
1945 ai_col = curwin->w_cursor.col;
1946 }
1947 else
1948 beginline(0); /* cursor in column 0 */
1949 truncate_line(FALSE); /* delete the rest of the line */
1950 /* leave cursor past last char in line */
1951 if (oap->line_count > 1)
1952 u_clearline(); /* "U" command not possible after "2cc" */
1953 }
1954 else
1955 {
1956 del_lines(oap->line_count, TRUE);
1957 beginline(BL_WHITE | BL_FIX);
1958 u_clearline(); /* "U" command not possible after "dd" */
1959 }
1960 }
1961 else
1962 {
1963#ifdef FEAT_VIRTUALEDIT
1964 if (virtual_op)
1965 {
1966 int endcol = 0;
1967
1968 /* For virtualedit: break the tabs that are partly included. */
1969 if (gchar_pos(&oap->start) == '\t')
1970 {
1971 if (u_save_cursor() == FAIL) /* save first line for undo */
1972 return FAIL;
1973 if (oap->line_count == 1)
1974 endcol = getviscol2(oap->end.col, oap->end.coladd);
1975 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1976 oap->start = curwin->w_cursor;
1977 if (oap->line_count == 1)
1978 {
1979 coladvance(endcol);
1980 oap->end.col = curwin->w_cursor.col;
1981 oap->end.coladd = curwin->w_cursor.coladd;
1982 curwin->w_cursor = oap->start;
1983 }
1984 }
1985
1986 /* Break a tab only when it's included in the area. */
1987 if (gchar_pos(&oap->end) == '\t'
1988 && (int)oap->end.coladd < oap->inclusive)
1989 {
1990 /* save last line for undo */
1991 if (u_save((linenr_T)(oap->end.lnum - 1),
1992 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1993 return FAIL;
1994 curwin->w_cursor = oap->end;
1995 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1996 oap->end = curwin->w_cursor;
1997 curwin->w_cursor = oap->start;
1998 }
1999 }
2000#endif
2001
2002 if (oap->line_count == 1) /* delete characters within one line */
2003 {
2004 if (u_save_cursor() == FAIL) /* save line for undo */
2005 return FAIL;
2006
2007 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002008 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 && oap->op_type == OP_CHANGE
2010 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002011 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 display_dollar(oap->end.col - !oap->inclusive);
2013
2014 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2015
2016#ifdef FEAT_VIRTUALEDIT
2017 if (virtual_op)
2018 {
2019 /* fix up things for virtualedit-delete:
2020 * break the tabs which are going to get in our way
2021 */
2022 char_u *curline = ml_get_curline();
2023 int len = (int)STRLEN(curline);
2024
2025 if (oap->end.coladd != 0
2026 && (int)oap->end.col >= len - 1
2027 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2028 n++;
2029 /* Delete at least one char (e.g, when on a control char). */
2030 if (n == 0 && oap->start.coladd != oap->end.coladd)
2031 n = 1;
2032
2033 /* When deleted a char in the line, reset coladd. */
2034 if (gchar_cursor() != NUL)
2035 curwin->w_cursor.coladd = 0;
2036 }
2037#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002038 (void)del_bytes((long)n, !virtual_op,
2039 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
2041 else /* delete characters between lines */
2042 {
2043 pos_T curpos;
2044
2045 /* save deleted and changed lines for undo */
2046 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2047 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2048 return FAIL;
2049
2050 truncate_line(TRUE); /* delete from cursor to end of line */
2051
2052 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2053 ++curwin->w_cursor.lnum;
2054 del_lines((long)(oap->line_count - 2), FALSE);
2055
Bram Moolenaard009e862015-06-09 20:20:03 +02002056 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002057 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002058 curwin->w_cursor.col = 0;
2059 (void)del_bytes((long)n, !virtual_op,
2060 oap->op_type == OP_DELETE && !oap->is_VIsual);
2061 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2062 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064 }
2065
2066 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2067
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002068#ifdef FEAT_VIRTUALEDIT
2069setmarks:
2070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (oap->block_mode)
2072 {
2073 curbuf->b_op_end.lnum = oap->end.lnum;
2074 curbuf->b_op_end.col = oap->start.col;
2075 }
2076 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 curbuf->b_op_end = oap->start;
2078 curbuf->b_op_start = oap->start;
2079
2080 return OK;
2081}
2082
2083#ifdef FEAT_MBYTE
2084/*
2085 * Adjust end of operating area for ending on a multi-byte character.
2086 * Used for deletion.
2087 */
2088 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002089mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
2091 char_u *p;
2092
2093 if (oap->inclusive)
2094 {
2095 p = ml_get(oap->end.lnum);
2096 oap->end.col += mb_tail_off(p, p + oap->end.col);
2097 }
2098}
2099#endif
2100
2101#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2102/*
2103 * Replace a whole area with one character.
2104 */
2105 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002106op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
2108 int n, numc;
2109#ifdef FEAT_MBYTE
2110 int num_chars;
2111#endif
2112 char_u *newp, *oldp;
2113 size_t oldlen;
2114 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002115 char_u *after_p = NULL;
2116 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
2118 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2119 return OK; /* nothing to do */
2120
Bram Moolenaard9820532013-11-04 01:41:17 +01002121 if (had_ctrl_v_cr)
2122 c = (c == -1 ? '\r' : '\n');
2123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef FEAT_MBYTE
2125 if (has_mbyte)
2126 mb_adjust_opend(oap);
2127#endif
2128
2129 if (u_save((linenr_T)(oap->start.lnum - 1),
2130 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2131 return FAIL;
2132
2133 /*
2134 * block mode replace
2135 */
2136 if (oap->block_mode)
2137 {
2138 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2139 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2140 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002141 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2143 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2144 continue; /* nothing to replace */
2145
2146 /* n == number of extra chars required
2147 * If we split a TAB, it may be replaced by several characters.
2148 * Thus the number of characters may increase!
2149 */
2150#ifdef FEAT_VIRTUALEDIT
2151 /* If the range starts in virtual space, count the initial
2152 * coladd offset as part of "startspaces" */
2153 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2154 {
2155 pos_T vpos;
2156
Bram Moolenaara1381de2009-11-03 15:44:21 +00002157 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 getvpos(&vpos, oap->start_vcol);
2159 bd.startspaces += vpos.coladd;
2160 n = bd.startspaces;
2161 }
2162 else
2163#endif
2164 /* allow for pre spaces */
2165 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2166
2167 /* allow for post spp */
2168 n += (bd.endspaces
2169#ifdef FEAT_VIRTUALEDIT
2170 && !bd.is_oneChar
2171#endif
2172 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2173 /* Figure out how many characters to replace. */
2174 numc = oap->end_vcol - oap->start_vcol + 1;
2175 if (bd.is_short && (!virtual_op || bd.is_MAX))
2176 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2177
2178#ifdef FEAT_MBYTE
2179 /* A double-wide character can be replaced only up to half the
2180 * times. */
2181 if ((*mb_char2cells)(c) > 1)
2182 {
2183 if ((numc & 1) && !bd.is_short)
2184 {
2185 ++bd.endspaces;
2186 ++n;
2187 }
2188 numc = numc / 2;
2189 }
2190
2191 /* Compute bytes needed, move character count to num_chars. */
2192 num_chars = numc;
2193 numc *= (*mb_char2len)(c);
2194#endif
2195 /* oldlen includes textlen, so don't double count */
2196 n += numc - bd.textlen;
2197
2198 oldp = ml_get_curline();
2199 oldlen = STRLEN(oldp);
2200 newp = alloc_check((unsigned)oldlen + 1 + n);
2201 if (newp == NULL)
2202 continue;
2203 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2204 /* copy up to deleted part */
2205 mch_memmove(newp, oldp, (size_t)bd.textcol);
2206 oldp += bd.textcol + bd.textlen;
2207 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002208 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002210 /* -1/-2 is used for entering CR literally. */
2211 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002213#ifdef FEAT_MBYTE
2214 if (has_mbyte)
2215 {
2216 n = (int)STRLEN(newp);
2217 while (--num_chars >= 0)
2218 n += (*mb_char2bytes)(c, newp + n);
2219 }
2220 else
2221#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002222 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002223 if (!bd.is_short)
2224 {
2225 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002226 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002227 /* copy the part after the changed part */
2228 STRMOVE(newp + STRLEN(newp), oldp);
2229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002233 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002234 after_p = alloc_check(
2235 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002236 if (after_p != NULL)
2237 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
2239 /* replace the line */
2240 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002241 if (after_p != NULL)
2242 {
2243 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2244 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2245 oap->end.lnum++;
2246 vim_free(after_p);
2247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249 }
2250 else
2251 {
2252 /*
2253 * MCHAR and MLINE motion replace.
2254 */
2255 if (oap->motion_type == MLINE)
2256 {
2257 oap->start.col = 0;
2258 curwin->w_cursor.col = 0;
2259 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2260 if (oap->end.col)
2261 --oap->end.col;
2262 }
2263 else if (!oap->inclusive)
2264 dec(&(oap->end));
2265
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002266 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
2268 n = gchar_cursor();
2269 if (n != NUL)
2270 {
2271#ifdef FEAT_MBYTE
2272 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2273 {
2274 /* This is slow, but it handles replacing a single-byte
2275 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002276 if (curwin->w_cursor.lnum == oap->end.lnum)
2277 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 n = State;
2279 State = REPLACE;
2280 ins_char(c);
2281 State = n;
2282 /* Backup to the replaced character. */
2283 dec_cursor();
2284 }
2285 else
2286#endif
2287 {
2288#ifdef FEAT_VIRTUALEDIT
2289 if (n == TAB)
2290 {
2291 int end_vcol = 0;
2292
2293 if (curwin->w_cursor.lnum == oap->end.lnum)
2294 {
2295 /* oap->end has to be recalculated when
2296 * the tab breaks */
2297 end_vcol = getviscol2(oap->end.col,
2298 oap->end.coladd);
2299 }
2300 coladvance_force(getviscol());
2301 if (curwin->w_cursor.lnum == oap->end.lnum)
2302 getvpos(&oap->end, end_vcol);
2303 }
2304#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002305 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
2307 }
2308#ifdef FEAT_VIRTUALEDIT
2309 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2310 {
2311 int virtcols = oap->end.coladd;
2312
2313 if (curwin->w_cursor.lnum == oap->start.lnum
2314 && oap->start.col == oap->end.col && oap->start.coladd)
2315 virtcols -= oap->start.coladd;
2316
2317 /* oap->end has been trimmed so it's effectively inclusive;
2318 * as a result an extra +1 must be counted so we don't
2319 * trample the NUL byte. */
2320 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2321 curwin->w_cursor.col -= (virtcols + 1);
2322 for (; virtcols >= 0; virtcols--)
2323 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002324 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (inc(&curwin->w_cursor) == -1)
2326 break;
2327 }
2328 }
2329#endif
2330
2331 /* Advance to next character, stop at the end of the file. */
2332 if (inc_cursor() == -1)
2333 break;
2334 }
2335 }
2336
2337 curwin->w_cursor = oap->start;
2338 check_cursor();
2339 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2340
2341 /* Set "'[" and "']" marks. */
2342 curbuf->b_op_start = oap->start;
2343 curbuf->b_op_end = oap->end;
2344
2345 return OK;
2346}
2347#endif
2348
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002349static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351/*
2352 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2353 */
2354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002355op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
2357 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002359 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 if (u_save((linenr_T)(oap->start.lnum - 1),
2362 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2363 return;
2364
2365 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (oap->block_mode) /* Visual block mode */
2367 {
2368 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2369 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002370 int one_change;
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 block_prep(oap, &bd, pos.lnum, FALSE);
2373 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002374 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2375 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002376
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002377#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002378 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
2380 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2381
Bram Moolenaar009b2592004-10-24 19:18:58 +00002382 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2383 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002385 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389 if (did_change)
2390 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2391 }
2392 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 if (oap->motion_type == MLINE)
2395 {
2396 oap->start.col = 0;
2397 pos.col = 0;
2398 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2399 if (oap->end.col)
2400 --oap->end.col;
2401 }
2402 else if (!oap->inclusive)
2403 dec(&(oap->end));
2404
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002405 if (pos.lnum == oap->end.lnum)
2406 did_change = swapchars(oap->op_type, &pos,
2407 oap->end.col - pos.col + 1);
2408 else
2409 for (;;)
2410 {
2411 did_change |= swapchars(oap->op_type, &pos,
2412 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2413 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002414 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002415 break;
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (did_change)
2418 {
2419 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2420 0L);
2421#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002422 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 char_u *ptr;
2425 int count;
2426
2427 pos = oap->start;
2428 while (pos.lnum < oap->end.lnum)
2429 {
2430 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002431 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002432 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002434 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 pos.col = 0;
2436 pos.lnum++;
2437 }
2438 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2439 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002440 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002442 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444#endif
2445 }
2446 }
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 if (!did_change && oap->is_VIsual)
2449 /* No change: need to remove the Visual selection */
2450 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 /*
2453 * Set '[ and '] marks.
2454 */
2455 curbuf->b_op_start = oap->start;
2456 curbuf->b_op_end = oap->end;
2457
2458 if (oap->line_count > p_report)
2459 {
2460 if (oap->line_count == 1)
2461 MSG(_("1 line changed"));
2462 else
2463 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2464 }
2465}
2466
2467/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002468 * Invoke swapchar() on "length" bytes at position "pos".
2469 * "pos" is advanced to just after the changed characters.
2470 * "length" is rounded up to include the whole last multi-byte character.
2471 * Also works correctly when the number of bytes changes.
2472 * Returns TRUE if some character was changed.
2473 */
2474 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002475swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002476{
2477 int todo;
2478 int did_change = 0;
2479
2480 for (todo = length; todo > 0; --todo)
2481 {
2482# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002483 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002484 {
2485 int len = (*mb_ptr2len)(ml_get_pos(pos));
2486
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002487 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002488 if (len > 0)
2489 todo -= len - 1;
2490 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002491# endif
2492 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002493 if (inc(pos) == -1) /* at end of file */
2494 break;
2495 }
2496 return did_change;
2497}
2498
2499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 * If op_type == OP_UPPER: make uppercase,
2501 * if op_type == OP_LOWER: make lowercase,
2502 * if op_type == OP_ROT13: do rot13 encoding,
2503 * else swap case of character at 'pos'
2504 * returns TRUE when something actually changed.
2505 */
2506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002507swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 int c;
2510 int nc;
2511
2512 c = gchar_pos(pos);
2513
2514 /* Only do rot13 encoding for ASCII characters. */
2515 if (c >= 0x80 && op_type == OP_ROT13)
2516 return FALSE;
2517
2518#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002519 if (op_type == OP_UPPER && c == 0xdf
2520 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002521 {
2522 pos_T sp = curwin->w_cursor;
2523
2524 /* Special handling of German sharp s: change to "SS". */
2525 curwin->w_cursor = *pos;
2526 del_char(FALSE);
2527 ins_char('S');
2528 ins_char('S');
2529 curwin->w_cursor = sp;
2530 inc(pos);
2531 }
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2534 return FALSE;
2535#endif
2536 nc = c;
2537 if (MB_ISLOWER(c))
2538 {
2539 if (op_type == OP_ROT13)
2540 nc = ROT13(c, 'a');
2541 else if (op_type != OP_LOWER)
2542 nc = MB_TOUPPER(c);
2543 }
2544 else if (MB_ISUPPER(c))
2545 {
2546 if (op_type == OP_ROT13)
2547 nc = ROT13(c, 'A');
2548 else if (op_type != OP_UPPER)
2549 nc = MB_TOLOWER(c);
2550 }
2551 if (nc != c)
2552 {
2553#ifdef FEAT_MBYTE
2554 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2555 {
2556 pos_T sp = curwin->w_cursor;
2557
2558 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002559 /* don't use del_char(), it also removes composing chars */
2560 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 ins_char(nc);
2562 curwin->w_cursor = sp;
2563 }
2564 else
2565#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002566 PCHAR(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 return TRUE;
2568 }
2569 return FALSE;
2570}
2571
2572#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2573/*
2574 * op_insert - Insert and append operators for Visual mode.
2575 */
2576 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002577op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579 long ins_len, pre_textlen = 0;
2580 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002581 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 struct block_def bd;
2583 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002584 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586 /* edit() changes this - record it for OP_APPEND */
2587 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2588
2589 /* vis block is still marked. Get rid of it now. */
2590 curwin->w_cursor.lnum = oap->start.lnum;
2591 update_screen(INVERTED);
2592
2593 if (oap->block_mode)
2594 {
2595#ifdef FEAT_VIRTUALEDIT
2596 /* When 'virtualedit' is used, need to insert the extra spaces before
2597 * doing block_prep(). When only "block" is used, virtual edit is
2598 * already disabled, but still need it when calling
2599 * coladvance_force(). */
2600 if (curwin->w_cursor.coladd > 0)
2601 {
2602 int old_ve_flags = ve_flags;
2603
2604 ve_flags = VE_ALL;
2605 if (u_save_cursor() == FAIL)
2606 return;
2607 coladvance_force(oap->op_type == OP_APPEND
2608 ? oap->end_vcol + 1 : getviscol());
2609 if (oap->op_type == OP_APPEND)
2610 --curwin->w_cursor.col;
2611 ve_flags = old_ve_flags;
2612 }
2613#endif
2614 /* Get the info about the block before entering the text */
2615 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002616 /* Get indent information */
2617 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (oap->op_type == OP_APPEND)
2621 firstline += bd.textlen;
2622 pre_textlen = (long)STRLEN(firstline);
2623 }
2624
2625 if (oap->op_type == OP_APPEND)
2626 {
2627 if (oap->block_mode
2628#ifdef FEAT_VIRTUALEDIT
2629 && curwin->w_cursor.coladd == 0
2630#endif
2631 )
2632 {
2633 /* Move the cursor to the character right of the block. */
2634 curwin->w_set_curswant = TRUE;
2635 while (*ml_get_cursor() != NUL
2636 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2637 ++curwin->w_cursor.col;
2638 if (bd.is_short && !bd.is_MAX)
2639 {
2640 /* First line was too short, make it longer and adjust the
2641 * values in "bd". */
2642 if (u_save_cursor() == FAIL)
2643 return;
2644 for (i = 0; i < bd.endspaces; ++i)
2645 ins_char(' ');
2646 bd.textlen += bd.endspaces;
2647 }
2648 }
2649 else
2650 {
2651 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002652 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002655 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 && oap->start_vcol != oap->end_vcol)
2657 inc_cursor();
2658 }
2659 }
2660
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002661 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002662 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002664 /* When a tab was inserted, and the characters in front of the tab
2665 * have been converted to a tab as well, the column of the cursor
2666 * might have actually been reduced, so need to adjust here. */
2667 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002668 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002669 oap->start = curbuf->b_op_start_orig;
2670
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002671 /* If user has moved off this line, we don't know what to do, so do
2672 * nothing.
2673 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2674 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 return;
2676
2677 if (oap->block_mode)
2678 {
2679 struct block_def bd2;
2680
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002681 /* If indent kicked in, the firstline might have changed
2682 * but only do that, if the indent actually increased. */
2683 ind_post = (colnr_T)getwhitecols_curline();
2684 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2685 {
2686 bd.textcol += ind_post - ind_pre;
2687 bd.start_vcol += ind_post - ind_pre;
2688 }
2689
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002690 /* The user may have moved the cursor before inserting something, try
2691 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002692 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002693 {
2694 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002695 && oap->start.col
2696#ifdef FEAT_VIRTUALEDIT
2697 + oap->start.coladd
2698#endif
2699 != curbuf->b_op_start_orig.col
2700#ifdef FEAT_VIRTUALEDIT
2701 + curbuf->b_op_start_orig.coladd
2702#endif
2703 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002704 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002705 int t = getviscol2(curbuf->b_op_start_orig.col,
2706 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002707 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002708 pre_textlen -= t - oap->start_vcol;
2709 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002710 }
2711 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002712 && oap->end.col
2713#ifdef FEAT_VIRTUALEDIT
2714 + oap->end.coladd
2715#endif
2716 >= curbuf->b_op_start_orig.col
2717#ifdef FEAT_VIRTUALEDIT
2718 + curbuf->b_op_start_orig.coladd
2719#endif
2720 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002721 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002722 int t = getviscol2(curbuf->b_op_start_orig.col,
2723 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002724 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002725 /* reset pre_textlen to the value of OP_INSERT */
2726 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002727 pre_textlen -= t - oap->start_vcol;
2728 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002729 oap->op_type = OP_INSERT;
2730 }
2731 }
2732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 /*
2734 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002735 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 * Don't do this when "$" used, end-of-line will have changed.
2737 */
2738 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2739 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2740 {
2741 if (oap->op_type == OP_APPEND)
2742 {
2743 pre_textlen += bd2.textlen - bd.textlen;
2744 if (bd2.endspaces)
2745 --bd2.textlen;
2746 }
2747 bd.textcol = bd2.textcol;
2748 bd.textlen = bd2.textlen;
2749 }
2750
2751 /*
2752 * Subsequent calls to ml_get() flush the firstline data - take a
2753 * copy of the required string.
2754 */
2755 firstline = ml_get(oap->start.lnum) + bd.textcol;
2756 if (oap->op_type == OP_APPEND)
2757 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002758 if (pre_textlen >= 0
2759 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 ins_text = vim_strnsave(firstline, (int)ins_len);
2762 if (ins_text != NULL)
2763 {
2764 /* block handled here */
2765 if (u_save(oap->start.lnum,
2766 (linenr_T)(oap->end.lnum + 1)) == OK)
2767 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2768 &bd);
2769
2770 curwin->w_cursor.col = oap->start.col;
2771 check_cursor();
2772 vim_free(ins_text);
2773 }
2774 }
2775 }
2776}
2777#endif
2778
2779/*
2780 * op_change - handle a change operation
2781 *
2782 * return TRUE if edit() returns because of a CTRL-O command
2783 */
2784 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002785op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787 colnr_T l;
2788 int retval;
2789#ifdef FEAT_VISUALEXTRA
2790 long offset;
2791 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002792 long ins_len;
2793 long pre_textlen = 0;
2794 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 char_u *firstline;
2796 char_u *ins_text, *newp, *oldp;
2797 struct block_def bd;
2798#endif
2799
2800 l = oap->start.col;
2801 if (oap->motion_type == MLINE)
2802 {
2803 l = 0;
2804#ifdef FEAT_SMARTINDENT
2805 if (!p_paste && curbuf->b_p_si
2806# ifdef FEAT_CINDENT
2807 && !curbuf->b_p_cin
2808# endif
2809 )
2810 can_si = TRUE; /* It's like opening a new line, do si */
2811#endif
2812 }
2813
2814 /* First delete the text in the region. In an empty buffer only need to
2815 * save for undo */
2816 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2817 {
2818 if (u_save_cursor() == FAIL)
2819 return FALSE;
2820 }
2821 else if (op_delete(oap) == FAIL)
2822 return FALSE;
2823
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002824 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 && !virtual_op)
2826 inc_cursor();
2827
2828#ifdef FEAT_VISUALEXTRA
2829 /* check for still on same line (<CR> in inserted text meaningless) */
2830 /* skip blank lines too */
2831 if (oap->block_mode)
2832 {
2833# ifdef FEAT_VIRTUALEDIT
2834 /* Add spaces before getting the current line length. */
2835 if (virtual_op && (curwin->w_cursor.coladd > 0
2836 || gchar_cursor() == NUL))
2837 coladvance_force(getviscol());
2838# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002839 firstline = ml_get(oap->start.lnum);
2840 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002841 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 bd.textcol = curwin->w_cursor.col;
2843 }
2844#endif
2845
2846#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2847 if (oap->motion_type == MLINE)
2848 fix_indent();
2849#endif
2850
2851 retval = edit(NUL, FALSE, (linenr_T)1);
2852
2853#ifdef FEAT_VISUALEXTRA
2854 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002855 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002857 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002859 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002861 /* Auto-indenting may have changed the indent. If the cursor was past
2862 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002864 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002866 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002867
2868 pre_textlen += new_indent - pre_indent;
2869 bd.textcol += new_indent - pre_indent;
2870 }
2871
2872 ins_len = (long)STRLEN(firstline) - pre_textlen;
2873 if (ins_len > 0)
2874 {
2875 /* Subsequent calls to ml_get() flush the firstline data - take a
2876 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2878 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002879 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2881 linenr++)
2882 {
2883 block_prep(oap, &bd, linenr, TRUE);
2884 if (!bd.is_short || virtual_op)
2885 {
2886# ifdef FEAT_VIRTUALEDIT
2887 pos_T vpos;
2888
2889 /* If the block starts in virtual space, count the
2890 * initial coladd offset as part of "startspaces" */
2891 if (bd.is_short)
2892 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002893 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 else
2897 vpos.coladd = 0;
2898# endif
2899 oldp = ml_get(linenr);
2900 newp = alloc_check((unsigned)(STRLEN(oldp)
2901# ifdef FEAT_VIRTUALEDIT
2902 + vpos.coladd
2903# endif
2904 + ins_len + 1));
2905 if (newp == NULL)
2906 continue;
2907 /* copy up to block start */
2908 mch_memmove(newp, oldp, (size_t)bd.textcol);
2909 offset = bd.textcol;
2910# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002911 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 offset += vpos.coladd;
2913# endif
2914 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2915 offset += ins_len;
2916 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002917 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 ml_replace(linenr, newp, FALSE);
2919 }
2920 }
2921 check_cursor();
2922
2923 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2924 }
2925 vim_free(ins_text);
2926 }
2927 }
2928#endif
2929
2930 return retval;
2931}
2932
2933/*
2934 * set all the yank registers to empty (called from main())
2935 */
2936 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002937init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
2939 int i;
2940
2941 for (i = 0; i < NUM_REGISTERS; ++i)
2942 y_regs[i].y_array = NULL;
2943}
2944
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002945#if defined(EXITFREE) || defined(PROTO)
2946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002947clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002948{
2949 int i;
2950
2951 for (i = 0; i < NUM_REGISTERS; ++i)
2952 {
2953 y_current = &y_regs[i];
2954 if (y_current->y_array != NULL)
2955 free_yank_all();
2956 }
2957}
2958#endif
2959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960/*
2961 * Free "n" lines from the current yank register.
2962 * Called for normal freeing and in case of error.
2963 */
2964 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002965free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 if (y_current->y_array != NULL)
2968 {
2969 long i;
2970
2971 for (i = n; --i >= 0; )
2972 {
2973#ifdef AMIGA /* only for very slow machines */
2974 if ((i & 1023) == 1023) /* this may take a while */
2975 {
2976 /*
2977 * This message should never cause a hit-return message.
2978 * Overwrite this message with any next message.
2979 */
2980 ++no_wait_return;
2981 smsg((char_u *)_("freeing %ld lines"), i + 1);
2982 --no_wait_return;
2983 msg_didout = FALSE;
2984 msg_col = 0;
2985 }
2986#endif
2987 vim_free(y_current->y_array[i]);
2988 }
2989 vim_free(y_current->y_array);
2990 y_current->y_array = NULL;
2991#ifdef AMIGA
2992 if (n >= 1000)
2993 MSG("");
2994#endif
2995 }
2996}
2997
2998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002999free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 free_yank(y_current->y_size);
3002}
3003
3004/*
3005 * Yank the text between "oap->start" and "oap->end" into a yank register.
3006 * If we are to append (uppercase register), we first yank into a new yank
3007 * register and then concatenate the old and the new one (so we keep the old
3008 * one in case of out-of-memory).
3009 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003010 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 */
3012 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003013op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003016 yankreg_T *curr; /* copy of y_current */
3017 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 char_u **new_ptr;
3019 linenr_T lnum; /* current line number */
3020 long j;
3021 int yanktype = oap->motion_type;
3022 long yanklines = oap->line_count;
3023 linenr_T yankendlnum = oap->end.lnum;
3024 char_u *p;
3025 char_u *pnew;
3026 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003027#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003028 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
3031 /* check for read-only register */
3032 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3033 {
3034 beep_flush();
3035 return FAIL;
3036 }
3037 if (oap->regname == '_') /* black hole: nothing to do */
3038 return OK;
3039
3040#ifdef FEAT_CLIPBOARD
3041 if (!clip_star.available && oap->regname == '*')
3042 oap->regname = 0;
3043 else if (!clip_plus.available && oap->regname == '+')
3044 oap->regname = 0;
3045#endif
3046
3047 if (!deleting) /* op_delete() already set y_current */
3048 get_yank_register(oap->regname, TRUE);
3049
3050 curr = y_current;
3051 /* append to existing contents */
3052 if (y_append && y_current->y_array != NULL)
3053 y_current = &newreg;
3054 else
3055 free_yank_all(); /* free previously yanked lines */
3056
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003057 /*
3058 * If the cursor was in column 1 before and after the movement, and the
3059 * operator is not inclusive, the yank is always linewise.
3060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 if ( oap->motion_type == MCHAR
3062 && oap->start.col == 0
3063 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003065 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 && oap->end.col == 0
3067 && yanklines > 1)
3068 {
3069 yanktype = MLINE;
3070 --yankendlnum;
3071 --yanklines;
3072 }
3073
3074 y_current->y_size = yanklines;
3075 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3078 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (y_current->y_array == NULL)
3080 {
3081 y_current = curr;
3082 return FAIL;
3083 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003084#ifdef FEAT_VIMINFO
3085 y_current->y_time_set = vim_time();
3086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088 y_idx = 0;
3089 lnum = oap->start.lnum;
3090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (oap->block_mode)
3092 {
3093 /* Visual block mode */
3094 y_current->y_type = MBLOCK; /* set the yank register type */
3095 y_current->y_width = oap->end_vcol - oap->start_vcol;
3096
3097 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3098 y_current->y_width--;
3099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3102 {
3103 switch (y_current->y_type)
3104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 case MBLOCK:
3106 block_prep(oap, &bd, lnum, FALSE);
3107 if (yank_copy_line(&bd, y_idx) == FAIL)
3108 goto fail;
3109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
3111 case MLINE:
3112 if ((y_current->y_array[y_idx] =
3113 vim_strsave(ml_get(lnum))) == NULL)
3114 goto fail;
3115 break;
3116
3117 case MCHAR:
3118 {
3119 colnr_T startcol = 0, endcol = MAXCOL;
3120#ifdef FEAT_VIRTUALEDIT
3121 int is_oneChar = FALSE;
3122 colnr_T cs, ce;
3123#endif
3124 p = ml_get(lnum);
3125 bd.startspaces = 0;
3126 bd.endspaces = 0;
3127
3128 if (lnum == oap->start.lnum)
3129 {
3130 startcol = oap->start.col;
3131#ifdef FEAT_VIRTUALEDIT
3132 if (virtual_op)
3133 {
3134 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3135 if (ce != cs && oap->start.coladd > 0)
3136 {
3137 /* Part of a tab selected -- but don't
3138 * double-count it. */
3139 bd.startspaces = (ce - cs + 1)
3140 - oap->start.coladd;
3141 startcol++;
3142 }
3143 }
3144#endif
3145 }
3146
3147 if (lnum == oap->end.lnum)
3148 {
3149 endcol = oap->end.col;
3150#ifdef FEAT_VIRTUALEDIT
3151 if (virtual_op)
3152 {
3153 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3154 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3155# ifdef FEAT_MBYTE
3156 /* Don't add space for double-wide
3157 * char; endcol will be on last byte
3158 * of multi-byte char. */
3159 && (*mb_head_off)(p, p + endcol) == 0
3160# endif
3161 ))
3162 {
3163 if (oap->start.lnum == oap->end.lnum
3164 && oap->start.col == oap->end.col)
3165 {
3166 /* Special case: inside a single char */
3167 is_oneChar = TRUE;
3168 bd.startspaces = oap->end.coladd
3169 - oap->start.coladd + oap->inclusive;
3170 endcol = startcol;
3171 }
3172 else
3173 {
3174 bd.endspaces = oap->end.coladd
3175 + oap->inclusive;
3176 endcol -= oap->inclusive;
3177 }
3178 }
3179 }
3180#endif
3181 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003182 if (endcol == MAXCOL)
3183 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (startcol > endcol
3185#ifdef FEAT_VIRTUALEDIT
3186 || is_oneChar
3187#endif
3188 )
3189 bd.textlen = 0;
3190 else
3191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 bd.textlen = endcol - startcol + oap->inclusive;
3193 }
3194 bd.textstart = p + startcol;
3195 if (yank_copy_line(&bd, y_idx) == FAIL)
3196 goto fail;
3197 break;
3198 }
3199 /* NOTREACHED */
3200 }
3201 }
3202
3203 if (curr != y_current) /* append the new block to the old block */
3204 {
3205 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3206 (curr->y_size + y_current->y_size)), TRUE);
3207 if (new_ptr == NULL)
3208 goto fail;
3209 for (j = 0; j < curr->y_size; ++j)
3210 new_ptr[j] = curr->y_array[j];
3211 vim_free(curr->y_array);
3212 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003213#ifdef FEAT_VIMINFO
3214 curr->y_time_set = vim_time();
3215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
3217 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3218 curr->y_type = MLINE;
3219
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003220 /* Concatenate the last line of the old block with the first line of
3221 * the new block, unless being Vi compatible. */
3222 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
3224 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3225 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3226 if (pnew == NULL)
3227 {
3228 y_idx = y_current->y_size - 1;
3229 goto fail;
3230 }
3231 STRCPY(pnew, curr->y_array[--j]);
3232 STRCAT(pnew, y_current->y_array[0]);
3233 vim_free(curr->y_array[j]);
3234 vim_free(y_current->y_array[0]);
3235 curr->y_array[j++] = pnew;
3236 y_idx = 1;
3237 }
3238 else
3239 y_idx = 0;
3240 while (y_idx < y_current->y_size)
3241 curr->y_array[j++] = y_current->y_array[y_idx++];
3242 curr->y_size = j;
3243 vim_free(y_current->y_array);
3244 y_current = curr;
3245 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003246 if (curwin->w_p_rnu)
3247 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (mess) /* Display message about yank? */
3249 {
3250 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 && yanklines == 1)
3253 yanklines = 0;
3254 /* Some versions of Vi use ">=" here, some don't... */
3255 if (yanklines > p_report)
3256 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003257 char namebuf[100];
3258
3259 if (oap->regname == NUL)
3260 *namebuf = NUL;
3261 else
3262 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003263 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 /* redisplay now, so message is not deleted */
3266 update_topline_redraw();
3267 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003268 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003269 if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003270 smsg((char_u *)_("block of 1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003271 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003272 smsg((char_u *)_("1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003273 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003274 else if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003275 smsg((char_u *)_("block of %ld lines yanked%s"),
3276 yanklines, namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003278 smsg((char_u *)_("%ld lines yanked%s"), yanklines,
3279 namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281 }
3282
3283 /*
3284 * Set "'[" and "']" marks.
3285 */
3286 curbuf->b_op_start = oap->start;
3287 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003288 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003289 {
3290 curbuf->b_op_start.col = 0;
3291 curbuf->b_op_end.col = MAXCOL;
3292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294#ifdef FEAT_CLIPBOARD
3295 /*
3296 * If we were yanking to the '*' register, send result to clipboard.
3297 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3298 * to the '*' register.
3299 */
3300 if (clip_star.available
3301 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003302 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003303 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 if (curr != &(y_regs[STAR_REGISTER]))
3306 /* Copy the text from register 0 to the clipboard register. */
3307 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3308
3309 clip_own_selection(&clip_star);
3310 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003311# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003312 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315
3316# ifdef FEAT_X11
3317 /*
3318 * If we were yanking to the '+' register, send result to selection.
3319 * Also copy to the '*' register, in case auto-select is off.
3320 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003321 if (clip_plus.available
3322 && (curr == &(y_regs[PLUS_REGISTER])
3323 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003324 && ((clip_unnamed | clip_unnamed_saved) &
3325 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003327 if (curr != &(y_regs[PLUS_REGISTER]))
3328 /* Copy the text from register 0 to the clipboard register. */
3329 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 clip_own_selection(&clip_plus);
3332 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003333 if (!clip_isautosel_star() && !clip_isautosel_plus()
3334 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3337 clip_own_selection(&clip_star);
3338 clip_gen_set_selection(&clip_star);
3339 }
3340 }
3341# endif
3342#endif
3343
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003344#ifdef FEAT_AUTOCMD
3345 if (!deleting && has_textyankpost())
3346 yank_do_autocmd(oap, y_current);
3347#endif
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 return OK;
3350
3351fail: /* free the allocated lines */
3352 free_yank(y_idx + 1);
3353 y_current = curr;
3354 return FAIL;
3355}
3356
3357 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003358yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 char_u *pnew;
3361
3362 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3363 == NULL)
3364 return FAIL;
3365 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003366 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 pnew += bd->startspaces;
3368 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3369 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003370 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 pnew += bd->endspaces;
3372 *pnew = NUL;
3373 return OK;
3374}
3375
3376#ifdef FEAT_CLIPBOARD
3377/*
3378 * Make a copy of the y_current register to register "reg".
3379 */
3380 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003381copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003383 yankreg_T *curr = y_current;
3384 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
3386 y_current = reg;
3387 free_yank_all();
3388 *y_current = *curr;
3389 y_current->y_array = (char_u **)lalloc_clear(
3390 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3391 if (y_current->y_array == NULL)
3392 y_current->y_size = 0;
3393 else
3394 for (j = 0; j < y_current->y_size; ++j)
3395 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3396 {
3397 free_yank(j);
3398 y_current->y_size = 0;
3399 break;
3400 }
3401 y_current = curr;
3402}
3403#endif
3404
3405/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003406 * Put contents of register "regname" into the text.
3407 * Caller must check "regname" to be valid!
3408 * "flags": PUT_FIXINDENT make indent look nice
3409 * PUT_CURSEND leave cursor after end of new text
3410 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 */
3412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003413do_put(
3414 int regname,
3415 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3416 long count,
3417 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 char_u *ptr;
3420 char_u *newp, *oldp;
3421 int yanklen;
3422 int totlen = 0; /* init for gcc */
3423 linenr_T lnum;
3424 colnr_T col;
3425 long i; /* index in y_array[] */
3426 int y_type;
3427 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 int oldlen;
3429 long y_width = 0;
3430 colnr_T vcol;
3431 int delcount;
3432 int incr = 0;
3433 long j;
3434 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 char_u **y_array = NULL;
3436 long nr_lines = 0;
3437 pos_T new_cursor;
3438 int indent;
3439 int orig_indent = 0; /* init for gcc */
3440 int indent_diff = 0; /* init for gcc */
3441 int first_indent = TRUE;
3442 int lendiff = 0;
3443 pos_T old_pos;
3444 char_u *insert_string = NULL;
3445 int allocated = FALSE;
3446 long cnt;
3447
3448#ifdef FEAT_CLIPBOARD
3449 /* Adjust register name for "unnamed" in 'clipboard'. */
3450 adjust_clip_reg(&regname);
3451 (void)may_get_selection(regname);
3452#endif
3453
3454 if (flags & PUT_FIXINDENT)
3455 orig_indent = get_indent();
3456
3457 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3458 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3459
3460 /*
3461 * Using inserted text works differently, because the register includes
3462 * special characters (newlines, etc.).
3463 */
3464 if (regname == '.')
3465 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003466 if (VIsual_active)
3467 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3469 (count == -1 ? 'O' : 'i')), count, FALSE);
3470 /* Putting the text is done later, so can't really move the cursor to
3471 * the next character. Use "l" to simulate it. */
3472 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3473 stuffcharReadbuff('l');
3474 return;
3475 }
3476
3477 /*
3478 * For special registers '%' (file name), '#' (alternate file name) and
3479 * ':' (last command line), etc. we have to create a fake yank register.
3480 */
3481 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3482 {
3483 if (insert_string == NULL)
3484 return;
3485 }
3486
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003487#ifdef FEAT_AUTOCMD
3488 /* Autocommands may be executed when saving lines for undo, which may make
3489 * y_array invalid. Start undo now to avoid that. */
3490 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3491#endif
3492
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (insert_string != NULL)
3494 {
3495 y_type = MCHAR;
3496#ifdef FEAT_EVAL
3497 if (regname == '=')
3498 {
3499 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003500 * characters.
3501 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 for (;;)
3503 {
3504 y_size = 0;
3505 ptr = insert_string;
3506 while (ptr != NULL)
3507 {
3508 if (y_array != NULL)
3509 y_array[y_size] = ptr;
3510 ++y_size;
3511 ptr = vim_strchr(ptr, '\n');
3512 if (ptr != NULL)
3513 {
3514 if (y_array != NULL)
3515 *ptr = NUL;
3516 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003517 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (*ptr == NUL)
3519 {
3520 y_type = MLINE;
3521 break;
3522 }
3523 }
3524 }
3525 if (y_array != NULL)
3526 break;
3527 y_array = (char_u **)alloc((unsigned)
3528 (y_size * sizeof(char_u *)));
3529 if (y_array == NULL)
3530 goto end;
3531 }
3532 }
3533 else
3534#endif
3535 {
3536 y_size = 1; /* use fake one-line yank register */
3537 y_array = &insert_string;
3538 }
3539 }
3540 else
3541 {
3542 get_yank_register(regname, FALSE);
3543
3544 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 y_size = y_current->y_size;
3547 y_array = y_current->y_array;
3548 }
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (y_type == MLINE)
3551 {
3552 if (flags & PUT_LINE_SPLIT)
3553 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003554 char_u *p;
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /* "p" or "P" in Visual mode: split the lines to put the text in
3557 * between. */
3558 if (u_save_cursor() == FAIL)
3559 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003560 p = ml_get_cursor();
3561 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003562 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003563 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (ptr == NULL)
3565 goto end;
3566 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3567 vim_free(ptr);
3568
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003569 oldp = ml_get_curline();
3570 p = oldp + curwin->w_cursor.col;
3571 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003572 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003573 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (ptr == NULL)
3575 goto end;
3576 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3577 ++nr_lines;
3578 dir = FORWARD;
3579 }
3580 if (flags & PUT_LINE_FORWARD)
3581 {
3582 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003583 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 dir = FORWARD;
3585 }
3586 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3587 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3591 y_type = MLINE;
3592
3593 if (y_size == 0 || y_array == NULL)
3594 {
3595 EMSG2(_("E353: Nothing in register %s"),
3596 regname == 0 ? (char_u *)"\"" : transchar(regname));
3597 goto end;
3598 }
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (y_type == MBLOCK)
3601 {
3602 lnum = curwin->w_cursor.lnum + y_size + 1;
3603 if (lnum > curbuf->b_ml.ml_line_count)
3604 lnum = curbuf->b_ml.ml_line_count + 1;
3605 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3606 goto end;
3607 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003608 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
3610 lnum = curwin->w_cursor.lnum;
3611#ifdef FEAT_FOLDING
3612 /* Correct line number for closed fold. Don't move the cursor yet,
3613 * u_save() uses it. */
3614 if (dir == BACKWARD)
3615 (void)hasFolding(lnum, &lnum, NULL);
3616 else
3617 (void)hasFolding(lnum, NULL, &lnum);
3618#endif
3619 if (dir == FORWARD)
3620 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003621 /* In an empty buffer the empty line is going to be replaced, include
3622 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003623 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 goto end;
3625#ifdef FEAT_FOLDING
3626 if (dir == FORWARD)
3627 curwin->w_cursor.lnum = lnum - 1;
3628 else
3629 curwin->w_cursor.lnum = lnum;
3630 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3631#endif
3632 }
3633 else if (u_save_cursor() == FAIL)
3634 goto end;
3635
3636 yanklen = (int)STRLEN(y_array[0]);
3637
3638#ifdef FEAT_VIRTUALEDIT
3639 if (ve_flags == VE_ALL && y_type == MCHAR)
3640 {
3641 if (gchar_cursor() == TAB)
3642 {
3643 /* Don't need to insert spaces when "p" on the last position of a
3644 * tab or "P" on the first position. */
3645 if (dir == FORWARD
3646 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3647 : curwin->w_cursor.coladd > 0)
3648 coladvance_force(getviscol());
3649 else
3650 curwin->w_cursor.coladd = 0;
3651 }
3652 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3653 coladvance_force(getviscol() + (dir == FORWARD));
3654 }
3655#endif
3656
3657 lnum = curwin->w_cursor.lnum;
3658 col = curwin->w_cursor.col;
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 /*
3661 * Block mode
3662 */
3663 if (y_type == MBLOCK)
3664 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003665 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 colnr_T endcol2 = 0;
3667
3668 if (dir == FORWARD && c != NUL)
3669 {
3670#ifdef FEAT_VIRTUALEDIT
3671 if (ve_flags == VE_ALL)
3672 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3673 else
3674#endif
3675 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3676
3677#ifdef FEAT_MBYTE
3678 if (has_mbyte)
3679 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003680 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 else
3682#endif
3683#ifdef FEAT_VIRTUALEDIT
3684 if (c != TAB || ve_flags != VE_ALL)
3685#endif
3686 ++curwin->w_cursor.col;
3687 ++col;
3688 }
3689 else
3690 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3691
3692#ifdef FEAT_VIRTUALEDIT
3693 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003694 if (ve_flags == VE_ALL
3695 && (curwin->w_cursor.coladd > 0
3696 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 if (dir == FORWARD && c == NUL)
3699 ++col;
3700 if (dir != FORWARD && c != NUL)
3701 ++curwin->w_cursor.col;
3702 if (c == TAB)
3703 {
3704 if (dir == BACKWARD && curwin->w_cursor.col)
3705 curwin->w_cursor.col--;
3706 if (dir == FORWARD && col - 1 == endcol2)
3707 curwin->w_cursor.col++;
3708 }
3709 }
3710 curwin->w_cursor.coladd = 0;
3711#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003712 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 for (i = 0; i < y_size; ++i)
3714 {
3715 int spaces;
3716 char shortline;
3717
3718 bd.startspaces = 0;
3719 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 vcol = 0;
3721 delcount = 0;
3722
3723 /* add a new line */
3724 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3725 {
3726 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3727 (colnr_T)1, FALSE) == FAIL)
3728 break;
3729 ++nr_lines;
3730 }
3731 /* get the old line and advance to the position to insert at */
3732 oldp = ml_get_curline();
3733 oldlen = (int)STRLEN(oldp);
3734 for (ptr = oldp; vcol < col && *ptr; )
3735 {
3736 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003737 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 vcol += incr;
3739 }
3740 bd.textcol = (colnr_T)(ptr - oldp);
3741
3742 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3743
3744 if (vcol < col) /* line too short, padd with spaces */
3745 bd.startspaces = col - vcol;
3746 else if (vcol > col)
3747 {
3748 bd.endspaces = vcol - col;
3749 bd.startspaces = incr - bd.endspaces;
3750 --bd.textcol;
3751 delcount = 1;
3752#ifdef FEAT_MBYTE
3753 if (has_mbyte)
3754 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3755#endif
3756 if (oldp[bd.textcol] != TAB)
3757 {
3758 /* Only a Tab can be split into spaces. Other
3759 * characters will have to be moved to after the
3760 * block, causing misalignment. */
3761 delcount = 0;
3762 bd.endspaces = 0;
3763 }
3764 }
3765
3766 yanklen = (int)STRLEN(y_array[i]);
3767
3768 /* calculate number of spaces required to fill right side of block*/
3769 spaces = y_width + 1;
3770 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003771 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (spaces < 0)
3773 spaces = 0;
3774
3775 /* insert the new text */
3776 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3777 newp = alloc_check((unsigned)totlen + oldlen + 1);
3778 if (newp == NULL)
3779 break;
3780 /* copy part up to cursor to new line */
3781 ptr = newp;
3782 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3783 ptr += bd.textcol;
3784 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003785 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 ptr += bd.startspaces;
3787 /* insert the new text */
3788 for (j = 0; j < count; ++j)
3789 {
3790 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3791 ptr += yanklen;
3792
3793 /* insert block's trailing spaces only if there's text behind */
3794 if ((j < count - 1 || !shortline) && spaces)
3795 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003796 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 ptr += spaces;
3798 }
3799 }
3800 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003801 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 ptr += bd.endspaces;
3803 /* move the text after the cursor to the end of the line. */
3804 mch_memmove(ptr, oldp + bd.textcol + delcount,
3805 (size_t)(oldlen - bd.textcol - delcount + 1));
3806 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3807
3808 ++curwin->w_cursor.lnum;
3809 if (i == 0)
3810 curwin->w_cursor.col += bd.startspaces;
3811 }
3812
3813 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3814
3815 /* Set '[ mark. */
3816 curbuf->b_op_start = curwin->w_cursor;
3817 curbuf->b_op_start.lnum = lnum;
3818
3819 /* adjust '] mark */
3820 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3821 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003822# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (flags & PUT_CURSEND)
3826 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003827 colnr_T len;
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 curwin->w_cursor = curbuf->b_op_end;
3830 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003831
3832 /* in Insert mode we might be after the NUL, correct for that */
3833 len = (colnr_T)STRLEN(ml_get_curline());
3834 if (curwin->w_cursor.col > len)
3835 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 else
3838 curwin->w_cursor.lnum = lnum;
3839 }
3840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 /*
3843 * Character or Line mode
3844 */
3845 if (y_type == MCHAR)
3846 {
3847 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3848 * char */
3849 if (dir == FORWARD && gchar_cursor() != NUL)
3850 {
3851#ifdef FEAT_MBYTE
3852 if (has_mbyte)
3853 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003854 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 /* put it on the next of the multi-byte character. */
3857 col += bytelen;
3858 if (yanklen)
3859 {
3860 curwin->w_cursor.col += bytelen;
3861 curbuf->b_op_end.col += bytelen;
3862 }
3863 }
3864 else
3865#endif
3866 {
3867 ++col;
3868 if (yanklen)
3869 {
3870 ++curwin->w_cursor.col;
3871 ++curbuf->b_op_end.col;
3872 }
3873 }
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 curbuf->b_op_start = curwin->w_cursor;
3876 }
3877 /*
3878 * Line mode: BACKWARD is the same as FORWARD on the previous line
3879 */
3880 else if (dir == BACKWARD)
3881 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003882 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
3884 /*
3885 * simple case: insert into current line
3886 */
3887 if (y_type == MCHAR && y_size == 1)
3888 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003889 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003890
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003891 if (VIsual_active)
3892 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003893 end_lnum = curbuf->b_visual.vi_end.lnum;
3894 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3895 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003896 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003897
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003898 do {
3899 totlen = count * yanklen;
3900 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003902 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003903 if (VIsual_active && col > (int)STRLEN(oldp))
3904 {
3905 lnum++;
3906 continue;
3907 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003908 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3909 if (newp == NULL)
3910 goto end; /* alloc() gave an error message */
3911 mch_memmove(newp, oldp, (size_t)col);
3912 ptr = newp + col;
3913 for (i = 0; i < count; ++i)
3914 {
3915 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3916 ptr += yanklen;
3917 }
3918 STRMOVE(ptr, oldp + col);
3919 ml_replace(lnum, newp, FALSE);
3920 /* Place cursor on last putted char. */
3921 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003922 {
3923 /* make sure curwin->w_virtcol is updated */
3924 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003925 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003928 if (VIsual_active)
3929 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003930 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003931
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003932 if (VIsual_active) /* reset lnum to the last visual line */
3933 lnum--;
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 curbuf->b_op_end = curwin->w_cursor;
3936 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3937 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3938 ++curwin->w_cursor.col;
3939 changed_bytes(lnum, col);
3940 }
3941 else
3942 {
3943 /*
3944 * Insert at least one line. When y_type is MCHAR, break the first
3945 * line in two.
3946 */
3947 for (cnt = 1; cnt <= count; ++cnt)
3948 {
3949 i = 0;
3950 if (y_type == MCHAR)
3951 {
3952 /*
3953 * Split the current line in two at the insert position.
3954 * First insert y_array[size - 1] in front of second line.
3955 * Then append y_array[0] to first line.
3956 */
3957 lnum = new_cursor.lnum;
3958 ptr = ml_get(lnum) + col;
3959 totlen = (int)STRLEN(y_array[y_size - 1]);
3960 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3961 if (newp == NULL)
3962 goto error;
3963 STRCPY(newp, y_array[y_size - 1]);
3964 STRCAT(newp, ptr);
3965 /* insert second line */
3966 ml_append(lnum, newp, (colnr_T)0, FALSE);
3967 vim_free(newp);
3968
3969 oldp = ml_get(lnum);
3970 newp = alloc_check((unsigned)(col + yanklen + 1));
3971 if (newp == NULL)
3972 goto error;
3973 /* copy first part of line */
3974 mch_memmove(newp, oldp, (size_t)col);
3975 /* append to first line */
3976 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3977 ml_replace(lnum, newp, FALSE);
3978
3979 curwin->w_cursor.lnum = lnum;
3980 i = 1;
3981 }
3982
3983 for (; i < y_size; ++i)
3984 {
3985 if ((y_type != MCHAR || i < y_size - 1)
3986 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3987 == FAIL)
3988 goto error;
3989 lnum++;
3990 ++nr_lines;
3991 if (flags & PUT_FIXINDENT)
3992 {
3993 old_pos = curwin->w_cursor;
3994 curwin->w_cursor.lnum = lnum;
3995 ptr = ml_get(lnum);
3996 if (cnt == count && i == y_size - 1)
3997 lendiff = (int)STRLEN(ptr);
3998#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3999 if (*ptr == '#' && preprocs_left())
4000 indent = 0; /* Leave # lines at start */
4001 else
4002#endif
4003 if (*ptr == NUL)
4004 indent = 0; /* Ignore empty lines */
4005 else if (first_indent)
4006 {
4007 indent_diff = orig_indent - get_indent();
4008 indent = orig_indent;
4009 first_indent = FALSE;
4010 }
4011 else if ((indent = get_indent() + indent_diff) < 0)
4012 indent = 0;
4013 (void)set_indent(indent, 0);
4014 curwin->w_cursor = old_pos;
4015 /* remember how many chars were removed */
4016 if (cnt == count && i == y_size - 1)
4017 lendiff -= (int)STRLEN(ml_get(lnum));
4018 }
4019 }
4020 }
4021
4022error:
4023 /* Adjust marks. */
4024 if (y_type == MLINE)
4025 {
4026 curbuf->b_op_start.col = 0;
4027 if (dir == FORWARD)
4028 curbuf->b_op_start.lnum++;
4029 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004030 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004031 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004032 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004033 < curbuf->b_ml.ml_line_count
4034#ifdef FEAT_DIFF
4035 || curwin->w_p_diff
4036#endif
4037 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004038 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 (linenr_T)MAXLNUM, nr_lines, 0L);
4040
4041 /* note changed text for displaying and folding */
4042 if (y_type == MCHAR)
4043 changed_lines(curwin->w_cursor.lnum, col,
4044 curwin->w_cursor.lnum + 1, nr_lines);
4045 else
4046 changed_lines(curbuf->b_op_start.lnum, 0,
4047 curbuf->b_op_start.lnum, nr_lines);
4048
4049 /* put '] mark at last inserted character */
4050 curbuf->b_op_end.lnum = lnum;
4051 /* correct length for change in indent */
4052 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4053 if (col > 1)
4054 curbuf->b_op_end.col = col - 1;
4055 else
4056 curbuf->b_op_end.col = 0;
4057
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004058 if (flags & PUT_CURSLINE)
4059 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004060 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004061 curwin->w_cursor.lnum = lnum;
4062 beginline(BL_WHITE | BL_FIX);
4063 }
4064 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
4066 /* put cursor after inserted text */
4067 if (y_type == MLINE)
4068 {
4069 if (lnum >= curbuf->b_ml.ml_line_count)
4070 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4071 else
4072 curwin->w_cursor.lnum = lnum + 1;
4073 curwin->w_cursor.col = 0;
4074 }
4075 else
4076 {
4077 curwin->w_cursor.lnum = lnum;
4078 curwin->w_cursor.col = col;
4079 }
4080 }
4081 else if (y_type == MLINE)
4082 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004083 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 curwin->w_cursor.col = 0;
4085 if (dir == FORWARD)
4086 ++curwin->w_cursor.lnum;
4087 beginline(BL_WHITE | BL_FIX);
4088 }
4089 else /* put cursor on first inserted character */
4090 curwin->w_cursor = new_cursor;
4091 }
4092 }
4093
4094 msgmore(nr_lines);
4095 curwin->w_set_curswant = TRUE;
4096
4097end:
4098 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004100 if (regname == '=')
4101 vim_free(y_array);
4102
Bram Moolenaar033d8882013-09-25 23:24:57 +02004103 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004104
Bram Moolenaar677ee682005-01-27 14:41:15 +00004105 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004106 adjust_cursor_eol();
4107}
4108
4109/*
4110 * When the cursor is on the NUL past the end of the line and it should not be
4111 * there move it left.
4112 */
4113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004114adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004115{
4116 if (curwin->w_cursor.col > 0
4117 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004118#ifdef FEAT_VIRTUALEDIT
4119 && (ve_flags & VE_ONEMORE) == 0
4120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 && !(restart_edit || (State & INSERT)))
4122 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004123 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004124 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004125
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126#ifdef FEAT_VIRTUALEDIT
4127 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004128 {
4129 colnr_T scol, ecol;
4130
4131 /* Coladd is set to the width of the last character. */
4132 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4133 curwin->w_cursor.coladd = ecol - scol + 1;
4134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#endif
4136 }
4137}
4138
4139#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4140/*
4141 * Return TRUE if lines starting with '#' should be left aligned.
4142 */
4143 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004144preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 return
4147# ifdef FEAT_SMARTINDENT
4148# ifdef FEAT_CINDENT
4149 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4150# else
4151 curbuf->b_p_si
4152# endif
4153# endif
4154# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004155 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4156 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157# endif
4158 ;
4159}
4160#endif
4161
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004162/*
4163 * Return the character name of the register with the given number.
4164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004166get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
4168 if (num == -1)
4169 return '"';
4170 else if (num < 10)
4171 return num + '0';
4172 else if (num == DELETION_REGISTER)
4173 return '-';
4174#ifdef FEAT_CLIPBOARD
4175 else if (num == STAR_REGISTER)
4176 return '*';
4177 else if (num == PLUS_REGISTER)
4178 return '+';
4179#endif
4180 else
4181 {
4182#ifdef EBCDIC
4183 int i;
4184
4185 /* EBCDIC is really braindead ... */
4186 i = 'a' + (num - 10);
4187 if (i > 'i')
4188 i += 7;
4189 if (i > 'r')
4190 i += 8;
4191 return i;
4192#else
4193 return num + 'a' - 10;
4194#endif
4195 }
4196}
4197
4198/*
4199 * ":dis" and ":registers": Display the contents of the yank registers.
4200 */
4201 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004202ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004204 int i, n;
4205 long j;
4206 char_u *p;
4207 yankreg_T *yb;
4208 int name;
4209 int attr;
4210 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004211#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004212 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004213#else
4214# define clen 1
4215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 if (arg != NULL && *arg == NUL)
4218 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004219 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 /* Highlight title */
4222 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4223 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4224 {
4225 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004226 if (arg != NULL && vim_strchr(arg, name) == NULL
4227#ifdef ONE_CLIPBOARD
4228 /* Star register and plus register contain the same thing. */
4229 && (name != '*' || vim_strchr(arg, '+') == NULL)
4230#endif
4231 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 continue; /* did not ask for this register */
4233
4234#ifdef FEAT_CLIPBOARD
4235 /* Adjust register name for "unnamed" in 'clipboard'.
4236 * When it's a clipboard register, fill it with the current contents
4237 * of the clipboard. */
4238 adjust_clip_reg(&name);
4239 (void)may_get_selection(name);
4240#endif
4241
4242 if (i == -1)
4243 {
4244 if (y_previous != NULL)
4245 yb = y_previous;
4246 else
4247 yb = &(y_regs[0]);
4248 }
4249 else
4250 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004251
4252#ifdef FEAT_EVAL
4253 if (name == MB_TOLOWER(redir_reg)
4254 || (redir_reg == '"' && yb == y_previous))
4255 continue; /* do not list register being written to, the
4256 * pointer can be freed */
4257#endif
4258
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (yb->y_array != NULL)
4260 {
4261 msg_putchar('\n');
4262 msg_putchar('"');
4263 msg_putchar(name);
4264 MSG_PUTS(" ");
4265
4266 n = (int)Columns - 6;
4267 for (j = 0; j < yb->y_size && n > 1; ++j)
4268 {
4269 if (j)
4270 {
4271 MSG_PUTS_ATTR("^J", attr);
4272 n -= 2;
4273 }
4274 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004277 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004278#endif
4279 msg_outtrans_len(p, clen);
4280#ifdef FEAT_MBYTE
4281 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#endif
4283 }
4284 }
4285 if (n > 1 && yb->y_type == MLINE)
4286 MSG_PUTS_ATTR("^J", attr);
4287 out_flush(); /* show one line at a time */
4288 }
4289 ui_breakcheck();
4290 }
4291
4292 /*
4293 * display last inserted text
4294 */
4295 if ((p = get_last_insert()) != NULL
4296 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4297 {
4298 MSG_PUTS("\n\". ");
4299 dis_msg(p, TRUE);
4300 }
4301
4302 /*
4303 * display last command line
4304 */
4305 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4306 && !got_int)
4307 {
4308 MSG_PUTS("\n\": ");
4309 dis_msg(last_cmdline, FALSE);
4310 }
4311
4312 /*
4313 * display current file name
4314 */
4315 if (curbuf->b_fname != NULL
4316 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4317 {
4318 MSG_PUTS("\n\"% ");
4319 dis_msg(curbuf->b_fname, FALSE);
4320 }
4321
4322 /*
4323 * display alternate file name
4324 */
4325 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4326 {
4327 char_u *fname;
4328 linenr_T dummy;
4329
4330 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4331 {
4332 MSG_PUTS("\n\"# ");
4333 dis_msg(fname, FALSE);
4334 }
4335 }
4336
4337 /*
4338 * display last search pattern
4339 */
4340 if (last_search_pat() != NULL
4341 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4342 {
4343 MSG_PUTS("\n\"/ ");
4344 dis_msg(last_search_pat(), FALSE);
4345 }
4346
4347#ifdef FEAT_EVAL
4348 /*
4349 * display last used expression
4350 */
4351 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4352 && !got_int)
4353 {
4354 MSG_PUTS("\n\"= ");
4355 dis_msg(expr_line, FALSE);
4356 }
4357#endif
4358}
4359
4360/*
4361 * display a string for do_dis()
4362 * truncate at end of screen line
4363 */
4364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004365dis_msg(
4366 char_u *p,
4367 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
4369 int n;
4370#ifdef FEAT_MBYTE
4371 int l;
4372#endif
4373
4374 n = (int)Columns - 6;
4375 while (*p != NUL
4376 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4377 && (n -= ptr2cells(p)) >= 0)
4378 {
4379#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004380 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
4382 msg_outtrans_len(p, l);
4383 p += l;
4384 }
4385 else
4386#endif
4387 msg_outtrans_len(p++, 1);
4388 }
4389 ui_breakcheck();
4390}
4391
Bram Moolenaar81340392012-06-06 16:12:59 +02004392#if defined(FEAT_COMMENTS) || defined(PROTO)
4393/*
4394 * If "process" is TRUE and the line begins with a comment leader (possibly
4395 * after some white space), return a pointer to the text after it. Put a boolean
4396 * value indicating whether the line ends with an unclosed comment in
4397 * "is_comment".
4398 * line - line to be processed,
4399 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004400 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004401 * include_space - whether to also skip space following the comment leader,
4402 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004403 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004404 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004405 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004406skip_comment(
4407 char_u *line,
4408 int process,
4409 int include_space,
4410 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004411{
4412 char_u *comment_flags = NULL;
4413 int lead_len;
4414 int leader_offset = get_last_leader_offset(line, &comment_flags);
4415
4416 *is_comment = FALSE;
4417 if (leader_offset != -1)
4418 {
4419 /* Let's check whether the line ends with an unclosed comment.
4420 * If the last comment leader has COM_END in flags, there's no comment.
4421 */
4422 while (*comment_flags)
4423 {
4424 if (*comment_flags == COM_END
4425 || *comment_flags == ':')
4426 break;
4427 ++comment_flags;
4428 }
4429 if (*comment_flags != COM_END)
4430 *is_comment = TRUE;
4431 }
4432
4433 if (process == FALSE)
4434 return line;
4435
4436 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4437
4438 if (lead_len == 0)
4439 return line;
4440
4441 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004442 * - COM_END,
4443 * - colon,
4444 * whichever comes first.
4445 */
4446 while (*comment_flags)
4447 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004448 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004449 || *comment_flags == ':')
4450 {
4451 break;
4452 }
4453 ++comment_flags;
4454 }
4455
4456 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004457 * starting with a closing part of a three-part comment. That's good,
4458 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004459 */
4460 if (*comment_flags == ':' || *comment_flags == NUL)
4461 line += lead_len;
4462
4463 return line;
4464}
4465#endif
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004468 * Join 'count' lines (minimal 2) at cursor position.
4469 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004470 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4471 * leaders should not be removed.
4472 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4473 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004475 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 */
4477 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004478do_join(
4479 long count,
4480 int insert_space,
4481 int save_undo,
4482 int use_formatoptions UNUSED,
4483 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004485 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004486 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004487 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004489 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004490 int endcurr1 = NUL;
4491 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004492 int currsize = 0; /* size of the current line */
4493 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004495 colnr_T col = 0;
4496 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004497#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004498 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004499 int remove_comments = (use_formatoptions == TRUE)
4500 && has_format_option(FO_REMOVE_COMS);
4501 int prev_was_comment;
4502#endif
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004505 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4506 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4507 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004509 /* Allocate an array to store the number of spaces inserted before each
4510 * line. We will use it to pre-compute the length of the new line and the
4511 * proper placement of each original line in the new one. */
4512 spaces = lalloc_clear((long_u)count, TRUE);
4513 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004515#if defined(FEAT_COMMENTS) || defined(PROTO)
4516 if (remove_comments)
4517 {
4518 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4519 if (comments == NULL)
4520 {
4521 vim_free(spaces);
4522 return FAIL;
4523 }
4524 }
4525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004528 * Don't move anything, just compute the final line length
4529 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004531 for (t = 0; t < count; ++t)
4532 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004533 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004534 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004535 {
4536 /* Set the '[ mark. */
4537 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4538 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4539 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004540#if defined(FEAT_COMMENTS) || defined(PROTO)
4541 if (remove_comments)
4542 {
4543 /* We don't want to remove the comment leader if the
4544 * previous line is not a comment. */
4545 if (t > 0 && prev_was_comment)
4546 {
4547
4548 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4549 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004550 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004551 curr = new_curr;
4552 }
4553 else
4554 curr = skip_comment(curr, FALSE, insert_space,
4555 &prev_was_comment);
4556 }
4557#endif
4558
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004559 if (insert_space && t > 0)
4560 {
4561 curr = skipwhite(curr);
4562 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4563#ifdef FEAT_MBYTE
4564 && (!has_format_option(FO_MBYTE_JOIN)
4565 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4566 && (!has_format_option(FO_MBYTE_JOIN2)
4567 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4568#endif
4569 )
4570 {
4571 /* don't add a space if the line is ending in a space */
4572 if (endcurr1 == ' ')
4573 endcurr1 = endcurr2;
4574 else
4575 ++spaces[t];
4576 /* extra space when 'joinspaces' set and line ends in '.' */
4577 if ( p_js
4578 && (endcurr1 == '.'
4579 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4580 && (endcurr1 == '?' || endcurr1 == '!'))))
4581 ++spaces[t];
4582 }
4583 }
4584 currsize = (int)STRLEN(curr);
4585 sumsize += currsize + spaces[t];
4586 endcurr1 = endcurr2 = NUL;
4587 if (insert_space && currsize > 0)
4588 {
4589#ifdef FEAT_MBYTE
4590 if (has_mbyte)
4591 {
4592 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004593 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004594 endcurr1 = (*mb_ptr2char)(cend);
4595 if (cend > curr)
4596 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004597 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004598 endcurr2 = (*mb_ptr2char)(cend);
4599 }
4600 }
4601 else
4602#endif
4603 {
4604 endcurr1 = *(curr + currsize - 1);
4605 if (currsize > 1)
4606 endcurr2 = *(curr + currsize - 2);
4607 }
4608 }
4609 line_breakcheck();
4610 if (got_int)
4611 {
4612 ret = FAIL;
4613 goto theend;
4614 }
4615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004617 /* store the column position before last line */
4618 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004620 /* allocate the space for the new line */
4621 newp = alloc_check((unsigned)(sumsize + 1));
4622 cend = newp + sumsize;
4623 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004625 /*
4626 * Move affected lines to the new long one.
4627 *
4628 * Move marks from each deleted line to the joined line, adjusting the
4629 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4630 * should not really be a problem.
4631 */
4632 for (t = count - 1; ; --t)
4633 {
4634 cend -= currsize;
4635 mch_memmove(cend, curr, (size_t)currsize);
4636 if (spaces[t] > 0)
4637 {
4638 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004639 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004640 }
4641 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004642 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004643 if (t == 0)
4644 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004645 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004646#if defined(FEAT_COMMENTS) || defined(PROTO)
4647 if (remove_comments)
4648 curr += comments[t - 1];
4649#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004650 if (insert_space && t > 1)
4651 curr = skipwhite(curr);
4652 currsize = (int)STRLEN(curr);
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4655
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004656 if (setmark)
4657 {
4658 /* Set the '] mark. */
4659 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4660 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4661 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 /* Only report the change in the first line here, del_lines() will report
4664 * the deleted line. */
4665 changed_lines(curwin->w_cursor.lnum, currsize,
4666 curwin->w_cursor.lnum + 1, 0L);
4667
4668 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004669 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 * briefly, and then move it back. After del_lines() the cursor may
4671 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 */
4673 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004675 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 curwin->w_cursor.lnum = t;
4677
4678 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004679 * Set the cursor column:
4680 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004681 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004683 curwin->w_cursor.col =
4684 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#ifdef FEAT_VIRTUALEDIT
4688 curwin->w_cursor.coladd = 0;
4689#endif
4690 curwin->w_set_curswant = TRUE;
4691
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004692theend:
4693 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004694#if defined(FEAT_COMMENTS) || defined(PROTO)
4695 if (remove_comments)
4696 vim_free(comments);
4697#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004698 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699}
4700
4701#ifdef FEAT_COMMENTS
4702/*
4703 * Return TRUE if the two comment leaders given are the same. "lnum" is
4704 * the first line. White-space is ignored. Note that the whole of
4705 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4706 */
4707 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004708same_leader(
4709 linenr_T lnum,
4710 int leader1_len,
4711 char_u *leader1_flags,
4712 int leader2_len,
4713 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 int idx1 = 0, idx2 = 0;
4716 char_u *p;
4717 char_u *line1;
4718 char_u *line2;
4719
4720 if (leader1_len == 0)
4721 return (leader2_len == 0);
4722
4723 /*
4724 * If first leader has 'f' flag, the lines can be joined only if the
4725 * second line does not have a leader.
4726 * If first leader has 'e' flag, the lines can never be joined.
4727 * If fist leader has 's' flag, the lines can only be joined if there is
4728 * some text after it and the second line has the 'm' flag.
4729 */
4730 if (leader1_flags != NULL)
4731 {
4732 for (p = leader1_flags; *p && *p != ':'; ++p)
4733 {
4734 if (*p == COM_FIRST)
4735 return (leader2_len == 0);
4736 if (*p == COM_END)
4737 return FALSE;
4738 if (*p == COM_START)
4739 {
4740 if (*(ml_get(lnum) + leader1_len) == NUL)
4741 return FALSE;
4742 if (leader2_flags == NULL || leader2_len == 0)
4743 return FALSE;
4744 for (p = leader2_flags; *p && *p != ':'; ++p)
4745 if (*p == COM_MIDDLE)
4746 return TRUE;
4747 return FALSE;
4748 }
4749 }
4750 }
4751
4752 /*
4753 * Get current line and next line, compare the leaders.
4754 * The first line has to be saved, only one line can be locked at a time.
4755 */
4756 line1 = vim_strsave(ml_get(lnum));
4757 if (line1 != NULL)
4758 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004759 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 ;
4761 line2 = ml_get(lnum + 1);
4762 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4763 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004764 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 if (line1[idx1++] != line2[idx2])
4767 break;
4768 }
4769 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004770 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 ++idx1;
4772 }
4773 vim_free(line1);
4774 }
4775 return (idx2 == leader2_len && idx1 == leader1_len);
4776}
4777#endif
4778
4779/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004780 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 */
4782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004783op_format(
4784 oparg_T *oap,
4785 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786{
4787 long old_line_count = curbuf->b_ml.ml_line_count;
4788
4789 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4790 * can put it back there. */
4791 curwin->w_cursor = oap->cursor_start;
4792
4793 if (u_save((linenr_T)(oap->start.lnum - 1),
4794 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4795 return;
4796 curwin->w_cursor = oap->start;
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (oap->is_VIsual)
4799 /* When there is no change: need to remove the Visual selection */
4800 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /* Set '[ mark at the start of the formatted area */
4803 curbuf->b_op_start = oap->start;
4804
4805 /* For "gw" remember the cursor position and put it back below (adjusted
4806 * for joined and split lines). */
4807 if (keep_cursor)
4808 saved_cursor = oap->cursor_start;
4809
Bram Moolenaar81a82092008-03-12 16:27:00 +00004810 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 /*
4813 * Leave the cursor at the first non-blank of the last formatted line.
4814 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4815 * line, so "." will do the next lines.
4816 */
4817 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4818 ++curwin->w_cursor.lnum;
4819 beginline(BL_WHITE | BL_FIX);
4820 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4821 msgmore(old_line_count);
4822
4823 /* put '] mark on the end of the formatted area */
4824 curbuf->b_op_end = curwin->w_cursor;
4825
4826 if (keep_cursor)
4827 {
4828 curwin->w_cursor = saved_cursor;
4829 saved_cursor.lnum = 0;
4830 }
4831
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (oap->is_VIsual)
4833 {
4834 win_T *wp;
4835
4836 FOR_ALL_WINDOWS(wp)
4837 {
4838 if (wp->w_old_cursor_lnum != 0)
4839 {
4840 /* When lines have been inserted or deleted, adjust the end of
4841 * the Visual area to be redrawn. */
4842 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4843 wp->w_old_cursor_lnum += old_line_count;
4844 else
4845 wp->w_old_visual_lnum += old_line_count;
4846 }
4847 }
4848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849}
4850
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004851#if defined(FEAT_EVAL) || defined(PROTO)
4852/*
4853 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4854 */
4855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004856op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004857{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004858 if (oap->is_VIsual)
4859 /* When there is no change: need to remove the Visual selection */
4860 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004861
Bram Moolenaar700303e2010-07-11 17:35:50 +02004862 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4863 /* As documented: when 'formatexpr' returns non-zero fall back to
4864 * internal formatting. */
4865 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004866}
4867
4868 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004869fex_format(
4870 linenr_T lnum,
4871 long count,
4872 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004873{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004874 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4875 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004876 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004877 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004878
4879 /*
4880 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004881 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004882 */
4883 set_vim_var_nr(VV_LNUM, lnum);
4884 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004885 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004886
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004887 /* Make a copy, the option could be changed while calling it. */
4888 fex = vim_strsave(curbuf->b_p_fex);
4889 if (fex == NULL)
4890 return 0;
4891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004892 /*
4893 * Evaluate the function.
4894 */
4895 if (use_sandbox)
4896 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004897 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004898 if (use_sandbox)
4899 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004900
4901 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004902 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004903
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004904 return r;
4905}
4906#endif
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908/*
4909 * Format "line_count" lines, starting at the cursor position.
4910 * When "line_count" is negative, format until the end of the paragraph.
4911 * Lines after the cursor line are saved for undo, caller must have saved the
4912 * first line.
4913 */
4914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004915format_lines(
4916 linenr_T line_count,
4917 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
4919 int max_len;
4920 int is_not_par; /* current line not part of parag. */
4921 int next_is_not_par; /* next line not part of paragraph */
4922 int is_end_par; /* at end of paragraph */
4923 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4924 int next_is_start_par = FALSE;
4925#ifdef FEAT_COMMENTS
4926 int leader_len = 0; /* leader len of current line */
4927 int next_leader_len; /* leader len of next line */
4928 char_u *leader_flags = NULL; /* flags for leader of current line */
4929 char_u *next_leader_flags; /* flags for leader of next line */
4930 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004931 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932#endif
4933 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004934 int second_indent = -1; /* indent for second line (comment
4935 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 int do_second_indent;
4937 int do_number_indent;
4938 int do_trail_white;
4939 int first_par_line = TRUE;
4940 int smd_save;
4941 long count;
4942 int need_set_indent = TRUE; /* set indent of next paragraph */
4943 int force_format = FALSE;
4944 int old_State = State;
4945
4946 /* length of a line to force formatting: 3 * 'tw' */
4947 max_len = comp_textwidth(TRUE) * 3;
4948
4949 /* check for 'q', '2' and '1' in 'formatoptions' */
4950#ifdef FEAT_COMMENTS
4951 do_comments = has_format_option(FO_Q_COMS);
4952#endif
4953 do_second_indent = has_format_option(FO_Q_SECOND);
4954 do_number_indent = has_format_option(FO_Q_NUMBER);
4955 do_trail_white = has_format_option(FO_WHITE_PAR);
4956
4957 /*
4958 * Get info about the previous and current line.
4959 */
4960 if (curwin->w_cursor.lnum > 1)
4961 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4962#ifdef FEAT_COMMENTS
4963 , &leader_len, &leader_flags, do_comments
4964#endif
4965 );
4966 else
4967 is_not_par = TRUE;
4968 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4969#ifdef FEAT_COMMENTS
4970 , &next_leader_len, &next_leader_flags, do_comments
4971#endif
4972 );
4973 is_end_par = (is_not_par || next_is_not_par);
4974 if (!is_end_par && do_trail_white)
4975 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4976
4977 curwin->w_cursor.lnum--;
4978 for (count = line_count; count != 0 && !got_int; --count)
4979 {
4980 /*
4981 * Advance to next paragraph.
4982 */
4983 if (advance)
4984 {
4985 curwin->w_cursor.lnum++;
4986 prev_is_end_par = is_end_par;
4987 is_not_par = next_is_not_par;
4988#ifdef FEAT_COMMENTS
4989 leader_len = next_leader_len;
4990 leader_flags = next_leader_flags;
4991#endif
4992 }
4993
4994 /*
4995 * The last line to be formatted.
4996 */
4997 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4998 {
4999 next_is_not_par = TRUE;
5000#ifdef FEAT_COMMENTS
5001 next_leader_len = 0;
5002 next_leader_flags = NULL;
5003#endif
5004 }
5005 else
5006 {
5007 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5008#ifdef FEAT_COMMENTS
5009 , &next_leader_len, &next_leader_flags, do_comments
5010#endif
5011 );
5012 if (do_number_indent)
5013 next_is_start_par =
5014 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5015 }
5016 advance = TRUE;
5017 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5018 if (!is_end_par && do_trail_white)
5019 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5020
5021 /*
5022 * Skip lines that are not in a paragraph.
5023 */
5024 if (is_not_par)
5025 {
5026 if (line_count < 0)
5027 break;
5028 }
5029 else
5030 {
5031 /*
5032 * For the first line of a paragraph, check indent of second line.
5033 * Don't do this for comments and empty lines.
5034 */
5035 if (first_par_line
5036 && (do_second_indent || do_number_indent)
5037 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005038 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005040 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005041 {
5042#ifdef FEAT_COMMENTS
5043 if (leader_len == 0 && next_leader_len == 0)
5044 {
5045 /* no comment found */
5046#endif
5047 second_indent =
5048 get_indent_lnum(curwin->w_cursor.lnum + 1);
5049#ifdef FEAT_COMMENTS
5050 }
5051 else
5052 {
5053 second_indent = next_leader_len;
5054 do_comments_list = 1;
5055 }
5056#endif
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005059 {
5060#ifdef FEAT_COMMENTS
5061 if (leader_len == 0 && next_leader_len == 0)
5062 {
5063 /* no comment found */
5064#endif
5065 second_indent =
5066 get_number_indent(curwin->w_cursor.lnum);
5067#ifdef FEAT_COMMENTS
5068 }
5069 else
5070 {
5071 /* get_number_indent() is now "comment aware"... */
5072 second_indent =
5073 get_number_indent(curwin->w_cursor.lnum);
5074 do_comments_list = 1;
5075 }
5076#endif
5077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079
5080 /*
5081 * When the comment leader changes, it's the end of the paragraph.
5082 */
5083 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5084#ifdef FEAT_COMMENTS
5085 || !same_leader(curwin->w_cursor.lnum,
5086 leader_len, leader_flags,
5087 next_leader_len, next_leader_flags)
5088#endif
5089 )
5090 is_end_par = TRUE;
5091
5092 /*
5093 * If we have got to the end of a paragraph, or the line is
5094 * getting long, format it.
5095 */
5096 if (is_end_par || force_format)
5097 {
5098 if (need_set_indent)
5099 /* replace indent in first line with minimal number of
5100 * tabs and spaces, according to current options */
5101 (void)set_indent(get_indent(), SIN_CHANGED);
5102
5103 /* put cursor on last non-space */
5104 State = NORMAL; /* don't go past end-of-line */
5105 coladvance((colnr_T)MAXCOL);
5106 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5107 dec_cursor();
5108
5109 /* do the formatting, without 'showmode' */
5110 State = INSERT; /* for open_line() */
5111 smd_save = p_smd;
5112 p_smd = FALSE;
5113 insertchar(NUL, INSCHAR_FORMAT
5114#ifdef FEAT_COMMENTS
5115 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005116 + (do_comments && do_comments_list
5117 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005119 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 State = old_State;
5121 p_smd = smd_save;
5122 second_indent = -1;
5123 /* at end of par.: need to set indent of next par. */
5124 need_set_indent = is_end_par;
5125 if (is_end_par)
5126 {
5127 /* When called with a negative line count, break at the
5128 * end of the paragraph. */
5129 if (line_count < 0)
5130 break;
5131 first_par_line = TRUE;
5132 }
5133 force_format = FALSE;
5134 }
5135
5136 /*
5137 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005138 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 */
5140 if (!is_end_par)
5141 {
5142 advance = FALSE;
5143 curwin->w_cursor.lnum++;
5144 curwin->w_cursor.col = 0;
5145 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005149 {
5150 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5152 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005153 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005155 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5156 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005157 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005158
5159 if (indent > 0)
5160 {
5161 (void)del_bytes(indent, FALSE, FALSE);
5162 mark_col_adjust(curwin->w_cursor.lnum,
5163 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005164 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005167 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
5169 beep_flush();
5170 break;
5171 }
5172 first_par_line = FALSE;
5173 /* If the line is getting long, format it next time */
5174 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5175 force_format = TRUE;
5176 else
5177 force_format = FALSE;
5178 }
5179 }
5180 line_breakcheck();
5181 }
5182}
5183
5184/*
5185 * Return TRUE if line "lnum" ends in a white character.
5186 */
5187 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005188ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 char_u *s = ml_get(lnum);
5191 size_t l;
5192
5193 if (*s == NUL)
5194 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005195 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 * invocation may call function multiple times". */
5197 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005198 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199}
5200
5201/*
5202 * Blank lines, and lines containing only the comment leader, are left
5203 * untouched by the formatting. The function returns TRUE in this
5204 * case. It also returns TRUE when a line starts with the end of a comment
5205 * ('e' in comment flags), so that this line is skipped, and not joined to the
5206 * previous line. A new paragraph starts after a blank line, or when the
5207 * comment leader changes -- webb.
5208 */
5209#ifdef FEAT_COMMENTS
5210 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211fmt_check_par(
5212 linenr_T lnum,
5213 int *leader_len,
5214 char_u **leader_flags,
5215 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 char_u *flags = NULL; /* init for GCC */
5218 char_u *ptr;
5219
5220 ptr = ml_get(lnum);
5221 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005222 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 else
5224 *leader_len = 0;
5225
5226 if (*leader_len > 0)
5227 {
5228 /*
5229 * Search for 'e' flag in comment leader flags.
5230 */
5231 flags = *leader_flags;
5232 while (*flags && *flags != ':' && *flags != COM_END)
5233 ++flags;
5234 }
5235
5236 return (*skipwhite(ptr + *leader_len) == NUL
5237 || (*leader_len > 0 && *flags == COM_END)
5238 || startPS(lnum, NUL, FALSE));
5239}
5240#else
5241 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005242fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5245}
5246#endif
5247
5248/*
5249 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5250 * previous line is in the same paragraph. Used for auto-formatting.
5251 */
5252 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005253paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 char_u *p;
5256#ifdef FEAT_COMMENTS
5257 int leader_len = 0; /* leader len of current line */
5258 char_u *leader_flags = NULL; /* flags for leader of current line */
5259 int next_leader_len; /* leader len of next line */
5260 char_u *next_leader_flags; /* flags for leader of next line */
5261 int do_comments; /* format comments */
5262#endif
5263
5264 if (lnum <= 1)
5265 return TRUE; /* start of the file */
5266
5267 p = ml_get(lnum - 1);
5268 if (*p == NUL)
5269 return TRUE; /* after empty line */
5270
5271#ifdef FEAT_COMMENTS
5272 do_comments = has_format_option(FO_Q_COMS);
5273#endif
5274 if (fmt_check_par(lnum - 1
5275#ifdef FEAT_COMMENTS
5276 , &leader_len, &leader_flags, do_comments
5277#endif
5278 ))
5279 return TRUE; /* after non-paragraph line */
5280
5281 if (fmt_check_par(lnum
5282#ifdef FEAT_COMMENTS
5283 , &next_leader_len, &next_leader_flags, do_comments
5284#endif
5285 ))
5286 return TRUE; /* "lnum" is not a paragraph line */
5287
5288 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5289 return TRUE; /* missing trailing space in previous line. */
5290
5291 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5292 return TRUE; /* numbered item starts in "lnum". */
5293
5294#ifdef FEAT_COMMENTS
5295 if (!same_leader(lnum - 1, leader_len, leader_flags,
5296 next_leader_len, next_leader_flags))
5297 return TRUE; /* change of comment leader. */
5298#endif
5299
5300 return FALSE;
5301}
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303/*
5304 * prepare a few things for block mode yank/delete/tilde
5305 *
5306 * for delete:
5307 * - textlen includes the first/last char to be (partly) deleted
5308 * - start/endspaces is the number of columns that are taken by the
5309 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005310 * deleted.
5311 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * - textlen includes the first/last char to be wholly yanked
5313 * - start/endspaces is the number of columns of the first/last yanked char
5314 * that are to be yanked.
5315 */
5316 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005317block_prep(
5318 oparg_T *oap,
5319 struct block_def *bdp,
5320 linenr_T lnum,
5321 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322{
5323 int incr = 0;
5324 char_u *pend;
5325 char_u *pstart;
5326 char_u *line;
5327 char_u *prev_pstart;
5328 char_u *prev_pend;
5329
5330 bdp->startspaces = 0;
5331 bdp->endspaces = 0;
5332 bdp->textlen = 0;
5333 bdp->start_vcol = 0;
5334 bdp->end_vcol = 0;
5335#ifdef FEAT_VISUALEXTRA
5336 bdp->is_short = FALSE;
5337 bdp->is_oneChar = FALSE;
5338 bdp->pre_whitesp = 0;
5339 bdp->pre_whitesp_c = 0;
5340 bdp->end_char_vcols = 0;
5341#endif
5342 bdp->start_char_vcols = 0;
5343
5344 line = ml_get(lnum);
5345 pstart = line;
5346 prev_pstart = line;
5347 while (bdp->start_vcol < oap->start_vcol && *pstart)
5348 {
5349 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005350 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 bdp->start_vcol += incr;
5352#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005353 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
5355 bdp->pre_whitesp += incr;
5356 bdp->pre_whitesp_c++;
5357 }
5358 else
5359 {
5360 bdp->pre_whitesp = 0;
5361 bdp->pre_whitesp_c = 0;
5362 }
5363#endif
5364 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005365 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367 bdp->start_char_vcols = incr;
5368 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5369 {
5370 bdp->end_vcol = bdp->start_vcol;
5371#ifdef FEAT_VISUALEXTRA
5372 bdp->is_short = TRUE;
5373#endif
5374 if (!is_del || oap->op_type == OP_APPEND)
5375 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5376 }
5377 else
5378 {
5379 /* notice: this converts partly selected Multibyte characters to
5380 * spaces, too. */
5381 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5382 if (is_del && bdp->startspaces)
5383 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5384 pend = pstart;
5385 bdp->end_vcol = bdp->start_vcol;
5386 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5387 {
5388#ifdef FEAT_VISUALEXTRA
5389 bdp->is_oneChar = TRUE;
5390#endif
5391 if (oap->op_type == OP_INSERT)
5392 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5393 else if (oap->op_type == OP_APPEND)
5394 {
5395 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5396 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5397 }
5398 else
5399 {
5400 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5401 if (is_del && oap->op_type != OP_LSHIFT)
5402 {
5403 /* just putting the sum of those two into
5404 * bdp->startspaces doesn't work for Visual replace,
5405 * so we have to split the tab in two */
5406 bdp->startspaces = bdp->start_char_vcols
5407 - (bdp->start_vcol - oap->start_vcol);
5408 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5409 }
5410 }
5411 }
5412 else
5413 {
5414 prev_pend = pend;
5415 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5416 {
5417 /* Count a tab for what it's worth (if list mode not on) */
5418 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005419 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 bdp->end_vcol += incr;
5421 }
5422 if (bdp->end_vcol <= oap->end_vcol
5423 && (!is_del
5424 || oap->op_type == OP_APPEND
5425 || oap->op_type == OP_REPLACE)) /* line too short */
5426 {
5427#ifdef FEAT_VISUALEXTRA
5428 bdp->is_short = TRUE;
5429#endif
5430 /* Alternative: include spaces to fill up the block.
5431 * Disadvantage: can lead to trailing spaces when the line is
5432 * short where the text is put */
5433 /* if (!is_del || oap->op_type == OP_APPEND) */
5434 if (oap->op_type == OP_APPEND || virtual_op)
5435 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005436 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 else
5438 bdp->endspaces = 0; /* replace doesn't add characters */
5439 }
5440 else if (bdp->end_vcol > oap->end_vcol)
5441 {
5442 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5443 if (!is_del && bdp->endspaces)
5444 {
5445 bdp->endspaces = incr - bdp->endspaces;
5446 if (pend != pstart)
5447 pend = prev_pend;
5448 }
5449 }
5450 }
5451#ifdef FEAT_VISUALEXTRA
5452 bdp->end_char_vcols = incr;
5453#endif
5454 if (is_del && bdp->startspaces)
5455 pstart = prev_pstart;
5456 bdp->textlen = (int)(pend - pstart);
5457 }
5458 bdp->textcol = (colnr_T) (pstart - line);
5459 bdp->textstart = pstart;
5460}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005463 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005465 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005466op_addsub(
5467 oparg_T *oap,
5468 linenr_T Prenum1, /* Amount of add/subtract */
5469 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005471 pos_T pos;
5472 struct block_def bd;
5473 int change_cnt = 0;
5474 linenr_T amount = Prenum1;
5475
5476 if (!VIsual_active)
5477 {
5478 pos = curwin->w_cursor;
5479 if (u_save_cursor() == FAIL)
5480 return;
5481 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5482 if (change_cnt)
5483 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5484 }
5485 else
5486 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005487 int one_change;
5488 int length;
5489 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005490
5491 if (u_save((linenr_T)(oap->start.lnum - 1),
5492 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5493 return;
5494
5495 pos = oap->start;
5496 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5497 {
5498 if (oap->block_mode) /* Visual block mode */
5499 {
5500 block_prep(oap, &bd, pos.lnum, FALSE);
5501 pos.col = bd.textcol;
5502 length = bd.textlen;
5503 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005504 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005505 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005506 curwin->w_cursor.col = 0;
5507 pos.col = 0;
5508 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5509 }
5510 else /* oap->motion_type == MCHAR */
5511 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005512 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005513 dec(&(oap->end));
5514 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5515 pos.col = 0;
5516 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005517 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005518 pos.col += oap->start.col;
5519 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005520 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005521 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005522 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005523 length = (int)STRLEN(ml_get(oap->end.lnum));
5524 if (oap->end.col >= length)
5525 oap->end.col = length - 1;
5526 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005527 }
5528 }
5529 one_change = do_addsub(oap->op_type, &pos, length, amount);
5530 if (one_change)
5531 {
5532 /* Remember the start position of the first change. */
5533 if (change_cnt == 0)
5534 startpos = curbuf->b_op_start;
5535 ++change_cnt;
5536 }
5537
5538#ifdef FEAT_NETBEANS_INTG
5539 if (netbeans_active() && one_change)
5540 {
5541 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5542
5543 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5544 netbeans_inserted(curbuf, pos.lnum, pos.col,
5545 &ptr[pos.col], length);
5546 }
5547#endif
5548 if (g_cmd && one_change)
5549 amount += Prenum1;
5550 }
5551 if (change_cnt)
5552 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5553
5554 if (!change_cnt && oap->is_VIsual)
5555 /* No change: need to remove the Visual selection */
5556 redraw_curbuf_later(INVERTED);
5557
5558 /* Set '[ mark if something changed. Keep the last end
5559 * position from do_addsub(). */
5560 if (change_cnt > 0)
5561 curbuf->b_op_start = startpos;
5562
5563 if (change_cnt > p_report)
5564 {
5565 if (change_cnt == 1)
5566 MSG(_("1 line changed"));
5567 else
5568 smsg((char_u *)_("%ld lines changed"), change_cnt);
5569 }
5570 }
5571}
5572
5573/*
5574 * Add or subtract 'Prenum1' from a number in a line
5575 * op_type is OP_NR_ADD or OP_NR_SUB
5576 *
5577 * Returns TRUE if some character was changed.
5578 */
5579 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005580do_addsub(
5581 int op_type,
5582 pos_T *pos,
5583 int length,
5584 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005585{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 int col;
5587 char_u *buf1;
5588 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005589 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005591 uvarnumber_T n;
5592 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 char_u *ptr;
5594 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 int todel;
5596 int dohex;
5597 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005598 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 int doalp;
5600 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005602 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005603 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005604 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005605 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005606 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005607 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005608 pos_T startpos;
5609 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
5611 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5612 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005613 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5615
Bram Moolenaard79e5502016-01-10 22:13:02 +01005616 curwin->w_cursor = *pos;
5617 ptr = ml_get(pos->lnum);
5618 col = pos->col;
5619
5620 if (*ptr == NUL)
5621 goto theend;
5622
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 /*
5624 * First check if we are on a hexadecimal number, after the "0x".
5625 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005626 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005628 if (dobin)
5629 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005630 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005631 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005632#ifdef FEAT_MBYTE
5633 if (has_mbyte)
5634 col -= (*mb_head_off)(ptr, ptr + col);
5635#endif
5636 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005637
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005638 if (dohex)
5639 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005640 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005641 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005642#ifdef FEAT_MBYTE
5643 if (has_mbyte)
5644 col -= (*mb_head_off)(ptr, ptr + col);
5645#endif
5646 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005647
5648 if ( dobin
5649 && dohex
5650 && ! ((col > 0
5651 && (ptr[col] == 'X'
5652 || ptr[col] == 'x')
5653 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005654#ifdef FEAT_MBYTE
5655 && (!has_mbyte ||
5656 !(*mb_head_off)(ptr, ptr + col - 1))
5657#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005658 && vim_isxdigit(ptr[col + 1]))))
5659 {
5660
5661 /* In case of binary/hexadecimal pattern overlap match, rescan */
5662
Bram Moolenaard79e5502016-01-10 22:13:02 +01005663 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005664
5665 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005666 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005667 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005668#ifdef FEAT_MBYTE
5669 if (has_mbyte)
5670 col -= (*mb_head_off)(ptr, ptr + col);
5671#endif
5672 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005673 }
5674
5675 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005676 && col > 0
5677 && (ptr[col] == 'X'
5678 || ptr[col] == 'x')
5679 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005680#ifdef FEAT_MBYTE
5681 && (!has_mbyte ||
5682 !(*mb_head_off)(ptr, ptr + col - 1))
5683#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005684 && vim_isxdigit(ptr[col + 1])) ||
5685 ( dobin
5686 && col > 0
5687 && (ptr[col] == 'B'
5688 || ptr[col] == 'b')
5689 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005690#ifdef FEAT_MBYTE
5691 && (!has_mbyte ||
5692 !(*mb_head_off)(ptr, ptr + col - 1))
5693#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005694 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005695 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005696 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005697 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005698#ifdef FEAT_MBYTE
5699 if (has_mbyte)
5700 col -= (*mb_head_off)(ptr, ptr + col);
5701#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005702 }
5703 else
5704 {
5705 /*
5706 * Search forward and then backward to find the start of number.
5707 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005708 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005709
5710 while (ptr[col] != NUL
5711 && !vim_isdigit(ptr[col])
5712 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005713 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005714
5715 while (col > 0
5716 && vim_isdigit(ptr[col - 1])
5717 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005718 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005719 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005720#ifdef FEAT_MBYTE
5721 if (has_mbyte)
5722 col -= (*mb_head_off)(ptr, ptr + col);
5723#endif
5724 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005725 }
5726 }
5727
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005728 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005729 {
5730 while (ptr[col] != NUL && length > 0
5731 && !vim_isdigit(ptr[col])
5732 && !(doalp && ASCII_ISALPHA(ptr[col])))
5733 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005734 int mb_len = MB_PTR2LEN(ptr + col);
5735
5736 col += mb_len;
5737 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005738 }
5739
5740 if (length == 0)
5741 goto theend;
5742
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005743 if (col > pos->col && ptr[col - 1] == '-'
5744#ifdef FEAT_MBYTE
5745 && (!has_mbyte ||
5746 !(*mb_head_off)(ptr, ptr + col - 1))
5747#endif
5748 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005749 {
5750 negative = TRUE;
5751 was_positive = FALSE;
5752 }
5753 }
5754
5755 /*
5756 * If a number was found, and saving for undo works, replace the number.
5757 */
5758 firstdigit = ptr[col];
5759 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5760 {
5761 beep_flush();
5762 goto theend;
5763 }
5764
5765 if (doalp && ASCII_ISALPHA(firstdigit))
5766 {
5767 /* decrement or increment alphabetic character */
5768 if (op_type == OP_NR_SUB)
5769 {
5770 if (CharOrd(firstdigit) < Prenum1)
5771 {
5772 if (isupper(firstdigit))
5773 firstdigit = 'A';
5774 else
5775 firstdigit = 'a';
5776 }
5777 else
5778#ifdef EBCDIC
5779 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5780#else
5781 firstdigit -= Prenum1;
5782#endif
5783 }
5784 else
5785 {
5786 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5787 {
5788 if (isupper(firstdigit))
5789 firstdigit = 'Z';
5790 else
5791 firstdigit = 'z';
5792 }
5793 else
5794#ifdef EBCDIC
5795 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5796#else
5797 firstdigit += Prenum1;
5798#endif
5799 }
5800 curwin->w_cursor.col = col;
5801 if (!did_change)
5802 startpos = curwin->w_cursor;
5803 did_change = TRUE;
5804 (void)del_char(FALSE);
5805 ins_char(firstdigit);
5806 endpos = curwin->w_cursor;
5807 curwin->w_cursor.col = col;
5808 }
5809 else
5810 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005811 if (col > 0 && ptr[col - 1] == '-'
5812#ifdef FEAT_MBYTE
5813 && (!has_mbyte ||
5814 !(*mb_head_off)(ptr, ptr + col - 1))
5815#endif
5816 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005817 {
5818 /* negative number */
5819 --col;
5820 negative = TRUE;
5821 }
5822 /* get the number value (unsigned) */
5823 if (visual && VIsual_mode != 'V')
5824 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5825 ? (int)STRLEN(ptr) - col
5826 : length);
5827
5828 vim_str2nr(ptr + col, &pre, &length,
5829 0 + (dobin ? STR2NR_BIN : 0)
5830 + (dooct ? STR2NR_OCT : 0)
5831 + (dohex ? STR2NR_HEX : 0),
5832 NULL, &n, maxlen);
5833
5834 /* ignore leading '-' for hex and octal and bin numbers */
5835 if (pre && negative)
5836 {
5837 ++col;
5838 --length;
5839 negative = FALSE;
5840 }
5841 /* add or subtract */
5842 subtract = FALSE;
5843 if (op_type == OP_NR_SUB)
5844 subtract ^= TRUE;
5845 if (negative)
5846 subtract ^= TRUE;
5847
5848 oldn = n;
5849 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005850 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005851 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005852 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005853 /* handle wraparound for decimal numbers */
5854 if (!pre)
5855 {
5856 if (subtract)
5857 {
5858 if (n > oldn)
5859 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005860 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005861 negative ^= TRUE;
5862 }
5863 }
5864 else
5865 {
5866 /* add */
5867 if (n < oldn)
5868 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005869 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005870 negative ^= TRUE;
5871 }
5872 }
5873 if (n == 0)
5874 negative = FALSE;
5875 }
5876
5877 if (visual && !was_positive && !negative && col > 0)
5878 {
5879 /* need to remove the '-' */
5880 col--;
5881 length++;
5882 }
5883
5884 /*
5885 * Delete the old number.
5886 */
5887 curwin->w_cursor.col = col;
5888 if (!did_change)
5889 startpos = curwin->w_cursor;
5890 did_change = TRUE;
5891 todel = length;
5892 c = gchar_cursor();
5893 /*
5894 * Don't include the '-' in the length, only the length of the
5895 * part after it is kept the same.
5896 */
5897 if (c == '-')
5898 --length;
5899 while (todel-- > 0)
5900 {
5901 if (c < 0x100 && isalpha(c))
5902 {
5903 if (isupper(c))
5904 hexupper = TRUE;
5905 else
5906 hexupper = FALSE;
5907 }
5908 /* del_char() will mark line needing displaying */
5909 (void)del_char(FALSE);
5910 c = gchar_cursor();
5911 }
5912
5913 /*
5914 * Prepare the leading characters in buf1[].
5915 * When there are many leading zeros it could be very long.
5916 * Allocate a bit too much.
5917 */
5918 buf1 = alloc((unsigned)length + NUMBUFLEN);
5919 if (buf1 == NULL)
5920 goto theend;
5921 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005922 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005923 {
5924 *ptr++ = '-';
5925 }
5926 if (pre)
5927 {
5928 *ptr++ = '0';
5929 --length;
5930 }
5931 if (pre == 'b' || pre == 'B' ||
5932 pre == 'x' || pre == 'X')
5933 {
5934 *ptr++ = pre;
5935 --length;
5936 }
5937
5938 /*
5939 * Put the number characters in buf2[].
5940 */
5941 if (pre == 'b' || pre == 'B')
5942 {
5943 int i;
5944 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005945 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005946
5947 /* leading zeros */
5948 for (bit = bits; bit > 0; bit--)
5949 if ((n >> (bit - 1)) & 0x1) break;
5950
5951 for (i = 0; bit > 0; bit--)
5952 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5953
5954 buf2[i] = '\0';
5955 }
5956 else if (pre == 0)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005957 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005958 else if (pre == '0')
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005959 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005960 else if (pre && hexupper)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005961 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005962 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005963 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005964 length -= (int)STRLEN(buf2);
5965
5966 /*
5967 * Adjust number of zeros to the new number of digits, so the
5968 * total length of the number remains the same.
5969 * Don't do this when
5970 * the result may look like an octal number.
5971 */
5972 if (firstdigit == '0' && !(dooct && pre == 0))
5973 while (length-- > 0)
5974 *ptr++ = '0';
5975 *ptr = NUL;
5976 STRCAT(buf1, buf2);
5977 ins_str(buf1); /* insert the new number */
5978 vim_free(buf1);
5979 endpos = curwin->w_cursor;
5980 if (did_change && curwin->w_cursor.col)
5981 --curwin->w_cursor.col;
5982 }
5983
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005984 if (did_change)
5985 {
5986 /* set the '[ and '] marks */
5987 curbuf->b_op_start = startpos;
5988 curbuf->b_op_end = endpos;
5989 if (curbuf->b_op_end.col > 0)
5990 --curbuf->b_op_end.col;
5991 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005992
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005993theend:
5994 if (visual)
5995 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005996 else if (did_change)
5997 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005998
Bram Moolenaard79e5502016-01-10 22:13:02 +01005999 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000}
6001
6002#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006003
6004static yankreg_T *y_read_regs = NULL;
6005
6006#define REG_PREVIOUS 1
6007#define REG_EXEC 2
6008
6009/*
6010 * Prepare for reading viminfo registers when writing viminfo later.
6011 */
6012 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006013prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006014{
6015 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6016 * (int)sizeof(yankreg_T));
6017}
6018
6019 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006020finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006021{
6022 int i;
6023 int j;
6024
6025 if (y_read_regs != NULL)
6026 {
6027 for (i = 0; i < NUM_REGISTERS; ++i)
6028 if (y_read_regs[i].y_array != NULL)
6029 {
6030 for (j = 0; j < y_read_regs[i].y_size; j++)
6031 vim_free(y_read_regs[i].y_array[j]);
6032 vim_free(y_read_regs[i].y_array);
6033 }
6034 vim_free(y_read_regs);
6035 y_read_regs = NULL;
6036 }
6037}
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006040read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 int eof;
6043 int do_it = TRUE;
6044 int size;
6045 int limit;
6046 int i;
6047 int set_prev = FALSE;
6048 char_u *str;
6049 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006050 int new_type = MCHAR; /* init to shut up compiler */
6051 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
6053 /* We only get here (hopefully) if line[0] == '"' */
6054 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006055
6056 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (*str == '"')
6058 {
6059 set_prev = TRUE;
6060 str++;
6061 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (!ASCII_ISALNUM(*str) && *str != '-')
6064 {
6065 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6066 return TRUE; /* too many errors, pretend end-of-file */
6067 do_it = FALSE;
6068 }
6069 get_yank_register(*str++, FALSE);
6070 if (!force && y_current->y_array != NULL)
6071 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006072
6073 if (*str == '@')
6074 {
6075 /* "x@: register x used for @@ */
6076 if (force || execreg_lastc == NUL)
6077 execreg_lastc = str[-1];
6078 }
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 size = 0;
6081 limit = 100; /* Optimized for registers containing <= 100 lines */
6082 if (do_it)
6083 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006084 /*
6085 * Build the new register in array[].
6086 * y_array is kept as-is until done.
6087 * The "do_it" flag is reset when something is wrong, in which case
6088 * array[] needs to be freed.
6089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (set_prev)
6091 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006092 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006093 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006095 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006097 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006099 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 /* get the block width; if it's missing we get a zero, which is OK */
6101 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006102 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 }
6104
6105 while (!(eof = viminfo_readline(virp))
6106 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6107 {
6108 if (do_it)
6109 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006110 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006112 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006114
6115 if (new_array == NULL)
6116 {
6117 do_it = FALSE;
6118 break;
6119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006121 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006123 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 }
6126 str = viminfo_readstring(virp, 1, TRUE);
6127 if (str != NULL)
6128 array[size++] = str;
6129 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006130 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 do_it = FALSE;
6132 }
6133 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (do_it)
6136 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006137 /* free y_array[] */
6138 for (i = 0; i < y_current->y_size; i++)
6139 vim_free(y_current->y_array[i]);
6140 vim_free(y_current->y_array);
6141
6142 y_current->y_type = new_type;
6143 y_current->y_width = new_width;
6144 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006145 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (size == 0)
6147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 y_current->y_array = NULL;
6149 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006150 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006152 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 y_current->y_array =
6154 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6155 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006156 {
6157 if (y_current->y_array == NULL)
6158 vim_free(array[i]);
6159 else
6160 y_current->y_array[i] = array[i];
6161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006164 else
6165 {
6166 /* Free array[] if it was filled. */
6167 for (i = 0; i < size; i++)
6168 vim_free(array[i]);
6169 }
6170 vim_free(array);
6171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return eof;
6173}
6174
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006175/*
6176 * Accept a new style register line from the viminfo, store it when it's new.
6177 */
6178 void
6179handle_viminfo_register(garray_T *values, int force)
6180{
6181 bval_T *vp = (bval_T *)values->ga_data;
6182 int flags;
6183 int name;
6184 int type;
6185 int linecount;
6186 int width;
6187 time_t timestamp;
6188 yankreg_T *y_ptr;
6189 int i;
6190
6191 /* Check the format:
6192 * |{bartype},{flags},{name},{type},
6193 * {linecount},{width},{timestamp},"line1","line2"
6194 */
6195 if (values->ga_len < 6
6196 || vp[0].bv_type != BVAL_NR
6197 || vp[1].bv_type != BVAL_NR
6198 || vp[2].bv_type != BVAL_NR
6199 || vp[3].bv_type != BVAL_NR
6200 || vp[4].bv_type != BVAL_NR
6201 || vp[5].bv_type != BVAL_NR)
6202 return;
6203 flags = vp[0].bv_nr;
6204 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006205 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006206 return;
6207 type = vp[2].bv_nr;
6208 if (type != MCHAR && type != MLINE && type != MBLOCK)
6209 return;
6210 linecount = vp[3].bv_nr;
6211 if (values->ga_len < 6 + linecount)
6212 return;
6213 width = vp[4].bv_nr;
6214 if (width < 0)
6215 return;
6216
6217 if (y_read_regs != NULL)
6218 /* Reading viminfo for merging and writing. Store the register
6219 * content, don't update the current registers. */
6220 y_ptr = &y_read_regs[name];
6221 else
6222 y_ptr = &y_regs[name];
6223
6224 /* Do not overwrite unless forced or the timestamp is newer. */
6225 timestamp = (time_t)vp[5].bv_nr;
6226 if (y_ptr->y_array != NULL && !force
6227 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6228 return;
6229
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006230 if (y_ptr->y_array != NULL)
6231 for (i = 0; i < y_ptr->y_size; i++)
6232 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006233 vim_free(y_ptr->y_array);
6234
6235 if (y_read_regs == NULL)
6236 {
6237 if (flags & REG_PREVIOUS)
6238 y_previous = y_ptr;
6239 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6240 execreg_lastc = get_register_name(name);
6241 }
6242 y_ptr->y_type = type;
6243 y_ptr->y_width = width;
6244 y_ptr->y_size = linecount;
6245 y_ptr->y_time_set = timestamp;
6246 if (linecount == 0)
6247 y_ptr->y_array = NULL;
6248 else
6249 {
6250 y_ptr->y_array =
6251 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6252 for (i = 0; i < linecount; i++)
6253 {
6254 if (vp[i + 6].bv_allocated)
6255 {
6256 y_ptr->y_array[i] = vp[i + 6].bv_string;
6257 vp[i + 6].bv_string = NULL;
6258 }
6259 else
6260 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6261 }
6262 }
6263}
6264
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006266write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006268 int i, j;
6269 char_u *type;
6270 char_u c;
6271 int num_lines;
6272 int max_num_lines;
6273 int max_kbyte;
6274 long len;
6275 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
Bram Moolenaar64404472010-06-26 06:24:45 +02006277 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
6279 /* Get '<' value, use old '"' value if '<' is not found. */
6280 max_num_lines = get_viminfo_parameter('<');
6281 if (max_num_lines < 0)
6282 max_num_lines = get_viminfo_parameter('"');
6283 if (max_num_lines == 0)
6284 return;
6285 max_kbyte = get_viminfo_parameter('s');
6286 if (max_kbyte == 0)
6287 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 for (i = 0; i < NUM_REGISTERS; i++)
6290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291#ifdef FEAT_CLIPBOARD
6292 /* Skip '*'/'+' register, we don't want them back next time */
6293 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6294 continue;
6295#endif
6296#ifdef FEAT_DND
6297 /* Neither do we want the '~' register */
6298 if (i == TILDE_REGISTER)
6299 continue;
6300#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006301 /* When reading viminfo for merging and writing: Use the register from
6302 * viminfo if it's newer. */
6303 if (y_read_regs != NULL
6304 && y_read_regs[i].y_array != NULL
6305 && (y_regs[i].y_array == NULL ||
6306 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6307 y_ptr = &y_read_regs[i];
6308 else if (y_regs[i].y_array == NULL)
6309 continue;
6310 else
6311 y_ptr = &y_regs[i];
6312
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006313 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006314 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006315 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006316 || (num_lines == 1 && y_ptr->y_type == MCHAR
6317 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006318 continue;
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (max_kbyte > 0)
6321 {
6322 /* Skip register if there is more text than the maximum size. */
6323 len = 0;
6324 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006325 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (len > (long)max_kbyte * 1024L)
6327 continue;
6328 }
6329
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006330 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
6332 case MLINE:
6333 type = (char_u *)"LINE";
6334 break;
6335 case MCHAR:
6336 type = (char_u *)"CHAR";
6337 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 case MBLOCK:
6339 type = (char_u *)"BLOCK";
6340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 default:
6342 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006343 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 emsg(IObuff);
6345 type = (char_u *)"LINE";
6346 break;
6347 }
6348 if (y_previous == &y_regs[i])
6349 fprintf(fp, "\"");
6350 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006351 fprintf(fp, "\"%c", c);
6352 if (c == execreg_lastc)
6353 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006354 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356 /* If max_num_lines < 0, then we save ALL the lines in the register */
6357 if (max_num_lines > 0 && num_lines > max_num_lines)
6358 num_lines = max_num_lines;
6359 for (j = 0; j < num_lines; j++)
6360 {
6361 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006362 viminfo_writestring(fp, y_ptr->y_array[j]);
6363 }
6364
6365 {
6366 int flags = 0;
6367 int remaining;
6368
6369 /* New style with a bar line. Format:
6370 * |{bartype},{flags},{name},{type},
6371 * {linecount},{width},{timestamp},"line1","line2"
6372 * flags: REG_PREVIOUS - register is y_previous
6373 * REG_EXEC - used for @@
6374 */
6375 if (y_previous == &y_regs[i])
6376 flags |= REG_PREVIOUS;
6377 if (c == execreg_lastc)
6378 flags |= REG_EXEC;
6379 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6380 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6381 (long)y_ptr->y_time_set);
6382 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6383 remaining = LSIZE - 71;
6384 for (j = 0; j < num_lines; j++)
6385 {
6386 putc(',', fp);
6387 --remaining;
6388 remaining = barline_writestring(fp, y_ptr->y_array[j],
6389 remaining);
6390 }
6391 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 }
6393 }
6394}
6395#endif /* FEAT_VIMINFO */
6396
6397#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6398/*
6399 * SELECTION / PRIMARY ('*')
6400 *
6401 * Text selection stuff that uses the GUI selection register '*'. When using a
6402 * GUI this may be text from another window, otherwise it is the last text we
6403 * had highlighted with VIsual mode. With mouse support, clicking the middle
6404 * button performs the paste, otherwise you will need to do <"*p>. "
6405 * If not under X, it is synonymous with the clipboard register '+'.
6406 *
6407 * X CLIPBOARD ('+')
6408 *
6409 * Text selection stuff that uses the GUI clipboard register '+'.
6410 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6411 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6412 * otherwise you will need to do <"+p>. "
6413 * If not under X, it is synonymous with the selection register '*'.
6414 */
6415
6416/*
6417 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006418 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 * only exist while the owning application exists, so we write to the
6420 * permanent (while X runs) store CUT_BUFFER0.
6421 * Dump the CLIPBOARD selection if we own it (it's logically the more
6422 * 'permanent' of the two), otherwise the PRIMARY one.
6423 * For now, use a hard-coded sanity limit of 1Mb of data.
6424 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006425#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006427x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 Display *dpy;
6430 char_u *str = NULL;
6431 long_u len = 0;
6432 int motion_type = -1;
6433
6434# ifdef FEAT_GUI
6435 if (gui.in_use)
6436 dpy = X_DISPLAY;
6437 else
6438# endif
6439# ifdef FEAT_XCLIPBOARD
6440 dpy = xterm_dpy;
6441# else
6442 return;
6443# endif
6444
6445 /* Get selection to export */
6446 if (clip_plus.owned)
6447 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6448 else if (clip_star.owned)
6449 motion_type = clip_convert_selection(&str, &len, &clip_star);
6450
6451 /* Check it's OK */
6452 if (dpy != NULL && str != NULL && motion_type >= 0
6453 && len < 1024*1024 && len > 0)
6454 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006455#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006456 int ok = TRUE;
6457
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006458 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6459 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6460 * encoding conversion usually doesn't work, so keep the text as-is.
6461 */
6462 if (has_mbyte)
6463 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006464 vimconv_T vc;
6465
6466 vc.vc_type = CONV_NONE;
6467 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6468 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006469 int intlen = len;
6470 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006471
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006472 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006473 conv_str = string_convert(&vc, str, &intlen);
6474 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006475 if (conv_str != NULL)
6476 {
6477 vim_free(str);
6478 str = conv_str;
6479 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006480 else
6481 {
6482 ok = FALSE;
6483 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006484 convert_setup(&vc, NULL, NULL);
6485 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006486 else
6487 {
6488 ok = FALSE;
6489 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006490 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006491
6492 /* Do not store the string if conversion failed. Better to use any
6493 * other selection than garbled text. */
6494 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006495#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006496 {
6497 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6498 XFlush(dpy);
6499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 }
6501
6502 vim_free(str);
6503}
6504#endif
6505
6506 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006507clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006509 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511 if (cbd == &clip_plus)
6512 y_current = &y_regs[PLUS_REGISTER];
6513 else
6514 y_current = &y_regs[STAR_REGISTER];
6515 free_yank_all();
6516 y_current->y_size = 0;
6517 y_current = y_ptr;
6518}
6519
6520/*
6521 * Get the selected text and put it in the gui selection register '*' or '+'.
6522 */
6523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006524clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006526 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 pos_T old_visual;
6529 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 colnr_T old_curswant;
6531 int old_set_curswant;
6532 pos_T old_op_start, old_op_end;
6533 oparg_T oa;
6534 cmdarg_T ca;
6535
6536 if (cbd->owned)
6537 {
6538 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6539 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6540 return;
6541
6542 /* Get the text between clip_star.start & clip_star.end */
6543 old_y_previous = y_previous;
6544 old_y_current = y_current;
6545 old_cursor = curwin->w_cursor;
6546 old_curswant = curwin->w_curswant;
6547 old_set_curswant = curwin->w_set_curswant;
6548 old_op_start = curbuf->b_op_start;
6549 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 old_visual = VIsual;
6551 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 clear_oparg(&oa);
6553 oa.regname = (cbd == &clip_plus ? '+' : '*');
6554 oa.op_type = OP_YANK;
6555 vim_memset(&ca, 0, sizeof(ca));
6556 ca.oap = &oa;
6557 ca.cmdchar = 'y';
6558 ca.count1 = 1;
6559 ca.retval = CA_NO_ADJ_OP_END;
6560 do_pending_operator(&ca, 0, TRUE);
6561 y_previous = old_y_previous;
6562 y_current = old_y_current;
6563 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006564 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 curwin->w_curswant = old_curswant;
6566 curwin->w_set_curswant = old_set_curswant;
6567 curbuf->b_op_start = old_op_start;
6568 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 VIsual = old_visual;
6570 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006572 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
6574 clip_free_selection(cbd);
6575
6576 /* Try to get selected text from another window */
6577 clip_gen_request_selection(cbd);
6578 }
6579}
6580
Bram Moolenaard44347f2011-06-19 01:14:29 +02006581/*
6582 * Convert from the GUI selection string into the '*'/'+' register.
6583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006585clip_yank_selection(
6586 int type,
6587 char_u *str,
6588 long len,
6589 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006591 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 if (cbd == &clip_plus)
6594 y_ptr = &y_regs[PLUS_REGISTER];
6595 else
6596 y_ptr = &y_regs[STAR_REGISTER];
6597
6598 clip_free_selection(cbd);
6599
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006600 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601}
6602
6603/*
6604 * Convert the '*'/'+' register into a GUI selection string returned in *str
6605 * with length *len.
6606 * Returns the motion type, or -1 for failure.
6607 */
6608 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006609clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 char_u *p;
6612 int lnum;
6613 int i, j;
6614 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006615 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616
6617 if (cbd == &clip_plus)
6618 y_ptr = &y_regs[PLUS_REGISTER];
6619 else
6620 y_ptr = &y_regs[STAR_REGISTER];
6621
6622#ifdef USE_CRNL
6623 eolsize = 2;
6624#else
6625 eolsize = 1;
6626#endif
6627
6628 *str = NULL;
6629 *len = 0;
6630 if (y_ptr->y_array == NULL)
6631 return -1;
6632
6633 for (i = 0; i < y_ptr->y_size; i++)
6634 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6635
6636 /*
6637 * Don't want newline character at end of last line if we're in MCHAR mode.
6638 */
6639 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6640 *len -= eolsize;
6641
6642 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6643 if (p == NULL)
6644 return -1;
6645 lnum = 0;
6646 for (i = 0, j = 0; i < (int)*len; i++, j++)
6647 {
6648 if (y_ptr->y_array[lnum][j] == '\n')
6649 p[i] = NUL;
6650 else if (y_ptr->y_array[lnum][j] == NUL)
6651 {
6652#ifdef USE_CRNL
6653 p[i++] = '\r';
6654#endif
6655#ifdef USE_CR
6656 p[i] = '\r';
6657#else
6658 p[i] = '\n';
6659#endif
6660 lnum++;
6661 j = -1;
6662 }
6663 else
6664 p[i] = y_ptr->y_array[lnum][j];
6665 }
6666 return y_ptr->y_type;
6667}
6668
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670/*
6671 * If we have written to a clipboard register, send the text to the clipboard.
6672 */
6673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006674may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6677 {
6678 clip_own_selection(&clip_star);
6679 clip_gen_set_selection(&clip_star);
6680 }
6681 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6682 {
6683 clip_own_selection(&clip_plus);
6684 clip_gen_set_selection(&clip_plus);
6685 }
6686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688#endif /* FEAT_CLIPBOARD || PROTO */
6689
6690
6691#if defined(FEAT_DND) || defined(PROTO)
6692/*
6693 * Replace the contents of the '~' register with str.
6694 */
6695 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006696dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006698 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
6700 curr = y_current;
6701 y_current = &y_regs[TILDE_REGISTER];
6702 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006703 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 y_current = curr;
6705}
6706#endif
6707
6708
6709#if defined(FEAT_EVAL) || defined(PROTO)
6710/*
6711 * Return the type of a register.
6712 * Used for getregtype()
6713 * Returns MAUTO for error.
6714 */
6715 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006716get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718 switch (regname)
6719 {
6720 case '%': /* file name */
6721 case '#': /* alternate file name */
6722 case '=': /* expression */
6723 case ':': /* last command line */
6724 case '/': /* last search-pattern */
6725 case '.': /* last inserted text */
6726#ifdef FEAT_SEARCHPATH
6727 case Ctrl_F: /* Filename under cursor */
6728 case Ctrl_P: /* Path under cursor, expand via "path" */
6729#endif
6730 case Ctrl_W: /* word under cursor */
6731 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6732 case '_': /* black hole: always empty */
6733 return MCHAR;
6734 }
6735
6736#ifdef FEAT_CLIPBOARD
6737 regname = may_get_selection(regname);
6738#endif
6739
Bram Moolenaar32b92012014-01-14 12:33:36 +01006740 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006741 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006742
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 get_yank_register(regname, FALSE);
6744
6745 if (y_current->y_array != NULL)
6746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (reglen != NULL && y_current->y_type == MBLOCK)
6748 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 return y_current->y_type;
6750 }
6751 return MAUTO;
6752}
6753
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006754static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006755
6756/*
6757 * When "flags" has GREG_LIST return a list with text "s".
6758 * Otherwise just return "s".
6759 */
6760 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006762{
6763 if (flags & GREG_LIST)
6764 {
6765 list_T *list = list_alloc();
6766
6767 if (list != NULL)
6768 {
6769 if (list_append_string(list, NULL, -1) == FAIL)
6770 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006771 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006772 return NULL;
6773 }
6774 list->lv_first->li_tv.vval.v_string = s;
6775 }
6776 return (char_u *)list;
6777 }
6778 return s;
6779}
6780
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781/*
6782 * Return the contents of a register as a single allocated string.
6783 * Used for "@r" in expressions and for getreg().
6784 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006785 * Flags:
6786 * GREG_NO_EXPR Do not allow expression register
6787 * GREG_EXPR_SRC For the expression register: return expression itself,
6788 * not the result of its evaluation.
6789 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 */
6791 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006792get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 long i;
6795 char_u *retval;
6796 int allocated;
6797 long len;
6798
6799 /* Don't allow using an expression register inside an expression */
6800 if (regname == '=')
6801 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006802 if (flags & GREG_NO_EXPR)
6803 return NULL;
6804 if (flags & GREG_EXPR_SRC)
6805 return getreg_wrap_one_line(get_expr_line_src(), flags);
6806 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
6809 if (regname == '@') /* "@@" is used for unnamed register */
6810 regname = '"';
6811
6812 /* check for valid regname */
6813 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6814 return NULL;
6815
6816#ifdef FEAT_CLIPBOARD
6817 regname = may_get_selection(regname);
6818#endif
6819
6820 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6821 {
6822 if (retval == NULL)
6823 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006824 if (allocated)
6825 return getreg_wrap_one_line(retval, flags);
6826 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
6828
6829 get_yank_register(regname, FALSE);
6830 if (y_current->y_array == NULL)
6831 return NULL;
6832
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006833 if (flags & GREG_LIST)
6834 {
6835 list_T *list = list_alloc();
6836 int error = FALSE;
6837
6838 if (list == NULL)
6839 return NULL;
6840 for (i = 0; i < y_current->y_size; ++i)
6841 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6842 error = TRUE;
6843 if (error)
6844 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006845 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006846 return NULL;
6847 }
6848 return (char_u *)list;
6849 }
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 /*
6852 * Compute length of resulting string.
6853 */
6854 len = 0;
6855 for (i = 0; i < y_current->y_size; ++i)
6856 {
6857 len += (long)STRLEN(y_current->y_array[i]);
6858 /*
6859 * Insert a newline between lines and after last line if
6860 * y_type is MLINE.
6861 */
6862 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6863 ++len;
6864 }
6865
6866 retval = lalloc(len + 1, TRUE);
6867
6868 /*
6869 * Copy the lines of the yank register into the string.
6870 */
6871 if (retval != NULL)
6872 {
6873 len = 0;
6874 for (i = 0; i < y_current->y_size; ++i)
6875 {
6876 STRCPY(retval + len, y_current->y_array[i]);
6877 len += (long)STRLEN(retval + len);
6878
6879 /*
6880 * Insert a NL between lines and after the last line if y_type is
6881 * MLINE.
6882 */
6883 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6884 retval[len++] = '\n';
6885 }
6886 retval[len] = NUL;
6887 }
6888
6889 return retval;
6890}
6891
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006892 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006893init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006894 int name,
6895 yankreg_T **old_y_previous,
6896 yankreg_T **old_y_current,
6897 int must_append,
6898 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006899{
6900 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6901 {
6902 emsg_invreg(name);
6903 return FAIL;
6904 }
6905
6906 /* Don't want to change the current (unnamed) register */
6907 *old_y_previous = y_previous;
6908 *old_y_current = y_current;
6909
6910 get_yank_register(name, TRUE);
6911 if (!y_append && !must_append)
6912 free_yank_all();
6913 return OK;
6914}
6915
6916 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006917finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006918 int name,
6919 yankreg_T *old_y_previous,
6920 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006921{
6922# ifdef FEAT_CLIPBOARD
6923 /* Send text of clipboard register to the clipboard. */
6924 may_set_selection();
6925# endif
6926
6927 /* ':let @" = "val"' should change the meaning of the "" register */
6928 if (name != '"')
6929 y_previous = old_y_previous;
6930 y_current = old_y_current;
6931}
6932
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933/*
6934 * Store string "str" in register "name".
6935 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6936 * If "must_append" is TRUE, always append to the register. Otherwise append
6937 * if "name" is an uppercase letter.
6938 * Note: "maxlen" and "must_append" don't work for the "/" register.
6939 * Careful: 'str' is modified, you may have to use a copy!
6940 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6941 */
6942 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006943write_reg_contents(
6944 int name,
6945 char_u *str,
6946 int maxlen,
6947 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
6949 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6950}
6951
6952 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006953write_reg_contents_lst(
6954 int name,
6955 char_u **strings,
6956 int maxlen UNUSED,
6957 int must_append,
6958 int yank_type,
6959 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006960{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006961 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006962
6963 if (name == '/'
6964#ifdef FEAT_EVAL
6965 || name == '='
6966#endif
6967 )
6968 {
6969 char_u *s;
6970
6971 if (strings[0] == NULL)
6972 s = (char_u *)"";
6973 else if (strings[1] != NULL)
6974 {
6975 EMSG(_("E883: search pattern and expression register may not "
6976 "contain two or more lines"));
6977 return;
6978 }
6979 else
6980 s = strings[0];
6981 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6982 return;
6983 }
6984
6985 if (name == '_') /* black hole: nothing to do */
6986 return;
6987
6988 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6989 &yank_type) == FAIL)
6990 return;
6991
6992 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6993
6994 finish_write_reg(name, old_y_previous, old_y_current);
6995}
6996
6997 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006998write_reg_contents_ex(
6999 int name,
7000 char_u *str,
7001 int maxlen,
7002 int must_append,
7003 int yank_type,
7004 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007006 yankreg_T *old_y_previous, *old_y_current;
7007 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
Bram Moolenaare7566042005-06-17 22:00:15 +00007009 if (maxlen >= 0)
7010 len = maxlen;
7011 else
7012 len = (long)STRLEN(str);
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 /* Special case: '/' search pattern */
7015 if (name == '/')
7016 {
7017 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7018 return;
7019 }
7020
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007021 if (name == '#')
7022 {
7023 buf_T *buf;
7024
7025 if (VIM_ISDIGIT(*str))
7026 {
7027 int num = atoi((char *)str);
7028
7029 buf = buflist_findnr(num);
7030 if (buf == NULL)
7031 EMSGN(_(e_nobufnr), (long)num);
7032 }
7033 else
7034 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7035 TRUE, FALSE, FALSE));
7036 if (buf == NULL)
7037 return;
7038 curwin->w_alt_fnum = buf->b_fnum;
7039 return;
7040 }
7041
Bram Moolenaare7566042005-06-17 22:00:15 +00007042#ifdef FEAT_EVAL
7043 if (name == '=')
7044 {
7045 char_u *p, *s;
7046
7047 p = vim_strnsave(str, (int)len);
7048 if (p == NULL)
7049 return;
7050 if (must_append)
7051 {
7052 s = concat_str(get_expr_line_src(), p);
7053 vim_free(p);
7054 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007055 }
7056 set_expr_line(p);
7057 return;
7058 }
7059#endif
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 if (name == '_') /* black hole: nothing to do */
7062 return;
7063
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007064 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7065 &yank_type) == FAIL)
7066 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007068 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007070 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071}
7072#endif /* FEAT_EVAL */
7073
7074#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7075/*
7076 * Put a string into a register. When the register is not empty, the string
7077 * is appended.
7078 */
7079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007080str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007081 yankreg_T *y_ptr, /* pointer to yank register */
7082 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7083 char_u *str, /* string to put in register */
7084 long len, /* length of string */
7085 long blocklen, /* width of Visual block */
7086 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007088 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 int lnum;
7090 long start;
7091 long i;
7092 int extra;
7093 int newlines; /* number of lines added */
7094 int extraline = 0; /* extra line at the end */
7095 int append = FALSE; /* append to last line in register */
7096 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007097 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007101 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 y_ptr->y_size = 0;
7103
Bram Moolenaard44347f2011-06-19 01:14:29 +02007104 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007105 type = ((str_list || (len > 0 && (str[len - 1] == NL
7106 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007107 ? MLINE : MCHAR);
7108 else
7109 type = yank_type;
7110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 /*
7112 * Count the number of lines within the string
7113 */
7114 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007115 if (str_list)
7116 {
7117 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007120 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007122 for (i = 0; i < len; i++)
7123 if (str[i] == '\n')
7124 ++newlines;
7125 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7126 {
7127 extraline = 1;
7128 ++newlines; /* count extra newline at the end */
7129 }
7130 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7131 {
7132 append = TRUE;
7133 --newlines; /* uncount newline when appending first line */
7134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
7136
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007137 /* Without any lines make the register empty. */
7138 if (y_ptr->y_size + newlines == 0)
7139 {
7140 vim_free(y_ptr->y_array);
7141 y_ptr->y_array = NULL;
7142 return;
7143 }
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 /*
7146 * Allocate an array to hold the pointers to the new register lines.
7147 * If the register was not empty, move the existing lines to the new array.
7148 */
7149 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7150 * sizeof(char_u *), TRUE);
7151 if (pp == NULL) /* out of memory */
7152 return;
7153 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7154 pp[lnum] = y_ptr->y_array[lnum];
7155 vim_free(y_ptr->y_array);
7156 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 /*
7160 * Find the end of each line and save it into the array.
7161 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007162 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007164 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7165 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007166 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007167 pp[lnum] = vim_strnsave(*ss, i);
7168 if (i > maxlen)
7169 maxlen = i;
7170 }
7171 }
7172 else
7173 {
7174 for (start = 0; start < len + extraline; start += i + 1)
7175 {
7176 for (i = start; i < len; ++i) /* find the end of the line */
7177 if (str[i] == '\n')
7178 break;
7179 i -= start; /* i is now length of line */
7180 if (i > maxlen)
7181 maxlen = i;
7182 if (append)
7183 {
7184 --lnum;
7185 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7186 }
7187 else
7188 extra = 0;
7189 s = alloc((unsigned)(i + extra + 1));
7190 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007192 if (extra)
7193 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7194 if (append)
7195 vim_free(y_ptr->y_array[lnum]);
7196 if (i)
7197 mch_memmove(s + extra, str + start, (size_t)i);
7198 extra += i;
7199 s[extra] = NUL;
7200 y_ptr->y_array[lnum++] = s;
7201 while (--extra >= 0)
7202 {
7203 if (*s == NUL)
7204 *s = '\n'; /* replace NUL with newline */
7205 ++s;
7206 }
7207 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
7210 y_ptr->y_type = type;
7211 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 if (type == MBLOCK)
7213 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7214 else
7215 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007216#ifdef FEAT_VIMINFO
7217 y_ptr->y_time_set = vim_time();
7218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219}
7220#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7221
7222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007223clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224{
7225 vim_memset(oap, 0, sizeof(oparg_T));
7226}
7227
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007228static 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 +00007229
7230/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007231 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 *
7233 * "Words" are counted by looking for boundaries between non-space and
7234 * space characters. (it seems to produce results that match 'wc'.)
7235 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007236 * Return value is byte count; word count for the line is added to "*wc".
7237 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 *
7239 * The function will only examine the first "limit" characters in the
7240 * line, stopping if it encounters an end-of-line (NUL byte). In that
7241 * case, eol_size will be added to the character count to account for
7242 * the size of the EOL character.
7243 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007244 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007245line_count_info(
7246 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007247 varnumber_T *wc,
7248 varnumber_T *cc,
7249 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007250 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007252 varnumber_T i;
7253 varnumber_T words = 0;
7254 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 int is_word = 0;
7256
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007257 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 if (is_word)
7260 {
7261 if (vim_isspace(line[i]))
7262 {
7263 words++;
7264 is_word = 0;
7265 }
7266 }
7267 else if (!vim_isspace(line[i]))
7268 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007269 ++chars;
7270#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007271 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007272#else
7273 ++i;
7274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276
7277 if (is_word)
7278 words++;
7279 *wc += words;
7280
7281 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007282 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007285 chars += eol_size;
7286 }
7287 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 return i;
7289}
7290
7291/*
7292 * Give some info about the position of the cursor (for "g CTRL-G").
7293 * In Visual mode, give some info about the selected region. (In this case,
7294 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007295 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 */
7297 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007298cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
7300 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007301 char_u buf1[50];
7302 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007304 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007305#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007306 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007307#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007308 varnumber_T byte_count_cursor = 0;
7309 varnumber_T char_count = 0;
7310 varnumber_T char_count_cursor = 0;
7311 varnumber_T word_count = 0;
7312 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007313 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007314 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 long line_count_selected = 0;
7316 pos_T min_pos, max_pos;
7317 oparg_T oparg;
7318 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 /*
7321 * Compute the length of the file in characters.
7322 */
7323 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7324 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007325 if (dict == NULL)
7326 {
7327 MSG(_(no_lines_msg));
7328 return;
7329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 }
7331 else
7332 {
7333 if (get_fileformat(curbuf) == EOL_DOS)
7334 eol_size = 2;
7335 else
7336 eol_size = 1;
7337
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (VIsual_active)
7339 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007340 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 {
7342 min_pos = VIsual;
7343 max_pos = curwin->w_cursor;
7344 }
7345 else
7346 {
7347 min_pos = curwin->w_cursor;
7348 max_pos = VIsual;
7349 }
7350 if (*p_sel == 'e' && max_pos.col > 0)
7351 --max_pos.col;
7352
7353 if (VIsual_mode == Ctrl_V)
7354 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007355#ifdef FEAT_LINEBREAK
7356 char_u * saved_sbr = p_sbr;
7357
7358 /* Make 'sbr' empty for a moment to get the correct size. */
7359 p_sbr = empty_option;
7360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 oparg.is_VIsual = 1;
7362 oparg.block_mode = TRUE;
7363 oparg.op_type = OP_NOP;
7364 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007365 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007366#ifdef FEAT_LINEBREAK
7367 p_sbr = saved_sbr;
7368#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007369 if (curwin->w_curswant == MAXCOL)
7370 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 /* Swap the start, end vcol if needed */
7372 if (oparg.end_vcol < oparg.start_vcol)
7373 {
7374 oparg.end_vcol += oparg.start_vcol;
7375 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7376 oparg.end_vcol -= oparg.start_vcol;
7377 }
7378 }
7379 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
7382 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7383 {
7384 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007385 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 {
7387 ui_breakcheck();
7388 if (got_int)
7389 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007390 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 }
7392
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 /* Do extra processing for VIsual mode. */
7394 if (VIsual_active
7395 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7396 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007397 char_u *s = NULL;
7398 long len = 0L;
7399
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 switch (VIsual_mode)
7401 {
7402 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007403#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007407#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007409#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007410 s = bd.textstart;
7411 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 break;
7413 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007414 s = ml_get(lnum);
7415 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 break;
7417 case 'v':
7418 {
7419 colnr_T start_col = (lnum == min_pos.lnum)
7420 ? min_pos.col : 0;
7421 colnr_T end_col = (lnum == max_pos.lnum)
7422 ? max_pos.col - start_col + 1 : MAXCOL;
7423
Bram Moolenaardef9e822004-12-31 20:58:58 +00007424 s = ml_get(lnum) + start_col;
7425 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427 break;
7428 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007429 if (s != NULL)
7430 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007431 byte_count_cursor += line_count_info(s, &word_count_cursor,
7432 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007433 if (lnum == curbuf->b_ml.ml_line_count
7434 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007435 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007436 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007437 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
7440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
7442 /* In non-visual mode, check for the line the cursor is on */
7443 if (lnum == curwin->w_cursor.lnum)
7444 {
7445 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007446 char_count_cursor += char_count;
7447 byte_count_cursor = byte_count +
7448 line_count_info(ml_get(lnum),
7449 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007450 (varnumber_T)(curwin->w_cursor.col + 1),
7451 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 }
7453 }
7454 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007455 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007456 &char_count, (varnumber_T)MAXCOL,
7457 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459
7460 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007461 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007462 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
Bram Moolenaared767a22016-01-03 22:49:16 +01007464 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007466 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007468 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7469 {
7470 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7471 &max_pos.col);
7472 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7473 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7474 }
7475 else
7476 buf1[0] = NUL;
7477
7478 if (char_count_cursor == byte_count_cursor
7479 && char_count == byte_count)
7480 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007481 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007482 buf1, line_count_selected,
7483 (long)curbuf->b_ml.ml_line_count,
7484 word_count_cursor, word_count,
7485 byte_count_cursor, byte_count);
7486 else
7487 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007488 _("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 +01007489 buf1, line_count_selected,
7490 (long)curbuf->b_ml.ml_line_count,
7491 word_count_cursor, word_count,
7492 char_count_cursor, char_count,
7493 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
7495 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007496 {
7497 p = ml_get_curline();
7498 validate_virtcol();
7499 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7500 (int)curwin->w_virtcol + 1);
7501 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7502 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaared767a22016-01-03 22:49:16 +01007504 if (char_count_cursor == byte_count_cursor
7505 && char_count == byte_count)
7506 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007507 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007508 (char *)buf1, (char *)buf2,
7509 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 (long)curbuf->b_ml.ml_line_count,
7511 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007512 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007513 else
7514 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007515 _("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 +01007516 (char *)buf1, (char *)buf2,
7517 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007518 (long)curbuf->b_ml.ml_line_count,
7519 word_count_cursor, word_count,
7520 char_count_cursor, char_count,
7521 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007522 }
7523 }
7524
Bram Moolenaared767a22016-01-03 22:49:16 +01007525#ifdef FEAT_MBYTE
7526 bom_count = bomb_size();
7527 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007528 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
7529 _("(+%ld for BOM)"), bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007530#endif
7531 if (dict == NULL)
7532 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007533 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007534 p = p_shm;
7535 p_shm = (char_u *)"";
7536 msg(IObuff);
7537 p_shm = p;
7538 }
7539 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007540#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007541 if (dict != NULL)
7542 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007543 dict_add_nr_str(dict, "words", word_count, NULL);
7544 dict_add_nr_str(dict, "chars", char_count, NULL);
7545 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007546# ifdef FEAT_MBYTE
7547 + bom_count
7548# endif
7549 , NULL);
7550 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007551 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007552 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007553 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007554 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007555 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558}