blob: ca327b955dc256bacfbd84c1e3b4ee15a3f09741 [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:
4253 * - COM_START,
4254 * - COM_END,
4255 * - colon,
4256 * whichever comes first.
4257 */
4258 while (*comment_flags)
4259 {
4260 if (*comment_flags == COM_START
4261 || *comment_flags == COM_END
4262 || *comment_flags == ':')
4263 {
4264 break;
4265 }
4266 ++comment_flags;
4267 }
4268
4269 /* If we found a colon, it means that we are not processing a line
4270 * starting with an opening or a closing part of a three-part
4271 * comment. That's good, because we don't want to remove those as
4272 * this would be annoying.
4273 */
4274 if (*comment_flags == ':' || *comment_flags == NUL)
4275 line += lead_len;
4276
4277 return line;
4278}
4279#endif
4280
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004282 * Join 'count' lines (minimal 2) at cursor position.
4283 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaar81340392012-06-06 16:12:59 +02004284 * Set "use_formatoptions" to FALSE when e.g. processing
4285 * backspace and comment leaders should not be removed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004287 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 */
4289 int
Bram Moolenaar81340392012-06-06 16:12:59 +02004290do_join(count, insert_space, save_undo, use_formatoptions)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004291 long count;
4292 int insert_space;
4293 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004294 int use_formatoptions UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004296 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004297 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004298 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004300 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004301 int endcurr1 = NUL;
4302 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004303 int currsize = 0; /* size of the current line */
4304 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004306 colnr_T col = 0;
4307 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004308#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004309 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004310 int remove_comments = (use_formatoptions == TRUE)
4311 && has_format_option(FO_REMOVE_COMS);
4312 int prev_was_comment;
4313#endif
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004316 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4317 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004320 /* Allocate an array to store the number of spaces inserted before each
4321 * line. We will use it to pre-compute the length of the new line and the
4322 * proper placement of each original line in the new one. */
4323 spaces = lalloc_clear((long_u)count, TRUE);
4324 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004326#if defined(FEAT_COMMENTS) || defined(PROTO)
4327 if (remove_comments)
4328 {
4329 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4330 if (comments == NULL)
4331 {
4332 vim_free(spaces);
4333 return FAIL;
4334 }
4335 }
4336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004339 * Don't move anything, just compute the final line length
4340 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004342 for (t = 0; t < count; ++t)
4343 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004344 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar81340392012-06-06 16:12:59 +02004345#if defined(FEAT_COMMENTS) || defined(PROTO)
4346 if (remove_comments)
4347 {
4348 /* We don't want to remove the comment leader if the
4349 * previous line is not a comment. */
4350 if (t > 0 && prev_was_comment)
4351 {
4352
4353 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4354 &prev_was_comment);
4355 comments[t] = new_curr - curr;
4356 curr = new_curr;
4357 }
4358 else
4359 curr = skip_comment(curr, FALSE, insert_space,
4360 &prev_was_comment);
4361 }
4362#endif
4363
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004364 if (insert_space && t > 0)
4365 {
4366 curr = skipwhite(curr);
4367 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4368#ifdef FEAT_MBYTE
4369 && (!has_format_option(FO_MBYTE_JOIN)
4370 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4371 && (!has_format_option(FO_MBYTE_JOIN2)
4372 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4373#endif
4374 )
4375 {
4376 /* don't add a space if the line is ending in a space */
4377 if (endcurr1 == ' ')
4378 endcurr1 = endcurr2;
4379 else
4380 ++spaces[t];
4381 /* extra space when 'joinspaces' set and line ends in '.' */
4382 if ( p_js
4383 && (endcurr1 == '.'
4384 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4385 && (endcurr1 == '?' || endcurr1 == '!'))))
4386 ++spaces[t];
4387 }
4388 }
4389 currsize = (int)STRLEN(curr);
4390 sumsize += currsize + spaces[t];
4391 endcurr1 = endcurr2 = NUL;
4392 if (insert_space && currsize > 0)
4393 {
4394#ifdef FEAT_MBYTE
4395 if (has_mbyte)
4396 {
4397 cend = curr + currsize;
4398 mb_ptr_back(curr, cend);
4399 endcurr1 = (*mb_ptr2char)(cend);
4400 if (cend > curr)
4401 {
4402 mb_ptr_back(curr, cend);
4403 endcurr2 = (*mb_ptr2char)(cend);
4404 }
4405 }
4406 else
4407#endif
4408 {
4409 endcurr1 = *(curr + currsize - 1);
4410 if (currsize > 1)
4411 endcurr2 = *(curr + currsize - 2);
4412 }
4413 }
4414 line_breakcheck();
4415 if (got_int)
4416 {
4417 ret = FAIL;
4418 goto theend;
4419 }
4420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004422 /* store the column position before last line */
4423 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004425 /* allocate the space for the new line */
4426 newp = alloc_check((unsigned)(sumsize + 1));
4427 cend = newp + sumsize;
4428 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004430 /*
4431 * Move affected lines to the new long one.
4432 *
4433 * Move marks from each deleted line to the joined line, adjusting the
4434 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4435 * should not really be a problem.
4436 */
4437 for (t = count - 1; ; --t)
4438 {
4439 cend -= currsize;
4440 mch_memmove(cend, curr, (size_t)currsize);
4441 if (spaces[t] > 0)
4442 {
4443 cend -= spaces[t];
4444 copy_spaces(cend, (size_t)(spaces[t]));
4445 }
4446 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004447 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004448 if (t == 0)
4449 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004450 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004451#if defined(FEAT_COMMENTS) || defined(PROTO)
4452 if (remove_comments)
4453 curr += comments[t - 1];
4454#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004455 if (insert_space && t > 1)
4456 curr = skipwhite(curr);
4457 currsize = (int)STRLEN(curr);
4458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4460
4461 /* Only report the change in the first line here, del_lines() will report
4462 * the deleted line. */
4463 changed_lines(curwin->w_cursor.lnum, currsize,
4464 curwin->w_cursor.lnum + 1, 0L);
4465
4466 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004467 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 * briefly, and then move it back. After del_lines() the cursor may
4469 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 */
4471 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004473 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 curwin->w_cursor.lnum = t;
4475
4476 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004477 * Set the cursor column:
4478 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004479 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004481 curwin->w_cursor.col =
4482 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485#ifdef FEAT_VIRTUALEDIT
4486 curwin->w_cursor.coladd = 0;
4487#endif
4488 curwin->w_set_curswant = TRUE;
4489
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004490theend:
4491 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004492#if defined(FEAT_COMMENTS) || defined(PROTO)
4493 if (remove_comments)
4494 vim_free(comments);
4495#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004496 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497}
4498
4499#ifdef FEAT_COMMENTS
4500/*
4501 * Return TRUE if the two comment leaders given are the same. "lnum" is
4502 * the first line. White-space is ignored. Note that the whole of
4503 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4504 */
4505 static int
4506same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4507 linenr_T lnum;
4508 int leader1_len;
4509 char_u *leader1_flags;
4510 int leader2_len;
4511 char_u *leader2_flags;
4512{
4513 int idx1 = 0, idx2 = 0;
4514 char_u *p;
4515 char_u *line1;
4516 char_u *line2;
4517
4518 if (leader1_len == 0)
4519 return (leader2_len == 0);
4520
4521 /*
4522 * If first leader has 'f' flag, the lines can be joined only if the
4523 * second line does not have a leader.
4524 * If first leader has 'e' flag, the lines can never be joined.
4525 * If fist leader has 's' flag, the lines can only be joined if there is
4526 * some text after it and the second line has the 'm' flag.
4527 */
4528 if (leader1_flags != NULL)
4529 {
4530 for (p = leader1_flags; *p && *p != ':'; ++p)
4531 {
4532 if (*p == COM_FIRST)
4533 return (leader2_len == 0);
4534 if (*p == COM_END)
4535 return FALSE;
4536 if (*p == COM_START)
4537 {
4538 if (*(ml_get(lnum) + leader1_len) == NUL)
4539 return FALSE;
4540 if (leader2_flags == NULL || leader2_len == 0)
4541 return FALSE;
4542 for (p = leader2_flags; *p && *p != ':'; ++p)
4543 if (*p == COM_MIDDLE)
4544 return TRUE;
4545 return FALSE;
4546 }
4547 }
4548 }
4549
4550 /*
4551 * Get current line and next line, compare the leaders.
4552 * The first line has to be saved, only one line can be locked at a time.
4553 */
4554 line1 = vim_strsave(ml_get(lnum));
4555 if (line1 != NULL)
4556 {
4557 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4558 ;
4559 line2 = ml_get(lnum + 1);
4560 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4561 {
4562 if (!vim_iswhite(line2[idx2]))
4563 {
4564 if (line1[idx1++] != line2[idx2])
4565 break;
4566 }
4567 else
4568 while (vim_iswhite(line1[idx1]))
4569 ++idx1;
4570 }
4571 vim_free(line1);
4572 }
4573 return (idx2 == leader2_len && idx1 == leader1_len);
4574}
4575#endif
4576
4577/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004578 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 */
4580 void
4581op_format(oap, keep_cursor)
4582 oparg_T *oap;
4583 int keep_cursor; /* keep cursor on same text char */
4584{
4585 long old_line_count = curbuf->b_ml.ml_line_count;
4586
4587 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4588 * can put it back there. */
4589 curwin->w_cursor = oap->cursor_start;
4590
4591 if (u_save((linenr_T)(oap->start.lnum - 1),
4592 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4593 return;
4594 curwin->w_cursor = oap->start;
4595
4596#ifdef FEAT_VISUAL
4597 if (oap->is_VIsual)
4598 /* When there is no change: need to remove the Visual selection */
4599 redraw_curbuf_later(INVERTED);
4600#endif
4601
4602 /* Set '[ mark at the start of the formatted area */
4603 curbuf->b_op_start = oap->start;
4604
4605 /* For "gw" remember the cursor position and put it back below (adjusted
4606 * for joined and split lines). */
4607 if (keep_cursor)
4608 saved_cursor = oap->cursor_start;
4609
Bram Moolenaar81a82092008-03-12 16:27:00 +00004610 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
4612 /*
4613 * Leave the cursor at the first non-blank of the last formatted line.
4614 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4615 * line, so "." will do the next lines.
4616 */
4617 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4618 ++curwin->w_cursor.lnum;
4619 beginline(BL_WHITE | BL_FIX);
4620 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4621 msgmore(old_line_count);
4622
4623 /* put '] mark on the end of the formatted area */
4624 curbuf->b_op_end = curwin->w_cursor;
4625
4626 if (keep_cursor)
4627 {
4628 curwin->w_cursor = saved_cursor;
4629 saved_cursor.lnum = 0;
4630 }
4631
4632#ifdef FEAT_VISUAL
4633 if (oap->is_VIsual)
4634 {
4635 win_T *wp;
4636
4637 FOR_ALL_WINDOWS(wp)
4638 {
4639 if (wp->w_old_cursor_lnum != 0)
4640 {
4641 /* When lines have been inserted or deleted, adjust the end of
4642 * the Visual area to be redrawn. */
4643 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4644 wp->w_old_cursor_lnum += old_line_count;
4645 else
4646 wp->w_old_visual_lnum += old_line_count;
4647 }
4648 }
4649 }
4650#endif
4651}
4652
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004653#if defined(FEAT_EVAL) || defined(PROTO)
4654/*
4655 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4656 */
4657 void
4658op_formatexpr(oap)
4659 oparg_T *oap;
4660{
4661# ifdef FEAT_VISUAL
4662 if (oap->is_VIsual)
4663 /* When there is no change: need to remove the Visual selection */
4664 redraw_curbuf_later(INVERTED);
4665# endif
4666
Bram Moolenaar700303e2010-07-11 17:35:50 +02004667 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4668 /* As documented: when 'formatexpr' returns non-zero fall back to
4669 * internal formatting. */
4670 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004671}
4672
4673 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004674fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004675 linenr_T lnum;
4676 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004677 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004678{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004679 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4680 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004681 int r;
4682
4683 /*
4684 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004685 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004686 */
4687 set_vim_var_nr(VV_LNUM, lnum);
4688 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004689 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004690
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004691 /*
4692 * Evaluate the function.
4693 */
4694 if (use_sandbox)
4695 ++sandbox;
4696 r = eval_to_number(curbuf->b_p_fex);
4697 if (use_sandbox)
4698 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004699
4700 set_vim_var_string(VV_CHAR, NULL, -1);
4701
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004702 return r;
4703}
4704#endif
4705
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706/*
4707 * Format "line_count" lines, starting at the cursor position.
4708 * When "line_count" is negative, format until the end of the paragraph.
4709 * Lines after the cursor line are saved for undo, caller must have saved the
4710 * first line.
4711 */
4712 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004713format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004715 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 int max_len;
4718 int is_not_par; /* current line not part of parag. */
4719 int next_is_not_par; /* next line not part of paragraph */
4720 int is_end_par; /* at end of paragraph */
4721 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4722 int next_is_start_par = FALSE;
4723#ifdef FEAT_COMMENTS
4724 int leader_len = 0; /* leader len of current line */
4725 int next_leader_len; /* leader len of next line */
4726 char_u *leader_flags = NULL; /* flags for leader of current line */
4727 char_u *next_leader_flags; /* flags for leader of next line */
4728 int do_comments; /* format comments */
4729#endif
4730 int advance = TRUE;
4731 int second_indent = -1;
4732 int do_second_indent;
4733 int do_number_indent;
4734 int do_trail_white;
4735 int first_par_line = TRUE;
4736 int smd_save;
4737 long count;
4738 int need_set_indent = TRUE; /* set indent of next paragraph */
4739 int force_format = FALSE;
4740 int old_State = State;
4741
4742 /* length of a line to force formatting: 3 * 'tw' */
4743 max_len = comp_textwidth(TRUE) * 3;
4744
4745 /* check for 'q', '2' and '1' in 'formatoptions' */
4746#ifdef FEAT_COMMENTS
4747 do_comments = has_format_option(FO_Q_COMS);
4748#endif
4749 do_second_indent = has_format_option(FO_Q_SECOND);
4750 do_number_indent = has_format_option(FO_Q_NUMBER);
4751 do_trail_white = has_format_option(FO_WHITE_PAR);
4752
4753 /*
4754 * Get info about the previous and current line.
4755 */
4756 if (curwin->w_cursor.lnum > 1)
4757 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4758#ifdef FEAT_COMMENTS
4759 , &leader_len, &leader_flags, do_comments
4760#endif
4761 );
4762 else
4763 is_not_par = TRUE;
4764 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4765#ifdef FEAT_COMMENTS
4766 , &next_leader_len, &next_leader_flags, do_comments
4767#endif
4768 );
4769 is_end_par = (is_not_par || next_is_not_par);
4770 if (!is_end_par && do_trail_white)
4771 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4772
4773 curwin->w_cursor.lnum--;
4774 for (count = line_count; count != 0 && !got_int; --count)
4775 {
4776 /*
4777 * Advance to next paragraph.
4778 */
4779 if (advance)
4780 {
4781 curwin->w_cursor.lnum++;
4782 prev_is_end_par = is_end_par;
4783 is_not_par = next_is_not_par;
4784#ifdef FEAT_COMMENTS
4785 leader_len = next_leader_len;
4786 leader_flags = next_leader_flags;
4787#endif
4788 }
4789
4790 /*
4791 * The last line to be formatted.
4792 */
4793 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4794 {
4795 next_is_not_par = TRUE;
4796#ifdef FEAT_COMMENTS
4797 next_leader_len = 0;
4798 next_leader_flags = NULL;
4799#endif
4800 }
4801 else
4802 {
4803 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4804#ifdef FEAT_COMMENTS
4805 , &next_leader_len, &next_leader_flags, do_comments
4806#endif
4807 );
4808 if (do_number_indent)
4809 next_is_start_par =
4810 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4811 }
4812 advance = TRUE;
4813 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4814 if (!is_end_par && do_trail_white)
4815 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4816
4817 /*
4818 * Skip lines that are not in a paragraph.
4819 */
4820 if (is_not_par)
4821 {
4822 if (line_count < 0)
4823 break;
4824 }
4825 else
4826 {
4827 /*
4828 * For the first line of a paragraph, check indent of second line.
4829 * Don't do this for comments and empty lines.
4830 */
4831 if (first_par_line
4832 && (do_second_indent || do_number_indent)
4833 && prev_is_end_par
4834 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4835#ifdef FEAT_COMMENTS
4836 && leader_len == 0
4837 && next_leader_len == 0
4838#endif
4839 )
4840 {
4841 if (do_second_indent
4842 && !lineempty(curwin->w_cursor.lnum + 1))
4843 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4844 else if (do_number_indent)
4845 second_indent = get_number_indent(curwin->w_cursor.lnum);
4846 }
4847
4848 /*
4849 * When the comment leader changes, it's the end of the paragraph.
4850 */
4851 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4852#ifdef FEAT_COMMENTS
4853 || !same_leader(curwin->w_cursor.lnum,
4854 leader_len, leader_flags,
4855 next_leader_len, next_leader_flags)
4856#endif
4857 )
4858 is_end_par = TRUE;
4859
4860 /*
4861 * If we have got to the end of a paragraph, or the line is
4862 * getting long, format it.
4863 */
4864 if (is_end_par || force_format)
4865 {
4866 if (need_set_indent)
4867 /* replace indent in first line with minimal number of
4868 * tabs and spaces, according to current options */
4869 (void)set_indent(get_indent(), SIN_CHANGED);
4870
4871 /* put cursor on last non-space */
4872 State = NORMAL; /* don't go past end-of-line */
4873 coladvance((colnr_T)MAXCOL);
4874 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4875 dec_cursor();
4876
4877 /* do the formatting, without 'showmode' */
4878 State = INSERT; /* for open_line() */
4879 smd_save = p_smd;
4880 p_smd = FALSE;
4881 insertchar(NUL, INSCHAR_FORMAT
4882#ifdef FEAT_COMMENTS
4883 + (do_comments ? INSCHAR_DO_COM : 0)
4884#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004885 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 State = old_State;
4887 p_smd = smd_save;
4888 second_indent = -1;
4889 /* at end of par.: need to set indent of next par. */
4890 need_set_indent = is_end_par;
4891 if (is_end_par)
4892 {
4893 /* When called with a negative line count, break at the
4894 * end of the paragraph. */
4895 if (line_count < 0)
4896 break;
4897 first_par_line = TRUE;
4898 }
4899 force_format = FALSE;
4900 }
4901
4902 /*
4903 * When still in same paragraph, join the lines together. But
4904 * first delete the comment leader from the second line.
4905 */
4906 if (!is_end_par)
4907 {
4908 advance = FALSE;
4909 curwin->w_cursor.lnum++;
4910 curwin->w_cursor.col = 0;
4911 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004912 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#ifdef FEAT_COMMENTS
Bram Moolenaare3226be2005-12-18 22:10:00 +00004914 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 if (next_leader_len > 0)
4916 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4917 (long)-next_leader_len);
4918#endif
4919 curwin->w_cursor.lnum--;
Bram Moolenaar81340392012-06-06 16:12:59 +02004920 if (do_join(2, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
4922 beep_flush();
4923 break;
4924 }
4925 first_par_line = FALSE;
4926 /* If the line is getting long, format it next time */
4927 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4928 force_format = TRUE;
4929 else
4930 force_format = FALSE;
4931 }
4932 }
4933 line_breakcheck();
4934 }
4935}
4936
4937/*
4938 * Return TRUE if line "lnum" ends in a white character.
4939 */
4940 static int
4941ends_in_white(lnum)
4942 linenr_T lnum;
4943{
4944 char_u *s = ml_get(lnum);
4945 size_t l;
4946
4947 if (*s == NUL)
4948 return FALSE;
4949 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4950 * invocation may call function multiple times". */
4951 l = STRLEN(s) - 1;
4952 return vim_iswhite(s[l]);
4953}
4954
4955/*
4956 * Blank lines, and lines containing only the comment leader, are left
4957 * untouched by the formatting. The function returns TRUE in this
4958 * case. It also returns TRUE when a line starts with the end of a comment
4959 * ('e' in comment flags), so that this line is skipped, and not joined to the
4960 * previous line. A new paragraph starts after a blank line, or when the
4961 * comment leader changes -- webb.
4962 */
4963#ifdef FEAT_COMMENTS
4964 static int
4965fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4966 linenr_T lnum;
4967 int *leader_len;
4968 char_u **leader_flags;
4969 int do_comments;
4970{
4971 char_u *flags = NULL; /* init for GCC */
4972 char_u *ptr;
4973
4974 ptr = ml_get(lnum);
4975 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02004976 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 else
4978 *leader_len = 0;
4979
4980 if (*leader_len > 0)
4981 {
4982 /*
4983 * Search for 'e' flag in comment leader flags.
4984 */
4985 flags = *leader_flags;
4986 while (*flags && *flags != ':' && *flags != COM_END)
4987 ++flags;
4988 }
4989
4990 return (*skipwhite(ptr + *leader_len) == NUL
4991 || (*leader_len > 0 && *flags == COM_END)
4992 || startPS(lnum, NUL, FALSE));
4993}
4994#else
4995 static int
4996fmt_check_par(lnum)
4997 linenr_T lnum;
4998{
4999 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5000}
5001#endif
5002
5003/*
5004 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5005 * previous line is in the same paragraph. Used for auto-formatting.
5006 */
5007 int
5008paragraph_start(lnum)
5009 linenr_T lnum;
5010{
5011 char_u *p;
5012#ifdef FEAT_COMMENTS
5013 int leader_len = 0; /* leader len of current line */
5014 char_u *leader_flags = NULL; /* flags for leader of current line */
5015 int next_leader_len; /* leader len of next line */
5016 char_u *next_leader_flags; /* flags for leader of next line */
5017 int do_comments; /* format comments */
5018#endif
5019
5020 if (lnum <= 1)
5021 return TRUE; /* start of the file */
5022
5023 p = ml_get(lnum - 1);
5024 if (*p == NUL)
5025 return TRUE; /* after empty line */
5026
5027#ifdef FEAT_COMMENTS
5028 do_comments = has_format_option(FO_Q_COMS);
5029#endif
5030 if (fmt_check_par(lnum - 1
5031#ifdef FEAT_COMMENTS
5032 , &leader_len, &leader_flags, do_comments
5033#endif
5034 ))
5035 return TRUE; /* after non-paragraph line */
5036
5037 if (fmt_check_par(lnum
5038#ifdef FEAT_COMMENTS
5039 , &next_leader_len, &next_leader_flags, do_comments
5040#endif
5041 ))
5042 return TRUE; /* "lnum" is not a paragraph line */
5043
5044 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5045 return TRUE; /* missing trailing space in previous line. */
5046
5047 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5048 return TRUE; /* numbered item starts in "lnum". */
5049
5050#ifdef FEAT_COMMENTS
5051 if (!same_leader(lnum - 1, leader_len, leader_flags,
5052 next_leader_len, next_leader_flags))
5053 return TRUE; /* change of comment leader. */
5054#endif
5055
5056 return FALSE;
5057}
5058
5059#ifdef FEAT_VISUAL
5060/*
5061 * prepare a few things for block mode yank/delete/tilde
5062 *
5063 * for delete:
5064 * - textlen includes the first/last char to be (partly) deleted
5065 * - start/endspaces is the number of columns that are taken by the
5066 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005067 * deleted.
5068 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 * - textlen includes the first/last char to be wholly yanked
5070 * - start/endspaces is the number of columns of the first/last yanked char
5071 * that are to be yanked.
5072 */
5073 static void
5074block_prep(oap, bdp, lnum, is_del)
5075 oparg_T *oap;
5076 struct block_def *bdp;
5077 linenr_T lnum;
5078 int is_del;
5079{
5080 int incr = 0;
5081 char_u *pend;
5082 char_u *pstart;
5083 char_u *line;
5084 char_u *prev_pstart;
5085 char_u *prev_pend;
5086
5087 bdp->startspaces = 0;
5088 bdp->endspaces = 0;
5089 bdp->textlen = 0;
5090 bdp->start_vcol = 0;
5091 bdp->end_vcol = 0;
5092#ifdef FEAT_VISUALEXTRA
5093 bdp->is_short = FALSE;
5094 bdp->is_oneChar = FALSE;
5095 bdp->pre_whitesp = 0;
5096 bdp->pre_whitesp_c = 0;
5097 bdp->end_char_vcols = 0;
5098#endif
5099 bdp->start_char_vcols = 0;
5100
5101 line = ml_get(lnum);
5102 pstart = line;
5103 prev_pstart = line;
5104 while (bdp->start_vcol < oap->start_vcol && *pstart)
5105 {
5106 /* Count a tab for what it's worth (if list mode not on) */
5107 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
5108 bdp->start_vcol += incr;
5109#ifdef FEAT_VISUALEXTRA
5110 if (vim_iswhite(*pstart))
5111 {
5112 bdp->pre_whitesp += incr;
5113 bdp->pre_whitesp_c++;
5114 }
5115 else
5116 {
5117 bdp->pre_whitesp = 0;
5118 bdp->pre_whitesp_c = 0;
5119 }
5120#endif
5121 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005122 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 bdp->start_char_vcols = incr;
5125 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5126 {
5127 bdp->end_vcol = bdp->start_vcol;
5128#ifdef FEAT_VISUALEXTRA
5129 bdp->is_short = TRUE;
5130#endif
5131 if (!is_del || oap->op_type == OP_APPEND)
5132 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5133 }
5134 else
5135 {
5136 /* notice: this converts partly selected Multibyte characters to
5137 * spaces, too. */
5138 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5139 if (is_del && bdp->startspaces)
5140 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5141 pend = pstart;
5142 bdp->end_vcol = bdp->start_vcol;
5143 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5144 {
5145#ifdef FEAT_VISUALEXTRA
5146 bdp->is_oneChar = TRUE;
5147#endif
5148 if (oap->op_type == OP_INSERT)
5149 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5150 else if (oap->op_type == OP_APPEND)
5151 {
5152 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5153 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5154 }
5155 else
5156 {
5157 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5158 if (is_del && oap->op_type != OP_LSHIFT)
5159 {
5160 /* just putting the sum of those two into
5161 * bdp->startspaces doesn't work for Visual replace,
5162 * so we have to split the tab in two */
5163 bdp->startspaces = bdp->start_char_vcols
5164 - (bdp->start_vcol - oap->start_vcol);
5165 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5166 }
5167 }
5168 }
5169 else
5170 {
5171 prev_pend = pend;
5172 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5173 {
5174 /* Count a tab for what it's worth (if list mode not on) */
5175 prev_pend = pend;
5176 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
5177 bdp->end_vcol += incr;
5178 }
5179 if (bdp->end_vcol <= oap->end_vcol
5180 && (!is_del
5181 || oap->op_type == OP_APPEND
5182 || oap->op_type == OP_REPLACE)) /* line too short */
5183 {
5184#ifdef FEAT_VISUALEXTRA
5185 bdp->is_short = TRUE;
5186#endif
5187 /* Alternative: include spaces to fill up the block.
5188 * Disadvantage: can lead to trailing spaces when the line is
5189 * short where the text is put */
5190 /* if (!is_del || oap->op_type == OP_APPEND) */
5191 if (oap->op_type == OP_APPEND || virtual_op)
5192 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005193 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 else
5195 bdp->endspaces = 0; /* replace doesn't add characters */
5196 }
5197 else if (bdp->end_vcol > oap->end_vcol)
5198 {
5199 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5200 if (!is_del && bdp->endspaces)
5201 {
5202 bdp->endspaces = incr - bdp->endspaces;
5203 if (pend != pstart)
5204 pend = prev_pend;
5205 }
5206 }
5207 }
5208#ifdef FEAT_VISUALEXTRA
5209 bdp->end_char_vcols = incr;
5210#endif
5211 if (is_del && bdp->startspaces)
5212 pstart = prev_pstart;
5213 bdp->textlen = (int)(pend - pstart);
5214 }
5215 bdp->textcol = (colnr_T) (pstart - line);
5216 bdp->textstart = pstart;
5217}
5218#endif /* FEAT_VISUAL */
5219
5220#ifdef FEAT_RIGHTLEFT
5221static void reverse_line __ARGS((char_u *s));
5222
5223 static void
5224reverse_line(s)
5225 char_u *s;
5226{
5227 int i, j;
5228 char_u c;
5229
5230 if ((i = (int)STRLEN(s) - 1) <= 0)
5231 return;
5232
5233 curwin->w_cursor.col = i - curwin->w_cursor.col;
5234 for (j = 0; j < i; j++, i--)
5235 {
5236 c = s[i]; s[i] = s[j]; s[j] = c;
5237 }
5238}
5239
5240# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5241#else
5242# define RLADDSUBFIX(ptr)
5243#endif
5244
5245/*
5246 * add or subtract 'Prenum1' from a number in a line
5247 * 'command' is CTRL-A for add, CTRL-X for subtract
5248 *
5249 * return FAIL for failure, OK otherwise
5250 */
5251 int
5252do_addsub(command, Prenum1)
5253 int command;
5254 linenr_T Prenum1;
5255{
5256 int col;
5257 char_u *buf1;
5258 char_u buf2[NUMBUFLEN];
5259 int hex; /* 'X' or 'x': hex; '0': octal */
5260 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005261 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 long_u oldn;
5263 char_u *ptr;
5264 int c;
5265 int length = 0; /* character length of the number */
5266 int todel;
5267 int dohex;
5268 int dooct;
5269 int doalp;
5270 int firstdigit;
5271 int negative;
5272 int subtract;
5273
5274 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5275 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5276 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5277
5278 ptr = ml_get_curline();
5279 RLADDSUBFIX(ptr);
5280
5281 /*
5282 * First check if we are on a hexadecimal number, after the "0x".
5283 */
5284 col = curwin->w_cursor.col;
5285 if (dohex)
5286 while (col > 0 && vim_isxdigit(ptr[col]))
5287 --col;
5288 if ( dohex
5289 && col > 0
5290 && (ptr[col] == 'X'
5291 || ptr[col] == 'x')
5292 && ptr[col - 1] == '0'
5293 && vim_isxdigit(ptr[col + 1]))
5294 {
5295 /*
5296 * Found hexadecimal number, move to its start.
5297 */
5298 --col;
5299 }
5300 else
5301 {
5302 /*
5303 * Search forward and then backward to find the start of number.
5304 */
5305 col = curwin->w_cursor.col;
5306
5307 while (ptr[col] != NUL
5308 && !vim_isdigit(ptr[col])
5309 && !(doalp && ASCII_ISALPHA(ptr[col])))
5310 ++col;
5311
5312 while (col > 0
5313 && vim_isdigit(ptr[col - 1])
5314 && !(doalp && ASCII_ISALPHA(ptr[col])))
5315 --col;
5316 }
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 /*
5319 * If a number was found, and saving for undo works, replace the number.
5320 */
5321 firstdigit = ptr[col];
5322 RLADDSUBFIX(ptr);
5323 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5324 || u_save_cursor() != OK)
5325 {
5326 beep_flush();
5327 return FAIL;
5328 }
5329
5330 /* get ptr again, because u_save() may have changed it */
5331 ptr = ml_get_curline();
5332 RLADDSUBFIX(ptr);
5333
5334 if (doalp && ASCII_ISALPHA(firstdigit))
5335 {
5336 /* decrement or increment alphabetic character */
5337 if (command == Ctrl_X)
5338 {
5339 if (CharOrd(firstdigit) < Prenum1)
5340 {
5341 if (isupper(firstdigit))
5342 firstdigit = 'A';
5343 else
5344 firstdigit = 'a';
5345 }
5346 else
5347#ifdef EBCDIC
5348 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5349#else
5350 firstdigit -= Prenum1;
5351#endif
5352 }
5353 else
5354 {
5355 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5356 {
5357 if (isupper(firstdigit))
5358 firstdigit = 'Z';
5359 else
5360 firstdigit = 'z';
5361 }
5362 else
5363#ifdef EBCDIC
5364 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5365#else
5366 firstdigit += Prenum1;
5367#endif
5368 }
5369 curwin->w_cursor.col = col;
5370 (void)del_char(FALSE);
5371 ins_char(firstdigit);
5372 }
5373 else
5374 {
5375 negative = FALSE;
5376 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5377 {
5378 --col;
5379 negative = TRUE;
5380 }
5381
5382 /* get the number value (unsigned) */
5383 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5384
5385 /* ignore leading '-' for hex and octal numbers */
5386 if (hex && negative)
5387 {
5388 ++col;
5389 --length;
5390 negative = FALSE;
5391 }
5392
5393 /* add or subtract */
5394 subtract = FALSE;
5395 if (command == Ctrl_X)
5396 subtract ^= TRUE;
5397 if (negative)
5398 subtract ^= TRUE;
5399
5400 oldn = n;
5401 if (subtract)
5402 n -= (unsigned long)Prenum1;
5403 else
5404 n += (unsigned long)Prenum1;
5405
5406 /* handle wraparound for decimal numbers */
5407 if (!hex)
5408 {
5409 if (subtract)
5410 {
5411 if (n > oldn)
5412 {
5413 n = 1 + (n ^ (unsigned long)-1);
5414 negative ^= TRUE;
5415 }
5416 }
5417 else /* add */
5418 {
5419 if (n < oldn)
5420 {
5421 n = (n ^ (unsigned long)-1);
5422 negative ^= TRUE;
5423 }
5424 }
5425 if (n == 0)
5426 negative = FALSE;
5427 }
5428
5429 /*
5430 * Delete the old number.
5431 */
5432 curwin->w_cursor.col = col;
5433 todel = length;
5434 c = gchar_cursor();
5435 /*
5436 * Don't include the '-' in the length, only the length of the part
5437 * after it is kept the same.
5438 */
5439 if (c == '-')
5440 --length;
5441 while (todel-- > 0)
5442 {
5443 if (c < 0x100 && isalpha(c))
5444 {
5445 if (isupper(c))
5446 hexupper = TRUE;
5447 else
5448 hexupper = FALSE;
5449 }
5450 /* del_char() will mark line needing displaying */
5451 (void)del_char(FALSE);
5452 c = gchar_cursor();
5453 }
5454
5455 /*
5456 * Prepare the leading characters in buf1[].
5457 * When there are many leading zeros it could be very long. Allocate
5458 * a bit too much.
5459 */
5460 buf1 = alloc((unsigned)length + NUMBUFLEN);
5461 if (buf1 == NULL)
5462 return FAIL;
5463 ptr = buf1;
5464 if (negative)
5465 {
5466 *ptr++ = '-';
5467 }
5468 if (hex)
5469 {
5470 *ptr++ = '0';
5471 --length;
5472 }
5473 if (hex == 'x' || hex == 'X')
5474 {
5475 *ptr++ = hex;
5476 --length;
5477 }
5478
5479 /*
5480 * Put the number characters in buf2[].
5481 */
5482 if (hex == 0)
5483 sprintf((char *)buf2, "%lu", n);
5484 else if (hex == '0')
5485 sprintf((char *)buf2, "%lo", n);
5486 else if (hex && hexupper)
5487 sprintf((char *)buf2, "%lX", n);
5488 else
5489 sprintf((char *)buf2, "%lx", n);
5490 length -= (int)STRLEN(buf2);
5491
5492 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005493 * Adjust number of zeros to the new number of digits, so the
5494 * total length of the number remains the same.
5495 * Don't do this when
5496 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005498 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 while (length-- > 0)
5500 *ptr++ = '0';
5501 *ptr = NUL;
5502 STRCAT(buf1, buf2);
5503 ins_str(buf1); /* insert the new number */
5504 vim_free(buf1);
5505 }
5506 --curwin->w_cursor.col;
5507 curwin->w_set_curswant = TRUE;
5508#ifdef FEAT_RIGHTLEFT
5509 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5510 RLADDSUBFIX(ptr);
5511#endif
5512 return OK;
5513}
5514
5515#ifdef FEAT_VIMINFO
5516 int
5517read_viminfo_register(virp, force)
5518 vir_T *virp;
5519 int force;
5520{
5521 int eof;
5522 int do_it = TRUE;
5523 int size;
5524 int limit;
5525 int i;
5526 int set_prev = FALSE;
5527 char_u *str;
5528 char_u **array = NULL;
5529
5530 /* We only get here (hopefully) if line[0] == '"' */
5531 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005532
5533 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (*str == '"')
5535 {
5536 set_prev = TRUE;
5537 str++;
5538 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 if (!ASCII_ISALNUM(*str) && *str != '-')
5541 {
5542 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5543 return TRUE; /* too many errors, pretend end-of-file */
5544 do_it = FALSE;
5545 }
5546 get_yank_register(*str++, FALSE);
5547 if (!force && y_current->y_array != NULL)
5548 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005549
5550 if (*str == '@')
5551 {
5552 /* "x@: register x used for @@ */
5553 if (force || execreg_lastc == NUL)
5554 execreg_lastc = str[-1];
5555 }
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 size = 0;
5558 limit = 100; /* Optimized for registers containing <= 100 lines */
5559 if (do_it)
5560 {
5561 if (set_prev)
5562 y_previous = y_current;
5563 vim_free(y_current->y_array);
5564 array = y_current->y_array =
5565 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005566 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (STRNCMP(str, "CHAR", 4) == 0)
5568 y_current->y_type = MCHAR;
5569#ifdef FEAT_VISUAL
5570 else if (STRNCMP(str, "BLOCK", 5) == 0)
5571 y_current->y_type = MBLOCK;
5572#endif
5573 else
5574 y_current->y_type = MLINE;
5575 /* get the block width; if it's missing we get a zero, which is OK */
5576 str = skipwhite(skiptowhite(str));
5577#ifdef FEAT_VISUAL
5578 y_current->y_width = getdigits(&str);
5579#else
5580 (void)getdigits(&str);
5581#endif
5582 }
5583
5584 while (!(eof = viminfo_readline(virp))
5585 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5586 {
5587 if (do_it)
5588 {
5589 if (size >= limit)
5590 {
5591 y_current->y_array = (char_u **)
5592 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5593 for (i = 0; i < limit; i++)
5594 y_current->y_array[i] = array[i];
5595 vim_free(array);
5596 limit *= 2;
5597 array = y_current->y_array;
5598 }
5599 str = viminfo_readstring(virp, 1, TRUE);
5600 if (str != NULL)
5601 array[size++] = str;
5602 else
5603 do_it = FALSE;
5604 }
5605 }
5606 if (do_it)
5607 {
5608 if (size == 0)
5609 {
5610 vim_free(array);
5611 y_current->y_array = NULL;
5612 }
5613 else if (size < limit)
5614 {
5615 y_current->y_array =
5616 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5617 for (i = 0; i < size; i++)
5618 y_current->y_array[i] = array[i];
5619 vim_free(array);
5620 }
5621 y_current->y_size = size;
5622 }
5623 return eof;
5624}
5625
5626 void
5627write_viminfo_registers(fp)
5628 FILE *fp;
5629{
5630 int i, j;
5631 char_u *type;
5632 char_u c;
5633 int num_lines;
5634 int max_num_lines;
5635 int max_kbyte;
5636 long len;
5637
Bram Moolenaar64404472010-06-26 06:24:45 +02005638 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 /* Get '<' value, use old '"' value if '<' is not found. */
5641 max_num_lines = get_viminfo_parameter('<');
5642 if (max_num_lines < 0)
5643 max_num_lines = get_viminfo_parameter('"');
5644 if (max_num_lines == 0)
5645 return;
5646 max_kbyte = get_viminfo_parameter('s');
5647 if (max_kbyte == 0)
5648 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005649
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 for (i = 0; i < NUM_REGISTERS; i++)
5651 {
5652 if (y_regs[i].y_array == NULL)
5653 continue;
5654#ifdef FEAT_CLIPBOARD
5655 /* Skip '*'/'+' register, we don't want them back next time */
5656 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5657 continue;
5658#endif
5659#ifdef FEAT_DND
5660 /* Neither do we want the '~' register */
5661 if (i == TILDE_REGISTER)
5662 continue;
5663#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005664 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005666 if (num_lines == 0
5667 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005668 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005669 continue;
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (max_kbyte > 0)
5672 {
5673 /* Skip register if there is more text than the maximum size. */
5674 len = 0;
5675 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005676 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 if (len > (long)max_kbyte * 1024L)
5678 continue;
5679 }
5680
5681 switch (y_regs[i].y_type)
5682 {
5683 case MLINE:
5684 type = (char_u *)"LINE";
5685 break;
5686 case MCHAR:
5687 type = (char_u *)"CHAR";
5688 break;
5689#ifdef FEAT_VISUAL
5690 case MBLOCK:
5691 type = (char_u *)"BLOCK";
5692 break;
5693#endif
5694 default:
5695 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005696 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 emsg(IObuff);
5698 type = (char_u *)"LINE";
5699 break;
5700 }
5701 if (y_previous == &y_regs[i])
5702 fprintf(fp, "\"");
5703 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005704 fprintf(fp, "\"%c", c);
5705 if (c == execreg_lastc)
5706 fprintf(fp, "@");
5707 fprintf(fp, "\t%s\t%d\n", type,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#ifdef FEAT_VISUAL
5709 (int)y_regs[i].y_width
5710#else
5711 0
5712#endif
5713 );
5714
5715 /* If max_num_lines < 0, then we save ALL the lines in the register */
5716 if (max_num_lines > 0 && num_lines > max_num_lines)
5717 num_lines = max_num_lines;
5718 for (j = 0; j < num_lines; j++)
5719 {
5720 putc('\t', fp);
5721 viminfo_writestring(fp, y_regs[i].y_array[j]);
5722 }
5723 }
5724}
5725#endif /* FEAT_VIMINFO */
5726
5727#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5728/*
5729 * SELECTION / PRIMARY ('*')
5730 *
5731 * Text selection stuff that uses the GUI selection register '*'. When using a
5732 * GUI this may be text from another window, otherwise it is the last text we
5733 * had highlighted with VIsual mode. With mouse support, clicking the middle
5734 * button performs the paste, otherwise you will need to do <"*p>. "
5735 * If not under X, it is synonymous with the clipboard register '+'.
5736 *
5737 * X CLIPBOARD ('+')
5738 *
5739 * Text selection stuff that uses the GUI clipboard register '+'.
5740 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5741 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5742 * otherwise you will need to do <"+p>. "
5743 * If not under X, it is synonymous with the selection register '*'.
5744 */
5745
5746/*
5747 * Routine to export any final X selection we had to the environment
5748 * so that the text is still available after vim has exited. X selections
5749 * only exist while the owning application exists, so we write to the
5750 * permanent (while X runs) store CUT_BUFFER0.
5751 * Dump the CLIPBOARD selection if we own it (it's logically the more
5752 * 'permanent' of the two), otherwise the PRIMARY one.
5753 * For now, use a hard-coded sanity limit of 1Mb of data.
5754 */
5755#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5756 void
5757x11_export_final_selection()
5758{
5759 Display *dpy;
5760 char_u *str = NULL;
5761 long_u len = 0;
5762 int motion_type = -1;
5763
5764# ifdef FEAT_GUI
5765 if (gui.in_use)
5766 dpy = X_DISPLAY;
5767 else
5768# endif
5769# ifdef FEAT_XCLIPBOARD
5770 dpy = xterm_dpy;
5771# else
5772 return;
5773# endif
5774
5775 /* Get selection to export */
5776 if (clip_plus.owned)
5777 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5778 else if (clip_star.owned)
5779 motion_type = clip_convert_selection(&str, &len, &clip_star);
5780
5781 /* Check it's OK */
5782 if (dpy != NULL && str != NULL && motion_type >= 0
5783 && len < 1024*1024 && len > 0)
5784 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005785#ifdef FEAT_MBYTE
5786 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5787 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5788 * encoding conversion usually doesn't work, so keep the text as-is.
5789 */
5790 if (has_mbyte)
5791 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005792 vimconv_T vc;
5793
5794 vc.vc_type = CONV_NONE;
5795 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5796 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005797 int intlen = len;
5798 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005799
5800 conv_str = string_convert(&vc, str, &intlen);
5801 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005802 if (conv_str != NULL)
5803 {
5804 vim_free(str);
5805 str = conv_str;
5806 }
5807 convert_setup(&vc, NULL, NULL);
5808 }
5809 }
5810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5812 XFlush(dpy);
5813 }
5814
5815 vim_free(str);
5816}
5817#endif
5818
5819 void
5820clip_free_selection(cbd)
5821 VimClipboard *cbd;
5822{
5823 struct yankreg *y_ptr = y_current;
5824
5825 if (cbd == &clip_plus)
5826 y_current = &y_regs[PLUS_REGISTER];
5827 else
5828 y_current = &y_regs[STAR_REGISTER];
5829 free_yank_all();
5830 y_current->y_size = 0;
5831 y_current = y_ptr;
5832}
5833
5834/*
5835 * Get the selected text and put it in the gui selection register '*' or '+'.
5836 */
5837 void
5838clip_get_selection(cbd)
5839 VimClipboard *cbd;
5840{
5841 struct yankreg *old_y_previous, *old_y_current;
5842 pos_T old_cursor;
5843#ifdef FEAT_VISUAL
5844 pos_T old_visual;
5845 int old_visual_mode;
5846#endif
5847 colnr_T old_curswant;
5848 int old_set_curswant;
5849 pos_T old_op_start, old_op_end;
5850 oparg_T oa;
5851 cmdarg_T ca;
5852
5853 if (cbd->owned)
5854 {
5855 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5856 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5857 return;
5858
5859 /* Get the text between clip_star.start & clip_star.end */
5860 old_y_previous = y_previous;
5861 old_y_current = y_current;
5862 old_cursor = curwin->w_cursor;
5863 old_curswant = curwin->w_curswant;
5864 old_set_curswant = curwin->w_set_curswant;
5865 old_op_start = curbuf->b_op_start;
5866 old_op_end = curbuf->b_op_end;
5867#ifdef FEAT_VISUAL
5868 old_visual = VIsual;
5869 old_visual_mode = VIsual_mode;
5870#endif
5871 clear_oparg(&oa);
5872 oa.regname = (cbd == &clip_plus ? '+' : '*');
5873 oa.op_type = OP_YANK;
5874 vim_memset(&ca, 0, sizeof(ca));
5875 ca.oap = &oa;
5876 ca.cmdchar = 'y';
5877 ca.count1 = 1;
5878 ca.retval = CA_NO_ADJ_OP_END;
5879 do_pending_operator(&ca, 0, TRUE);
5880 y_previous = old_y_previous;
5881 y_current = old_y_current;
5882 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005883 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 curwin->w_curswant = old_curswant;
5885 curwin->w_set_curswant = old_set_curswant;
5886 curbuf->b_op_start = old_op_start;
5887 curbuf->b_op_end = old_op_end;
5888#ifdef FEAT_VISUAL
5889 VIsual = old_visual;
5890 VIsual_mode = old_visual_mode;
5891#endif
5892 }
5893 else
5894 {
5895 clip_free_selection(cbd);
5896
5897 /* Try to get selected text from another window */
5898 clip_gen_request_selection(cbd);
5899 }
5900}
5901
Bram Moolenaard44347f2011-06-19 01:14:29 +02005902/*
5903 * Convert from the GUI selection string into the '*'/'+' register.
5904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 void
5906clip_yank_selection(type, str, len, cbd)
5907 int type;
5908 char_u *str;
5909 long len;
5910 VimClipboard *cbd;
5911{
5912 struct yankreg *y_ptr;
5913
5914 if (cbd == &clip_plus)
5915 y_ptr = &y_regs[PLUS_REGISTER];
5916 else
5917 y_ptr = &y_regs[STAR_REGISTER];
5918
5919 clip_free_selection(cbd);
5920
5921 str_to_reg(y_ptr, type, str, len, 0L);
5922}
5923
5924/*
5925 * Convert the '*'/'+' register into a GUI selection string returned in *str
5926 * with length *len.
5927 * Returns the motion type, or -1 for failure.
5928 */
5929 int
5930clip_convert_selection(str, len, cbd)
5931 char_u **str;
5932 long_u *len;
5933 VimClipboard *cbd;
5934{
5935 char_u *p;
5936 int lnum;
5937 int i, j;
5938 int_u eolsize;
5939 struct yankreg *y_ptr;
5940
5941 if (cbd == &clip_plus)
5942 y_ptr = &y_regs[PLUS_REGISTER];
5943 else
5944 y_ptr = &y_regs[STAR_REGISTER];
5945
5946#ifdef USE_CRNL
5947 eolsize = 2;
5948#else
5949 eolsize = 1;
5950#endif
5951
5952 *str = NULL;
5953 *len = 0;
5954 if (y_ptr->y_array == NULL)
5955 return -1;
5956
5957 for (i = 0; i < y_ptr->y_size; i++)
5958 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5959
5960 /*
5961 * Don't want newline character at end of last line if we're in MCHAR mode.
5962 */
5963 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5964 *len -= eolsize;
5965
5966 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5967 if (p == NULL)
5968 return -1;
5969 lnum = 0;
5970 for (i = 0, j = 0; i < (int)*len; i++, j++)
5971 {
5972 if (y_ptr->y_array[lnum][j] == '\n')
5973 p[i] = NUL;
5974 else if (y_ptr->y_array[lnum][j] == NUL)
5975 {
5976#ifdef USE_CRNL
5977 p[i++] = '\r';
5978#endif
5979#ifdef USE_CR
5980 p[i] = '\r';
5981#else
5982 p[i] = '\n';
5983#endif
5984 lnum++;
5985 j = -1;
5986 }
5987 else
5988 p[i] = y_ptr->y_array[lnum][j];
5989 }
5990 return y_ptr->y_type;
5991}
5992
5993
5994# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5995/*
5996 * If we have written to a clipboard register, send the text to the clipboard.
5997 */
5998 static void
5999may_set_selection()
6000{
6001 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6002 {
6003 clip_own_selection(&clip_star);
6004 clip_gen_set_selection(&clip_star);
6005 }
6006 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6007 {
6008 clip_own_selection(&clip_plus);
6009 clip_gen_set_selection(&clip_plus);
6010 }
6011}
6012# endif
6013
6014#endif /* FEAT_CLIPBOARD || PROTO */
6015
6016
6017#if defined(FEAT_DND) || defined(PROTO)
6018/*
6019 * Replace the contents of the '~' register with str.
6020 */
6021 void
6022dnd_yank_drag_data(str, len)
6023 char_u *str;
6024 long len;
6025{
6026 struct yankreg *curr;
6027
6028 curr = y_current;
6029 y_current = &y_regs[TILDE_REGISTER];
6030 free_yank_all();
6031 str_to_reg(y_current, MCHAR, str, len, 0L);
6032 y_current = curr;
6033}
6034#endif
6035
6036
6037#if defined(FEAT_EVAL) || defined(PROTO)
6038/*
6039 * Return the type of a register.
6040 * Used for getregtype()
6041 * Returns MAUTO for error.
6042 */
6043 char_u
6044get_reg_type(regname, reglen)
6045 int regname;
6046 long *reglen;
6047{
6048 switch (regname)
6049 {
6050 case '%': /* file name */
6051 case '#': /* alternate file name */
6052 case '=': /* expression */
6053 case ':': /* last command line */
6054 case '/': /* last search-pattern */
6055 case '.': /* last inserted text */
6056#ifdef FEAT_SEARCHPATH
6057 case Ctrl_F: /* Filename under cursor */
6058 case Ctrl_P: /* Path under cursor, expand via "path" */
6059#endif
6060 case Ctrl_W: /* word under cursor */
6061 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6062 case '_': /* black hole: always empty */
6063 return MCHAR;
6064 }
6065
6066#ifdef FEAT_CLIPBOARD
6067 regname = may_get_selection(regname);
6068#endif
6069
6070 /* Should we check for a valid name? */
6071 get_yank_register(regname, FALSE);
6072
6073 if (y_current->y_array != NULL)
6074 {
6075#ifdef FEAT_VISUAL
6076 if (reglen != NULL && y_current->y_type == MBLOCK)
6077 *reglen = y_current->y_width;
6078#endif
6079 return y_current->y_type;
6080 }
6081 return MAUTO;
6082}
6083
6084/*
6085 * Return the contents of a register as a single allocated string.
6086 * Used for "@r" in expressions and for getreg().
6087 * Returns NULL for error.
6088 */
6089 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00006090get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00006092 int allowexpr; /* allow "=" register */
6093 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 long i;
6096 char_u *retval;
6097 int allocated;
6098 long len;
6099
6100 /* Don't allow using an expression register inside an expression */
6101 if (regname == '=')
6102 {
6103 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00006104 {
6105 if (expr_src)
6106 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00006108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 return NULL;
6110 }
6111
6112 if (regname == '@') /* "@@" is used for unnamed register */
6113 regname = '"';
6114
6115 /* check for valid regname */
6116 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6117 return NULL;
6118
6119#ifdef FEAT_CLIPBOARD
6120 regname = may_get_selection(regname);
6121#endif
6122
6123 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6124 {
6125 if (retval == NULL)
6126 return NULL;
6127 if (!allocated)
6128 retval = vim_strsave(retval);
6129 return retval;
6130 }
6131
6132 get_yank_register(regname, FALSE);
6133 if (y_current->y_array == NULL)
6134 return NULL;
6135
6136 /*
6137 * Compute length of resulting string.
6138 */
6139 len = 0;
6140 for (i = 0; i < y_current->y_size; ++i)
6141 {
6142 len += (long)STRLEN(y_current->y_array[i]);
6143 /*
6144 * Insert a newline between lines and after last line if
6145 * y_type is MLINE.
6146 */
6147 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6148 ++len;
6149 }
6150
6151 retval = lalloc(len + 1, TRUE);
6152
6153 /*
6154 * Copy the lines of the yank register into the string.
6155 */
6156 if (retval != NULL)
6157 {
6158 len = 0;
6159 for (i = 0; i < y_current->y_size; ++i)
6160 {
6161 STRCPY(retval + len, y_current->y_array[i]);
6162 len += (long)STRLEN(retval + len);
6163
6164 /*
6165 * Insert a NL between lines and after the last line if y_type is
6166 * MLINE.
6167 */
6168 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6169 retval[len++] = '\n';
6170 }
6171 retval[len] = NUL;
6172 }
6173
6174 return retval;
6175}
6176
6177/*
6178 * Store string "str" in register "name".
6179 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6180 * If "must_append" is TRUE, always append to the register. Otherwise append
6181 * if "name" is an uppercase letter.
6182 * Note: "maxlen" and "must_append" don't work for the "/" register.
6183 * Careful: 'str' is modified, you may have to use a copy!
6184 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6185 */
6186 void
6187write_reg_contents(name, str, maxlen, must_append)
6188 int name;
6189 char_u *str;
6190 int maxlen;
6191 int must_append;
6192{
6193 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6194}
6195
6196 void
6197write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6198 int name;
6199 char_u *str;
6200 int maxlen;
6201 int must_append;
6202 int yank_type;
6203 long block_len;
6204{
6205 struct yankreg *old_y_previous, *old_y_current;
6206 long len;
6207
Bram Moolenaare7566042005-06-17 22:00:15 +00006208 if (maxlen >= 0)
6209 len = maxlen;
6210 else
6211 len = (long)STRLEN(str);
6212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 /* Special case: '/' search pattern */
6214 if (name == '/')
6215 {
6216 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6217 return;
6218 }
6219
Bram Moolenaare7566042005-06-17 22:00:15 +00006220#ifdef FEAT_EVAL
6221 if (name == '=')
6222 {
6223 char_u *p, *s;
6224
6225 p = vim_strnsave(str, (int)len);
6226 if (p == NULL)
6227 return;
6228 if (must_append)
6229 {
6230 s = concat_str(get_expr_line_src(), p);
6231 vim_free(p);
6232 p = s;
6233
6234 }
6235 set_expr_line(p);
6236 return;
6237 }
6238#endif
6239
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6241 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006242 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return;
6244 }
6245
6246 if (name == '_') /* black hole: nothing to do */
6247 return;
6248
6249 /* Don't want to change the current (unnamed) register */
6250 old_y_previous = y_previous;
6251 old_y_current = y_current;
6252
6253 get_yank_register(name, TRUE);
6254 if (!y_append && !must_append)
6255 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256#ifndef FEAT_VISUAL
6257 /* Just in case - make sure we don't use MBLOCK */
6258 if (yank_type == MBLOCK)
6259 yank_type = MAUTO;
6260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 str_to_reg(y_current, yank_type, str, len, block_len);
6262
6263# ifdef FEAT_CLIPBOARD
6264 /* Send text of clipboard register to the clipboard. */
6265 may_set_selection();
6266# endif
6267
6268 /* ':let @" = "val"' should change the meaning of the "" register */
6269 if (name != '"')
6270 y_previous = old_y_previous;
6271 y_current = old_y_current;
6272}
6273#endif /* FEAT_EVAL */
6274
6275#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6276/*
6277 * Put a string into a register. When the register is not empty, the string
6278 * is appended.
6279 */
6280 static void
Bram Moolenaard44347f2011-06-19 01:14:29 +02006281str_to_reg(y_ptr, yank_type, str, len, blocklen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006283 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 char_u *str; /* string to put in register */
6285 long len; /* length of string */
6286 long blocklen; /* width of Visual block */
6287{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006288 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 int lnum;
6290 long start;
6291 long i;
6292 int extra;
6293 int newlines; /* number of lines added */
6294 int extraline = 0; /* extra line at the end */
6295 int append = FALSE; /* append to last line in register */
6296 char_u *s;
6297 char_u **pp;
6298#ifdef FEAT_VISUAL
6299 long maxlen;
6300#endif
6301
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006302 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 y_ptr->y_size = 0;
6304
Bram Moolenaard44347f2011-06-19 01:14:29 +02006305 if (yank_type == MAUTO)
6306 type = ((len > 0 && (str[len - 1] == NL || str[len - 1] == CAR))
6307 ? MLINE : MCHAR);
6308 else
6309 type = yank_type;
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 /*
6312 * Count the number of lines within the string
6313 */
6314 newlines = 0;
6315 for (i = 0; i < len; i++)
6316 if (str[i] == '\n')
6317 ++newlines;
6318 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6319 {
6320 extraline = 1;
6321 ++newlines; /* count extra newline at the end */
6322 }
6323 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6324 {
6325 append = TRUE;
6326 --newlines; /* uncount newline when appending first line */
6327 }
6328
6329 /*
6330 * Allocate an array to hold the pointers to the new register lines.
6331 * If the register was not empty, move the existing lines to the new array.
6332 */
6333 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6334 * sizeof(char_u *), TRUE);
6335 if (pp == NULL) /* out of memory */
6336 return;
6337 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6338 pp[lnum] = y_ptr->y_array[lnum];
6339 vim_free(y_ptr->y_array);
6340 y_ptr->y_array = pp;
6341#ifdef FEAT_VISUAL
6342 maxlen = 0;
6343#endif
6344
6345 /*
6346 * Find the end of each line and save it into the array.
6347 */
6348 for (start = 0; start < len + extraline; start += i + 1)
6349 {
6350 for (i = start; i < len; ++i) /* find the end of the line */
6351 if (str[i] == '\n')
6352 break;
6353 i -= start; /* i is now length of line */
6354#ifdef FEAT_VISUAL
6355 if (i > maxlen)
6356 maxlen = i;
6357#endif
6358 if (append)
6359 {
6360 --lnum;
6361 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6362 }
6363 else
6364 extra = 0;
6365 s = alloc((unsigned)(i + extra + 1));
6366 if (s == NULL)
6367 break;
6368 if (extra)
6369 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6370 if (append)
6371 vim_free(y_ptr->y_array[lnum]);
6372 if (i)
6373 mch_memmove(s + extra, str + start, (size_t)i);
6374 extra += i;
6375 s[extra] = NUL;
6376 y_ptr->y_array[lnum++] = s;
6377 while (--extra >= 0)
6378 {
6379 if (*s == NUL)
6380 *s = '\n'; /* replace NUL with newline */
6381 ++s;
6382 }
6383 append = FALSE; /* only first line is appended */
6384 }
6385 y_ptr->y_type = type;
6386 y_ptr->y_size = lnum;
6387# ifdef FEAT_VISUAL
6388 if (type == MBLOCK)
6389 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6390 else
6391 y_ptr->y_width = 0;
6392# endif
6393}
6394#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6395
6396 void
6397clear_oparg(oap)
6398 oparg_T *oap;
6399{
6400 vim_memset(oap, 0, sizeof(oparg_T));
6401}
6402
Bram Moolenaar7c626922005-02-07 22:01:03 +00006403static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006406 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 *
6408 * "Words" are counted by looking for boundaries between non-space and
6409 * space characters. (it seems to produce results that match 'wc'.)
6410 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006411 * Return value is byte count; word count for the line is added to "*wc".
6412 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 *
6414 * The function will only examine the first "limit" characters in the
6415 * line, stopping if it encounters an end-of-line (NUL byte). In that
6416 * case, eol_size will be added to the character count to account for
6417 * the size of the EOL character.
6418 */
6419 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006420line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 char_u *line;
6422 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006423 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 long limit;
6425 int eol_size;
6426{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006427 long i;
6428 long words = 0;
6429 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 int is_word = 0;
6431
Bram Moolenaar7c626922005-02-07 22:01:03 +00006432 for (i = 0; line[i] && i < limit; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
6434 if (is_word)
6435 {
6436 if (vim_isspace(line[i]))
6437 {
6438 words++;
6439 is_word = 0;
6440 }
6441 }
6442 else if (!vim_isspace(line[i]))
6443 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006444 ++chars;
6445#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006446 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006447#else
6448 ++i;
6449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
6451
6452 if (is_word)
6453 words++;
6454 *wc += words;
6455
6456 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006457 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006458 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006460 chars += eol_size;
6461 }
6462 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 return i;
6464}
6465
6466/*
6467 * Give some info about the position of the cursor (for "g CTRL-G").
6468 * In Visual mode, give some info about the selected region. (In this case,
6469 * the *_count_cursor variables store running totals for the selection.)
6470 */
6471 void
6472cursor_pos_info()
6473{
6474 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006475 char_u buf1[50];
6476 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006478 long byte_count = 0;
6479 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 long char_count = 0;
6481 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 long word_count = 0;
6483 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006484 int eol_size;
6485 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486#ifdef FEAT_VISUAL
6487 long line_count_selected = 0;
6488 pos_T min_pos, max_pos;
6489 oparg_T oparg;
6490 struct block_def bd;
6491#endif
6492
6493 /*
6494 * Compute the length of the file in characters.
6495 */
6496 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6497 {
6498 MSG(_(no_lines_msg));
6499 }
6500 else
6501 {
6502 if (get_fileformat(curbuf) == EOL_DOS)
6503 eol_size = 2;
6504 else
6505 eol_size = 1;
6506
6507#ifdef FEAT_VISUAL
6508 if (VIsual_active)
6509 {
6510 if (lt(VIsual, curwin->w_cursor))
6511 {
6512 min_pos = VIsual;
6513 max_pos = curwin->w_cursor;
6514 }
6515 else
6516 {
6517 min_pos = curwin->w_cursor;
6518 max_pos = VIsual;
6519 }
6520 if (*p_sel == 'e' && max_pos.col > 0)
6521 --max_pos.col;
6522
6523 if (VIsual_mode == Ctrl_V)
6524 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006525#ifdef FEAT_LINEBREAK
6526 char_u * saved_sbr = p_sbr;
6527
6528 /* Make 'sbr' empty for a moment to get the correct size. */
6529 p_sbr = empty_option;
6530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 oparg.is_VIsual = 1;
6532 oparg.block_mode = TRUE;
6533 oparg.op_type = OP_NOP;
6534 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006535 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006536#ifdef FEAT_LINEBREAK
6537 p_sbr = saved_sbr;
6538#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006539 if (curwin->w_curswant == MAXCOL)
6540 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 /* Swap the start, end vcol if needed */
6542 if (oparg.end_vcol < oparg.start_vcol)
6543 {
6544 oparg.end_vcol += oparg.start_vcol;
6545 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6546 oparg.end_vcol -= oparg.start_vcol;
6547 }
6548 }
6549 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6550 }
6551#endif
6552
6553 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6554 {
6555 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006556 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
6558 ui_breakcheck();
6559 if (got_int)
6560 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006561 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563
6564#ifdef FEAT_VISUAL
6565 /* Do extra processing for VIsual mode. */
6566 if (VIsual_active
6567 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6568 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006569 char_u *s = NULL;
6570 long len = 0L;
6571
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 switch (VIsual_mode)
6573 {
6574 case Ctrl_V:
6575# ifdef FEAT_VIRTUALEDIT
6576 virtual_op = virtual_active();
6577# endif
6578 block_prep(&oparg, &bd, lnum, 0);
6579# ifdef FEAT_VIRTUALEDIT
6580 virtual_op = MAYBE;
6581# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006582 s = bd.textstart;
6583 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 break;
6585 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006586 s = ml_get(lnum);
6587 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 break;
6589 case 'v':
6590 {
6591 colnr_T start_col = (lnum == min_pos.lnum)
6592 ? min_pos.col : 0;
6593 colnr_T end_col = (lnum == max_pos.lnum)
6594 ? max_pos.col - start_col + 1 : MAXCOL;
6595
Bram Moolenaardef9e822004-12-31 20:58:58 +00006596 s = ml_get(lnum) + start_col;
6597 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
6599 break;
6600 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006601 if (s != NULL)
6602 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006603 byte_count_cursor += line_count_info(s, &word_count_cursor,
6604 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006605 if (lnum == curbuf->b_ml.ml_line_count
6606 && !curbuf->b_p_eol
6607 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006608 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006609 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 }
6612 else
6613#endif
6614 {
6615 /* In non-visual mode, check for the line the cursor is on */
6616 if (lnum == curwin->w_cursor.lnum)
6617 {
6618 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006619 char_count_cursor += char_count;
6620 byte_count_cursor = byte_count +
6621 line_count_info(ml_get(lnum),
6622 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 (long)(curwin->w_cursor.col + 1), eol_size);
6624 }
6625 }
6626 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006627 byte_count += line_count_info(ml_get(lnum), &word_count,
6628 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630
6631 /* Correction for when last line doesn't have an EOL. */
6632 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006633 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635#ifdef FEAT_VISUAL
6636 if (VIsual_active)
6637 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006638 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
6640 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006641 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006642 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6644 }
6645 else
6646 buf1[0] = NUL;
6647
Bram Moolenaar7c626922005-02-07 22:01:03 +00006648 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006649 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006650 vim_snprintf((char *)IObuff, IOSIZE,
6651 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 buf1, line_count_selected,
6653 (long)curbuf->b_ml.ml_line_count,
6654 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006655 byte_count_cursor, byte_count);
6656 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006657 vim_snprintf((char *)IObuff, IOSIZE,
6658 _("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 +00006659 buf1, line_count_selected,
6660 (long)curbuf->b_ml.ml_line_count,
6661 word_count_cursor, word_count,
6662 char_count_cursor, char_count,
6663 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 }
6665 else
6666#endif
6667 {
6668 p = ml_get_curline();
6669 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006670 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 (int)curwin->w_virtcol + 1);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006672 col_print(buf2, sizeof(buf2), (int)STRLEN(p), linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaar7c626922005-02-07 22:01:03 +00006674 if (char_count_cursor == byte_count_cursor
6675 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006676 vim_snprintf((char *)IObuff, IOSIZE,
6677 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 (char *)buf1, (char *)buf2,
6679 (long)curwin->w_cursor.lnum,
6680 (long)curbuf->b_ml.ml_line_count,
6681 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006682 byte_count_cursor, byte_count);
6683 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006684 vim_snprintf((char *)IObuff, IOSIZE,
6685 _("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 +00006686 (char *)buf1, (char *)buf2,
6687 (long)curwin->w_cursor.lnum,
6688 (long)curbuf->b_ml.ml_line_count,
6689 word_count_cursor, word_count,
6690 char_count_cursor, char_count,
6691 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 }
6693
6694#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006695 byte_count = bomb_size();
6696 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006698 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699#endif
6700 /* Don't shorten this message, the user asked for it. */
6701 p = p_shm;
6702 p_shm = (char_u *)"";
6703 msg(IObuff);
6704 p_shm = p;
6705 }
6706}