blob: 5383a7c6dd5863174776de0213296f1cb55de602 [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 */
60#ifdef FEAT_VISUAL
61 colnr_T y_width; /* only set if y_type == MBLOCK */
62#endif
63} y_regs[NUM_REGISTERS];
64
65static struct yankreg *y_current; /* ptr to current yankreg */
66static int y_append; /* TRUE when appending */
67static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
68
69/*
70 * structure used by block_prep, op_delete and op_yank for blockwise operators
71 * also op_change, op_shift, op_insert, op_replace - AKelly
72 */
73struct block_def
74{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000075 int startspaces; /* 'extra' cols before first char */
76 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000078 char_u *textstart; /* pointer to 1st char (partially) in block */
79 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 colnr_T start_vcol; /* start col of 1st char wholly inside block */
81 colnr_T end_vcol; /* start col of 1st char wholly after block */
82#ifdef FEAT_VISUALEXTRA
83 int is_short; /* TRUE if line is too short to fit in block */
84 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
85 int is_oneChar; /* TRUE if block within one character */
86 int pre_whitesp; /* screen cols of ws before block */
87 int pre_whitesp_c; /* chars of ws before block */
88 colnr_T end_char_vcols; /* number of vcols of post-block char */
89#endif
90 colnr_T start_char_vcols; /* number of vcols of pre-block char */
91};
92
93#ifdef FEAT_VISUALEXTRA
94static void shift_block __ARGS((oparg_T *oap, int amount));
95static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
96#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000097static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000098static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000099static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
100 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#ifdef FEAT_MBYTE
103static void mb_adjust_opend __ARGS((oparg_T *oap));
104#endif
105static void free_yank __ARGS((long));
106static void free_yank_all __ARGS((void));
107static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
108#ifdef FEAT_CLIPBOARD
109static void copy_yank_reg __ARGS((struct yankreg *reg));
110# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
111static void may_set_selection __ARGS((void));
112# endif
113#endif
114static void dis_msg __ARGS((char_u *p, int skip_esc));
Bram Moolenaar81340392012-06-06 16:12:59 +0200115#if defined(FEAT_COMMENTS) || defined(PROTO)
116static char_u *skip_comment __ARGS((char_u *line, int process, int include_space, int *is_comment));
117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_VISUAL
119static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
120#endif
121#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
122static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
123#endif
124static int ends_in_white __ARGS((linenr_T lnum));
125#ifdef FEAT_COMMENTS
126static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
127static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
128#else
129static int fmt_check_par __ARGS((linenr_T));
130#endif
131
132/*
133 * The names of operators.
134 * IMPORTANT: Index must correspond with defines in vim.h!!!
135 * The third field indicates whether the operator always works on lines.
136 */
137static char opchars[][3] =
138{
139 {NUL, NUL, FALSE}, /* OP_NOP */
140 {'d', NUL, FALSE}, /* OP_DELETE */
141 {'y', NUL, FALSE}, /* OP_YANK */
142 {'c', NUL, FALSE}, /* OP_CHANGE */
143 {'<', NUL, TRUE}, /* OP_LSHIFT */
144 {'>', NUL, TRUE}, /* OP_RSHIFT */
145 {'!', NUL, TRUE}, /* OP_FILTER */
146 {'g', '~', FALSE}, /* OP_TILDE */
147 {'=', NUL, TRUE}, /* OP_INDENT */
148 {'g', 'q', TRUE}, /* OP_FORMAT */
149 {':', NUL, TRUE}, /* OP_COLON */
150 {'g', 'U', FALSE}, /* OP_UPPER */
151 {'g', 'u', FALSE}, /* OP_LOWER */
152 {'J', NUL, TRUE}, /* DO_JOIN */
153 {'g', 'J', TRUE}, /* DO_JOIN_NS */
154 {'g', '?', FALSE}, /* OP_ROT13 */
155 {'r', NUL, FALSE}, /* OP_REPLACE */
156 {'I', NUL, FALSE}, /* OP_INSERT */
157 {'A', NUL, FALSE}, /* OP_APPEND */
158 {'z', 'f', TRUE}, /* OP_FOLD */
159 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
160 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
161 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
162 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
163 {'z', 'd', TRUE}, /* OP_FOLDDEL */
164 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
165 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000166 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167};
168
169/*
170 * Translate a command name into an operator type.
171 * Must only be called with a valid operator name!
172 */
173 int
174get_op_type(char1, char2)
175 int char1;
176 int char2;
177{
178 int i;
179
180 if (char1 == 'r') /* ignore second character */
181 return OP_REPLACE;
182 if (char1 == '~') /* when tilde is an operator */
183 return OP_TILDE;
184 for (i = 0; ; ++i)
185 if (opchars[i][0] == char1 && opchars[i][1] == char2)
186 break;
187 return i;
188}
189
190#if defined(FEAT_VISUAL) || defined(PROTO)
191/*
192 * Return TRUE if operator "op" always works on whole lines.
193 */
194 int
195op_on_lines(op)
196 int op;
197{
198 return opchars[op][2];
199}
200#endif
201
202/*
203 * Get first operator command character.
204 * Returns 'g' or 'z' if there is another command character.
205 */
206 int
207get_op_char(optype)
208 int optype;
209{
210 return opchars[optype][0];
211}
212
213/*
214 * Get second operator command character.
215 */
216 int
217get_extra_op_char(optype)
218 int optype;
219{
220 return opchars[optype][1];
221}
222
223/*
224 * op_shift - handle a shift operation
225 */
226 void
227op_shift(oap, curs_top, amount)
228 oparg_T *oap;
229 int curs_top;
230 int amount;
231{
232 long i;
233 int first_char;
234 char_u *s;
235#ifdef FEAT_VISUAL
236 int block_col = 0;
237#endif
238
239 if (u_save((linenr_T)(oap->start.lnum - 1),
240 (linenr_T)(oap->end.lnum + 1)) == FAIL)
241 return;
242
243#ifdef FEAT_VISUAL
244 if (oap->block_mode)
245 block_col = curwin->w_cursor.col;
246#endif
247
248 for (i = oap->line_count; --i >= 0; )
249 {
250 first_char = *ml_get_curline();
251 if (first_char == NUL) /* empty line */
252 curwin->w_cursor.col = 0;
253#ifdef FEAT_VISUALEXTRA
254 else if (oap->block_mode)
255 shift_block(oap, amount);
256#endif
257 else
258 /* Move the line right if it doesn't start with '#', 'smartindent'
259 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
260#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
261 if (first_char != '#' || !preprocs_left())
262#endif
263 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000264 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 ++curwin->w_cursor.lnum;
267 }
268
269 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
270
271#ifdef FEAT_VISUAL
272 if (oap->block_mode)
273 {
274 curwin->w_cursor.lnum = oap->start.lnum;
275 curwin->w_cursor.col = block_col;
276 }
277 else
278#endif
279 if (curs_top) /* put cursor on first line, for ">>" */
280 {
281 curwin->w_cursor.lnum = oap->start.lnum;
282 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
283 }
284 else
285 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
286
287 if (oap->line_count > p_report)
288 {
289 if (oap->op_type == OP_RSHIFT)
290 s = (char_u *)">";
291 else
292 s = (char_u *)"<";
293 if (oap->line_count == 1)
294 {
295 if (amount == 1)
296 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
297 else
298 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
299 }
300 else
301 {
302 if (amount == 1)
303 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
304 oap->line_count, s);
305 else
306 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
307 oap->line_count, s, amount);
308 }
309 msg(IObuff);
310 }
311
312 /*
313 * Set "'[" and "']" marks.
314 */
315 curbuf->b_op_start = oap->start;
316 curbuf->b_op_end.lnum = oap->end.lnum;
317 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
318 if (curbuf->b_op_end.col > 0)
319 --curbuf->b_op_end.col;
320}
321
322/*
323 * shift the current line one shiftwidth left (if left != 0) or right
324 * leaves cursor on first blank in the line
325 */
326 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000327shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 int left;
329 int round;
330 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000331 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332{
333 int count;
334 int i, j;
335 int p_sw = (int)curbuf->b_p_sw;
336
337 count = get_indent(); /* get current indent */
338
339 if (round) /* round off indent */
340 {
341 i = count / p_sw; /* number of p_sw rounded down */
342 j = count % p_sw; /* extra spaces */
343 if (j && left) /* first remove extra spaces */
344 --amount;
345 if (left)
346 {
347 i -= amount;
348 if (i < 0)
349 i = 0;
350 }
351 else
352 i += amount;
353 count = i * p_sw;
354 }
355 else /* original vi indent */
356 {
357 if (left)
358 {
359 count -= p_sw * amount;
360 if (count < 0)
361 count = 0;
362 }
363 else
364 count += p_sw * amount;
365 }
366
367 /* Set new indent */
368#ifdef FEAT_VREPLACE
369 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000370 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 else
372#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000373 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374}
375
376#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
377/*
378 * Shift one line of the current block one shiftwidth right or left.
379 * Leaves cursor on first character in block.
380 */
381 static void
382shift_block(oap, amount)
383 oparg_T *oap;
384 int amount;
385{
386 int left = (oap->op_type == OP_LSHIFT);
387 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000388 int total;
389 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 int oldcol = curwin->w_cursor.col;
391 int p_sw = (int)curbuf->b_p_sw;
392 int p_ts = (int)curbuf->b_p_ts;
393 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000395 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 int i = 0, j = 0;
397 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#ifdef FEAT_RIGHTLEFT
399 int old_p_ri = p_ri;
400
401 p_ri = 0; /* don't want revins in ident */
402#endif
403
404 State = INSERT; /* don't want REPLACE for State */
405 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
406 if (bd.is_short)
407 return;
408
409 /* total is number of screen columns to be inserted/removed */
410 total = amount * p_sw;
411 oldp = ml_get_curline();
412
413 if (!left)
414 {
415 /*
416 * 1. Get start vcol
417 * 2. Total ws vcols
418 * 3. Divvy into TABs & spp
419 * 4. Construct new string
420 */
421 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
422 ws_vcol = bd.start_vcol - bd.pre_whitesp;
423 if (bd.startspaces)
424 {
425#ifdef FEAT_MBYTE
426 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000427 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000430 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432 for ( ; vim_iswhite(*bd.textstart); )
433 {
434 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
435 total += incr;
436 bd.start_vcol += incr;
437 }
438 /* OK, now total=all the VWS reqd, and textstart points at the 1st
439 * non-ws char in the block. */
440 if (!curbuf->b_p_et)
441 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
442 if (i)
443 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
444 else
445 j = total;
446 /* if we're splitting a TAB, allow for it */
447 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
448 len = (int)STRLEN(bd.textstart) + 1;
449 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
450 if (newp == NULL)
451 return;
452 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
453 mch_memmove(newp, oldp, (size_t)bd.textcol);
454 copy_chars(newp + bd.textcol, (size_t)i, TAB);
455 copy_spaces(newp + bd.textcol + i, (size_t)j);
456 /* the end */
457 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
458 }
459 else /* left */
460 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000461 colnr_T destination_col; /* column to which text in block will
462 be shifted */
463 char_u *verbatim_copy_end; /* end of the part of the line which is
464 copied verbatim */
465 colnr_T verbatim_copy_width;/* the (displayed) width of this part
466 of line */
467 unsigned fill; /* nr of spaces that replace a TAB */
468 unsigned new_line_len; /* the length of the line after the
469 block shift */
470 size_t block_space_width;
471 size_t shift_amount;
472 char_u *non_white = bd.textstart;
473 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000475 /*
476 * Firstly, let's find the first non-whitespace character that is
477 * displayed after the block's start column and the character's column
478 * number. Also, let's calculate the width of all the whitespace
479 * characters that are displayed in the block and precede the searched
480 * non-whitespace character.
481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000483 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
484 * the part of which is displayed at the block's beginning. Let's start
485 * searching from the next character. */
486 if (bd.startspaces)
487 mb_ptr_adv(non_white);
488
489 /* The character's column is in "bd.start_vcol". */
490 non_white_col = bd.start_vcol;
491
492 while (vim_iswhite(*non_white))
493 {
494 incr = lbr_chartabsize_adv(&non_white, non_white_col);
495 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 }
497
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000498 block_space_width = non_white_col - oap->start_vcol;
499 /* We will shift by "total" or "block_space_width", whichever is less.
500 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000501 shift_amount = (block_space_width < (size_t)total
502 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000503
504 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000505 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000506
507 /* Now let's find out how much of the beginning of the line we can
508 * reuse without modification. */
509 verbatim_copy_end = bd.textstart;
510 verbatim_copy_width = bd.start_vcol;
511
512 /* If "bd.startspaces" is set, "bd.textstart" points to the character
513 * preceding the block. We have to subtract its width to obtain its
514 * column number. */
515 if (bd.startspaces)
516 verbatim_copy_width -= bd.start_char_vcols;
517 while (verbatim_copy_width < destination_col)
518 {
519 incr = lbr_chartabsize(verbatim_copy_end, verbatim_copy_width);
520 if (verbatim_copy_width + incr > destination_col)
521 break;
522 verbatim_copy_width += incr;
523 mb_ptr_adv(verbatim_copy_end);
524 }
525
526 /* If "destination_col" is different from the width of the initial
527 * part of the line that will be copied, it means we encountered a tab
528 * character, which we will have to partly replace with spaces. */
529 fill = destination_col - verbatim_copy_width;
530
531 /* The replacement line will consist of:
532 * - the beginning of the original line up to "verbatim_copy_end",
533 * - "fill" number of spaces,
534 * - the rest of the line, pointed to by non_white. */
535 new_line_len = (unsigned)(verbatim_copy_end - oldp)
536 + fill
537 + (unsigned)STRLEN(non_white) + 1;
538
539 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (newp == NULL)
541 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000542 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
543 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
544 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 /* replace the line */
547 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
548 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
549 State = oldstate;
550 curwin->w_cursor.col = oldcol;
551#ifdef FEAT_RIGHTLEFT
552 p_ri = old_p_ri;
553#endif
554}
555#endif
556
557#ifdef FEAT_VISUALEXTRA
558/*
559 * Insert string "s" (b_insert ? before : after) block :AKelly
560 * Caller must prepare for undo.
561 */
562 static void
563block_insert(oap, s, b_insert, bdp)
564 oparg_T *oap;
565 char_u *s;
566 int b_insert;
567 struct block_def *bdp;
568{
569 int p_ts;
570 int count = 0; /* extra spaces to replace a cut TAB */
571 int spaces = 0; /* non-zero if cutting a TAB */
572 colnr_T offset; /* pointer along new line */
573 unsigned s_len; /* STRLEN(s) */
574 char_u *newp, *oldp; /* new, old lines */
575 linenr_T lnum; /* loop var */
576 int oldstate = State;
577
578 State = INSERT; /* don't want REPLACE for State */
579 s_len = (unsigned)STRLEN(s);
580
581 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
582 {
583 block_prep(oap, bdp, lnum, TRUE);
584 if (bdp->is_short && b_insert)
585 continue; /* OP_INSERT, line ends before block start */
586
587 oldp = ml_get(lnum);
588
589 if (b_insert)
590 {
591 p_ts = bdp->start_char_vcols;
592 spaces = bdp->startspaces;
593 if (spaces != 0)
594 count = p_ts - 1; /* we're cutting a TAB */
595 offset = bdp->textcol;
596 }
597 else /* append */
598 {
599 p_ts = bdp->end_char_vcols;
600 if (!bdp->is_short) /* spaces = padding after block */
601 {
602 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
603 if (spaces != 0)
604 count = p_ts - 1; /* we're cutting a TAB */
605 offset = bdp->textcol + bdp->textlen - (spaces != 0);
606 }
607 else /* spaces = padding to block edge */
608 {
609 /* if $ used, just append to EOL (ie spaces==0) */
610 if (!bdp->is_MAX)
611 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
612 count = spaces;
613 offset = bdp->textcol + bdp->textlen;
614 }
615 }
616
617 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
618 if (newp == NULL)
619 continue;
620
621 /* copy up to shifted part */
622 mch_memmove(newp, oldp, (size_t)(offset));
623 oldp += offset;
624
625 /* insert pre-padding */
626 copy_spaces(newp + offset, (size_t)spaces);
627
628 /* copy the new text */
629 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
630 offset += s_len;
631
632 if (spaces && !bdp->is_short)
633 {
634 /* insert post-padding */
635 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
636 /* We're splitting a TAB, don't copy it. */
637 oldp++;
638 /* We allowed for that TAB, remember this now */
639 count++;
640 }
641
642 if (spaces > 0)
643 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000644 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 ml_replace(lnum, newp, FALSE);
647
648 if (lnum == oap->end.lnum)
649 {
650 /* Set "']" mark to the end of the block instead of the end of
651 * the insert in the first line. */
652 curbuf->b_op_end.lnum = oap->end.lnum;
653 curbuf->b_op_end.col = offset;
654 }
655 } /* for all lnum */
656
657 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
658
659 State = oldstate;
660}
661#endif
662
663#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
664/*
665 * op_reindent - handle reindenting a block of lines.
666 */
667 void
668op_reindent(oap, how)
669 oparg_T *oap;
670 int (*how) __ARGS((void));
671{
672 long i;
673 char_u *l;
674 int count;
675 linenr_T first_changed = 0;
676 linenr_T last_changed = 0;
677 linenr_T start_lnum = curwin->w_cursor.lnum;
678
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000679 /* Don't even try when 'modifiable' is off. */
680 if (!curbuf->b_p_ma)
681 {
682 EMSG(_(e_modifiable));
683 return;
684 }
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 for (i = oap->line_count; --i >= 0 && !got_int; )
687 {
688 /* it's a slow thing to do, so give feedback so there's no worry that
689 * the computer's just hung. */
690
691 if (i > 1
692 && (i % 50 == 0 || i == oap->line_count - 1)
693 && oap->line_count > p_report)
694 smsg((char_u *)_("%ld lines to indent... "), i);
695
696 /*
697 * Be vi-compatible: For lisp indenting the first line is not
698 * indented, unless there is only one line.
699 */
700#ifdef FEAT_LISP
701 if (i != oap->line_count - 1 || oap->line_count == 1
702 || how != get_lisp_indent)
703#endif
704 {
705 l = skipwhite(ml_get_curline());
706 if (*l == NUL) /* empty or blank line */
707 count = 0;
708 else
709 count = how(); /* get the indent for this line */
710
711 if (set_indent(count, SIN_UNDO))
712 {
713 /* did change the indent, call changed_lines() later */
714 if (first_changed == 0)
715 first_changed = curwin->w_cursor.lnum;
716 last_changed = curwin->w_cursor.lnum;
717 }
718 }
719 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000720 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
722
723 /* put cursor on first non-blank of indented line */
724 curwin->w_cursor.lnum = start_lnum;
725 beginline(BL_SOL | BL_FIX);
726
727 /* Mark changed lines so that they will be redrawn. When Visual
728 * highlighting was present, need to continue until the last line. When
729 * there is no change still need to remove the Visual highlighting. */
730 if (last_changed != 0)
731 changed_lines(first_changed, 0,
732#ifdef FEAT_VISUAL
733 oap->is_VIsual ? start_lnum + oap->line_count :
734#endif
735 last_changed + 1, 0L);
736#ifdef FEAT_VISUAL
737 else if (oap->is_VIsual)
738 redraw_curbuf_later(INVERTED);
739#endif
740
741 if (oap->line_count > p_report)
742 {
743 i = oap->line_count - (i + 1);
744 if (i == 1)
745 MSG(_("1 line indented "));
746 else
747 smsg((char_u *)_("%ld lines indented "), i);
748 }
749 /* set '[ and '] marks */
750 curbuf->b_op_start = oap->start;
751 curbuf->b_op_end = oap->end;
752}
753#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
754
755#if defined(FEAT_EVAL) || defined(PROTO)
756/*
757 * Keep the last expression line here, for repeating.
758 */
759static char_u *expr_line = NULL;
760
761/*
762 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
763 * Returns '=' when OK, NUL otherwise.
764 */
765 int
766get_expr_register()
767{
768 char_u *new_line;
769
770 new_line = getcmdline('=', 0L, 0);
771 if (new_line == NULL)
772 return NUL;
773 if (*new_line == NUL) /* use previous line */
774 vim_free(new_line);
775 else
776 set_expr_line(new_line);
777 return '=';
778}
779
780/*
781 * Set the expression for the '=' register.
782 * Argument must be an allocated string.
783 */
784 void
785set_expr_line(new_line)
786 char_u *new_line;
787{
788 vim_free(expr_line);
789 expr_line = new_line;
790}
791
792/*
793 * Get the result of the '=' register expression.
794 * Returns a pointer to allocated memory, or NULL for failure.
795 */
796 char_u *
797get_expr_line()
798{
799 char_u *expr_copy;
800 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000801 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 if (expr_line == NULL)
804 return NULL;
805
806 /* Make a copy of the expression, because evaluating it may cause it to be
807 * changed. */
808 expr_copy = vim_strsave(expr_line);
809 if (expr_copy == NULL)
810 return NULL;
811
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000812 /* When we are invoked recursively limit the evaluation to 10 levels.
813 * Then return the string as-is. */
814 if (nested >= 10)
815 return expr_copy;
816
817 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000818 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000819 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 vim_free(expr_copy);
821 return rv;
822}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000823
824/*
825 * Get the '=' register expression itself, without evaluating it.
826 */
827 char_u *
828get_expr_line_src()
829{
830 if (expr_line == NULL)
831 return NULL;
832 return vim_strsave(expr_line);
833}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#endif /* FEAT_EVAL */
835
836/*
837 * Check if 'regname' is a valid name of a yank register.
838 * Note: There is no check for 0 (default register), caller should do this
839 */
840 int
841valid_yank_reg(regname, writing)
842 int regname;
843 int writing; /* if TRUE check for writable registers */
844{
845 if ( (regname > 0 && ASCII_ISALNUM(regname))
846 || (!writing && vim_strchr((char_u *)
847#ifdef FEAT_EVAL
848 "/.%#:="
849#else
850 "/.%#:"
851#endif
852 , regname) != NULL)
853 || regname == '"'
854 || regname == '-'
855 || regname == '_'
856#ifdef FEAT_CLIPBOARD
857 || regname == '*'
858 || regname == '+'
859#endif
860#ifdef FEAT_DND
861 || (!writing && regname == '~')
862#endif
863 )
864 return TRUE;
865 return FALSE;
866}
867
868/*
869 * Set y_current and y_append, according to the value of "regname".
870 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000871 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 *
873 * If regname is 0 and writing, use register 0
874 * If regname is 0 and reading, use previous register
875 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000876 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877get_yank_register(regname, writing)
878 int regname;
879 int writing;
880{
881 int i;
882
883 y_append = FALSE;
884 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
885 {
886 y_current = y_previous;
887 return;
888 }
889 i = regname;
890 if (VIM_ISDIGIT(i))
891 i -= '0';
892 else if (ASCII_ISLOWER(i))
893 i = CharOrdLow(i) + 10;
894 else if (ASCII_ISUPPER(i))
895 {
896 i = CharOrdUp(i) + 10;
897 y_append = TRUE;
898 }
899 else if (regname == '-')
900 i = DELETION_REGISTER;
901#ifdef FEAT_CLIPBOARD
902 /* When selection is not available, use register 0 instead of '*' */
903 else if (clip_star.available && regname == '*')
904 i = STAR_REGISTER;
905 /* When clipboard is not available, use register 0 instead of '+' */
906 else if (clip_plus.available && regname == '+')
907 i = PLUS_REGISTER;
908#endif
909#ifdef FEAT_DND
910 else if (!writing && regname == '~')
911 i = TILDE_REGISTER;
912#endif
913 else /* not 0-9, a-z, A-Z or '-': use register 0 */
914 i = 0;
915 y_current = &(y_regs[i]);
916 if (writing) /* remember the register we write into for do_put() */
917 y_previous = y_current;
918}
919
Bram Moolenaar8299df92004-07-10 09:47:34 +0000920#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921/*
922 * When "regname" is a clipboard register, obtain the selection. If it's not
923 * available return zero, otherwise return "regname".
924 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000925 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926may_get_selection(regname)
927 int regname;
928{
929 if (regname == '*')
930 {
931 if (!clip_star.available)
932 regname = 0;
933 else
934 clip_get_selection(&clip_star);
935 }
936 else if (regname == '+')
937 {
938 if (!clip_plus.available)
939 regname = 0;
940 else
941 clip_get_selection(&clip_plus);
942 }
943 return regname;
944}
945#endif
946
947#if defined(FEAT_VISUAL) || defined(PROTO)
948/*
949 * Obtain the contents of a "normal" register. The register is made empty.
950 * The returned pointer has allocated memory, use put_register() later.
951 */
952 void *
953get_register(name, copy)
954 int name;
955 int copy; /* make a copy, if FALSE make register empty. */
956{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000957 struct yankreg *reg;
958 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
960#ifdef FEAT_CLIPBOARD
961 /* When Visual area changed, may have to update selection. Obtain the
962 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000963 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 {
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000965 if (clip_isautosel())
966 clip_update_selection();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 may_get_selection(name);
968 }
969#endif
970
971 get_yank_register(name, 0);
972 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
973 if (reg != NULL)
974 {
975 *reg = *y_current;
976 if (copy)
977 {
978 /* If we run out of memory some or all of the lines are empty. */
979 if (reg->y_size == 0)
980 reg->y_array = NULL;
981 else
982 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
983 * reg->y_size));
984 if (reg->y_array != NULL)
985 {
986 for (i = 0; i < reg->y_size; ++i)
987 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
988 }
989 }
990 else
991 y_current->y_array = NULL;
992 }
993 return (void *)reg;
994}
995
996/*
Bram Moolenaar0a307462007-12-01 20:13:05 +0000997 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 */
999 void
1000put_register(name, reg)
1001 int name;
1002 void *reg;
1003{
1004 get_yank_register(name, 0);
1005 free_yank_all();
1006 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001007 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009# ifdef FEAT_CLIPBOARD
1010 /* Send text written to clipboard register to the clipboard. */
1011 may_set_selection();
1012# endif
1013}
1014#endif
1015
1016#if defined(FEAT_MOUSE) || defined(PROTO)
1017/*
1018 * return TRUE if the current yank register has type MLINE
1019 */
1020 int
1021yank_register_mline(regname)
1022 int regname;
1023{
1024 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1025 return FALSE;
1026 if (regname == '_') /* black hole is always empty */
1027 return FALSE;
1028 get_yank_register(regname, FALSE);
1029 return (y_current->y_type == MLINE);
1030}
1031#endif
1032
1033/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001034 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001036 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 */
1038 int
1039do_record(c)
1040 int c;
1041{
Bram Moolenaard55de222007-05-06 13:38:48 +00001042 char_u *p;
1043 static int regname;
1044 struct yankreg *old_y_previous, *old_y_current;
1045 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047 if (Recording == FALSE) /* start recording */
1048 {
1049 /* registers 0-9, a-z and " are allowed */
1050 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1051 retval = FAIL;
1052 else
1053 {
1054 Recording = TRUE;
1055 showmode();
1056 regname = c;
1057 retval = OK;
1058 }
1059 }
1060 else /* stop recording */
1061 {
1062 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001063 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1064 * needs to be removed again to put it in a register. exec_reg then
1065 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 Recording = FALSE;
1068 MSG("");
1069 p = get_recorded();
1070 if (p == NULL)
1071 retval = FAIL;
1072 else
1073 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001074 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1075 vim_unescape_csi(p);
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 /*
1078 * We don't want to change the default register here, so save and
1079 * restore the current register name.
1080 */
1081 old_y_previous = y_previous;
1082 old_y_current = y_current;
1083
1084 retval = stuff_yank(regname, p);
1085
1086 y_previous = old_y_previous;
1087 y_current = old_y_current;
1088 }
1089 }
1090 return retval;
1091}
1092
1093/*
1094 * Stuff string "p" into yank register "regname" as a single line (append if
1095 * uppercase). "p" must have been alloced.
1096 *
1097 * return FAIL for failure, OK otherwise
1098 */
1099 static int
1100stuff_yank(regname, p)
1101 int regname;
1102 char_u *p;
1103{
1104 char_u *lp;
1105 char_u **pp;
1106
1107 /* check for read-only register */
1108 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1109 {
1110 vim_free(p);
1111 return FAIL;
1112 }
1113 if (regname == '_') /* black hole: don't do anything */
1114 {
1115 vim_free(p);
1116 return OK;
1117 }
1118 get_yank_register(regname, TRUE);
1119 if (y_append && y_current->y_array != NULL)
1120 {
1121 pp = &(y_current->y_array[y_current->y_size - 1]);
1122 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1123 if (lp == NULL)
1124 {
1125 vim_free(p);
1126 return FAIL;
1127 }
1128 STRCPY(lp, *pp);
1129 STRCAT(lp, p);
1130 vim_free(p);
1131 vim_free(*pp);
1132 *pp = lp;
1133 }
1134 else
1135 {
1136 free_yank_all();
1137 if ((y_current->y_array =
1138 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1139 {
1140 vim_free(p);
1141 return FAIL;
1142 }
1143 y_current->y_array[0] = p;
1144 y_current->y_size = 1;
1145 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1146 }
1147 return OK;
1148}
1149
Bram Moolenaar42b94362009-05-26 16:12:37 +00001150static int execreg_lastc = NUL;
1151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152/*
1153 * execute a yank register: copy it into the stuff buffer
1154 *
1155 * return FAIL for failure, OK otherwise
1156 */
1157 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001158do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 int regname;
1160 int colon; /* insert ':' before each line */
1161 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001162 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 long i;
1165 char_u *p;
1166 int retval = OK;
1167 int remap;
1168
1169 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001170 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001171 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001172 {
1173 EMSG(_("E748: No previously used register"));
1174 return FAIL;
1175 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001176 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 /* check for valid regname */
1179 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001180 {
1181 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001183 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001184 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
1186#ifdef FEAT_CLIPBOARD
1187 regname = may_get_selection(regname);
1188#endif
1189
1190 if (regname == '_') /* black hole: don't stuff anything */
1191 return OK;
1192
1193#ifdef FEAT_CMDHIST
1194 if (regname == ':') /* use last command line */
1195 {
1196 if (last_cmdline == NULL)
1197 {
1198 EMSG(_(e_nolastcmd));
1199 return FAIL;
1200 }
1201 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1202 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001203 /* Escape all control characters with a CTRL-V */
1204 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001205 (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 +00001206 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001207 {
1208 /* When in Visual mode "'<,'>" will be prepended to the command.
1209 * Remove it when it's already there. */
1210 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001211 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001212 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001213 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001214 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001215 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217#endif
1218#ifdef FEAT_EVAL
1219 else if (regname == '=')
1220 {
1221 p = get_expr_line();
1222 if (p == NULL)
1223 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001224 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 vim_free(p);
1226 }
1227#endif
1228 else if (regname == '.') /* use last inserted text */
1229 {
1230 p = get_last_insert_save();
1231 if (p == NULL)
1232 {
1233 EMSG(_(e_noinstext));
1234 return FAIL;
1235 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001236 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 vim_free(p);
1238 }
1239 else
1240 {
1241 get_yank_register(regname, FALSE);
1242 if (y_current->y_array == NULL)
1243 return FAIL;
1244
1245 /* Disallow remaping for ":@r". */
1246 remap = colon ? REMAP_NONE : REMAP_YES;
1247
1248 /*
1249 * Insert lines into typeahead buffer, from last one to first one.
1250 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001251 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 for (i = y_current->y_size; --i >= 0; )
1253 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001254 char_u *escaped;
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 /* insert NL between lines and after last line if type is MLINE */
1257 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1258 || addcr)
1259 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001260 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 return FAIL;
1262 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001263 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1264 if (escaped == NULL)
1265 return FAIL;
1266 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1267 vim_free(escaped);
1268 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001270 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 == FAIL)
1272 return FAIL;
1273 }
1274 Exec_reg = TRUE; /* disable the 'q' command */
1275 }
1276 return retval;
1277}
1278
1279/*
1280 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1281 * used only after other typeahead has been processed.
1282 */
1283 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001284put_reedit_in_typebuf(silent)
1285 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 char_u buf[3];
1288
1289 if (restart_edit != NUL)
1290 {
1291 if (restart_edit == 'V')
1292 {
1293 buf[0] = 'g';
1294 buf[1] = 'R';
1295 buf[2] = NUL;
1296 }
1297 else
1298 {
1299 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1300 buf[1] = NUL;
1301 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001302 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 restart_edit = NUL;
1304 }
1305}
1306
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001307/*
1308 * Insert register contents "s" into the typeahead buffer, so that it will be
1309 * executed again.
1310 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1311 * no remapping.
1312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001314put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001316 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001318 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int retval = OK;
1321
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001322 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001324 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001326 {
1327 char_u *p;
1328
1329 if (esc)
1330 p = vim_strsave_escape_csi(s);
1331 else
1332 p = s;
1333 if (p == NULL)
1334 retval = FAIL;
1335 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001336 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1337 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001338 if (esc)
1339 vim_free(p);
1340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001342 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 return retval;
1344}
1345
1346/*
1347 * Insert a yank register: copy it into the Read buffer.
1348 * Used by CTRL-R command and middle mouse button in insert mode.
1349 *
1350 * return FAIL for failure, OK otherwise
1351 */
1352 int
1353insert_reg(regname, literally)
1354 int regname;
1355 int literally; /* insert literally, not as if typed */
1356{
1357 long i;
1358 int retval = OK;
1359 char_u *arg;
1360 int allocated;
1361
1362 /*
1363 * It is possible to get into an endless loop by having CTRL-R a in
1364 * register a and then, in insert mode, doing CTRL-R a.
1365 * If you hit CTRL-C, the loop will be broken here.
1366 */
1367 ui_breakcheck();
1368 if (got_int)
1369 return FAIL;
1370
1371 /* check for valid regname */
1372 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1373 return FAIL;
1374
1375#ifdef FEAT_CLIPBOARD
1376 regname = may_get_selection(regname);
1377#endif
1378
1379 if (regname == '.') /* insert last inserted text */
1380 retval = stuff_inserted(NUL, 1L, TRUE);
1381 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1382 {
1383 if (arg == NULL)
1384 return FAIL;
1385 stuffescaped(arg, literally);
1386 if (allocated)
1387 vim_free(arg);
1388 }
1389 else /* name or number register */
1390 {
1391 get_yank_register(regname, FALSE);
1392 if (y_current->y_array == NULL)
1393 retval = FAIL;
1394 else
1395 {
1396 for (i = 0; i < y_current->y_size; ++i)
1397 {
1398 stuffescaped(y_current->y_array[i], literally);
1399 /*
1400 * Insert a newline between lines and after last line if
1401 * y_type is MLINE.
1402 */
1403 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1404 stuffcharReadbuff('\n');
1405 }
1406 }
1407 }
1408
1409 return retval;
1410}
1411
1412/*
1413 * Stuff a string into the typeahead buffer, such that edit() will insert it
1414 * literally ("literally" TRUE) or interpret is as typed characters.
1415 */
1416 static void
1417stuffescaped(arg, literally)
1418 char_u *arg;
1419 int literally;
1420{
1421 int c;
1422 char_u *start;
1423
1424 while (*arg != NUL)
1425 {
1426 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1427 * stuff K_SPECIAL to get the effect of a special key when "literally"
1428 * is TRUE. */
1429 start = arg;
1430 while ((*arg >= ' '
1431#ifndef EBCDIC
1432 && *arg < DEL /* EBCDIC: chars above space are normal */
1433#endif
1434 )
1435 || (*arg == K_SPECIAL && !literally))
1436 ++arg;
1437 if (arg > start)
1438 stuffReadbuffLen(start, (long)(arg - start));
1439
1440 /* stuff a single special character */
1441 if (*arg != NUL)
1442 {
1443#ifdef FEAT_MBYTE
1444 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001445 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 else
1447#endif
1448 c = *arg++;
1449 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1450 stuffcharReadbuff(Ctrl_V);
1451 stuffcharReadbuff(c);
1452 }
1453 }
1454}
1455
1456/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001457 * If "regname" is a special register, return TRUE and store a pointer to its
1458 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001460 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461get_spec_reg(regname, argp, allocated, errmsg)
1462 int regname;
1463 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001464 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 int errmsg; /* give error message when failing */
1466{
1467 int cnt;
1468
1469 *argp = NULL;
1470 *allocated = FALSE;
1471 switch (regname)
1472 {
1473 case '%': /* file name */
1474 if (errmsg)
1475 check_fname(); /* will give emsg if not set */
1476 *argp = curbuf->b_fname;
1477 return TRUE;
1478
1479 case '#': /* alternate file name */
1480 *argp = getaltfname(errmsg); /* may give emsg if not set */
1481 return TRUE;
1482
1483#ifdef FEAT_EVAL
1484 case '=': /* result of expression */
1485 *argp = get_expr_line();
1486 *allocated = TRUE;
1487 return TRUE;
1488#endif
1489
1490 case ':': /* last command line */
1491 if (last_cmdline == NULL && errmsg)
1492 EMSG(_(e_nolastcmd));
1493 *argp = last_cmdline;
1494 return TRUE;
1495
1496 case '/': /* last search-pattern */
1497 if (last_search_pat() == NULL && errmsg)
1498 EMSG(_(e_noprevre));
1499 *argp = last_search_pat();
1500 return TRUE;
1501
1502 case '.': /* last inserted text */
1503 *argp = get_last_insert_save();
1504 *allocated = TRUE;
1505 if (*argp == NULL && errmsg)
1506 EMSG(_(e_noinstext));
1507 return TRUE;
1508
1509#ifdef FEAT_SEARCHPATH
1510 case Ctrl_F: /* Filename under cursor */
1511 case Ctrl_P: /* Path under cursor, expand via "path" */
1512 if (!errmsg)
1513 return FALSE;
1514 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001515 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 *allocated = TRUE;
1517 return TRUE;
1518#endif
1519
1520 case Ctrl_W: /* word under cursor */
1521 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1522 if (!errmsg)
1523 return FALSE;
1524 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1525 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1526 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1527 *allocated = TRUE;
1528 return TRUE;
1529
1530 case '_': /* black hole: always empty */
1531 *argp = (char_u *)"";
1532 return TRUE;
1533 }
1534
1535 return FALSE;
1536}
1537
1538/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001539 * Paste a yank register into the command line.
1540 * Only for non-special registers.
1541 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 * insert_reg() can't be used here, because special characters from the
1543 * register contents will be interpreted as commands.
1544 *
1545 * return FAIL for failure, OK otherwise
1546 */
1547 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001548cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 int regname;
1550 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001551 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555 get_yank_register(regname, FALSE);
1556 if (y_current->y_array == NULL)
1557 return FAIL;
1558
1559 for (i = 0; i < y_current->y_size; ++i)
1560 {
1561 cmdline_paste_str(y_current->y_array[i], literally);
1562
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001563 /* Insert ^M between lines and after last line if type is MLINE.
1564 * Don't do this when "remcr" is TRUE and the next line is empty. */
1565 if (y_current->y_type == MLINE
1566 || (i < y_current->y_size - 1
1567 && !(remcr
1568 && i == y_current->y_size - 2
1569 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 cmdline_paste_str((char_u *)"\r", literally);
1571
1572 /* Check for CTRL-C, in case someone tries to paste a few thousand
1573 * lines and gets bored. */
1574 ui_breakcheck();
1575 if (got_int)
1576 return FAIL;
1577 }
1578 return OK;
1579}
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1582/*
1583 * Adjust the register name pointed to with "rp" for the clipboard being
1584 * used always and the clipboard being available.
1585 */
1586 void
1587adjust_clip_reg(rp)
1588 int *rp;
1589{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001590 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1591 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
1592 if (*rp == 0 && clip_unnamed != 0)
1593 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
1594 ? '+' : '*';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (!clip_star.available && *rp == '*')
1596 *rp = 0;
1597 if (!clip_plus.available && *rp == '+')
1598 *rp = 0;
1599}
1600#endif
1601
1602/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001603 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001605 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
1607 int
1608op_delete(oap)
1609 oparg_T *oap;
1610{
1611 int n;
1612 linenr_T lnum;
1613 char_u *ptr;
1614#ifdef FEAT_VISUAL
1615 char_u *newp, *oldp;
1616 struct block_def bd;
1617#endif
1618 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1619 int did_yank = FALSE;
1620
1621 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1622 return OK;
1623
1624 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1625 if (oap->empty)
1626 return u_save_cursor();
1627
1628 if (!curbuf->b_p_ma)
1629 {
1630 EMSG(_(e_modifiable));
1631 return FAIL;
1632 }
1633
1634#ifdef FEAT_CLIPBOARD
1635 adjust_clip_reg(&oap->regname);
1636#endif
1637
1638#ifdef FEAT_MBYTE
1639 if (has_mbyte)
1640 mb_adjust_opend(oap);
1641#endif
1642
Bram Moolenaard04b7502010-07-08 22:27:55 +02001643 /*
1644 * Imitate the strange Vi behaviour: If the delete spans more than one
1645 * line and motion_type == MCHAR and the result is a blank line, make the
1646 * delete linewise. Don't do this for the change command or Visual mode.
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if ( oap->motion_type == MCHAR
1649#ifdef FEAT_VISUAL
1650 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001651 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652#endif
1653 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001654 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 && oap->op_type == OP_DELETE)
1656 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001657 ptr = ml_get(oap->end.lnum) + oap->end.col;
1658 if (*ptr != NUL)
1659 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 ptr = skipwhite(ptr);
1661 if (*ptr == NUL && inindent(0))
1662 oap->motion_type = MLINE;
1663 }
1664
Bram Moolenaard04b7502010-07-08 22:27:55 +02001665 /*
1666 * Check for trying to delete (e.g. "D") in an empty line.
1667 * Note: For the change operator it is ok.
1668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if ( oap->motion_type == MCHAR
1670 && oap->line_count == 1
1671 && oap->op_type == OP_DELETE
1672 && *ml_get(oap->start.lnum) == NUL)
1673 {
1674 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001675 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 * 'cpoptions' (Vi compatible).
1677 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001678#ifdef FEAT_VIRTUALEDIT
1679 if (virtual_op)
1680 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1681 * marks as if it happened. */
1682 goto setmarks;
1683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1685 beep_flush();
1686 return OK;
1687 }
1688
Bram Moolenaard04b7502010-07-08 22:27:55 +02001689 /*
1690 * Do a yank of whatever we're about to delete.
1691 * If a yank register was specified, put the deleted text into that
1692 * register. For the black hole register '_' don't yank anything.
1693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (oap->regname != '_')
1695 {
1696 if (oap->regname != 0)
1697 {
1698 /* check for read-only register */
1699 if (!valid_yank_reg(oap->regname, TRUE))
1700 {
1701 beep_flush();
1702 return OK;
1703 }
1704 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1705 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1706 did_yank = TRUE;
1707 }
1708
1709 /*
1710 * Put deleted text into register 1 and shift number registers if the
1711 * delete contains a line break, or when a regname has been specified.
1712 */
1713 if (oap->regname != 0 || oap->motion_type == MLINE
1714 || oap->line_count > 1 || oap->use_reg_one)
1715 {
1716 y_current = &y_regs[9];
1717 free_yank_all(); /* free register nine */
1718 for (n = 9; n > 1; --n)
1719 y_regs[n] = y_regs[n - 1];
1720 y_previous = y_current = &y_regs[1];
1721 y_regs[1].y_array = NULL; /* set register one to empty */
1722 if (op_yank(oap, TRUE, FALSE) == OK)
1723 did_yank = TRUE;
1724 }
1725
Bram Moolenaar84298db2012-04-20 13:46:08 +02001726 /* Yank into small delete register when no named register specified
1727 * and the delete is within one line. */
1728 if ((
1729#ifdef FEAT_CLIPBOARD
1730 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1731 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
1732#endif
1733 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 && oap->line_count == 1)
1735 {
1736 oap->regname = '-';
1737 get_yank_register(oap->regname, TRUE);
1738 if (op_yank(oap, TRUE, FALSE) == OK)
1739 did_yank = TRUE;
1740 oap->regname = 0;
1741 }
1742
1743 /*
1744 * If there's too much stuff to fit in the yank register, then get a
1745 * confirmation before doing the delete. This is crude, but simple.
1746 * And it avoids doing a delete of something we can't put back if we
1747 * want.
1748 */
1749 if (!did_yank)
1750 {
1751 int msg_silent_save = msg_silent;
1752
1753 msg_silent = 0; /* must display the prompt */
1754 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1755 msg_silent = msg_silent_save;
1756 if (n != 'y')
1757 {
1758 EMSG(_(e_abort));
1759 return FAIL;
1760 }
1761 }
1762 }
1763
1764#ifdef FEAT_VISUAL
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 }
1815 else
1816#endif
1817 if (oap->motion_type == MLINE)
1818 {
1819 if (oap->op_type == OP_CHANGE)
1820 {
1821 /* Delete the lines except the first one. Temporarily move the
1822 * cursor to the next line. Save the current line number, if the
1823 * last line is deleted it may be changed.
1824 */
1825 if (oap->line_count > 1)
1826 {
1827 lnum = curwin->w_cursor.lnum;
1828 ++curwin->w_cursor.lnum;
1829 del_lines((long)(oap->line_count - 1), TRUE);
1830 curwin->w_cursor.lnum = lnum;
1831 }
1832 if (u_save_cursor() == FAIL)
1833 return FAIL;
1834 if (curbuf->b_p_ai) /* don't delete indent */
1835 {
1836 beginline(BL_WHITE); /* cursor on first non-white */
1837 did_ai = TRUE; /* delete the indent when ESC hit */
1838 ai_col = curwin->w_cursor.col;
1839 }
1840 else
1841 beginline(0); /* cursor in column 0 */
1842 truncate_line(FALSE); /* delete the rest of the line */
1843 /* leave cursor past last char in line */
1844 if (oap->line_count > 1)
1845 u_clearline(); /* "U" command not possible after "2cc" */
1846 }
1847 else
1848 {
1849 del_lines(oap->line_count, TRUE);
1850 beginline(BL_WHITE | BL_FIX);
1851 u_clearline(); /* "U" command not possible after "dd" */
1852 }
1853 }
1854 else
1855 {
1856#ifdef FEAT_VIRTUALEDIT
1857 if (virtual_op)
1858 {
1859 int endcol = 0;
1860
1861 /* For virtualedit: break the tabs that are partly included. */
1862 if (gchar_pos(&oap->start) == '\t')
1863 {
1864 if (u_save_cursor() == FAIL) /* save first line for undo */
1865 return FAIL;
1866 if (oap->line_count == 1)
1867 endcol = getviscol2(oap->end.col, oap->end.coladd);
1868 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1869 oap->start = curwin->w_cursor;
1870 if (oap->line_count == 1)
1871 {
1872 coladvance(endcol);
1873 oap->end.col = curwin->w_cursor.col;
1874 oap->end.coladd = curwin->w_cursor.coladd;
1875 curwin->w_cursor = oap->start;
1876 }
1877 }
1878
1879 /* Break a tab only when it's included in the area. */
1880 if (gchar_pos(&oap->end) == '\t'
1881 && (int)oap->end.coladd < oap->inclusive)
1882 {
1883 /* save last line for undo */
1884 if (u_save((linenr_T)(oap->end.lnum - 1),
1885 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1886 return FAIL;
1887 curwin->w_cursor = oap->end;
1888 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1889 oap->end = curwin->w_cursor;
1890 curwin->w_cursor = oap->start;
1891 }
1892 }
1893#endif
1894
1895 if (oap->line_count == 1) /* delete characters within one line */
1896 {
1897 if (u_save_cursor() == FAIL) /* save line for undo */
1898 return FAIL;
1899
1900 /* if 'cpoptions' contains '$', display '$' at end of change */
1901 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1902 && oap->op_type == OP_CHANGE
1903 && oap->end.lnum == curwin->w_cursor.lnum
1904#ifdef FEAT_VISUAL
1905 && !oap->is_VIsual
1906#endif
1907 )
1908 display_dollar(oap->end.col - !oap->inclusive);
1909
1910 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1911
1912#ifdef FEAT_VIRTUALEDIT
1913 if (virtual_op)
1914 {
1915 /* fix up things for virtualedit-delete:
1916 * break the tabs which are going to get in our way
1917 */
1918 char_u *curline = ml_get_curline();
1919 int len = (int)STRLEN(curline);
1920
1921 if (oap->end.coladd != 0
1922 && (int)oap->end.col >= len - 1
1923 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1924 n++;
1925 /* Delete at least one char (e.g, when on a control char). */
1926 if (n == 0 && oap->start.coladd != oap->end.coladd)
1927 n = 1;
1928
1929 /* When deleted a char in the line, reset coladd. */
1930 if (gchar_cursor() != NUL)
1931 curwin->w_cursor.coladd = 0;
1932 }
1933#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001934 if (oap->op_type == OP_DELETE
1935 && oap->inclusive
1936 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001937 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1938 {
1939 /* Special case: gH<Del> deletes the last line. */
1940 del_lines(1L, FALSE);
1941 }
1942 else
1943 {
1944 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001945#ifdef FEAT_VISUAL
1946 && !oap->is_VIsual
1947#endif
1948 );
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951 else /* delete characters between lines */
1952 {
1953 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001954 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956 /* save deleted and changed lines for undo */
1957 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1958 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1959 return FAIL;
1960
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001961 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 truncate_line(TRUE); /* delete from cursor to end of line */
1963
1964 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1965 ++curwin->w_cursor.lnum;
1966 del_lines((long)(oap->line_count - 2), FALSE);
1967
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001968 if (delete_last_line)
1969 oap->end.lnum = curbuf->b_ml.ml_line_count;
1970
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001971 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001972 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001973 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1974 {
1975 /* Special case: gH<Del> deletes the last line. */
1976 del_lines(1L, FALSE);
1977 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01001978 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1979 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001980 }
1981 else
1982 {
1983 /* delete from start of line until op_end */
1984 curwin->w_cursor.col = 0;
1985 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001986#ifdef FEAT_VISUAL
1987 && !oap->is_VIsual
1988#endif
1989 );
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001990 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1991 }
1992 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar81340392012-06-06 16:12:59 +02001993 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995 }
1996
1997 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1998
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001999#ifdef FEAT_VIRTUALEDIT
2000setmarks:
2001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#ifdef FEAT_VISUAL
2003 if (oap->block_mode)
2004 {
2005 curbuf->b_op_end.lnum = oap->end.lnum;
2006 curbuf->b_op_end.col = oap->start.col;
2007 }
2008 else
2009#endif
2010 curbuf->b_op_end = oap->start;
2011 curbuf->b_op_start = oap->start;
2012
2013 return OK;
2014}
2015
2016#ifdef FEAT_MBYTE
2017/*
2018 * Adjust end of operating area for ending on a multi-byte character.
2019 * Used for deletion.
2020 */
2021 static void
2022mb_adjust_opend(oap)
2023 oparg_T *oap;
2024{
2025 char_u *p;
2026
2027 if (oap->inclusive)
2028 {
2029 p = ml_get(oap->end.lnum);
2030 oap->end.col += mb_tail_off(p, p + oap->end.col);
2031 }
2032}
2033#endif
2034
2035#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2036/*
2037 * Replace a whole area with one character.
2038 */
2039 int
2040op_replace(oap, c)
2041 oparg_T *oap;
2042 int c;
2043{
2044 int n, numc;
2045#ifdef FEAT_MBYTE
2046 int num_chars;
2047#endif
2048 char_u *newp, *oldp;
2049 size_t oldlen;
2050 struct block_def bd;
2051
2052 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2053 return OK; /* nothing to do */
2054
2055#ifdef FEAT_MBYTE
2056 if (has_mbyte)
2057 mb_adjust_opend(oap);
2058#endif
2059
2060 if (u_save((linenr_T)(oap->start.lnum - 1),
2061 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2062 return FAIL;
2063
2064 /*
2065 * block mode replace
2066 */
2067 if (oap->block_mode)
2068 {
2069 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2070 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2071 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002072 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2074 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2075 continue; /* nothing to replace */
2076
2077 /* n == number of extra chars required
2078 * If we split a TAB, it may be replaced by several characters.
2079 * Thus the number of characters may increase!
2080 */
2081#ifdef FEAT_VIRTUALEDIT
2082 /* If the range starts in virtual space, count the initial
2083 * coladd offset as part of "startspaces" */
2084 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2085 {
2086 pos_T vpos;
2087
Bram Moolenaara1381de2009-11-03 15:44:21 +00002088 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 getvpos(&vpos, oap->start_vcol);
2090 bd.startspaces += vpos.coladd;
2091 n = bd.startspaces;
2092 }
2093 else
2094#endif
2095 /* allow for pre spaces */
2096 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2097
2098 /* allow for post spp */
2099 n += (bd.endspaces
2100#ifdef FEAT_VIRTUALEDIT
2101 && !bd.is_oneChar
2102#endif
2103 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2104 /* Figure out how many characters to replace. */
2105 numc = oap->end_vcol - oap->start_vcol + 1;
2106 if (bd.is_short && (!virtual_op || bd.is_MAX))
2107 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2108
2109#ifdef FEAT_MBYTE
2110 /* A double-wide character can be replaced only up to half the
2111 * times. */
2112 if ((*mb_char2cells)(c) > 1)
2113 {
2114 if ((numc & 1) && !bd.is_short)
2115 {
2116 ++bd.endspaces;
2117 ++n;
2118 }
2119 numc = numc / 2;
2120 }
2121
2122 /* Compute bytes needed, move character count to num_chars. */
2123 num_chars = numc;
2124 numc *= (*mb_char2len)(c);
2125#endif
2126 /* oldlen includes textlen, so don't double count */
2127 n += numc - bd.textlen;
2128
2129 oldp = ml_get_curline();
2130 oldlen = STRLEN(oldp);
2131 newp = alloc_check((unsigned)oldlen + 1 + n);
2132 if (newp == NULL)
2133 continue;
2134 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2135 /* copy up to deleted part */
2136 mch_memmove(newp, oldp, (size_t)bd.textcol);
2137 oldp += bd.textcol + bd.textlen;
2138 /* insert pre-spaces */
2139 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2140 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2141#ifdef FEAT_MBYTE
2142 if (has_mbyte)
2143 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002144 n = (int)STRLEN(newp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 while (--num_chars >= 0)
2146 n += (*mb_char2bytes)(c, newp + n);
2147 }
2148 else
2149#endif
2150 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2151 if (!bd.is_short)
2152 {
2153 /* insert post-spaces */
2154 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2155 /* copy the part after the changed part */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002156 STRMOVE(newp + STRLEN(newp), oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 /* replace the line */
2159 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2160 }
2161 }
2162 else
2163 {
2164 /*
2165 * MCHAR and MLINE motion replace.
2166 */
2167 if (oap->motion_type == MLINE)
2168 {
2169 oap->start.col = 0;
2170 curwin->w_cursor.col = 0;
2171 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2172 if (oap->end.col)
2173 --oap->end.col;
2174 }
2175 else if (!oap->inclusive)
2176 dec(&(oap->end));
2177
2178 while (ltoreq(curwin->w_cursor, oap->end))
2179 {
2180 n = gchar_cursor();
2181 if (n != NUL)
2182 {
2183#ifdef FEAT_MBYTE
2184 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2185 {
2186 /* This is slow, but it handles replacing a single-byte
2187 * with a multi-byte and the other way around. */
2188 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2189 n = State;
2190 State = REPLACE;
2191 ins_char(c);
2192 State = n;
2193 /* Backup to the replaced character. */
2194 dec_cursor();
2195 }
2196 else
2197#endif
2198 {
2199#ifdef FEAT_VIRTUALEDIT
2200 if (n == TAB)
2201 {
2202 int end_vcol = 0;
2203
2204 if (curwin->w_cursor.lnum == oap->end.lnum)
2205 {
2206 /* oap->end has to be recalculated when
2207 * the tab breaks */
2208 end_vcol = getviscol2(oap->end.col,
2209 oap->end.coladd);
2210 }
2211 coladvance_force(getviscol());
2212 if (curwin->w_cursor.lnum == oap->end.lnum)
2213 getvpos(&oap->end, end_vcol);
2214 }
2215#endif
2216 pchar(curwin->w_cursor, c);
2217 }
2218 }
2219#ifdef FEAT_VIRTUALEDIT
2220 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2221 {
2222 int virtcols = oap->end.coladd;
2223
2224 if (curwin->w_cursor.lnum == oap->start.lnum
2225 && oap->start.col == oap->end.col && oap->start.coladd)
2226 virtcols -= oap->start.coladd;
2227
2228 /* oap->end has been trimmed so it's effectively inclusive;
2229 * as a result an extra +1 must be counted so we don't
2230 * trample the NUL byte. */
2231 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2232 curwin->w_cursor.col -= (virtcols + 1);
2233 for (; virtcols >= 0; virtcols--)
2234 {
2235 pchar(curwin->w_cursor, c);
2236 if (inc(&curwin->w_cursor) == -1)
2237 break;
2238 }
2239 }
2240#endif
2241
2242 /* Advance to next character, stop at the end of the file. */
2243 if (inc_cursor() == -1)
2244 break;
2245 }
2246 }
2247
2248 curwin->w_cursor = oap->start;
2249 check_cursor();
2250 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2251
2252 /* Set "'[" and "']" marks. */
2253 curbuf->b_op_start = oap->start;
2254 curbuf->b_op_end = oap->end;
2255
2256 return OK;
2257}
2258#endif
2259
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002260static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262/*
2263 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2264 */
2265 void
2266op_tilde(oap)
2267 oparg_T *oap;
2268{
2269 pos_T pos;
2270#ifdef FEAT_VISUAL
2271 struct block_def bd;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002272#endif
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002273 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275 if (u_save((linenr_T)(oap->start.lnum - 1),
2276 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2277 return;
2278
2279 pos = oap->start;
2280#ifdef FEAT_VISUAL
2281 if (oap->block_mode) /* Visual block mode */
2282 {
2283 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2284 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002285 int one_change;
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 block_prep(oap, &bd, pos.lnum, FALSE);
2288 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002289 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2290 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002293 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2296
Bram Moolenaar009b2592004-10-24 19:18:58 +00002297 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2298 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002300 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302# endif
2303 }
2304 if (did_change)
2305 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2306 }
2307 else /* not block mode */
2308#endif
2309 {
2310 if (oap->motion_type == MLINE)
2311 {
2312 oap->start.col = 0;
2313 pos.col = 0;
2314 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2315 if (oap->end.col)
2316 --oap->end.col;
2317 }
2318 else if (!oap->inclusive)
2319 dec(&(oap->end));
2320
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002321 if (pos.lnum == oap->end.lnum)
2322 did_change = swapchars(oap->op_type, &pos,
2323 oap->end.col - pos.col + 1);
2324 else
2325 for (;;)
2326 {
2327 did_change |= swapchars(oap->op_type, &pos,
2328 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2329 (int)STRLEN(ml_get_pos(&pos)));
2330 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2331 break;
2332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (did_change)
2334 {
2335 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2336 0L);
2337#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002338 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 char_u *ptr;
2341 int count;
2342
2343 pos = oap->start;
2344 while (pos.lnum < oap->end.lnum)
2345 {
2346 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002347 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002348 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002350 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 pos.col = 0;
2352 pos.lnum++;
2353 }
2354 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2355 count = oap->end.col - pos.col + 1;
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 }
2360#endif
2361 }
2362 }
2363
2364#ifdef FEAT_VISUAL
2365 if (!did_change && oap->is_VIsual)
2366 /* No change: need to remove the Visual selection */
2367 redraw_curbuf_later(INVERTED);
2368#endif
2369
2370 /*
2371 * Set '[ and '] marks.
2372 */
2373 curbuf->b_op_start = oap->start;
2374 curbuf->b_op_end = oap->end;
2375
2376 if (oap->line_count > p_report)
2377 {
2378 if (oap->line_count == 1)
2379 MSG(_("1 line changed"));
2380 else
2381 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2382 }
2383}
2384
2385/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002386 * Invoke swapchar() on "length" bytes at position "pos".
2387 * "pos" is advanced to just after the changed characters.
2388 * "length" is rounded up to include the whole last multi-byte character.
2389 * Also works correctly when the number of bytes changes.
2390 * Returns TRUE if some character was changed.
2391 */
2392 static int
2393swapchars(op_type, pos, length)
2394 int op_type;
2395 pos_T *pos;
2396 int length;
2397{
2398 int todo;
2399 int did_change = 0;
2400
2401 for (todo = length; todo > 0; --todo)
2402 {
2403# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002404 if (has_mbyte)
2405 /* we're counting bytes, not characters */
2406 todo -= (*mb_ptr2len)(ml_get_pos(pos)) - 1;
2407# endif
2408 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002409 if (inc(pos) == -1) /* at end of file */
2410 break;
2411 }
2412 return did_change;
2413}
2414
2415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 * If op_type == OP_UPPER: make uppercase,
2417 * if op_type == OP_LOWER: make lowercase,
2418 * if op_type == OP_ROT13: do rot13 encoding,
2419 * else swap case of character at 'pos'
2420 * returns TRUE when something actually changed.
2421 */
2422 int
2423swapchar(op_type, pos)
2424 int op_type;
2425 pos_T *pos;
2426{
2427 int c;
2428 int nc;
2429
2430 c = gchar_pos(pos);
2431
2432 /* Only do rot13 encoding for ASCII characters. */
2433 if (c >= 0x80 && op_type == OP_ROT13)
2434 return FALSE;
2435
2436#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002437 if (op_type == OP_UPPER && c == 0xdf
2438 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002439 {
2440 pos_T sp = curwin->w_cursor;
2441
2442 /* Special handling of German sharp s: change to "SS". */
2443 curwin->w_cursor = *pos;
2444 del_char(FALSE);
2445 ins_char('S');
2446 ins_char('S');
2447 curwin->w_cursor = sp;
2448 inc(pos);
2449 }
2450
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2452 return FALSE;
2453#endif
2454 nc = c;
2455 if (MB_ISLOWER(c))
2456 {
2457 if (op_type == OP_ROT13)
2458 nc = ROT13(c, 'a');
2459 else if (op_type != OP_LOWER)
2460 nc = MB_TOUPPER(c);
2461 }
2462 else if (MB_ISUPPER(c))
2463 {
2464 if (op_type == OP_ROT13)
2465 nc = ROT13(c, 'A');
2466 else if (op_type != OP_UPPER)
2467 nc = MB_TOLOWER(c);
2468 }
2469 if (nc != c)
2470 {
2471#ifdef FEAT_MBYTE
2472 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2473 {
2474 pos_T sp = curwin->w_cursor;
2475
2476 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002477 /* don't use del_char(), it also removes composing chars */
2478 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 ins_char(nc);
2480 curwin->w_cursor = sp;
2481 }
2482 else
2483#endif
2484 pchar(*pos, nc);
2485 return TRUE;
2486 }
2487 return FALSE;
2488}
2489
2490#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2491/*
2492 * op_insert - Insert and append operators for Visual mode.
2493 */
2494 void
2495op_insert(oap, count1)
2496 oparg_T *oap;
2497 long count1;
2498{
2499 long ins_len, pre_textlen = 0;
2500 char_u *firstline, *ins_text;
2501 struct block_def bd;
2502 int i;
2503
2504 /* edit() changes this - record it for OP_APPEND */
2505 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2506
2507 /* vis block is still marked. Get rid of it now. */
2508 curwin->w_cursor.lnum = oap->start.lnum;
2509 update_screen(INVERTED);
2510
2511 if (oap->block_mode)
2512 {
2513#ifdef FEAT_VIRTUALEDIT
2514 /* When 'virtualedit' is used, need to insert the extra spaces before
2515 * doing block_prep(). When only "block" is used, virtual edit is
2516 * already disabled, but still need it when calling
2517 * coladvance_force(). */
2518 if (curwin->w_cursor.coladd > 0)
2519 {
2520 int old_ve_flags = ve_flags;
2521
2522 ve_flags = VE_ALL;
2523 if (u_save_cursor() == FAIL)
2524 return;
2525 coladvance_force(oap->op_type == OP_APPEND
2526 ? oap->end_vcol + 1 : getviscol());
2527 if (oap->op_type == OP_APPEND)
2528 --curwin->w_cursor.col;
2529 ve_flags = old_ve_flags;
2530 }
2531#endif
2532 /* Get the info about the block before entering the text */
2533 block_prep(oap, &bd, oap->start.lnum, TRUE);
2534 firstline = ml_get(oap->start.lnum) + bd.textcol;
2535 if (oap->op_type == OP_APPEND)
2536 firstline += bd.textlen;
2537 pre_textlen = (long)STRLEN(firstline);
2538 }
2539
2540 if (oap->op_type == OP_APPEND)
2541 {
2542 if (oap->block_mode
2543#ifdef FEAT_VIRTUALEDIT
2544 && curwin->w_cursor.coladd == 0
2545#endif
2546 )
2547 {
2548 /* Move the cursor to the character right of the block. */
2549 curwin->w_set_curswant = TRUE;
2550 while (*ml_get_cursor() != NUL
2551 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2552 ++curwin->w_cursor.col;
2553 if (bd.is_short && !bd.is_MAX)
2554 {
2555 /* First line was too short, make it longer and adjust the
2556 * values in "bd". */
2557 if (u_save_cursor() == FAIL)
2558 return;
2559 for (i = 0; i < bd.endspaces; ++i)
2560 ins_char(' ');
2561 bd.textlen += bd.endspaces;
2562 }
2563 }
2564 else
2565 {
2566 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002567 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
2569 /* Works just like an 'i'nsert on the next character. */
2570 if (!lineempty(curwin->w_cursor.lnum)
2571 && oap->start_vcol != oap->end_vcol)
2572 inc_cursor();
2573 }
2574 }
2575
2576 edit(NUL, FALSE, (linenr_T)count1);
2577
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002578 /* If user has moved off this line, we don't know what to do, so do
2579 * nothing.
2580 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2581 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 return;
2583
2584 if (oap->block_mode)
2585 {
2586 struct block_def bd2;
2587
2588 /*
2589 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002590 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 * Don't do this when "$" used, end-of-line will have changed.
2592 */
2593 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2594 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2595 {
2596 if (oap->op_type == OP_APPEND)
2597 {
2598 pre_textlen += bd2.textlen - bd.textlen;
2599 if (bd2.endspaces)
2600 --bd2.textlen;
2601 }
2602 bd.textcol = bd2.textcol;
2603 bd.textlen = bd2.textlen;
2604 }
2605
2606 /*
2607 * Subsequent calls to ml_get() flush the firstline data - take a
2608 * copy of the required string.
2609 */
2610 firstline = ml_get(oap->start.lnum) + bd.textcol;
2611 if (oap->op_type == OP_APPEND)
2612 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002613 if (pre_textlen >= 0
2614 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 ins_text = vim_strnsave(firstline, (int)ins_len);
2617 if (ins_text != NULL)
2618 {
2619 /* block handled here */
2620 if (u_save(oap->start.lnum,
2621 (linenr_T)(oap->end.lnum + 1)) == OK)
2622 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2623 &bd);
2624
2625 curwin->w_cursor.col = oap->start.col;
2626 check_cursor();
2627 vim_free(ins_text);
2628 }
2629 }
2630 }
2631}
2632#endif
2633
2634/*
2635 * op_change - handle a change operation
2636 *
2637 * return TRUE if edit() returns because of a CTRL-O command
2638 */
2639 int
2640op_change(oap)
2641 oparg_T *oap;
2642{
2643 colnr_T l;
2644 int retval;
2645#ifdef FEAT_VISUALEXTRA
2646 long offset;
2647 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002648 long ins_len;
2649 long pre_textlen = 0;
2650 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 char_u *firstline;
2652 char_u *ins_text, *newp, *oldp;
2653 struct block_def bd;
2654#endif
2655
2656 l = oap->start.col;
2657 if (oap->motion_type == MLINE)
2658 {
2659 l = 0;
2660#ifdef FEAT_SMARTINDENT
2661 if (!p_paste && curbuf->b_p_si
2662# ifdef FEAT_CINDENT
2663 && !curbuf->b_p_cin
2664# endif
2665 )
2666 can_si = TRUE; /* It's like opening a new line, do si */
2667#endif
2668 }
2669
2670 /* First delete the text in the region. In an empty buffer only need to
2671 * save for undo */
2672 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2673 {
2674 if (u_save_cursor() == FAIL)
2675 return FALSE;
2676 }
2677 else if (op_delete(oap) == FAIL)
2678 return FALSE;
2679
2680 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2681 && !virtual_op)
2682 inc_cursor();
2683
2684#ifdef FEAT_VISUALEXTRA
2685 /* check for still on same line (<CR> in inserted text meaningless) */
2686 /* skip blank lines too */
2687 if (oap->block_mode)
2688 {
2689# ifdef FEAT_VIRTUALEDIT
2690 /* Add spaces before getting the current line length. */
2691 if (virtual_op && (curwin->w_cursor.coladd > 0
2692 || gchar_cursor() == NUL))
2693 coladvance_force(getviscol());
2694# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002695 firstline = ml_get(oap->start.lnum);
2696 pre_textlen = (long)STRLEN(firstline);
2697 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 bd.textcol = curwin->w_cursor.col;
2699 }
2700#endif
2701
2702#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2703 if (oap->motion_type == MLINE)
2704 fix_indent();
2705#endif
2706
2707 retval = edit(NUL, FALSE, (linenr_T)1);
2708
2709#ifdef FEAT_VISUALEXTRA
2710 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002711 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002713 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002715 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002717 /* Auto-indenting may have changed the indent. If the cursor was past
2718 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002720 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002722 long new_indent = (long)(skipwhite(firstline) - firstline);
2723
2724 pre_textlen += new_indent - pre_indent;
2725 bd.textcol += new_indent - pre_indent;
2726 }
2727
2728 ins_len = (long)STRLEN(firstline) - pre_textlen;
2729 if (ins_len > 0)
2730 {
2731 /* Subsequent calls to ml_get() flush the firstline data - take a
2732 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2734 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002735 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2737 linenr++)
2738 {
2739 block_prep(oap, &bd, linenr, TRUE);
2740 if (!bd.is_short || virtual_op)
2741 {
2742# ifdef FEAT_VIRTUALEDIT
2743 pos_T vpos;
2744
2745 /* If the block starts in virtual space, count the
2746 * initial coladd offset as part of "startspaces" */
2747 if (bd.is_short)
2748 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002749 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752 else
2753 vpos.coladd = 0;
2754# endif
2755 oldp = ml_get(linenr);
2756 newp = alloc_check((unsigned)(STRLEN(oldp)
2757# ifdef FEAT_VIRTUALEDIT
2758 + vpos.coladd
2759# endif
2760 + ins_len + 1));
2761 if (newp == NULL)
2762 continue;
2763 /* copy up to block start */
2764 mch_memmove(newp, oldp, (size_t)bd.textcol);
2765 offset = bd.textcol;
2766# ifdef FEAT_VIRTUALEDIT
2767 copy_spaces(newp + offset, (size_t)vpos.coladd);
2768 offset += vpos.coladd;
2769# endif
2770 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2771 offset += ins_len;
2772 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002773 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 ml_replace(linenr, newp, FALSE);
2775 }
2776 }
2777 check_cursor();
2778
2779 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2780 }
2781 vim_free(ins_text);
2782 }
2783 }
2784#endif
2785
2786 return retval;
2787}
2788
2789/*
2790 * set all the yank registers to empty (called from main())
2791 */
2792 void
2793init_yank()
2794{
2795 int i;
2796
2797 for (i = 0; i < NUM_REGISTERS; ++i)
2798 y_regs[i].y_array = NULL;
2799}
2800
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002801#if defined(EXITFREE) || defined(PROTO)
2802 void
2803clear_registers()
2804{
2805 int i;
2806
2807 for (i = 0; i < NUM_REGISTERS; ++i)
2808 {
2809 y_current = &y_regs[i];
2810 if (y_current->y_array != NULL)
2811 free_yank_all();
2812 }
2813}
2814#endif
2815
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816/*
2817 * Free "n" lines from the current yank register.
2818 * Called for normal freeing and in case of error.
2819 */
2820 static void
2821free_yank(n)
2822 long n;
2823{
2824 if (y_current->y_array != NULL)
2825 {
2826 long i;
2827
2828 for (i = n; --i >= 0; )
2829 {
2830#ifdef AMIGA /* only for very slow machines */
2831 if ((i & 1023) == 1023) /* this may take a while */
2832 {
2833 /*
2834 * This message should never cause a hit-return message.
2835 * Overwrite this message with any next message.
2836 */
2837 ++no_wait_return;
2838 smsg((char_u *)_("freeing %ld lines"), i + 1);
2839 --no_wait_return;
2840 msg_didout = FALSE;
2841 msg_col = 0;
2842 }
2843#endif
2844 vim_free(y_current->y_array[i]);
2845 }
2846 vim_free(y_current->y_array);
2847 y_current->y_array = NULL;
2848#ifdef AMIGA
2849 if (n >= 1000)
2850 MSG("");
2851#endif
2852 }
2853}
2854
2855 static void
2856free_yank_all()
2857{
2858 free_yank(y_current->y_size);
2859}
2860
2861/*
2862 * Yank the text between "oap->start" and "oap->end" into a yank register.
2863 * If we are to append (uppercase register), we first yank into a new yank
2864 * register and then concatenate the old and the new one (so we keep the old
2865 * one in case of out-of-memory).
2866 *
2867 * return FAIL for failure, OK otherwise
2868 */
2869 int
2870op_yank(oap, deleting, mess)
2871 oparg_T *oap;
2872 int deleting;
2873 int mess;
2874{
2875 long y_idx; /* index in y_array[] */
2876 struct yankreg *curr; /* copy of y_current */
2877 struct yankreg newreg; /* new yank register when appending */
2878 char_u **new_ptr;
2879 linenr_T lnum; /* current line number */
2880 long j;
2881 int yanktype = oap->motion_type;
2882 long yanklines = oap->line_count;
2883 linenr_T yankendlnum = oap->end.lnum;
2884 char_u *p;
2885 char_u *pnew;
2886 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002887#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002888 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
2891 /* check for read-only register */
2892 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2893 {
2894 beep_flush();
2895 return FAIL;
2896 }
2897 if (oap->regname == '_') /* black hole: nothing to do */
2898 return OK;
2899
2900#ifdef FEAT_CLIPBOARD
2901 if (!clip_star.available && oap->regname == '*')
2902 oap->regname = 0;
2903 else if (!clip_plus.available && oap->regname == '+')
2904 oap->regname = 0;
2905#endif
2906
2907 if (!deleting) /* op_delete() already set y_current */
2908 get_yank_register(oap->regname, TRUE);
2909
2910 curr = y_current;
2911 /* append to existing contents */
2912 if (y_append && y_current->y_array != NULL)
2913 y_current = &newreg;
2914 else
2915 free_yank_all(); /* free previously yanked lines */
2916
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002917 /*
2918 * If the cursor was in column 1 before and after the movement, and the
2919 * operator is not inclusive, the yank is always linewise.
2920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 if ( oap->motion_type == MCHAR
2922 && oap->start.col == 0
2923 && !oap->inclusive
2924#ifdef FEAT_VISUAL
2925 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002926 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#endif
2928 && oap->end.col == 0
2929 && yanklines > 1)
2930 {
2931 yanktype = MLINE;
2932 --yankendlnum;
2933 --yanklines;
2934 }
2935
2936 y_current->y_size = yanklines;
2937 y_current->y_type = yanktype; /* set the yank register type */
2938#ifdef FEAT_VISUAL
2939 y_current->y_width = 0;
2940#endif
2941 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2942 yanklines), TRUE);
2943
2944 if (y_current->y_array == NULL)
2945 {
2946 y_current = curr;
2947 return FAIL;
2948 }
2949
2950 y_idx = 0;
2951 lnum = oap->start.lnum;
2952
2953#ifdef FEAT_VISUAL
2954 if (oap->block_mode)
2955 {
2956 /* Visual block mode */
2957 y_current->y_type = MBLOCK; /* set the yank register type */
2958 y_current->y_width = oap->end_vcol - oap->start_vcol;
2959
2960 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2961 y_current->y_width--;
2962 }
2963#endif
2964
2965 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2966 {
2967 switch (y_current->y_type)
2968 {
2969#ifdef FEAT_VISUAL
2970 case MBLOCK:
2971 block_prep(oap, &bd, lnum, FALSE);
2972 if (yank_copy_line(&bd, y_idx) == FAIL)
2973 goto fail;
2974 break;
2975#endif
2976
2977 case MLINE:
2978 if ((y_current->y_array[y_idx] =
2979 vim_strsave(ml_get(lnum))) == NULL)
2980 goto fail;
2981 break;
2982
2983 case MCHAR:
2984 {
2985 colnr_T startcol = 0, endcol = MAXCOL;
2986#ifdef FEAT_VIRTUALEDIT
2987 int is_oneChar = FALSE;
2988 colnr_T cs, ce;
2989#endif
2990 p = ml_get(lnum);
2991 bd.startspaces = 0;
2992 bd.endspaces = 0;
2993
2994 if (lnum == oap->start.lnum)
2995 {
2996 startcol = oap->start.col;
2997#ifdef FEAT_VIRTUALEDIT
2998 if (virtual_op)
2999 {
3000 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3001 if (ce != cs && oap->start.coladd > 0)
3002 {
3003 /* Part of a tab selected -- but don't
3004 * double-count it. */
3005 bd.startspaces = (ce - cs + 1)
3006 - oap->start.coladd;
3007 startcol++;
3008 }
3009 }
3010#endif
3011 }
3012
3013 if (lnum == oap->end.lnum)
3014 {
3015 endcol = oap->end.col;
3016#ifdef FEAT_VIRTUALEDIT
3017 if (virtual_op)
3018 {
3019 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3020 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3021# ifdef FEAT_MBYTE
3022 /* Don't add space for double-wide
3023 * char; endcol will be on last byte
3024 * of multi-byte char. */
3025 && (*mb_head_off)(p, p + endcol) == 0
3026# endif
3027 ))
3028 {
3029 if (oap->start.lnum == oap->end.lnum
3030 && oap->start.col == oap->end.col)
3031 {
3032 /* Special case: inside a single char */
3033 is_oneChar = TRUE;
3034 bd.startspaces = oap->end.coladd
3035 - oap->start.coladd + oap->inclusive;
3036 endcol = startcol;
3037 }
3038 else
3039 {
3040 bd.endspaces = oap->end.coladd
3041 + oap->inclusive;
3042 endcol -= oap->inclusive;
3043 }
3044 }
3045 }
3046#endif
3047 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003048 if (endcol == MAXCOL)
3049 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (startcol > endcol
3051#ifdef FEAT_VIRTUALEDIT
3052 || is_oneChar
3053#endif
3054 )
3055 bd.textlen = 0;
3056 else
3057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 bd.textlen = endcol - startcol + oap->inclusive;
3059 }
3060 bd.textstart = p + startcol;
3061 if (yank_copy_line(&bd, y_idx) == FAIL)
3062 goto fail;
3063 break;
3064 }
3065 /* NOTREACHED */
3066 }
3067 }
3068
3069 if (curr != y_current) /* append the new block to the old block */
3070 {
3071 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3072 (curr->y_size + y_current->y_size)), TRUE);
3073 if (new_ptr == NULL)
3074 goto fail;
3075 for (j = 0; j < curr->y_size; ++j)
3076 new_ptr[j] = curr->y_array[j];
3077 vim_free(curr->y_array);
3078 curr->y_array = new_ptr;
3079
3080 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3081 curr->y_type = MLINE;
3082
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003083 /* Concatenate the last line of the old block with the first line of
3084 * the new block, unless being Vi compatible. */
3085 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
3087 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3088 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3089 if (pnew == NULL)
3090 {
3091 y_idx = y_current->y_size - 1;
3092 goto fail;
3093 }
3094 STRCPY(pnew, curr->y_array[--j]);
3095 STRCAT(pnew, y_current->y_array[0]);
3096 vim_free(curr->y_array[j]);
3097 vim_free(y_current->y_array[0]);
3098 curr->y_array[j++] = pnew;
3099 y_idx = 1;
3100 }
3101 else
3102 y_idx = 0;
3103 while (y_idx < y_current->y_size)
3104 curr->y_array[j++] = y_current->y_array[y_idx++];
3105 curr->y_size = j;
3106 vim_free(y_current->y_array);
3107 y_current = curr;
3108 }
3109 if (mess) /* Display message about yank? */
3110 {
3111 if (yanktype == MCHAR
3112#ifdef FEAT_VISUAL
3113 && !oap->block_mode
3114#endif
3115 && yanklines == 1)
3116 yanklines = 0;
3117 /* Some versions of Vi use ">=" here, some don't... */
3118 if (yanklines > p_report)
3119 {
3120 /* redisplay now, so message is not deleted */
3121 update_topline_redraw();
3122 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003123 {
3124#ifdef FEAT_VISUAL
3125 if (oap->block_mode)
3126 MSG(_("block of 1 line yanked"));
3127 else
3128#endif
3129 MSG(_("1 line yanked"));
3130 }
3131#ifdef FEAT_VISUAL
3132 else if (oap->block_mode)
3133 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 else
3136 smsg((char_u *)_("%ld lines yanked"), yanklines);
3137 }
3138 }
3139
3140 /*
3141 * Set "'[" and "']" marks.
3142 */
3143 curbuf->b_op_start = oap->start;
3144 curbuf->b_op_end = oap->end;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003145 if (yanktype == MLINE
3146#ifdef FEAT_VISUAL
3147 && !oap->block_mode
3148#endif
3149 )
3150 {
3151 curbuf->b_op_start.col = 0;
3152 curbuf->b_op_end.col = MAXCOL;
3153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155#ifdef FEAT_CLIPBOARD
3156 /*
3157 * If we were yanking to the '*' register, send result to clipboard.
3158 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3159 * to the '*' register.
3160 */
3161 if (clip_star.available
3162 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003163 || (!deleting && oap->regname == 0
3164 && (clip_unnamed & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 if (curr != &(y_regs[STAR_REGISTER]))
3167 /* Copy the text from register 0 to the clipboard register. */
3168 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3169
3170 clip_own_selection(&clip_star);
3171 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003172# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003173 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176
3177# ifdef FEAT_X11
3178 /*
3179 * If we were yanking to the '+' register, send result to selection.
3180 * Also copy to the '*' register, in case auto-select is off.
3181 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003182 if (clip_plus.available
3183 && (curr == &(y_regs[PLUS_REGISTER])
3184 || (!deleting && oap->regname == 0
3185 && (clip_unnamed & CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003187 if (curr != &(y_regs[PLUS_REGISTER]))
3188 /* Copy the text from register 0 to the clipboard register. */
3189 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 clip_own_selection(&clip_plus);
3192 clip_gen_set_selection(&clip_plus);
Bram Moolenaar337ae062011-04-01 16:28:38 +02003193 if (!clip_isautosel() && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3196 clip_own_selection(&clip_star);
3197 clip_gen_set_selection(&clip_star);
3198 }
3199 }
3200# endif
3201#endif
3202
3203 return OK;
3204
3205fail: /* free the allocated lines */
3206 free_yank(y_idx + 1);
3207 y_current = curr;
3208 return FAIL;
3209}
3210
3211 static int
3212yank_copy_line(bd, y_idx)
3213 struct block_def *bd;
3214 long y_idx;
3215{
3216 char_u *pnew;
3217
3218 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3219 == NULL)
3220 return FAIL;
3221 y_current->y_array[y_idx] = pnew;
3222 copy_spaces(pnew, (size_t)bd->startspaces);
3223 pnew += bd->startspaces;
3224 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3225 pnew += bd->textlen;
3226 copy_spaces(pnew, (size_t)bd->endspaces);
3227 pnew += bd->endspaces;
3228 *pnew = NUL;
3229 return OK;
3230}
3231
3232#ifdef FEAT_CLIPBOARD
3233/*
3234 * Make a copy of the y_current register to register "reg".
3235 */
3236 static void
3237copy_yank_reg(reg)
3238 struct yankreg *reg;
3239{
3240 struct yankreg *curr = y_current;
3241 long j;
3242
3243 y_current = reg;
3244 free_yank_all();
3245 *y_current = *curr;
3246 y_current->y_array = (char_u **)lalloc_clear(
3247 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3248 if (y_current->y_array == NULL)
3249 y_current->y_size = 0;
3250 else
3251 for (j = 0; j < y_current->y_size; ++j)
3252 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3253 {
3254 free_yank(j);
3255 y_current->y_size = 0;
3256 break;
3257 }
3258 y_current = curr;
3259}
3260#endif
3261
3262/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003263 * Put contents of register "regname" into the text.
3264 * Caller must check "regname" to be valid!
3265 * "flags": PUT_FIXINDENT make indent look nice
3266 * PUT_CURSEND leave cursor after end of new text
3267 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 void
3270do_put(regname, dir, count, flags)
3271 int regname;
3272 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3273 long count;
3274 int flags;
3275{
3276 char_u *ptr;
3277 char_u *newp, *oldp;
3278 int yanklen;
3279 int totlen = 0; /* init for gcc */
3280 linenr_T lnum;
3281 colnr_T col;
3282 long i; /* index in y_array[] */
3283 int y_type;
3284 long y_size;
3285#ifdef FEAT_VISUAL
3286 int oldlen;
3287 long y_width = 0;
3288 colnr_T vcol;
3289 int delcount;
3290 int incr = 0;
3291 long j;
3292 struct block_def bd;
3293#endif
3294 char_u **y_array = NULL;
3295 long nr_lines = 0;
3296 pos_T new_cursor;
3297 int indent;
3298 int orig_indent = 0; /* init for gcc */
3299 int indent_diff = 0; /* init for gcc */
3300 int first_indent = TRUE;
3301 int lendiff = 0;
3302 pos_T old_pos;
3303 char_u *insert_string = NULL;
3304 int allocated = FALSE;
3305 long cnt;
3306
3307#ifdef FEAT_CLIPBOARD
3308 /* Adjust register name for "unnamed" in 'clipboard'. */
3309 adjust_clip_reg(&regname);
3310 (void)may_get_selection(regname);
3311#endif
3312
3313 if (flags & PUT_FIXINDENT)
3314 orig_indent = get_indent();
3315
3316 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3317 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3318
3319 /*
3320 * Using inserted text works differently, because the register includes
3321 * special characters (newlines, etc.).
3322 */
3323 if (regname == '.')
3324 {
3325 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3326 (count == -1 ? 'O' : 'i')), count, FALSE);
3327 /* Putting the text is done later, so can't really move the cursor to
3328 * the next character. Use "l" to simulate it. */
3329 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3330 stuffcharReadbuff('l');
3331 return;
3332 }
3333
3334 /*
3335 * For special registers '%' (file name), '#' (alternate file name) and
3336 * ':' (last command line), etc. we have to create a fake yank register.
3337 */
3338 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3339 {
3340 if (insert_string == NULL)
3341 return;
3342 }
3343
3344 if (insert_string != NULL)
3345 {
3346 y_type = MCHAR;
3347#ifdef FEAT_EVAL
3348 if (regname == '=')
3349 {
3350 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003351 * characters.
3352 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 for (;;)
3354 {
3355 y_size = 0;
3356 ptr = insert_string;
3357 while (ptr != NULL)
3358 {
3359 if (y_array != NULL)
3360 y_array[y_size] = ptr;
3361 ++y_size;
3362 ptr = vim_strchr(ptr, '\n');
3363 if (ptr != NULL)
3364 {
3365 if (y_array != NULL)
3366 *ptr = NUL;
3367 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003368 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (*ptr == NUL)
3370 {
3371 y_type = MLINE;
3372 break;
3373 }
3374 }
3375 }
3376 if (y_array != NULL)
3377 break;
3378 y_array = (char_u **)alloc((unsigned)
3379 (y_size * sizeof(char_u *)));
3380 if (y_array == NULL)
3381 goto end;
3382 }
3383 }
3384 else
3385#endif
3386 {
3387 y_size = 1; /* use fake one-line yank register */
3388 y_array = &insert_string;
3389 }
3390 }
3391 else
3392 {
3393 get_yank_register(regname, FALSE);
3394
3395 y_type = y_current->y_type;
3396#ifdef FEAT_VISUAL
3397 y_width = y_current->y_width;
3398#endif
3399 y_size = y_current->y_size;
3400 y_array = y_current->y_array;
3401 }
3402
3403#ifdef FEAT_VISUAL
3404 if (y_type == MLINE)
3405 {
3406 if (flags & PUT_LINE_SPLIT)
3407 {
3408 /* "p" or "P" in Visual mode: split the lines to put the text in
3409 * between. */
3410 if (u_save_cursor() == FAIL)
3411 goto end;
3412 ptr = vim_strsave(ml_get_cursor());
3413 if (ptr == NULL)
3414 goto end;
3415 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3416 vim_free(ptr);
3417
3418 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3419 if (ptr == NULL)
3420 goto end;
3421 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3422 ++nr_lines;
3423 dir = FORWARD;
3424 }
3425 if (flags & PUT_LINE_FORWARD)
3426 {
3427 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003428 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 dir = FORWARD;
3430 }
3431 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3432 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3433 }
3434#endif
3435
3436 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3437 y_type = MLINE;
3438
3439 if (y_size == 0 || y_array == NULL)
3440 {
3441 EMSG2(_("E353: Nothing in register %s"),
3442 regname == 0 ? (char_u *)"\"" : transchar(regname));
3443 goto end;
3444 }
3445
3446#ifdef FEAT_VISUAL
3447 if (y_type == MBLOCK)
3448 {
3449 lnum = curwin->w_cursor.lnum + y_size + 1;
3450 if (lnum > curbuf->b_ml.ml_line_count)
3451 lnum = curbuf->b_ml.ml_line_count + 1;
3452 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3453 goto end;
3454 }
3455 else
3456#endif
3457 if (y_type == MLINE)
3458 {
3459 lnum = curwin->w_cursor.lnum;
3460#ifdef FEAT_FOLDING
3461 /* Correct line number for closed fold. Don't move the cursor yet,
3462 * u_save() uses it. */
3463 if (dir == BACKWARD)
3464 (void)hasFolding(lnum, &lnum, NULL);
3465 else
3466 (void)hasFolding(lnum, NULL, &lnum);
3467#endif
3468 if (dir == FORWARD)
3469 ++lnum;
3470 if (u_save(lnum - 1, lnum) == FAIL)
3471 goto end;
3472#ifdef FEAT_FOLDING
3473 if (dir == FORWARD)
3474 curwin->w_cursor.lnum = lnum - 1;
3475 else
3476 curwin->w_cursor.lnum = lnum;
3477 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3478#endif
3479 }
3480 else if (u_save_cursor() == FAIL)
3481 goto end;
3482
3483 yanklen = (int)STRLEN(y_array[0]);
3484
3485#ifdef FEAT_VIRTUALEDIT
3486 if (ve_flags == VE_ALL && y_type == MCHAR)
3487 {
3488 if (gchar_cursor() == TAB)
3489 {
3490 /* Don't need to insert spaces when "p" on the last position of a
3491 * tab or "P" on the first position. */
3492 if (dir == FORWARD
3493 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3494 : curwin->w_cursor.coladd > 0)
3495 coladvance_force(getviscol());
3496 else
3497 curwin->w_cursor.coladd = 0;
3498 }
3499 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3500 coladvance_force(getviscol() + (dir == FORWARD));
3501 }
3502#endif
3503
3504 lnum = curwin->w_cursor.lnum;
3505 col = curwin->w_cursor.col;
3506
3507#ifdef FEAT_VISUAL
3508 /*
3509 * Block mode
3510 */
3511 if (y_type == MBLOCK)
3512 {
3513 char c = gchar_cursor();
3514 colnr_T endcol2 = 0;
3515
3516 if (dir == FORWARD && c != NUL)
3517 {
3518#ifdef FEAT_VIRTUALEDIT
3519 if (ve_flags == VE_ALL)
3520 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3521 else
3522#endif
3523 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3524
3525#ifdef FEAT_MBYTE
3526 if (has_mbyte)
3527 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003528 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 else
3530#endif
3531#ifdef FEAT_VIRTUALEDIT
3532 if (c != TAB || ve_flags != VE_ALL)
3533#endif
3534 ++curwin->w_cursor.col;
3535 ++col;
3536 }
3537 else
3538 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3539
3540#ifdef FEAT_VIRTUALEDIT
3541 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003542 if (ve_flags == VE_ALL
3543 && (curwin->w_cursor.coladd > 0
3544 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
3546 if (dir == FORWARD && c == NUL)
3547 ++col;
3548 if (dir != FORWARD && c != NUL)
3549 ++curwin->w_cursor.col;
3550 if (c == TAB)
3551 {
3552 if (dir == BACKWARD && curwin->w_cursor.col)
3553 curwin->w_cursor.col--;
3554 if (dir == FORWARD && col - 1 == endcol2)
3555 curwin->w_cursor.col++;
3556 }
3557 }
3558 curwin->w_cursor.coladd = 0;
3559#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003560 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 for (i = 0; i < y_size; ++i)
3562 {
3563 int spaces;
3564 char shortline;
3565
3566 bd.startspaces = 0;
3567 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 vcol = 0;
3569 delcount = 0;
3570
3571 /* add a new line */
3572 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3573 {
3574 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3575 (colnr_T)1, FALSE) == FAIL)
3576 break;
3577 ++nr_lines;
3578 }
3579 /* get the old line and advance to the position to insert at */
3580 oldp = ml_get_curline();
3581 oldlen = (int)STRLEN(oldp);
3582 for (ptr = oldp; vcol < col && *ptr; )
3583 {
3584 /* Count a tab for what it's worth (if list mode not on) */
3585 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3586 vcol += incr;
3587 }
3588 bd.textcol = (colnr_T)(ptr - oldp);
3589
3590 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3591
3592 if (vcol < col) /* line too short, padd with spaces */
3593 bd.startspaces = col - vcol;
3594 else if (vcol > col)
3595 {
3596 bd.endspaces = vcol - col;
3597 bd.startspaces = incr - bd.endspaces;
3598 --bd.textcol;
3599 delcount = 1;
3600#ifdef FEAT_MBYTE
3601 if (has_mbyte)
3602 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3603#endif
3604 if (oldp[bd.textcol] != TAB)
3605 {
3606 /* Only a Tab can be split into spaces. Other
3607 * characters will have to be moved to after the
3608 * block, causing misalignment. */
3609 delcount = 0;
3610 bd.endspaces = 0;
3611 }
3612 }
3613
3614 yanklen = (int)STRLEN(y_array[i]);
3615
3616 /* calculate number of spaces required to fill right side of block*/
3617 spaces = y_width + 1;
3618 for (j = 0; j < yanklen; j++)
3619 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3620 if (spaces < 0)
3621 spaces = 0;
3622
3623 /* insert the new text */
3624 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3625 newp = alloc_check((unsigned)totlen + oldlen + 1);
3626 if (newp == NULL)
3627 break;
3628 /* copy part up to cursor to new line */
3629 ptr = newp;
3630 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3631 ptr += bd.textcol;
3632 /* may insert some spaces before the new text */
3633 copy_spaces(ptr, (size_t)bd.startspaces);
3634 ptr += bd.startspaces;
3635 /* insert the new text */
3636 for (j = 0; j < count; ++j)
3637 {
3638 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3639 ptr += yanklen;
3640
3641 /* insert block's trailing spaces only if there's text behind */
3642 if ((j < count - 1 || !shortline) && spaces)
3643 {
3644 copy_spaces(ptr, (size_t)spaces);
3645 ptr += spaces;
3646 }
3647 }
3648 /* may insert some spaces after the new text */
3649 copy_spaces(ptr, (size_t)bd.endspaces);
3650 ptr += bd.endspaces;
3651 /* move the text after the cursor to the end of the line. */
3652 mch_memmove(ptr, oldp + bd.textcol + delcount,
3653 (size_t)(oldlen - bd.textcol - delcount + 1));
3654 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3655
3656 ++curwin->w_cursor.lnum;
3657 if (i == 0)
3658 curwin->w_cursor.col += bd.startspaces;
3659 }
3660
3661 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3662
3663 /* Set '[ mark. */
3664 curbuf->b_op_start = curwin->w_cursor;
3665 curbuf->b_op_start.lnum = lnum;
3666
3667 /* adjust '] mark */
3668 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3669 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003670# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (flags & PUT_CURSEND)
3674 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003675 colnr_T len;
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 curwin->w_cursor = curbuf->b_op_end;
3678 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003679
3680 /* in Insert mode we might be after the NUL, correct for that */
3681 len = (colnr_T)STRLEN(ml_get_curline());
3682 if (curwin->w_cursor.col > len)
3683 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 else
3686 curwin->w_cursor.lnum = lnum;
3687 }
3688 else
3689#endif
3690 {
3691 /*
3692 * Character or Line mode
3693 */
3694 if (y_type == MCHAR)
3695 {
3696 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3697 * char */
3698 if (dir == FORWARD && gchar_cursor() != NUL)
3699 {
3700#ifdef FEAT_MBYTE
3701 if (has_mbyte)
3702 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003703 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 /* put it on the next of the multi-byte character. */
3706 col += bytelen;
3707 if (yanklen)
3708 {
3709 curwin->w_cursor.col += bytelen;
3710 curbuf->b_op_end.col += bytelen;
3711 }
3712 }
3713 else
3714#endif
3715 {
3716 ++col;
3717 if (yanklen)
3718 {
3719 ++curwin->w_cursor.col;
3720 ++curbuf->b_op_end.col;
3721 }
3722 }
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 curbuf->b_op_start = curwin->w_cursor;
3725 }
3726 /*
3727 * Line mode: BACKWARD is the same as FORWARD on the previous line
3728 */
3729 else if (dir == BACKWARD)
3730 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003731 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /*
3734 * simple case: insert into current line
3735 */
3736 if (y_type == MCHAR && y_size == 1)
3737 {
3738 totlen = count * yanklen;
3739 if (totlen)
3740 {
3741 oldp = ml_get(lnum);
3742 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3743 if (newp == NULL)
3744 goto end; /* alloc() will give error message */
3745 mch_memmove(newp, oldp, (size_t)col);
3746 ptr = newp + col;
3747 for (i = 0; i < count; ++i)
3748 {
3749 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3750 ptr += yanklen;
3751 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003752 STRMOVE(ptr, oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 ml_replace(lnum, newp, FALSE);
3754 /* Put cursor on last putted char. */
3755 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3756 }
3757 curbuf->b_op_end = curwin->w_cursor;
3758 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3759 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3760 ++curwin->w_cursor.col;
3761 changed_bytes(lnum, col);
3762 }
3763 else
3764 {
3765 /*
3766 * Insert at least one line. When y_type is MCHAR, break the first
3767 * line in two.
3768 */
3769 for (cnt = 1; cnt <= count; ++cnt)
3770 {
3771 i = 0;
3772 if (y_type == MCHAR)
3773 {
3774 /*
3775 * Split the current line in two at the insert position.
3776 * First insert y_array[size - 1] in front of second line.
3777 * Then append y_array[0] to first line.
3778 */
3779 lnum = new_cursor.lnum;
3780 ptr = ml_get(lnum) + col;
3781 totlen = (int)STRLEN(y_array[y_size - 1]);
3782 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3783 if (newp == NULL)
3784 goto error;
3785 STRCPY(newp, y_array[y_size - 1]);
3786 STRCAT(newp, ptr);
3787 /* insert second line */
3788 ml_append(lnum, newp, (colnr_T)0, FALSE);
3789 vim_free(newp);
3790
3791 oldp = ml_get(lnum);
3792 newp = alloc_check((unsigned)(col + yanklen + 1));
3793 if (newp == NULL)
3794 goto error;
3795 /* copy first part of line */
3796 mch_memmove(newp, oldp, (size_t)col);
3797 /* append to first line */
3798 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3799 ml_replace(lnum, newp, FALSE);
3800
3801 curwin->w_cursor.lnum = lnum;
3802 i = 1;
3803 }
3804
3805 for (; i < y_size; ++i)
3806 {
3807 if ((y_type != MCHAR || i < y_size - 1)
3808 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3809 == FAIL)
3810 goto error;
3811 lnum++;
3812 ++nr_lines;
3813 if (flags & PUT_FIXINDENT)
3814 {
3815 old_pos = curwin->w_cursor;
3816 curwin->w_cursor.lnum = lnum;
3817 ptr = ml_get(lnum);
3818 if (cnt == count && i == y_size - 1)
3819 lendiff = (int)STRLEN(ptr);
3820#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3821 if (*ptr == '#' && preprocs_left())
3822 indent = 0; /* Leave # lines at start */
3823 else
3824#endif
3825 if (*ptr == NUL)
3826 indent = 0; /* Ignore empty lines */
3827 else if (first_indent)
3828 {
3829 indent_diff = orig_indent - get_indent();
3830 indent = orig_indent;
3831 first_indent = FALSE;
3832 }
3833 else if ((indent = get_indent() + indent_diff) < 0)
3834 indent = 0;
3835 (void)set_indent(indent, 0);
3836 curwin->w_cursor = old_pos;
3837 /* remember how many chars were removed */
3838 if (cnt == count && i == y_size - 1)
3839 lendiff -= (int)STRLEN(ml_get(lnum));
3840 }
3841 }
3842 }
3843
3844error:
3845 /* Adjust marks. */
3846 if (y_type == MLINE)
3847 {
3848 curbuf->b_op_start.col = 0;
3849 if (dir == FORWARD)
3850 curbuf->b_op_start.lnum++;
3851 }
3852 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3853 (linenr_T)MAXLNUM, nr_lines, 0L);
3854
3855 /* note changed text for displaying and folding */
3856 if (y_type == MCHAR)
3857 changed_lines(curwin->w_cursor.lnum, col,
3858 curwin->w_cursor.lnum + 1, nr_lines);
3859 else
3860 changed_lines(curbuf->b_op_start.lnum, 0,
3861 curbuf->b_op_start.lnum, nr_lines);
3862
3863 /* put '] mark at last inserted character */
3864 curbuf->b_op_end.lnum = lnum;
3865 /* correct length for change in indent */
3866 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3867 if (col > 1)
3868 curbuf->b_op_end.col = col - 1;
3869 else
3870 curbuf->b_op_end.col = 0;
3871
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003872 if (flags & PUT_CURSLINE)
3873 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003874 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003875 curwin->w_cursor.lnum = lnum;
3876 beginline(BL_WHITE | BL_FIX);
3877 }
3878 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
3880 /* put cursor after inserted text */
3881 if (y_type == MLINE)
3882 {
3883 if (lnum >= curbuf->b_ml.ml_line_count)
3884 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3885 else
3886 curwin->w_cursor.lnum = lnum + 1;
3887 curwin->w_cursor.col = 0;
3888 }
3889 else
3890 {
3891 curwin->w_cursor.lnum = lnum;
3892 curwin->w_cursor.col = col;
3893 }
3894 }
3895 else if (y_type == MLINE)
3896 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003897 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 curwin->w_cursor.col = 0;
3899 if (dir == FORWARD)
3900 ++curwin->w_cursor.lnum;
3901 beginline(BL_WHITE | BL_FIX);
3902 }
3903 else /* put cursor on first inserted character */
3904 curwin->w_cursor = new_cursor;
3905 }
3906 }
3907
3908 msgmore(nr_lines);
3909 curwin->w_set_curswant = TRUE;
3910
3911end:
3912 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003914 if (regname == '=')
3915 vim_free(y_array);
3916
Bram Moolenaar677ee682005-01-27 14:41:15 +00003917 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003918 adjust_cursor_eol();
3919}
3920
3921/*
3922 * When the cursor is on the NUL past the end of the line and it should not be
3923 * there move it left.
3924 */
3925 void
3926adjust_cursor_eol()
3927{
3928 if (curwin->w_cursor.col > 0
3929 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003930#ifdef FEAT_VIRTUALEDIT
3931 && (ve_flags & VE_ONEMORE) == 0
3932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 && !(restart_edit || (State & INSERT)))
3934 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003935 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003936 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#ifdef FEAT_VIRTUALEDIT
3939 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003940 {
3941 colnr_T scol, ecol;
3942
3943 /* Coladd is set to the width of the last character. */
3944 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3945 curwin->w_cursor.coladd = ecol - scol + 1;
3946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947#endif
3948 }
3949}
3950
3951#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3952/*
3953 * Return TRUE if lines starting with '#' should be left aligned.
3954 */
3955 int
3956preprocs_left()
3957{
3958 return
3959# ifdef FEAT_SMARTINDENT
3960# ifdef FEAT_CINDENT
3961 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3962# else
3963 curbuf->b_p_si
3964# endif
3965# endif
3966# ifdef FEAT_CINDENT
3967 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3968# endif
3969 ;
3970}
3971#endif
3972
3973/* Return the character name of the register with the given number */
3974 int
3975get_register_name(num)
3976 int num;
3977{
3978 if (num == -1)
3979 return '"';
3980 else if (num < 10)
3981 return num + '0';
3982 else if (num == DELETION_REGISTER)
3983 return '-';
3984#ifdef FEAT_CLIPBOARD
3985 else if (num == STAR_REGISTER)
3986 return '*';
3987 else if (num == PLUS_REGISTER)
3988 return '+';
3989#endif
3990 else
3991 {
3992#ifdef EBCDIC
3993 int i;
3994
3995 /* EBCDIC is really braindead ... */
3996 i = 'a' + (num - 10);
3997 if (i > 'i')
3998 i += 7;
3999 if (i > 'r')
4000 i += 8;
4001 return i;
4002#else
4003 return num + 'a' - 10;
4004#endif
4005 }
4006}
4007
4008/*
4009 * ":dis" and ":registers": Display the contents of the yank registers.
4010 */
4011 void
4012ex_display(eap)
4013 exarg_T *eap;
4014{
4015 int i, n;
4016 long j;
4017 char_u *p;
4018 struct yankreg *yb;
4019 int name;
4020 int attr;
4021 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004022#ifdef FEAT_MBYTE
4023 int clen;
4024#else
4025# define clen 1
4026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
4028 if (arg != NULL && *arg == NUL)
4029 arg = NULL;
4030 attr = hl_attr(HLF_8);
4031
4032 /* Highlight title */
4033 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4034 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4035 {
4036 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004037 if (arg != NULL && vim_strchr(arg, name) == NULL
4038#ifdef ONE_CLIPBOARD
4039 /* Star register and plus register contain the same thing. */
4040 && (name != '*' || vim_strchr(arg, '+') == NULL)
4041#endif
4042 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 continue; /* did not ask for this register */
4044
4045#ifdef FEAT_CLIPBOARD
4046 /* Adjust register name for "unnamed" in 'clipboard'.
4047 * When it's a clipboard register, fill it with the current contents
4048 * of the clipboard. */
4049 adjust_clip_reg(&name);
4050 (void)may_get_selection(name);
4051#endif
4052
4053 if (i == -1)
4054 {
4055 if (y_previous != NULL)
4056 yb = y_previous;
4057 else
4058 yb = &(y_regs[0]);
4059 }
4060 else
4061 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004062
4063#ifdef FEAT_EVAL
4064 if (name == MB_TOLOWER(redir_reg)
4065 || (redir_reg == '"' && yb == y_previous))
4066 continue; /* do not list register being written to, the
4067 * pointer can be freed */
4068#endif
4069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (yb->y_array != NULL)
4071 {
4072 msg_putchar('\n');
4073 msg_putchar('"');
4074 msg_putchar(name);
4075 MSG_PUTS(" ");
4076
4077 n = (int)Columns - 6;
4078 for (j = 0; j < yb->y_size && n > 1; ++j)
4079 {
4080 if (j)
4081 {
4082 MSG_PUTS_ATTR("^J", attr);
4083 n -= 2;
4084 }
4085 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004088 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004089#endif
4090 msg_outtrans_len(p, clen);
4091#ifdef FEAT_MBYTE
4092 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#endif
4094 }
4095 }
4096 if (n > 1 && yb->y_type == MLINE)
4097 MSG_PUTS_ATTR("^J", attr);
4098 out_flush(); /* show one line at a time */
4099 }
4100 ui_breakcheck();
4101 }
4102
4103 /*
4104 * display last inserted text
4105 */
4106 if ((p = get_last_insert()) != NULL
4107 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4108 {
4109 MSG_PUTS("\n\". ");
4110 dis_msg(p, TRUE);
4111 }
4112
4113 /*
4114 * display last command line
4115 */
4116 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4117 && !got_int)
4118 {
4119 MSG_PUTS("\n\": ");
4120 dis_msg(last_cmdline, FALSE);
4121 }
4122
4123 /*
4124 * display current file name
4125 */
4126 if (curbuf->b_fname != NULL
4127 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4128 {
4129 MSG_PUTS("\n\"% ");
4130 dis_msg(curbuf->b_fname, FALSE);
4131 }
4132
4133 /*
4134 * display alternate file name
4135 */
4136 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4137 {
4138 char_u *fname;
4139 linenr_T dummy;
4140
4141 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4142 {
4143 MSG_PUTS("\n\"# ");
4144 dis_msg(fname, FALSE);
4145 }
4146 }
4147
4148 /*
4149 * display last search pattern
4150 */
4151 if (last_search_pat() != NULL
4152 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4153 {
4154 MSG_PUTS("\n\"/ ");
4155 dis_msg(last_search_pat(), FALSE);
4156 }
4157
4158#ifdef FEAT_EVAL
4159 /*
4160 * display last used expression
4161 */
4162 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4163 && !got_int)
4164 {
4165 MSG_PUTS("\n\"= ");
4166 dis_msg(expr_line, FALSE);
4167 }
4168#endif
4169}
4170
4171/*
4172 * display a string for do_dis()
4173 * truncate at end of screen line
4174 */
4175 static void
4176dis_msg(p, skip_esc)
4177 char_u *p;
4178 int skip_esc; /* if TRUE, ignore trailing ESC */
4179{
4180 int n;
4181#ifdef FEAT_MBYTE
4182 int l;
4183#endif
4184
4185 n = (int)Columns - 6;
4186 while (*p != NUL
4187 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4188 && (n -= ptr2cells(p)) >= 0)
4189 {
4190#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004191 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
4193 msg_outtrans_len(p, l);
4194 p += l;
4195 }
4196 else
4197#endif
4198 msg_outtrans_len(p++, 1);
4199 }
4200 ui_breakcheck();
4201}
4202
Bram Moolenaar81340392012-06-06 16:12:59 +02004203#if defined(FEAT_COMMENTS) || defined(PROTO)
4204/*
4205 * If "process" is TRUE and the line begins with a comment leader (possibly
4206 * after some white space), return a pointer to the text after it. Put a boolean
4207 * value indicating whether the line ends with an unclosed comment in
4208 * "is_comment".
4209 * line - line to be processed,
4210 * process - if FALSE, will only check whether the line ends with an unclosed
4211 * comment,
4212 * include_space - whether to also skip space following the comment leader,
4213 * is_comment - will indicate whether the current line ends with an unclosed
4214 * comment.
4215 */
4216 static char_u *
4217skip_comment(line, process, include_space, is_comment)
4218 char_u *line;
4219 int process;
4220 int include_space;
4221 int *is_comment;
4222{
4223 char_u *comment_flags = NULL;
4224 int lead_len;
4225 int leader_offset = get_last_leader_offset(line, &comment_flags);
4226
4227 *is_comment = FALSE;
4228 if (leader_offset != -1)
4229 {
4230 /* Let's check whether the line ends with an unclosed comment.
4231 * If the last comment leader has COM_END in flags, there's no comment.
4232 */
4233 while (*comment_flags)
4234 {
4235 if (*comment_flags == COM_END
4236 || *comment_flags == ':')
4237 break;
4238 ++comment_flags;
4239 }
4240 if (*comment_flags != COM_END)
4241 *is_comment = TRUE;
4242 }
4243
4244 if (process == FALSE)
4245 return line;
4246
4247 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4248
4249 if (lead_len == 0)
4250 return line;
4251
4252 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004253 * - COM_END,
4254 * - colon,
4255 * whichever comes first.
4256 */
4257 while (*comment_flags)
4258 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004259 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004260 || *comment_flags == ':')
4261 {
4262 break;
4263 }
4264 ++comment_flags;
4265 }
4266
4267 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004268 * starting with a closing part of a three-part comment. That's good,
4269 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004270 */
4271 if (*comment_flags == ':' || *comment_flags == NUL)
4272 line += lead_len;
4273
4274 return line;
4275}
4276#endif
4277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004279 * Join 'count' lines (minimal 2) at cursor position.
4280 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaar81340392012-06-06 16:12:59 +02004281 * Set "use_formatoptions" to FALSE when e.g. processing
4282 * backspace and comment leaders should not be removed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004284 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 */
4286 int
Bram Moolenaar81340392012-06-06 16:12:59 +02004287do_join(count, insert_space, save_undo, use_formatoptions)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004288 long count;
4289 int insert_space;
4290 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004291 int use_formatoptions UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004293 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004294 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004295 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004297 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004298 int endcurr1 = NUL;
4299 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004300 int currsize = 0; /* size of the current line */
4301 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004303 colnr_T col = 0;
4304 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004305#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004306 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004307 int remove_comments = (use_formatoptions == TRUE)
4308 && has_format_option(FO_REMOVE_COMS);
4309 int prev_was_comment;
4310#endif
4311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004313 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4314 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4315 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004317 /* Allocate an array to store the number of spaces inserted before each
4318 * line. We will use it to pre-compute the length of the new line and the
4319 * proper placement of each original line in the new one. */
4320 spaces = lalloc_clear((long_u)count, TRUE);
4321 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004323#if defined(FEAT_COMMENTS) || defined(PROTO)
4324 if (remove_comments)
4325 {
4326 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4327 if (comments == NULL)
4328 {
4329 vim_free(spaces);
4330 return FAIL;
4331 }
4332 }
4333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004336 * Don't move anything, just compute the final line length
4337 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004339 for (t = 0; t < count; ++t)
4340 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004341 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar81340392012-06-06 16:12:59 +02004342#if defined(FEAT_COMMENTS) || defined(PROTO)
4343 if (remove_comments)
4344 {
4345 /* We don't want to remove the comment leader if the
4346 * previous line is not a comment. */
4347 if (t > 0 && prev_was_comment)
4348 {
4349
4350 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4351 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004352 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004353 curr = new_curr;
4354 }
4355 else
4356 curr = skip_comment(curr, FALSE, insert_space,
4357 &prev_was_comment);
4358 }
4359#endif
4360
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004361 if (insert_space && t > 0)
4362 {
4363 curr = skipwhite(curr);
4364 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4365#ifdef FEAT_MBYTE
4366 && (!has_format_option(FO_MBYTE_JOIN)
4367 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4368 && (!has_format_option(FO_MBYTE_JOIN2)
4369 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4370#endif
4371 )
4372 {
4373 /* don't add a space if the line is ending in a space */
4374 if (endcurr1 == ' ')
4375 endcurr1 = endcurr2;
4376 else
4377 ++spaces[t];
4378 /* extra space when 'joinspaces' set and line ends in '.' */
4379 if ( p_js
4380 && (endcurr1 == '.'
4381 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4382 && (endcurr1 == '?' || endcurr1 == '!'))))
4383 ++spaces[t];
4384 }
4385 }
4386 currsize = (int)STRLEN(curr);
4387 sumsize += currsize + spaces[t];
4388 endcurr1 = endcurr2 = NUL;
4389 if (insert_space && currsize > 0)
4390 {
4391#ifdef FEAT_MBYTE
4392 if (has_mbyte)
4393 {
4394 cend = curr + currsize;
4395 mb_ptr_back(curr, cend);
4396 endcurr1 = (*mb_ptr2char)(cend);
4397 if (cend > curr)
4398 {
4399 mb_ptr_back(curr, cend);
4400 endcurr2 = (*mb_ptr2char)(cend);
4401 }
4402 }
4403 else
4404#endif
4405 {
4406 endcurr1 = *(curr + currsize - 1);
4407 if (currsize > 1)
4408 endcurr2 = *(curr + currsize - 2);
4409 }
4410 }
4411 line_breakcheck();
4412 if (got_int)
4413 {
4414 ret = FAIL;
4415 goto theend;
4416 }
4417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004419 /* store the column position before last line */
4420 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004422 /* allocate the space for the new line */
4423 newp = alloc_check((unsigned)(sumsize + 1));
4424 cend = newp + sumsize;
4425 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004427 /*
4428 * Move affected lines to the new long one.
4429 *
4430 * Move marks from each deleted line to the joined line, adjusting the
4431 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4432 * should not really be a problem.
4433 */
4434 for (t = count - 1; ; --t)
4435 {
4436 cend -= currsize;
4437 mch_memmove(cend, curr, (size_t)currsize);
4438 if (spaces[t] > 0)
4439 {
4440 cend -= spaces[t];
4441 copy_spaces(cend, (size_t)(spaces[t]));
4442 }
4443 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004444 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004445 if (t == 0)
4446 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004447 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004448#if defined(FEAT_COMMENTS) || defined(PROTO)
4449 if (remove_comments)
4450 curr += comments[t - 1];
4451#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004452 if (insert_space && t > 1)
4453 curr = skipwhite(curr);
4454 currsize = (int)STRLEN(curr);
4455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4457
4458 /* Only report the change in the first line here, del_lines() will report
4459 * the deleted line. */
4460 changed_lines(curwin->w_cursor.lnum, currsize,
4461 curwin->w_cursor.lnum + 1, 0L);
4462
4463 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004464 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 * briefly, and then move it back. After del_lines() the cursor may
4466 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 */
4468 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004470 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 curwin->w_cursor.lnum = t;
4472
4473 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004474 * Set the cursor column:
4475 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004476 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004478 curwin->w_cursor.col =
4479 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482#ifdef FEAT_VIRTUALEDIT
4483 curwin->w_cursor.coladd = 0;
4484#endif
4485 curwin->w_set_curswant = TRUE;
4486
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004487theend:
4488 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004489#if defined(FEAT_COMMENTS) || defined(PROTO)
4490 if (remove_comments)
4491 vim_free(comments);
4492#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004493 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494}
4495
4496#ifdef FEAT_COMMENTS
4497/*
4498 * Return TRUE if the two comment leaders given are the same. "lnum" is
4499 * the first line. White-space is ignored. Note that the whole of
4500 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4501 */
4502 static int
4503same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4504 linenr_T lnum;
4505 int leader1_len;
4506 char_u *leader1_flags;
4507 int leader2_len;
4508 char_u *leader2_flags;
4509{
4510 int idx1 = 0, idx2 = 0;
4511 char_u *p;
4512 char_u *line1;
4513 char_u *line2;
4514
4515 if (leader1_len == 0)
4516 return (leader2_len == 0);
4517
4518 /*
4519 * If first leader has 'f' flag, the lines can be joined only if the
4520 * second line does not have a leader.
4521 * If first leader has 'e' flag, the lines can never be joined.
4522 * If fist leader has 's' flag, the lines can only be joined if there is
4523 * some text after it and the second line has the 'm' flag.
4524 */
4525 if (leader1_flags != NULL)
4526 {
4527 for (p = leader1_flags; *p && *p != ':'; ++p)
4528 {
4529 if (*p == COM_FIRST)
4530 return (leader2_len == 0);
4531 if (*p == COM_END)
4532 return FALSE;
4533 if (*p == COM_START)
4534 {
4535 if (*(ml_get(lnum) + leader1_len) == NUL)
4536 return FALSE;
4537 if (leader2_flags == NULL || leader2_len == 0)
4538 return FALSE;
4539 for (p = leader2_flags; *p && *p != ':'; ++p)
4540 if (*p == COM_MIDDLE)
4541 return TRUE;
4542 return FALSE;
4543 }
4544 }
4545 }
4546
4547 /*
4548 * Get current line and next line, compare the leaders.
4549 * The first line has to be saved, only one line can be locked at a time.
4550 */
4551 line1 = vim_strsave(ml_get(lnum));
4552 if (line1 != NULL)
4553 {
4554 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4555 ;
4556 line2 = ml_get(lnum + 1);
4557 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4558 {
4559 if (!vim_iswhite(line2[idx2]))
4560 {
4561 if (line1[idx1++] != line2[idx2])
4562 break;
4563 }
4564 else
4565 while (vim_iswhite(line1[idx1]))
4566 ++idx1;
4567 }
4568 vim_free(line1);
4569 }
4570 return (idx2 == leader2_len && idx1 == leader1_len);
4571}
4572#endif
4573
4574/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004575 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
4577 void
4578op_format(oap, keep_cursor)
4579 oparg_T *oap;
4580 int keep_cursor; /* keep cursor on same text char */
4581{
4582 long old_line_count = curbuf->b_ml.ml_line_count;
4583
4584 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4585 * can put it back there. */
4586 curwin->w_cursor = oap->cursor_start;
4587
4588 if (u_save((linenr_T)(oap->start.lnum - 1),
4589 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4590 return;
4591 curwin->w_cursor = oap->start;
4592
4593#ifdef FEAT_VISUAL
4594 if (oap->is_VIsual)
4595 /* When there is no change: need to remove the Visual selection */
4596 redraw_curbuf_later(INVERTED);
4597#endif
4598
4599 /* Set '[ mark at the start of the formatted area */
4600 curbuf->b_op_start = oap->start;
4601
4602 /* For "gw" remember the cursor position and put it back below (adjusted
4603 * for joined and split lines). */
4604 if (keep_cursor)
4605 saved_cursor = oap->cursor_start;
4606
Bram Moolenaar81a82092008-03-12 16:27:00 +00004607 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609 /*
4610 * Leave the cursor at the first non-blank of the last formatted line.
4611 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4612 * line, so "." will do the next lines.
4613 */
4614 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4615 ++curwin->w_cursor.lnum;
4616 beginline(BL_WHITE | BL_FIX);
4617 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4618 msgmore(old_line_count);
4619
4620 /* put '] mark on the end of the formatted area */
4621 curbuf->b_op_end = curwin->w_cursor;
4622
4623 if (keep_cursor)
4624 {
4625 curwin->w_cursor = saved_cursor;
4626 saved_cursor.lnum = 0;
4627 }
4628
4629#ifdef FEAT_VISUAL
4630 if (oap->is_VIsual)
4631 {
4632 win_T *wp;
4633
4634 FOR_ALL_WINDOWS(wp)
4635 {
4636 if (wp->w_old_cursor_lnum != 0)
4637 {
4638 /* When lines have been inserted or deleted, adjust the end of
4639 * the Visual area to be redrawn. */
4640 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4641 wp->w_old_cursor_lnum += old_line_count;
4642 else
4643 wp->w_old_visual_lnum += old_line_count;
4644 }
4645 }
4646 }
4647#endif
4648}
4649
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004650#if defined(FEAT_EVAL) || defined(PROTO)
4651/*
4652 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4653 */
4654 void
4655op_formatexpr(oap)
4656 oparg_T *oap;
4657{
4658# ifdef FEAT_VISUAL
4659 if (oap->is_VIsual)
4660 /* When there is no change: need to remove the Visual selection */
4661 redraw_curbuf_later(INVERTED);
4662# endif
4663
Bram Moolenaar700303e2010-07-11 17:35:50 +02004664 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4665 /* As documented: when 'formatexpr' returns non-zero fall back to
4666 * internal formatting. */
4667 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004668}
4669
4670 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004671fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004672 linenr_T lnum;
4673 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004674 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004675{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004676 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4677 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004678 int r;
4679
4680 /*
4681 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004682 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004683 */
4684 set_vim_var_nr(VV_LNUM, lnum);
4685 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004686 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004687
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004688 /*
4689 * Evaluate the function.
4690 */
4691 if (use_sandbox)
4692 ++sandbox;
4693 r = eval_to_number(curbuf->b_p_fex);
4694 if (use_sandbox)
4695 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004696
4697 set_vim_var_string(VV_CHAR, NULL, -1);
4698
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004699 return r;
4700}
4701#endif
4702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703/*
4704 * Format "line_count" lines, starting at the cursor position.
4705 * When "line_count" is negative, format until the end of the paragraph.
4706 * Lines after the cursor line are saved for undo, caller must have saved the
4707 * first line.
4708 */
4709 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004710format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004712 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
4714 int max_len;
4715 int is_not_par; /* current line not part of parag. */
4716 int next_is_not_par; /* next line not part of paragraph */
4717 int is_end_par; /* at end of paragraph */
4718 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4719 int next_is_start_par = FALSE;
4720#ifdef FEAT_COMMENTS
4721 int leader_len = 0; /* leader len of current line */
4722 int next_leader_len; /* leader len of next line */
4723 char_u *leader_flags = NULL; /* flags for leader of current line */
4724 char_u *next_leader_flags; /* flags for leader of next line */
4725 int do_comments; /* format comments */
4726#endif
4727 int advance = TRUE;
4728 int second_indent = -1;
4729 int do_second_indent;
4730 int do_number_indent;
4731 int do_trail_white;
4732 int first_par_line = TRUE;
4733 int smd_save;
4734 long count;
4735 int need_set_indent = TRUE; /* set indent of next paragraph */
4736 int force_format = FALSE;
4737 int old_State = State;
4738
4739 /* length of a line to force formatting: 3 * 'tw' */
4740 max_len = comp_textwidth(TRUE) * 3;
4741
4742 /* check for 'q', '2' and '1' in 'formatoptions' */
4743#ifdef FEAT_COMMENTS
4744 do_comments = has_format_option(FO_Q_COMS);
4745#endif
4746 do_second_indent = has_format_option(FO_Q_SECOND);
4747 do_number_indent = has_format_option(FO_Q_NUMBER);
4748 do_trail_white = has_format_option(FO_WHITE_PAR);
4749
4750 /*
4751 * Get info about the previous and current line.
4752 */
4753 if (curwin->w_cursor.lnum > 1)
4754 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4755#ifdef FEAT_COMMENTS
4756 , &leader_len, &leader_flags, do_comments
4757#endif
4758 );
4759 else
4760 is_not_par = TRUE;
4761 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4762#ifdef FEAT_COMMENTS
4763 , &next_leader_len, &next_leader_flags, do_comments
4764#endif
4765 );
4766 is_end_par = (is_not_par || next_is_not_par);
4767 if (!is_end_par && do_trail_white)
4768 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4769
4770 curwin->w_cursor.lnum--;
4771 for (count = line_count; count != 0 && !got_int; --count)
4772 {
4773 /*
4774 * Advance to next paragraph.
4775 */
4776 if (advance)
4777 {
4778 curwin->w_cursor.lnum++;
4779 prev_is_end_par = is_end_par;
4780 is_not_par = next_is_not_par;
4781#ifdef FEAT_COMMENTS
4782 leader_len = next_leader_len;
4783 leader_flags = next_leader_flags;
4784#endif
4785 }
4786
4787 /*
4788 * The last line to be formatted.
4789 */
4790 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4791 {
4792 next_is_not_par = TRUE;
4793#ifdef FEAT_COMMENTS
4794 next_leader_len = 0;
4795 next_leader_flags = NULL;
4796#endif
4797 }
4798 else
4799 {
4800 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4801#ifdef FEAT_COMMENTS
4802 , &next_leader_len, &next_leader_flags, do_comments
4803#endif
4804 );
4805 if (do_number_indent)
4806 next_is_start_par =
4807 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4808 }
4809 advance = TRUE;
4810 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4811 if (!is_end_par && do_trail_white)
4812 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4813
4814 /*
4815 * Skip lines that are not in a paragraph.
4816 */
4817 if (is_not_par)
4818 {
4819 if (line_count < 0)
4820 break;
4821 }
4822 else
4823 {
4824 /*
4825 * For the first line of a paragraph, check indent of second line.
4826 * Don't do this for comments and empty lines.
4827 */
4828 if (first_par_line
4829 && (do_second_indent || do_number_indent)
4830 && prev_is_end_par
4831 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4832#ifdef FEAT_COMMENTS
4833 && leader_len == 0
4834 && next_leader_len == 0
4835#endif
4836 )
4837 {
4838 if (do_second_indent
4839 && !lineempty(curwin->w_cursor.lnum + 1))
4840 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4841 else if (do_number_indent)
4842 second_indent = get_number_indent(curwin->w_cursor.lnum);
4843 }
4844
4845 /*
4846 * When the comment leader changes, it's the end of the paragraph.
4847 */
4848 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4849#ifdef FEAT_COMMENTS
4850 || !same_leader(curwin->w_cursor.lnum,
4851 leader_len, leader_flags,
4852 next_leader_len, next_leader_flags)
4853#endif
4854 )
4855 is_end_par = TRUE;
4856
4857 /*
4858 * If we have got to the end of a paragraph, or the line is
4859 * getting long, format it.
4860 */
4861 if (is_end_par || force_format)
4862 {
4863 if (need_set_indent)
4864 /* replace indent in first line with minimal number of
4865 * tabs and spaces, according to current options */
4866 (void)set_indent(get_indent(), SIN_CHANGED);
4867
4868 /* put cursor on last non-space */
4869 State = NORMAL; /* don't go past end-of-line */
4870 coladvance((colnr_T)MAXCOL);
4871 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4872 dec_cursor();
4873
4874 /* do the formatting, without 'showmode' */
4875 State = INSERT; /* for open_line() */
4876 smd_save = p_smd;
4877 p_smd = FALSE;
4878 insertchar(NUL, INSCHAR_FORMAT
4879#ifdef FEAT_COMMENTS
4880 + (do_comments ? INSCHAR_DO_COM : 0)
4881#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004882 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 State = old_State;
4884 p_smd = smd_save;
4885 second_indent = -1;
4886 /* at end of par.: need to set indent of next par. */
4887 need_set_indent = is_end_par;
4888 if (is_end_par)
4889 {
4890 /* When called with a negative line count, break at the
4891 * end of the paragraph. */
4892 if (line_count < 0)
4893 break;
4894 first_par_line = TRUE;
4895 }
4896 force_format = FALSE;
4897 }
4898
4899 /*
4900 * When still in same paragraph, join the lines together. But
4901 * first delete the comment leader from the second line.
4902 */
4903 if (!is_end_par)
4904 {
4905 advance = FALSE;
4906 curwin->w_cursor.lnum++;
4907 curwin->w_cursor.col = 0;
4908 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#ifdef FEAT_COMMENTS
Bram Moolenaare3226be2005-12-18 22:10:00 +00004911 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 if (next_leader_len > 0)
4913 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4914 (long)-next_leader_len);
4915#endif
4916 curwin->w_cursor.lnum--;
Bram Moolenaar81340392012-06-06 16:12:59 +02004917 if (do_join(2, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
4919 beep_flush();
4920 break;
4921 }
4922 first_par_line = FALSE;
4923 /* If the line is getting long, format it next time */
4924 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4925 force_format = TRUE;
4926 else
4927 force_format = FALSE;
4928 }
4929 }
4930 line_breakcheck();
4931 }
4932}
4933
4934/*
4935 * Return TRUE if line "lnum" ends in a white character.
4936 */
4937 static int
4938ends_in_white(lnum)
4939 linenr_T lnum;
4940{
4941 char_u *s = ml_get(lnum);
4942 size_t l;
4943
4944 if (*s == NUL)
4945 return FALSE;
4946 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4947 * invocation may call function multiple times". */
4948 l = STRLEN(s) - 1;
4949 return vim_iswhite(s[l]);
4950}
4951
4952/*
4953 * Blank lines, and lines containing only the comment leader, are left
4954 * untouched by the formatting. The function returns TRUE in this
4955 * case. It also returns TRUE when a line starts with the end of a comment
4956 * ('e' in comment flags), so that this line is skipped, and not joined to the
4957 * previous line. A new paragraph starts after a blank line, or when the
4958 * comment leader changes -- webb.
4959 */
4960#ifdef FEAT_COMMENTS
4961 static int
4962fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4963 linenr_T lnum;
4964 int *leader_len;
4965 char_u **leader_flags;
4966 int do_comments;
4967{
4968 char_u *flags = NULL; /* init for GCC */
4969 char_u *ptr;
4970
4971 ptr = ml_get(lnum);
4972 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02004973 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 else
4975 *leader_len = 0;
4976
4977 if (*leader_len > 0)
4978 {
4979 /*
4980 * Search for 'e' flag in comment leader flags.
4981 */
4982 flags = *leader_flags;
4983 while (*flags && *flags != ':' && *flags != COM_END)
4984 ++flags;
4985 }
4986
4987 return (*skipwhite(ptr + *leader_len) == NUL
4988 || (*leader_len > 0 && *flags == COM_END)
4989 || startPS(lnum, NUL, FALSE));
4990}
4991#else
4992 static int
4993fmt_check_par(lnum)
4994 linenr_T lnum;
4995{
4996 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4997}
4998#endif
4999
5000/*
5001 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5002 * previous line is in the same paragraph. Used for auto-formatting.
5003 */
5004 int
5005paragraph_start(lnum)
5006 linenr_T lnum;
5007{
5008 char_u *p;
5009#ifdef FEAT_COMMENTS
5010 int leader_len = 0; /* leader len of current line */
5011 char_u *leader_flags = NULL; /* flags for leader of current line */
5012 int next_leader_len; /* leader len of next line */
5013 char_u *next_leader_flags; /* flags for leader of next line */
5014 int do_comments; /* format comments */
5015#endif
5016
5017 if (lnum <= 1)
5018 return TRUE; /* start of the file */
5019
5020 p = ml_get(lnum - 1);
5021 if (*p == NUL)
5022 return TRUE; /* after empty line */
5023
5024#ifdef FEAT_COMMENTS
5025 do_comments = has_format_option(FO_Q_COMS);
5026#endif
5027 if (fmt_check_par(lnum - 1
5028#ifdef FEAT_COMMENTS
5029 , &leader_len, &leader_flags, do_comments
5030#endif
5031 ))
5032 return TRUE; /* after non-paragraph line */
5033
5034 if (fmt_check_par(lnum
5035#ifdef FEAT_COMMENTS
5036 , &next_leader_len, &next_leader_flags, do_comments
5037#endif
5038 ))
5039 return TRUE; /* "lnum" is not a paragraph line */
5040
5041 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5042 return TRUE; /* missing trailing space in previous line. */
5043
5044 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5045 return TRUE; /* numbered item starts in "lnum". */
5046
5047#ifdef FEAT_COMMENTS
5048 if (!same_leader(lnum - 1, leader_len, leader_flags,
5049 next_leader_len, next_leader_flags))
5050 return TRUE; /* change of comment leader. */
5051#endif
5052
5053 return FALSE;
5054}
5055
5056#ifdef FEAT_VISUAL
5057/*
5058 * prepare a few things for block mode yank/delete/tilde
5059 *
5060 * for delete:
5061 * - textlen includes the first/last char to be (partly) deleted
5062 * - start/endspaces is the number of columns that are taken by the
5063 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005064 * deleted.
5065 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 * - textlen includes the first/last char to be wholly yanked
5067 * - start/endspaces is the number of columns of the first/last yanked char
5068 * that are to be yanked.
5069 */
5070 static void
5071block_prep(oap, bdp, lnum, is_del)
5072 oparg_T *oap;
5073 struct block_def *bdp;
5074 linenr_T lnum;
5075 int is_del;
5076{
5077 int incr = 0;
5078 char_u *pend;
5079 char_u *pstart;
5080 char_u *line;
5081 char_u *prev_pstart;
5082 char_u *prev_pend;
5083
5084 bdp->startspaces = 0;
5085 bdp->endspaces = 0;
5086 bdp->textlen = 0;
5087 bdp->start_vcol = 0;
5088 bdp->end_vcol = 0;
5089#ifdef FEAT_VISUALEXTRA
5090 bdp->is_short = FALSE;
5091 bdp->is_oneChar = FALSE;
5092 bdp->pre_whitesp = 0;
5093 bdp->pre_whitesp_c = 0;
5094 bdp->end_char_vcols = 0;
5095#endif
5096 bdp->start_char_vcols = 0;
5097
5098 line = ml_get(lnum);
5099 pstart = line;
5100 prev_pstart = line;
5101 while (bdp->start_vcol < oap->start_vcol && *pstart)
5102 {
5103 /* Count a tab for what it's worth (if list mode not on) */
5104 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
5105 bdp->start_vcol += incr;
5106#ifdef FEAT_VISUALEXTRA
5107 if (vim_iswhite(*pstart))
5108 {
5109 bdp->pre_whitesp += incr;
5110 bdp->pre_whitesp_c++;
5111 }
5112 else
5113 {
5114 bdp->pre_whitesp = 0;
5115 bdp->pre_whitesp_c = 0;
5116 }
5117#endif
5118 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005119 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 bdp->start_char_vcols = incr;
5122 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5123 {
5124 bdp->end_vcol = bdp->start_vcol;
5125#ifdef FEAT_VISUALEXTRA
5126 bdp->is_short = TRUE;
5127#endif
5128 if (!is_del || oap->op_type == OP_APPEND)
5129 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5130 }
5131 else
5132 {
5133 /* notice: this converts partly selected Multibyte characters to
5134 * spaces, too. */
5135 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5136 if (is_del && bdp->startspaces)
5137 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5138 pend = pstart;
5139 bdp->end_vcol = bdp->start_vcol;
5140 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5141 {
5142#ifdef FEAT_VISUALEXTRA
5143 bdp->is_oneChar = TRUE;
5144#endif
5145 if (oap->op_type == OP_INSERT)
5146 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5147 else if (oap->op_type == OP_APPEND)
5148 {
5149 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5150 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5151 }
5152 else
5153 {
5154 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5155 if (is_del && oap->op_type != OP_LSHIFT)
5156 {
5157 /* just putting the sum of those two into
5158 * bdp->startspaces doesn't work for Visual replace,
5159 * so we have to split the tab in two */
5160 bdp->startspaces = bdp->start_char_vcols
5161 - (bdp->start_vcol - oap->start_vcol);
5162 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5163 }
5164 }
5165 }
5166 else
5167 {
5168 prev_pend = pend;
5169 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5170 {
5171 /* Count a tab for what it's worth (if list mode not on) */
5172 prev_pend = pend;
5173 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
5174 bdp->end_vcol += incr;
5175 }
5176 if (bdp->end_vcol <= oap->end_vcol
5177 && (!is_del
5178 || oap->op_type == OP_APPEND
5179 || oap->op_type == OP_REPLACE)) /* line too short */
5180 {
5181#ifdef FEAT_VISUALEXTRA
5182 bdp->is_short = TRUE;
5183#endif
5184 /* Alternative: include spaces to fill up the block.
5185 * Disadvantage: can lead to trailing spaces when the line is
5186 * short where the text is put */
5187 /* if (!is_del || oap->op_type == OP_APPEND) */
5188 if (oap->op_type == OP_APPEND || virtual_op)
5189 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005190 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else
5192 bdp->endspaces = 0; /* replace doesn't add characters */
5193 }
5194 else if (bdp->end_vcol > oap->end_vcol)
5195 {
5196 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5197 if (!is_del && bdp->endspaces)
5198 {
5199 bdp->endspaces = incr - bdp->endspaces;
5200 if (pend != pstart)
5201 pend = prev_pend;
5202 }
5203 }
5204 }
5205#ifdef FEAT_VISUALEXTRA
5206 bdp->end_char_vcols = incr;
5207#endif
5208 if (is_del && bdp->startspaces)
5209 pstart = prev_pstart;
5210 bdp->textlen = (int)(pend - pstart);
5211 }
5212 bdp->textcol = (colnr_T) (pstart - line);
5213 bdp->textstart = pstart;
5214}
5215#endif /* FEAT_VISUAL */
5216
5217#ifdef FEAT_RIGHTLEFT
5218static void reverse_line __ARGS((char_u *s));
5219
5220 static void
5221reverse_line(s)
5222 char_u *s;
5223{
5224 int i, j;
5225 char_u c;
5226
5227 if ((i = (int)STRLEN(s) - 1) <= 0)
5228 return;
5229
5230 curwin->w_cursor.col = i - curwin->w_cursor.col;
5231 for (j = 0; j < i; j++, i--)
5232 {
5233 c = s[i]; s[i] = s[j]; s[j] = c;
5234 }
5235}
5236
5237# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5238#else
5239# define RLADDSUBFIX(ptr)
5240#endif
5241
5242/*
5243 * add or subtract 'Prenum1' from a number in a line
5244 * 'command' is CTRL-A for add, CTRL-X for subtract
5245 *
5246 * return FAIL for failure, OK otherwise
5247 */
5248 int
5249do_addsub(command, Prenum1)
5250 int command;
5251 linenr_T Prenum1;
5252{
5253 int col;
5254 char_u *buf1;
5255 char_u buf2[NUMBUFLEN];
5256 int hex; /* 'X' or 'x': hex; '0': octal */
5257 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005258 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 long_u oldn;
5260 char_u *ptr;
5261 int c;
5262 int length = 0; /* character length of the number */
5263 int todel;
5264 int dohex;
5265 int dooct;
5266 int doalp;
5267 int firstdigit;
5268 int negative;
5269 int subtract;
5270
5271 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5272 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5273 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5274
5275 ptr = ml_get_curline();
5276 RLADDSUBFIX(ptr);
5277
5278 /*
5279 * First check if we are on a hexadecimal number, after the "0x".
5280 */
5281 col = curwin->w_cursor.col;
5282 if (dohex)
5283 while (col > 0 && vim_isxdigit(ptr[col]))
5284 --col;
5285 if ( dohex
5286 && col > 0
5287 && (ptr[col] == 'X'
5288 || ptr[col] == 'x')
5289 && ptr[col - 1] == '0'
5290 && vim_isxdigit(ptr[col + 1]))
5291 {
5292 /*
5293 * Found hexadecimal number, move to its start.
5294 */
5295 --col;
5296 }
5297 else
5298 {
5299 /*
5300 * Search forward and then backward to find the start of number.
5301 */
5302 col = curwin->w_cursor.col;
5303
5304 while (ptr[col] != NUL
5305 && !vim_isdigit(ptr[col])
5306 && !(doalp && ASCII_ISALPHA(ptr[col])))
5307 ++col;
5308
5309 while (col > 0
5310 && vim_isdigit(ptr[col - 1])
5311 && !(doalp && ASCII_ISALPHA(ptr[col])))
5312 --col;
5313 }
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 /*
5316 * If a number was found, and saving for undo works, replace the number.
5317 */
5318 firstdigit = ptr[col];
5319 RLADDSUBFIX(ptr);
5320 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5321 || u_save_cursor() != OK)
5322 {
5323 beep_flush();
5324 return FAIL;
5325 }
5326
5327 /* get ptr again, because u_save() may have changed it */
5328 ptr = ml_get_curline();
5329 RLADDSUBFIX(ptr);
5330
5331 if (doalp && ASCII_ISALPHA(firstdigit))
5332 {
5333 /* decrement or increment alphabetic character */
5334 if (command == Ctrl_X)
5335 {
5336 if (CharOrd(firstdigit) < Prenum1)
5337 {
5338 if (isupper(firstdigit))
5339 firstdigit = 'A';
5340 else
5341 firstdigit = 'a';
5342 }
5343 else
5344#ifdef EBCDIC
5345 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5346#else
5347 firstdigit -= Prenum1;
5348#endif
5349 }
5350 else
5351 {
5352 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5353 {
5354 if (isupper(firstdigit))
5355 firstdigit = 'Z';
5356 else
5357 firstdigit = 'z';
5358 }
5359 else
5360#ifdef EBCDIC
5361 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5362#else
5363 firstdigit += Prenum1;
5364#endif
5365 }
5366 curwin->w_cursor.col = col;
5367 (void)del_char(FALSE);
5368 ins_char(firstdigit);
5369 }
5370 else
5371 {
5372 negative = FALSE;
5373 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5374 {
5375 --col;
5376 negative = TRUE;
5377 }
5378
5379 /* get the number value (unsigned) */
5380 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5381
5382 /* ignore leading '-' for hex and octal numbers */
5383 if (hex && negative)
5384 {
5385 ++col;
5386 --length;
5387 negative = FALSE;
5388 }
5389
5390 /* add or subtract */
5391 subtract = FALSE;
5392 if (command == Ctrl_X)
5393 subtract ^= TRUE;
5394 if (negative)
5395 subtract ^= TRUE;
5396
5397 oldn = n;
5398 if (subtract)
5399 n -= (unsigned long)Prenum1;
5400 else
5401 n += (unsigned long)Prenum1;
5402
5403 /* handle wraparound for decimal numbers */
5404 if (!hex)
5405 {
5406 if (subtract)
5407 {
5408 if (n > oldn)
5409 {
5410 n = 1 + (n ^ (unsigned long)-1);
5411 negative ^= TRUE;
5412 }
5413 }
5414 else /* add */
5415 {
5416 if (n < oldn)
5417 {
5418 n = (n ^ (unsigned long)-1);
5419 negative ^= TRUE;
5420 }
5421 }
5422 if (n == 0)
5423 negative = FALSE;
5424 }
5425
5426 /*
5427 * Delete the old number.
5428 */
5429 curwin->w_cursor.col = col;
5430 todel = length;
5431 c = gchar_cursor();
5432 /*
5433 * Don't include the '-' in the length, only the length of the part
5434 * after it is kept the same.
5435 */
5436 if (c == '-')
5437 --length;
5438 while (todel-- > 0)
5439 {
5440 if (c < 0x100 && isalpha(c))
5441 {
5442 if (isupper(c))
5443 hexupper = TRUE;
5444 else
5445 hexupper = FALSE;
5446 }
5447 /* del_char() will mark line needing displaying */
5448 (void)del_char(FALSE);
5449 c = gchar_cursor();
5450 }
5451
5452 /*
5453 * Prepare the leading characters in buf1[].
5454 * When there are many leading zeros it could be very long. Allocate
5455 * a bit too much.
5456 */
5457 buf1 = alloc((unsigned)length + NUMBUFLEN);
5458 if (buf1 == NULL)
5459 return FAIL;
5460 ptr = buf1;
5461 if (negative)
5462 {
5463 *ptr++ = '-';
5464 }
5465 if (hex)
5466 {
5467 *ptr++ = '0';
5468 --length;
5469 }
5470 if (hex == 'x' || hex == 'X')
5471 {
5472 *ptr++ = hex;
5473 --length;
5474 }
5475
5476 /*
5477 * Put the number characters in buf2[].
5478 */
5479 if (hex == 0)
5480 sprintf((char *)buf2, "%lu", n);
5481 else if (hex == '0')
5482 sprintf((char *)buf2, "%lo", n);
5483 else if (hex && hexupper)
5484 sprintf((char *)buf2, "%lX", n);
5485 else
5486 sprintf((char *)buf2, "%lx", n);
5487 length -= (int)STRLEN(buf2);
5488
5489 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005490 * Adjust number of zeros to the new number of digits, so the
5491 * total length of the number remains the same.
5492 * Don't do this when
5493 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005495 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 while (length-- > 0)
5497 *ptr++ = '0';
5498 *ptr = NUL;
5499 STRCAT(buf1, buf2);
5500 ins_str(buf1); /* insert the new number */
5501 vim_free(buf1);
5502 }
5503 --curwin->w_cursor.col;
5504 curwin->w_set_curswant = TRUE;
5505#ifdef FEAT_RIGHTLEFT
5506 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5507 RLADDSUBFIX(ptr);
5508#endif
5509 return OK;
5510}
5511
5512#ifdef FEAT_VIMINFO
5513 int
5514read_viminfo_register(virp, force)
5515 vir_T *virp;
5516 int force;
5517{
5518 int eof;
5519 int do_it = TRUE;
5520 int size;
5521 int limit;
5522 int i;
5523 int set_prev = FALSE;
5524 char_u *str;
5525 char_u **array = NULL;
5526
5527 /* We only get here (hopefully) if line[0] == '"' */
5528 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005529
5530 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 if (*str == '"')
5532 {
5533 set_prev = TRUE;
5534 str++;
5535 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (!ASCII_ISALNUM(*str) && *str != '-')
5538 {
5539 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5540 return TRUE; /* too many errors, pretend end-of-file */
5541 do_it = FALSE;
5542 }
5543 get_yank_register(*str++, FALSE);
5544 if (!force && y_current->y_array != NULL)
5545 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005546
5547 if (*str == '@')
5548 {
5549 /* "x@: register x used for @@ */
5550 if (force || execreg_lastc == NUL)
5551 execreg_lastc = str[-1];
5552 }
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 size = 0;
5555 limit = 100; /* Optimized for registers containing <= 100 lines */
5556 if (do_it)
5557 {
5558 if (set_prev)
5559 y_previous = y_current;
5560 vim_free(y_current->y_array);
5561 array = y_current->y_array =
5562 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005563 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 if (STRNCMP(str, "CHAR", 4) == 0)
5565 y_current->y_type = MCHAR;
5566#ifdef FEAT_VISUAL
5567 else if (STRNCMP(str, "BLOCK", 5) == 0)
5568 y_current->y_type = MBLOCK;
5569#endif
5570 else
5571 y_current->y_type = MLINE;
5572 /* get the block width; if it's missing we get a zero, which is OK */
5573 str = skipwhite(skiptowhite(str));
5574#ifdef FEAT_VISUAL
5575 y_current->y_width = getdigits(&str);
5576#else
5577 (void)getdigits(&str);
5578#endif
5579 }
5580
5581 while (!(eof = viminfo_readline(virp))
5582 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5583 {
5584 if (do_it)
5585 {
5586 if (size >= limit)
5587 {
5588 y_current->y_array = (char_u **)
5589 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5590 for (i = 0; i < limit; i++)
5591 y_current->y_array[i] = array[i];
5592 vim_free(array);
5593 limit *= 2;
5594 array = y_current->y_array;
5595 }
5596 str = viminfo_readstring(virp, 1, TRUE);
5597 if (str != NULL)
5598 array[size++] = str;
5599 else
5600 do_it = FALSE;
5601 }
5602 }
5603 if (do_it)
5604 {
5605 if (size == 0)
5606 {
5607 vim_free(array);
5608 y_current->y_array = NULL;
5609 }
5610 else if (size < limit)
5611 {
5612 y_current->y_array =
5613 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5614 for (i = 0; i < size; i++)
5615 y_current->y_array[i] = array[i];
5616 vim_free(array);
5617 }
5618 y_current->y_size = size;
5619 }
5620 return eof;
5621}
5622
5623 void
5624write_viminfo_registers(fp)
5625 FILE *fp;
5626{
5627 int i, j;
5628 char_u *type;
5629 char_u c;
5630 int num_lines;
5631 int max_num_lines;
5632 int max_kbyte;
5633 long len;
5634
Bram Moolenaar64404472010-06-26 06:24:45 +02005635 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
5637 /* Get '<' value, use old '"' value if '<' is not found. */
5638 max_num_lines = get_viminfo_parameter('<');
5639 if (max_num_lines < 0)
5640 max_num_lines = get_viminfo_parameter('"');
5641 if (max_num_lines == 0)
5642 return;
5643 max_kbyte = get_viminfo_parameter('s');
5644 if (max_kbyte == 0)
5645 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 for (i = 0; i < NUM_REGISTERS; i++)
5648 {
5649 if (y_regs[i].y_array == NULL)
5650 continue;
5651#ifdef FEAT_CLIPBOARD
5652 /* Skip '*'/'+' register, we don't want them back next time */
5653 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5654 continue;
5655#endif
5656#ifdef FEAT_DND
5657 /* Neither do we want the '~' register */
5658 if (i == TILDE_REGISTER)
5659 continue;
5660#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005661 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005663 if (num_lines == 0
5664 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005665 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005666 continue;
5667
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (max_kbyte > 0)
5669 {
5670 /* Skip register if there is more text than the maximum size. */
5671 len = 0;
5672 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005673 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 if (len > (long)max_kbyte * 1024L)
5675 continue;
5676 }
5677
5678 switch (y_regs[i].y_type)
5679 {
5680 case MLINE:
5681 type = (char_u *)"LINE";
5682 break;
5683 case MCHAR:
5684 type = (char_u *)"CHAR";
5685 break;
5686#ifdef FEAT_VISUAL
5687 case MBLOCK:
5688 type = (char_u *)"BLOCK";
5689 break;
5690#endif
5691 default:
5692 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005693 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 emsg(IObuff);
5695 type = (char_u *)"LINE";
5696 break;
5697 }
5698 if (y_previous == &y_regs[i])
5699 fprintf(fp, "\"");
5700 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005701 fprintf(fp, "\"%c", c);
5702 if (c == execreg_lastc)
5703 fprintf(fp, "@");
5704 fprintf(fp, "\t%s\t%d\n", type,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705#ifdef FEAT_VISUAL
5706 (int)y_regs[i].y_width
5707#else
5708 0
5709#endif
5710 );
5711
5712 /* If max_num_lines < 0, then we save ALL the lines in the register */
5713 if (max_num_lines > 0 && num_lines > max_num_lines)
5714 num_lines = max_num_lines;
5715 for (j = 0; j < num_lines; j++)
5716 {
5717 putc('\t', fp);
5718 viminfo_writestring(fp, y_regs[i].y_array[j]);
5719 }
5720 }
5721}
5722#endif /* FEAT_VIMINFO */
5723
5724#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5725/*
5726 * SELECTION / PRIMARY ('*')
5727 *
5728 * Text selection stuff that uses the GUI selection register '*'. When using a
5729 * GUI this may be text from another window, otherwise it is the last text we
5730 * had highlighted with VIsual mode. With mouse support, clicking the middle
5731 * button performs the paste, otherwise you will need to do <"*p>. "
5732 * If not under X, it is synonymous with the clipboard register '+'.
5733 *
5734 * X CLIPBOARD ('+')
5735 *
5736 * Text selection stuff that uses the GUI clipboard register '+'.
5737 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5738 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5739 * otherwise you will need to do <"+p>. "
5740 * If not under X, it is synonymous with the selection register '*'.
5741 */
5742
5743/*
5744 * Routine to export any final X selection we had to the environment
5745 * so that the text is still available after vim has exited. X selections
5746 * only exist while the owning application exists, so we write to the
5747 * permanent (while X runs) store CUT_BUFFER0.
5748 * Dump the CLIPBOARD selection if we own it (it's logically the more
5749 * 'permanent' of the two), otherwise the PRIMARY one.
5750 * For now, use a hard-coded sanity limit of 1Mb of data.
5751 */
5752#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5753 void
5754x11_export_final_selection()
5755{
5756 Display *dpy;
5757 char_u *str = NULL;
5758 long_u len = 0;
5759 int motion_type = -1;
5760
5761# ifdef FEAT_GUI
5762 if (gui.in_use)
5763 dpy = X_DISPLAY;
5764 else
5765# endif
5766# ifdef FEAT_XCLIPBOARD
5767 dpy = xterm_dpy;
5768# else
5769 return;
5770# endif
5771
5772 /* Get selection to export */
5773 if (clip_plus.owned)
5774 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5775 else if (clip_star.owned)
5776 motion_type = clip_convert_selection(&str, &len, &clip_star);
5777
5778 /* Check it's OK */
5779 if (dpy != NULL && str != NULL && motion_type >= 0
5780 && len < 1024*1024 && len > 0)
5781 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005782#ifdef FEAT_MBYTE
5783 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5784 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5785 * encoding conversion usually doesn't work, so keep the text as-is.
5786 */
5787 if (has_mbyte)
5788 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005789 vimconv_T vc;
5790
5791 vc.vc_type = CONV_NONE;
5792 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5793 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005794 int intlen = len;
5795 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005796
5797 conv_str = string_convert(&vc, str, &intlen);
5798 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005799 if (conv_str != NULL)
5800 {
5801 vim_free(str);
5802 str = conv_str;
5803 }
5804 convert_setup(&vc, NULL, NULL);
5805 }
5806 }
5807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5809 XFlush(dpy);
5810 }
5811
5812 vim_free(str);
5813}
5814#endif
5815
5816 void
5817clip_free_selection(cbd)
5818 VimClipboard *cbd;
5819{
5820 struct yankreg *y_ptr = y_current;
5821
5822 if (cbd == &clip_plus)
5823 y_current = &y_regs[PLUS_REGISTER];
5824 else
5825 y_current = &y_regs[STAR_REGISTER];
5826 free_yank_all();
5827 y_current->y_size = 0;
5828 y_current = y_ptr;
5829}
5830
5831/*
5832 * Get the selected text and put it in the gui selection register '*' or '+'.
5833 */
5834 void
5835clip_get_selection(cbd)
5836 VimClipboard *cbd;
5837{
5838 struct yankreg *old_y_previous, *old_y_current;
5839 pos_T old_cursor;
5840#ifdef FEAT_VISUAL
5841 pos_T old_visual;
5842 int old_visual_mode;
5843#endif
5844 colnr_T old_curswant;
5845 int old_set_curswant;
5846 pos_T old_op_start, old_op_end;
5847 oparg_T oa;
5848 cmdarg_T ca;
5849
5850 if (cbd->owned)
5851 {
5852 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5853 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5854 return;
5855
5856 /* Get the text between clip_star.start & clip_star.end */
5857 old_y_previous = y_previous;
5858 old_y_current = y_current;
5859 old_cursor = curwin->w_cursor;
5860 old_curswant = curwin->w_curswant;
5861 old_set_curswant = curwin->w_set_curswant;
5862 old_op_start = curbuf->b_op_start;
5863 old_op_end = curbuf->b_op_end;
5864#ifdef FEAT_VISUAL
5865 old_visual = VIsual;
5866 old_visual_mode = VIsual_mode;
5867#endif
5868 clear_oparg(&oa);
5869 oa.regname = (cbd == &clip_plus ? '+' : '*');
5870 oa.op_type = OP_YANK;
5871 vim_memset(&ca, 0, sizeof(ca));
5872 ca.oap = &oa;
5873 ca.cmdchar = 'y';
5874 ca.count1 = 1;
5875 ca.retval = CA_NO_ADJ_OP_END;
5876 do_pending_operator(&ca, 0, TRUE);
5877 y_previous = old_y_previous;
5878 y_current = old_y_current;
5879 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005880 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 curwin->w_curswant = old_curswant;
5882 curwin->w_set_curswant = old_set_curswant;
5883 curbuf->b_op_start = old_op_start;
5884 curbuf->b_op_end = old_op_end;
5885#ifdef FEAT_VISUAL
5886 VIsual = old_visual;
5887 VIsual_mode = old_visual_mode;
5888#endif
5889 }
5890 else
5891 {
5892 clip_free_selection(cbd);
5893
5894 /* Try to get selected text from another window */
5895 clip_gen_request_selection(cbd);
5896 }
5897}
5898
Bram Moolenaard44347f2011-06-19 01:14:29 +02005899/*
5900 * Convert from the GUI selection string into the '*'/'+' register.
5901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 void
5903clip_yank_selection(type, str, len, cbd)
5904 int type;
5905 char_u *str;
5906 long len;
5907 VimClipboard *cbd;
5908{
5909 struct yankreg *y_ptr;
5910
5911 if (cbd == &clip_plus)
5912 y_ptr = &y_regs[PLUS_REGISTER];
5913 else
5914 y_ptr = &y_regs[STAR_REGISTER];
5915
5916 clip_free_selection(cbd);
5917
5918 str_to_reg(y_ptr, type, str, len, 0L);
5919}
5920
5921/*
5922 * Convert the '*'/'+' register into a GUI selection string returned in *str
5923 * with length *len.
5924 * Returns the motion type, or -1 for failure.
5925 */
5926 int
5927clip_convert_selection(str, len, cbd)
5928 char_u **str;
5929 long_u *len;
5930 VimClipboard *cbd;
5931{
5932 char_u *p;
5933 int lnum;
5934 int i, j;
5935 int_u eolsize;
5936 struct yankreg *y_ptr;
5937
5938 if (cbd == &clip_plus)
5939 y_ptr = &y_regs[PLUS_REGISTER];
5940 else
5941 y_ptr = &y_regs[STAR_REGISTER];
5942
5943#ifdef USE_CRNL
5944 eolsize = 2;
5945#else
5946 eolsize = 1;
5947#endif
5948
5949 *str = NULL;
5950 *len = 0;
5951 if (y_ptr->y_array == NULL)
5952 return -1;
5953
5954 for (i = 0; i < y_ptr->y_size; i++)
5955 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5956
5957 /*
5958 * Don't want newline character at end of last line if we're in MCHAR mode.
5959 */
5960 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5961 *len -= eolsize;
5962
5963 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5964 if (p == NULL)
5965 return -1;
5966 lnum = 0;
5967 for (i = 0, j = 0; i < (int)*len; i++, j++)
5968 {
5969 if (y_ptr->y_array[lnum][j] == '\n')
5970 p[i] = NUL;
5971 else if (y_ptr->y_array[lnum][j] == NUL)
5972 {
5973#ifdef USE_CRNL
5974 p[i++] = '\r';
5975#endif
5976#ifdef USE_CR
5977 p[i] = '\r';
5978#else
5979 p[i] = '\n';
5980#endif
5981 lnum++;
5982 j = -1;
5983 }
5984 else
5985 p[i] = y_ptr->y_array[lnum][j];
5986 }
5987 return y_ptr->y_type;
5988}
5989
5990
5991# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5992/*
5993 * If we have written to a clipboard register, send the text to the clipboard.
5994 */
5995 static void
5996may_set_selection()
5997{
5998 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5999 {
6000 clip_own_selection(&clip_star);
6001 clip_gen_set_selection(&clip_star);
6002 }
6003 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6004 {
6005 clip_own_selection(&clip_plus);
6006 clip_gen_set_selection(&clip_plus);
6007 }
6008}
6009# endif
6010
6011#endif /* FEAT_CLIPBOARD || PROTO */
6012
6013
6014#if defined(FEAT_DND) || defined(PROTO)
6015/*
6016 * Replace the contents of the '~' register with str.
6017 */
6018 void
6019dnd_yank_drag_data(str, len)
6020 char_u *str;
6021 long len;
6022{
6023 struct yankreg *curr;
6024
6025 curr = y_current;
6026 y_current = &y_regs[TILDE_REGISTER];
6027 free_yank_all();
6028 str_to_reg(y_current, MCHAR, str, len, 0L);
6029 y_current = curr;
6030}
6031#endif
6032
6033
6034#if defined(FEAT_EVAL) || defined(PROTO)
6035/*
6036 * Return the type of a register.
6037 * Used for getregtype()
6038 * Returns MAUTO for error.
6039 */
6040 char_u
6041get_reg_type(regname, reglen)
6042 int regname;
6043 long *reglen;
6044{
6045 switch (regname)
6046 {
6047 case '%': /* file name */
6048 case '#': /* alternate file name */
6049 case '=': /* expression */
6050 case ':': /* last command line */
6051 case '/': /* last search-pattern */
6052 case '.': /* last inserted text */
6053#ifdef FEAT_SEARCHPATH
6054 case Ctrl_F: /* Filename under cursor */
6055 case Ctrl_P: /* Path under cursor, expand via "path" */
6056#endif
6057 case Ctrl_W: /* word under cursor */
6058 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6059 case '_': /* black hole: always empty */
6060 return MCHAR;
6061 }
6062
6063#ifdef FEAT_CLIPBOARD
6064 regname = may_get_selection(regname);
6065#endif
6066
6067 /* Should we check for a valid name? */
6068 get_yank_register(regname, FALSE);
6069
6070 if (y_current->y_array != NULL)
6071 {
6072#ifdef FEAT_VISUAL
6073 if (reglen != NULL && y_current->y_type == MBLOCK)
6074 *reglen = y_current->y_width;
6075#endif
6076 return y_current->y_type;
6077 }
6078 return MAUTO;
6079}
6080
6081/*
6082 * Return the contents of a register as a single allocated string.
6083 * Used for "@r" in expressions and for getreg().
6084 * Returns NULL for error.
6085 */
6086 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00006087get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00006089 int allowexpr; /* allow "=" register */
6090 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 long i;
6093 char_u *retval;
6094 int allocated;
6095 long len;
6096
6097 /* Don't allow using an expression register inside an expression */
6098 if (regname == '=')
6099 {
6100 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00006101 {
6102 if (expr_src)
6103 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00006105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 return NULL;
6107 }
6108
6109 if (regname == '@') /* "@@" is used for unnamed register */
6110 regname = '"';
6111
6112 /* check for valid regname */
6113 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6114 return NULL;
6115
6116#ifdef FEAT_CLIPBOARD
6117 regname = may_get_selection(regname);
6118#endif
6119
6120 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6121 {
6122 if (retval == NULL)
6123 return NULL;
6124 if (!allocated)
6125 retval = vim_strsave(retval);
6126 return retval;
6127 }
6128
6129 get_yank_register(regname, FALSE);
6130 if (y_current->y_array == NULL)
6131 return NULL;
6132
6133 /*
6134 * Compute length of resulting string.
6135 */
6136 len = 0;
6137 for (i = 0; i < y_current->y_size; ++i)
6138 {
6139 len += (long)STRLEN(y_current->y_array[i]);
6140 /*
6141 * Insert a newline between lines and after last line if
6142 * y_type is MLINE.
6143 */
6144 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6145 ++len;
6146 }
6147
6148 retval = lalloc(len + 1, TRUE);
6149
6150 /*
6151 * Copy the lines of the yank register into the string.
6152 */
6153 if (retval != NULL)
6154 {
6155 len = 0;
6156 for (i = 0; i < y_current->y_size; ++i)
6157 {
6158 STRCPY(retval + len, y_current->y_array[i]);
6159 len += (long)STRLEN(retval + len);
6160
6161 /*
6162 * Insert a NL between lines and after the last line if y_type is
6163 * MLINE.
6164 */
6165 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6166 retval[len++] = '\n';
6167 }
6168 retval[len] = NUL;
6169 }
6170
6171 return retval;
6172}
6173
6174/*
6175 * Store string "str" in register "name".
6176 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6177 * If "must_append" is TRUE, always append to the register. Otherwise append
6178 * if "name" is an uppercase letter.
6179 * Note: "maxlen" and "must_append" don't work for the "/" register.
6180 * Careful: 'str' is modified, you may have to use a copy!
6181 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6182 */
6183 void
6184write_reg_contents(name, str, maxlen, must_append)
6185 int name;
6186 char_u *str;
6187 int maxlen;
6188 int must_append;
6189{
6190 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6191}
6192
6193 void
6194write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6195 int name;
6196 char_u *str;
6197 int maxlen;
6198 int must_append;
6199 int yank_type;
6200 long block_len;
6201{
6202 struct yankreg *old_y_previous, *old_y_current;
6203 long len;
6204
Bram Moolenaare7566042005-06-17 22:00:15 +00006205 if (maxlen >= 0)
6206 len = maxlen;
6207 else
6208 len = (long)STRLEN(str);
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /* Special case: '/' search pattern */
6211 if (name == '/')
6212 {
6213 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6214 return;
6215 }
6216
Bram Moolenaare7566042005-06-17 22:00:15 +00006217#ifdef FEAT_EVAL
6218 if (name == '=')
6219 {
6220 char_u *p, *s;
6221
6222 p = vim_strnsave(str, (int)len);
6223 if (p == NULL)
6224 return;
6225 if (must_append)
6226 {
6227 s = concat_str(get_expr_line_src(), p);
6228 vim_free(p);
6229 p = s;
6230
6231 }
6232 set_expr_line(p);
6233 return;
6234 }
6235#endif
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6238 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006239 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 return;
6241 }
6242
6243 if (name == '_') /* black hole: nothing to do */
6244 return;
6245
6246 /* Don't want to change the current (unnamed) register */
6247 old_y_previous = y_previous;
6248 old_y_current = y_current;
6249
6250 get_yank_register(name, TRUE);
6251 if (!y_append && !must_append)
6252 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253#ifndef FEAT_VISUAL
6254 /* Just in case - make sure we don't use MBLOCK */
6255 if (yank_type == MBLOCK)
6256 yank_type = MAUTO;
6257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 str_to_reg(y_current, yank_type, str, len, block_len);
6259
6260# ifdef FEAT_CLIPBOARD
6261 /* Send text of clipboard register to the clipboard. */
6262 may_set_selection();
6263# endif
6264
6265 /* ':let @" = "val"' should change the meaning of the "" register */
6266 if (name != '"')
6267 y_previous = old_y_previous;
6268 y_current = old_y_current;
6269}
6270#endif /* FEAT_EVAL */
6271
6272#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6273/*
6274 * Put a string into a register. When the register is not empty, the string
6275 * is appended.
6276 */
6277 static void
Bram Moolenaard44347f2011-06-19 01:14:29 +02006278str_to_reg(y_ptr, yank_type, str, len, blocklen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006280 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 char_u *str; /* string to put in register */
6282 long len; /* length of string */
6283 long blocklen; /* width of Visual block */
6284{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006285 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 int lnum;
6287 long start;
6288 long i;
6289 int extra;
6290 int newlines; /* number of lines added */
6291 int extraline = 0; /* extra line at the end */
6292 int append = FALSE; /* append to last line in register */
6293 char_u *s;
6294 char_u **pp;
6295#ifdef FEAT_VISUAL
6296 long maxlen;
6297#endif
6298
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006299 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 y_ptr->y_size = 0;
6301
Bram Moolenaard44347f2011-06-19 01:14:29 +02006302 if (yank_type == MAUTO)
6303 type = ((len > 0 && (str[len - 1] == NL || str[len - 1] == CAR))
6304 ? MLINE : MCHAR);
6305 else
6306 type = yank_type;
6307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 /*
6309 * Count the number of lines within the string
6310 */
6311 newlines = 0;
6312 for (i = 0; i < len; i++)
6313 if (str[i] == '\n')
6314 ++newlines;
6315 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6316 {
6317 extraline = 1;
6318 ++newlines; /* count extra newline at the end */
6319 }
6320 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6321 {
6322 append = TRUE;
6323 --newlines; /* uncount newline when appending first line */
6324 }
6325
6326 /*
6327 * Allocate an array to hold the pointers to the new register lines.
6328 * If the register was not empty, move the existing lines to the new array.
6329 */
6330 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6331 * sizeof(char_u *), TRUE);
6332 if (pp == NULL) /* out of memory */
6333 return;
6334 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6335 pp[lnum] = y_ptr->y_array[lnum];
6336 vim_free(y_ptr->y_array);
6337 y_ptr->y_array = pp;
6338#ifdef FEAT_VISUAL
6339 maxlen = 0;
6340#endif
6341
6342 /*
6343 * Find the end of each line and save it into the array.
6344 */
6345 for (start = 0; start < len + extraline; start += i + 1)
6346 {
6347 for (i = start; i < len; ++i) /* find the end of the line */
6348 if (str[i] == '\n')
6349 break;
6350 i -= start; /* i is now length of line */
6351#ifdef FEAT_VISUAL
6352 if (i > maxlen)
6353 maxlen = i;
6354#endif
6355 if (append)
6356 {
6357 --lnum;
6358 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6359 }
6360 else
6361 extra = 0;
6362 s = alloc((unsigned)(i + extra + 1));
6363 if (s == NULL)
6364 break;
6365 if (extra)
6366 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6367 if (append)
6368 vim_free(y_ptr->y_array[lnum]);
6369 if (i)
6370 mch_memmove(s + extra, str + start, (size_t)i);
6371 extra += i;
6372 s[extra] = NUL;
6373 y_ptr->y_array[lnum++] = s;
6374 while (--extra >= 0)
6375 {
6376 if (*s == NUL)
6377 *s = '\n'; /* replace NUL with newline */
6378 ++s;
6379 }
6380 append = FALSE; /* only first line is appended */
6381 }
6382 y_ptr->y_type = type;
6383 y_ptr->y_size = lnum;
6384# ifdef FEAT_VISUAL
6385 if (type == MBLOCK)
6386 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6387 else
6388 y_ptr->y_width = 0;
6389# endif
6390}
6391#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6392
6393 void
6394clear_oparg(oap)
6395 oparg_T *oap;
6396{
6397 vim_memset(oap, 0, sizeof(oparg_T));
6398}
6399
Bram Moolenaar7c626922005-02-07 22:01:03 +00006400static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006403 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 *
6405 * "Words" are counted by looking for boundaries between non-space and
6406 * space characters. (it seems to produce results that match 'wc'.)
6407 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006408 * Return value is byte count; word count for the line is added to "*wc".
6409 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 *
6411 * The function will only examine the first "limit" characters in the
6412 * line, stopping if it encounters an end-of-line (NUL byte). In that
6413 * case, eol_size will be added to the character count to account for
6414 * the size of the EOL character.
6415 */
6416 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006417line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 char_u *line;
6419 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006420 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 long limit;
6422 int eol_size;
6423{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006424 long i;
6425 long words = 0;
6426 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 int is_word = 0;
6428
Bram Moolenaar7c626922005-02-07 22:01:03 +00006429 for (i = 0; line[i] && i < limit; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
6431 if (is_word)
6432 {
6433 if (vim_isspace(line[i]))
6434 {
6435 words++;
6436 is_word = 0;
6437 }
6438 }
6439 else if (!vim_isspace(line[i]))
6440 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006441 ++chars;
6442#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006443 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006444#else
6445 ++i;
6446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
6448
6449 if (is_word)
6450 words++;
6451 *wc += words;
6452
6453 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006454 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006457 chars += eol_size;
6458 }
6459 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 return i;
6461}
6462
6463/*
6464 * Give some info about the position of the cursor (for "g CTRL-G").
6465 * In Visual mode, give some info about the selected region. (In this case,
6466 * the *_count_cursor variables store running totals for the selection.)
6467 */
6468 void
6469cursor_pos_info()
6470{
6471 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006472 char_u buf1[50];
6473 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006475 long byte_count = 0;
6476 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 long char_count = 0;
6478 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 long word_count = 0;
6480 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006481 int eol_size;
6482 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483#ifdef FEAT_VISUAL
6484 long line_count_selected = 0;
6485 pos_T min_pos, max_pos;
6486 oparg_T oparg;
6487 struct block_def bd;
6488#endif
6489
6490 /*
6491 * Compute the length of the file in characters.
6492 */
6493 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6494 {
6495 MSG(_(no_lines_msg));
6496 }
6497 else
6498 {
6499 if (get_fileformat(curbuf) == EOL_DOS)
6500 eol_size = 2;
6501 else
6502 eol_size = 1;
6503
6504#ifdef FEAT_VISUAL
6505 if (VIsual_active)
6506 {
6507 if (lt(VIsual, curwin->w_cursor))
6508 {
6509 min_pos = VIsual;
6510 max_pos = curwin->w_cursor;
6511 }
6512 else
6513 {
6514 min_pos = curwin->w_cursor;
6515 max_pos = VIsual;
6516 }
6517 if (*p_sel == 'e' && max_pos.col > 0)
6518 --max_pos.col;
6519
6520 if (VIsual_mode == Ctrl_V)
6521 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006522#ifdef FEAT_LINEBREAK
6523 char_u * saved_sbr = p_sbr;
6524
6525 /* Make 'sbr' empty for a moment to get the correct size. */
6526 p_sbr = empty_option;
6527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 oparg.is_VIsual = 1;
6529 oparg.block_mode = TRUE;
6530 oparg.op_type = OP_NOP;
6531 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006532 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006533#ifdef FEAT_LINEBREAK
6534 p_sbr = saved_sbr;
6535#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006536 if (curwin->w_curswant == MAXCOL)
6537 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 /* Swap the start, end vcol if needed */
6539 if (oparg.end_vcol < oparg.start_vcol)
6540 {
6541 oparg.end_vcol += oparg.start_vcol;
6542 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6543 oparg.end_vcol -= oparg.start_vcol;
6544 }
6545 }
6546 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6547 }
6548#endif
6549
6550 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6551 {
6552 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006553 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
6555 ui_breakcheck();
6556 if (got_int)
6557 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006558 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
6560
6561#ifdef FEAT_VISUAL
6562 /* Do extra processing for VIsual mode. */
6563 if (VIsual_active
6564 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6565 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006566 char_u *s = NULL;
6567 long len = 0L;
6568
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 switch (VIsual_mode)
6570 {
6571 case Ctrl_V:
6572# ifdef FEAT_VIRTUALEDIT
6573 virtual_op = virtual_active();
6574# endif
6575 block_prep(&oparg, &bd, lnum, 0);
6576# ifdef FEAT_VIRTUALEDIT
6577 virtual_op = MAYBE;
6578# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006579 s = bd.textstart;
6580 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 break;
6582 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006583 s = ml_get(lnum);
6584 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 break;
6586 case 'v':
6587 {
6588 colnr_T start_col = (lnum == min_pos.lnum)
6589 ? min_pos.col : 0;
6590 colnr_T end_col = (lnum == max_pos.lnum)
6591 ? max_pos.col - start_col + 1 : MAXCOL;
6592
Bram Moolenaardef9e822004-12-31 20:58:58 +00006593 s = ml_get(lnum) + start_col;
6594 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596 break;
6597 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006598 if (s != NULL)
6599 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006600 byte_count_cursor += line_count_info(s, &word_count_cursor,
6601 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006602 if (lnum == curbuf->b_ml.ml_line_count
6603 && !curbuf->b_p_eol
6604 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006605 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006606 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 else
6610#endif
6611 {
6612 /* In non-visual mode, check for the line the cursor is on */
6613 if (lnum == curwin->w_cursor.lnum)
6614 {
6615 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006616 char_count_cursor += char_count;
6617 byte_count_cursor = byte_count +
6618 line_count_info(ml_get(lnum),
6619 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 (long)(curwin->w_cursor.col + 1), eol_size);
6621 }
6622 }
6623 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006624 byte_count += line_count_info(ml_get(lnum), &word_count,
6625 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 }
6627
6628 /* Correction for when last line doesn't have an EOL. */
6629 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006630 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632#ifdef FEAT_VISUAL
6633 if (VIsual_active)
6634 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006635 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 {
6637 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006638 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006639 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6641 }
6642 else
6643 buf1[0] = NUL;
6644
Bram Moolenaar7c626922005-02-07 22:01:03 +00006645 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006646 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006647 vim_snprintf((char *)IObuff, IOSIZE,
6648 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 buf1, line_count_selected,
6650 (long)curbuf->b_ml.ml_line_count,
6651 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006652 byte_count_cursor, byte_count);
6653 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006654 vim_snprintf((char *)IObuff, IOSIZE,
6655 _("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 +00006656 buf1, line_count_selected,
6657 (long)curbuf->b_ml.ml_line_count,
6658 word_count_cursor, word_count,
6659 char_count_cursor, char_count,
6660 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
6662 else
6663#endif
6664 {
6665 p = ml_get_curline();
6666 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006667 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 (int)curwin->w_virtcol + 1);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006669 col_print(buf2, sizeof(buf2), (int)STRLEN(p), linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
Bram Moolenaar7c626922005-02-07 22:01:03 +00006671 if (char_count_cursor == byte_count_cursor
6672 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006673 vim_snprintf((char *)IObuff, IOSIZE,
6674 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 (char *)buf1, (char *)buf2,
6676 (long)curwin->w_cursor.lnum,
6677 (long)curbuf->b_ml.ml_line_count,
6678 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006679 byte_count_cursor, byte_count);
6680 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006681 vim_snprintf((char *)IObuff, IOSIZE,
6682 _("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 +00006683 (char *)buf1, (char *)buf2,
6684 (long)curwin->w_cursor.lnum,
6685 (long)curbuf->b_ml.ml_line_count,
6686 word_count_cursor, word_count,
6687 char_count_cursor, char_count,
6688 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690
6691#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006692 byte_count = bomb_size();
6693 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006695 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696#endif
6697 /* Don't shorten this message, the user asked for it. */
6698 p = p_shm;
6699 p_shm = (char_u *)"";
6700 msg(IObuff);
6701 p_shm = p;
6702 }
6703}