blob: 0348353fc952e2e4c6e93de930abf0bbe9f26922 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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/*
53 * Each yank register is an array of pointers to lines.
54 */
55static struct yankreg
56{
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 Moolenaar071d4272004-06-13 20:20:40 +000061} y_regs[NUM_REGISTERS];
62
63static struct yankreg *y_current; /* ptr to current yankreg */
64static int y_append; /* TRUE when appending */
65static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
66
67/*
68 * structure used by block_prep, op_delete and op_yank for blockwise operators
69 * also op_change, op_shift, op_insert, op_replace - AKelly
70 */
71struct block_def
72{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000073 int startspaces; /* 'extra' cols before first char */
74 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000076 char_u *textstart; /* pointer to 1st char (partially) in block */
77 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 colnr_T start_vcol; /* start col of 1st char wholly inside block */
79 colnr_T end_vcol; /* start col of 1st char wholly after block */
80#ifdef FEAT_VISUALEXTRA
81 int is_short; /* TRUE if line is too short to fit in block */
82 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
83 int is_oneChar; /* TRUE if block within one character */
84 int pre_whitesp; /* screen cols of ws before block */
85 int pre_whitesp_c; /* chars of ws before block */
86 colnr_T end_char_vcols; /* number of vcols of post-block char */
87#endif
88 colnr_T start_char_vcols; /* number of vcols of pre-block char */
89};
90
91#ifdef FEAT_VISUALEXTRA
92static void shift_block __ARGS((oparg_T *oap, int amount));
93static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
94#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000095static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000096static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000097static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
98 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +000099static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
101static void mb_adjust_opend __ARGS((oparg_T *oap));
102#endif
103static void free_yank __ARGS((long));
104static void free_yank_all __ARGS((void));
105static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
106#ifdef FEAT_CLIPBOARD
107static void copy_yank_reg __ARGS((struct yankreg *reg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static void may_set_selection __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110static void dis_msg __ARGS((char_u *p, int skip_esc));
Bram Moolenaar81340392012-06-06 16:12:59 +0200111#if defined(FEAT_COMMENTS) || defined(PROTO)
112static char_u *skip_comment __ARGS((char_u *line, int process, int include_space, int *is_comment));
113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar0eac8282014-04-12 12:26:36 +0200116static void str_to_reg __ARGS((struct yankreg *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118static int ends_in_white __ARGS((linenr_T lnum));
119#ifdef FEAT_COMMENTS
120static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
121static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
122#else
123static int fmt_check_par __ARGS((linenr_T));
124#endif
125
126/*
127 * The names of operators.
128 * IMPORTANT: Index must correspond with defines in vim.h!!!
129 * The third field indicates whether the operator always works on lines.
130 */
131static char opchars[][3] =
132{
133 {NUL, NUL, FALSE}, /* OP_NOP */
134 {'d', NUL, FALSE}, /* OP_DELETE */
135 {'y', NUL, FALSE}, /* OP_YANK */
136 {'c', NUL, FALSE}, /* OP_CHANGE */
137 {'<', NUL, TRUE}, /* OP_LSHIFT */
138 {'>', NUL, TRUE}, /* OP_RSHIFT */
139 {'!', NUL, TRUE}, /* OP_FILTER */
140 {'g', '~', FALSE}, /* OP_TILDE */
141 {'=', NUL, TRUE}, /* OP_INDENT */
142 {'g', 'q', TRUE}, /* OP_FORMAT */
143 {':', NUL, TRUE}, /* OP_COLON */
144 {'g', 'U', FALSE}, /* OP_UPPER */
145 {'g', 'u', FALSE}, /* OP_LOWER */
146 {'J', NUL, TRUE}, /* DO_JOIN */
147 {'g', 'J', TRUE}, /* DO_JOIN_NS */
148 {'g', '?', FALSE}, /* OP_ROT13 */
149 {'r', NUL, FALSE}, /* OP_REPLACE */
150 {'I', NUL, FALSE}, /* OP_INSERT */
151 {'A', NUL, FALSE}, /* OP_APPEND */
152 {'z', 'f', TRUE}, /* OP_FOLD */
153 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
154 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
155 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
156 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
157 {'z', 'd', TRUE}, /* OP_FOLDDEL */
158 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
159 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000160 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161};
162
163/*
164 * Translate a command name into an operator type.
165 * Must only be called with a valid operator name!
166 */
167 int
168get_op_type(char1, char2)
169 int char1;
170 int char2;
171{
172 int i;
173
174 if (char1 == 'r') /* ignore second character */
175 return OP_REPLACE;
176 if (char1 == '~') /* when tilde is an operator */
177 return OP_TILDE;
178 for (i = 0; ; ++i)
179 if (opchars[i][0] == char1 && opchars[i][1] == char2)
180 break;
181 return i;
182}
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Return TRUE if operator "op" always works on whole lines.
186 */
187 int
188op_on_lines(op)
189 int op;
190{
191 return opchars[op][2];
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
195 * Get first operator command character.
196 * Returns 'g' or 'z' if there is another command character.
197 */
198 int
199get_op_char(optype)
200 int optype;
201{
202 return opchars[optype][0];
203}
204
205/*
206 * Get second operator command character.
207 */
208 int
209get_extra_op_char(optype)
210 int optype;
211{
212 return opchars[optype][1];
213}
214
215/*
216 * op_shift - handle a shift operation
217 */
218 void
219op_shift(oap, curs_top, amount)
220 oparg_T *oap;
221 int curs_top;
222 int amount;
223{
224 long i;
225 int first_char;
226 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 if (u_save((linenr_T)(oap->start.lnum - 1),
230 (linenr_T)(oap->end.lnum + 1)) == FAIL)
231 return;
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (oap->block_mode)
234 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
236 for (i = oap->line_count; --i >= 0; )
237 {
238 first_char = *ml_get_curline();
239 if (first_char == NUL) /* empty line */
240 curwin->w_cursor.col = 0;
241#ifdef FEAT_VISUALEXTRA
242 else if (oap->block_mode)
243 shift_block(oap, amount);
244#endif
245 else
246 /* Move the line right if it doesn't start with '#', 'smartindent'
247 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
248#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
249 if (first_char != '#' || !preprocs_left())
250#endif
251 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000252 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
254 ++curwin->w_cursor.lnum;
255 }
256
257 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200258#ifdef FEAT_FOLDING
259 /* The cursor line is not in a closed fold */
260 foldOpenCursor();
261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (oap->block_mode)
264 {
265 curwin->w_cursor.lnum = oap->start.lnum;
266 curwin->w_cursor.col = block_col;
267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100268 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 curwin->w_cursor.lnum = oap->start.lnum;
271 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
272 }
273 else
274 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
275
276 if (oap->line_count > p_report)
277 {
278 if (oap->op_type == OP_RSHIFT)
279 s = (char_u *)">";
280 else
281 s = (char_u *)"<";
282 if (oap->line_count == 1)
283 {
284 if (amount == 1)
285 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
286 else
287 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
288 }
289 else
290 {
291 if (amount == 1)
292 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
293 oap->line_count, s);
294 else
295 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
296 oap->line_count, s, amount);
297 }
298 msg(IObuff);
299 }
300
301 /*
302 * Set "'[" and "']" marks.
303 */
304 curbuf->b_op_start = oap->start;
305 curbuf->b_op_end.lnum = oap->end.lnum;
306 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
307 if (curbuf->b_op_end.col > 0)
308 --curbuf->b_op_end.col;
309}
310
311/*
312 * shift the current line one shiftwidth left (if left != 0) or right
313 * leaves cursor on first blank in the line
314 */
315 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000316shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 int left;
318 int round;
319 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000320 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
322 int count;
323 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100324 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
326 count = get_indent(); /* get current indent */
327
328 if (round) /* round off indent */
329 {
330 i = count / p_sw; /* number of p_sw rounded down */
331 j = count % p_sw; /* extra spaces */
332 if (j && left) /* first remove extra spaces */
333 --amount;
334 if (left)
335 {
336 i -= amount;
337 if (i < 0)
338 i = 0;
339 }
340 else
341 i += amount;
342 count = i * p_sw;
343 }
344 else /* original vi indent */
345 {
346 if (left)
347 {
348 count -= p_sw * amount;
349 if (count < 0)
350 count = 0;
351 }
352 else
353 count += p_sw * amount;
354 }
355
356 /* Set new indent */
357#ifdef FEAT_VREPLACE
358 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000359 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 else
361#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000362 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363}
364
365#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
366/*
367 * Shift one line of the current block one shiftwidth right or left.
368 * Leaves cursor on first character in block.
369 */
370 static void
371shift_block(oap, amount)
372 oparg_T *oap;
373 int amount;
374{
375 int left = (oap->op_type == OP_LSHIFT);
376 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000377 int total;
378 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100380 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int p_ts = (int)curbuf->b_p_ts;
382 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int i = 0, j = 0;
386 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_RIGHTLEFT
388 int old_p_ri = p_ri;
389
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200390 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392
393 State = INSERT; /* don't want REPLACE for State */
394 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
395 if (bd.is_short)
396 return;
397
398 /* total is number of screen columns to be inserted/removed */
399 total = amount * p_sw;
400 oldp = ml_get_curline();
401
402 if (!left)
403 {
404 /*
405 * 1. Get start vcol
406 * 2. Total ws vcols
407 * 3. Divvy into TABs & spp
408 * 4. Construct new string
409 */
410 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
411 ws_vcol = bd.start_vcol - bd.pre_whitesp;
412 if (bd.startspaces)
413 {
414#ifdef FEAT_MBYTE
415 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000416 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000419 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 for ( ; vim_iswhite(*bd.textstart); )
422 {
423 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
424 total += incr;
425 bd.start_vcol += incr;
426 }
427 /* OK, now total=all the VWS reqd, and textstart points at the 1st
428 * non-ws char in the block. */
429 if (!curbuf->b_p_et)
430 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
431 if (i)
432 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
433 else
434 j = total;
435 /* if we're splitting a TAB, allow for it */
436 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
437 len = (int)STRLEN(bd.textstart) + 1;
438 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
439 if (newp == NULL)
440 return;
441 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
442 mch_memmove(newp, oldp, (size_t)bd.textcol);
443 copy_chars(newp + bd.textcol, (size_t)i, TAB);
444 copy_spaces(newp + bd.textcol + i, (size_t)j);
445 /* the end */
446 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
447 }
448 else /* left */
449 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000450 colnr_T destination_col; /* column to which text in block will
451 be shifted */
452 char_u *verbatim_copy_end; /* end of the part of the line which is
453 copied verbatim */
454 colnr_T verbatim_copy_width;/* the (displayed) width of this part
455 of line */
456 unsigned fill; /* nr of spaces that replace a TAB */
457 unsigned new_line_len; /* the length of the line after the
458 block shift */
459 size_t block_space_width;
460 size_t shift_amount;
461 char_u *non_white = bd.textstart;
462 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000464 /*
465 * Firstly, let's find the first non-whitespace character that is
466 * displayed after the block's start column and the character's column
467 * number. Also, let's calculate the width of all the whitespace
468 * characters that are displayed in the block and precede the searched
469 * non-whitespace character.
470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000472 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
473 * the part of which is displayed at the block's beginning. Let's start
474 * searching from the next character. */
475 if (bd.startspaces)
476 mb_ptr_adv(non_white);
477
478 /* The character's column is in "bd.start_vcol". */
479 non_white_col = bd.start_vcol;
480
481 while (vim_iswhite(*non_white))
482 {
483 incr = lbr_chartabsize_adv(&non_white, non_white_col);
484 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 }
486
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000487 block_space_width = non_white_col - oap->start_vcol;
488 /* We will shift by "total" or "block_space_width", whichever is less.
489 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000490 shift_amount = (block_space_width < (size_t)total
491 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000492
493 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000494 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000495
496 /* Now let's find out how much of the beginning of the line we can
497 * reuse without modification. */
498 verbatim_copy_end = bd.textstart;
499 verbatim_copy_width = bd.start_vcol;
500
501 /* If "bd.startspaces" is set, "bd.textstart" points to the character
502 * preceding the block. We have to subtract its width to obtain its
503 * column number. */
504 if (bd.startspaces)
505 verbatim_copy_width -= bd.start_char_vcols;
506 while (verbatim_copy_width < destination_col)
507 {
508 incr = lbr_chartabsize(verbatim_copy_end, verbatim_copy_width);
509 if (verbatim_copy_width + incr > destination_col)
510 break;
511 verbatim_copy_width += incr;
512 mb_ptr_adv(verbatim_copy_end);
513 }
514
515 /* If "destination_col" is different from the width of the initial
516 * part of the line that will be copied, it means we encountered a tab
517 * character, which we will have to partly replace with spaces. */
518 fill = destination_col - verbatim_copy_width;
519
520 /* The replacement line will consist of:
521 * - the beginning of the original line up to "verbatim_copy_end",
522 * - "fill" number of spaces,
523 * - the rest of the line, pointed to by non_white. */
524 new_line_len = (unsigned)(verbatim_copy_end - oldp)
525 + fill
526 + (unsigned)STRLEN(non_white) + 1;
527
528 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 if (newp == NULL)
530 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000531 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
532 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
533 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 }
535 /* replace the line */
536 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
537 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
538 State = oldstate;
539 curwin->w_cursor.col = oldcol;
540#ifdef FEAT_RIGHTLEFT
541 p_ri = old_p_ri;
542#endif
543}
544#endif
545
546#ifdef FEAT_VISUALEXTRA
547/*
548 * Insert string "s" (b_insert ? before : after) block :AKelly
549 * Caller must prepare for undo.
550 */
551 static void
552block_insert(oap, s, b_insert, bdp)
553 oparg_T *oap;
554 char_u *s;
555 int b_insert;
556 struct block_def *bdp;
557{
558 int p_ts;
559 int count = 0; /* extra spaces to replace a cut TAB */
560 int spaces = 0; /* non-zero if cutting a TAB */
561 colnr_T offset; /* pointer along new line */
562 unsigned s_len; /* STRLEN(s) */
563 char_u *newp, *oldp; /* new, old lines */
564 linenr_T lnum; /* loop var */
565 int oldstate = State;
566
567 State = INSERT; /* don't want REPLACE for State */
568 s_len = (unsigned)STRLEN(s);
569
570 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
571 {
572 block_prep(oap, bdp, lnum, TRUE);
573 if (bdp->is_short && b_insert)
574 continue; /* OP_INSERT, line ends before block start */
575
576 oldp = ml_get(lnum);
577
578 if (b_insert)
579 {
580 p_ts = bdp->start_char_vcols;
581 spaces = bdp->startspaces;
582 if (spaces != 0)
583 count = p_ts - 1; /* we're cutting a TAB */
584 offset = bdp->textcol;
585 }
586 else /* append */
587 {
588 p_ts = bdp->end_char_vcols;
589 if (!bdp->is_short) /* spaces = padding after block */
590 {
591 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
592 if (spaces != 0)
593 count = p_ts - 1; /* we're cutting a TAB */
594 offset = bdp->textcol + bdp->textlen - (spaces != 0);
595 }
596 else /* spaces = padding to block edge */
597 {
598 /* if $ used, just append to EOL (ie spaces==0) */
599 if (!bdp->is_MAX)
600 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
601 count = spaces;
602 offset = bdp->textcol + bdp->textlen;
603 }
604 }
605
606 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
607 if (newp == NULL)
608 continue;
609
610 /* copy up to shifted part */
611 mch_memmove(newp, oldp, (size_t)(offset));
612 oldp += offset;
613
614 /* insert pre-padding */
615 copy_spaces(newp + offset, (size_t)spaces);
616
617 /* copy the new text */
618 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
619 offset += s_len;
620
621 if (spaces && !bdp->is_short)
622 {
623 /* insert post-padding */
624 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
625 /* We're splitting a TAB, don't copy it. */
626 oldp++;
627 /* We allowed for that TAB, remember this now */
628 count++;
629 }
630
631 if (spaces > 0)
632 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000633 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 ml_replace(lnum, newp, FALSE);
636
637 if (lnum == oap->end.lnum)
638 {
639 /* Set "']" mark to the end of the block instead of the end of
640 * the insert in the first line. */
641 curbuf->b_op_end.lnum = oap->end.lnum;
642 curbuf->b_op_end.col = offset;
643 }
644 } /* for all lnum */
645
646 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
647
648 State = oldstate;
649}
650#endif
651
652#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
653/*
654 * op_reindent - handle reindenting a block of lines.
655 */
656 void
657op_reindent(oap, how)
658 oparg_T *oap;
659 int (*how) __ARGS((void));
660{
661 long i;
662 char_u *l;
663 int count;
664 linenr_T first_changed = 0;
665 linenr_T last_changed = 0;
666 linenr_T start_lnum = curwin->w_cursor.lnum;
667
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000668 /* Don't even try when 'modifiable' is off. */
669 if (!curbuf->b_p_ma)
670 {
671 EMSG(_(e_modifiable));
672 return;
673 }
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 for (i = oap->line_count; --i >= 0 && !got_int; )
676 {
677 /* it's a slow thing to do, so give feedback so there's no worry that
678 * the computer's just hung. */
679
680 if (i > 1
681 && (i % 50 == 0 || i == oap->line_count - 1)
682 && oap->line_count > p_report)
683 smsg((char_u *)_("%ld lines to indent... "), i);
684
685 /*
686 * Be vi-compatible: For lisp indenting the first line is not
687 * indented, unless there is only one line.
688 */
689#ifdef FEAT_LISP
690 if (i != oap->line_count - 1 || oap->line_count == 1
691 || how != get_lisp_indent)
692#endif
693 {
694 l = skipwhite(ml_get_curline());
695 if (*l == NUL) /* empty or blank line */
696 count = 0;
697 else
698 count = how(); /* get the indent for this line */
699
700 if (set_indent(count, SIN_UNDO))
701 {
702 /* did change the indent, call changed_lines() later */
703 if (first_changed == 0)
704 first_changed = curwin->w_cursor.lnum;
705 last_changed = curwin->w_cursor.lnum;
706 }
707 }
708 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000709 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711
712 /* put cursor on first non-blank of indented line */
713 curwin->w_cursor.lnum = start_lnum;
714 beginline(BL_SOL | BL_FIX);
715
716 /* Mark changed lines so that they will be redrawn. When Visual
717 * highlighting was present, need to continue until the last line. When
718 * there is no change still need to remove the Visual highlighting. */
719 if (last_changed != 0)
720 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 else if (oap->is_VIsual)
724 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
726 if (oap->line_count > p_report)
727 {
728 i = oap->line_count - (i + 1);
729 if (i == 1)
730 MSG(_("1 line indented "));
731 else
732 smsg((char_u *)_("%ld lines indented "), i);
733 }
734 /* set '[ and '] marks */
735 curbuf->b_op_start = oap->start;
736 curbuf->b_op_end = oap->end;
737}
738#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
739
740#if defined(FEAT_EVAL) || defined(PROTO)
741/*
742 * Keep the last expression line here, for repeating.
743 */
744static char_u *expr_line = NULL;
745
746/*
747 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
748 * Returns '=' when OK, NUL otherwise.
749 */
750 int
751get_expr_register()
752{
753 char_u *new_line;
754
755 new_line = getcmdline('=', 0L, 0);
756 if (new_line == NULL)
757 return NUL;
758 if (*new_line == NUL) /* use previous line */
759 vim_free(new_line);
760 else
761 set_expr_line(new_line);
762 return '=';
763}
764
765/*
766 * Set the expression for the '=' register.
767 * Argument must be an allocated string.
768 */
769 void
770set_expr_line(new_line)
771 char_u *new_line;
772{
773 vim_free(expr_line);
774 expr_line = new_line;
775}
776
777/*
778 * Get the result of the '=' register expression.
779 * Returns a pointer to allocated memory, or NULL for failure.
780 */
781 char_u *
782get_expr_line()
783{
784 char_u *expr_copy;
785 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000786 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788 if (expr_line == NULL)
789 return NULL;
790
791 /* Make a copy of the expression, because evaluating it may cause it to be
792 * changed. */
793 expr_copy = vim_strsave(expr_line);
794 if (expr_copy == NULL)
795 return NULL;
796
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000797 /* When we are invoked recursively limit the evaluation to 10 levels.
798 * Then return the string as-is. */
799 if (nested >= 10)
800 return expr_copy;
801
802 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000803 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000804 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 vim_free(expr_copy);
806 return rv;
807}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000808
809/*
810 * Get the '=' register expression itself, without evaluating it.
811 */
812 char_u *
813get_expr_line_src()
814{
815 if (expr_line == NULL)
816 return NULL;
817 return vim_strsave(expr_line);
818}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#endif /* FEAT_EVAL */
820
821/*
822 * Check if 'regname' is a valid name of a yank register.
823 * Note: There is no check for 0 (default register), caller should do this
824 */
825 int
826valid_yank_reg(regname, writing)
827 int regname;
828 int writing; /* if TRUE check for writable registers */
829{
830 if ( (regname > 0 && ASCII_ISALNUM(regname))
831 || (!writing && vim_strchr((char_u *)
832#ifdef FEAT_EVAL
833 "/.%#:="
834#else
835 "/.%#:"
836#endif
837 , regname) != NULL)
838 || regname == '"'
839 || regname == '-'
840 || regname == '_'
841#ifdef FEAT_CLIPBOARD
842 || regname == '*'
843 || regname == '+'
844#endif
845#ifdef FEAT_DND
846 || (!writing && regname == '~')
847#endif
848 )
849 return TRUE;
850 return FALSE;
851}
852
853/*
854 * Set y_current and y_append, according to the value of "regname".
855 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000856 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 *
858 * If regname is 0 and writing, use register 0
859 * If regname is 0 and reading, use previous register
860 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000861 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862get_yank_register(regname, writing)
863 int regname;
864 int writing;
865{
866 int i;
867
868 y_append = FALSE;
869 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
870 {
871 y_current = y_previous;
872 return;
873 }
874 i = regname;
875 if (VIM_ISDIGIT(i))
876 i -= '0';
877 else if (ASCII_ISLOWER(i))
878 i = CharOrdLow(i) + 10;
879 else if (ASCII_ISUPPER(i))
880 {
881 i = CharOrdUp(i) + 10;
882 y_append = TRUE;
883 }
884 else if (regname == '-')
885 i = DELETION_REGISTER;
886#ifdef FEAT_CLIPBOARD
887 /* When selection is not available, use register 0 instead of '*' */
888 else if (clip_star.available && regname == '*')
889 i = STAR_REGISTER;
890 /* When clipboard is not available, use register 0 instead of '+' */
891 else if (clip_plus.available && regname == '+')
892 i = PLUS_REGISTER;
893#endif
894#ifdef FEAT_DND
895 else if (!writing && regname == '~')
896 i = TILDE_REGISTER;
897#endif
898 else /* not 0-9, a-z, A-Z or '-': use register 0 */
899 i = 0;
900 y_current = &(y_regs[i]);
901 if (writing) /* remember the register we write into for do_put() */
902 y_previous = y_current;
903}
904
Bram Moolenaar8299df92004-07-10 09:47:34 +0000905#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906/*
907 * When "regname" is a clipboard register, obtain the selection. If it's not
908 * available return zero, otherwise return "regname".
909 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000910 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911may_get_selection(regname)
912 int regname;
913{
914 if (regname == '*')
915 {
916 if (!clip_star.available)
917 regname = 0;
918 else
919 clip_get_selection(&clip_star);
920 }
921 else if (regname == '+')
922 {
923 if (!clip_plus.available)
924 regname = 0;
925 else
926 clip_get_selection(&clip_plus);
927 }
928 return regname;
929}
930#endif
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932/*
933 * Obtain the contents of a "normal" register. The register is made empty.
934 * The returned pointer has allocated memory, use put_register() later.
935 */
936 void *
937get_register(name, copy)
938 int name;
939 int copy; /* make a copy, if FALSE make register empty. */
940{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000941 struct yankreg *reg;
942 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
944#ifdef FEAT_CLIPBOARD
945 /* When Visual area changed, may have to update selection. Obtain the
946 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000947 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200949 if (clip_isautosel_star())
950 clip_update_selection(&clip_star);
951 may_get_selection(name);
952 }
953 if (name == '+' && clip_plus.available)
954 {
955 if (clip_isautosel_plus())
956 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 may_get_selection(name);
958 }
959#endif
960
961 get_yank_register(name, 0);
962 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
963 if (reg != NULL)
964 {
965 *reg = *y_current;
966 if (copy)
967 {
968 /* If we run out of memory some or all of the lines are empty. */
969 if (reg->y_size == 0)
970 reg->y_array = NULL;
971 else
972 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
973 * reg->y_size));
974 if (reg->y_array != NULL)
975 {
976 for (i = 0; i < reg->y_size; ++i)
977 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
978 }
979 }
980 else
981 y_current->y_array = NULL;
982 }
983 return (void *)reg;
984}
985
986/*
Bram Moolenaar0a307462007-12-01 20:13:05 +0000987 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 */
989 void
990put_register(name, reg)
991 int name;
992 void *reg;
993{
994 get_yank_register(name, 0);
995 free_yank_all();
996 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +0000997 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100999#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 /* Send text written to clipboard register to the clipboard. */
1001 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001004
1005 void
1006free_register(reg)
1007 void *reg;
1008{
1009 struct yankreg tmp;
1010
1011 tmp = *y_current;
1012 *y_current = *(struct yankreg *)reg;
1013 free_yank_all();
1014 vim_free(reg);
1015 *y_current = tmp;
1016}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018#if defined(FEAT_MOUSE) || defined(PROTO)
1019/*
1020 * return TRUE if the current yank register has type MLINE
1021 */
1022 int
1023yank_register_mline(regname)
1024 int regname;
1025{
1026 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1027 return FALSE;
1028 if (regname == '_') /* black hole is always empty */
1029 return FALSE;
1030 get_yank_register(regname, FALSE);
1031 return (y_current->y_type == MLINE);
1032}
1033#endif
1034
1035/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001036 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001038 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
1040 int
1041do_record(c)
1042 int c;
1043{
Bram Moolenaard55de222007-05-06 13:38:48 +00001044 char_u *p;
1045 static int regname;
1046 struct yankreg *old_y_previous, *old_y_current;
1047 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 if (Recording == FALSE) /* start recording */
1050 {
1051 /* registers 0-9, a-z and " are allowed */
1052 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1053 retval = FAIL;
1054 else
1055 {
1056 Recording = TRUE;
1057 showmode();
1058 regname = c;
1059 retval = OK;
1060 }
1061 }
1062 else /* stop recording */
1063 {
1064 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1066 * needs to be removed again to put it in a register. exec_reg then
1067 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 */
1069 Recording = FALSE;
1070 MSG("");
1071 p = get_recorded();
1072 if (p == NULL)
1073 retval = FAIL;
1074 else
1075 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001076 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1077 vim_unescape_csi(p);
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 /*
1080 * We don't want to change the default register here, so save and
1081 * restore the current register name.
1082 */
1083 old_y_previous = y_previous;
1084 old_y_current = y_current;
1085
1086 retval = stuff_yank(regname, p);
1087
1088 y_previous = old_y_previous;
1089 y_current = old_y_current;
1090 }
1091 }
1092 return retval;
1093}
1094
1095/*
1096 * Stuff string "p" into yank register "regname" as a single line (append if
1097 * uppercase). "p" must have been alloced.
1098 *
1099 * return FAIL for failure, OK otherwise
1100 */
1101 static int
1102stuff_yank(regname, p)
1103 int regname;
1104 char_u *p;
1105{
1106 char_u *lp;
1107 char_u **pp;
1108
1109 /* check for read-only register */
1110 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1111 {
1112 vim_free(p);
1113 return FAIL;
1114 }
1115 if (regname == '_') /* black hole: don't do anything */
1116 {
1117 vim_free(p);
1118 return OK;
1119 }
1120 get_yank_register(regname, TRUE);
1121 if (y_append && y_current->y_array != NULL)
1122 {
1123 pp = &(y_current->y_array[y_current->y_size - 1]);
1124 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1125 if (lp == NULL)
1126 {
1127 vim_free(p);
1128 return FAIL;
1129 }
1130 STRCPY(lp, *pp);
1131 STRCAT(lp, p);
1132 vim_free(p);
1133 vim_free(*pp);
1134 *pp = lp;
1135 }
1136 else
1137 {
1138 free_yank_all();
1139 if ((y_current->y_array =
1140 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1141 {
1142 vim_free(p);
1143 return FAIL;
1144 }
1145 y_current->y_array[0] = p;
1146 y_current->y_size = 1;
1147 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1148 }
1149 return OK;
1150}
1151
Bram Moolenaar42b94362009-05-26 16:12:37 +00001152static int execreg_lastc = NUL;
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154/*
1155 * execute a yank register: copy it into the stuff buffer
1156 *
1157 * return FAIL for failure, OK otherwise
1158 */
1159 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001160do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 int regname;
1162 int colon; /* insert ':' before each line */
1163 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001164 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 long i;
1167 char_u *p;
1168 int retval = OK;
1169 int remap;
1170
1171 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001172 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001173 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001174 {
1175 EMSG(_("E748: No previously used register"));
1176 return FAIL;
1177 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001178 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 /* check for valid regname */
1181 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001182 {
1183 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001185 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001186 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
1188#ifdef FEAT_CLIPBOARD
1189 regname = may_get_selection(regname);
1190#endif
1191
1192 if (regname == '_') /* black hole: don't stuff anything */
1193 return OK;
1194
1195#ifdef FEAT_CMDHIST
1196 if (regname == ':') /* use last command line */
1197 {
1198 if (last_cmdline == NULL)
1199 {
1200 EMSG(_(e_nolastcmd));
1201 return FAIL;
1202 }
1203 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1204 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001205 /* Escape all control characters with a CTRL-V */
1206 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001207 (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 +00001208 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001209 {
1210 /* When in Visual mode "'<,'>" will be prepended to the command.
1211 * Remove it when it's already there. */
1212 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001213 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001214 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001215 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001216 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001217 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219#endif
1220#ifdef FEAT_EVAL
1221 else if (regname == '=')
1222 {
1223 p = get_expr_line();
1224 if (p == NULL)
1225 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001226 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 vim_free(p);
1228 }
1229#endif
1230 else if (regname == '.') /* use last inserted text */
1231 {
1232 p = get_last_insert_save();
1233 if (p == NULL)
1234 {
1235 EMSG(_(e_noinstext));
1236 return FAIL;
1237 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001238 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 vim_free(p);
1240 }
1241 else
1242 {
1243 get_yank_register(regname, FALSE);
1244 if (y_current->y_array == NULL)
1245 return FAIL;
1246
1247 /* Disallow remaping for ":@r". */
1248 remap = colon ? REMAP_NONE : REMAP_YES;
1249
1250 /*
1251 * Insert lines into typeahead buffer, from last one to first one.
1252 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001253 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 for (i = y_current->y_size; --i >= 0; )
1255 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001256 char_u *escaped;
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 /* insert NL between lines and after last line if type is MLINE */
1259 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1260 || addcr)
1261 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001262 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 return FAIL;
1264 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001265 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1266 if (escaped == NULL)
1267 return FAIL;
1268 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1269 vim_free(escaped);
1270 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001272 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 == FAIL)
1274 return FAIL;
1275 }
1276 Exec_reg = TRUE; /* disable the 'q' command */
1277 }
1278 return retval;
1279}
1280
1281/*
1282 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1283 * used only after other typeahead has been processed.
1284 */
1285 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001286put_reedit_in_typebuf(silent)
1287 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
1289 char_u buf[3];
1290
1291 if (restart_edit != NUL)
1292 {
1293 if (restart_edit == 'V')
1294 {
1295 buf[0] = 'g';
1296 buf[1] = 'R';
1297 buf[2] = NUL;
1298 }
1299 else
1300 {
1301 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1302 buf[1] = NUL;
1303 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001304 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 restart_edit = NUL;
1306 }
1307}
1308
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001309/*
1310 * Insert register contents "s" into the typeahead buffer, so that it will be
1311 * executed again.
1312 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1313 * no remapping.
1314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001316put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001318 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001320 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 int retval = OK;
1323
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001324 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001326 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001328 {
1329 char_u *p;
1330
1331 if (esc)
1332 p = vim_strsave_escape_csi(s);
1333 else
1334 p = s;
1335 if (p == NULL)
1336 retval = FAIL;
1337 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001338 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1339 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001340 if (esc)
1341 vim_free(p);
1342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001344 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 return retval;
1346}
1347
1348/*
1349 * Insert a yank register: copy it into the Read buffer.
1350 * Used by CTRL-R command and middle mouse button in insert mode.
1351 *
1352 * return FAIL for failure, OK otherwise
1353 */
1354 int
1355insert_reg(regname, literally)
1356 int regname;
1357 int literally; /* insert literally, not as if typed */
1358{
1359 long i;
1360 int retval = OK;
1361 char_u *arg;
1362 int allocated;
1363
1364 /*
1365 * It is possible to get into an endless loop by having CTRL-R a in
1366 * register a and then, in insert mode, doing CTRL-R a.
1367 * If you hit CTRL-C, the loop will be broken here.
1368 */
1369 ui_breakcheck();
1370 if (got_int)
1371 return FAIL;
1372
1373 /* check for valid regname */
1374 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1375 return FAIL;
1376
1377#ifdef FEAT_CLIPBOARD
1378 regname = may_get_selection(regname);
1379#endif
1380
1381 if (regname == '.') /* insert last inserted text */
1382 retval = stuff_inserted(NUL, 1L, TRUE);
1383 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1384 {
1385 if (arg == NULL)
1386 return FAIL;
1387 stuffescaped(arg, literally);
1388 if (allocated)
1389 vim_free(arg);
1390 }
1391 else /* name or number register */
1392 {
1393 get_yank_register(regname, FALSE);
1394 if (y_current->y_array == NULL)
1395 retval = FAIL;
1396 else
1397 {
1398 for (i = 0; i < y_current->y_size; ++i)
1399 {
1400 stuffescaped(y_current->y_array[i], literally);
1401 /*
1402 * Insert a newline between lines and after last line if
1403 * y_type is MLINE.
1404 */
1405 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1406 stuffcharReadbuff('\n');
1407 }
1408 }
1409 }
1410
1411 return retval;
1412}
1413
1414/*
1415 * Stuff a string into the typeahead buffer, such that edit() will insert it
1416 * literally ("literally" TRUE) or interpret is as typed characters.
1417 */
1418 static void
1419stuffescaped(arg, literally)
1420 char_u *arg;
1421 int literally;
1422{
1423 int c;
1424 char_u *start;
1425
1426 while (*arg != NUL)
1427 {
1428 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1429 * stuff K_SPECIAL to get the effect of a special key when "literally"
1430 * is TRUE. */
1431 start = arg;
1432 while ((*arg >= ' '
1433#ifndef EBCDIC
1434 && *arg < DEL /* EBCDIC: chars above space are normal */
1435#endif
1436 )
1437 || (*arg == K_SPECIAL && !literally))
1438 ++arg;
1439 if (arg > start)
1440 stuffReadbuffLen(start, (long)(arg - start));
1441
1442 /* stuff a single special character */
1443 if (*arg != NUL)
1444 {
1445#ifdef FEAT_MBYTE
1446 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001447 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 else
1449#endif
1450 c = *arg++;
1451 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1452 stuffcharReadbuff(Ctrl_V);
1453 stuffcharReadbuff(c);
1454 }
1455 }
1456}
1457
1458/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001459 * If "regname" is a special register, return TRUE and store a pointer to its
1460 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001462 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463get_spec_reg(regname, argp, allocated, errmsg)
1464 int regname;
1465 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001466 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int errmsg; /* give error message when failing */
1468{
1469 int cnt;
1470
1471 *argp = NULL;
1472 *allocated = FALSE;
1473 switch (regname)
1474 {
1475 case '%': /* file name */
1476 if (errmsg)
1477 check_fname(); /* will give emsg if not set */
1478 *argp = curbuf->b_fname;
1479 return TRUE;
1480
1481 case '#': /* alternate file name */
1482 *argp = getaltfname(errmsg); /* may give emsg if not set */
1483 return TRUE;
1484
1485#ifdef FEAT_EVAL
1486 case '=': /* result of expression */
1487 *argp = get_expr_line();
1488 *allocated = TRUE;
1489 return TRUE;
1490#endif
1491
1492 case ':': /* last command line */
1493 if (last_cmdline == NULL && errmsg)
1494 EMSG(_(e_nolastcmd));
1495 *argp = last_cmdline;
1496 return TRUE;
1497
1498 case '/': /* last search-pattern */
1499 if (last_search_pat() == NULL && errmsg)
1500 EMSG(_(e_noprevre));
1501 *argp = last_search_pat();
1502 return TRUE;
1503
1504 case '.': /* last inserted text */
1505 *argp = get_last_insert_save();
1506 *allocated = TRUE;
1507 if (*argp == NULL && errmsg)
1508 EMSG(_(e_noinstext));
1509 return TRUE;
1510
1511#ifdef FEAT_SEARCHPATH
1512 case Ctrl_F: /* Filename under cursor */
1513 case Ctrl_P: /* Path under cursor, expand via "path" */
1514 if (!errmsg)
1515 return FALSE;
1516 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001517 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 *allocated = TRUE;
1519 return TRUE;
1520#endif
1521
1522 case Ctrl_W: /* word under cursor */
1523 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1524 if (!errmsg)
1525 return FALSE;
1526 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1527 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1528 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1529 *allocated = TRUE;
1530 return TRUE;
1531
1532 case '_': /* black hole: always empty */
1533 *argp = (char_u *)"";
1534 return TRUE;
1535 }
1536
1537 return FALSE;
1538}
1539
1540/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001541 * Paste a yank register into the command line.
1542 * Only for non-special registers.
1543 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * insert_reg() can't be used here, because special characters from the
1545 * register contents will be interpreted as commands.
1546 *
1547 * return FAIL for failure, OK otherwise
1548 */
1549 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001550cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 int regname;
1552 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001553 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
1557 get_yank_register(regname, FALSE);
1558 if (y_current->y_array == NULL)
1559 return FAIL;
1560
1561 for (i = 0; i < y_current->y_size; ++i)
1562 {
1563 cmdline_paste_str(y_current->y_array[i], literally);
1564
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001565 /* Insert ^M between lines and after last line if type is MLINE.
1566 * Don't do this when "remcr" is TRUE and the next line is empty. */
1567 if (y_current->y_type == MLINE
1568 || (i < y_current->y_size - 1
1569 && !(remcr
1570 && i == y_current->y_size - 2
1571 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 cmdline_paste_str((char_u *)"\r", literally);
1573
1574 /* Check for CTRL-C, in case someone tries to paste a few thousand
1575 * lines and gets bored. */
1576 ui_breakcheck();
1577 if (got_int)
1578 return FAIL;
1579 }
1580 return OK;
1581}
1582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1584/*
1585 * Adjust the register name pointed to with "rp" for the clipboard being
1586 * used always and the clipboard being available.
1587 */
1588 void
1589adjust_clip_reg(rp)
1590 int *rp;
1591{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001592 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1593 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
1594 if (*rp == 0 && clip_unnamed != 0)
1595 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
1596 ? '+' : '*';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (!clip_star.available && *rp == '*')
1598 *rp = 0;
1599 if (!clip_plus.available && *rp == '+')
1600 *rp = 0;
1601}
1602#endif
1603
1604/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001605 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001607 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 */
1609 int
1610op_delete(oap)
1611 oparg_T *oap;
1612{
1613 int n;
1614 linenr_T lnum;
1615 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 char_u *newp, *oldp;
1617 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1619 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001620 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
1622 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1623 return OK;
1624
1625 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1626 if (oap->empty)
1627 return u_save_cursor();
1628
1629 if (!curbuf->b_p_ma)
1630 {
1631 EMSG(_(e_modifiable));
1632 return FAIL;
1633 }
1634
1635#ifdef FEAT_CLIPBOARD
1636 adjust_clip_reg(&oap->regname);
1637#endif
1638
1639#ifdef FEAT_MBYTE
1640 if (has_mbyte)
1641 mb_adjust_opend(oap);
1642#endif
1643
Bram Moolenaard04b7502010-07-08 22:27:55 +02001644 /*
1645 * Imitate the strange Vi behaviour: If the delete spans more than one
1646 * line and motion_type == MCHAR and the result is a blank line, make the
1647 * delete linewise. Don't do this for the change command or Visual mode.
1648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001651 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001653 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 && oap->op_type == OP_DELETE)
1655 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001656 ptr = ml_get(oap->end.lnum) + oap->end.col;
1657 if (*ptr != NUL)
1658 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 ptr = skipwhite(ptr);
1660 if (*ptr == NUL && inindent(0))
1661 oap->motion_type = MLINE;
1662 }
1663
Bram Moolenaard04b7502010-07-08 22:27:55 +02001664 /*
1665 * Check for trying to delete (e.g. "D") in an empty line.
1666 * Note: For the change operator it is ok.
1667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if ( oap->motion_type == MCHAR
1669 && oap->line_count == 1
1670 && oap->op_type == OP_DELETE
1671 && *ml_get(oap->start.lnum) == NUL)
1672 {
1673 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001674 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 * 'cpoptions' (Vi compatible).
1676 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001677#ifdef FEAT_VIRTUALEDIT
1678 if (virtual_op)
1679 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1680 * marks as if it happened. */
1681 goto setmarks;
1682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1684 beep_flush();
1685 return OK;
1686 }
1687
Bram Moolenaard04b7502010-07-08 22:27:55 +02001688 /*
1689 * Do a yank of whatever we're about to delete.
1690 * If a yank register was specified, put the deleted text into that
1691 * register. For the black hole register '_' don't yank anything.
1692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (oap->regname != '_')
1694 {
1695 if (oap->regname != 0)
1696 {
1697 /* check for read-only register */
1698 if (!valid_yank_reg(oap->regname, TRUE))
1699 {
1700 beep_flush();
1701 return OK;
1702 }
1703 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1704 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1705 did_yank = TRUE;
1706 }
1707
1708 /*
1709 * Put deleted text into register 1 and shift number registers if the
1710 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001711 * Use the register name from before adjust_clip_reg() may have
1712 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001714 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 || oap->line_count > 1 || oap->use_reg_one)
1716 {
1717 y_current = &y_regs[9];
1718 free_yank_all(); /* free register nine */
1719 for (n = 9; n > 1; --n)
1720 y_regs[n] = y_regs[n - 1];
1721 y_previous = y_current = &y_regs[1];
1722 y_regs[1].y_array = NULL; /* set register one to empty */
1723 if (op_yank(oap, TRUE, FALSE) == OK)
1724 did_yank = TRUE;
1725 }
1726
Bram Moolenaar84298db2012-04-20 13:46:08 +02001727 /* Yank into small delete register when no named register specified
1728 * and the delete is within one line. */
1729 if ((
1730#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001731 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1732 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001733#endif
1734 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 && oap->line_count == 1)
1736 {
1737 oap->regname = '-';
1738 get_yank_register(oap->regname, TRUE);
1739 if (op_yank(oap, TRUE, FALSE) == OK)
1740 did_yank = TRUE;
1741 oap->regname = 0;
1742 }
1743
1744 /*
1745 * If there's too much stuff to fit in the yank register, then get a
1746 * confirmation before doing the delete. This is crude, but simple.
1747 * And it avoids doing a delete of something we can't put back if we
1748 * want.
1749 */
1750 if (!did_yank)
1751 {
1752 int msg_silent_save = msg_silent;
1753
1754 msg_silent = 0; /* must display the prompt */
1755 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1756 msg_silent = msg_silent_save;
1757 if (n != 'y')
1758 {
1759 EMSG(_(e_abort));
1760 return FAIL;
1761 }
1762 }
1763 }
1764
Bram Moolenaard04b7502010-07-08 22:27:55 +02001765 /*
1766 * block mode delete
1767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (oap->block_mode)
1769 {
1770 if (u_save((linenr_T)(oap->start.lnum - 1),
1771 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1772 return FAIL;
1773
1774 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1775 {
1776 block_prep(oap, &bd, lnum, TRUE);
1777 if (bd.textlen == 0) /* nothing to delete */
1778 continue;
1779
1780 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1781 if (lnum == curwin->w_cursor.lnum)
1782 {
1783 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1784# ifdef FEAT_VIRTUALEDIT
1785 curwin->w_cursor.coladd = 0;
1786# endif
1787 }
1788
1789 /* n == number of chars deleted
1790 * If we delete a TAB, it may be replaced by several characters.
1791 * Thus the number of characters may increase!
1792 */
1793 n = bd.textlen - bd.startspaces - bd.endspaces;
1794 oldp = ml_get(lnum);
1795 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1796 if (newp == NULL)
1797 continue;
1798 /* copy up to deleted part */
1799 mch_memmove(newp, oldp, (size_t)bd.textcol);
1800 /* insert spaces */
1801 copy_spaces(newp + bd.textcol,
1802 (size_t)(bd.startspaces + bd.endspaces));
1803 /* copy the part after the deleted part */
1804 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001805 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 /* replace the line */
1807 ml_replace(lnum, newp, FALSE);
1808 }
1809
1810 check_cursor_col();
1811 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1812 oap->end.lnum + 1, 0L);
1813 oap->line_count = 0; /* no lines deleted */
1814 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001815 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
1817 if (oap->op_type == OP_CHANGE)
1818 {
1819 /* Delete the lines except the first one. Temporarily move the
1820 * cursor to the next line. Save the current line number, if the
1821 * last line is deleted it may be changed.
1822 */
1823 if (oap->line_count > 1)
1824 {
1825 lnum = curwin->w_cursor.lnum;
1826 ++curwin->w_cursor.lnum;
1827 del_lines((long)(oap->line_count - 1), TRUE);
1828 curwin->w_cursor.lnum = lnum;
1829 }
1830 if (u_save_cursor() == FAIL)
1831 return FAIL;
1832 if (curbuf->b_p_ai) /* don't delete indent */
1833 {
1834 beginline(BL_WHITE); /* cursor on first non-white */
1835 did_ai = TRUE; /* delete the indent when ESC hit */
1836 ai_col = curwin->w_cursor.col;
1837 }
1838 else
1839 beginline(0); /* cursor in column 0 */
1840 truncate_line(FALSE); /* delete the rest of the line */
1841 /* leave cursor past last char in line */
1842 if (oap->line_count > 1)
1843 u_clearline(); /* "U" command not possible after "2cc" */
1844 }
1845 else
1846 {
1847 del_lines(oap->line_count, TRUE);
1848 beginline(BL_WHITE | BL_FIX);
1849 u_clearline(); /* "U" command not possible after "dd" */
1850 }
1851 }
1852 else
1853 {
1854#ifdef FEAT_VIRTUALEDIT
1855 if (virtual_op)
1856 {
1857 int endcol = 0;
1858
1859 /* For virtualedit: break the tabs that are partly included. */
1860 if (gchar_pos(&oap->start) == '\t')
1861 {
1862 if (u_save_cursor() == FAIL) /* save first line for undo */
1863 return FAIL;
1864 if (oap->line_count == 1)
1865 endcol = getviscol2(oap->end.col, oap->end.coladd);
1866 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1867 oap->start = curwin->w_cursor;
1868 if (oap->line_count == 1)
1869 {
1870 coladvance(endcol);
1871 oap->end.col = curwin->w_cursor.col;
1872 oap->end.coladd = curwin->w_cursor.coladd;
1873 curwin->w_cursor = oap->start;
1874 }
1875 }
1876
1877 /* Break a tab only when it's included in the area. */
1878 if (gchar_pos(&oap->end) == '\t'
1879 && (int)oap->end.coladd < oap->inclusive)
1880 {
1881 /* save last line for undo */
1882 if (u_save((linenr_T)(oap->end.lnum - 1),
1883 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1884 return FAIL;
1885 curwin->w_cursor = oap->end;
1886 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1887 oap->end = curwin->w_cursor;
1888 curwin->w_cursor = oap->start;
1889 }
1890 }
1891#endif
1892
1893 if (oap->line_count == 1) /* delete characters within one line */
1894 {
1895 if (u_save_cursor() == FAIL) /* save line for undo */
1896 return FAIL;
1897
1898 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001899 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 && oap->op_type == OP_CHANGE
1901 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001902 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 display_dollar(oap->end.col - !oap->inclusive);
1904
1905 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1906
1907#ifdef FEAT_VIRTUALEDIT
1908 if (virtual_op)
1909 {
1910 /* fix up things for virtualedit-delete:
1911 * break the tabs which are going to get in our way
1912 */
1913 char_u *curline = ml_get_curline();
1914 int len = (int)STRLEN(curline);
1915
1916 if (oap->end.coladd != 0
1917 && (int)oap->end.col >= len - 1
1918 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1919 n++;
1920 /* Delete at least one char (e.g, when on a control char). */
1921 if (n == 0 && oap->start.coladd != oap->end.coladd)
1922 n = 1;
1923
1924 /* When deleted a char in the line, reset coladd. */
1925 if (gchar_cursor() != NUL)
1926 curwin->w_cursor.coladd = 0;
1927 }
1928#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001929 if (oap->op_type == OP_DELETE
1930 && oap->inclusive
1931 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001932 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1933 {
1934 /* Special case: gH<Del> deletes the last line. */
1935 del_lines(1L, FALSE);
1936 }
1937 else
1938 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001939 (void)del_bytes((long)n, !virtual_op,
1940 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 }
1943 else /* delete characters between lines */
1944 {
1945 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001946 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948 /* save deleted and changed lines for undo */
1949 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1950 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1951 return FAIL;
1952
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001953 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 truncate_line(TRUE); /* delete from cursor to end of line */
1955
1956 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1957 ++curwin->w_cursor.lnum;
1958 del_lines((long)(oap->line_count - 2), FALSE);
1959
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001960 if (delete_last_line)
1961 oap->end.lnum = curbuf->b_ml.ml_line_count;
1962
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001963 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001964 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001965 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1966 {
1967 /* Special case: gH<Del> deletes the last line. */
1968 del_lines(1L, FALSE);
1969 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01001970 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1971 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001972 }
1973 else
1974 {
1975 /* delete from start of line until op_end */
1976 curwin->w_cursor.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001977 (void)del_bytes((long)n, !virtual_op,
1978 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001979 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1980 }
1981 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001982 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 }
1984 }
1985
1986 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1987
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001988#ifdef FEAT_VIRTUALEDIT
1989setmarks:
1990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (oap->block_mode)
1992 {
1993 curbuf->b_op_end.lnum = oap->end.lnum;
1994 curbuf->b_op_end.col = oap->start.col;
1995 }
1996 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 curbuf->b_op_end = oap->start;
1998 curbuf->b_op_start = oap->start;
1999
2000 return OK;
2001}
2002
2003#ifdef FEAT_MBYTE
2004/*
2005 * Adjust end of operating area for ending on a multi-byte character.
2006 * Used for deletion.
2007 */
2008 static void
2009mb_adjust_opend(oap)
2010 oparg_T *oap;
2011{
2012 char_u *p;
2013
2014 if (oap->inclusive)
2015 {
2016 p = ml_get(oap->end.lnum);
2017 oap->end.col += mb_tail_off(p, p + oap->end.col);
2018 }
2019}
2020#endif
2021
2022#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2023/*
2024 * Replace a whole area with one character.
2025 */
2026 int
2027op_replace(oap, c)
2028 oparg_T *oap;
2029 int c;
2030{
2031 int n, numc;
2032#ifdef FEAT_MBYTE
2033 int num_chars;
2034#endif
2035 char_u *newp, *oldp;
2036 size_t oldlen;
2037 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002038 char_u *after_p = NULL;
2039 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2042 return OK; /* nothing to do */
2043
Bram Moolenaard9820532013-11-04 01:41:17 +01002044 if (had_ctrl_v_cr)
2045 c = (c == -1 ? '\r' : '\n');
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047#ifdef FEAT_MBYTE
2048 if (has_mbyte)
2049 mb_adjust_opend(oap);
2050#endif
2051
2052 if (u_save((linenr_T)(oap->start.lnum - 1),
2053 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2054 return FAIL;
2055
2056 /*
2057 * block mode replace
2058 */
2059 if (oap->block_mode)
2060 {
2061 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2062 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2063 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002064 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2066 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2067 continue; /* nothing to replace */
2068
2069 /* n == number of extra chars required
2070 * If we split a TAB, it may be replaced by several characters.
2071 * Thus the number of characters may increase!
2072 */
2073#ifdef FEAT_VIRTUALEDIT
2074 /* If the range starts in virtual space, count the initial
2075 * coladd offset as part of "startspaces" */
2076 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2077 {
2078 pos_T vpos;
2079
Bram Moolenaara1381de2009-11-03 15:44:21 +00002080 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 getvpos(&vpos, oap->start_vcol);
2082 bd.startspaces += vpos.coladd;
2083 n = bd.startspaces;
2084 }
2085 else
2086#endif
2087 /* allow for pre spaces */
2088 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2089
2090 /* allow for post spp */
2091 n += (bd.endspaces
2092#ifdef FEAT_VIRTUALEDIT
2093 && !bd.is_oneChar
2094#endif
2095 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2096 /* Figure out how many characters to replace. */
2097 numc = oap->end_vcol - oap->start_vcol + 1;
2098 if (bd.is_short && (!virtual_op || bd.is_MAX))
2099 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2100
2101#ifdef FEAT_MBYTE
2102 /* A double-wide character can be replaced only up to half the
2103 * times. */
2104 if ((*mb_char2cells)(c) > 1)
2105 {
2106 if ((numc & 1) && !bd.is_short)
2107 {
2108 ++bd.endspaces;
2109 ++n;
2110 }
2111 numc = numc / 2;
2112 }
2113
2114 /* Compute bytes needed, move character count to num_chars. */
2115 num_chars = numc;
2116 numc *= (*mb_char2len)(c);
2117#endif
2118 /* oldlen includes textlen, so don't double count */
2119 n += numc - bd.textlen;
2120
2121 oldp = ml_get_curline();
2122 oldlen = STRLEN(oldp);
2123 newp = alloc_check((unsigned)oldlen + 1 + n);
2124 if (newp == NULL)
2125 continue;
2126 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2127 /* copy up to deleted part */
2128 mch_memmove(newp, oldp, (size_t)bd.textcol);
2129 oldp += bd.textcol + bd.textlen;
2130 /* insert pre-spaces */
2131 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2132 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002133 /* -1/-2 is used for entering CR literally. */
2134 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002136#ifdef FEAT_MBYTE
2137 if (has_mbyte)
2138 {
2139 n = (int)STRLEN(newp);
2140 while (--num_chars >= 0)
2141 n += (*mb_char2bytes)(c, newp + n);
2142 }
2143 else
2144#endif
2145 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2146 if (!bd.is_short)
2147 {
2148 /* insert post-spaces */
2149 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2150 /* copy the part after the changed part */
2151 STRMOVE(newp + STRLEN(newp), oldp);
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002156 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002157 after_p = alloc_check(
2158 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002159 if (after_p != NULL)
2160 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
2162 /* replace the line */
2163 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002164 if (after_p != NULL)
2165 {
2166 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2167 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2168 oap->end.lnum++;
2169 vim_free(after_p);
2170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172 }
2173 else
2174 {
2175 /*
2176 * MCHAR and MLINE motion replace.
2177 */
2178 if (oap->motion_type == MLINE)
2179 {
2180 oap->start.col = 0;
2181 curwin->w_cursor.col = 0;
2182 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2183 if (oap->end.col)
2184 --oap->end.col;
2185 }
2186 else if (!oap->inclusive)
2187 dec(&(oap->end));
2188
2189 while (ltoreq(curwin->w_cursor, oap->end))
2190 {
2191 n = gchar_cursor();
2192 if (n != NUL)
2193 {
2194#ifdef FEAT_MBYTE
2195 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2196 {
2197 /* This is slow, but it handles replacing a single-byte
2198 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002199 if (curwin->w_cursor.lnum == oap->end.lnum)
2200 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 n = State;
2202 State = REPLACE;
2203 ins_char(c);
2204 State = n;
2205 /* Backup to the replaced character. */
2206 dec_cursor();
2207 }
2208 else
2209#endif
2210 {
2211#ifdef FEAT_VIRTUALEDIT
2212 if (n == TAB)
2213 {
2214 int end_vcol = 0;
2215
2216 if (curwin->w_cursor.lnum == oap->end.lnum)
2217 {
2218 /* oap->end has to be recalculated when
2219 * the tab breaks */
2220 end_vcol = getviscol2(oap->end.col,
2221 oap->end.coladd);
2222 }
2223 coladvance_force(getviscol());
2224 if (curwin->w_cursor.lnum == oap->end.lnum)
2225 getvpos(&oap->end, end_vcol);
2226 }
2227#endif
2228 pchar(curwin->w_cursor, c);
2229 }
2230 }
2231#ifdef FEAT_VIRTUALEDIT
2232 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2233 {
2234 int virtcols = oap->end.coladd;
2235
2236 if (curwin->w_cursor.lnum == oap->start.lnum
2237 && oap->start.col == oap->end.col && oap->start.coladd)
2238 virtcols -= oap->start.coladd;
2239
2240 /* oap->end has been trimmed so it's effectively inclusive;
2241 * as a result an extra +1 must be counted so we don't
2242 * trample the NUL byte. */
2243 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2244 curwin->w_cursor.col -= (virtcols + 1);
2245 for (; virtcols >= 0; virtcols--)
2246 {
2247 pchar(curwin->w_cursor, c);
2248 if (inc(&curwin->w_cursor) == -1)
2249 break;
2250 }
2251 }
2252#endif
2253
2254 /* Advance to next character, stop at the end of the file. */
2255 if (inc_cursor() == -1)
2256 break;
2257 }
2258 }
2259
2260 curwin->w_cursor = oap->start;
2261 check_cursor();
2262 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2263
2264 /* Set "'[" and "']" marks. */
2265 curbuf->b_op_start = oap->start;
2266 curbuf->b_op_end = oap->end;
2267
2268 return OK;
2269}
2270#endif
2271
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002272static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274/*
2275 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2276 */
2277 void
2278op_tilde(oap)
2279 oparg_T *oap;
2280{
2281 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002283 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 if (u_save((linenr_T)(oap->start.lnum - 1),
2286 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2287 return;
2288
2289 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (oap->block_mode) /* Visual block mode */
2291 {
2292 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2293 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002294 int one_change;
2295
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 block_prep(oap, &bd, pos.lnum, FALSE);
2297 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002298 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2299 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002300
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002301#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002302 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2305
Bram Moolenaar009b2592004-10-24 19:18:58 +00002306 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2307 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002309 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313 if (did_change)
2314 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2315 }
2316 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318 if (oap->motion_type == MLINE)
2319 {
2320 oap->start.col = 0;
2321 pos.col = 0;
2322 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2323 if (oap->end.col)
2324 --oap->end.col;
2325 }
2326 else if (!oap->inclusive)
2327 dec(&(oap->end));
2328
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002329 if (pos.lnum == oap->end.lnum)
2330 did_change = swapchars(oap->op_type, &pos,
2331 oap->end.col - pos.col + 1);
2332 else
2333 for (;;)
2334 {
2335 did_change |= swapchars(oap->op_type, &pos,
2336 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2337 (int)STRLEN(ml_get_pos(&pos)));
2338 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2339 break;
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (did_change)
2342 {
2343 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2344 0L);
2345#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002346 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 char_u *ptr;
2349 int count;
2350
2351 pos = oap->start;
2352 while (pos.lnum < oap->end.lnum)
2353 {
2354 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002355 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002356 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002358 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 pos.col = 0;
2360 pos.lnum++;
2361 }
2362 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2363 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002364 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002366 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368#endif
2369 }
2370 }
2371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (!did_change && oap->is_VIsual)
2373 /* No change: need to remove the Visual selection */
2374 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 /*
2377 * Set '[ and '] marks.
2378 */
2379 curbuf->b_op_start = oap->start;
2380 curbuf->b_op_end = oap->end;
2381
2382 if (oap->line_count > p_report)
2383 {
2384 if (oap->line_count == 1)
2385 MSG(_("1 line changed"));
2386 else
2387 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2388 }
2389}
2390
2391/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002392 * Invoke swapchar() on "length" bytes at position "pos".
2393 * "pos" is advanced to just after the changed characters.
2394 * "length" is rounded up to include the whole last multi-byte character.
2395 * Also works correctly when the number of bytes changes.
2396 * Returns TRUE if some character was changed.
2397 */
2398 static int
2399swapchars(op_type, pos, length)
2400 int op_type;
2401 pos_T *pos;
2402 int length;
2403{
2404 int todo;
2405 int did_change = 0;
2406
2407 for (todo = length; todo > 0; --todo)
2408 {
2409# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002410 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002411 {
2412 int len = (*mb_ptr2len)(ml_get_pos(pos));
2413
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002414 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002415 if (len > 0)
2416 todo -= len - 1;
2417 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002418# endif
2419 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002420 if (inc(pos) == -1) /* at end of file */
2421 break;
2422 }
2423 return did_change;
2424}
2425
2426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 * If op_type == OP_UPPER: make uppercase,
2428 * if op_type == OP_LOWER: make lowercase,
2429 * if op_type == OP_ROT13: do rot13 encoding,
2430 * else swap case of character at 'pos'
2431 * returns TRUE when something actually changed.
2432 */
2433 int
2434swapchar(op_type, pos)
2435 int op_type;
2436 pos_T *pos;
2437{
2438 int c;
2439 int nc;
2440
2441 c = gchar_pos(pos);
2442
2443 /* Only do rot13 encoding for ASCII characters. */
2444 if (c >= 0x80 && op_type == OP_ROT13)
2445 return FALSE;
2446
2447#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002448 if (op_type == OP_UPPER && c == 0xdf
2449 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002450 {
2451 pos_T sp = curwin->w_cursor;
2452
2453 /* Special handling of German sharp s: change to "SS". */
2454 curwin->w_cursor = *pos;
2455 del_char(FALSE);
2456 ins_char('S');
2457 ins_char('S');
2458 curwin->w_cursor = sp;
2459 inc(pos);
2460 }
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2463 return FALSE;
2464#endif
2465 nc = c;
2466 if (MB_ISLOWER(c))
2467 {
2468 if (op_type == OP_ROT13)
2469 nc = ROT13(c, 'a');
2470 else if (op_type != OP_LOWER)
2471 nc = MB_TOUPPER(c);
2472 }
2473 else if (MB_ISUPPER(c))
2474 {
2475 if (op_type == OP_ROT13)
2476 nc = ROT13(c, 'A');
2477 else if (op_type != OP_UPPER)
2478 nc = MB_TOLOWER(c);
2479 }
2480 if (nc != c)
2481 {
2482#ifdef FEAT_MBYTE
2483 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2484 {
2485 pos_T sp = curwin->w_cursor;
2486
2487 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002488 /* don't use del_char(), it also removes composing chars */
2489 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 ins_char(nc);
2491 curwin->w_cursor = sp;
2492 }
2493 else
2494#endif
2495 pchar(*pos, nc);
2496 return TRUE;
2497 }
2498 return FALSE;
2499}
2500
2501#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2502/*
2503 * op_insert - Insert and append operators for Visual mode.
2504 */
2505 void
2506op_insert(oap, count1)
2507 oparg_T *oap;
2508 long count1;
2509{
2510 long ins_len, pre_textlen = 0;
2511 char_u *firstline, *ins_text;
2512 struct block_def bd;
2513 int i;
2514
2515 /* edit() changes this - record it for OP_APPEND */
2516 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2517
2518 /* vis block is still marked. Get rid of it now. */
2519 curwin->w_cursor.lnum = oap->start.lnum;
2520 update_screen(INVERTED);
2521
2522 if (oap->block_mode)
2523 {
2524#ifdef FEAT_VIRTUALEDIT
2525 /* When 'virtualedit' is used, need to insert the extra spaces before
2526 * doing block_prep(). When only "block" is used, virtual edit is
2527 * already disabled, but still need it when calling
2528 * coladvance_force(). */
2529 if (curwin->w_cursor.coladd > 0)
2530 {
2531 int old_ve_flags = ve_flags;
2532
2533 ve_flags = VE_ALL;
2534 if (u_save_cursor() == FAIL)
2535 return;
2536 coladvance_force(oap->op_type == OP_APPEND
2537 ? oap->end_vcol + 1 : getviscol());
2538 if (oap->op_type == OP_APPEND)
2539 --curwin->w_cursor.col;
2540 ve_flags = old_ve_flags;
2541 }
2542#endif
2543 /* Get the info about the block before entering the text */
2544 block_prep(oap, &bd, oap->start.lnum, TRUE);
2545 firstline = ml_get(oap->start.lnum) + bd.textcol;
2546 if (oap->op_type == OP_APPEND)
2547 firstline += bd.textlen;
2548 pre_textlen = (long)STRLEN(firstline);
2549 }
2550
2551 if (oap->op_type == OP_APPEND)
2552 {
2553 if (oap->block_mode
2554#ifdef FEAT_VIRTUALEDIT
2555 && curwin->w_cursor.coladd == 0
2556#endif
2557 )
2558 {
2559 /* Move the cursor to the character right of the block. */
2560 curwin->w_set_curswant = TRUE;
2561 while (*ml_get_cursor() != NUL
2562 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2563 ++curwin->w_cursor.col;
2564 if (bd.is_short && !bd.is_MAX)
2565 {
2566 /* First line was too short, make it longer and adjust the
2567 * values in "bd". */
2568 if (u_save_cursor() == FAIL)
2569 return;
2570 for (i = 0; i < bd.endspaces; ++i)
2571 ins_char(' ');
2572 bd.textlen += bd.endspaces;
2573 }
2574 }
2575 else
2576 {
2577 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002578 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 /* Works just like an 'i'nsert on the next character. */
2581 if (!lineempty(curwin->w_cursor.lnum)
2582 && oap->start_vcol != oap->end_vcol)
2583 inc_cursor();
2584 }
2585 }
2586
2587 edit(NUL, FALSE, (linenr_T)count1);
2588
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002589 /* If user has moved off this line, we don't know what to do, so do
2590 * nothing.
2591 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2592 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 return;
2594
2595 if (oap->block_mode)
2596 {
2597 struct block_def bd2;
2598
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002599 /* The user may have moved the cursor before inserting something, try
2600 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002601 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002602 {
2603 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002604 && oap->start.col
2605#ifdef FEAT_VIRTUALEDIT
2606 + oap->start.coladd
2607#endif
2608 != curbuf->b_op_start_orig.col
2609#ifdef FEAT_VIRTUALEDIT
2610 + curbuf->b_op_start_orig.coladd
2611#endif
2612 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002613 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002614 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002615 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2616 - oap->start_vcol;
2617 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2618 }
2619 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002620 && oap->end.col
2621#ifdef FEAT_VIRTUALEDIT
2622 + oap->end.coladd
2623#endif
2624 >= curbuf->b_op_start_orig.col
2625#ifdef FEAT_VIRTUALEDIT
2626 + curbuf->b_op_start_orig.coladd
2627#endif
2628 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002629 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002630 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002631 /* reset pre_textlen to the value of OP_INSERT */
2632 pre_textlen += bd.textlen;
2633 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2634 - oap->start_vcol;
2635 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2636 oap->op_type = OP_INSERT;
2637 }
2638 }
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 /*
2641 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002642 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 * Don't do this when "$" used, end-of-line will have changed.
2644 */
2645 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2646 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2647 {
2648 if (oap->op_type == OP_APPEND)
2649 {
2650 pre_textlen += bd2.textlen - bd.textlen;
2651 if (bd2.endspaces)
2652 --bd2.textlen;
2653 }
2654 bd.textcol = bd2.textcol;
2655 bd.textlen = bd2.textlen;
2656 }
2657
2658 /*
2659 * Subsequent calls to ml_get() flush the firstline data - take a
2660 * copy of the required string.
2661 */
2662 firstline = ml_get(oap->start.lnum) + bd.textcol;
2663 if (oap->op_type == OP_APPEND)
2664 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002665 if (pre_textlen >= 0
2666 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 ins_text = vim_strnsave(firstline, (int)ins_len);
2669 if (ins_text != NULL)
2670 {
2671 /* block handled here */
2672 if (u_save(oap->start.lnum,
2673 (linenr_T)(oap->end.lnum + 1)) == OK)
2674 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2675 &bd);
2676
2677 curwin->w_cursor.col = oap->start.col;
2678 check_cursor();
2679 vim_free(ins_text);
2680 }
2681 }
2682 }
2683}
2684#endif
2685
2686/*
2687 * op_change - handle a change operation
2688 *
2689 * return TRUE if edit() returns because of a CTRL-O command
2690 */
2691 int
2692op_change(oap)
2693 oparg_T *oap;
2694{
2695 colnr_T l;
2696 int retval;
2697#ifdef FEAT_VISUALEXTRA
2698 long offset;
2699 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002700 long ins_len;
2701 long pre_textlen = 0;
2702 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 char_u *firstline;
2704 char_u *ins_text, *newp, *oldp;
2705 struct block_def bd;
2706#endif
2707
2708 l = oap->start.col;
2709 if (oap->motion_type == MLINE)
2710 {
2711 l = 0;
2712#ifdef FEAT_SMARTINDENT
2713 if (!p_paste && curbuf->b_p_si
2714# ifdef FEAT_CINDENT
2715 && !curbuf->b_p_cin
2716# endif
2717 )
2718 can_si = TRUE; /* It's like opening a new line, do si */
2719#endif
2720 }
2721
2722 /* First delete the text in the region. In an empty buffer only need to
2723 * save for undo */
2724 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2725 {
2726 if (u_save_cursor() == FAIL)
2727 return FALSE;
2728 }
2729 else if (op_delete(oap) == FAIL)
2730 return FALSE;
2731
2732 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2733 && !virtual_op)
2734 inc_cursor();
2735
2736#ifdef FEAT_VISUALEXTRA
2737 /* check for still on same line (<CR> in inserted text meaningless) */
2738 /* skip blank lines too */
2739 if (oap->block_mode)
2740 {
2741# ifdef FEAT_VIRTUALEDIT
2742 /* Add spaces before getting the current line length. */
2743 if (virtual_op && (curwin->w_cursor.coladd > 0
2744 || gchar_cursor() == NUL))
2745 coladvance_force(getviscol());
2746# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002747 firstline = ml_get(oap->start.lnum);
2748 pre_textlen = (long)STRLEN(firstline);
2749 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 bd.textcol = curwin->w_cursor.col;
2751 }
2752#endif
2753
2754#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2755 if (oap->motion_type == MLINE)
2756 fix_indent();
2757#endif
2758
2759 retval = edit(NUL, FALSE, (linenr_T)1);
2760
2761#ifdef FEAT_VISUALEXTRA
2762 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002763 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002765 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002767 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002769 /* Auto-indenting may have changed the indent. If the cursor was past
2770 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002772 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002774 long new_indent = (long)(skipwhite(firstline) - firstline);
2775
2776 pre_textlen += new_indent - pre_indent;
2777 bd.textcol += new_indent - pre_indent;
2778 }
2779
2780 ins_len = (long)STRLEN(firstline) - pre_textlen;
2781 if (ins_len > 0)
2782 {
2783 /* Subsequent calls to ml_get() flush the firstline data - take a
2784 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2786 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002787 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2789 linenr++)
2790 {
2791 block_prep(oap, &bd, linenr, TRUE);
2792 if (!bd.is_short || virtual_op)
2793 {
2794# ifdef FEAT_VIRTUALEDIT
2795 pos_T vpos;
2796
2797 /* If the block starts in virtual space, count the
2798 * initial coladd offset as part of "startspaces" */
2799 if (bd.is_short)
2800 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002801 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
2805 vpos.coladd = 0;
2806# endif
2807 oldp = ml_get(linenr);
2808 newp = alloc_check((unsigned)(STRLEN(oldp)
2809# ifdef FEAT_VIRTUALEDIT
2810 + vpos.coladd
2811# endif
2812 + ins_len + 1));
2813 if (newp == NULL)
2814 continue;
2815 /* copy up to block start */
2816 mch_memmove(newp, oldp, (size_t)bd.textcol);
2817 offset = bd.textcol;
2818# ifdef FEAT_VIRTUALEDIT
2819 copy_spaces(newp + offset, (size_t)vpos.coladd);
2820 offset += vpos.coladd;
2821# endif
2822 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2823 offset += ins_len;
2824 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002825 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 ml_replace(linenr, newp, FALSE);
2827 }
2828 }
2829 check_cursor();
2830
2831 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2832 }
2833 vim_free(ins_text);
2834 }
2835 }
2836#endif
2837
2838 return retval;
2839}
2840
2841/*
2842 * set all the yank registers to empty (called from main())
2843 */
2844 void
2845init_yank()
2846{
2847 int i;
2848
2849 for (i = 0; i < NUM_REGISTERS; ++i)
2850 y_regs[i].y_array = NULL;
2851}
2852
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002853#if defined(EXITFREE) || defined(PROTO)
2854 void
2855clear_registers()
2856{
2857 int i;
2858
2859 for (i = 0; i < NUM_REGISTERS; ++i)
2860 {
2861 y_current = &y_regs[i];
2862 if (y_current->y_array != NULL)
2863 free_yank_all();
2864 }
2865}
2866#endif
2867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868/*
2869 * Free "n" lines from the current yank register.
2870 * Called for normal freeing and in case of error.
2871 */
2872 static void
2873free_yank(n)
2874 long n;
2875{
2876 if (y_current->y_array != NULL)
2877 {
2878 long i;
2879
2880 for (i = n; --i >= 0; )
2881 {
2882#ifdef AMIGA /* only for very slow machines */
2883 if ((i & 1023) == 1023) /* this may take a while */
2884 {
2885 /*
2886 * This message should never cause a hit-return message.
2887 * Overwrite this message with any next message.
2888 */
2889 ++no_wait_return;
2890 smsg((char_u *)_("freeing %ld lines"), i + 1);
2891 --no_wait_return;
2892 msg_didout = FALSE;
2893 msg_col = 0;
2894 }
2895#endif
2896 vim_free(y_current->y_array[i]);
2897 }
2898 vim_free(y_current->y_array);
2899 y_current->y_array = NULL;
2900#ifdef AMIGA
2901 if (n >= 1000)
2902 MSG("");
2903#endif
2904 }
2905}
2906
2907 static void
2908free_yank_all()
2909{
2910 free_yank(y_current->y_size);
2911}
2912
2913/*
2914 * Yank the text between "oap->start" and "oap->end" into a yank register.
2915 * If we are to append (uppercase register), we first yank into a new yank
2916 * register and then concatenate the old and the new one (so we keep the old
2917 * one in case of out-of-memory).
2918 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002919 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
2921 int
2922op_yank(oap, deleting, mess)
2923 oparg_T *oap;
2924 int deleting;
2925 int mess;
2926{
2927 long y_idx; /* index in y_array[] */
2928 struct yankreg *curr; /* copy of y_current */
2929 struct yankreg newreg; /* new yank register when appending */
2930 char_u **new_ptr;
2931 linenr_T lnum; /* current line number */
2932 long j;
2933 int yanktype = oap->motion_type;
2934 long yanklines = oap->line_count;
2935 linenr_T yankendlnum = oap->end.lnum;
2936 char_u *p;
2937 char_u *pnew;
2938 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002939#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002940 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 /* check for read-only register */
2944 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2945 {
2946 beep_flush();
2947 return FAIL;
2948 }
2949 if (oap->regname == '_') /* black hole: nothing to do */
2950 return OK;
2951
2952#ifdef FEAT_CLIPBOARD
2953 if (!clip_star.available && oap->regname == '*')
2954 oap->regname = 0;
2955 else if (!clip_plus.available && oap->regname == '+')
2956 oap->regname = 0;
2957#endif
2958
2959 if (!deleting) /* op_delete() already set y_current */
2960 get_yank_register(oap->regname, TRUE);
2961
2962 curr = y_current;
2963 /* append to existing contents */
2964 if (y_append && y_current->y_array != NULL)
2965 y_current = &newreg;
2966 else
2967 free_yank_all(); /* free previously yanked lines */
2968
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002969 /*
2970 * If the cursor was in column 1 before and after the movement, and the
2971 * operator is not inclusive, the yank is always linewise.
2972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if ( oap->motion_type == MCHAR
2974 && oap->start.col == 0
2975 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002977 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 && oap->end.col == 0
2979 && yanklines > 1)
2980 {
2981 yanktype = MLINE;
2982 --yankendlnum;
2983 --yanklines;
2984 }
2985
2986 y_current->y_size = yanklines;
2987 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2990 yanklines), TRUE);
2991
2992 if (y_current->y_array == NULL)
2993 {
2994 y_current = curr;
2995 return FAIL;
2996 }
2997
2998 y_idx = 0;
2999 lnum = oap->start.lnum;
3000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (oap->block_mode)
3002 {
3003 /* Visual block mode */
3004 y_current->y_type = MBLOCK; /* set the yank register type */
3005 y_current->y_width = oap->end_vcol - oap->start_vcol;
3006
3007 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3008 y_current->y_width--;
3009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3012 {
3013 switch (y_current->y_type)
3014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 case MBLOCK:
3016 block_prep(oap, &bd, lnum, FALSE);
3017 if (yank_copy_line(&bd, y_idx) == FAIL)
3018 goto fail;
3019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 case MLINE:
3022 if ((y_current->y_array[y_idx] =
3023 vim_strsave(ml_get(lnum))) == NULL)
3024 goto fail;
3025 break;
3026
3027 case MCHAR:
3028 {
3029 colnr_T startcol = 0, endcol = MAXCOL;
3030#ifdef FEAT_VIRTUALEDIT
3031 int is_oneChar = FALSE;
3032 colnr_T cs, ce;
3033#endif
3034 p = ml_get(lnum);
3035 bd.startspaces = 0;
3036 bd.endspaces = 0;
3037
3038 if (lnum == oap->start.lnum)
3039 {
3040 startcol = oap->start.col;
3041#ifdef FEAT_VIRTUALEDIT
3042 if (virtual_op)
3043 {
3044 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3045 if (ce != cs && oap->start.coladd > 0)
3046 {
3047 /* Part of a tab selected -- but don't
3048 * double-count it. */
3049 bd.startspaces = (ce - cs + 1)
3050 - oap->start.coladd;
3051 startcol++;
3052 }
3053 }
3054#endif
3055 }
3056
3057 if (lnum == oap->end.lnum)
3058 {
3059 endcol = oap->end.col;
3060#ifdef FEAT_VIRTUALEDIT
3061 if (virtual_op)
3062 {
3063 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3064 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3065# ifdef FEAT_MBYTE
3066 /* Don't add space for double-wide
3067 * char; endcol will be on last byte
3068 * of multi-byte char. */
3069 && (*mb_head_off)(p, p + endcol) == 0
3070# endif
3071 ))
3072 {
3073 if (oap->start.lnum == oap->end.lnum
3074 && oap->start.col == oap->end.col)
3075 {
3076 /* Special case: inside a single char */
3077 is_oneChar = TRUE;
3078 bd.startspaces = oap->end.coladd
3079 - oap->start.coladd + oap->inclusive;
3080 endcol = startcol;
3081 }
3082 else
3083 {
3084 bd.endspaces = oap->end.coladd
3085 + oap->inclusive;
3086 endcol -= oap->inclusive;
3087 }
3088 }
3089 }
3090#endif
3091 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003092 if (endcol == MAXCOL)
3093 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (startcol > endcol
3095#ifdef FEAT_VIRTUALEDIT
3096 || is_oneChar
3097#endif
3098 )
3099 bd.textlen = 0;
3100 else
3101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 bd.textlen = endcol - startcol + oap->inclusive;
3103 }
3104 bd.textstart = p + startcol;
3105 if (yank_copy_line(&bd, y_idx) == FAIL)
3106 goto fail;
3107 break;
3108 }
3109 /* NOTREACHED */
3110 }
3111 }
3112
3113 if (curr != y_current) /* append the new block to the old block */
3114 {
3115 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3116 (curr->y_size + y_current->y_size)), TRUE);
3117 if (new_ptr == NULL)
3118 goto fail;
3119 for (j = 0; j < curr->y_size; ++j)
3120 new_ptr[j] = curr->y_array[j];
3121 vim_free(curr->y_array);
3122 curr->y_array = new_ptr;
3123
3124 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3125 curr->y_type = MLINE;
3126
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003127 /* Concatenate the last line of the old block with the first line of
3128 * the new block, unless being Vi compatible. */
3129 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
3131 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3132 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3133 if (pnew == NULL)
3134 {
3135 y_idx = y_current->y_size - 1;
3136 goto fail;
3137 }
3138 STRCPY(pnew, curr->y_array[--j]);
3139 STRCAT(pnew, y_current->y_array[0]);
3140 vim_free(curr->y_array[j]);
3141 vim_free(y_current->y_array[0]);
3142 curr->y_array[j++] = pnew;
3143 y_idx = 1;
3144 }
3145 else
3146 y_idx = 0;
3147 while (y_idx < y_current->y_size)
3148 curr->y_array[j++] = y_current->y_array[y_idx++];
3149 curr->y_size = j;
3150 vim_free(y_current->y_array);
3151 y_current = curr;
3152 }
3153 if (mess) /* Display message about yank? */
3154 {
3155 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 && yanklines == 1)
3158 yanklines = 0;
3159 /* Some versions of Vi use ">=" here, some don't... */
3160 if (yanklines > p_report)
3161 {
3162 /* redisplay now, so message is not deleted */
3163 update_topline_redraw();
3164 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003165 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003166 if (oap->block_mode)
3167 MSG(_("block of 1 line yanked"));
3168 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003169 MSG(_("1 line yanked"));
3170 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003171 else if (oap->block_mode)
3172 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 else
3174 smsg((char_u *)_("%ld lines yanked"), yanklines);
3175 }
3176 }
3177
3178 /*
3179 * Set "'[" and "']" marks.
3180 */
3181 curbuf->b_op_start = oap->start;
3182 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003183 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003184 {
3185 curbuf->b_op_start.col = 0;
3186 curbuf->b_op_end.col = MAXCOL;
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189#ifdef FEAT_CLIPBOARD
3190 /*
3191 * If we were yanking to the '*' register, send result to clipboard.
3192 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3193 * to the '*' register.
3194 */
3195 if (clip_star.available
3196 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003197 || (!deleting && oap->regname == 0
3198 && (clip_unnamed & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 if (curr != &(y_regs[STAR_REGISTER]))
3201 /* Copy the text from register 0 to the clipboard register. */
3202 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3203
3204 clip_own_selection(&clip_star);
3205 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003206# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003207 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210
3211# ifdef FEAT_X11
3212 /*
3213 * If we were yanking to the '+' register, send result to selection.
3214 * Also copy to the '*' register, in case auto-select is off.
3215 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003216 if (clip_plus.available
3217 && (curr == &(y_regs[PLUS_REGISTER])
3218 || (!deleting && oap->regname == 0
3219 && (clip_unnamed & CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003221 if (curr != &(y_regs[PLUS_REGISTER]))
3222 /* Copy the text from register 0 to the clipboard register. */
3223 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 clip_own_selection(&clip_plus);
3226 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003227 if (!clip_isautosel_star() && !did_star
3228 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
3230 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3231 clip_own_selection(&clip_star);
3232 clip_gen_set_selection(&clip_star);
3233 }
3234 }
3235# endif
3236#endif
3237
3238 return OK;
3239
3240fail: /* free the allocated lines */
3241 free_yank(y_idx + 1);
3242 y_current = curr;
3243 return FAIL;
3244}
3245
3246 static int
3247yank_copy_line(bd, y_idx)
3248 struct block_def *bd;
3249 long y_idx;
3250{
3251 char_u *pnew;
3252
3253 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3254 == NULL)
3255 return FAIL;
3256 y_current->y_array[y_idx] = pnew;
3257 copy_spaces(pnew, (size_t)bd->startspaces);
3258 pnew += bd->startspaces;
3259 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3260 pnew += bd->textlen;
3261 copy_spaces(pnew, (size_t)bd->endspaces);
3262 pnew += bd->endspaces;
3263 *pnew = NUL;
3264 return OK;
3265}
3266
3267#ifdef FEAT_CLIPBOARD
3268/*
3269 * Make a copy of the y_current register to register "reg".
3270 */
3271 static void
3272copy_yank_reg(reg)
3273 struct yankreg *reg;
3274{
3275 struct yankreg *curr = y_current;
3276 long j;
3277
3278 y_current = reg;
3279 free_yank_all();
3280 *y_current = *curr;
3281 y_current->y_array = (char_u **)lalloc_clear(
3282 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3283 if (y_current->y_array == NULL)
3284 y_current->y_size = 0;
3285 else
3286 for (j = 0; j < y_current->y_size; ++j)
3287 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3288 {
3289 free_yank(j);
3290 y_current->y_size = 0;
3291 break;
3292 }
3293 y_current = curr;
3294}
3295#endif
3296
3297/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003298 * Put contents of register "regname" into the text.
3299 * Caller must check "regname" to be valid!
3300 * "flags": PUT_FIXINDENT make indent look nice
3301 * PUT_CURSEND leave cursor after end of new text
3302 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 */
3304 void
3305do_put(regname, dir, count, flags)
3306 int regname;
3307 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3308 long count;
3309 int flags;
3310{
3311 char_u *ptr;
3312 char_u *newp, *oldp;
3313 int yanklen;
3314 int totlen = 0; /* init for gcc */
3315 linenr_T lnum;
3316 colnr_T col;
3317 long i; /* index in y_array[] */
3318 int y_type;
3319 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 int oldlen;
3321 long y_width = 0;
3322 colnr_T vcol;
3323 int delcount;
3324 int incr = 0;
3325 long j;
3326 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 char_u **y_array = NULL;
3328 long nr_lines = 0;
3329 pos_T new_cursor;
3330 int indent;
3331 int orig_indent = 0; /* init for gcc */
3332 int indent_diff = 0; /* init for gcc */
3333 int first_indent = TRUE;
3334 int lendiff = 0;
3335 pos_T old_pos;
3336 char_u *insert_string = NULL;
3337 int allocated = FALSE;
3338 long cnt;
3339
3340#ifdef FEAT_CLIPBOARD
3341 /* Adjust register name for "unnamed" in 'clipboard'. */
3342 adjust_clip_reg(&regname);
3343 (void)may_get_selection(regname);
3344#endif
3345
3346 if (flags & PUT_FIXINDENT)
3347 orig_indent = get_indent();
3348
3349 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3350 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3351
3352 /*
3353 * Using inserted text works differently, because the register includes
3354 * special characters (newlines, etc.).
3355 */
3356 if (regname == '.')
3357 {
3358 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3359 (count == -1 ? 'O' : 'i')), count, FALSE);
3360 /* Putting the text is done later, so can't really move the cursor to
3361 * the next character. Use "l" to simulate it. */
3362 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3363 stuffcharReadbuff('l');
3364 return;
3365 }
3366
3367 /*
3368 * For special registers '%' (file name), '#' (alternate file name) and
3369 * ':' (last command line), etc. we have to create a fake yank register.
3370 */
3371 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3372 {
3373 if (insert_string == NULL)
3374 return;
3375 }
3376
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003377#ifdef FEAT_AUTOCMD
3378 /* Autocommands may be executed when saving lines for undo, which may make
3379 * y_array invalid. Start undo now to avoid that. */
3380 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3381#endif
3382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 if (insert_string != NULL)
3384 {
3385 y_type = MCHAR;
3386#ifdef FEAT_EVAL
3387 if (regname == '=')
3388 {
3389 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003390 * characters.
3391 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 for (;;)
3393 {
3394 y_size = 0;
3395 ptr = insert_string;
3396 while (ptr != NULL)
3397 {
3398 if (y_array != NULL)
3399 y_array[y_size] = ptr;
3400 ++y_size;
3401 ptr = vim_strchr(ptr, '\n');
3402 if (ptr != NULL)
3403 {
3404 if (y_array != NULL)
3405 *ptr = NUL;
3406 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003407 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (*ptr == NUL)
3409 {
3410 y_type = MLINE;
3411 break;
3412 }
3413 }
3414 }
3415 if (y_array != NULL)
3416 break;
3417 y_array = (char_u **)alloc((unsigned)
3418 (y_size * sizeof(char_u *)));
3419 if (y_array == NULL)
3420 goto end;
3421 }
3422 }
3423 else
3424#endif
3425 {
3426 y_size = 1; /* use fake one-line yank register */
3427 y_array = &insert_string;
3428 }
3429 }
3430 else
3431 {
3432 get_yank_register(regname, FALSE);
3433
3434 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 y_size = y_current->y_size;
3437 y_array = y_current->y_array;
3438 }
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (y_type == MLINE)
3441 {
3442 if (flags & PUT_LINE_SPLIT)
3443 {
3444 /* "p" or "P" in Visual mode: split the lines to put the text in
3445 * between. */
3446 if (u_save_cursor() == FAIL)
3447 goto end;
3448 ptr = vim_strsave(ml_get_cursor());
3449 if (ptr == NULL)
3450 goto end;
3451 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3452 vim_free(ptr);
3453
3454 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3455 if (ptr == NULL)
3456 goto end;
3457 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3458 ++nr_lines;
3459 dir = FORWARD;
3460 }
3461 if (flags & PUT_LINE_FORWARD)
3462 {
3463 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003464 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 dir = FORWARD;
3466 }
3467 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3468 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3472 y_type = MLINE;
3473
3474 if (y_size == 0 || y_array == NULL)
3475 {
3476 EMSG2(_("E353: Nothing in register %s"),
3477 regname == 0 ? (char_u *)"\"" : transchar(regname));
3478 goto end;
3479 }
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (y_type == MBLOCK)
3482 {
3483 lnum = curwin->w_cursor.lnum + y_size + 1;
3484 if (lnum > curbuf->b_ml.ml_line_count)
3485 lnum = curbuf->b_ml.ml_line_count + 1;
3486 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3487 goto end;
3488 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003489 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
3491 lnum = curwin->w_cursor.lnum;
3492#ifdef FEAT_FOLDING
3493 /* Correct line number for closed fold. Don't move the cursor yet,
3494 * u_save() uses it. */
3495 if (dir == BACKWARD)
3496 (void)hasFolding(lnum, &lnum, NULL);
3497 else
3498 (void)hasFolding(lnum, NULL, &lnum);
3499#endif
3500 if (dir == FORWARD)
3501 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003502 /* In an empty buffer the empty line is going to be replaced, include
3503 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003504 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 goto end;
3506#ifdef FEAT_FOLDING
3507 if (dir == FORWARD)
3508 curwin->w_cursor.lnum = lnum - 1;
3509 else
3510 curwin->w_cursor.lnum = lnum;
3511 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3512#endif
3513 }
3514 else if (u_save_cursor() == FAIL)
3515 goto end;
3516
3517 yanklen = (int)STRLEN(y_array[0]);
3518
3519#ifdef FEAT_VIRTUALEDIT
3520 if (ve_flags == VE_ALL && y_type == MCHAR)
3521 {
3522 if (gchar_cursor() == TAB)
3523 {
3524 /* Don't need to insert spaces when "p" on the last position of a
3525 * tab or "P" on the first position. */
3526 if (dir == FORWARD
3527 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3528 : curwin->w_cursor.coladd > 0)
3529 coladvance_force(getviscol());
3530 else
3531 curwin->w_cursor.coladd = 0;
3532 }
3533 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3534 coladvance_force(getviscol() + (dir == FORWARD));
3535 }
3536#endif
3537
3538 lnum = curwin->w_cursor.lnum;
3539 col = curwin->w_cursor.col;
3540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /*
3542 * Block mode
3543 */
3544 if (y_type == MBLOCK)
3545 {
3546 char c = gchar_cursor();
3547 colnr_T endcol2 = 0;
3548
3549 if (dir == FORWARD && c != NUL)
3550 {
3551#ifdef FEAT_VIRTUALEDIT
3552 if (ve_flags == VE_ALL)
3553 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3554 else
3555#endif
3556 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3557
3558#ifdef FEAT_MBYTE
3559 if (has_mbyte)
3560 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003561 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
3563#endif
3564#ifdef FEAT_VIRTUALEDIT
3565 if (c != TAB || ve_flags != VE_ALL)
3566#endif
3567 ++curwin->w_cursor.col;
3568 ++col;
3569 }
3570 else
3571 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3572
3573#ifdef FEAT_VIRTUALEDIT
3574 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003575 if (ve_flags == VE_ALL
3576 && (curwin->w_cursor.coladd > 0
3577 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 if (dir == FORWARD && c == NUL)
3580 ++col;
3581 if (dir != FORWARD && c != NUL)
3582 ++curwin->w_cursor.col;
3583 if (c == TAB)
3584 {
3585 if (dir == BACKWARD && curwin->w_cursor.col)
3586 curwin->w_cursor.col--;
3587 if (dir == FORWARD && col - 1 == endcol2)
3588 curwin->w_cursor.col++;
3589 }
3590 }
3591 curwin->w_cursor.coladd = 0;
3592#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003593 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 for (i = 0; i < y_size; ++i)
3595 {
3596 int spaces;
3597 char shortline;
3598
3599 bd.startspaces = 0;
3600 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 vcol = 0;
3602 delcount = 0;
3603
3604 /* add a new line */
3605 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3606 {
3607 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3608 (colnr_T)1, FALSE) == FAIL)
3609 break;
3610 ++nr_lines;
3611 }
3612 /* get the old line and advance to the position to insert at */
3613 oldp = ml_get_curline();
3614 oldlen = (int)STRLEN(oldp);
3615 for (ptr = oldp; vcol < col && *ptr; )
3616 {
3617 /* Count a tab for what it's worth (if list mode not on) */
3618 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3619 vcol += incr;
3620 }
3621 bd.textcol = (colnr_T)(ptr - oldp);
3622
3623 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3624
3625 if (vcol < col) /* line too short, padd with spaces */
3626 bd.startspaces = col - vcol;
3627 else if (vcol > col)
3628 {
3629 bd.endspaces = vcol - col;
3630 bd.startspaces = incr - bd.endspaces;
3631 --bd.textcol;
3632 delcount = 1;
3633#ifdef FEAT_MBYTE
3634 if (has_mbyte)
3635 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3636#endif
3637 if (oldp[bd.textcol] != TAB)
3638 {
3639 /* Only a Tab can be split into spaces. Other
3640 * characters will have to be moved to after the
3641 * block, causing misalignment. */
3642 delcount = 0;
3643 bd.endspaces = 0;
3644 }
3645 }
3646
3647 yanklen = (int)STRLEN(y_array[i]);
3648
3649 /* calculate number of spaces required to fill right side of block*/
3650 spaces = y_width + 1;
3651 for (j = 0; j < yanklen; j++)
3652 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3653 if (spaces < 0)
3654 spaces = 0;
3655
3656 /* insert the new text */
3657 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3658 newp = alloc_check((unsigned)totlen + oldlen + 1);
3659 if (newp == NULL)
3660 break;
3661 /* copy part up to cursor to new line */
3662 ptr = newp;
3663 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3664 ptr += bd.textcol;
3665 /* may insert some spaces before the new text */
3666 copy_spaces(ptr, (size_t)bd.startspaces);
3667 ptr += bd.startspaces;
3668 /* insert the new text */
3669 for (j = 0; j < count; ++j)
3670 {
3671 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3672 ptr += yanklen;
3673
3674 /* insert block's trailing spaces only if there's text behind */
3675 if ((j < count - 1 || !shortline) && spaces)
3676 {
3677 copy_spaces(ptr, (size_t)spaces);
3678 ptr += spaces;
3679 }
3680 }
3681 /* may insert some spaces after the new text */
3682 copy_spaces(ptr, (size_t)bd.endspaces);
3683 ptr += bd.endspaces;
3684 /* move the text after the cursor to the end of the line. */
3685 mch_memmove(ptr, oldp + bd.textcol + delcount,
3686 (size_t)(oldlen - bd.textcol - delcount + 1));
3687 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3688
3689 ++curwin->w_cursor.lnum;
3690 if (i == 0)
3691 curwin->w_cursor.col += bd.startspaces;
3692 }
3693
3694 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3695
3696 /* Set '[ mark. */
3697 curbuf->b_op_start = curwin->w_cursor;
3698 curbuf->b_op_start.lnum = lnum;
3699
3700 /* adjust '] mark */
3701 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3702 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003703# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (flags & PUT_CURSEND)
3707 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003708 colnr_T len;
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 curwin->w_cursor = curbuf->b_op_end;
3711 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003712
3713 /* in Insert mode we might be after the NUL, correct for that */
3714 len = (colnr_T)STRLEN(ml_get_curline());
3715 if (curwin->w_cursor.col > len)
3716 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 else
3719 curwin->w_cursor.lnum = lnum;
3720 }
3721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
3723 /*
3724 * Character or Line mode
3725 */
3726 if (y_type == MCHAR)
3727 {
3728 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3729 * char */
3730 if (dir == FORWARD && gchar_cursor() != NUL)
3731 {
3732#ifdef FEAT_MBYTE
3733 if (has_mbyte)
3734 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003735 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737 /* put it on the next of the multi-byte character. */
3738 col += bytelen;
3739 if (yanklen)
3740 {
3741 curwin->w_cursor.col += bytelen;
3742 curbuf->b_op_end.col += bytelen;
3743 }
3744 }
3745 else
3746#endif
3747 {
3748 ++col;
3749 if (yanklen)
3750 {
3751 ++curwin->w_cursor.col;
3752 ++curbuf->b_op_end.col;
3753 }
3754 }
3755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 curbuf->b_op_start = curwin->w_cursor;
3757 }
3758 /*
3759 * Line mode: BACKWARD is the same as FORWARD on the previous line
3760 */
3761 else if (dir == BACKWARD)
3762 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003763 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /*
3766 * simple case: insert into current line
3767 */
3768 if (y_type == MCHAR && y_size == 1)
3769 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003770 do {
3771 totlen = count * yanklen;
3772 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003774 oldp = ml_get(lnum);
3775 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3776 if (newp == NULL)
3777 goto end; /* alloc() gave an error message */
3778 mch_memmove(newp, oldp, (size_t)col);
3779 ptr = newp + col;
3780 for (i = 0; i < count; ++i)
3781 {
3782 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3783 ptr += yanklen;
3784 }
3785 STRMOVE(ptr, oldp + col);
3786 ml_replace(lnum, newp, FALSE);
3787 /* Place cursor on last putted char. */
3788 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003789 {
3790 /* make sure curwin->w_virtcol is updated */
3791 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003792 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003795 if (VIsual_active)
3796 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003797 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003798
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 curbuf->b_op_end = curwin->w_cursor;
3800 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3801 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3802 ++curwin->w_cursor.col;
3803 changed_bytes(lnum, col);
3804 }
3805 else
3806 {
3807 /*
3808 * Insert at least one line. When y_type is MCHAR, break the first
3809 * line in two.
3810 */
3811 for (cnt = 1; cnt <= count; ++cnt)
3812 {
3813 i = 0;
3814 if (y_type == MCHAR)
3815 {
3816 /*
3817 * Split the current line in two at the insert position.
3818 * First insert y_array[size - 1] in front of second line.
3819 * Then append y_array[0] to first line.
3820 */
3821 lnum = new_cursor.lnum;
3822 ptr = ml_get(lnum) + col;
3823 totlen = (int)STRLEN(y_array[y_size - 1]);
3824 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3825 if (newp == NULL)
3826 goto error;
3827 STRCPY(newp, y_array[y_size - 1]);
3828 STRCAT(newp, ptr);
3829 /* insert second line */
3830 ml_append(lnum, newp, (colnr_T)0, FALSE);
3831 vim_free(newp);
3832
3833 oldp = ml_get(lnum);
3834 newp = alloc_check((unsigned)(col + yanklen + 1));
3835 if (newp == NULL)
3836 goto error;
3837 /* copy first part of line */
3838 mch_memmove(newp, oldp, (size_t)col);
3839 /* append to first line */
3840 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3841 ml_replace(lnum, newp, FALSE);
3842
3843 curwin->w_cursor.lnum = lnum;
3844 i = 1;
3845 }
3846
3847 for (; i < y_size; ++i)
3848 {
3849 if ((y_type != MCHAR || i < y_size - 1)
3850 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3851 == FAIL)
3852 goto error;
3853 lnum++;
3854 ++nr_lines;
3855 if (flags & PUT_FIXINDENT)
3856 {
3857 old_pos = curwin->w_cursor;
3858 curwin->w_cursor.lnum = lnum;
3859 ptr = ml_get(lnum);
3860 if (cnt == count && i == y_size - 1)
3861 lendiff = (int)STRLEN(ptr);
3862#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3863 if (*ptr == '#' && preprocs_left())
3864 indent = 0; /* Leave # lines at start */
3865 else
3866#endif
3867 if (*ptr == NUL)
3868 indent = 0; /* Ignore empty lines */
3869 else if (first_indent)
3870 {
3871 indent_diff = orig_indent - get_indent();
3872 indent = orig_indent;
3873 first_indent = FALSE;
3874 }
3875 else if ((indent = get_indent() + indent_diff) < 0)
3876 indent = 0;
3877 (void)set_indent(indent, 0);
3878 curwin->w_cursor = old_pos;
3879 /* remember how many chars were removed */
3880 if (cnt == count && i == y_size - 1)
3881 lendiff -= (int)STRLEN(ml_get(lnum));
3882 }
3883 }
3884 }
3885
3886error:
3887 /* Adjust marks. */
3888 if (y_type == MLINE)
3889 {
3890 curbuf->b_op_start.col = 0;
3891 if (dir == FORWARD)
3892 curbuf->b_op_start.lnum++;
3893 }
3894 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3895 (linenr_T)MAXLNUM, nr_lines, 0L);
3896
3897 /* note changed text for displaying and folding */
3898 if (y_type == MCHAR)
3899 changed_lines(curwin->w_cursor.lnum, col,
3900 curwin->w_cursor.lnum + 1, nr_lines);
3901 else
3902 changed_lines(curbuf->b_op_start.lnum, 0,
3903 curbuf->b_op_start.lnum, nr_lines);
3904
3905 /* put '] mark at last inserted character */
3906 curbuf->b_op_end.lnum = lnum;
3907 /* correct length for change in indent */
3908 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3909 if (col > 1)
3910 curbuf->b_op_end.col = col - 1;
3911 else
3912 curbuf->b_op_end.col = 0;
3913
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003914 if (flags & PUT_CURSLINE)
3915 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003916 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003917 curwin->w_cursor.lnum = lnum;
3918 beginline(BL_WHITE | BL_FIX);
3919 }
3920 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
3922 /* put cursor after inserted text */
3923 if (y_type == MLINE)
3924 {
3925 if (lnum >= curbuf->b_ml.ml_line_count)
3926 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3927 else
3928 curwin->w_cursor.lnum = lnum + 1;
3929 curwin->w_cursor.col = 0;
3930 }
3931 else
3932 {
3933 curwin->w_cursor.lnum = lnum;
3934 curwin->w_cursor.col = col;
3935 }
3936 }
3937 else if (y_type == MLINE)
3938 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003939 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 curwin->w_cursor.col = 0;
3941 if (dir == FORWARD)
3942 ++curwin->w_cursor.lnum;
3943 beginline(BL_WHITE | BL_FIX);
3944 }
3945 else /* put cursor on first inserted character */
3946 curwin->w_cursor = new_cursor;
3947 }
3948 }
3949
3950 msgmore(nr_lines);
3951 curwin->w_set_curswant = TRUE;
3952
3953end:
3954 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003956 if (regname == '=')
3957 vim_free(y_array);
3958
Bram Moolenaar033d8882013-09-25 23:24:57 +02003959 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003960
Bram Moolenaar677ee682005-01-27 14:41:15 +00003961 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003962 adjust_cursor_eol();
3963}
3964
3965/*
3966 * When the cursor is on the NUL past the end of the line and it should not be
3967 * there move it left.
3968 */
3969 void
3970adjust_cursor_eol()
3971{
3972 if (curwin->w_cursor.col > 0
3973 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003974#ifdef FEAT_VIRTUALEDIT
3975 && (ve_flags & VE_ONEMORE) == 0
3976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 && !(restart_edit || (State & INSERT)))
3978 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003979 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003980 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003981
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982#ifdef FEAT_VIRTUALEDIT
3983 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003984 {
3985 colnr_T scol, ecol;
3986
3987 /* Coladd is set to the width of the last character. */
3988 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3989 curwin->w_cursor.coladd = ecol - scol + 1;
3990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#endif
3992 }
3993}
3994
3995#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3996/*
3997 * Return TRUE if lines starting with '#' should be left aligned.
3998 */
3999 int
4000preprocs_left()
4001{
4002 return
4003# ifdef FEAT_SMARTINDENT
4004# ifdef FEAT_CINDENT
4005 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4006# else
4007 curbuf->b_p_si
4008# endif
4009# endif
4010# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004011 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4012 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013# endif
4014 ;
4015}
4016#endif
4017
4018/* Return the character name of the register with the given number */
4019 int
4020get_register_name(num)
4021 int num;
4022{
4023 if (num == -1)
4024 return '"';
4025 else if (num < 10)
4026 return num + '0';
4027 else if (num == DELETION_REGISTER)
4028 return '-';
4029#ifdef FEAT_CLIPBOARD
4030 else if (num == STAR_REGISTER)
4031 return '*';
4032 else if (num == PLUS_REGISTER)
4033 return '+';
4034#endif
4035 else
4036 {
4037#ifdef EBCDIC
4038 int i;
4039
4040 /* EBCDIC is really braindead ... */
4041 i = 'a' + (num - 10);
4042 if (i > 'i')
4043 i += 7;
4044 if (i > 'r')
4045 i += 8;
4046 return i;
4047#else
4048 return num + 'a' - 10;
4049#endif
4050 }
4051}
4052
4053/*
4054 * ":dis" and ":registers": Display the contents of the yank registers.
4055 */
4056 void
4057ex_display(eap)
4058 exarg_T *eap;
4059{
4060 int i, n;
4061 long j;
4062 char_u *p;
4063 struct yankreg *yb;
4064 int name;
4065 int attr;
4066 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004067#ifdef FEAT_MBYTE
4068 int clen;
4069#else
4070# define clen 1
4071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 if (arg != NULL && *arg == NUL)
4074 arg = NULL;
4075 attr = hl_attr(HLF_8);
4076
4077 /* Highlight title */
4078 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4079 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4080 {
4081 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004082 if (arg != NULL && vim_strchr(arg, name) == NULL
4083#ifdef ONE_CLIPBOARD
4084 /* Star register and plus register contain the same thing. */
4085 && (name != '*' || vim_strchr(arg, '+') == NULL)
4086#endif
4087 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 continue; /* did not ask for this register */
4089
4090#ifdef FEAT_CLIPBOARD
4091 /* Adjust register name for "unnamed" in 'clipboard'.
4092 * When it's a clipboard register, fill it with the current contents
4093 * of the clipboard. */
4094 adjust_clip_reg(&name);
4095 (void)may_get_selection(name);
4096#endif
4097
4098 if (i == -1)
4099 {
4100 if (y_previous != NULL)
4101 yb = y_previous;
4102 else
4103 yb = &(y_regs[0]);
4104 }
4105 else
4106 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004107
4108#ifdef FEAT_EVAL
4109 if (name == MB_TOLOWER(redir_reg)
4110 || (redir_reg == '"' && yb == y_previous))
4111 continue; /* do not list register being written to, the
4112 * pointer can be freed */
4113#endif
4114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (yb->y_array != NULL)
4116 {
4117 msg_putchar('\n');
4118 msg_putchar('"');
4119 msg_putchar(name);
4120 MSG_PUTS(" ");
4121
4122 n = (int)Columns - 6;
4123 for (j = 0; j < yb->y_size && n > 1; ++j)
4124 {
4125 if (j)
4126 {
4127 MSG_PUTS_ATTR("^J", attr);
4128 n -= 2;
4129 }
4130 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004133 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004134#endif
4135 msg_outtrans_len(p, clen);
4136#ifdef FEAT_MBYTE
4137 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
4139 }
4140 }
4141 if (n > 1 && yb->y_type == MLINE)
4142 MSG_PUTS_ATTR("^J", attr);
4143 out_flush(); /* show one line at a time */
4144 }
4145 ui_breakcheck();
4146 }
4147
4148 /*
4149 * display last inserted text
4150 */
4151 if ((p = get_last_insert()) != NULL
4152 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4153 {
4154 MSG_PUTS("\n\". ");
4155 dis_msg(p, TRUE);
4156 }
4157
4158 /*
4159 * display last command line
4160 */
4161 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4162 && !got_int)
4163 {
4164 MSG_PUTS("\n\": ");
4165 dis_msg(last_cmdline, FALSE);
4166 }
4167
4168 /*
4169 * display current file name
4170 */
4171 if (curbuf->b_fname != NULL
4172 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4173 {
4174 MSG_PUTS("\n\"% ");
4175 dis_msg(curbuf->b_fname, FALSE);
4176 }
4177
4178 /*
4179 * display alternate file name
4180 */
4181 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4182 {
4183 char_u *fname;
4184 linenr_T dummy;
4185
4186 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4187 {
4188 MSG_PUTS("\n\"# ");
4189 dis_msg(fname, FALSE);
4190 }
4191 }
4192
4193 /*
4194 * display last search pattern
4195 */
4196 if (last_search_pat() != NULL
4197 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4198 {
4199 MSG_PUTS("\n\"/ ");
4200 dis_msg(last_search_pat(), FALSE);
4201 }
4202
4203#ifdef FEAT_EVAL
4204 /*
4205 * display last used expression
4206 */
4207 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4208 && !got_int)
4209 {
4210 MSG_PUTS("\n\"= ");
4211 dis_msg(expr_line, FALSE);
4212 }
4213#endif
4214}
4215
4216/*
4217 * display a string for do_dis()
4218 * truncate at end of screen line
4219 */
4220 static void
4221dis_msg(p, skip_esc)
4222 char_u *p;
4223 int skip_esc; /* if TRUE, ignore trailing ESC */
4224{
4225 int n;
4226#ifdef FEAT_MBYTE
4227 int l;
4228#endif
4229
4230 n = (int)Columns - 6;
4231 while (*p != NUL
4232 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4233 && (n -= ptr2cells(p)) >= 0)
4234 {
4235#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004236 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
4238 msg_outtrans_len(p, l);
4239 p += l;
4240 }
4241 else
4242#endif
4243 msg_outtrans_len(p++, 1);
4244 }
4245 ui_breakcheck();
4246}
4247
Bram Moolenaar81340392012-06-06 16:12:59 +02004248#if defined(FEAT_COMMENTS) || defined(PROTO)
4249/*
4250 * If "process" is TRUE and the line begins with a comment leader (possibly
4251 * after some white space), return a pointer to the text after it. Put a boolean
4252 * value indicating whether the line ends with an unclosed comment in
4253 * "is_comment".
4254 * line - line to be processed,
4255 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004256 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004257 * include_space - whether to also skip space following the comment leader,
4258 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004259 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004260 */
4261 static char_u *
4262skip_comment(line, process, include_space, is_comment)
4263 char_u *line;
4264 int process;
4265 int include_space;
4266 int *is_comment;
4267{
4268 char_u *comment_flags = NULL;
4269 int lead_len;
4270 int leader_offset = get_last_leader_offset(line, &comment_flags);
4271
4272 *is_comment = FALSE;
4273 if (leader_offset != -1)
4274 {
4275 /* Let's check whether the line ends with an unclosed comment.
4276 * If the last comment leader has COM_END in flags, there's no comment.
4277 */
4278 while (*comment_flags)
4279 {
4280 if (*comment_flags == COM_END
4281 || *comment_flags == ':')
4282 break;
4283 ++comment_flags;
4284 }
4285 if (*comment_flags != COM_END)
4286 *is_comment = TRUE;
4287 }
4288
4289 if (process == FALSE)
4290 return line;
4291
4292 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4293
4294 if (lead_len == 0)
4295 return line;
4296
4297 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004298 * - COM_END,
4299 * - colon,
4300 * whichever comes first.
4301 */
4302 while (*comment_flags)
4303 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004304 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004305 || *comment_flags == ':')
4306 {
4307 break;
4308 }
4309 ++comment_flags;
4310 }
4311
4312 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004313 * starting with a closing part of a three-part comment. That's good,
4314 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004315 */
4316 if (*comment_flags == ':' || *comment_flags == NUL)
4317 line += lead_len;
4318
4319 return line;
4320}
4321#endif
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004324 * Join 'count' lines (minimal 2) at cursor position.
4325 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004326 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4327 * leaders should not be removed.
4328 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4329 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004331 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 */
4333 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004334do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004335 long count;
4336 int insert_space;
4337 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004338 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004339 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004341 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004342 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004343 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004345 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004346 int endcurr1 = NUL;
4347 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004348 int currsize = 0; /* size of the current line */
4349 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004351 colnr_T col = 0;
4352 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004353#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004354 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004355 int remove_comments = (use_formatoptions == TRUE)
4356 && has_format_option(FO_REMOVE_COMS);
4357 int prev_was_comment;
4358#endif
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004361 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4362 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004365 /* Allocate an array to store the number of spaces inserted before each
4366 * line. We will use it to pre-compute the length of the new line and the
4367 * proper placement of each original line in the new one. */
4368 spaces = lalloc_clear((long_u)count, TRUE);
4369 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004371#if defined(FEAT_COMMENTS) || defined(PROTO)
4372 if (remove_comments)
4373 {
4374 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4375 if (comments == NULL)
4376 {
4377 vim_free(spaces);
4378 return FAIL;
4379 }
4380 }
4381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004384 * Don't move anything, just compute the final line length
4385 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004387 for (t = 0; t < count; ++t)
4388 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004389 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004390 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004391 {
4392 /* Set the '[ mark. */
4393 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4394 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4395 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004396#if defined(FEAT_COMMENTS) || defined(PROTO)
4397 if (remove_comments)
4398 {
4399 /* We don't want to remove the comment leader if the
4400 * previous line is not a comment. */
4401 if (t > 0 && prev_was_comment)
4402 {
4403
4404 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4405 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004406 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004407 curr = new_curr;
4408 }
4409 else
4410 curr = skip_comment(curr, FALSE, insert_space,
4411 &prev_was_comment);
4412 }
4413#endif
4414
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004415 if (insert_space && t > 0)
4416 {
4417 curr = skipwhite(curr);
4418 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4419#ifdef FEAT_MBYTE
4420 && (!has_format_option(FO_MBYTE_JOIN)
4421 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4422 && (!has_format_option(FO_MBYTE_JOIN2)
4423 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4424#endif
4425 )
4426 {
4427 /* don't add a space if the line is ending in a space */
4428 if (endcurr1 == ' ')
4429 endcurr1 = endcurr2;
4430 else
4431 ++spaces[t];
4432 /* extra space when 'joinspaces' set and line ends in '.' */
4433 if ( p_js
4434 && (endcurr1 == '.'
4435 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4436 && (endcurr1 == '?' || endcurr1 == '!'))))
4437 ++spaces[t];
4438 }
4439 }
4440 currsize = (int)STRLEN(curr);
4441 sumsize += currsize + spaces[t];
4442 endcurr1 = endcurr2 = NUL;
4443 if (insert_space && currsize > 0)
4444 {
4445#ifdef FEAT_MBYTE
4446 if (has_mbyte)
4447 {
4448 cend = curr + currsize;
4449 mb_ptr_back(curr, cend);
4450 endcurr1 = (*mb_ptr2char)(cend);
4451 if (cend > curr)
4452 {
4453 mb_ptr_back(curr, cend);
4454 endcurr2 = (*mb_ptr2char)(cend);
4455 }
4456 }
4457 else
4458#endif
4459 {
4460 endcurr1 = *(curr + currsize - 1);
4461 if (currsize > 1)
4462 endcurr2 = *(curr + currsize - 2);
4463 }
4464 }
4465 line_breakcheck();
4466 if (got_int)
4467 {
4468 ret = FAIL;
4469 goto theend;
4470 }
4471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004473 /* store the column position before last line */
4474 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004476 /* allocate the space for the new line */
4477 newp = alloc_check((unsigned)(sumsize + 1));
4478 cend = newp + sumsize;
4479 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004481 /*
4482 * Move affected lines to the new long one.
4483 *
4484 * Move marks from each deleted line to the joined line, adjusting the
4485 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4486 * should not really be a problem.
4487 */
4488 for (t = count - 1; ; --t)
4489 {
4490 cend -= currsize;
4491 mch_memmove(cend, curr, (size_t)currsize);
4492 if (spaces[t] > 0)
4493 {
4494 cend -= spaces[t];
4495 copy_spaces(cend, (size_t)(spaces[t]));
4496 }
4497 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004498 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004499 if (t == 0)
4500 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004501 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004502#if defined(FEAT_COMMENTS) || defined(PROTO)
4503 if (remove_comments)
4504 curr += comments[t - 1];
4505#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004506 if (insert_space && t > 1)
4507 curr = skipwhite(curr);
4508 currsize = (int)STRLEN(curr);
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4511
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004512 if (setmark)
4513 {
4514 /* Set the '] mark. */
4515 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4516 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4517 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /* Only report the change in the first line here, del_lines() will report
4520 * the deleted line. */
4521 changed_lines(curwin->w_cursor.lnum, currsize,
4522 curwin->w_cursor.lnum + 1, 0L);
4523
4524 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004525 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 * briefly, and then move it back. After del_lines() the cursor may
4527 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 */
4529 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004531 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 curwin->w_cursor.lnum = t;
4533
4534 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004535 * Set the cursor column:
4536 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004537 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004539 curwin->w_cursor.col =
4540 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543#ifdef FEAT_VIRTUALEDIT
4544 curwin->w_cursor.coladd = 0;
4545#endif
4546 curwin->w_set_curswant = TRUE;
4547
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004548theend:
4549 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004550#if defined(FEAT_COMMENTS) || defined(PROTO)
4551 if (remove_comments)
4552 vim_free(comments);
4553#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004554 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555}
4556
4557#ifdef FEAT_COMMENTS
4558/*
4559 * Return TRUE if the two comment leaders given are the same. "lnum" is
4560 * the first line. White-space is ignored. Note that the whole of
4561 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4562 */
4563 static int
4564same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4565 linenr_T lnum;
4566 int leader1_len;
4567 char_u *leader1_flags;
4568 int leader2_len;
4569 char_u *leader2_flags;
4570{
4571 int idx1 = 0, idx2 = 0;
4572 char_u *p;
4573 char_u *line1;
4574 char_u *line2;
4575
4576 if (leader1_len == 0)
4577 return (leader2_len == 0);
4578
4579 /*
4580 * If first leader has 'f' flag, the lines can be joined only if the
4581 * second line does not have a leader.
4582 * If first leader has 'e' flag, the lines can never be joined.
4583 * If fist leader has 's' flag, the lines can only be joined if there is
4584 * some text after it and the second line has the 'm' flag.
4585 */
4586 if (leader1_flags != NULL)
4587 {
4588 for (p = leader1_flags; *p && *p != ':'; ++p)
4589 {
4590 if (*p == COM_FIRST)
4591 return (leader2_len == 0);
4592 if (*p == COM_END)
4593 return FALSE;
4594 if (*p == COM_START)
4595 {
4596 if (*(ml_get(lnum) + leader1_len) == NUL)
4597 return FALSE;
4598 if (leader2_flags == NULL || leader2_len == 0)
4599 return FALSE;
4600 for (p = leader2_flags; *p && *p != ':'; ++p)
4601 if (*p == COM_MIDDLE)
4602 return TRUE;
4603 return FALSE;
4604 }
4605 }
4606 }
4607
4608 /*
4609 * Get current line and next line, compare the leaders.
4610 * The first line has to be saved, only one line can be locked at a time.
4611 */
4612 line1 = vim_strsave(ml_get(lnum));
4613 if (line1 != NULL)
4614 {
4615 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4616 ;
4617 line2 = ml_get(lnum + 1);
4618 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4619 {
4620 if (!vim_iswhite(line2[idx2]))
4621 {
4622 if (line1[idx1++] != line2[idx2])
4623 break;
4624 }
4625 else
4626 while (vim_iswhite(line1[idx1]))
4627 ++idx1;
4628 }
4629 vim_free(line1);
4630 }
4631 return (idx2 == leader2_len && idx1 == leader1_len);
4632}
4633#endif
4634
4635/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004636 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 */
4638 void
4639op_format(oap, keep_cursor)
4640 oparg_T *oap;
4641 int keep_cursor; /* keep cursor on same text char */
4642{
4643 long old_line_count = curbuf->b_ml.ml_line_count;
4644
4645 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4646 * can put it back there. */
4647 curwin->w_cursor = oap->cursor_start;
4648
4649 if (u_save((linenr_T)(oap->start.lnum - 1),
4650 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4651 return;
4652 curwin->w_cursor = oap->start;
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (oap->is_VIsual)
4655 /* When there is no change: need to remove the Visual selection */
4656 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 /* Set '[ mark at the start of the formatted area */
4659 curbuf->b_op_start = oap->start;
4660
4661 /* For "gw" remember the cursor position and put it back below (adjusted
4662 * for joined and split lines). */
4663 if (keep_cursor)
4664 saved_cursor = oap->cursor_start;
4665
Bram Moolenaar81a82092008-03-12 16:27:00 +00004666 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668 /*
4669 * Leave the cursor at the first non-blank of the last formatted line.
4670 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4671 * line, so "." will do the next lines.
4672 */
4673 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4674 ++curwin->w_cursor.lnum;
4675 beginline(BL_WHITE | BL_FIX);
4676 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4677 msgmore(old_line_count);
4678
4679 /* put '] mark on the end of the formatted area */
4680 curbuf->b_op_end = curwin->w_cursor;
4681
4682 if (keep_cursor)
4683 {
4684 curwin->w_cursor = saved_cursor;
4685 saved_cursor.lnum = 0;
4686 }
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (oap->is_VIsual)
4689 {
4690 win_T *wp;
4691
4692 FOR_ALL_WINDOWS(wp)
4693 {
4694 if (wp->w_old_cursor_lnum != 0)
4695 {
4696 /* When lines have been inserted or deleted, adjust the end of
4697 * the Visual area to be redrawn. */
4698 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4699 wp->w_old_cursor_lnum += old_line_count;
4700 else
4701 wp->w_old_visual_lnum += old_line_count;
4702 }
4703 }
4704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705}
4706
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004707#if defined(FEAT_EVAL) || defined(PROTO)
4708/*
4709 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4710 */
4711 void
4712op_formatexpr(oap)
4713 oparg_T *oap;
4714{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004715 if (oap->is_VIsual)
4716 /* When there is no change: need to remove the Visual selection */
4717 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004718
Bram Moolenaar700303e2010-07-11 17:35:50 +02004719 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4720 /* As documented: when 'formatexpr' returns non-zero fall back to
4721 * internal formatting. */
4722 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004723}
4724
4725 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004726fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004727 linenr_T lnum;
4728 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004729 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004730{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004731 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4732 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004733 int r;
4734
4735 /*
4736 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004737 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004738 */
4739 set_vim_var_nr(VV_LNUM, lnum);
4740 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004741 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004742
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004743 /*
4744 * Evaluate the function.
4745 */
4746 if (use_sandbox)
4747 ++sandbox;
4748 r = eval_to_number(curbuf->b_p_fex);
4749 if (use_sandbox)
4750 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004751
4752 set_vim_var_string(VV_CHAR, NULL, -1);
4753
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004754 return r;
4755}
4756#endif
4757
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758/*
4759 * Format "line_count" lines, starting at the cursor position.
4760 * When "line_count" is negative, format until the end of the paragraph.
4761 * Lines after the cursor line are saved for undo, caller must have saved the
4762 * first line.
4763 */
4764 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004765format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004767 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 int max_len;
4770 int is_not_par; /* current line not part of parag. */
4771 int next_is_not_par; /* next line not part of paragraph */
4772 int is_end_par; /* at end of paragraph */
4773 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4774 int next_is_start_par = FALSE;
4775#ifdef FEAT_COMMENTS
4776 int leader_len = 0; /* leader len of current line */
4777 int next_leader_len; /* leader len of next line */
4778 char_u *leader_flags = NULL; /* flags for leader of current line */
4779 char_u *next_leader_flags; /* flags for leader of next line */
4780 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004781 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782#endif
4783 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004784 int second_indent = -1; /* indent for second line (comment
4785 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 int do_second_indent;
4787 int do_number_indent;
4788 int do_trail_white;
4789 int first_par_line = TRUE;
4790 int smd_save;
4791 long count;
4792 int need_set_indent = TRUE; /* set indent of next paragraph */
4793 int force_format = FALSE;
4794 int old_State = State;
4795
4796 /* length of a line to force formatting: 3 * 'tw' */
4797 max_len = comp_textwidth(TRUE) * 3;
4798
4799 /* check for 'q', '2' and '1' in 'formatoptions' */
4800#ifdef FEAT_COMMENTS
4801 do_comments = has_format_option(FO_Q_COMS);
4802#endif
4803 do_second_indent = has_format_option(FO_Q_SECOND);
4804 do_number_indent = has_format_option(FO_Q_NUMBER);
4805 do_trail_white = has_format_option(FO_WHITE_PAR);
4806
4807 /*
4808 * Get info about the previous and current line.
4809 */
4810 if (curwin->w_cursor.lnum > 1)
4811 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4812#ifdef FEAT_COMMENTS
4813 , &leader_len, &leader_flags, do_comments
4814#endif
4815 );
4816 else
4817 is_not_par = TRUE;
4818 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4819#ifdef FEAT_COMMENTS
4820 , &next_leader_len, &next_leader_flags, do_comments
4821#endif
4822 );
4823 is_end_par = (is_not_par || next_is_not_par);
4824 if (!is_end_par && do_trail_white)
4825 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4826
4827 curwin->w_cursor.lnum--;
4828 for (count = line_count; count != 0 && !got_int; --count)
4829 {
4830 /*
4831 * Advance to next paragraph.
4832 */
4833 if (advance)
4834 {
4835 curwin->w_cursor.lnum++;
4836 prev_is_end_par = is_end_par;
4837 is_not_par = next_is_not_par;
4838#ifdef FEAT_COMMENTS
4839 leader_len = next_leader_len;
4840 leader_flags = next_leader_flags;
4841#endif
4842 }
4843
4844 /*
4845 * The last line to be formatted.
4846 */
4847 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4848 {
4849 next_is_not_par = TRUE;
4850#ifdef FEAT_COMMENTS
4851 next_leader_len = 0;
4852 next_leader_flags = NULL;
4853#endif
4854 }
4855 else
4856 {
4857 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4858#ifdef FEAT_COMMENTS
4859 , &next_leader_len, &next_leader_flags, do_comments
4860#endif
4861 );
4862 if (do_number_indent)
4863 next_is_start_par =
4864 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4865 }
4866 advance = TRUE;
4867 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4868 if (!is_end_par && do_trail_white)
4869 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4870
4871 /*
4872 * Skip lines that are not in a paragraph.
4873 */
4874 if (is_not_par)
4875 {
4876 if (line_count < 0)
4877 break;
4878 }
4879 else
4880 {
4881 /*
4882 * For the first line of a paragraph, check indent of second line.
4883 * Don't do this for comments and empty lines.
4884 */
4885 if (first_par_line
4886 && (do_second_indent || do_number_indent)
4887 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004888 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004890 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4891 {
4892#ifdef FEAT_COMMENTS
4893 if (leader_len == 0 && next_leader_len == 0)
4894 {
4895 /* no comment found */
4896#endif
4897 second_indent =
4898 get_indent_lnum(curwin->w_cursor.lnum + 1);
4899#ifdef FEAT_COMMENTS
4900 }
4901 else
4902 {
4903 second_indent = next_leader_len;
4904 do_comments_list = 1;
4905 }
4906#endif
4907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004909 {
4910#ifdef FEAT_COMMENTS
4911 if (leader_len == 0 && next_leader_len == 0)
4912 {
4913 /* no comment found */
4914#endif
4915 second_indent =
4916 get_number_indent(curwin->w_cursor.lnum);
4917#ifdef FEAT_COMMENTS
4918 }
4919 else
4920 {
4921 /* get_number_indent() is now "comment aware"... */
4922 second_indent =
4923 get_number_indent(curwin->w_cursor.lnum);
4924 do_comments_list = 1;
4925 }
4926#endif
4927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929
4930 /*
4931 * When the comment leader changes, it's the end of the paragraph.
4932 */
4933 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4934#ifdef FEAT_COMMENTS
4935 || !same_leader(curwin->w_cursor.lnum,
4936 leader_len, leader_flags,
4937 next_leader_len, next_leader_flags)
4938#endif
4939 )
4940 is_end_par = TRUE;
4941
4942 /*
4943 * If we have got to the end of a paragraph, or the line is
4944 * getting long, format it.
4945 */
4946 if (is_end_par || force_format)
4947 {
4948 if (need_set_indent)
4949 /* replace indent in first line with minimal number of
4950 * tabs and spaces, according to current options */
4951 (void)set_indent(get_indent(), SIN_CHANGED);
4952
4953 /* put cursor on last non-space */
4954 State = NORMAL; /* don't go past end-of-line */
4955 coladvance((colnr_T)MAXCOL);
4956 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4957 dec_cursor();
4958
4959 /* do the formatting, without 'showmode' */
4960 State = INSERT; /* for open_line() */
4961 smd_save = p_smd;
4962 p_smd = FALSE;
4963 insertchar(NUL, INSCHAR_FORMAT
4964#ifdef FEAT_COMMENTS
4965 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004966 + (do_comments && do_comments_list
4967 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004969 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 State = old_State;
4971 p_smd = smd_save;
4972 second_indent = -1;
4973 /* at end of par.: need to set indent of next par. */
4974 need_set_indent = is_end_par;
4975 if (is_end_par)
4976 {
4977 /* When called with a negative line count, break at the
4978 * end of the paragraph. */
4979 if (line_count < 0)
4980 break;
4981 first_par_line = TRUE;
4982 }
4983 force_format = FALSE;
4984 }
4985
4986 /*
4987 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004988 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 */
4990 if (!is_end_par)
4991 {
4992 advance = FALSE;
4993 curwin->w_cursor.lnum++;
4994 curwin->w_cursor.col = 0;
4995 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004999 {
5000 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5002 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005003 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005005 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5006 {
5007 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005008 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005009
5010 if (indent > 0)
5011 {
5012 (void)del_bytes(indent, FALSE, FALSE);
5013 mark_col_adjust(curwin->w_cursor.lnum,
5014 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005015 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005018 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 beep_flush();
5021 break;
5022 }
5023 first_par_line = FALSE;
5024 /* If the line is getting long, format it next time */
5025 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5026 force_format = TRUE;
5027 else
5028 force_format = FALSE;
5029 }
5030 }
5031 line_breakcheck();
5032 }
5033}
5034
5035/*
5036 * Return TRUE if line "lnum" ends in a white character.
5037 */
5038 static int
5039ends_in_white(lnum)
5040 linenr_T lnum;
5041{
5042 char_u *s = ml_get(lnum);
5043 size_t l;
5044
5045 if (*s == NUL)
5046 return FALSE;
5047 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5048 * invocation may call function multiple times". */
5049 l = STRLEN(s) - 1;
5050 return vim_iswhite(s[l]);
5051}
5052
5053/*
5054 * Blank lines, and lines containing only the comment leader, are left
5055 * untouched by the formatting. The function returns TRUE in this
5056 * case. It also returns TRUE when a line starts with the end of a comment
5057 * ('e' in comment flags), so that this line is skipped, and not joined to the
5058 * previous line. A new paragraph starts after a blank line, or when the
5059 * comment leader changes -- webb.
5060 */
5061#ifdef FEAT_COMMENTS
5062 static int
5063fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5064 linenr_T lnum;
5065 int *leader_len;
5066 char_u **leader_flags;
5067 int do_comments;
5068{
5069 char_u *flags = NULL; /* init for GCC */
5070 char_u *ptr;
5071
5072 ptr = ml_get(lnum);
5073 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005074 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 else
5076 *leader_len = 0;
5077
5078 if (*leader_len > 0)
5079 {
5080 /*
5081 * Search for 'e' flag in comment leader flags.
5082 */
5083 flags = *leader_flags;
5084 while (*flags && *flags != ':' && *flags != COM_END)
5085 ++flags;
5086 }
5087
5088 return (*skipwhite(ptr + *leader_len) == NUL
5089 || (*leader_len > 0 && *flags == COM_END)
5090 || startPS(lnum, NUL, FALSE));
5091}
5092#else
5093 static int
5094fmt_check_par(lnum)
5095 linenr_T lnum;
5096{
5097 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5098}
5099#endif
5100
5101/*
5102 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5103 * previous line is in the same paragraph. Used for auto-formatting.
5104 */
5105 int
5106paragraph_start(lnum)
5107 linenr_T lnum;
5108{
5109 char_u *p;
5110#ifdef FEAT_COMMENTS
5111 int leader_len = 0; /* leader len of current line */
5112 char_u *leader_flags = NULL; /* flags for leader of current line */
5113 int next_leader_len; /* leader len of next line */
5114 char_u *next_leader_flags; /* flags for leader of next line */
5115 int do_comments; /* format comments */
5116#endif
5117
5118 if (lnum <= 1)
5119 return TRUE; /* start of the file */
5120
5121 p = ml_get(lnum - 1);
5122 if (*p == NUL)
5123 return TRUE; /* after empty line */
5124
5125#ifdef FEAT_COMMENTS
5126 do_comments = has_format_option(FO_Q_COMS);
5127#endif
5128 if (fmt_check_par(lnum - 1
5129#ifdef FEAT_COMMENTS
5130 , &leader_len, &leader_flags, do_comments
5131#endif
5132 ))
5133 return TRUE; /* after non-paragraph line */
5134
5135 if (fmt_check_par(lnum
5136#ifdef FEAT_COMMENTS
5137 , &next_leader_len, &next_leader_flags, do_comments
5138#endif
5139 ))
5140 return TRUE; /* "lnum" is not a paragraph line */
5141
5142 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5143 return TRUE; /* missing trailing space in previous line. */
5144
5145 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5146 return TRUE; /* numbered item starts in "lnum". */
5147
5148#ifdef FEAT_COMMENTS
5149 if (!same_leader(lnum - 1, leader_len, leader_flags,
5150 next_leader_len, next_leader_flags))
5151 return TRUE; /* change of comment leader. */
5152#endif
5153
5154 return FALSE;
5155}
5156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157/*
5158 * prepare a few things for block mode yank/delete/tilde
5159 *
5160 * for delete:
5161 * - textlen includes the first/last char to be (partly) deleted
5162 * - start/endspaces is the number of columns that are taken by the
5163 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005164 * deleted.
5165 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 * - textlen includes the first/last char to be wholly yanked
5167 * - start/endspaces is the number of columns of the first/last yanked char
5168 * that are to be yanked.
5169 */
5170 static void
5171block_prep(oap, bdp, lnum, is_del)
5172 oparg_T *oap;
5173 struct block_def *bdp;
5174 linenr_T lnum;
5175 int is_del;
5176{
5177 int incr = 0;
5178 char_u *pend;
5179 char_u *pstart;
5180 char_u *line;
5181 char_u *prev_pstart;
5182 char_u *prev_pend;
5183
5184 bdp->startspaces = 0;
5185 bdp->endspaces = 0;
5186 bdp->textlen = 0;
5187 bdp->start_vcol = 0;
5188 bdp->end_vcol = 0;
5189#ifdef FEAT_VISUALEXTRA
5190 bdp->is_short = FALSE;
5191 bdp->is_oneChar = FALSE;
5192 bdp->pre_whitesp = 0;
5193 bdp->pre_whitesp_c = 0;
5194 bdp->end_char_vcols = 0;
5195#endif
5196 bdp->start_char_vcols = 0;
5197
5198 line = ml_get(lnum);
5199 pstart = line;
5200 prev_pstart = line;
5201 while (bdp->start_vcol < oap->start_vcol && *pstart)
5202 {
5203 /* Count a tab for what it's worth (if list mode not on) */
5204 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
5205 bdp->start_vcol += incr;
5206#ifdef FEAT_VISUALEXTRA
5207 if (vim_iswhite(*pstart))
5208 {
5209 bdp->pre_whitesp += incr;
5210 bdp->pre_whitesp_c++;
5211 }
5212 else
5213 {
5214 bdp->pre_whitesp = 0;
5215 bdp->pre_whitesp_c = 0;
5216 }
5217#endif
5218 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005219 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221 bdp->start_char_vcols = incr;
5222 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5223 {
5224 bdp->end_vcol = bdp->start_vcol;
5225#ifdef FEAT_VISUALEXTRA
5226 bdp->is_short = TRUE;
5227#endif
5228 if (!is_del || oap->op_type == OP_APPEND)
5229 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5230 }
5231 else
5232 {
5233 /* notice: this converts partly selected Multibyte characters to
5234 * spaces, too. */
5235 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5236 if (is_del && bdp->startspaces)
5237 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5238 pend = pstart;
5239 bdp->end_vcol = bdp->start_vcol;
5240 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5241 {
5242#ifdef FEAT_VISUALEXTRA
5243 bdp->is_oneChar = TRUE;
5244#endif
5245 if (oap->op_type == OP_INSERT)
5246 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5247 else if (oap->op_type == OP_APPEND)
5248 {
5249 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5250 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5251 }
5252 else
5253 {
5254 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5255 if (is_del && oap->op_type != OP_LSHIFT)
5256 {
5257 /* just putting the sum of those two into
5258 * bdp->startspaces doesn't work for Visual replace,
5259 * so we have to split the tab in two */
5260 bdp->startspaces = bdp->start_char_vcols
5261 - (bdp->start_vcol - oap->start_vcol);
5262 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5263 }
5264 }
5265 }
5266 else
5267 {
5268 prev_pend = pend;
5269 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5270 {
5271 /* Count a tab for what it's worth (if list mode not on) */
5272 prev_pend = pend;
5273 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
5274 bdp->end_vcol += incr;
5275 }
5276 if (bdp->end_vcol <= oap->end_vcol
5277 && (!is_del
5278 || oap->op_type == OP_APPEND
5279 || oap->op_type == OP_REPLACE)) /* line too short */
5280 {
5281#ifdef FEAT_VISUALEXTRA
5282 bdp->is_short = TRUE;
5283#endif
5284 /* Alternative: include spaces to fill up the block.
5285 * Disadvantage: can lead to trailing spaces when the line is
5286 * short where the text is put */
5287 /* if (!is_del || oap->op_type == OP_APPEND) */
5288 if (oap->op_type == OP_APPEND || virtual_op)
5289 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005290 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 else
5292 bdp->endspaces = 0; /* replace doesn't add characters */
5293 }
5294 else if (bdp->end_vcol > oap->end_vcol)
5295 {
5296 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5297 if (!is_del && bdp->endspaces)
5298 {
5299 bdp->endspaces = incr - bdp->endspaces;
5300 if (pend != pstart)
5301 pend = prev_pend;
5302 }
5303 }
5304 }
5305#ifdef FEAT_VISUALEXTRA
5306 bdp->end_char_vcols = incr;
5307#endif
5308 if (is_del && bdp->startspaces)
5309 pstart = prev_pstart;
5310 bdp->textlen = (int)(pend - pstart);
5311 }
5312 bdp->textcol = (colnr_T) (pstart - line);
5313 bdp->textstart = pstart;
5314}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316#ifdef FEAT_RIGHTLEFT
5317static void reverse_line __ARGS((char_u *s));
5318
5319 static void
5320reverse_line(s)
5321 char_u *s;
5322{
5323 int i, j;
5324 char_u c;
5325
5326 if ((i = (int)STRLEN(s) - 1) <= 0)
5327 return;
5328
5329 curwin->w_cursor.col = i - curwin->w_cursor.col;
5330 for (j = 0; j < i; j++, i--)
5331 {
5332 c = s[i]; s[i] = s[j]; s[j] = c;
5333 }
5334}
5335
5336# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5337#else
5338# define RLADDSUBFIX(ptr)
5339#endif
5340
5341/*
5342 * add or subtract 'Prenum1' from a number in a line
5343 * 'command' is CTRL-A for add, CTRL-X for subtract
5344 *
5345 * return FAIL for failure, OK otherwise
5346 */
5347 int
5348do_addsub(command, Prenum1)
5349 int command;
5350 linenr_T Prenum1;
5351{
5352 int col;
5353 char_u *buf1;
5354 char_u buf2[NUMBUFLEN];
5355 int hex; /* 'X' or 'x': hex; '0': octal */
5356 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005357 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 long_u oldn;
5359 char_u *ptr;
5360 int c;
5361 int length = 0; /* character length of the number */
5362 int todel;
5363 int dohex;
5364 int dooct;
5365 int doalp;
5366 int firstdigit;
5367 int negative;
5368 int subtract;
5369
5370 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5371 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5372 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5373
5374 ptr = ml_get_curline();
5375 RLADDSUBFIX(ptr);
5376
5377 /*
5378 * First check if we are on a hexadecimal number, after the "0x".
5379 */
5380 col = curwin->w_cursor.col;
5381 if (dohex)
5382 while (col > 0 && vim_isxdigit(ptr[col]))
5383 --col;
5384 if ( dohex
5385 && col > 0
5386 && (ptr[col] == 'X'
5387 || ptr[col] == 'x')
5388 && ptr[col - 1] == '0'
5389 && vim_isxdigit(ptr[col + 1]))
5390 {
5391 /*
5392 * Found hexadecimal number, move to its start.
5393 */
5394 --col;
5395 }
5396 else
5397 {
5398 /*
5399 * Search forward and then backward to find the start of number.
5400 */
5401 col = curwin->w_cursor.col;
5402
5403 while (ptr[col] != NUL
5404 && !vim_isdigit(ptr[col])
5405 && !(doalp && ASCII_ISALPHA(ptr[col])))
5406 ++col;
5407
5408 while (col > 0
5409 && vim_isdigit(ptr[col - 1])
5410 && !(doalp && ASCII_ISALPHA(ptr[col])))
5411 --col;
5412 }
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 /*
5415 * If a number was found, and saving for undo works, replace the number.
5416 */
5417 firstdigit = ptr[col];
5418 RLADDSUBFIX(ptr);
5419 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5420 || u_save_cursor() != OK)
5421 {
5422 beep_flush();
5423 return FAIL;
5424 }
5425
5426 /* get ptr again, because u_save() may have changed it */
5427 ptr = ml_get_curline();
5428 RLADDSUBFIX(ptr);
5429
5430 if (doalp && ASCII_ISALPHA(firstdigit))
5431 {
5432 /* decrement or increment alphabetic character */
5433 if (command == Ctrl_X)
5434 {
5435 if (CharOrd(firstdigit) < Prenum1)
5436 {
5437 if (isupper(firstdigit))
5438 firstdigit = 'A';
5439 else
5440 firstdigit = 'a';
5441 }
5442 else
5443#ifdef EBCDIC
5444 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5445#else
5446 firstdigit -= Prenum1;
5447#endif
5448 }
5449 else
5450 {
5451 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5452 {
5453 if (isupper(firstdigit))
5454 firstdigit = 'Z';
5455 else
5456 firstdigit = 'z';
5457 }
5458 else
5459#ifdef EBCDIC
5460 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5461#else
5462 firstdigit += Prenum1;
5463#endif
5464 }
5465 curwin->w_cursor.col = col;
5466 (void)del_char(FALSE);
5467 ins_char(firstdigit);
5468 }
5469 else
5470 {
5471 negative = FALSE;
5472 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5473 {
5474 --col;
5475 negative = TRUE;
5476 }
5477
5478 /* get the number value (unsigned) */
5479 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5480
5481 /* ignore leading '-' for hex and octal numbers */
5482 if (hex && negative)
5483 {
5484 ++col;
5485 --length;
5486 negative = FALSE;
5487 }
5488
5489 /* add or subtract */
5490 subtract = FALSE;
5491 if (command == Ctrl_X)
5492 subtract ^= TRUE;
5493 if (negative)
5494 subtract ^= TRUE;
5495
5496 oldn = n;
5497 if (subtract)
5498 n -= (unsigned long)Prenum1;
5499 else
5500 n += (unsigned long)Prenum1;
5501
5502 /* handle wraparound for decimal numbers */
5503 if (!hex)
5504 {
5505 if (subtract)
5506 {
5507 if (n > oldn)
5508 {
5509 n = 1 + (n ^ (unsigned long)-1);
5510 negative ^= TRUE;
5511 }
5512 }
5513 else /* add */
5514 {
5515 if (n < oldn)
5516 {
5517 n = (n ^ (unsigned long)-1);
5518 negative ^= TRUE;
5519 }
5520 }
5521 if (n == 0)
5522 negative = FALSE;
5523 }
5524
5525 /*
5526 * Delete the old number.
5527 */
5528 curwin->w_cursor.col = col;
5529 todel = length;
5530 c = gchar_cursor();
5531 /*
5532 * Don't include the '-' in the length, only the length of the part
5533 * after it is kept the same.
5534 */
5535 if (c == '-')
5536 --length;
5537 while (todel-- > 0)
5538 {
5539 if (c < 0x100 && isalpha(c))
5540 {
5541 if (isupper(c))
5542 hexupper = TRUE;
5543 else
5544 hexupper = FALSE;
5545 }
5546 /* del_char() will mark line needing displaying */
5547 (void)del_char(FALSE);
5548 c = gchar_cursor();
5549 }
5550
5551 /*
5552 * Prepare the leading characters in buf1[].
5553 * When there are many leading zeros it could be very long. Allocate
5554 * a bit too much.
5555 */
5556 buf1 = alloc((unsigned)length + NUMBUFLEN);
5557 if (buf1 == NULL)
5558 return FAIL;
5559 ptr = buf1;
5560 if (negative)
5561 {
5562 *ptr++ = '-';
5563 }
5564 if (hex)
5565 {
5566 *ptr++ = '0';
5567 --length;
5568 }
5569 if (hex == 'x' || hex == 'X')
5570 {
5571 *ptr++ = hex;
5572 --length;
5573 }
5574
5575 /*
5576 * Put the number characters in buf2[].
5577 */
5578 if (hex == 0)
5579 sprintf((char *)buf2, "%lu", n);
5580 else if (hex == '0')
5581 sprintf((char *)buf2, "%lo", n);
5582 else if (hex && hexupper)
5583 sprintf((char *)buf2, "%lX", n);
5584 else
5585 sprintf((char *)buf2, "%lx", n);
5586 length -= (int)STRLEN(buf2);
5587
5588 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005589 * Adjust number of zeros to the new number of digits, so the
5590 * total length of the number remains the same.
5591 * Don't do this when
5592 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005594 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 while (length-- > 0)
5596 *ptr++ = '0';
5597 *ptr = NUL;
5598 STRCAT(buf1, buf2);
5599 ins_str(buf1); /* insert the new number */
5600 vim_free(buf1);
5601 }
5602 --curwin->w_cursor.col;
5603 curwin->w_set_curswant = TRUE;
5604#ifdef FEAT_RIGHTLEFT
5605 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5606 RLADDSUBFIX(ptr);
5607#endif
5608 return OK;
5609}
5610
5611#ifdef FEAT_VIMINFO
5612 int
5613read_viminfo_register(virp, force)
5614 vir_T *virp;
5615 int force;
5616{
5617 int eof;
5618 int do_it = TRUE;
5619 int size;
5620 int limit;
5621 int i;
5622 int set_prev = FALSE;
5623 char_u *str;
5624 char_u **array = NULL;
5625
5626 /* We only get here (hopefully) if line[0] == '"' */
5627 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005628
5629 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (*str == '"')
5631 {
5632 set_prev = TRUE;
5633 str++;
5634 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005635
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (!ASCII_ISALNUM(*str) && *str != '-')
5637 {
5638 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5639 return TRUE; /* too many errors, pretend end-of-file */
5640 do_it = FALSE;
5641 }
5642 get_yank_register(*str++, FALSE);
5643 if (!force && y_current->y_array != NULL)
5644 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005645
5646 if (*str == '@')
5647 {
5648 /* "x@: register x used for @@ */
5649 if (force || execreg_lastc == NUL)
5650 execreg_lastc = str[-1];
5651 }
5652
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 size = 0;
5654 limit = 100; /* Optimized for registers containing <= 100 lines */
5655 if (do_it)
5656 {
5657 if (set_prev)
5658 y_previous = y_current;
5659 vim_free(y_current->y_array);
5660 array = y_current->y_array =
5661 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005662 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 if (STRNCMP(str, "CHAR", 4) == 0)
5664 y_current->y_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 else if (STRNCMP(str, "BLOCK", 5) == 0)
5666 y_current->y_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 else
5668 y_current->y_type = MLINE;
5669 /* get the block width; if it's missing we get a zero, which is OK */
5670 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 y_current->y_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673
5674 while (!(eof = viminfo_readline(virp))
5675 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5676 {
5677 if (do_it)
5678 {
5679 if (size >= limit)
5680 {
5681 y_current->y_array = (char_u **)
5682 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5683 for (i = 0; i < limit; i++)
5684 y_current->y_array[i] = array[i];
5685 vim_free(array);
5686 limit *= 2;
5687 array = y_current->y_array;
5688 }
5689 str = viminfo_readstring(virp, 1, TRUE);
5690 if (str != NULL)
5691 array[size++] = str;
5692 else
5693 do_it = FALSE;
5694 }
5695 }
5696 if (do_it)
5697 {
5698 if (size == 0)
5699 {
5700 vim_free(array);
5701 y_current->y_array = NULL;
5702 }
5703 else if (size < limit)
5704 {
5705 y_current->y_array =
5706 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5707 for (i = 0; i < size; i++)
5708 y_current->y_array[i] = array[i];
5709 vim_free(array);
5710 }
5711 y_current->y_size = size;
5712 }
5713 return eof;
5714}
5715
5716 void
5717write_viminfo_registers(fp)
5718 FILE *fp;
5719{
5720 int i, j;
5721 char_u *type;
5722 char_u c;
5723 int num_lines;
5724 int max_num_lines;
5725 int max_kbyte;
5726 long len;
5727
Bram Moolenaar64404472010-06-26 06:24:45 +02005728 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
5730 /* Get '<' value, use old '"' value if '<' is not found. */
5731 max_num_lines = get_viminfo_parameter('<');
5732 if (max_num_lines < 0)
5733 max_num_lines = get_viminfo_parameter('"');
5734 if (max_num_lines == 0)
5735 return;
5736 max_kbyte = get_viminfo_parameter('s');
5737 if (max_kbyte == 0)
5738 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 for (i = 0; i < NUM_REGISTERS; i++)
5741 {
5742 if (y_regs[i].y_array == NULL)
5743 continue;
5744#ifdef FEAT_CLIPBOARD
5745 /* Skip '*'/'+' register, we don't want them back next time */
5746 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5747 continue;
5748#endif
5749#ifdef FEAT_DND
5750 /* Neither do we want the '~' register */
5751 if (i == TILDE_REGISTER)
5752 continue;
5753#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005754 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005756 if (num_lines == 0
5757 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005758 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005759 continue;
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 if (max_kbyte > 0)
5762 {
5763 /* Skip register if there is more text than the maximum size. */
5764 len = 0;
5765 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005766 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (len > (long)max_kbyte * 1024L)
5768 continue;
5769 }
5770
5771 switch (y_regs[i].y_type)
5772 {
5773 case MLINE:
5774 type = (char_u *)"LINE";
5775 break;
5776 case MCHAR:
5777 type = (char_u *)"CHAR";
5778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 case MBLOCK:
5780 type = (char_u *)"BLOCK";
5781 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 default:
5783 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005784 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 emsg(IObuff);
5786 type = (char_u *)"LINE";
5787 break;
5788 }
5789 if (y_previous == &y_regs[i])
5790 fprintf(fp, "\"");
5791 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005792 fprintf(fp, "\"%c", c);
5793 if (c == execreg_lastc)
5794 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005795 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 /* If max_num_lines < 0, then we save ALL the lines in the register */
5798 if (max_num_lines > 0 && num_lines > max_num_lines)
5799 num_lines = max_num_lines;
5800 for (j = 0; j < num_lines; j++)
5801 {
5802 putc('\t', fp);
5803 viminfo_writestring(fp, y_regs[i].y_array[j]);
5804 }
5805 }
5806}
5807#endif /* FEAT_VIMINFO */
5808
5809#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5810/*
5811 * SELECTION / PRIMARY ('*')
5812 *
5813 * Text selection stuff that uses the GUI selection register '*'. When using a
5814 * GUI this may be text from another window, otherwise it is the last text we
5815 * had highlighted with VIsual mode. With mouse support, clicking the middle
5816 * button performs the paste, otherwise you will need to do <"*p>. "
5817 * If not under X, it is synonymous with the clipboard register '+'.
5818 *
5819 * X CLIPBOARD ('+')
5820 *
5821 * Text selection stuff that uses the GUI clipboard register '+'.
5822 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5823 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5824 * otherwise you will need to do <"+p>. "
5825 * If not under X, it is synonymous with the selection register '*'.
5826 */
5827
5828/*
5829 * Routine to export any final X selection we had to the environment
5830 * so that the text is still available after vim has exited. X selections
5831 * only exist while the owning application exists, so we write to the
5832 * permanent (while X runs) store CUT_BUFFER0.
5833 * Dump the CLIPBOARD selection if we own it (it's logically the more
5834 * 'permanent' of the two), otherwise the PRIMARY one.
5835 * For now, use a hard-coded sanity limit of 1Mb of data.
5836 */
5837#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5838 void
5839x11_export_final_selection()
5840{
5841 Display *dpy;
5842 char_u *str = NULL;
5843 long_u len = 0;
5844 int motion_type = -1;
5845
5846# ifdef FEAT_GUI
5847 if (gui.in_use)
5848 dpy = X_DISPLAY;
5849 else
5850# endif
5851# ifdef FEAT_XCLIPBOARD
5852 dpy = xterm_dpy;
5853# else
5854 return;
5855# endif
5856
5857 /* Get selection to export */
5858 if (clip_plus.owned)
5859 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5860 else if (clip_star.owned)
5861 motion_type = clip_convert_selection(&str, &len, &clip_star);
5862
5863 /* Check it's OK */
5864 if (dpy != NULL && str != NULL && motion_type >= 0
5865 && len < 1024*1024 && len > 0)
5866 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005867#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005868 int ok = TRUE;
5869
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005870 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5871 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5872 * encoding conversion usually doesn't work, so keep the text as-is.
5873 */
5874 if (has_mbyte)
5875 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005876 vimconv_T vc;
5877
5878 vc.vc_type = CONV_NONE;
5879 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5880 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005881 int intlen = len;
5882 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005883
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005884 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005885 conv_str = string_convert(&vc, str, &intlen);
5886 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005887 if (conv_str != NULL)
5888 {
5889 vim_free(str);
5890 str = conv_str;
5891 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005892 else
5893 {
5894 ok = FALSE;
5895 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005896 convert_setup(&vc, NULL, NULL);
5897 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005898 else
5899 {
5900 ok = FALSE;
5901 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005902 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005903
5904 /* Do not store the string if conversion failed. Better to use any
5905 * other selection than garbled text. */
5906 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005907#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005908 {
5909 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5910 XFlush(dpy);
5911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 }
5913
5914 vim_free(str);
5915}
5916#endif
5917
5918 void
5919clip_free_selection(cbd)
5920 VimClipboard *cbd;
5921{
5922 struct yankreg *y_ptr = y_current;
5923
5924 if (cbd == &clip_plus)
5925 y_current = &y_regs[PLUS_REGISTER];
5926 else
5927 y_current = &y_regs[STAR_REGISTER];
5928 free_yank_all();
5929 y_current->y_size = 0;
5930 y_current = y_ptr;
5931}
5932
5933/*
5934 * Get the selected text and put it in the gui selection register '*' or '+'.
5935 */
5936 void
5937clip_get_selection(cbd)
5938 VimClipboard *cbd;
5939{
5940 struct yankreg *old_y_previous, *old_y_current;
5941 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 pos_T old_visual;
5943 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 colnr_T old_curswant;
5945 int old_set_curswant;
5946 pos_T old_op_start, old_op_end;
5947 oparg_T oa;
5948 cmdarg_T ca;
5949
5950 if (cbd->owned)
5951 {
5952 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5953 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5954 return;
5955
5956 /* Get the text between clip_star.start & clip_star.end */
5957 old_y_previous = y_previous;
5958 old_y_current = y_current;
5959 old_cursor = curwin->w_cursor;
5960 old_curswant = curwin->w_curswant;
5961 old_set_curswant = curwin->w_set_curswant;
5962 old_op_start = curbuf->b_op_start;
5963 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 old_visual = VIsual;
5965 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 clear_oparg(&oa);
5967 oa.regname = (cbd == &clip_plus ? '+' : '*');
5968 oa.op_type = OP_YANK;
5969 vim_memset(&ca, 0, sizeof(ca));
5970 ca.oap = &oa;
5971 ca.cmdchar = 'y';
5972 ca.count1 = 1;
5973 ca.retval = CA_NO_ADJ_OP_END;
5974 do_pending_operator(&ca, 0, TRUE);
5975 y_previous = old_y_previous;
5976 y_current = old_y_current;
5977 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005978 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 curwin->w_curswant = old_curswant;
5980 curwin->w_set_curswant = old_set_curswant;
5981 curbuf->b_op_start = old_op_start;
5982 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 VIsual = old_visual;
5984 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986 else
5987 {
5988 clip_free_selection(cbd);
5989
5990 /* Try to get selected text from another window */
5991 clip_gen_request_selection(cbd);
5992 }
5993}
5994
Bram Moolenaard44347f2011-06-19 01:14:29 +02005995/*
5996 * Convert from the GUI selection string into the '*'/'+' register.
5997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 void
5999clip_yank_selection(type, str, len, cbd)
6000 int type;
6001 char_u *str;
6002 long len;
6003 VimClipboard *cbd;
6004{
6005 struct yankreg *y_ptr;
6006
6007 if (cbd == &clip_plus)
6008 y_ptr = &y_regs[PLUS_REGISTER];
6009 else
6010 y_ptr = &y_regs[STAR_REGISTER];
6011
6012 clip_free_selection(cbd);
6013
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006014 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015}
6016
6017/*
6018 * Convert the '*'/'+' register into a GUI selection string returned in *str
6019 * with length *len.
6020 * Returns the motion type, or -1 for failure.
6021 */
6022 int
6023clip_convert_selection(str, len, cbd)
6024 char_u **str;
6025 long_u *len;
6026 VimClipboard *cbd;
6027{
6028 char_u *p;
6029 int lnum;
6030 int i, j;
6031 int_u eolsize;
6032 struct yankreg *y_ptr;
6033
6034 if (cbd == &clip_plus)
6035 y_ptr = &y_regs[PLUS_REGISTER];
6036 else
6037 y_ptr = &y_regs[STAR_REGISTER];
6038
6039#ifdef USE_CRNL
6040 eolsize = 2;
6041#else
6042 eolsize = 1;
6043#endif
6044
6045 *str = NULL;
6046 *len = 0;
6047 if (y_ptr->y_array == NULL)
6048 return -1;
6049
6050 for (i = 0; i < y_ptr->y_size; i++)
6051 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6052
6053 /*
6054 * Don't want newline character at end of last line if we're in MCHAR mode.
6055 */
6056 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6057 *len -= eolsize;
6058
6059 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6060 if (p == NULL)
6061 return -1;
6062 lnum = 0;
6063 for (i = 0, j = 0; i < (int)*len; i++, j++)
6064 {
6065 if (y_ptr->y_array[lnum][j] == '\n')
6066 p[i] = NUL;
6067 else if (y_ptr->y_array[lnum][j] == NUL)
6068 {
6069#ifdef USE_CRNL
6070 p[i++] = '\r';
6071#endif
6072#ifdef USE_CR
6073 p[i] = '\r';
6074#else
6075 p[i] = '\n';
6076#endif
6077 lnum++;
6078 j = -1;
6079 }
6080 else
6081 p[i] = y_ptr->y_array[lnum][j];
6082 }
6083 return y_ptr->y_type;
6084}
6085
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087/*
6088 * If we have written to a clipboard register, send the text to the clipboard.
6089 */
6090 static void
6091may_set_selection()
6092{
6093 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6094 {
6095 clip_own_selection(&clip_star);
6096 clip_gen_set_selection(&clip_star);
6097 }
6098 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6099 {
6100 clip_own_selection(&clip_plus);
6101 clip_gen_set_selection(&clip_plus);
6102 }
6103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105#endif /* FEAT_CLIPBOARD || PROTO */
6106
6107
6108#if defined(FEAT_DND) || defined(PROTO)
6109/*
6110 * Replace the contents of the '~' register with str.
6111 */
6112 void
6113dnd_yank_drag_data(str, len)
6114 char_u *str;
6115 long len;
6116{
6117 struct yankreg *curr;
6118
6119 curr = y_current;
6120 y_current = &y_regs[TILDE_REGISTER];
6121 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006122 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 y_current = curr;
6124}
6125#endif
6126
6127
6128#if defined(FEAT_EVAL) || defined(PROTO)
6129/*
6130 * Return the type of a register.
6131 * Used for getregtype()
6132 * Returns MAUTO for error.
6133 */
6134 char_u
6135get_reg_type(regname, reglen)
6136 int regname;
6137 long *reglen;
6138{
6139 switch (regname)
6140 {
6141 case '%': /* file name */
6142 case '#': /* alternate file name */
6143 case '=': /* expression */
6144 case ':': /* last command line */
6145 case '/': /* last search-pattern */
6146 case '.': /* last inserted text */
6147#ifdef FEAT_SEARCHPATH
6148 case Ctrl_F: /* Filename under cursor */
6149 case Ctrl_P: /* Path under cursor, expand via "path" */
6150#endif
6151 case Ctrl_W: /* word under cursor */
6152 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6153 case '_': /* black hole: always empty */
6154 return MCHAR;
6155 }
6156
6157#ifdef FEAT_CLIPBOARD
6158 regname = may_get_selection(regname);
6159#endif
6160
Bram Moolenaar32b92012014-01-14 12:33:36 +01006161 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6162 return MAUTO;
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 get_yank_register(regname, FALSE);
6165
6166 if (y_current->y_array != NULL)
6167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (reglen != NULL && y_current->y_type == MBLOCK)
6169 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 return y_current->y_type;
6171 }
6172 return MAUTO;
6173}
6174
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006175static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6176
6177/*
6178 * When "flags" has GREG_LIST return a list with text "s".
6179 * Otherwise just return "s".
6180 */
6181 static char_u *
6182getreg_wrap_one_line(s, flags)
6183 char_u *s;
6184 int flags;
6185{
6186 if (flags & GREG_LIST)
6187 {
6188 list_T *list = list_alloc();
6189
6190 if (list != NULL)
6191 {
6192 if (list_append_string(list, NULL, -1) == FAIL)
6193 {
6194 list_free(list, TRUE);
6195 return NULL;
6196 }
6197 list->lv_first->li_tv.vval.v_string = s;
6198 }
6199 return (char_u *)list;
6200 }
6201 return s;
6202}
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204/*
6205 * Return the contents of a register as a single allocated string.
6206 * Used for "@r" in expressions and for getreg().
6207 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006208 * Flags:
6209 * GREG_NO_EXPR Do not allow expression register
6210 * GREG_EXPR_SRC For the expression register: return expression itself,
6211 * not the result of its evaluation.
6212 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 */
6214 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006215get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006217 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 long i;
6220 char_u *retval;
6221 int allocated;
6222 long len;
6223
6224 /* Don't allow using an expression register inside an expression */
6225 if (regname == '=')
6226 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006227 if (flags & GREG_NO_EXPR)
6228 return NULL;
6229 if (flags & GREG_EXPR_SRC)
6230 return getreg_wrap_one_line(get_expr_line_src(), flags);
6231 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
6233
6234 if (regname == '@') /* "@@" is used for unnamed register */
6235 regname = '"';
6236
6237 /* check for valid regname */
6238 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6239 return NULL;
6240
6241#ifdef FEAT_CLIPBOARD
6242 regname = may_get_selection(regname);
6243#endif
6244
6245 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6246 {
6247 if (retval == NULL)
6248 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006249 if (allocated)
6250 return getreg_wrap_one_line(retval, flags);
6251 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
6253
6254 get_yank_register(regname, FALSE);
6255 if (y_current->y_array == NULL)
6256 return NULL;
6257
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006258 if (flags & GREG_LIST)
6259 {
6260 list_T *list = list_alloc();
6261 int error = FALSE;
6262
6263 if (list == NULL)
6264 return NULL;
6265 for (i = 0; i < y_current->y_size; ++i)
6266 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6267 error = TRUE;
6268 if (error)
6269 {
6270 list_free(list, TRUE);
6271 return NULL;
6272 }
6273 return (char_u *)list;
6274 }
6275
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 /*
6277 * Compute length of resulting string.
6278 */
6279 len = 0;
6280 for (i = 0; i < y_current->y_size; ++i)
6281 {
6282 len += (long)STRLEN(y_current->y_array[i]);
6283 /*
6284 * Insert a newline between lines and after last line if
6285 * y_type is MLINE.
6286 */
6287 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6288 ++len;
6289 }
6290
6291 retval = lalloc(len + 1, TRUE);
6292
6293 /*
6294 * Copy the lines of the yank register into the string.
6295 */
6296 if (retval != NULL)
6297 {
6298 len = 0;
6299 for (i = 0; i < y_current->y_size; ++i)
6300 {
6301 STRCPY(retval + len, y_current->y_array[i]);
6302 len += (long)STRLEN(retval + len);
6303
6304 /*
6305 * Insert a NL between lines and after the last line if y_type is
6306 * MLINE.
6307 */
6308 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6309 retval[len++] = '\n';
6310 }
6311 retval[len] = NUL;
6312 }
6313
6314 return retval;
6315}
6316
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006317 static int
6318init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6319 int name;
6320 struct yankreg **old_y_previous;
6321 struct yankreg **old_y_current;
6322 int must_append;
6323 int *yank_type UNUSED;
6324{
6325 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6326 {
6327 emsg_invreg(name);
6328 return FAIL;
6329 }
6330
6331 /* Don't want to change the current (unnamed) register */
6332 *old_y_previous = y_previous;
6333 *old_y_current = y_current;
6334
6335 get_yank_register(name, TRUE);
6336 if (!y_append && !must_append)
6337 free_yank_all();
6338 return OK;
6339}
6340
6341 static void
6342finish_write_reg(name, old_y_previous, old_y_current)
6343 int name;
6344 struct yankreg *old_y_previous;
6345 struct yankreg *old_y_current;
6346{
6347# ifdef FEAT_CLIPBOARD
6348 /* Send text of clipboard register to the clipboard. */
6349 may_set_selection();
6350# endif
6351
6352 /* ':let @" = "val"' should change the meaning of the "" register */
6353 if (name != '"')
6354 y_previous = old_y_previous;
6355 y_current = old_y_current;
6356}
6357
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358/*
6359 * Store string "str" in register "name".
6360 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6361 * If "must_append" is TRUE, always append to the register. Otherwise append
6362 * if "name" is an uppercase letter.
6363 * Note: "maxlen" and "must_append" don't work for the "/" register.
6364 * Careful: 'str' is modified, you may have to use a copy!
6365 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6366 */
6367 void
6368write_reg_contents(name, str, maxlen, must_append)
6369 int name;
6370 char_u *str;
6371 int maxlen;
6372 int must_append;
6373{
6374 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6375}
6376
6377 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006378write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6379 int name;
6380 char_u **strings;
6381 int maxlen UNUSED;
6382 int must_append;
6383 int yank_type;
6384 long block_len;
6385{
6386 struct yankreg *old_y_previous, *old_y_current;
6387
6388 if (name == '/'
6389#ifdef FEAT_EVAL
6390 || name == '='
6391#endif
6392 )
6393 {
6394 char_u *s;
6395
6396 if (strings[0] == NULL)
6397 s = (char_u *)"";
6398 else if (strings[1] != NULL)
6399 {
6400 EMSG(_("E883: search pattern and expression register may not "
6401 "contain two or more lines"));
6402 return;
6403 }
6404 else
6405 s = strings[0];
6406 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6407 return;
6408 }
6409
6410 if (name == '_') /* black hole: nothing to do */
6411 return;
6412
6413 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6414 &yank_type) == FAIL)
6415 return;
6416
6417 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6418
6419 finish_write_reg(name, old_y_previous, old_y_current);
6420}
6421
6422 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6424 int name;
6425 char_u *str;
6426 int maxlen;
6427 int must_append;
6428 int yank_type;
6429 long block_len;
6430{
6431 struct yankreg *old_y_previous, *old_y_current;
6432 long len;
6433
Bram Moolenaare7566042005-06-17 22:00:15 +00006434 if (maxlen >= 0)
6435 len = maxlen;
6436 else
6437 len = (long)STRLEN(str);
6438
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 /* Special case: '/' search pattern */
6440 if (name == '/')
6441 {
6442 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6443 return;
6444 }
6445
Bram Moolenaare7566042005-06-17 22:00:15 +00006446#ifdef FEAT_EVAL
6447 if (name == '=')
6448 {
6449 char_u *p, *s;
6450
6451 p = vim_strnsave(str, (int)len);
6452 if (p == NULL)
6453 return;
6454 if (must_append)
6455 {
6456 s = concat_str(get_expr_line_src(), p);
6457 vim_free(p);
6458 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006459 }
6460 set_expr_line(p);
6461 return;
6462 }
6463#endif
6464
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 if (name == '_') /* black hole: nothing to do */
6466 return;
6467
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006468 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6469 &yank_type) == FAIL)
6470 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006472 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006474 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475}
6476#endif /* FEAT_EVAL */
6477
6478#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6479/*
6480 * Put a string into a register. When the register is not empty, the string
6481 * is appended.
6482 */
6483 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006484str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006486 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 char_u *str; /* string to put in register */
6488 long len; /* length of string */
6489 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006490 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006492 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 int lnum;
6494 long start;
6495 long i;
6496 int extra;
6497 int newlines; /* number of lines added */
6498 int extraline = 0; /* extra line at the end */
6499 int append = FALSE; /* append to last line in register */
6500 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006501 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006505 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 y_ptr->y_size = 0;
6507
Bram Moolenaard44347f2011-06-19 01:14:29 +02006508 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006509 type = ((str_list || (len > 0 && (str[len - 1] == NL
6510 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006511 ? MLINE : MCHAR);
6512 else
6513 type = yank_type;
6514
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 /*
6516 * Count the number of lines within the string
6517 */
6518 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006519 if (str_list)
6520 {
6521 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006524 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006526 for (i = 0; i < len; i++)
6527 if (str[i] == '\n')
6528 ++newlines;
6529 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6530 {
6531 extraline = 1;
6532 ++newlines; /* count extra newline at the end */
6533 }
6534 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6535 {
6536 append = TRUE;
6537 --newlines; /* uncount newline when appending first line */
6538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 }
6540
6541 /*
6542 * Allocate an array to hold the pointers to the new register lines.
6543 * If the register was not empty, move the existing lines to the new array.
6544 */
6545 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6546 * sizeof(char_u *), TRUE);
6547 if (pp == NULL) /* out of memory */
6548 return;
6549 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6550 pp[lnum] = y_ptr->y_array[lnum];
6551 vim_free(y_ptr->y_array);
6552 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
6555 /*
6556 * Find the end of each line and save it into the array.
6557 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006558 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006560 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6561 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006562 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006563 pp[lnum] = vim_strnsave(*ss, i);
6564 if (i > maxlen)
6565 maxlen = i;
6566 }
6567 }
6568 else
6569 {
6570 for (start = 0; start < len + extraline; start += i + 1)
6571 {
6572 for (i = start; i < len; ++i) /* find the end of the line */
6573 if (str[i] == '\n')
6574 break;
6575 i -= start; /* i is now length of line */
6576 if (i > maxlen)
6577 maxlen = i;
6578 if (append)
6579 {
6580 --lnum;
6581 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6582 }
6583 else
6584 extra = 0;
6585 s = alloc((unsigned)(i + extra + 1));
6586 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006588 if (extra)
6589 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6590 if (append)
6591 vim_free(y_ptr->y_array[lnum]);
6592 if (i)
6593 mch_memmove(s + extra, str + start, (size_t)i);
6594 extra += i;
6595 s[extra] = NUL;
6596 y_ptr->y_array[lnum++] = s;
6597 while (--extra >= 0)
6598 {
6599 if (*s == NUL)
6600 *s = '\n'; /* replace NUL with newline */
6601 ++s;
6602 }
6603 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 }
6606 y_ptr->y_type = type;
6607 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if (type == MBLOCK)
6609 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6610 else
6611 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612}
6613#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6614
6615 void
6616clear_oparg(oap)
6617 oparg_T *oap;
6618{
6619 vim_memset(oap, 0, sizeof(oparg_T));
6620}
6621
Bram Moolenaar7c626922005-02-07 22:01:03 +00006622static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006625 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 *
6627 * "Words" are counted by looking for boundaries between non-space and
6628 * space characters. (it seems to produce results that match 'wc'.)
6629 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006630 * Return value is byte count; word count for the line is added to "*wc".
6631 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 *
6633 * The function will only examine the first "limit" characters in the
6634 * line, stopping if it encounters an end-of-line (NUL byte). In that
6635 * case, eol_size will be added to the character count to account for
6636 * the size of the EOL character.
6637 */
6638 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006639line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 char_u *line;
6641 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006642 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 long limit;
6644 int eol_size;
6645{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006646 long i;
6647 long words = 0;
6648 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 int is_word = 0;
6650
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006651 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
6653 if (is_word)
6654 {
6655 if (vim_isspace(line[i]))
6656 {
6657 words++;
6658 is_word = 0;
6659 }
6660 }
6661 else if (!vim_isspace(line[i]))
6662 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006663 ++chars;
6664#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006665 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006666#else
6667 ++i;
6668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
6670
6671 if (is_word)
6672 words++;
6673 *wc += words;
6674
6675 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006676 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006679 chars += eol_size;
6680 }
6681 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 return i;
6683}
6684
6685/*
6686 * Give some info about the position of the cursor (for "g CTRL-G").
6687 * In Visual mode, give some info about the selected region. (In this case,
6688 * the *_count_cursor variables store running totals for the selection.)
6689 */
6690 void
6691cursor_pos_info()
6692{
6693 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006694 char_u buf1[50];
6695 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006697 long byte_count = 0;
6698 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 long char_count = 0;
6700 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 long word_count = 0;
6702 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006703 int eol_size;
6704 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 long line_count_selected = 0;
6706 pos_T min_pos, max_pos;
6707 oparg_T oparg;
6708 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 /*
6711 * Compute the length of the file in characters.
6712 */
6713 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6714 {
6715 MSG(_(no_lines_msg));
6716 }
6717 else
6718 {
6719 if (get_fileformat(curbuf) == EOL_DOS)
6720 eol_size = 2;
6721 else
6722 eol_size = 1;
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (VIsual_active)
6725 {
6726 if (lt(VIsual, curwin->w_cursor))
6727 {
6728 min_pos = VIsual;
6729 max_pos = curwin->w_cursor;
6730 }
6731 else
6732 {
6733 min_pos = curwin->w_cursor;
6734 max_pos = VIsual;
6735 }
6736 if (*p_sel == 'e' && max_pos.col > 0)
6737 --max_pos.col;
6738
6739 if (VIsual_mode == Ctrl_V)
6740 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006741#ifdef FEAT_LINEBREAK
6742 char_u * saved_sbr = p_sbr;
6743
6744 /* Make 'sbr' empty for a moment to get the correct size. */
6745 p_sbr = empty_option;
6746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 oparg.is_VIsual = 1;
6748 oparg.block_mode = TRUE;
6749 oparg.op_type = OP_NOP;
6750 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006751 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006752#ifdef FEAT_LINEBREAK
6753 p_sbr = saved_sbr;
6754#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006755 if (curwin->w_curswant == MAXCOL)
6756 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 /* Swap the start, end vcol if needed */
6758 if (oparg.end_vcol < oparg.start_vcol)
6759 {
6760 oparg.end_vcol += oparg.start_vcol;
6761 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6762 oparg.end_vcol -= oparg.start_vcol;
6763 }
6764 }
6765 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
6768 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6769 {
6770 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006771 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
6773 ui_breakcheck();
6774 if (got_int)
6775 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006776 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 }
6778
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 /* Do extra processing for VIsual mode. */
6780 if (VIsual_active
6781 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6782 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006783 char_u *s = NULL;
6784 long len = 0L;
6785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 switch (VIsual_mode)
6787 {
6788 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006789#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006793#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006795#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006796 s = bd.textstart;
6797 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 break;
6799 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006800 s = ml_get(lnum);
6801 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 break;
6803 case 'v':
6804 {
6805 colnr_T start_col = (lnum == min_pos.lnum)
6806 ? min_pos.col : 0;
6807 colnr_T end_col = (lnum == max_pos.lnum)
6808 ? max_pos.col - start_col + 1 : MAXCOL;
6809
Bram Moolenaardef9e822004-12-31 20:58:58 +00006810 s = ml_get(lnum) + start_col;
6811 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 }
6813 break;
6814 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006815 if (s != NULL)
6816 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006817 byte_count_cursor += line_count_info(s, &word_count_cursor,
6818 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006819 if (lnum == curbuf->b_ml.ml_line_count
6820 && !curbuf->b_p_eol
6821 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006822 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006823 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
6826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
6828 /* In non-visual mode, check for the line the cursor is on */
6829 if (lnum == curwin->w_cursor.lnum)
6830 {
6831 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006832 char_count_cursor += char_count;
6833 byte_count_cursor = byte_count +
6834 line_count_info(ml_get(lnum),
6835 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 (long)(curwin->w_cursor.col + 1), eol_size);
6837 }
6838 }
6839 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006840 byte_count += line_count_info(ml_get(lnum), &word_count,
6841 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 }
6843
6844 /* Correction for when last line doesn't have an EOL. */
6845 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006846 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (VIsual_active)
6849 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006850 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {
6852 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006853 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006854 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6856 }
6857 else
6858 buf1[0] = NUL;
6859
Bram Moolenaar7c626922005-02-07 22:01:03 +00006860 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006861 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006862 vim_snprintf((char *)IObuff, IOSIZE,
6863 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 buf1, line_count_selected,
6865 (long)curbuf->b_ml.ml_line_count,
6866 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006867 byte_count_cursor, byte_count);
6868 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006869 vim_snprintf((char *)IObuff, IOSIZE,
6870 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006871 buf1, line_count_selected,
6872 (long)curbuf->b_ml.ml_line_count,
6873 word_count_cursor, word_count,
6874 char_count_cursor, char_count,
6875 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
6877 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
6879 p = ml_get_curline();
6880 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006881 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 (int)curwin->w_virtcol + 1);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006883 col_print(buf2, sizeof(buf2), (int)STRLEN(p), linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
Bram Moolenaar7c626922005-02-07 22:01:03 +00006885 if (char_count_cursor == byte_count_cursor
6886 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006887 vim_snprintf((char *)IObuff, IOSIZE,
6888 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 (char *)buf1, (char *)buf2,
6890 (long)curwin->w_cursor.lnum,
6891 (long)curbuf->b_ml.ml_line_count,
6892 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006893 byte_count_cursor, byte_count);
6894 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006895 vim_snprintf((char *)IObuff, IOSIZE,
6896 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006897 (char *)buf1, (char *)buf2,
6898 (long)curwin->w_cursor.lnum,
6899 (long)curbuf->b_ml.ml_line_count,
6900 word_count_cursor, word_count,
6901 char_count_cursor, char_count,
6902 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904
6905#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006906 byte_count = bomb_size();
6907 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006909 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910#endif
6911 /* Don't shorten this message, the user asked for it. */
6912 p = p_shm;
6913 p_shm = (char_u *)"";
6914 msg(IObuff);
6915 p_shm = p;
6916 }
6917}