blob: f5bf0f8f302dc25d8c76e921319c9f8a888294f3 [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 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003153 if (curwin->w_p_rnu)
3154 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (mess) /* Display message about yank? */
3156 {
3157 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 && yanklines == 1)
3160 yanklines = 0;
3161 /* Some versions of Vi use ">=" here, some don't... */
3162 if (yanklines > p_report)
3163 {
3164 /* redisplay now, so message is not deleted */
3165 update_topline_redraw();
3166 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003167 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003168 if (oap->block_mode)
3169 MSG(_("block of 1 line yanked"));
3170 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003171 MSG(_("1 line yanked"));
3172 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003173 else if (oap->block_mode)
3174 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 else
3176 smsg((char_u *)_("%ld lines yanked"), yanklines);
3177 }
3178 }
3179
3180 /*
3181 * Set "'[" and "']" marks.
3182 */
3183 curbuf->b_op_start = oap->start;
3184 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003185 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003186 {
3187 curbuf->b_op_start.col = 0;
3188 curbuf->b_op_end.col = MAXCOL;
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191#ifdef FEAT_CLIPBOARD
3192 /*
3193 * If we were yanking to the '*' register, send result to clipboard.
3194 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3195 * to the '*' register.
3196 */
3197 if (clip_star.available
3198 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003199 || (!deleting && oap->regname == 0
3200 && (clip_unnamed & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
3202 if (curr != &(y_regs[STAR_REGISTER]))
3203 /* Copy the text from register 0 to the clipboard register. */
3204 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3205
3206 clip_own_selection(&clip_star);
3207 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003208# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003209 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212
3213# ifdef FEAT_X11
3214 /*
3215 * If we were yanking to the '+' register, send result to selection.
3216 * Also copy to the '*' register, in case auto-select is off.
3217 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003218 if (clip_plus.available
3219 && (curr == &(y_regs[PLUS_REGISTER])
3220 || (!deleting && oap->regname == 0
3221 && (clip_unnamed & CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003223 if (curr != &(y_regs[PLUS_REGISTER]))
3224 /* Copy the text from register 0 to the clipboard register. */
3225 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 clip_own_selection(&clip_plus);
3228 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003229 if (!clip_isautosel_star() && !did_star
3230 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3233 clip_own_selection(&clip_star);
3234 clip_gen_set_selection(&clip_star);
3235 }
3236 }
3237# endif
3238#endif
3239
3240 return OK;
3241
3242fail: /* free the allocated lines */
3243 free_yank(y_idx + 1);
3244 y_current = curr;
3245 return FAIL;
3246}
3247
3248 static int
3249yank_copy_line(bd, y_idx)
3250 struct block_def *bd;
3251 long y_idx;
3252{
3253 char_u *pnew;
3254
3255 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3256 == NULL)
3257 return FAIL;
3258 y_current->y_array[y_idx] = pnew;
3259 copy_spaces(pnew, (size_t)bd->startspaces);
3260 pnew += bd->startspaces;
3261 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3262 pnew += bd->textlen;
3263 copy_spaces(pnew, (size_t)bd->endspaces);
3264 pnew += bd->endspaces;
3265 *pnew = NUL;
3266 return OK;
3267}
3268
3269#ifdef FEAT_CLIPBOARD
3270/*
3271 * Make a copy of the y_current register to register "reg".
3272 */
3273 static void
3274copy_yank_reg(reg)
3275 struct yankreg *reg;
3276{
3277 struct yankreg *curr = y_current;
3278 long j;
3279
3280 y_current = reg;
3281 free_yank_all();
3282 *y_current = *curr;
3283 y_current->y_array = (char_u **)lalloc_clear(
3284 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3285 if (y_current->y_array == NULL)
3286 y_current->y_size = 0;
3287 else
3288 for (j = 0; j < y_current->y_size; ++j)
3289 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3290 {
3291 free_yank(j);
3292 y_current->y_size = 0;
3293 break;
3294 }
3295 y_current = curr;
3296}
3297#endif
3298
3299/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003300 * Put contents of register "regname" into the text.
3301 * Caller must check "regname" to be valid!
3302 * "flags": PUT_FIXINDENT make indent look nice
3303 * PUT_CURSEND leave cursor after end of new text
3304 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 */
3306 void
3307do_put(regname, dir, count, flags)
3308 int regname;
3309 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3310 long count;
3311 int flags;
3312{
3313 char_u *ptr;
3314 char_u *newp, *oldp;
3315 int yanklen;
3316 int totlen = 0; /* init for gcc */
3317 linenr_T lnum;
3318 colnr_T col;
3319 long i; /* index in y_array[] */
3320 int y_type;
3321 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 int oldlen;
3323 long y_width = 0;
3324 colnr_T vcol;
3325 int delcount;
3326 int incr = 0;
3327 long j;
3328 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 char_u **y_array = NULL;
3330 long nr_lines = 0;
3331 pos_T new_cursor;
3332 int indent;
3333 int orig_indent = 0; /* init for gcc */
3334 int indent_diff = 0; /* init for gcc */
3335 int first_indent = TRUE;
3336 int lendiff = 0;
3337 pos_T old_pos;
3338 char_u *insert_string = NULL;
3339 int allocated = FALSE;
3340 long cnt;
3341
3342#ifdef FEAT_CLIPBOARD
3343 /* Adjust register name for "unnamed" in 'clipboard'. */
3344 adjust_clip_reg(&regname);
3345 (void)may_get_selection(regname);
3346#endif
3347
3348 if (flags & PUT_FIXINDENT)
3349 orig_indent = get_indent();
3350
3351 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3352 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3353
3354 /*
3355 * Using inserted text works differently, because the register includes
3356 * special characters (newlines, etc.).
3357 */
3358 if (regname == '.')
3359 {
3360 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3361 (count == -1 ? 'O' : 'i')), count, FALSE);
3362 /* Putting the text is done later, so can't really move the cursor to
3363 * the next character. Use "l" to simulate it. */
3364 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3365 stuffcharReadbuff('l');
3366 return;
3367 }
3368
3369 /*
3370 * For special registers '%' (file name), '#' (alternate file name) and
3371 * ':' (last command line), etc. we have to create a fake yank register.
3372 */
3373 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3374 {
3375 if (insert_string == NULL)
3376 return;
3377 }
3378
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003379#ifdef FEAT_AUTOCMD
3380 /* Autocommands may be executed when saving lines for undo, which may make
3381 * y_array invalid. Start undo now to avoid that. */
3382 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3383#endif
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (insert_string != NULL)
3386 {
3387 y_type = MCHAR;
3388#ifdef FEAT_EVAL
3389 if (regname == '=')
3390 {
3391 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003392 * characters.
3393 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 for (;;)
3395 {
3396 y_size = 0;
3397 ptr = insert_string;
3398 while (ptr != NULL)
3399 {
3400 if (y_array != NULL)
3401 y_array[y_size] = ptr;
3402 ++y_size;
3403 ptr = vim_strchr(ptr, '\n');
3404 if (ptr != NULL)
3405 {
3406 if (y_array != NULL)
3407 *ptr = NUL;
3408 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003409 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (*ptr == NUL)
3411 {
3412 y_type = MLINE;
3413 break;
3414 }
3415 }
3416 }
3417 if (y_array != NULL)
3418 break;
3419 y_array = (char_u **)alloc((unsigned)
3420 (y_size * sizeof(char_u *)));
3421 if (y_array == NULL)
3422 goto end;
3423 }
3424 }
3425 else
3426#endif
3427 {
3428 y_size = 1; /* use fake one-line yank register */
3429 y_array = &insert_string;
3430 }
3431 }
3432 else
3433 {
3434 get_yank_register(regname, FALSE);
3435
3436 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 y_size = y_current->y_size;
3439 y_array = y_current->y_array;
3440 }
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (y_type == MLINE)
3443 {
3444 if (flags & PUT_LINE_SPLIT)
3445 {
3446 /* "p" or "P" in Visual mode: split the lines to put the text in
3447 * between. */
3448 if (u_save_cursor() == FAIL)
3449 goto end;
3450 ptr = vim_strsave(ml_get_cursor());
3451 if (ptr == NULL)
3452 goto end;
3453 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3454 vim_free(ptr);
3455
3456 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3457 if (ptr == NULL)
3458 goto end;
3459 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3460 ++nr_lines;
3461 dir = FORWARD;
3462 }
3463 if (flags & PUT_LINE_FORWARD)
3464 {
3465 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003466 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 dir = FORWARD;
3468 }
3469 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3470 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3474 y_type = MLINE;
3475
3476 if (y_size == 0 || y_array == NULL)
3477 {
3478 EMSG2(_("E353: Nothing in register %s"),
3479 regname == 0 ? (char_u *)"\"" : transchar(regname));
3480 goto end;
3481 }
3482
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (y_type == MBLOCK)
3484 {
3485 lnum = curwin->w_cursor.lnum + y_size + 1;
3486 if (lnum > curbuf->b_ml.ml_line_count)
3487 lnum = curbuf->b_ml.ml_line_count + 1;
3488 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3489 goto end;
3490 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003491 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 lnum = curwin->w_cursor.lnum;
3494#ifdef FEAT_FOLDING
3495 /* Correct line number for closed fold. Don't move the cursor yet,
3496 * u_save() uses it. */
3497 if (dir == BACKWARD)
3498 (void)hasFolding(lnum, &lnum, NULL);
3499 else
3500 (void)hasFolding(lnum, NULL, &lnum);
3501#endif
3502 if (dir == FORWARD)
3503 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003504 /* In an empty buffer the empty line is going to be replaced, include
3505 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003506 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 goto end;
3508#ifdef FEAT_FOLDING
3509 if (dir == FORWARD)
3510 curwin->w_cursor.lnum = lnum - 1;
3511 else
3512 curwin->w_cursor.lnum = lnum;
3513 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3514#endif
3515 }
3516 else if (u_save_cursor() == FAIL)
3517 goto end;
3518
3519 yanklen = (int)STRLEN(y_array[0]);
3520
3521#ifdef FEAT_VIRTUALEDIT
3522 if (ve_flags == VE_ALL && y_type == MCHAR)
3523 {
3524 if (gchar_cursor() == TAB)
3525 {
3526 /* Don't need to insert spaces when "p" on the last position of a
3527 * tab or "P" on the first position. */
3528 if (dir == FORWARD
3529 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3530 : curwin->w_cursor.coladd > 0)
3531 coladvance_force(getviscol());
3532 else
3533 curwin->w_cursor.coladd = 0;
3534 }
3535 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3536 coladvance_force(getviscol() + (dir == FORWARD));
3537 }
3538#endif
3539
3540 lnum = curwin->w_cursor.lnum;
3541 col = curwin->w_cursor.col;
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /*
3544 * Block mode
3545 */
3546 if (y_type == MBLOCK)
3547 {
3548 char c = gchar_cursor();
3549 colnr_T endcol2 = 0;
3550
3551 if (dir == FORWARD && c != NUL)
3552 {
3553#ifdef FEAT_VIRTUALEDIT
3554 if (ve_flags == VE_ALL)
3555 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3556 else
3557#endif
3558 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3559
3560#ifdef FEAT_MBYTE
3561 if (has_mbyte)
3562 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003563 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 else
3565#endif
3566#ifdef FEAT_VIRTUALEDIT
3567 if (c != TAB || ve_flags != VE_ALL)
3568#endif
3569 ++curwin->w_cursor.col;
3570 ++col;
3571 }
3572 else
3573 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3574
3575#ifdef FEAT_VIRTUALEDIT
3576 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003577 if (ve_flags == VE_ALL
3578 && (curwin->w_cursor.coladd > 0
3579 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 if (dir == FORWARD && c == NUL)
3582 ++col;
3583 if (dir != FORWARD && c != NUL)
3584 ++curwin->w_cursor.col;
3585 if (c == TAB)
3586 {
3587 if (dir == BACKWARD && curwin->w_cursor.col)
3588 curwin->w_cursor.col--;
3589 if (dir == FORWARD && col - 1 == endcol2)
3590 curwin->w_cursor.col++;
3591 }
3592 }
3593 curwin->w_cursor.coladd = 0;
3594#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003595 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 for (i = 0; i < y_size; ++i)
3597 {
3598 int spaces;
3599 char shortline;
3600
3601 bd.startspaces = 0;
3602 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 vcol = 0;
3604 delcount = 0;
3605
3606 /* add a new line */
3607 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3608 {
3609 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3610 (colnr_T)1, FALSE) == FAIL)
3611 break;
3612 ++nr_lines;
3613 }
3614 /* get the old line and advance to the position to insert at */
3615 oldp = ml_get_curline();
3616 oldlen = (int)STRLEN(oldp);
3617 for (ptr = oldp; vcol < col && *ptr; )
3618 {
3619 /* Count a tab for what it's worth (if list mode not on) */
3620 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3621 vcol += incr;
3622 }
3623 bd.textcol = (colnr_T)(ptr - oldp);
3624
3625 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3626
3627 if (vcol < col) /* line too short, padd with spaces */
3628 bd.startspaces = col - vcol;
3629 else if (vcol > col)
3630 {
3631 bd.endspaces = vcol - col;
3632 bd.startspaces = incr - bd.endspaces;
3633 --bd.textcol;
3634 delcount = 1;
3635#ifdef FEAT_MBYTE
3636 if (has_mbyte)
3637 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3638#endif
3639 if (oldp[bd.textcol] != TAB)
3640 {
3641 /* Only a Tab can be split into spaces. Other
3642 * characters will have to be moved to after the
3643 * block, causing misalignment. */
3644 delcount = 0;
3645 bd.endspaces = 0;
3646 }
3647 }
3648
3649 yanklen = (int)STRLEN(y_array[i]);
3650
3651 /* calculate number of spaces required to fill right side of block*/
3652 spaces = y_width + 1;
3653 for (j = 0; j < yanklen; j++)
3654 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3655 if (spaces < 0)
3656 spaces = 0;
3657
3658 /* insert the new text */
3659 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3660 newp = alloc_check((unsigned)totlen + oldlen + 1);
3661 if (newp == NULL)
3662 break;
3663 /* copy part up to cursor to new line */
3664 ptr = newp;
3665 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3666 ptr += bd.textcol;
3667 /* may insert some spaces before the new text */
3668 copy_spaces(ptr, (size_t)bd.startspaces);
3669 ptr += bd.startspaces;
3670 /* insert the new text */
3671 for (j = 0; j < count; ++j)
3672 {
3673 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3674 ptr += yanklen;
3675
3676 /* insert block's trailing spaces only if there's text behind */
3677 if ((j < count - 1 || !shortline) && spaces)
3678 {
3679 copy_spaces(ptr, (size_t)spaces);
3680 ptr += spaces;
3681 }
3682 }
3683 /* may insert some spaces after the new text */
3684 copy_spaces(ptr, (size_t)bd.endspaces);
3685 ptr += bd.endspaces;
3686 /* move the text after the cursor to the end of the line. */
3687 mch_memmove(ptr, oldp + bd.textcol + delcount,
3688 (size_t)(oldlen - bd.textcol - delcount + 1));
3689 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3690
3691 ++curwin->w_cursor.lnum;
3692 if (i == 0)
3693 curwin->w_cursor.col += bd.startspaces;
3694 }
3695
3696 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3697
3698 /* Set '[ mark. */
3699 curbuf->b_op_start = curwin->w_cursor;
3700 curbuf->b_op_start.lnum = lnum;
3701
3702 /* adjust '] mark */
3703 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3704 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003705# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (flags & PUT_CURSEND)
3709 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003710 colnr_T len;
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 curwin->w_cursor = curbuf->b_op_end;
3713 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003714
3715 /* in Insert mode we might be after the NUL, correct for that */
3716 len = (colnr_T)STRLEN(ml_get_curline());
3717 if (curwin->w_cursor.col > len)
3718 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else
3721 curwin->w_cursor.lnum = lnum;
3722 }
3723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 /*
3726 * Character or Line mode
3727 */
3728 if (y_type == MCHAR)
3729 {
3730 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3731 * char */
3732 if (dir == FORWARD && gchar_cursor() != NUL)
3733 {
3734#ifdef FEAT_MBYTE
3735 if (has_mbyte)
3736 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003737 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
3739 /* put it on the next of the multi-byte character. */
3740 col += bytelen;
3741 if (yanklen)
3742 {
3743 curwin->w_cursor.col += bytelen;
3744 curbuf->b_op_end.col += bytelen;
3745 }
3746 }
3747 else
3748#endif
3749 {
3750 ++col;
3751 if (yanklen)
3752 {
3753 ++curwin->w_cursor.col;
3754 ++curbuf->b_op_end.col;
3755 }
3756 }
3757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 curbuf->b_op_start = curwin->w_cursor;
3759 }
3760 /*
3761 * Line mode: BACKWARD is the same as FORWARD on the previous line
3762 */
3763 else if (dir == BACKWARD)
3764 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003765 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 /*
3768 * simple case: insert into current line
3769 */
3770 if (y_type == MCHAR && y_size == 1)
3771 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003772 do {
3773 totlen = count * yanklen;
3774 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003776 oldp = ml_get(lnum);
3777 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3778 if (newp == NULL)
3779 goto end; /* alloc() gave an error message */
3780 mch_memmove(newp, oldp, (size_t)col);
3781 ptr = newp + col;
3782 for (i = 0; i < count; ++i)
3783 {
3784 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3785 ptr += yanklen;
3786 }
3787 STRMOVE(ptr, oldp + col);
3788 ml_replace(lnum, newp, FALSE);
3789 /* Place cursor on last putted char. */
3790 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003791 {
3792 /* make sure curwin->w_virtcol is updated */
3793 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003794 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003797 if (VIsual_active)
3798 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003799 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 curbuf->b_op_end = curwin->w_cursor;
3802 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3803 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3804 ++curwin->w_cursor.col;
3805 changed_bytes(lnum, col);
3806 }
3807 else
3808 {
3809 /*
3810 * Insert at least one line. When y_type is MCHAR, break the first
3811 * line in two.
3812 */
3813 for (cnt = 1; cnt <= count; ++cnt)
3814 {
3815 i = 0;
3816 if (y_type == MCHAR)
3817 {
3818 /*
3819 * Split the current line in two at the insert position.
3820 * First insert y_array[size - 1] in front of second line.
3821 * Then append y_array[0] to first line.
3822 */
3823 lnum = new_cursor.lnum;
3824 ptr = ml_get(lnum) + col;
3825 totlen = (int)STRLEN(y_array[y_size - 1]);
3826 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3827 if (newp == NULL)
3828 goto error;
3829 STRCPY(newp, y_array[y_size - 1]);
3830 STRCAT(newp, ptr);
3831 /* insert second line */
3832 ml_append(lnum, newp, (colnr_T)0, FALSE);
3833 vim_free(newp);
3834
3835 oldp = ml_get(lnum);
3836 newp = alloc_check((unsigned)(col + yanklen + 1));
3837 if (newp == NULL)
3838 goto error;
3839 /* copy first part of line */
3840 mch_memmove(newp, oldp, (size_t)col);
3841 /* append to first line */
3842 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3843 ml_replace(lnum, newp, FALSE);
3844
3845 curwin->w_cursor.lnum = lnum;
3846 i = 1;
3847 }
3848
3849 for (; i < y_size; ++i)
3850 {
3851 if ((y_type != MCHAR || i < y_size - 1)
3852 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3853 == FAIL)
3854 goto error;
3855 lnum++;
3856 ++nr_lines;
3857 if (flags & PUT_FIXINDENT)
3858 {
3859 old_pos = curwin->w_cursor;
3860 curwin->w_cursor.lnum = lnum;
3861 ptr = ml_get(lnum);
3862 if (cnt == count && i == y_size - 1)
3863 lendiff = (int)STRLEN(ptr);
3864#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3865 if (*ptr == '#' && preprocs_left())
3866 indent = 0; /* Leave # lines at start */
3867 else
3868#endif
3869 if (*ptr == NUL)
3870 indent = 0; /* Ignore empty lines */
3871 else if (first_indent)
3872 {
3873 indent_diff = orig_indent - get_indent();
3874 indent = orig_indent;
3875 first_indent = FALSE;
3876 }
3877 else if ((indent = get_indent() + indent_diff) < 0)
3878 indent = 0;
3879 (void)set_indent(indent, 0);
3880 curwin->w_cursor = old_pos;
3881 /* remember how many chars were removed */
3882 if (cnt == count && i == y_size - 1)
3883 lendiff -= (int)STRLEN(ml_get(lnum));
3884 }
3885 }
3886 }
3887
3888error:
3889 /* Adjust marks. */
3890 if (y_type == MLINE)
3891 {
3892 curbuf->b_op_start.col = 0;
3893 if (dir == FORWARD)
3894 curbuf->b_op_start.lnum++;
3895 }
3896 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3897 (linenr_T)MAXLNUM, nr_lines, 0L);
3898
3899 /* note changed text for displaying and folding */
3900 if (y_type == MCHAR)
3901 changed_lines(curwin->w_cursor.lnum, col,
3902 curwin->w_cursor.lnum + 1, nr_lines);
3903 else
3904 changed_lines(curbuf->b_op_start.lnum, 0,
3905 curbuf->b_op_start.lnum, nr_lines);
3906
3907 /* put '] mark at last inserted character */
3908 curbuf->b_op_end.lnum = lnum;
3909 /* correct length for change in indent */
3910 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3911 if (col > 1)
3912 curbuf->b_op_end.col = col - 1;
3913 else
3914 curbuf->b_op_end.col = 0;
3915
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003916 if (flags & PUT_CURSLINE)
3917 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003918 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003919 curwin->w_cursor.lnum = lnum;
3920 beginline(BL_WHITE | BL_FIX);
3921 }
3922 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
3924 /* put cursor after inserted text */
3925 if (y_type == MLINE)
3926 {
3927 if (lnum >= curbuf->b_ml.ml_line_count)
3928 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3929 else
3930 curwin->w_cursor.lnum = lnum + 1;
3931 curwin->w_cursor.col = 0;
3932 }
3933 else
3934 {
3935 curwin->w_cursor.lnum = lnum;
3936 curwin->w_cursor.col = col;
3937 }
3938 }
3939 else if (y_type == MLINE)
3940 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003941 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 curwin->w_cursor.col = 0;
3943 if (dir == FORWARD)
3944 ++curwin->w_cursor.lnum;
3945 beginline(BL_WHITE | BL_FIX);
3946 }
3947 else /* put cursor on first inserted character */
3948 curwin->w_cursor = new_cursor;
3949 }
3950 }
3951
3952 msgmore(nr_lines);
3953 curwin->w_set_curswant = TRUE;
3954
3955end:
3956 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003958 if (regname == '=')
3959 vim_free(y_array);
3960
Bram Moolenaar033d8882013-09-25 23:24:57 +02003961 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003962
Bram Moolenaar677ee682005-01-27 14:41:15 +00003963 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003964 adjust_cursor_eol();
3965}
3966
3967/*
3968 * When the cursor is on the NUL past the end of the line and it should not be
3969 * there move it left.
3970 */
3971 void
3972adjust_cursor_eol()
3973{
3974 if (curwin->w_cursor.col > 0
3975 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003976#ifdef FEAT_VIRTUALEDIT
3977 && (ve_flags & VE_ONEMORE) == 0
3978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 && !(restart_edit || (State & INSERT)))
3980 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003981 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003982 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#ifdef FEAT_VIRTUALEDIT
3985 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003986 {
3987 colnr_T scol, ecol;
3988
3989 /* Coladd is set to the width of the last character. */
3990 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3991 curwin->w_cursor.coladd = ecol - scol + 1;
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993#endif
3994 }
3995}
3996
3997#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3998/*
3999 * Return TRUE if lines starting with '#' should be left aligned.
4000 */
4001 int
4002preprocs_left()
4003{
4004 return
4005# ifdef FEAT_SMARTINDENT
4006# ifdef FEAT_CINDENT
4007 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4008# else
4009 curbuf->b_p_si
4010# endif
4011# endif
4012# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004013 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4014 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015# endif
4016 ;
4017}
4018#endif
4019
4020/* Return the character name of the register with the given number */
4021 int
4022get_register_name(num)
4023 int num;
4024{
4025 if (num == -1)
4026 return '"';
4027 else if (num < 10)
4028 return num + '0';
4029 else if (num == DELETION_REGISTER)
4030 return '-';
4031#ifdef FEAT_CLIPBOARD
4032 else if (num == STAR_REGISTER)
4033 return '*';
4034 else if (num == PLUS_REGISTER)
4035 return '+';
4036#endif
4037 else
4038 {
4039#ifdef EBCDIC
4040 int i;
4041
4042 /* EBCDIC is really braindead ... */
4043 i = 'a' + (num - 10);
4044 if (i > 'i')
4045 i += 7;
4046 if (i > 'r')
4047 i += 8;
4048 return i;
4049#else
4050 return num + 'a' - 10;
4051#endif
4052 }
4053}
4054
4055/*
4056 * ":dis" and ":registers": Display the contents of the yank registers.
4057 */
4058 void
4059ex_display(eap)
4060 exarg_T *eap;
4061{
4062 int i, n;
4063 long j;
4064 char_u *p;
4065 struct yankreg *yb;
4066 int name;
4067 int attr;
4068 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004069#ifdef FEAT_MBYTE
4070 int clen;
4071#else
4072# define clen 1
4073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075 if (arg != NULL && *arg == NUL)
4076 arg = NULL;
4077 attr = hl_attr(HLF_8);
4078
4079 /* Highlight title */
4080 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4081 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4082 {
4083 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004084 if (arg != NULL && vim_strchr(arg, name) == NULL
4085#ifdef ONE_CLIPBOARD
4086 /* Star register and plus register contain the same thing. */
4087 && (name != '*' || vim_strchr(arg, '+') == NULL)
4088#endif
4089 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 continue; /* did not ask for this register */
4091
4092#ifdef FEAT_CLIPBOARD
4093 /* Adjust register name for "unnamed" in 'clipboard'.
4094 * When it's a clipboard register, fill it with the current contents
4095 * of the clipboard. */
4096 adjust_clip_reg(&name);
4097 (void)may_get_selection(name);
4098#endif
4099
4100 if (i == -1)
4101 {
4102 if (y_previous != NULL)
4103 yb = y_previous;
4104 else
4105 yb = &(y_regs[0]);
4106 }
4107 else
4108 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004109
4110#ifdef FEAT_EVAL
4111 if (name == MB_TOLOWER(redir_reg)
4112 || (redir_reg == '"' && yb == y_previous))
4113 continue; /* do not list register being written to, the
4114 * pointer can be freed */
4115#endif
4116
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (yb->y_array != NULL)
4118 {
4119 msg_putchar('\n');
4120 msg_putchar('"');
4121 msg_putchar(name);
4122 MSG_PUTS(" ");
4123
4124 n = (int)Columns - 6;
4125 for (j = 0; j < yb->y_size && n > 1; ++j)
4126 {
4127 if (j)
4128 {
4129 MSG_PUTS_ATTR("^J", attr);
4130 n -= 2;
4131 }
4132 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004135 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004136#endif
4137 msg_outtrans_len(p, clen);
4138#ifdef FEAT_MBYTE
4139 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#endif
4141 }
4142 }
4143 if (n > 1 && yb->y_type == MLINE)
4144 MSG_PUTS_ATTR("^J", attr);
4145 out_flush(); /* show one line at a time */
4146 }
4147 ui_breakcheck();
4148 }
4149
4150 /*
4151 * display last inserted text
4152 */
4153 if ((p = get_last_insert()) != NULL
4154 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4155 {
4156 MSG_PUTS("\n\". ");
4157 dis_msg(p, TRUE);
4158 }
4159
4160 /*
4161 * display last command line
4162 */
4163 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4164 && !got_int)
4165 {
4166 MSG_PUTS("\n\": ");
4167 dis_msg(last_cmdline, FALSE);
4168 }
4169
4170 /*
4171 * display current file name
4172 */
4173 if (curbuf->b_fname != NULL
4174 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4175 {
4176 MSG_PUTS("\n\"% ");
4177 dis_msg(curbuf->b_fname, FALSE);
4178 }
4179
4180 /*
4181 * display alternate file name
4182 */
4183 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4184 {
4185 char_u *fname;
4186 linenr_T dummy;
4187
4188 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4189 {
4190 MSG_PUTS("\n\"# ");
4191 dis_msg(fname, FALSE);
4192 }
4193 }
4194
4195 /*
4196 * display last search pattern
4197 */
4198 if (last_search_pat() != NULL
4199 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4200 {
4201 MSG_PUTS("\n\"/ ");
4202 dis_msg(last_search_pat(), FALSE);
4203 }
4204
4205#ifdef FEAT_EVAL
4206 /*
4207 * display last used expression
4208 */
4209 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4210 && !got_int)
4211 {
4212 MSG_PUTS("\n\"= ");
4213 dis_msg(expr_line, FALSE);
4214 }
4215#endif
4216}
4217
4218/*
4219 * display a string for do_dis()
4220 * truncate at end of screen line
4221 */
4222 static void
4223dis_msg(p, skip_esc)
4224 char_u *p;
4225 int skip_esc; /* if TRUE, ignore trailing ESC */
4226{
4227 int n;
4228#ifdef FEAT_MBYTE
4229 int l;
4230#endif
4231
4232 n = (int)Columns - 6;
4233 while (*p != NUL
4234 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4235 && (n -= ptr2cells(p)) >= 0)
4236 {
4237#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004238 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
4240 msg_outtrans_len(p, l);
4241 p += l;
4242 }
4243 else
4244#endif
4245 msg_outtrans_len(p++, 1);
4246 }
4247 ui_breakcheck();
4248}
4249
Bram Moolenaar81340392012-06-06 16:12:59 +02004250#if defined(FEAT_COMMENTS) || defined(PROTO)
4251/*
4252 * If "process" is TRUE and the line begins with a comment leader (possibly
4253 * after some white space), return a pointer to the text after it. Put a boolean
4254 * value indicating whether the line ends with an unclosed comment in
4255 * "is_comment".
4256 * line - line to be processed,
4257 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004258 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004259 * include_space - whether to also skip space following the comment leader,
4260 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004261 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004262 */
4263 static char_u *
4264skip_comment(line, process, include_space, is_comment)
4265 char_u *line;
4266 int process;
4267 int include_space;
4268 int *is_comment;
4269{
4270 char_u *comment_flags = NULL;
4271 int lead_len;
4272 int leader_offset = get_last_leader_offset(line, &comment_flags);
4273
4274 *is_comment = FALSE;
4275 if (leader_offset != -1)
4276 {
4277 /* Let's check whether the line ends with an unclosed comment.
4278 * If the last comment leader has COM_END in flags, there's no comment.
4279 */
4280 while (*comment_flags)
4281 {
4282 if (*comment_flags == COM_END
4283 || *comment_flags == ':')
4284 break;
4285 ++comment_flags;
4286 }
4287 if (*comment_flags != COM_END)
4288 *is_comment = TRUE;
4289 }
4290
4291 if (process == FALSE)
4292 return line;
4293
4294 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4295
4296 if (lead_len == 0)
4297 return line;
4298
4299 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004300 * - COM_END,
4301 * - colon,
4302 * whichever comes first.
4303 */
4304 while (*comment_flags)
4305 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004306 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004307 || *comment_flags == ':')
4308 {
4309 break;
4310 }
4311 ++comment_flags;
4312 }
4313
4314 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004315 * starting with a closing part of a three-part comment. That's good,
4316 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004317 */
4318 if (*comment_flags == ':' || *comment_flags == NUL)
4319 line += lead_len;
4320
4321 return line;
4322}
4323#endif
4324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004326 * Join 'count' lines (minimal 2) at cursor position.
4327 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004328 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4329 * leaders should not be removed.
4330 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4331 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004333 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
4335 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004336do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004337 long count;
4338 int insert_space;
4339 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004340 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004341 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004343 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004344 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004345 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004347 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004348 int endcurr1 = NUL;
4349 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004350 int currsize = 0; /* size of the current line */
4351 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004353 colnr_T col = 0;
4354 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004355#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004356 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004357 int remove_comments = (use_formatoptions == TRUE)
4358 && has_format_option(FO_REMOVE_COMS);
4359 int prev_was_comment;
4360#endif
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004363 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4364 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4365 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004367 /* Allocate an array to store the number of spaces inserted before each
4368 * line. We will use it to pre-compute the length of the new line and the
4369 * proper placement of each original line in the new one. */
4370 spaces = lalloc_clear((long_u)count, TRUE);
4371 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004373#if defined(FEAT_COMMENTS) || defined(PROTO)
4374 if (remove_comments)
4375 {
4376 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4377 if (comments == NULL)
4378 {
4379 vim_free(spaces);
4380 return FAIL;
4381 }
4382 }
4383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004386 * Don't move anything, just compute the final line length
4387 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004389 for (t = 0; t < count; ++t)
4390 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004391 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004392 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004393 {
4394 /* Set the '[ mark. */
4395 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4396 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4397 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004398#if defined(FEAT_COMMENTS) || defined(PROTO)
4399 if (remove_comments)
4400 {
4401 /* We don't want to remove the comment leader if the
4402 * previous line is not a comment. */
4403 if (t > 0 && prev_was_comment)
4404 {
4405
4406 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4407 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004408 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004409 curr = new_curr;
4410 }
4411 else
4412 curr = skip_comment(curr, FALSE, insert_space,
4413 &prev_was_comment);
4414 }
4415#endif
4416
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004417 if (insert_space && t > 0)
4418 {
4419 curr = skipwhite(curr);
4420 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4421#ifdef FEAT_MBYTE
4422 && (!has_format_option(FO_MBYTE_JOIN)
4423 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4424 && (!has_format_option(FO_MBYTE_JOIN2)
4425 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4426#endif
4427 )
4428 {
4429 /* don't add a space if the line is ending in a space */
4430 if (endcurr1 == ' ')
4431 endcurr1 = endcurr2;
4432 else
4433 ++spaces[t];
4434 /* extra space when 'joinspaces' set and line ends in '.' */
4435 if ( p_js
4436 && (endcurr1 == '.'
4437 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4438 && (endcurr1 == '?' || endcurr1 == '!'))))
4439 ++spaces[t];
4440 }
4441 }
4442 currsize = (int)STRLEN(curr);
4443 sumsize += currsize + spaces[t];
4444 endcurr1 = endcurr2 = NUL;
4445 if (insert_space && currsize > 0)
4446 {
4447#ifdef FEAT_MBYTE
4448 if (has_mbyte)
4449 {
4450 cend = curr + currsize;
4451 mb_ptr_back(curr, cend);
4452 endcurr1 = (*mb_ptr2char)(cend);
4453 if (cend > curr)
4454 {
4455 mb_ptr_back(curr, cend);
4456 endcurr2 = (*mb_ptr2char)(cend);
4457 }
4458 }
4459 else
4460#endif
4461 {
4462 endcurr1 = *(curr + currsize - 1);
4463 if (currsize > 1)
4464 endcurr2 = *(curr + currsize - 2);
4465 }
4466 }
4467 line_breakcheck();
4468 if (got_int)
4469 {
4470 ret = FAIL;
4471 goto theend;
4472 }
4473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004475 /* store the column position before last line */
4476 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004478 /* allocate the space for the new line */
4479 newp = alloc_check((unsigned)(sumsize + 1));
4480 cend = newp + sumsize;
4481 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004483 /*
4484 * Move affected lines to the new long one.
4485 *
4486 * Move marks from each deleted line to the joined line, adjusting the
4487 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4488 * should not really be a problem.
4489 */
4490 for (t = count - 1; ; --t)
4491 {
4492 cend -= currsize;
4493 mch_memmove(cend, curr, (size_t)currsize);
4494 if (spaces[t] > 0)
4495 {
4496 cend -= spaces[t];
4497 copy_spaces(cend, (size_t)(spaces[t]));
4498 }
4499 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004500 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004501 if (t == 0)
4502 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004503 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004504#if defined(FEAT_COMMENTS) || defined(PROTO)
4505 if (remove_comments)
4506 curr += comments[t - 1];
4507#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004508 if (insert_space && t > 1)
4509 curr = skipwhite(curr);
4510 currsize = (int)STRLEN(curr);
4511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4513
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004514 if (setmark)
4515 {
4516 /* Set the '] mark. */
4517 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4518 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4519 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 /* Only report the change in the first line here, del_lines() will report
4522 * the deleted line. */
4523 changed_lines(curwin->w_cursor.lnum, currsize,
4524 curwin->w_cursor.lnum + 1, 0L);
4525
4526 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004527 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 * briefly, and then move it back. After del_lines() the cursor may
4529 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 */
4531 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004533 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 curwin->w_cursor.lnum = t;
4535
4536 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004537 * Set the cursor column:
4538 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004539 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004541 curwin->w_cursor.col =
4542 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#ifdef FEAT_VIRTUALEDIT
4546 curwin->w_cursor.coladd = 0;
4547#endif
4548 curwin->w_set_curswant = TRUE;
4549
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004550theend:
4551 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004552#if defined(FEAT_COMMENTS) || defined(PROTO)
4553 if (remove_comments)
4554 vim_free(comments);
4555#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004556 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557}
4558
4559#ifdef FEAT_COMMENTS
4560/*
4561 * Return TRUE if the two comment leaders given are the same. "lnum" is
4562 * the first line. White-space is ignored. Note that the whole of
4563 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4564 */
4565 static int
4566same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4567 linenr_T lnum;
4568 int leader1_len;
4569 char_u *leader1_flags;
4570 int leader2_len;
4571 char_u *leader2_flags;
4572{
4573 int idx1 = 0, idx2 = 0;
4574 char_u *p;
4575 char_u *line1;
4576 char_u *line2;
4577
4578 if (leader1_len == 0)
4579 return (leader2_len == 0);
4580
4581 /*
4582 * If first leader has 'f' flag, the lines can be joined only if the
4583 * second line does not have a leader.
4584 * If first leader has 'e' flag, the lines can never be joined.
4585 * If fist leader has 's' flag, the lines can only be joined if there is
4586 * some text after it and the second line has the 'm' flag.
4587 */
4588 if (leader1_flags != NULL)
4589 {
4590 for (p = leader1_flags; *p && *p != ':'; ++p)
4591 {
4592 if (*p == COM_FIRST)
4593 return (leader2_len == 0);
4594 if (*p == COM_END)
4595 return FALSE;
4596 if (*p == COM_START)
4597 {
4598 if (*(ml_get(lnum) + leader1_len) == NUL)
4599 return FALSE;
4600 if (leader2_flags == NULL || leader2_len == 0)
4601 return FALSE;
4602 for (p = leader2_flags; *p && *p != ':'; ++p)
4603 if (*p == COM_MIDDLE)
4604 return TRUE;
4605 return FALSE;
4606 }
4607 }
4608 }
4609
4610 /*
4611 * Get current line and next line, compare the leaders.
4612 * The first line has to be saved, only one line can be locked at a time.
4613 */
4614 line1 = vim_strsave(ml_get(lnum));
4615 if (line1 != NULL)
4616 {
4617 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4618 ;
4619 line2 = ml_get(lnum + 1);
4620 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4621 {
4622 if (!vim_iswhite(line2[idx2]))
4623 {
4624 if (line1[idx1++] != line2[idx2])
4625 break;
4626 }
4627 else
4628 while (vim_iswhite(line1[idx1]))
4629 ++idx1;
4630 }
4631 vim_free(line1);
4632 }
4633 return (idx2 == leader2_len && idx1 == leader1_len);
4634}
4635#endif
4636
4637/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004638 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 */
4640 void
4641op_format(oap, keep_cursor)
4642 oparg_T *oap;
4643 int keep_cursor; /* keep cursor on same text char */
4644{
4645 long old_line_count = curbuf->b_ml.ml_line_count;
4646
4647 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4648 * can put it back there. */
4649 curwin->w_cursor = oap->cursor_start;
4650
4651 if (u_save((linenr_T)(oap->start.lnum - 1),
4652 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4653 return;
4654 curwin->w_cursor = oap->start;
4655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (oap->is_VIsual)
4657 /* When there is no change: need to remove the Visual selection */
4658 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
4660 /* Set '[ mark at the start of the formatted area */
4661 curbuf->b_op_start = oap->start;
4662
4663 /* For "gw" remember the cursor position and put it back below (adjusted
4664 * for joined and split lines). */
4665 if (keep_cursor)
4666 saved_cursor = oap->cursor_start;
4667
Bram Moolenaar81a82092008-03-12 16:27:00 +00004668 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 /*
4671 * Leave the cursor at the first non-blank of the last formatted line.
4672 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4673 * line, so "." will do the next lines.
4674 */
4675 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4676 ++curwin->w_cursor.lnum;
4677 beginline(BL_WHITE | BL_FIX);
4678 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4679 msgmore(old_line_count);
4680
4681 /* put '] mark on the end of the formatted area */
4682 curbuf->b_op_end = curwin->w_cursor;
4683
4684 if (keep_cursor)
4685 {
4686 curwin->w_cursor = saved_cursor;
4687 saved_cursor.lnum = 0;
4688 }
4689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 if (oap->is_VIsual)
4691 {
4692 win_T *wp;
4693
4694 FOR_ALL_WINDOWS(wp)
4695 {
4696 if (wp->w_old_cursor_lnum != 0)
4697 {
4698 /* When lines have been inserted or deleted, adjust the end of
4699 * the Visual area to be redrawn. */
4700 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4701 wp->w_old_cursor_lnum += old_line_count;
4702 else
4703 wp->w_old_visual_lnum += old_line_count;
4704 }
4705 }
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707}
4708
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004709#if defined(FEAT_EVAL) || defined(PROTO)
4710/*
4711 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4712 */
4713 void
4714op_formatexpr(oap)
4715 oparg_T *oap;
4716{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004717 if (oap->is_VIsual)
4718 /* When there is no change: need to remove the Visual selection */
4719 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004720
Bram Moolenaar700303e2010-07-11 17:35:50 +02004721 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4722 /* As documented: when 'formatexpr' returns non-zero fall back to
4723 * internal formatting. */
4724 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004725}
4726
4727 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004728fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004729 linenr_T lnum;
4730 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004731 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004732{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004733 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4734 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004735 int r;
4736
4737 /*
4738 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004739 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004740 */
4741 set_vim_var_nr(VV_LNUM, lnum);
4742 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004743 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004744
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004745 /*
4746 * Evaluate the function.
4747 */
4748 if (use_sandbox)
4749 ++sandbox;
4750 r = eval_to_number(curbuf->b_p_fex);
4751 if (use_sandbox)
4752 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004753
4754 set_vim_var_string(VV_CHAR, NULL, -1);
4755
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004756 return r;
4757}
4758#endif
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760/*
4761 * Format "line_count" lines, starting at the cursor position.
4762 * When "line_count" is negative, format until the end of the paragraph.
4763 * Lines after the cursor line are saved for undo, caller must have saved the
4764 * first line.
4765 */
4766 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004767format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004769 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 int max_len;
4772 int is_not_par; /* current line not part of parag. */
4773 int next_is_not_par; /* next line not part of paragraph */
4774 int is_end_par; /* at end of paragraph */
4775 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4776 int next_is_start_par = FALSE;
4777#ifdef FEAT_COMMENTS
4778 int leader_len = 0; /* leader len of current line */
4779 int next_leader_len; /* leader len of next line */
4780 char_u *leader_flags = NULL; /* flags for leader of current line */
4781 char_u *next_leader_flags; /* flags for leader of next line */
4782 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004783 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#endif
4785 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004786 int second_indent = -1; /* indent for second line (comment
4787 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 int do_second_indent;
4789 int do_number_indent;
4790 int do_trail_white;
4791 int first_par_line = TRUE;
4792 int smd_save;
4793 long count;
4794 int need_set_indent = TRUE; /* set indent of next paragraph */
4795 int force_format = FALSE;
4796 int old_State = State;
4797
4798 /* length of a line to force formatting: 3 * 'tw' */
4799 max_len = comp_textwidth(TRUE) * 3;
4800
4801 /* check for 'q', '2' and '1' in 'formatoptions' */
4802#ifdef FEAT_COMMENTS
4803 do_comments = has_format_option(FO_Q_COMS);
4804#endif
4805 do_second_indent = has_format_option(FO_Q_SECOND);
4806 do_number_indent = has_format_option(FO_Q_NUMBER);
4807 do_trail_white = has_format_option(FO_WHITE_PAR);
4808
4809 /*
4810 * Get info about the previous and current line.
4811 */
4812 if (curwin->w_cursor.lnum > 1)
4813 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4814#ifdef FEAT_COMMENTS
4815 , &leader_len, &leader_flags, do_comments
4816#endif
4817 );
4818 else
4819 is_not_par = TRUE;
4820 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4821#ifdef FEAT_COMMENTS
4822 , &next_leader_len, &next_leader_flags, do_comments
4823#endif
4824 );
4825 is_end_par = (is_not_par || next_is_not_par);
4826 if (!is_end_par && do_trail_white)
4827 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4828
4829 curwin->w_cursor.lnum--;
4830 for (count = line_count; count != 0 && !got_int; --count)
4831 {
4832 /*
4833 * Advance to next paragraph.
4834 */
4835 if (advance)
4836 {
4837 curwin->w_cursor.lnum++;
4838 prev_is_end_par = is_end_par;
4839 is_not_par = next_is_not_par;
4840#ifdef FEAT_COMMENTS
4841 leader_len = next_leader_len;
4842 leader_flags = next_leader_flags;
4843#endif
4844 }
4845
4846 /*
4847 * The last line to be formatted.
4848 */
4849 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4850 {
4851 next_is_not_par = TRUE;
4852#ifdef FEAT_COMMENTS
4853 next_leader_len = 0;
4854 next_leader_flags = NULL;
4855#endif
4856 }
4857 else
4858 {
4859 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4860#ifdef FEAT_COMMENTS
4861 , &next_leader_len, &next_leader_flags, do_comments
4862#endif
4863 );
4864 if (do_number_indent)
4865 next_is_start_par =
4866 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4867 }
4868 advance = TRUE;
4869 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4870 if (!is_end_par && do_trail_white)
4871 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4872
4873 /*
4874 * Skip lines that are not in a paragraph.
4875 */
4876 if (is_not_par)
4877 {
4878 if (line_count < 0)
4879 break;
4880 }
4881 else
4882 {
4883 /*
4884 * For the first line of a paragraph, check indent of second line.
4885 * Don't do this for comments and empty lines.
4886 */
4887 if (first_par_line
4888 && (do_second_indent || do_number_indent)
4889 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004890 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004892 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4893 {
4894#ifdef FEAT_COMMENTS
4895 if (leader_len == 0 && next_leader_len == 0)
4896 {
4897 /* no comment found */
4898#endif
4899 second_indent =
4900 get_indent_lnum(curwin->w_cursor.lnum + 1);
4901#ifdef FEAT_COMMENTS
4902 }
4903 else
4904 {
4905 second_indent = next_leader_len;
4906 do_comments_list = 1;
4907 }
4908#endif
4909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004911 {
4912#ifdef FEAT_COMMENTS
4913 if (leader_len == 0 && next_leader_len == 0)
4914 {
4915 /* no comment found */
4916#endif
4917 second_indent =
4918 get_number_indent(curwin->w_cursor.lnum);
4919#ifdef FEAT_COMMENTS
4920 }
4921 else
4922 {
4923 /* get_number_indent() is now "comment aware"... */
4924 second_indent =
4925 get_number_indent(curwin->w_cursor.lnum);
4926 do_comments_list = 1;
4927 }
4928#endif
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931
4932 /*
4933 * When the comment leader changes, it's the end of the paragraph.
4934 */
4935 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4936#ifdef FEAT_COMMENTS
4937 || !same_leader(curwin->w_cursor.lnum,
4938 leader_len, leader_flags,
4939 next_leader_len, next_leader_flags)
4940#endif
4941 )
4942 is_end_par = TRUE;
4943
4944 /*
4945 * If we have got to the end of a paragraph, or the line is
4946 * getting long, format it.
4947 */
4948 if (is_end_par || force_format)
4949 {
4950 if (need_set_indent)
4951 /* replace indent in first line with minimal number of
4952 * tabs and spaces, according to current options */
4953 (void)set_indent(get_indent(), SIN_CHANGED);
4954
4955 /* put cursor on last non-space */
4956 State = NORMAL; /* don't go past end-of-line */
4957 coladvance((colnr_T)MAXCOL);
4958 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4959 dec_cursor();
4960
4961 /* do the formatting, without 'showmode' */
4962 State = INSERT; /* for open_line() */
4963 smd_save = p_smd;
4964 p_smd = FALSE;
4965 insertchar(NUL, INSCHAR_FORMAT
4966#ifdef FEAT_COMMENTS
4967 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004968 + (do_comments && do_comments_list
4969 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004971 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 State = old_State;
4973 p_smd = smd_save;
4974 second_indent = -1;
4975 /* at end of par.: need to set indent of next par. */
4976 need_set_indent = is_end_par;
4977 if (is_end_par)
4978 {
4979 /* When called with a negative line count, break at the
4980 * end of the paragraph. */
4981 if (line_count < 0)
4982 break;
4983 first_par_line = TRUE;
4984 }
4985 force_format = FALSE;
4986 }
4987
4988 /*
4989 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004990 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 */
4992 if (!is_end_par)
4993 {
4994 advance = FALSE;
4995 curwin->w_cursor.lnum++;
4996 curwin->w_cursor.col = 0;
4997 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005001 {
5002 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5004 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005005 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005007 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5008 {
5009 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005010 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005011
5012 if (indent > 0)
5013 {
5014 (void)del_bytes(indent, FALSE, FALSE);
5015 mark_col_adjust(curwin->w_cursor.lnum,
5016 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005017 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005020 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
5022 beep_flush();
5023 break;
5024 }
5025 first_par_line = FALSE;
5026 /* If the line is getting long, format it next time */
5027 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5028 force_format = TRUE;
5029 else
5030 force_format = FALSE;
5031 }
5032 }
5033 line_breakcheck();
5034 }
5035}
5036
5037/*
5038 * Return TRUE if line "lnum" ends in a white character.
5039 */
5040 static int
5041ends_in_white(lnum)
5042 linenr_T lnum;
5043{
5044 char_u *s = ml_get(lnum);
5045 size_t l;
5046
5047 if (*s == NUL)
5048 return FALSE;
5049 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5050 * invocation may call function multiple times". */
5051 l = STRLEN(s) - 1;
5052 return vim_iswhite(s[l]);
5053}
5054
5055/*
5056 * Blank lines, and lines containing only the comment leader, are left
5057 * untouched by the formatting. The function returns TRUE in this
5058 * case. It also returns TRUE when a line starts with the end of a comment
5059 * ('e' in comment flags), so that this line is skipped, and not joined to the
5060 * previous line. A new paragraph starts after a blank line, or when the
5061 * comment leader changes -- webb.
5062 */
5063#ifdef FEAT_COMMENTS
5064 static int
5065fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5066 linenr_T lnum;
5067 int *leader_len;
5068 char_u **leader_flags;
5069 int do_comments;
5070{
5071 char_u *flags = NULL; /* init for GCC */
5072 char_u *ptr;
5073
5074 ptr = ml_get(lnum);
5075 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005076 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 else
5078 *leader_len = 0;
5079
5080 if (*leader_len > 0)
5081 {
5082 /*
5083 * Search for 'e' flag in comment leader flags.
5084 */
5085 flags = *leader_flags;
5086 while (*flags && *flags != ':' && *flags != COM_END)
5087 ++flags;
5088 }
5089
5090 return (*skipwhite(ptr + *leader_len) == NUL
5091 || (*leader_len > 0 && *flags == COM_END)
5092 || startPS(lnum, NUL, FALSE));
5093}
5094#else
5095 static int
5096fmt_check_par(lnum)
5097 linenr_T lnum;
5098{
5099 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5100}
5101#endif
5102
5103/*
5104 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5105 * previous line is in the same paragraph. Used for auto-formatting.
5106 */
5107 int
5108paragraph_start(lnum)
5109 linenr_T lnum;
5110{
5111 char_u *p;
5112#ifdef FEAT_COMMENTS
5113 int leader_len = 0; /* leader len of current line */
5114 char_u *leader_flags = NULL; /* flags for leader of current line */
5115 int next_leader_len; /* leader len of next line */
5116 char_u *next_leader_flags; /* flags for leader of next line */
5117 int do_comments; /* format comments */
5118#endif
5119
5120 if (lnum <= 1)
5121 return TRUE; /* start of the file */
5122
5123 p = ml_get(lnum - 1);
5124 if (*p == NUL)
5125 return TRUE; /* after empty line */
5126
5127#ifdef FEAT_COMMENTS
5128 do_comments = has_format_option(FO_Q_COMS);
5129#endif
5130 if (fmt_check_par(lnum - 1
5131#ifdef FEAT_COMMENTS
5132 , &leader_len, &leader_flags, do_comments
5133#endif
5134 ))
5135 return TRUE; /* after non-paragraph line */
5136
5137 if (fmt_check_par(lnum
5138#ifdef FEAT_COMMENTS
5139 , &next_leader_len, &next_leader_flags, do_comments
5140#endif
5141 ))
5142 return TRUE; /* "lnum" is not a paragraph line */
5143
5144 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5145 return TRUE; /* missing trailing space in previous line. */
5146
5147 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5148 return TRUE; /* numbered item starts in "lnum". */
5149
5150#ifdef FEAT_COMMENTS
5151 if (!same_leader(lnum - 1, leader_len, leader_flags,
5152 next_leader_len, next_leader_flags))
5153 return TRUE; /* change of comment leader. */
5154#endif
5155
5156 return FALSE;
5157}
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159/*
5160 * prepare a few things for block mode yank/delete/tilde
5161 *
5162 * for delete:
5163 * - textlen includes the first/last char to be (partly) deleted
5164 * - start/endspaces is the number of columns that are taken by the
5165 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005166 * deleted.
5167 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 * - textlen includes the first/last char to be wholly yanked
5169 * - start/endspaces is the number of columns of the first/last yanked char
5170 * that are to be yanked.
5171 */
5172 static void
5173block_prep(oap, bdp, lnum, is_del)
5174 oparg_T *oap;
5175 struct block_def *bdp;
5176 linenr_T lnum;
5177 int is_del;
5178{
5179 int incr = 0;
5180 char_u *pend;
5181 char_u *pstart;
5182 char_u *line;
5183 char_u *prev_pstart;
5184 char_u *prev_pend;
5185
5186 bdp->startspaces = 0;
5187 bdp->endspaces = 0;
5188 bdp->textlen = 0;
5189 bdp->start_vcol = 0;
5190 bdp->end_vcol = 0;
5191#ifdef FEAT_VISUALEXTRA
5192 bdp->is_short = FALSE;
5193 bdp->is_oneChar = FALSE;
5194 bdp->pre_whitesp = 0;
5195 bdp->pre_whitesp_c = 0;
5196 bdp->end_char_vcols = 0;
5197#endif
5198 bdp->start_char_vcols = 0;
5199
5200 line = ml_get(lnum);
5201 pstart = line;
5202 prev_pstart = line;
5203 while (bdp->start_vcol < oap->start_vcol && *pstart)
5204 {
5205 /* Count a tab for what it's worth (if list mode not on) */
5206 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
5207 bdp->start_vcol += incr;
5208#ifdef FEAT_VISUALEXTRA
5209 if (vim_iswhite(*pstart))
5210 {
5211 bdp->pre_whitesp += incr;
5212 bdp->pre_whitesp_c++;
5213 }
5214 else
5215 {
5216 bdp->pre_whitesp = 0;
5217 bdp->pre_whitesp_c = 0;
5218 }
5219#endif
5220 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005221 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223 bdp->start_char_vcols = incr;
5224 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5225 {
5226 bdp->end_vcol = bdp->start_vcol;
5227#ifdef FEAT_VISUALEXTRA
5228 bdp->is_short = TRUE;
5229#endif
5230 if (!is_del || oap->op_type == OP_APPEND)
5231 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5232 }
5233 else
5234 {
5235 /* notice: this converts partly selected Multibyte characters to
5236 * spaces, too. */
5237 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5238 if (is_del && bdp->startspaces)
5239 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5240 pend = pstart;
5241 bdp->end_vcol = bdp->start_vcol;
5242 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5243 {
5244#ifdef FEAT_VISUALEXTRA
5245 bdp->is_oneChar = TRUE;
5246#endif
5247 if (oap->op_type == OP_INSERT)
5248 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5249 else if (oap->op_type == OP_APPEND)
5250 {
5251 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5252 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5253 }
5254 else
5255 {
5256 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5257 if (is_del && oap->op_type != OP_LSHIFT)
5258 {
5259 /* just putting the sum of those two into
5260 * bdp->startspaces doesn't work for Visual replace,
5261 * so we have to split the tab in two */
5262 bdp->startspaces = bdp->start_char_vcols
5263 - (bdp->start_vcol - oap->start_vcol);
5264 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5265 }
5266 }
5267 }
5268 else
5269 {
5270 prev_pend = pend;
5271 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5272 {
5273 /* Count a tab for what it's worth (if list mode not on) */
5274 prev_pend = pend;
5275 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
5276 bdp->end_vcol += incr;
5277 }
5278 if (bdp->end_vcol <= oap->end_vcol
5279 && (!is_del
5280 || oap->op_type == OP_APPEND
5281 || oap->op_type == OP_REPLACE)) /* line too short */
5282 {
5283#ifdef FEAT_VISUALEXTRA
5284 bdp->is_short = TRUE;
5285#endif
5286 /* Alternative: include spaces to fill up the block.
5287 * Disadvantage: can lead to trailing spaces when the line is
5288 * short where the text is put */
5289 /* if (!is_del || oap->op_type == OP_APPEND) */
5290 if (oap->op_type == OP_APPEND || virtual_op)
5291 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005292 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 else
5294 bdp->endspaces = 0; /* replace doesn't add characters */
5295 }
5296 else if (bdp->end_vcol > oap->end_vcol)
5297 {
5298 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5299 if (!is_del && bdp->endspaces)
5300 {
5301 bdp->endspaces = incr - bdp->endspaces;
5302 if (pend != pstart)
5303 pend = prev_pend;
5304 }
5305 }
5306 }
5307#ifdef FEAT_VISUALEXTRA
5308 bdp->end_char_vcols = incr;
5309#endif
5310 if (is_del && bdp->startspaces)
5311 pstart = prev_pstart;
5312 bdp->textlen = (int)(pend - pstart);
5313 }
5314 bdp->textcol = (colnr_T) (pstart - line);
5315 bdp->textstart = pstart;
5316}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
5318#ifdef FEAT_RIGHTLEFT
5319static void reverse_line __ARGS((char_u *s));
5320
5321 static void
5322reverse_line(s)
5323 char_u *s;
5324{
5325 int i, j;
5326 char_u c;
5327
5328 if ((i = (int)STRLEN(s) - 1) <= 0)
5329 return;
5330
5331 curwin->w_cursor.col = i - curwin->w_cursor.col;
5332 for (j = 0; j < i; j++, i--)
5333 {
5334 c = s[i]; s[i] = s[j]; s[j] = c;
5335 }
5336}
5337
5338# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5339#else
5340# define RLADDSUBFIX(ptr)
5341#endif
5342
5343/*
5344 * add or subtract 'Prenum1' from a number in a line
5345 * 'command' is CTRL-A for add, CTRL-X for subtract
5346 *
5347 * return FAIL for failure, OK otherwise
5348 */
5349 int
5350do_addsub(command, Prenum1)
5351 int command;
5352 linenr_T Prenum1;
5353{
5354 int col;
5355 char_u *buf1;
5356 char_u buf2[NUMBUFLEN];
5357 int hex; /* 'X' or 'x': hex; '0': octal */
5358 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005359 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 long_u oldn;
5361 char_u *ptr;
5362 int c;
5363 int length = 0; /* character length of the number */
5364 int todel;
5365 int dohex;
5366 int dooct;
5367 int doalp;
5368 int firstdigit;
5369 int negative;
5370 int subtract;
5371
5372 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5373 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5374 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5375
5376 ptr = ml_get_curline();
5377 RLADDSUBFIX(ptr);
5378
5379 /*
5380 * First check if we are on a hexadecimal number, after the "0x".
5381 */
5382 col = curwin->w_cursor.col;
5383 if (dohex)
5384 while (col > 0 && vim_isxdigit(ptr[col]))
5385 --col;
5386 if ( dohex
5387 && col > 0
5388 && (ptr[col] == 'X'
5389 || ptr[col] == 'x')
5390 && ptr[col - 1] == '0'
5391 && vim_isxdigit(ptr[col + 1]))
5392 {
5393 /*
5394 * Found hexadecimal number, move to its start.
5395 */
5396 --col;
5397 }
5398 else
5399 {
5400 /*
5401 * Search forward and then backward to find the start of number.
5402 */
5403 col = curwin->w_cursor.col;
5404
5405 while (ptr[col] != NUL
5406 && !vim_isdigit(ptr[col])
5407 && !(doalp && ASCII_ISALPHA(ptr[col])))
5408 ++col;
5409
5410 while (col > 0
5411 && vim_isdigit(ptr[col - 1])
5412 && !(doalp && ASCII_ISALPHA(ptr[col])))
5413 --col;
5414 }
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 /*
5417 * If a number was found, and saving for undo works, replace the number.
5418 */
5419 firstdigit = ptr[col];
5420 RLADDSUBFIX(ptr);
5421 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5422 || u_save_cursor() != OK)
5423 {
5424 beep_flush();
5425 return FAIL;
5426 }
5427
5428 /* get ptr again, because u_save() may have changed it */
5429 ptr = ml_get_curline();
5430 RLADDSUBFIX(ptr);
5431
5432 if (doalp && ASCII_ISALPHA(firstdigit))
5433 {
5434 /* decrement or increment alphabetic character */
5435 if (command == Ctrl_X)
5436 {
5437 if (CharOrd(firstdigit) < Prenum1)
5438 {
5439 if (isupper(firstdigit))
5440 firstdigit = 'A';
5441 else
5442 firstdigit = 'a';
5443 }
5444 else
5445#ifdef EBCDIC
5446 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5447#else
5448 firstdigit -= Prenum1;
5449#endif
5450 }
5451 else
5452 {
5453 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5454 {
5455 if (isupper(firstdigit))
5456 firstdigit = 'Z';
5457 else
5458 firstdigit = 'z';
5459 }
5460 else
5461#ifdef EBCDIC
5462 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5463#else
5464 firstdigit += Prenum1;
5465#endif
5466 }
5467 curwin->w_cursor.col = col;
5468 (void)del_char(FALSE);
5469 ins_char(firstdigit);
5470 }
5471 else
5472 {
5473 negative = FALSE;
5474 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5475 {
5476 --col;
5477 negative = TRUE;
5478 }
5479
5480 /* get the number value (unsigned) */
5481 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5482
5483 /* ignore leading '-' for hex and octal numbers */
5484 if (hex && negative)
5485 {
5486 ++col;
5487 --length;
5488 negative = FALSE;
5489 }
5490
5491 /* add or subtract */
5492 subtract = FALSE;
5493 if (command == Ctrl_X)
5494 subtract ^= TRUE;
5495 if (negative)
5496 subtract ^= TRUE;
5497
5498 oldn = n;
5499 if (subtract)
5500 n -= (unsigned long)Prenum1;
5501 else
5502 n += (unsigned long)Prenum1;
5503
5504 /* handle wraparound for decimal numbers */
5505 if (!hex)
5506 {
5507 if (subtract)
5508 {
5509 if (n > oldn)
5510 {
5511 n = 1 + (n ^ (unsigned long)-1);
5512 negative ^= TRUE;
5513 }
5514 }
5515 else /* add */
5516 {
5517 if (n < oldn)
5518 {
5519 n = (n ^ (unsigned long)-1);
5520 negative ^= TRUE;
5521 }
5522 }
5523 if (n == 0)
5524 negative = FALSE;
5525 }
5526
5527 /*
5528 * Delete the old number.
5529 */
5530 curwin->w_cursor.col = col;
5531 todel = length;
5532 c = gchar_cursor();
5533 /*
5534 * Don't include the '-' in the length, only the length of the part
5535 * after it is kept the same.
5536 */
5537 if (c == '-')
5538 --length;
5539 while (todel-- > 0)
5540 {
5541 if (c < 0x100 && isalpha(c))
5542 {
5543 if (isupper(c))
5544 hexupper = TRUE;
5545 else
5546 hexupper = FALSE;
5547 }
5548 /* del_char() will mark line needing displaying */
5549 (void)del_char(FALSE);
5550 c = gchar_cursor();
5551 }
5552
5553 /*
5554 * Prepare the leading characters in buf1[].
5555 * When there are many leading zeros it could be very long. Allocate
5556 * a bit too much.
5557 */
5558 buf1 = alloc((unsigned)length + NUMBUFLEN);
5559 if (buf1 == NULL)
5560 return FAIL;
5561 ptr = buf1;
5562 if (negative)
5563 {
5564 *ptr++ = '-';
5565 }
5566 if (hex)
5567 {
5568 *ptr++ = '0';
5569 --length;
5570 }
5571 if (hex == 'x' || hex == 'X')
5572 {
5573 *ptr++ = hex;
5574 --length;
5575 }
5576
5577 /*
5578 * Put the number characters in buf2[].
5579 */
5580 if (hex == 0)
5581 sprintf((char *)buf2, "%lu", n);
5582 else if (hex == '0')
5583 sprintf((char *)buf2, "%lo", n);
5584 else if (hex && hexupper)
5585 sprintf((char *)buf2, "%lX", n);
5586 else
5587 sprintf((char *)buf2, "%lx", n);
5588 length -= (int)STRLEN(buf2);
5589
5590 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005591 * Adjust number of zeros to the new number of digits, so the
5592 * total length of the number remains the same.
5593 * Don't do this when
5594 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005596 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 while (length-- > 0)
5598 *ptr++ = '0';
5599 *ptr = NUL;
5600 STRCAT(buf1, buf2);
5601 ins_str(buf1); /* insert the new number */
5602 vim_free(buf1);
5603 }
5604 --curwin->w_cursor.col;
5605 curwin->w_set_curswant = TRUE;
5606#ifdef FEAT_RIGHTLEFT
5607 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5608 RLADDSUBFIX(ptr);
5609#endif
5610 return OK;
5611}
5612
5613#ifdef FEAT_VIMINFO
5614 int
5615read_viminfo_register(virp, force)
5616 vir_T *virp;
5617 int force;
5618{
5619 int eof;
5620 int do_it = TRUE;
5621 int size;
5622 int limit;
5623 int i;
5624 int set_prev = FALSE;
5625 char_u *str;
5626 char_u **array = NULL;
5627
5628 /* We only get here (hopefully) if line[0] == '"' */
5629 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005630
5631 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 if (*str == '"')
5633 {
5634 set_prev = TRUE;
5635 str++;
5636 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (!ASCII_ISALNUM(*str) && *str != '-')
5639 {
5640 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5641 return TRUE; /* too many errors, pretend end-of-file */
5642 do_it = FALSE;
5643 }
5644 get_yank_register(*str++, FALSE);
5645 if (!force && y_current->y_array != NULL)
5646 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005647
5648 if (*str == '@')
5649 {
5650 /* "x@: register x used for @@ */
5651 if (force || execreg_lastc == NUL)
5652 execreg_lastc = str[-1];
5653 }
5654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 size = 0;
5656 limit = 100; /* Optimized for registers containing <= 100 lines */
5657 if (do_it)
5658 {
5659 if (set_prev)
5660 y_previous = y_current;
5661 vim_free(y_current->y_array);
5662 array = y_current->y_array =
5663 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005664 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 if (STRNCMP(str, "CHAR", 4) == 0)
5666 y_current->y_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 else if (STRNCMP(str, "BLOCK", 5) == 0)
5668 y_current->y_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 else
5670 y_current->y_type = MLINE;
5671 /* get the block width; if it's missing we get a zero, which is OK */
5672 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 y_current->y_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675
5676 while (!(eof = viminfo_readline(virp))
5677 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5678 {
5679 if (do_it)
5680 {
5681 if (size >= limit)
5682 {
5683 y_current->y_array = (char_u **)
5684 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5685 for (i = 0; i < limit; i++)
5686 y_current->y_array[i] = array[i];
5687 vim_free(array);
5688 limit *= 2;
5689 array = y_current->y_array;
5690 }
5691 str = viminfo_readstring(virp, 1, TRUE);
5692 if (str != NULL)
5693 array[size++] = str;
5694 else
5695 do_it = FALSE;
5696 }
5697 }
5698 if (do_it)
5699 {
5700 if (size == 0)
5701 {
5702 vim_free(array);
5703 y_current->y_array = NULL;
5704 }
5705 else if (size < limit)
5706 {
5707 y_current->y_array =
5708 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5709 for (i = 0; i < size; i++)
5710 y_current->y_array[i] = array[i];
5711 vim_free(array);
5712 }
5713 y_current->y_size = size;
5714 }
5715 return eof;
5716}
5717
5718 void
5719write_viminfo_registers(fp)
5720 FILE *fp;
5721{
5722 int i, j;
5723 char_u *type;
5724 char_u c;
5725 int num_lines;
5726 int max_num_lines;
5727 int max_kbyte;
5728 long len;
5729
Bram Moolenaar64404472010-06-26 06:24:45 +02005730 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 /* Get '<' value, use old '"' value if '<' is not found. */
5733 max_num_lines = get_viminfo_parameter('<');
5734 if (max_num_lines < 0)
5735 max_num_lines = get_viminfo_parameter('"');
5736 if (max_num_lines == 0)
5737 return;
5738 max_kbyte = get_viminfo_parameter('s');
5739 if (max_kbyte == 0)
5740 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 for (i = 0; i < NUM_REGISTERS; i++)
5743 {
5744 if (y_regs[i].y_array == NULL)
5745 continue;
5746#ifdef FEAT_CLIPBOARD
5747 /* Skip '*'/'+' register, we don't want them back next time */
5748 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5749 continue;
5750#endif
5751#ifdef FEAT_DND
5752 /* Neither do we want the '~' register */
5753 if (i == TILDE_REGISTER)
5754 continue;
5755#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005756 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005758 if (num_lines == 0
5759 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005760 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005761 continue;
5762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 if (max_kbyte > 0)
5764 {
5765 /* Skip register if there is more text than the maximum size. */
5766 len = 0;
5767 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005768 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 if (len > (long)max_kbyte * 1024L)
5770 continue;
5771 }
5772
5773 switch (y_regs[i].y_type)
5774 {
5775 case MLINE:
5776 type = (char_u *)"LINE";
5777 break;
5778 case MCHAR:
5779 type = (char_u *)"CHAR";
5780 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 case MBLOCK:
5782 type = (char_u *)"BLOCK";
5783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 default:
5785 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005786 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 emsg(IObuff);
5788 type = (char_u *)"LINE";
5789 break;
5790 }
5791 if (y_previous == &y_regs[i])
5792 fprintf(fp, "\"");
5793 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005794 fprintf(fp, "\"%c", c);
5795 if (c == execreg_lastc)
5796 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005797 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
5799 /* If max_num_lines < 0, then we save ALL the lines in the register */
5800 if (max_num_lines > 0 && num_lines > max_num_lines)
5801 num_lines = max_num_lines;
5802 for (j = 0; j < num_lines; j++)
5803 {
5804 putc('\t', fp);
5805 viminfo_writestring(fp, y_regs[i].y_array[j]);
5806 }
5807 }
5808}
5809#endif /* FEAT_VIMINFO */
5810
5811#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5812/*
5813 * SELECTION / PRIMARY ('*')
5814 *
5815 * Text selection stuff that uses the GUI selection register '*'. When using a
5816 * GUI this may be text from another window, otherwise it is the last text we
5817 * had highlighted with VIsual mode. With mouse support, clicking the middle
5818 * button performs the paste, otherwise you will need to do <"*p>. "
5819 * If not under X, it is synonymous with the clipboard register '+'.
5820 *
5821 * X CLIPBOARD ('+')
5822 *
5823 * Text selection stuff that uses the GUI clipboard register '+'.
5824 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5825 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5826 * otherwise you will need to do <"+p>. "
5827 * If not under X, it is synonymous with the selection register '*'.
5828 */
5829
5830/*
5831 * Routine to export any final X selection we had to the environment
5832 * so that the text is still available after vim has exited. X selections
5833 * only exist while the owning application exists, so we write to the
5834 * permanent (while X runs) store CUT_BUFFER0.
5835 * Dump the CLIPBOARD selection if we own it (it's logically the more
5836 * 'permanent' of the two), otherwise the PRIMARY one.
5837 * For now, use a hard-coded sanity limit of 1Mb of data.
5838 */
5839#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5840 void
5841x11_export_final_selection()
5842{
5843 Display *dpy;
5844 char_u *str = NULL;
5845 long_u len = 0;
5846 int motion_type = -1;
5847
5848# ifdef FEAT_GUI
5849 if (gui.in_use)
5850 dpy = X_DISPLAY;
5851 else
5852# endif
5853# ifdef FEAT_XCLIPBOARD
5854 dpy = xterm_dpy;
5855# else
5856 return;
5857# endif
5858
5859 /* Get selection to export */
5860 if (clip_plus.owned)
5861 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5862 else if (clip_star.owned)
5863 motion_type = clip_convert_selection(&str, &len, &clip_star);
5864
5865 /* Check it's OK */
5866 if (dpy != NULL && str != NULL && motion_type >= 0
5867 && len < 1024*1024 && len > 0)
5868 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005869#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005870 int ok = TRUE;
5871
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005872 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5873 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5874 * encoding conversion usually doesn't work, so keep the text as-is.
5875 */
5876 if (has_mbyte)
5877 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005878 vimconv_T vc;
5879
5880 vc.vc_type = CONV_NONE;
5881 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5882 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005883 int intlen = len;
5884 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005885
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005886 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005887 conv_str = string_convert(&vc, str, &intlen);
5888 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005889 if (conv_str != NULL)
5890 {
5891 vim_free(str);
5892 str = conv_str;
5893 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005894 else
5895 {
5896 ok = FALSE;
5897 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005898 convert_setup(&vc, NULL, NULL);
5899 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005900 else
5901 {
5902 ok = FALSE;
5903 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005904 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005905
5906 /* Do not store the string if conversion failed. Better to use any
5907 * other selection than garbled text. */
5908 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005909#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005910 {
5911 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5912 XFlush(dpy);
5913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915
5916 vim_free(str);
5917}
5918#endif
5919
5920 void
5921clip_free_selection(cbd)
5922 VimClipboard *cbd;
5923{
5924 struct yankreg *y_ptr = y_current;
5925
5926 if (cbd == &clip_plus)
5927 y_current = &y_regs[PLUS_REGISTER];
5928 else
5929 y_current = &y_regs[STAR_REGISTER];
5930 free_yank_all();
5931 y_current->y_size = 0;
5932 y_current = y_ptr;
5933}
5934
5935/*
5936 * Get the selected text and put it in the gui selection register '*' or '+'.
5937 */
5938 void
5939clip_get_selection(cbd)
5940 VimClipboard *cbd;
5941{
5942 struct yankreg *old_y_previous, *old_y_current;
5943 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 pos_T old_visual;
5945 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 colnr_T old_curswant;
5947 int old_set_curswant;
5948 pos_T old_op_start, old_op_end;
5949 oparg_T oa;
5950 cmdarg_T ca;
5951
5952 if (cbd->owned)
5953 {
5954 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5955 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5956 return;
5957
5958 /* Get the text between clip_star.start & clip_star.end */
5959 old_y_previous = y_previous;
5960 old_y_current = y_current;
5961 old_cursor = curwin->w_cursor;
5962 old_curswant = curwin->w_curswant;
5963 old_set_curswant = curwin->w_set_curswant;
5964 old_op_start = curbuf->b_op_start;
5965 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 old_visual = VIsual;
5967 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 clear_oparg(&oa);
5969 oa.regname = (cbd == &clip_plus ? '+' : '*');
5970 oa.op_type = OP_YANK;
5971 vim_memset(&ca, 0, sizeof(ca));
5972 ca.oap = &oa;
5973 ca.cmdchar = 'y';
5974 ca.count1 = 1;
5975 ca.retval = CA_NO_ADJ_OP_END;
5976 do_pending_operator(&ca, 0, TRUE);
5977 y_previous = old_y_previous;
5978 y_current = old_y_current;
5979 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005980 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 curwin->w_curswant = old_curswant;
5982 curwin->w_set_curswant = old_set_curswant;
5983 curbuf->b_op_start = old_op_start;
5984 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 VIsual = old_visual;
5986 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
5988 else
5989 {
5990 clip_free_selection(cbd);
5991
5992 /* Try to get selected text from another window */
5993 clip_gen_request_selection(cbd);
5994 }
5995}
5996
Bram Moolenaard44347f2011-06-19 01:14:29 +02005997/*
5998 * Convert from the GUI selection string into the '*'/'+' register.
5999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 void
6001clip_yank_selection(type, str, len, cbd)
6002 int type;
6003 char_u *str;
6004 long len;
6005 VimClipboard *cbd;
6006{
6007 struct yankreg *y_ptr;
6008
6009 if (cbd == &clip_plus)
6010 y_ptr = &y_regs[PLUS_REGISTER];
6011 else
6012 y_ptr = &y_regs[STAR_REGISTER];
6013
6014 clip_free_selection(cbd);
6015
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006016 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017}
6018
6019/*
6020 * Convert the '*'/'+' register into a GUI selection string returned in *str
6021 * with length *len.
6022 * Returns the motion type, or -1 for failure.
6023 */
6024 int
6025clip_convert_selection(str, len, cbd)
6026 char_u **str;
6027 long_u *len;
6028 VimClipboard *cbd;
6029{
6030 char_u *p;
6031 int lnum;
6032 int i, j;
6033 int_u eolsize;
6034 struct yankreg *y_ptr;
6035
6036 if (cbd == &clip_plus)
6037 y_ptr = &y_regs[PLUS_REGISTER];
6038 else
6039 y_ptr = &y_regs[STAR_REGISTER];
6040
6041#ifdef USE_CRNL
6042 eolsize = 2;
6043#else
6044 eolsize = 1;
6045#endif
6046
6047 *str = NULL;
6048 *len = 0;
6049 if (y_ptr->y_array == NULL)
6050 return -1;
6051
6052 for (i = 0; i < y_ptr->y_size; i++)
6053 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6054
6055 /*
6056 * Don't want newline character at end of last line if we're in MCHAR mode.
6057 */
6058 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6059 *len -= eolsize;
6060
6061 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6062 if (p == NULL)
6063 return -1;
6064 lnum = 0;
6065 for (i = 0, j = 0; i < (int)*len; i++, j++)
6066 {
6067 if (y_ptr->y_array[lnum][j] == '\n')
6068 p[i] = NUL;
6069 else if (y_ptr->y_array[lnum][j] == NUL)
6070 {
6071#ifdef USE_CRNL
6072 p[i++] = '\r';
6073#endif
6074#ifdef USE_CR
6075 p[i] = '\r';
6076#else
6077 p[i] = '\n';
6078#endif
6079 lnum++;
6080 j = -1;
6081 }
6082 else
6083 p[i] = y_ptr->y_array[lnum][j];
6084 }
6085 return y_ptr->y_type;
6086}
6087
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089/*
6090 * If we have written to a clipboard register, send the text to the clipboard.
6091 */
6092 static void
6093may_set_selection()
6094{
6095 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6096 {
6097 clip_own_selection(&clip_star);
6098 clip_gen_set_selection(&clip_star);
6099 }
6100 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6101 {
6102 clip_own_selection(&clip_plus);
6103 clip_gen_set_selection(&clip_plus);
6104 }
6105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107#endif /* FEAT_CLIPBOARD || PROTO */
6108
6109
6110#if defined(FEAT_DND) || defined(PROTO)
6111/*
6112 * Replace the contents of the '~' register with str.
6113 */
6114 void
6115dnd_yank_drag_data(str, len)
6116 char_u *str;
6117 long len;
6118{
6119 struct yankreg *curr;
6120
6121 curr = y_current;
6122 y_current = &y_regs[TILDE_REGISTER];
6123 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006124 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 y_current = curr;
6126}
6127#endif
6128
6129
6130#if defined(FEAT_EVAL) || defined(PROTO)
6131/*
6132 * Return the type of a register.
6133 * Used for getregtype()
6134 * Returns MAUTO for error.
6135 */
6136 char_u
6137get_reg_type(regname, reglen)
6138 int regname;
6139 long *reglen;
6140{
6141 switch (regname)
6142 {
6143 case '%': /* file name */
6144 case '#': /* alternate file name */
6145 case '=': /* expression */
6146 case ':': /* last command line */
6147 case '/': /* last search-pattern */
6148 case '.': /* last inserted text */
6149#ifdef FEAT_SEARCHPATH
6150 case Ctrl_F: /* Filename under cursor */
6151 case Ctrl_P: /* Path under cursor, expand via "path" */
6152#endif
6153 case Ctrl_W: /* word under cursor */
6154 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6155 case '_': /* black hole: always empty */
6156 return MCHAR;
6157 }
6158
6159#ifdef FEAT_CLIPBOARD
6160 regname = may_get_selection(regname);
6161#endif
6162
Bram Moolenaar32b92012014-01-14 12:33:36 +01006163 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6164 return MAUTO;
6165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 get_yank_register(regname, FALSE);
6167
6168 if (y_current->y_array != NULL)
6169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (reglen != NULL && y_current->y_type == MBLOCK)
6171 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return y_current->y_type;
6173 }
6174 return MAUTO;
6175}
6176
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006177static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6178
6179/*
6180 * When "flags" has GREG_LIST return a list with text "s".
6181 * Otherwise just return "s".
6182 */
6183 static char_u *
6184getreg_wrap_one_line(s, flags)
6185 char_u *s;
6186 int flags;
6187{
6188 if (flags & GREG_LIST)
6189 {
6190 list_T *list = list_alloc();
6191
6192 if (list != NULL)
6193 {
6194 if (list_append_string(list, NULL, -1) == FAIL)
6195 {
6196 list_free(list, TRUE);
6197 return NULL;
6198 }
6199 list->lv_first->li_tv.vval.v_string = s;
6200 }
6201 return (char_u *)list;
6202 }
6203 return s;
6204}
6205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206/*
6207 * Return the contents of a register as a single allocated string.
6208 * Used for "@r" in expressions and for getreg().
6209 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006210 * Flags:
6211 * GREG_NO_EXPR Do not allow expression register
6212 * GREG_EXPR_SRC For the expression register: return expression itself,
6213 * not the result of its evaluation.
6214 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 */
6216 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006217get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006219 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 long i;
6222 char_u *retval;
6223 int allocated;
6224 long len;
6225
6226 /* Don't allow using an expression register inside an expression */
6227 if (regname == '=')
6228 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006229 if (flags & GREG_NO_EXPR)
6230 return NULL;
6231 if (flags & GREG_EXPR_SRC)
6232 return getreg_wrap_one_line(get_expr_line_src(), flags);
6233 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
6235
6236 if (regname == '@') /* "@@" is used for unnamed register */
6237 regname = '"';
6238
6239 /* check for valid regname */
6240 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6241 return NULL;
6242
6243#ifdef FEAT_CLIPBOARD
6244 regname = may_get_selection(regname);
6245#endif
6246
6247 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6248 {
6249 if (retval == NULL)
6250 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006251 if (allocated)
6252 return getreg_wrap_one_line(retval, flags);
6253 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255
6256 get_yank_register(regname, FALSE);
6257 if (y_current->y_array == NULL)
6258 return NULL;
6259
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006260 if (flags & GREG_LIST)
6261 {
6262 list_T *list = list_alloc();
6263 int error = FALSE;
6264
6265 if (list == NULL)
6266 return NULL;
6267 for (i = 0; i < y_current->y_size; ++i)
6268 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6269 error = TRUE;
6270 if (error)
6271 {
6272 list_free(list, TRUE);
6273 return NULL;
6274 }
6275 return (char_u *)list;
6276 }
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 /*
6279 * Compute length of resulting string.
6280 */
6281 len = 0;
6282 for (i = 0; i < y_current->y_size; ++i)
6283 {
6284 len += (long)STRLEN(y_current->y_array[i]);
6285 /*
6286 * Insert a newline between lines and after last line if
6287 * y_type is MLINE.
6288 */
6289 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6290 ++len;
6291 }
6292
6293 retval = lalloc(len + 1, TRUE);
6294
6295 /*
6296 * Copy the lines of the yank register into the string.
6297 */
6298 if (retval != NULL)
6299 {
6300 len = 0;
6301 for (i = 0; i < y_current->y_size; ++i)
6302 {
6303 STRCPY(retval + len, y_current->y_array[i]);
6304 len += (long)STRLEN(retval + len);
6305
6306 /*
6307 * Insert a NL between lines and after the last line if y_type is
6308 * MLINE.
6309 */
6310 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6311 retval[len++] = '\n';
6312 }
6313 retval[len] = NUL;
6314 }
6315
6316 return retval;
6317}
6318
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006319 static int
6320init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6321 int name;
6322 struct yankreg **old_y_previous;
6323 struct yankreg **old_y_current;
6324 int must_append;
6325 int *yank_type UNUSED;
6326{
6327 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6328 {
6329 emsg_invreg(name);
6330 return FAIL;
6331 }
6332
6333 /* Don't want to change the current (unnamed) register */
6334 *old_y_previous = y_previous;
6335 *old_y_current = y_current;
6336
6337 get_yank_register(name, TRUE);
6338 if (!y_append && !must_append)
6339 free_yank_all();
6340 return OK;
6341}
6342
6343 static void
6344finish_write_reg(name, old_y_previous, old_y_current)
6345 int name;
6346 struct yankreg *old_y_previous;
6347 struct yankreg *old_y_current;
6348{
6349# ifdef FEAT_CLIPBOARD
6350 /* Send text of clipboard register to the clipboard. */
6351 may_set_selection();
6352# endif
6353
6354 /* ':let @" = "val"' should change the meaning of the "" register */
6355 if (name != '"')
6356 y_previous = old_y_previous;
6357 y_current = old_y_current;
6358}
6359
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360/*
6361 * Store string "str" in register "name".
6362 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6363 * If "must_append" is TRUE, always append to the register. Otherwise append
6364 * if "name" is an uppercase letter.
6365 * Note: "maxlen" and "must_append" don't work for the "/" register.
6366 * Careful: 'str' is modified, you may have to use a copy!
6367 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6368 */
6369 void
6370write_reg_contents(name, str, maxlen, must_append)
6371 int name;
6372 char_u *str;
6373 int maxlen;
6374 int must_append;
6375{
6376 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6377}
6378
6379 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006380write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6381 int name;
6382 char_u **strings;
6383 int maxlen UNUSED;
6384 int must_append;
6385 int yank_type;
6386 long block_len;
6387{
6388 struct yankreg *old_y_previous, *old_y_current;
6389
6390 if (name == '/'
6391#ifdef FEAT_EVAL
6392 || name == '='
6393#endif
6394 )
6395 {
6396 char_u *s;
6397
6398 if (strings[0] == NULL)
6399 s = (char_u *)"";
6400 else if (strings[1] != NULL)
6401 {
6402 EMSG(_("E883: search pattern and expression register may not "
6403 "contain two or more lines"));
6404 return;
6405 }
6406 else
6407 s = strings[0];
6408 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6409 return;
6410 }
6411
6412 if (name == '_') /* black hole: nothing to do */
6413 return;
6414
6415 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6416 &yank_type) == FAIL)
6417 return;
6418
6419 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6420
6421 finish_write_reg(name, old_y_previous, old_y_current);
6422}
6423
6424 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6426 int name;
6427 char_u *str;
6428 int maxlen;
6429 int must_append;
6430 int yank_type;
6431 long block_len;
6432{
6433 struct yankreg *old_y_previous, *old_y_current;
6434 long len;
6435
Bram Moolenaare7566042005-06-17 22:00:15 +00006436 if (maxlen >= 0)
6437 len = maxlen;
6438 else
6439 len = (long)STRLEN(str);
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 /* Special case: '/' search pattern */
6442 if (name == '/')
6443 {
6444 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6445 return;
6446 }
6447
Bram Moolenaare7566042005-06-17 22:00:15 +00006448#ifdef FEAT_EVAL
6449 if (name == '=')
6450 {
6451 char_u *p, *s;
6452
6453 p = vim_strnsave(str, (int)len);
6454 if (p == NULL)
6455 return;
6456 if (must_append)
6457 {
6458 s = concat_str(get_expr_line_src(), p);
6459 vim_free(p);
6460 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006461 }
6462 set_expr_line(p);
6463 return;
6464 }
6465#endif
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (name == '_') /* black hole: nothing to do */
6468 return;
6469
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006470 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6471 &yank_type) == FAIL)
6472 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006474 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006476 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477}
6478#endif /* FEAT_EVAL */
6479
6480#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6481/*
6482 * Put a string into a register. When the register is not empty, the string
6483 * is appended.
6484 */
6485 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006486str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006488 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 char_u *str; /* string to put in register */
6490 long len; /* length of string */
6491 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006492 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006494 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 int lnum;
6496 long start;
6497 long i;
6498 int extra;
6499 int newlines; /* number of lines added */
6500 int extraline = 0; /* extra line at the end */
6501 int append = FALSE; /* append to last line in register */
6502 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006503 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006507 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 y_ptr->y_size = 0;
6509
Bram Moolenaard44347f2011-06-19 01:14:29 +02006510 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006511 type = ((str_list || (len > 0 && (str[len - 1] == NL
6512 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006513 ? MLINE : MCHAR);
6514 else
6515 type = yank_type;
6516
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 /*
6518 * Count the number of lines within the string
6519 */
6520 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006521 if (str_list)
6522 {
6523 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006528 for (i = 0; i < len; i++)
6529 if (str[i] == '\n')
6530 ++newlines;
6531 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6532 {
6533 extraline = 1;
6534 ++newlines; /* count extra newline at the end */
6535 }
6536 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6537 {
6538 append = TRUE;
6539 --newlines; /* uncount newline when appending first line */
6540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
6542
6543 /*
6544 * Allocate an array to hold the pointers to the new register lines.
6545 * If the register was not empty, move the existing lines to the new array.
6546 */
6547 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6548 * sizeof(char_u *), TRUE);
6549 if (pp == NULL) /* out of memory */
6550 return;
6551 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6552 pp[lnum] = y_ptr->y_array[lnum];
6553 vim_free(y_ptr->y_array);
6554 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 /*
6558 * Find the end of each line and save it into the array.
6559 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006560 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006562 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6563 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006564 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006565 pp[lnum] = vim_strnsave(*ss, i);
6566 if (i > maxlen)
6567 maxlen = i;
6568 }
6569 }
6570 else
6571 {
6572 for (start = 0; start < len + extraline; start += i + 1)
6573 {
6574 for (i = start; i < len; ++i) /* find the end of the line */
6575 if (str[i] == '\n')
6576 break;
6577 i -= start; /* i is now length of line */
6578 if (i > maxlen)
6579 maxlen = i;
6580 if (append)
6581 {
6582 --lnum;
6583 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6584 }
6585 else
6586 extra = 0;
6587 s = alloc((unsigned)(i + extra + 1));
6588 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006590 if (extra)
6591 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6592 if (append)
6593 vim_free(y_ptr->y_array[lnum]);
6594 if (i)
6595 mch_memmove(s + extra, str + start, (size_t)i);
6596 extra += i;
6597 s[extra] = NUL;
6598 y_ptr->y_array[lnum++] = s;
6599 while (--extra >= 0)
6600 {
6601 if (*s == NUL)
6602 *s = '\n'; /* replace NUL with newline */
6603 ++s;
6604 }
6605 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 }
6608 y_ptr->y_type = type;
6609 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (type == MBLOCK)
6611 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6612 else
6613 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614}
6615#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6616
6617 void
6618clear_oparg(oap)
6619 oparg_T *oap;
6620{
6621 vim_memset(oap, 0, sizeof(oparg_T));
6622}
6623
Bram Moolenaar7c626922005-02-07 22:01:03 +00006624static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
6626/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006627 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 *
6629 * "Words" are counted by looking for boundaries between non-space and
6630 * space characters. (it seems to produce results that match 'wc'.)
6631 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006632 * Return value is byte count; word count for the line is added to "*wc".
6633 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 *
6635 * The function will only examine the first "limit" characters in the
6636 * line, stopping if it encounters an end-of-line (NUL byte). In that
6637 * case, eol_size will be added to the character count to account for
6638 * the size of the EOL character.
6639 */
6640 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006641line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 char_u *line;
6643 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006644 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 long limit;
6646 int eol_size;
6647{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006648 long i;
6649 long words = 0;
6650 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 int is_word = 0;
6652
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006653 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
6655 if (is_word)
6656 {
6657 if (vim_isspace(line[i]))
6658 {
6659 words++;
6660 is_word = 0;
6661 }
6662 }
6663 else if (!vim_isspace(line[i]))
6664 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006665 ++chars;
6666#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006667 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006668#else
6669 ++i;
6670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
6672
6673 if (is_word)
6674 words++;
6675 *wc += words;
6676
6677 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006678 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006681 chars += eol_size;
6682 }
6683 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 return i;
6685}
6686
6687/*
6688 * Give some info about the position of the cursor (for "g CTRL-G").
6689 * In Visual mode, give some info about the selected region. (In this case,
6690 * the *_count_cursor variables store running totals for the selection.)
6691 */
6692 void
6693cursor_pos_info()
6694{
6695 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006696 char_u buf1[50];
6697 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006699 long byte_count = 0;
6700 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 long char_count = 0;
6702 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 long word_count = 0;
6704 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006705 int eol_size;
6706 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 long line_count_selected = 0;
6708 pos_T min_pos, max_pos;
6709 oparg_T oparg;
6710 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712 /*
6713 * Compute the length of the file in characters.
6714 */
6715 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6716 {
6717 MSG(_(no_lines_msg));
6718 }
6719 else
6720 {
6721 if (get_fileformat(curbuf) == EOL_DOS)
6722 eol_size = 2;
6723 else
6724 eol_size = 1;
6725
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (VIsual_active)
6727 {
6728 if (lt(VIsual, curwin->w_cursor))
6729 {
6730 min_pos = VIsual;
6731 max_pos = curwin->w_cursor;
6732 }
6733 else
6734 {
6735 min_pos = curwin->w_cursor;
6736 max_pos = VIsual;
6737 }
6738 if (*p_sel == 'e' && max_pos.col > 0)
6739 --max_pos.col;
6740
6741 if (VIsual_mode == Ctrl_V)
6742 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006743#ifdef FEAT_LINEBREAK
6744 char_u * saved_sbr = p_sbr;
6745
6746 /* Make 'sbr' empty for a moment to get the correct size. */
6747 p_sbr = empty_option;
6748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 oparg.is_VIsual = 1;
6750 oparg.block_mode = TRUE;
6751 oparg.op_type = OP_NOP;
6752 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006753 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006754#ifdef FEAT_LINEBREAK
6755 p_sbr = saved_sbr;
6756#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006757 if (curwin->w_curswant == MAXCOL)
6758 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 /* Swap the start, end vcol if needed */
6760 if (oparg.end_vcol < oparg.start_vcol)
6761 {
6762 oparg.end_vcol += oparg.start_vcol;
6763 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6764 oparg.end_vcol -= oparg.start_vcol;
6765 }
6766 }
6767 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6771 {
6772 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006773 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
6775 ui_breakcheck();
6776 if (got_int)
6777 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006778 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 }
6780
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 /* Do extra processing for VIsual mode. */
6782 if (VIsual_active
6783 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6784 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006785 char_u *s = NULL;
6786 long len = 0L;
6787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 switch (VIsual_mode)
6789 {
6790 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006791#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006795#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006797#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006798 s = bd.textstart;
6799 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 break;
6801 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006802 s = ml_get(lnum);
6803 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 break;
6805 case 'v':
6806 {
6807 colnr_T start_col = (lnum == min_pos.lnum)
6808 ? min_pos.col : 0;
6809 colnr_T end_col = (lnum == max_pos.lnum)
6810 ? max_pos.col - start_col + 1 : MAXCOL;
6811
Bram Moolenaardef9e822004-12-31 20:58:58 +00006812 s = ml_get(lnum) + start_col;
6813 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
6815 break;
6816 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006817 if (s != NULL)
6818 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006819 byte_count_cursor += line_count_info(s, &word_count_cursor,
6820 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006821 if (lnum == curbuf->b_ml.ml_line_count
6822 && !curbuf->b_p_eol
6823 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006824 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006825 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
6828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {
6830 /* In non-visual mode, check for the line the cursor is on */
6831 if (lnum == curwin->w_cursor.lnum)
6832 {
6833 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006834 char_count_cursor += char_count;
6835 byte_count_cursor = byte_count +
6836 line_count_info(ml_get(lnum),
6837 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 (long)(curwin->w_cursor.col + 1), eol_size);
6839 }
6840 }
6841 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006842 byte_count += line_count_info(ml_get(lnum), &word_count,
6843 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 }
6845
6846 /* Correction for when last line doesn't have an EOL. */
6847 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006848 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 if (VIsual_active)
6851 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006852 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
6854 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006855 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006856 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6858 }
6859 else
6860 buf1[0] = NUL;
6861
Bram Moolenaar7c626922005-02-07 22:01:03 +00006862 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006863 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006864 vim_snprintf((char *)IObuff, IOSIZE,
6865 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 buf1, line_count_selected,
6867 (long)curbuf->b_ml.ml_line_count,
6868 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006869 byte_count_cursor, byte_count);
6870 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006871 vim_snprintf((char *)IObuff, IOSIZE,
6872 _("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 +00006873 buf1, line_count_selected,
6874 (long)curbuf->b_ml.ml_line_count,
6875 word_count_cursor, word_count,
6876 char_count_cursor, char_count,
6877 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
6879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
6881 p = ml_get_curline();
6882 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006883 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 (int)curwin->w_virtcol + 1);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006885 col_print(buf2, sizeof(buf2), (int)STRLEN(p), linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886
Bram Moolenaar7c626922005-02-07 22:01:03 +00006887 if (char_count_cursor == byte_count_cursor
6888 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006889 vim_snprintf((char *)IObuff, IOSIZE,
6890 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 (char *)buf1, (char *)buf2,
6892 (long)curwin->w_cursor.lnum,
6893 (long)curbuf->b_ml.ml_line_count,
6894 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006895 byte_count_cursor, byte_count);
6896 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006897 vim_snprintf((char *)IObuff, IOSIZE,
6898 _("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 +00006899 (char *)buf1, (char *)buf2,
6900 (long)curwin->w_cursor.lnum,
6901 (long)curbuf->b_ml.ml_line_count,
6902 word_count_cursor, word_count,
6903 char_count_cursor, char_count,
6904 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906
6907#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006908 byte_count = bomb_size();
6909 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006911 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912#endif
6913 /* Don't shorten this message, the user asked for it. */
6914 p = p_shm;
6915 p_shm = (char_u *)"";
6916 msg(IObuff);
6917 p_shm = p;
6918 }
6919}