blob: d2060a47e83890a8c007ae3ccc7bbfa3b709203b [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);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200270#ifdef FEAT_FOLDING
271 /* The cursor line is not in a closed fold */
272 foldOpenCursor();
273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275#ifdef FEAT_VISUAL
276 if (oap->block_mode)
277 {
278 curwin->w_cursor.lnum = oap->start.lnum;
279 curwin->w_cursor.col = block_col;
280 }
281 else
282#endif
283 if (curs_top) /* put cursor on first line, for ">>" */
284 {
285 curwin->w_cursor.lnum = oap->start.lnum;
286 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
287 }
288 else
289 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
290
291 if (oap->line_count > p_report)
292 {
293 if (oap->op_type == OP_RSHIFT)
294 s = (char_u *)">";
295 else
296 s = (char_u *)"<";
297 if (oap->line_count == 1)
298 {
299 if (amount == 1)
300 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
301 else
302 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
303 }
304 else
305 {
306 if (amount == 1)
307 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
308 oap->line_count, s);
309 else
310 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
311 oap->line_count, s, amount);
312 }
313 msg(IObuff);
314 }
315
316 /*
317 * Set "'[" and "']" marks.
318 */
319 curbuf->b_op_start = oap->start;
320 curbuf->b_op_end.lnum = oap->end.lnum;
321 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
322 if (curbuf->b_op_end.col > 0)
323 --curbuf->b_op_end.col;
324}
325
326/*
327 * shift the current line one shiftwidth left (if left != 0) or right
328 * leaves cursor on first blank in the line
329 */
330 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000331shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 int left;
333 int round;
334 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000335 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 int count;
338 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100339 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340
341 count = get_indent(); /* get current indent */
342
343 if (round) /* round off indent */
344 {
345 i = count / p_sw; /* number of p_sw rounded down */
346 j = count % p_sw; /* extra spaces */
347 if (j && left) /* first remove extra spaces */
348 --amount;
349 if (left)
350 {
351 i -= amount;
352 if (i < 0)
353 i = 0;
354 }
355 else
356 i += amount;
357 count = i * p_sw;
358 }
359 else /* original vi indent */
360 {
361 if (left)
362 {
363 count -= p_sw * amount;
364 if (count < 0)
365 count = 0;
366 }
367 else
368 count += p_sw * amount;
369 }
370
371 /* Set new indent */
372#ifdef FEAT_VREPLACE
373 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000374 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 else
376#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000377 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378}
379
380#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
381/*
382 * Shift one line of the current block one shiftwidth right or left.
383 * Leaves cursor on first character in block.
384 */
385 static void
386shift_block(oap, amount)
387 oparg_T *oap;
388 int amount;
389{
390 int left = (oap->op_type == OP_LSHIFT);
391 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000392 int total;
393 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100395 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 int p_ts = (int)curbuf->b_p_ts;
397 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000399 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 int i = 0, j = 0;
401 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#ifdef FEAT_RIGHTLEFT
403 int old_p_ri = p_ri;
404
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200405 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#endif
407
408 State = INSERT; /* don't want REPLACE for State */
409 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
410 if (bd.is_short)
411 return;
412
413 /* total is number of screen columns to be inserted/removed */
414 total = amount * p_sw;
415 oldp = ml_get_curline();
416
417 if (!left)
418 {
419 /*
420 * 1. Get start vcol
421 * 2. Total ws vcols
422 * 3. Divvy into TABs & spp
423 * 4. Construct new string
424 */
425 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
426 ws_vcol = bd.start_vcol - bd.pre_whitesp;
427 if (bd.startspaces)
428 {
429#ifdef FEAT_MBYTE
430 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000431 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000434 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 }
436 for ( ; vim_iswhite(*bd.textstart); )
437 {
438 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
439 total += incr;
440 bd.start_vcol += incr;
441 }
442 /* OK, now total=all the VWS reqd, and textstart points at the 1st
443 * non-ws char in the block. */
444 if (!curbuf->b_p_et)
445 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
446 if (i)
447 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
448 else
449 j = total;
450 /* if we're splitting a TAB, allow for it */
451 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
452 len = (int)STRLEN(bd.textstart) + 1;
453 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
454 if (newp == NULL)
455 return;
456 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
457 mch_memmove(newp, oldp, (size_t)bd.textcol);
458 copy_chars(newp + bd.textcol, (size_t)i, TAB);
459 copy_spaces(newp + bd.textcol + i, (size_t)j);
460 /* the end */
461 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
462 }
463 else /* left */
464 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000465 colnr_T destination_col; /* column to which text in block will
466 be shifted */
467 char_u *verbatim_copy_end; /* end of the part of the line which is
468 copied verbatim */
469 colnr_T verbatim_copy_width;/* the (displayed) width of this part
470 of line */
471 unsigned fill; /* nr of spaces that replace a TAB */
472 unsigned new_line_len; /* the length of the line after the
473 block shift */
474 size_t block_space_width;
475 size_t shift_amount;
476 char_u *non_white = bd.textstart;
477 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000479 /*
480 * Firstly, let's find the first non-whitespace character that is
481 * displayed after the block's start column and the character's column
482 * number. Also, let's calculate the width of all the whitespace
483 * characters that are displayed in the block and precede the searched
484 * non-whitespace character.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000487 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
488 * the part of which is displayed at the block's beginning. Let's start
489 * searching from the next character. */
490 if (bd.startspaces)
491 mb_ptr_adv(non_white);
492
493 /* The character's column is in "bd.start_vcol". */
494 non_white_col = bd.start_vcol;
495
496 while (vim_iswhite(*non_white))
497 {
498 incr = lbr_chartabsize_adv(&non_white, non_white_col);
499 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 }
501
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502 block_space_width = non_white_col - oap->start_vcol;
503 /* We will shift by "total" or "block_space_width", whichever is less.
504 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000505 shift_amount = (block_space_width < (size_t)total
506 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000507
508 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000509 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000510
511 /* Now let's find out how much of the beginning of the line we can
512 * reuse without modification. */
513 verbatim_copy_end = bd.textstart;
514 verbatim_copy_width = bd.start_vcol;
515
516 /* If "bd.startspaces" is set, "bd.textstart" points to the character
517 * preceding the block. We have to subtract its width to obtain its
518 * column number. */
519 if (bd.startspaces)
520 verbatim_copy_width -= bd.start_char_vcols;
521 while (verbatim_copy_width < destination_col)
522 {
523 incr = lbr_chartabsize(verbatim_copy_end, verbatim_copy_width);
524 if (verbatim_copy_width + incr > destination_col)
525 break;
526 verbatim_copy_width += incr;
527 mb_ptr_adv(verbatim_copy_end);
528 }
529
530 /* If "destination_col" is different from the width of the initial
531 * part of the line that will be copied, it means we encountered a tab
532 * character, which we will have to partly replace with spaces. */
533 fill = destination_col - verbatim_copy_width;
534
535 /* The replacement line will consist of:
536 * - the beginning of the original line up to "verbatim_copy_end",
537 * - "fill" number of spaces,
538 * - the rest of the line, pointed to by non_white. */
539 new_line_len = (unsigned)(verbatim_copy_end - oldp)
540 + fill
541 + (unsigned)STRLEN(non_white) + 1;
542
543 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (newp == NULL)
545 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000546 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
547 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
548 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 }
550 /* replace the line */
551 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
552 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
553 State = oldstate;
554 curwin->w_cursor.col = oldcol;
555#ifdef FEAT_RIGHTLEFT
556 p_ri = old_p_ri;
557#endif
558}
559#endif
560
561#ifdef FEAT_VISUALEXTRA
562/*
563 * Insert string "s" (b_insert ? before : after) block :AKelly
564 * Caller must prepare for undo.
565 */
566 static void
567block_insert(oap, s, b_insert, bdp)
568 oparg_T *oap;
569 char_u *s;
570 int b_insert;
571 struct block_def *bdp;
572{
573 int p_ts;
574 int count = 0; /* extra spaces to replace a cut TAB */
575 int spaces = 0; /* non-zero if cutting a TAB */
576 colnr_T offset; /* pointer along new line */
577 unsigned s_len; /* STRLEN(s) */
578 char_u *newp, *oldp; /* new, old lines */
579 linenr_T lnum; /* loop var */
580 int oldstate = State;
581
582 State = INSERT; /* don't want REPLACE for State */
583 s_len = (unsigned)STRLEN(s);
584
585 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
586 {
587 block_prep(oap, bdp, lnum, TRUE);
588 if (bdp->is_short && b_insert)
589 continue; /* OP_INSERT, line ends before block start */
590
591 oldp = ml_get(lnum);
592
593 if (b_insert)
594 {
595 p_ts = bdp->start_char_vcols;
596 spaces = bdp->startspaces;
597 if (spaces != 0)
598 count = p_ts - 1; /* we're cutting a TAB */
599 offset = bdp->textcol;
600 }
601 else /* append */
602 {
603 p_ts = bdp->end_char_vcols;
604 if (!bdp->is_short) /* spaces = padding after block */
605 {
606 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
607 if (spaces != 0)
608 count = p_ts - 1; /* we're cutting a TAB */
609 offset = bdp->textcol + bdp->textlen - (spaces != 0);
610 }
611 else /* spaces = padding to block edge */
612 {
613 /* if $ used, just append to EOL (ie spaces==0) */
614 if (!bdp->is_MAX)
615 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
616 count = spaces;
617 offset = bdp->textcol + bdp->textlen;
618 }
619 }
620
621 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
622 if (newp == NULL)
623 continue;
624
625 /* copy up to shifted part */
626 mch_memmove(newp, oldp, (size_t)(offset));
627 oldp += offset;
628
629 /* insert pre-padding */
630 copy_spaces(newp + offset, (size_t)spaces);
631
632 /* copy the new text */
633 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
634 offset += s_len;
635
636 if (spaces && !bdp->is_short)
637 {
638 /* insert post-padding */
639 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
640 /* We're splitting a TAB, don't copy it. */
641 oldp++;
642 /* We allowed for that TAB, remember this now */
643 count++;
644 }
645
646 if (spaces > 0)
647 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000648 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 ml_replace(lnum, newp, FALSE);
651
652 if (lnum == oap->end.lnum)
653 {
654 /* Set "']" mark to the end of the block instead of the end of
655 * the insert in the first line. */
656 curbuf->b_op_end.lnum = oap->end.lnum;
657 curbuf->b_op_end.col = offset;
658 }
659 } /* for all lnum */
660
661 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
662
663 State = oldstate;
664}
665#endif
666
667#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
668/*
669 * op_reindent - handle reindenting a block of lines.
670 */
671 void
672op_reindent(oap, how)
673 oparg_T *oap;
674 int (*how) __ARGS((void));
675{
676 long i;
677 char_u *l;
678 int count;
679 linenr_T first_changed = 0;
680 linenr_T last_changed = 0;
681 linenr_T start_lnum = curwin->w_cursor.lnum;
682
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000683 /* Don't even try when 'modifiable' is off. */
684 if (!curbuf->b_p_ma)
685 {
686 EMSG(_(e_modifiable));
687 return;
688 }
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 for (i = oap->line_count; --i >= 0 && !got_int; )
691 {
692 /* it's a slow thing to do, so give feedback so there's no worry that
693 * the computer's just hung. */
694
695 if (i > 1
696 && (i % 50 == 0 || i == oap->line_count - 1)
697 && oap->line_count > p_report)
698 smsg((char_u *)_("%ld lines to indent... "), i);
699
700 /*
701 * Be vi-compatible: For lisp indenting the first line is not
702 * indented, unless there is only one line.
703 */
704#ifdef FEAT_LISP
705 if (i != oap->line_count - 1 || oap->line_count == 1
706 || how != get_lisp_indent)
707#endif
708 {
709 l = skipwhite(ml_get_curline());
710 if (*l == NUL) /* empty or blank line */
711 count = 0;
712 else
713 count = how(); /* get the indent for this line */
714
715 if (set_indent(count, SIN_UNDO))
716 {
717 /* did change the indent, call changed_lines() later */
718 if (first_changed == 0)
719 first_changed = curwin->w_cursor.lnum;
720 last_changed = curwin->w_cursor.lnum;
721 }
722 }
723 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000724 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 }
726
727 /* put cursor on first non-blank of indented line */
728 curwin->w_cursor.lnum = start_lnum;
729 beginline(BL_SOL | BL_FIX);
730
731 /* Mark changed lines so that they will be redrawn. When Visual
732 * highlighting was present, need to continue until the last line. When
733 * there is no change still need to remove the Visual highlighting. */
734 if (last_changed != 0)
735 changed_lines(first_changed, 0,
736#ifdef FEAT_VISUAL
737 oap->is_VIsual ? start_lnum + oap->line_count :
738#endif
739 last_changed + 1, 0L);
740#ifdef FEAT_VISUAL
741 else if (oap->is_VIsual)
742 redraw_curbuf_later(INVERTED);
743#endif
744
745 if (oap->line_count > p_report)
746 {
747 i = oap->line_count - (i + 1);
748 if (i == 1)
749 MSG(_("1 line indented "));
750 else
751 smsg((char_u *)_("%ld lines indented "), i);
752 }
753 /* set '[ and '] marks */
754 curbuf->b_op_start = oap->start;
755 curbuf->b_op_end = oap->end;
756}
757#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
758
759#if defined(FEAT_EVAL) || defined(PROTO)
760/*
761 * Keep the last expression line here, for repeating.
762 */
763static char_u *expr_line = NULL;
764
765/*
766 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
767 * Returns '=' when OK, NUL otherwise.
768 */
769 int
770get_expr_register()
771{
772 char_u *new_line;
773
774 new_line = getcmdline('=', 0L, 0);
775 if (new_line == NULL)
776 return NUL;
777 if (*new_line == NUL) /* use previous line */
778 vim_free(new_line);
779 else
780 set_expr_line(new_line);
781 return '=';
782}
783
784/*
785 * Set the expression for the '=' register.
786 * Argument must be an allocated string.
787 */
788 void
789set_expr_line(new_line)
790 char_u *new_line;
791{
792 vim_free(expr_line);
793 expr_line = new_line;
794}
795
796/*
797 * Get the result of the '=' register expression.
798 * Returns a pointer to allocated memory, or NULL for failure.
799 */
800 char_u *
801get_expr_line()
802{
803 char_u *expr_copy;
804 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000805 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
807 if (expr_line == NULL)
808 return NULL;
809
810 /* Make a copy of the expression, because evaluating it may cause it to be
811 * changed. */
812 expr_copy = vim_strsave(expr_line);
813 if (expr_copy == NULL)
814 return NULL;
815
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000816 /* When we are invoked recursively limit the evaluation to 10 levels.
817 * Then return the string as-is. */
818 if (nested >= 10)
819 return expr_copy;
820
821 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000822 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000823 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 vim_free(expr_copy);
825 return rv;
826}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000827
828/*
829 * Get the '=' register expression itself, without evaluating it.
830 */
831 char_u *
832get_expr_line_src()
833{
834 if (expr_line == NULL)
835 return NULL;
836 return vim_strsave(expr_line);
837}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838#endif /* FEAT_EVAL */
839
840/*
841 * Check if 'regname' is a valid name of a yank register.
842 * Note: There is no check for 0 (default register), caller should do this
843 */
844 int
845valid_yank_reg(regname, writing)
846 int regname;
847 int writing; /* if TRUE check for writable registers */
848{
849 if ( (regname > 0 && ASCII_ISALNUM(regname))
850 || (!writing && vim_strchr((char_u *)
851#ifdef FEAT_EVAL
852 "/.%#:="
853#else
854 "/.%#:"
855#endif
856 , regname) != NULL)
857 || regname == '"'
858 || regname == '-'
859 || regname == '_'
860#ifdef FEAT_CLIPBOARD
861 || regname == '*'
862 || regname == '+'
863#endif
864#ifdef FEAT_DND
865 || (!writing && regname == '~')
866#endif
867 )
868 return TRUE;
869 return FALSE;
870}
871
872/*
873 * Set y_current and y_append, according to the value of "regname".
874 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000875 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 *
877 * If regname is 0 and writing, use register 0
878 * If regname is 0 and reading, use previous register
879 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000880 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881get_yank_register(regname, writing)
882 int regname;
883 int writing;
884{
885 int i;
886
887 y_append = FALSE;
888 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
889 {
890 y_current = y_previous;
891 return;
892 }
893 i = regname;
894 if (VIM_ISDIGIT(i))
895 i -= '0';
896 else if (ASCII_ISLOWER(i))
897 i = CharOrdLow(i) + 10;
898 else if (ASCII_ISUPPER(i))
899 {
900 i = CharOrdUp(i) + 10;
901 y_append = TRUE;
902 }
903 else if (regname == '-')
904 i = DELETION_REGISTER;
905#ifdef FEAT_CLIPBOARD
906 /* When selection is not available, use register 0 instead of '*' */
907 else if (clip_star.available && regname == '*')
908 i = STAR_REGISTER;
909 /* When clipboard is not available, use register 0 instead of '+' */
910 else if (clip_plus.available && regname == '+')
911 i = PLUS_REGISTER;
912#endif
913#ifdef FEAT_DND
914 else if (!writing && regname == '~')
915 i = TILDE_REGISTER;
916#endif
917 else /* not 0-9, a-z, A-Z or '-': use register 0 */
918 i = 0;
919 y_current = &(y_regs[i]);
920 if (writing) /* remember the register we write into for do_put() */
921 y_previous = y_current;
922}
923
Bram Moolenaar8299df92004-07-10 09:47:34 +0000924#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925/*
926 * When "regname" is a clipboard register, obtain the selection. If it's not
927 * available return zero, otherwise return "regname".
928 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000929 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930may_get_selection(regname)
931 int regname;
932{
933 if (regname == '*')
934 {
935 if (!clip_star.available)
936 regname = 0;
937 else
938 clip_get_selection(&clip_star);
939 }
940 else if (regname == '+')
941 {
942 if (!clip_plus.available)
943 regname = 0;
944 else
945 clip_get_selection(&clip_plus);
946 }
947 return regname;
948}
949#endif
950
951#if defined(FEAT_VISUAL) || defined(PROTO)
952/*
953 * Obtain the contents of a "normal" register. The register is made empty.
954 * The returned pointer has allocated memory, use put_register() later.
955 */
956 void *
957get_register(name, copy)
958 int name;
959 int copy; /* make a copy, if FALSE make register empty. */
960{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000961 struct yankreg *reg;
962 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
964#ifdef FEAT_CLIPBOARD
965 /* When Visual area changed, may have to update selection. Obtain the
966 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000967 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200969 if (clip_isautosel_star())
970 clip_update_selection(&clip_star);
971 may_get_selection(name);
972 }
973 if (name == '+' && clip_plus.available)
974 {
975 if (clip_isautosel_plus())
976 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 may_get_selection(name);
978 }
979#endif
980
981 get_yank_register(name, 0);
982 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
983 if (reg != NULL)
984 {
985 *reg = *y_current;
986 if (copy)
987 {
988 /* If we run out of memory some or all of the lines are empty. */
989 if (reg->y_size == 0)
990 reg->y_array = NULL;
991 else
992 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
993 * reg->y_size));
994 if (reg->y_array != NULL)
995 {
996 for (i = 0; i < reg->y_size; ++i)
997 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
998 }
999 }
1000 else
1001 y_current->y_array = NULL;
1002 }
1003 return (void *)reg;
1004}
1005
1006/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001007 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 */
1009 void
1010put_register(name, reg)
1011 int name;
1012 void *reg;
1013{
1014 get_yank_register(name, 0);
1015 free_yank_all();
1016 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001017 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019# ifdef FEAT_CLIPBOARD
1020 /* Send text written to clipboard register to the clipboard. */
1021 may_set_selection();
1022# endif
1023}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001024
1025 void
1026free_register(reg)
1027 void *reg;
1028{
1029 struct yankreg tmp;
1030
1031 tmp = *y_current;
1032 *y_current = *(struct yankreg *)reg;
1033 free_yank_all();
1034 vim_free(reg);
1035 *y_current = tmp;
1036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037#endif
1038
1039#if defined(FEAT_MOUSE) || defined(PROTO)
1040/*
1041 * return TRUE if the current yank register has type MLINE
1042 */
1043 int
1044yank_register_mline(regname)
1045 int regname;
1046{
1047 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1048 return FALSE;
1049 if (regname == '_') /* black hole is always empty */
1050 return FALSE;
1051 get_yank_register(regname, FALSE);
1052 return (y_current->y_type == MLINE);
1053}
1054#endif
1055
1056/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001057 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001059 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 */
1061 int
1062do_record(c)
1063 int c;
1064{
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 char_u *p;
1066 static int regname;
1067 struct yankreg *old_y_previous, *old_y_current;
1068 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
1070 if (Recording == FALSE) /* start recording */
1071 {
1072 /* registers 0-9, a-z and " are allowed */
1073 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1074 retval = FAIL;
1075 else
1076 {
1077 Recording = TRUE;
1078 showmode();
1079 regname = c;
1080 retval = OK;
1081 }
1082 }
1083 else /* stop recording */
1084 {
1085 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001086 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1087 * needs to be removed again to put it in a register. exec_reg then
1088 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
1090 Recording = FALSE;
1091 MSG("");
1092 p = get_recorded();
1093 if (p == NULL)
1094 retval = FAIL;
1095 else
1096 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001097 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1098 vim_unescape_csi(p);
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 /*
1101 * We don't want to change the default register here, so save and
1102 * restore the current register name.
1103 */
1104 old_y_previous = y_previous;
1105 old_y_current = y_current;
1106
1107 retval = stuff_yank(regname, p);
1108
1109 y_previous = old_y_previous;
1110 y_current = old_y_current;
1111 }
1112 }
1113 return retval;
1114}
1115
1116/*
1117 * Stuff string "p" into yank register "regname" as a single line (append if
1118 * uppercase). "p" must have been alloced.
1119 *
1120 * return FAIL for failure, OK otherwise
1121 */
1122 static int
1123stuff_yank(regname, p)
1124 int regname;
1125 char_u *p;
1126{
1127 char_u *lp;
1128 char_u **pp;
1129
1130 /* check for read-only register */
1131 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1132 {
1133 vim_free(p);
1134 return FAIL;
1135 }
1136 if (regname == '_') /* black hole: don't do anything */
1137 {
1138 vim_free(p);
1139 return OK;
1140 }
1141 get_yank_register(regname, TRUE);
1142 if (y_append && y_current->y_array != NULL)
1143 {
1144 pp = &(y_current->y_array[y_current->y_size - 1]);
1145 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1146 if (lp == NULL)
1147 {
1148 vim_free(p);
1149 return FAIL;
1150 }
1151 STRCPY(lp, *pp);
1152 STRCAT(lp, p);
1153 vim_free(p);
1154 vim_free(*pp);
1155 *pp = lp;
1156 }
1157 else
1158 {
1159 free_yank_all();
1160 if ((y_current->y_array =
1161 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1162 {
1163 vim_free(p);
1164 return FAIL;
1165 }
1166 y_current->y_array[0] = p;
1167 y_current->y_size = 1;
1168 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1169 }
1170 return OK;
1171}
1172
Bram Moolenaar42b94362009-05-26 16:12:37 +00001173static int execreg_lastc = NUL;
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175/*
1176 * execute a yank register: copy it into the stuff buffer
1177 *
1178 * return FAIL for failure, OK otherwise
1179 */
1180 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001181do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 int regname;
1183 int colon; /* insert ':' before each line */
1184 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001185 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 long i;
1188 char_u *p;
1189 int retval = OK;
1190 int remap;
1191
1192 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001193 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001194 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001195 {
1196 EMSG(_("E748: No previously used register"));
1197 return FAIL;
1198 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001199 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 /* check for valid regname */
1202 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001203 {
1204 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001206 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001207 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
1209#ifdef FEAT_CLIPBOARD
1210 regname = may_get_selection(regname);
1211#endif
1212
1213 if (regname == '_') /* black hole: don't stuff anything */
1214 return OK;
1215
1216#ifdef FEAT_CMDHIST
1217 if (regname == ':') /* use last command line */
1218 {
1219 if (last_cmdline == NULL)
1220 {
1221 EMSG(_(e_nolastcmd));
1222 return FAIL;
1223 }
1224 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1225 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001226 /* Escape all control characters with a CTRL-V */
1227 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001228 (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 +00001229 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001230 {
1231 /* When in Visual mode "'<,'>" will be prepended to the command.
1232 * Remove it when it's already there. */
1233 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001234 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001235 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001236 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001237 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001238 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240#endif
1241#ifdef FEAT_EVAL
1242 else if (regname == '=')
1243 {
1244 p = get_expr_line();
1245 if (p == NULL)
1246 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001247 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 vim_free(p);
1249 }
1250#endif
1251 else if (regname == '.') /* use last inserted text */
1252 {
1253 p = get_last_insert_save();
1254 if (p == NULL)
1255 {
1256 EMSG(_(e_noinstext));
1257 return FAIL;
1258 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001259 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 vim_free(p);
1261 }
1262 else
1263 {
1264 get_yank_register(regname, FALSE);
1265 if (y_current->y_array == NULL)
1266 return FAIL;
1267
1268 /* Disallow remaping for ":@r". */
1269 remap = colon ? REMAP_NONE : REMAP_YES;
1270
1271 /*
1272 * Insert lines into typeahead buffer, from last one to first one.
1273 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001274 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 for (i = y_current->y_size; --i >= 0; )
1276 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001277 char_u *escaped;
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 /* insert NL between lines and after last line if type is MLINE */
1280 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1281 || addcr)
1282 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001283 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 return FAIL;
1285 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001286 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1287 if (escaped == NULL)
1288 return FAIL;
1289 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1290 vim_free(escaped);
1291 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001293 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 == FAIL)
1295 return FAIL;
1296 }
1297 Exec_reg = TRUE; /* disable the 'q' command */
1298 }
1299 return retval;
1300}
1301
1302/*
1303 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1304 * used only after other typeahead has been processed.
1305 */
1306 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001307put_reedit_in_typebuf(silent)
1308 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 char_u buf[3];
1311
1312 if (restart_edit != NUL)
1313 {
1314 if (restart_edit == 'V')
1315 {
1316 buf[0] = 'g';
1317 buf[1] = 'R';
1318 buf[2] = NUL;
1319 }
1320 else
1321 {
1322 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1323 buf[1] = NUL;
1324 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001325 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 restart_edit = NUL;
1327 }
1328}
1329
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001330/*
1331 * Insert register contents "s" into the typeahead buffer, so that it will be
1332 * executed again.
1333 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1334 * no remapping.
1335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001337put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001339 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001341 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 int retval = OK;
1344
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001345 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001347 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001349 {
1350 char_u *p;
1351
1352 if (esc)
1353 p = vim_strsave_escape_csi(s);
1354 else
1355 p = s;
1356 if (p == NULL)
1357 retval = FAIL;
1358 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001359 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1360 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001361 if (esc)
1362 vim_free(p);
1363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001365 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 return retval;
1367}
1368
1369/*
1370 * Insert a yank register: copy it into the Read buffer.
1371 * Used by CTRL-R command and middle mouse button in insert mode.
1372 *
1373 * return FAIL for failure, OK otherwise
1374 */
1375 int
1376insert_reg(regname, literally)
1377 int regname;
1378 int literally; /* insert literally, not as if typed */
1379{
1380 long i;
1381 int retval = OK;
1382 char_u *arg;
1383 int allocated;
1384
1385 /*
1386 * It is possible to get into an endless loop by having CTRL-R a in
1387 * register a and then, in insert mode, doing CTRL-R a.
1388 * If you hit CTRL-C, the loop will be broken here.
1389 */
1390 ui_breakcheck();
1391 if (got_int)
1392 return FAIL;
1393
1394 /* check for valid regname */
1395 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1396 return FAIL;
1397
1398#ifdef FEAT_CLIPBOARD
1399 regname = may_get_selection(regname);
1400#endif
1401
1402 if (regname == '.') /* insert last inserted text */
1403 retval = stuff_inserted(NUL, 1L, TRUE);
1404 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1405 {
1406 if (arg == NULL)
1407 return FAIL;
1408 stuffescaped(arg, literally);
1409 if (allocated)
1410 vim_free(arg);
1411 }
1412 else /* name or number register */
1413 {
1414 get_yank_register(regname, FALSE);
1415 if (y_current->y_array == NULL)
1416 retval = FAIL;
1417 else
1418 {
1419 for (i = 0; i < y_current->y_size; ++i)
1420 {
1421 stuffescaped(y_current->y_array[i], literally);
1422 /*
1423 * Insert a newline between lines and after last line if
1424 * y_type is MLINE.
1425 */
1426 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1427 stuffcharReadbuff('\n');
1428 }
1429 }
1430 }
1431
1432 return retval;
1433}
1434
1435/*
1436 * Stuff a string into the typeahead buffer, such that edit() will insert it
1437 * literally ("literally" TRUE) or interpret is as typed characters.
1438 */
1439 static void
1440stuffescaped(arg, literally)
1441 char_u *arg;
1442 int literally;
1443{
1444 int c;
1445 char_u *start;
1446
1447 while (*arg != NUL)
1448 {
1449 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1450 * stuff K_SPECIAL to get the effect of a special key when "literally"
1451 * is TRUE. */
1452 start = arg;
1453 while ((*arg >= ' '
1454#ifndef EBCDIC
1455 && *arg < DEL /* EBCDIC: chars above space are normal */
1456#endif
1457 )
1458 || (*arg == K_SPECIAL && !literally))
1459 ++arg;
1460 if (arg > start)
1461 stuffReadbuffLen(start, (long)(arg - start));
1462
1463 /* stuff a single special character */
1464 if (*arg != NUL)
1465 {
1466#ifdef FEAT_MBYTE
1467 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001468 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 else
1470#endif
1471 c = *arg++;
1472 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1473 stuffcharReadbuff(Ctrl_V);
1474 stuffcharReadbuff(c);
1475 }
1476 }
1477}
1478
1479/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001480 * If "regname" is a special register, return TRUE and store a pointer to its
1481 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001483 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484get_spec_reg(regname, argp, allocated, errmsg)
1485 int regname;
1486 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001487 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 int errmsg; /* give error message when failing */
1489{
1490 int cnt;
1491
1492 *argp = NULL;
1493 *allocated = FALSE;
1494 switch (regname)
1495 {
1496 case '%': /* file name */
1497 if (errmsg)
1498 check_fname(); /* will give emsg if not set */
1499 *argp = curbuf->b_fname;
1500 return TRUE;
1501
1502 case '#': /* alternate file name */
1503 *argp = getaltfname(errmsg); /* may give emsg if not set */
1504 return TRUE;
1505
1506#ifdef FEAT_EVAL
1507 case '=': /* result of expression */
1508 *argp = get_expr_line();
1509 *allocated = TRUE;
1510 return TRUE;
1511#endif
1512
1513 case ':': /* last command line */
1514 if (last_cmdline == NULL && errmsg)
1515 EMSG(_(e_nolastcmd));
1516 *argp = last_cmdline;
1517 return TRUE;
1518
1519 case '/': /* last search-pattern */
1520 if (last_search_pat() == NULL && errmsg)
1521 EMSG(_(e_noprevre));
1522 *argp = last_search_pat();
1523 return TRUE;
1524
1525 case '.': /* last inserted text */
1526 *argp = get_last_insert_save();
1527 *allocated = TRUE;
1528 if (*argp == NULL && errmsg)
1529 EMSG(_(e_noinstext));
1530 return TRUE;
1531
1532#ifdef FEAT_SEARCHPATH
1533 case Ctrl_F: /* Filename under cursor */
1534 case Ctrl_P: /* Path under cursor, expand via "path" */
1535 if (!errmsg)
1536 return FALSE;
1537 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001538 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 *allocated = TRUE;
1540 return TRUE;
1541#endif
1542
1543 case Ctrl_W: /* word under cursor */
1544 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1545 if (!errmsg)
1546 return FALSE;
1547 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1548 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1549 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1550 *allocated = TRUE;
1551 return TRUE;
1552
1553 case '_': /* black hole: always empty */
1554 *argp = (char_u *)"";
1555 return TRUE;
1556 }
1557
1558 return FALSE;
1559}
1560
1561/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001562 * Paste a yank register into the command line.
1563 * Only for non-special registers.
1564 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 * insert_reg() can't be used here, because special characters from the
1566 * register contents will be interpreted as commands.
1567 *
1568 * return FAIL for failure, OK otherwise
1569 */
1570 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001571cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 int regname;
1573 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001574 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
1576 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 get_yank_register(regname, FALSE);
1579 if (y_current->y_array == NULL)
1580 return FAIL;
1581
1582 for (i = 0; i < y_current->y_size; ++i)
1583 {
1584 cmdline_paste_str(y_current->y_array[i], literally);
1585
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001586 /* Insert ^M between lines and after last line if type is MLINE.
1587 * Don't do this when "remcr" is TRUE and the next line is empty. */
1588 if (y_current->y_type == MLINE
1589 || (i < y_current->y_size - 1
1590 && !(remcr
1591 && i == y_current->y_size - 2
1592 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 cmdline_paste_str((char_u *)"\r", literally);
1594
1595 /* Check for CTRL-C, in case someone tries to paste a few thousand
1596 * lines and gets bored. */
1597 ui_breakcheck();
1598 if (got_int)
1599 return FAIL;
1600 }
1601 return OK;
1602}
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1605/*
1606 * Adjust the register name pointed to with "rp" for the clipboard being
1607 * used always and the clipboard being available.
1608 */
1609 void
1610adjust_clip_reg(rp)
1611 int *rp;
1612{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001613 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1614 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
1615 if (*rp == 0 && clip_unnamed != 0)
1616 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
1617 ? '+' : '*';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (!clip_star.available && *rp == '*')
1619 *rp = 0;
1620 if (!clip_plus.available && *rp == '+')
1621 *rp = 0;
1622}
1623#endif
1624
1625/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001626 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001628 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 */
1630 int
1631op_delete(oap)
1632 oparg_T *oap;
1633{
1634 int n;
1635 linenr_T lnum;
1636 char_u *ptr;
1637#ifdef FEAT_VISUAL
1638 char_u *newp, *oldp;
1639 struct block_def bd;
1640#endif
1641 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1642 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001643 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1646 return OK;
1647
1648 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1649 if (oap->empty)
1650 return u_save_cursor();
1651
1652 if (!curbuf->b_p_ma)
1653 {
1654 EMSG(_(e_modifiable));
1655 return FAIL;
1656 }
1657
1658#ifdef FEAT_CLIPBOARD
1659 adjust_clip_reg(&oap->regname);
1660#endif
1661
1662#ifdef FEAT_MBYTE
1663 if (has_mbyte)
1664 mb_adjust_opend(oap);
1665#endif
1666
Bram Moolenaard04b7502010-07-08 22:27:55 +02001667 /*
1668 * Imitate the strange Vi behaviour: If the delete spans more than one
1669 * line and motion_type == MCHAR and the result is a blank line, make the
1670 * delete linewise. Don't do this for the change command or Visual mode.
1671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if ( oap->motion_type == MCHAR
1673#ifdef FEAT_VISUAL
1674 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001675 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676#endif
1677 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001678 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 && oap->op_type == OP_DELETE)
1680 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001681 ptr = ml_get(oap->end.lnum) + oap->end.col;
1682 if (*ptr != NUL)
1683 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ptr = skipwhite(ptr);
1685 if (*ptr == NUL && inindent(0))
1686 oap->motion_type = MLINE;
1687 }
1688
Bram Moolenaard04b7502010-07-08 22:27:55 +02001689 /*
1690 * Check for trying to delete (e.g. "D") in an empty line.
1691 * Note: For the change operator it is ok.
1692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if ( oap->motion_type == MCHAR
1694 && oap->line_count == 1
1695 && oap->op_type == OP_DELETE
1696 && *ml_get(oap->start.lnum) == NUL)
1697 {
1698 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001699 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 * 'cpoptions' (Vi compatible).
1701 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001702#ifdef FEAT_VIRTUALEDIT
1703 if (virtual_op)
1704 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1705 * marks as if it happened. */
1706 goto setmarks;
1707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1709 beep_flush();
1710 return OK;
1711 }
1712
Bram Moolenaard04b7502010-07-08 22:27:55 +02001713 /*
1714 * Do a yank of whatever we're about to delete.
1715 * If a yank register was specified, put the deleted text into that
1716 * register. For the black hole register '_' don't yank anything.
1717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (oap->regname != '_')
1719 {
1720 if (oap->regname != 0)
1721 {
1722 /* check for read-only register */
1723 if (!valid_yank_reg(oap->regname, TRUE))
1724 {
1725 beep_flush();
1726 return OK;
1727 }
1728 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1729 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1730 did_yank = TRUE;
1731 }
1732
1733 /*
1734 * Put deleted text into register 1 and shift number registers if the
1735 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001736 * Use the register name from before adjust_clip_reg() may have
1737 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001739 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 || oap->line_count > 1 || oap->use_reg_one)
1741 {
1742 y_current = &y_regs[9];
1743 free_yank_all(); /* free register nine */
1744 for (n = 9; n > 1; --n)
1745 y_regs[n] = y_regs[n - 1];
1746 y_previous = y_current = &y_regs[1];
1747 y_regs[1].y_array = NULL; /* set register one to empty */
1748 if (op_yank(oap, TRUE, FALSE) == OK)
1749 did_yank = TRUE;
1750 }
1751
Bram Moolenaar84298db2012-04-20 13:46:08 +02001752 /* Yank into small delete register when no named register specified
1753 * and the delete is within one line. */
1754 if ((
1755#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001756 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1757 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001758#endif
1759 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 && oap->line_count == 1)
1761 {
1762 oap->regname = '-';
1763 get_yank_register(oap->regname, TRUE);
1764 if (op_yank(oap, TRUE, FALSE) == OK)
1765 did_yank = TRUE;
1766 oap->regname = 0;
1767 }
1768
1769 /*
1770 * If there's too much stuff to fit in the yank register, then get a
1771 * confirmation before doing the delete. This is crude, but simple.
1772 * And it avoids doing a delete of something we can't put back if we
1773 * want.
1774 */
1775 if (!did_yank)
1776 {
1777 int msg_silent_save = msg_silent;
1778
1779 msg_silent = 0; /* must display the prompt */
1780 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1781 msg_silent = msg_silent_save;
1782 if (n != 'y')
1783 {
1784 EMSG(_(e_abort));
1785 return FAIL;
1786 }
1787 }
1788 }
1789
1790#ifdef FEAT_VISUAL
Bram Moolenaard04b7502010-07-08 22:27:55 +02001791 /*
1792 * block mode delete
1793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (oap->block_mode)
1795 {
1796 if (u_save((linenr_T)(oap->start.lnum - 1),
1797 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1798 return FAIL;
1799
1800 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1801 {
1802 block_prep(oap, &bd, lnum, TRUE);
1803 if (bd.textlen == 0) /* nothing to delete */
1804 continue;
1805
1806 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1807 if (lnum == curwin->w_cursor.lnum)
1808 {
1809 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1810# ifdef FEAT_VIRTUALEDIT
1811 curwin->w_cursor.coladd = 0;
1812# endif
1813 }
1814
1815 /* n == number of chars deleted
1816 * If we delete a TAB, it may be replaced by several characters.
1817 * Thus the number of characters may increase!
1818 */
1819 n = bd.textlen - bd.startspaces - bd.endspaces;
1820 oldp = ml_get(lnum);
1821 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1822 if (newp == NULL)
1823 continue;
1824 /* copy up to deleted part */
1825 mch_memmove(newp, oldp, (size_t)bd.textcol);
1826 /* insert spaces */
1827 copy_spaces(newp + bd.textcol,
1828 (size_t)(bd.startspaces + bd.endspaces));
1829 /* copy the part after the deleted part */
1830 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001831 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 /* replace the line */
1833 ml_replace(lnum, newp, FALSE);
1834 }
1835
1836 check_cursor_col();
1837 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1838 oap->end.lnum + 1, 0L);
1839 oap->line_count = 0; /* no lines deleted */
1840 }
1841 else
1842#endif
1843 if (oap->motion_type == MLINE)
1844 {
1845 if (oap->op_type == OP_CHANGE)
1846 {
1847 /* Delete the lines except the first one. Temporarily move the
1848 * cursor to the next line. Save the current line number, if the
1849 * last line is deleted it may be changed.
1850 */
1851 if (oap->line_count > 1)
1852 {
1853 lnum = curwin->w_cursor.lnum;
1854 ++curwin->w_cursor.lnum;
1855 del_lines((long)(oap->line_count - 1), TRUE);
1856 curwin->w_cursor.lnum = lnum;
1857 }
1858 if (u_save_cursor() == FAIL)
1859 return FAIL;
1860 if (curbuf->b_p_ai) /* don't delete indent */
1861 {
1862 beginline(BL_WHITE); /* cursor on first non-white */
1863 did_ai = TRUE; /* delete the indent when ESC hit */
1864 ai_col = curwin->w_cursor.col;
1865 }
1866 else
1867 beginline(0); /* cursor in column 0 */
1868 truncate_line(FALSE); /* delete the rest of the line */
1869 /* leave cursor past last char in line */
1870 if (oap->line_count > 1)
1871 u_clearline(); /* "U" command not possible after "2cc" */
1872 }
1873 else
1874 {
1875 del_lines(oap->line_count, TRUE);
1876 beginline(BL_WHITE | BL_FIX);
1877 u_clearline(); /* "U" command not possible after "dd" */
1878 }
1879 }
1880 else
1881 {
1882#ifdef FEAT_VIRTUALEDIT
1883 if (virtual_op)
1884 {
1885 int endcol = 0;
1886
1887 /* For virtualedit: break the tabs that are partly included. */
1888 if (gchar_pos(&oap->start) == '\t')
1889 {
1890 if (u_save_cursor() == FAIL) /* save first line for undo */
1891 return FAIL;
1892 if (oap->line_count == 1)
1893 endcol = getviscol2(oap->end.col, oap->end.coladd);
1894 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1895 oap->start = curwin->w_cursor;
1896 if (oap->line_count == 1)
1897 {
1898 coladvance(endcol);
1899 oap->end.col = curwin->w_cursor.col;
1900 oap->end.coladd = curwin->w_cursor.coladd;
1901 curwin->w_cursor = oap->start;
1902 }
1903 }
1904
1905 /* Break a tab only when it's included in the area. */
1906 if (gchar_pos(&oap->end) == '\t'
1907 && (int)oap->end.coladd < oap->inclusive)
1908 {
1909 /* save last line for undo */
1910 if (u_save((linenr_T)(oap->end.lnum - 1),
1911 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1912 return FAIL;
1913 curwin->w_cursor = oap->end;
1914 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1915 oap->end = curwin->w_cursor;
1916 curwin->w_cursor = oap->start;
1917 }
1918 }
1919#endif
1920
1921 if (oap->line_count == 1) /* delete characters within one line */
1922 {
1923 if (u_save_cursor() == FAIL) /* save line for undo */
1924 return FAIL;
1925
1926 /* if 'cpoptions' contains '$', display '$' at end of change */
1927 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1928 && oap->op_type == OP_CHANGE
1929 && oap->end.lnum == curwin->w_cursor.lnum
1930#ifdef FEAT_VISUAL
1931 && !oap->is_VIsual
1932#endif
1933 )
1934 display_dollar(oap->end.col - !oap->inclusive);
1935
1936 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1937
1938#ifdef FEAT_VIRTUALEDIT
1939 if (virtual_op)
1940 {
1941 /* fix up things for virtualedit-delete:
1942 * break the tabs which are going to get in our way
1943 */
1944 char_u *curline = ml_get_curline();
1945 int len = (int)STRLEN(curline);
1946
1947 if (oap->end.coladd != 0
1948 && (int)oap->end.col >= len - 1
1949 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1950 n++;
1951 /* Delete at least one char (e.g, when on a control char). */
1952 if (n == 0 && oap->start.coladd != oap->end.coladd)
1953 n = 1;
1954
1955 /* When deleted a char in the line, reset coladd. */
1956 if (gchar_cursor() != NUL)
1957 curwin->w_cursor.coladd = 0;
1958 }
1959#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001960 if (oap->op_type == OP_DELETE
1961 && oap->inclusive
1962 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001963 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1964 {
1965 /* Special case: gH<Del> deletes the last line. */
1966 del_lines(1L, FALSE);
1967 }
1968 else
1969 {
1970 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001971#ifdef FEAT_VISUAL
1972 && !oap->is_VIsual
1973#endif
1974 );
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 else /* delete characters between lines */
1978 {
1979 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001980 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982 /* save deleted and changed lines for undo */
1983 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1984 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1985 return FAIL;
1986
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001987 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 truncate_line(TRUE); /* delete from cursor to end of line */
1989
1990 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1991 ++curwin->w_cursor.lnum;
1992 del_lines((long)(oap->line_count - 2), FALSE);
1993
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001994 if (delete_last_line)
1995 oap->end.lnum = curbuf->b_ml.ml_line_count;
1996
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001997 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001998 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001999 && n > (int)STRLEN(ml_get(oap->end.lnum)))
2000 {
2001 /* Special case: gH<Del> deletes the last line. */
2002 del_lines(1L, FALSE);
2003 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01002004 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2005 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002006 }
2007 else
2008 {
2009 /* delete from start of line until op_end */
2010 curwin->w_cursor.col = 0;
2011 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002012#ifdef FEAT_VISUAL
2013 && !oap->is_VIsual
2014#endif
2015 );
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002016 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2017 }
2018 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar81340392012-06-06 16:12:59 +02002019 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 }
2021 }
2022
2023 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2024
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002025#ifdef FEAT_VIRTUALEDIT
2026setmarks:
2027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028#ifdef FEAT_VISUAL
2029 if (oap->block_mode)
2030 {
2031 curbuf->b_op_end.lnum = oap->end.lnum;
2032 curbuf->b_op_end.col = oap->start.col;
2033 }
2034 else
2035#endif
2036 curbuf->b_op_end = oap->start;
2037 curbuf->b_op_start = oap->start;
2038
2039 return OK;
2040}
2041
2042#ifdef FEAT_MBYTE
2043/*
2044 * Adjust end of operating area for ending on a multi-byte character.
2045 * Used for deletion.
2046 */
2047 static void
2048mb_adjust_opend(oap)
2049 oparg_T *oap;
2050{
2051 char_u *p;
2052
2053 if (oap->inclusive)
2054 {
2055 p = ml_get(oap->end.lnum);
2056 oap->end.col += mb_tail_off(p, p + oap->end.col);
2057 }
2058}
2059#endif
2060
2061#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2062/*
2063 * Replace a whole area with one character.
2064 */
2065 int
2066op_replace(oap, c)
2067 oparg_T *oap;
2068 int c;
2069{
2070 int n, numc;
2071#ifdef FEAT_MBYTE
2072 int num_chars;
2073#endif
2074 char_u *newp, *oldp;
2075 size_t oldlen;
2076 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002077 char_u *after_p = NULL;
2078 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2081 return OK; /* nothing to do */
2082
Bram Moolenaard9820532013-11-04 01:41:17 +01002083 if (had_ctrl_v_cr)
2084 c = (c == -1 ? '\r' : '\n');
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086#ifdef FEAT_MBYTE
2087 if (has_mbyte)
2088 mb_adjust_opend(oap);
2089#endif
2090
2091 if (u_save((linenr_T)(oap->start.lnum - 1),
2092 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2093 return FAIL;
2094
2095 /*
2096 * block mode replace
2097 */
2098 if (oap->block_mode)
2099 {
2100 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2101 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2102 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002103 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2105 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2106 continue; /* nothing to replace */
2107
2108 /* n == number of extra chars required
2109 * If we split a TAB, it may be replaced by several characters.
2110 * Thus the number of characters may increase!
2111 */
2112#ifdef FEAT_VIRTUALEDIT
2113 /* If the range starts in virtual space, count the initial
2114 * coladd offset as part of "startspaces" */
2115 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2116 {
2117 pos_T vpos;
2118
Bram Moolenaara1381de2009-11-03 15:44:21 +00002119 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 getvpos(&vpos, oap->start_vcol);
2121 bd.startspaces += vpos.coladd;
2122 n = bd.startspaces;
2123 }
2124 else
2125#endif
2126 /* allow for pre spaces */
2127 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2128
2129 /* allow for post spp */
2130 n += (bd.endspaces
2131#ifdef FEAT_VIRTUALEDIT
2132 && !bd.is_oneChar
2133#endif
2134 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2135 /* Figure out how many characters to replace. */
2136 numc = oap->end_vcol - oap->start_vcol + 1;
2137 if (bd.is_short && (!virtual_op || bd.is_MAX))
2138 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2139
2140#ifdef FEAT_MBYTE
2141 /* A double-wide character can be replaced only up to half the
2142 * times. */
2143 if ((*mb_char2cells)(c) > 1)
2144 {
2145 if ((numc & 1) && !bd.is_short)
2146 {
2147 ++bd.endspaces;
2148 ++n;
2149 }
2150 numc = numc / 2;
2151 }
2152
2153 /* Compute bytes needed, move character count to num_chars. */
2154 num_chars = numc;
2155 numc *= (*mb_char2len)(c);
2156#endif
2157 /* oldlen includes textlen, so don't double count */
2158 n += numc - bd.textlen;
2159
2160 oldp = ml_get_curline();
2161 oldlen = STRLEN(oldp);
2162 newp = alloc_check((unsigned)oldlen + 1 + n);
2163 if (newp == NULL)
2164 continue;
2165 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2166 /* copy up to deleted part */
2167 mch_memmove(newp, oldp, (size_t)bd.textcol);
2168 oldp += bd.textcol + bd.textlen;
2169 /* insert pre-spaces */
2170 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2171 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002172 /* -1/-2 is used for entering CR literally. */
2173 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002175#ifdef FEAT_MBYTE
2176 if (has_mbyte)
2177 {
2178 n = (int)STRLEN(newp);
2179 while (--num_chars >= 0)
2180 n += (*mb_char2bytes)(c, newp + n);
2181 }
2182 else
2183#endif
2184 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2185 if (!bd.is_short)
2186 {
2187 /* insert post-spaces */
2188 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2189 /* copy the part after the changed part */
2190 STRMOVE(newp + STRLEN(newp), oldp);
2191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 }
2193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002195 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002196 after_p = alloc_check(
2197 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002198 if (after_p != NULL)
2199 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201 /* replace the line */
2202 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002203 if (after_p != NULL)
2204 {
2205 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2206 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2207 oap->end.lnum++;
2208 vim_free(after_p);
2209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211 }
2212 else
2213 {
2214 /*
2215 * MCHAR and MLINE motion replace.
2216 */
2217 if (oap->motion_type == MLINE)
2218 {
2219 oap->start.col = 0;
2220 curwin->w_cursor.col = 0;
2221 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2222 if (oap->end.col)
2223 --oap->end.col;
2224 }
2225 else if (!oap->inclusive)
2226 dec(&(oap->end));
2227
2228 while (ltoreq(curwin->w_cursor, oap->end))
2229 {
2230 n = gchar_cursor();
2231 if (n != NUL)
2232 {
2233#ifdef FEAT_MBYTE
2234 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2235 {
2236 /* This is slow, but it handles replacing a single-byte
2237 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002238 if (curwin->w_cursor.lnum == oap->end.lnum)
2239 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 n = State;
2241 State = REPLACE;
2242 ins_char(c);
2243 State = n;
2244 /* Backup to the replaced character. */
2245 dec_cursor();
2246 }
2247 else
2248#endif
2249 {
2250#ifdef FEAT_VIRTUALEDIT
2251 if (n == TAB)
2252 {
2253 int end_vcol = 0;
2254
2255 if (curwin->w_cursor.lnum == oap->end.lnum)
2256 {
2257 /* oap->end has to be recalculated when
2258 * the tab breaks */
2259 end_vcol = getviscol2(oap->end.col,
2260 oap->end.coladd);
2261 }
2262 coladvance_force(getviscol());
2263 if (curwin->w_cursor.lnum == oap->end.lnum)
2264 getvpos(&oap->end, end_vcol);
2265 }
2266#endif
2267 pchar(curwin->w_cursor, c);
2268 }
2269 }
2270#ifdef FEAT_VIRTUALEDIT
2271 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2272 {
2273 int virtcols = oap->end.coladd;
2274
2275 if (curwin->w_cursor.lnum == oap->start.lnum
2276 && oap->start.col == oap->end.col && oap->start.coladd)
2277 virtcols -= oap->start.coladd;
2278
2279 /* oap->end has been trimmed so it's effectively inclusive;
2280 * as a result an extra +1 must be counted so we don't
2281 * trample the NUL byte. */
2282 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2283 curwin->w_cursor.col -= (virtcols + 1);
2284 for (; virtcols >= 0; virtcols--)
2285 {
2286 pchar(curwin->w_cursor, c);
2287 if (inc(&curwin->w_cursor) == -1)
2288 break;
2289 }
2290 }
2291#endif
2292
2293 /* Advance to next character, stop at the end of the file. */
2294 if (inc_cursor() == -1)
2295 break;
2296 }
2297 }
2298
2299 curwin->w_cursor = oap->start;
2300 check_cursor();
2301 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2302
2303 /* Set "'[" and "']" marks. */
2304 curbuf->b_op_start = oap->start;
2305 curbuf->b_op_end = oap->end;
2306
2307 return OK;
2308}
2309#endif
2310
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002311static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313/*
2314 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2315 */
2316 void
2317op_tilde(oap)
2318 oparg_T *oap;
2319{
2320 pos_T pos;
2321#ifdef FEAT_VISUAL
2322 struct block_def bd;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002323#endif
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002324 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 if (u_save((linenr_T)(oap->start.lnum - 1),
2327 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2328 return;
2329
2330 pos = oap->start;
2331#ifdef FEAT_VISUAL
2332 if (oap->block_mode) /* Visual block mode */
2333 {
2334 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2335 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002336 int one_change;
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 block_prep(oap, &bd, pos.lnum, FALSE);
2339 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002340 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2341 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002344 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2347
Bram Moolenaar009b2592004-10-24 19:18:58 +00002348 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2349 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002351 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353# endif
2354 }
2355 if (did_change)
2356 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2357 }
2358 else /* not block mode */
2359#endif
2360 {
2361 if (oap->motion_type == MLINE)
2362 {
2363 oap->start.col = 0;
2364 pos.col = 0;
2365 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2366 if (oap->end.col)
2367 --oap->end.col;
2368 }
2369 else if (!oap->inclusive)
2370 dec(&(oap->end));
2371
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002372 if (pos.lnum == oap->end.lnum)
2373 did_change = swapchars(oap->op_type, &pos,
2374 oap->end.col - pos.col + 1);
2375 else
2376 for (;;)
2377 {
2378 did_change |= swapchars(oap->op_type, &pos,
2379 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2380 (int)STRLEN(ml_get_pos(&pos)));
2381 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2382 break;
2383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (did_change)
2385 {
2386 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2387 0L);
2388#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002389 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
2391 char_u *ptr;
2392 int count;
2393
2394 pos = oap->start;
2395 while (pos.lnum < oap->end.lnum)
2396 {
2397 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002398 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002399 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002401 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 pos.col = 0;
2403 pos.lnum++;
2404 }
2405 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2406 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002407 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002409 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411#endif
2412 }
2413 }
2414
2415#ifdef FEAT_VISUAL
2416 if (!did_change && oap->is_VIsual)
2417 /* No change: need to remove the Visual selection */
2418 redraw_curbuf_later(INVERTED);
2419#endif
2420
2421 /*
2422 * Set '[ and '] marks.
2423 */
2424 curbuf->b_op_start = oap->start;
2425 curbuf->b_op_end = oap->end;
2426
2427 if (oap->line_count > p_report)
2428 {
2429 if (oap->line_count == 1)
2430 MSG(_("1 line changed"));
2431 else
2432 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2433 }
2434}
2435
2436/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002437 * Invoke swapchar() on "length" bytes at position "pos".
2438 * "pos" is advanced to just after the changed characters.
2439 * "length" is rounded up to include the whole last multi-byte character.
2440 * Also works correctly when the number of bytes changes.
2441 * Returns TRUE if some character was changed.
2442 */
2443 static int
2444swapchars(op_type, pos, length)
2445 int op_type;
2446 pos_T *pos;
2447 int length;
2448{
2449 int todo;
2450 int did_change = 0;
2451
2452 for (todo = length; todo > 0; --todo)
2453 {
2454# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002455 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002456 {
2457 int len = (*mb_ptr2len)(ml_get_pos(pos));
2458
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002459 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002460 if (len > 0)
2461 todo -= len - 1;
2462 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002463# endif
2464 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002465 if (inc(pos) == -1) /* at end of file */
2466 break;
2467 }
2468 return did_change;
2469}
2470
2471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 * If op_type == OP_UPPER: make uppercase,
2473 * if op_type == OP_LOWER: make lowercase,
2474 * if op_type == OP_ROT13: do rot13 encoding,
2475 * else swap case of character at 'pos'
2476 * returns TRUE when something actually changed.
2477 */
2478 int
2479swapchar(op_type, pos)
2480 int op_type;
2481 pos_T *pos;
2482{
2483 int c;
2484 int nc;
2485
2486 c = gchar_pos(pos);
2487
2488 /* Only do rot13 encoding for ASCII characters. */
2489 if (c >= 0x80 && op_type == OP_ROT13)
2490 return FALSE;
2491
2492#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002493 if (op_type == OP_UPPER && c == 0xdf
2494 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002495 {
2496 pos_T sp = curwin->w_cursor;
2497
2498 /* Special handling of German sharp s: change to "SS". */
2499 curwin->w_cursor = *pos;
2500 del_char(FALSE);
2501 ins_char('S');
2502 ins_char('S');
2503 curwin->w_cursor = sp;
2504 inc(pos);
2505 }
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2508 return FALSE;
2509#endif
2510 nc = c;
2511 if (MB_ISLOWER(c))
2512 {
2513 if (op_type == OP_ROT13)
2514 nc = ROT13(c, 'a');
2515 else if (op_type != OP_LOWER)
2516 nc = MB_TOUPPER(c);
2517 }
2518 else if (MB_ISUPPER(c))
2519 {
2520 if (op_type == OP_ROT13)
2521 nc = ROT13(c, 'A');
2522 else if (op_type != OP_UPPER)
2523 nc = MB_TOLOWER(c);
2524 }
2525 if (nc != c)
2526 {
2527#ifdef FEAT_MBYTE
2528 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2529 {
2530 pos_T sp = curwin->w_cursor;
2531
2532 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002533 /* don't use del_char(), it also removes composing chars */
2534 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 ins_char(nc);
2536 curwin->w_cursor = sp;
2537 }
2538 else
2539#endif
2540 pchar(*pos, nc);
2541 return TRUE;
2542 }
2543 return FALSE;
2544}
2545
2546#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2547/*
2548 * op_insert - Insert and append operators for Visual mode.
2549 */
2550 void
2551op_insert(oap, count1)
2552 oparg_T *oap;
2553 long count1;
2554{
2555 long ins_len, pre_textlen = 0;
2556 char_u *firstline, *ins_text;
2557 struct block_def bd;
2558 int i;
2559
2560 /* edit() changes this - record it for OP_APPEND */
2561 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2562
2563 /* vis block is still marked. Get rid of it now. */
2564 curwin->w_cursor.lnum = oap->start.lnum;
2565 update_screen(INVERTED);
2566
2567 if (oap->block_mode)
2568 {
2569#ifdef FEAT_VIRTUALEDIT
2570 /* When 'virtualedit' is used, need to insert the extra spaces before
2571 * doing block_prep(). When only "block" is used, virtual edit is
2572 * already disabled, but still need it when calling
2573 * coladvance_force(). */
2574 if (curwin->w_cursor.coladd > 0)
2575 {
2576 int old_ve_flags = ve_flags;
2577
2578 ve_flags = VE_ALL;
2579 if (u_save_cursor() == FAIL)
2580 return;
2581 coladvance_force(oap->op_type == OP_APPEND
2582 ? oap->end_vcol + 1 : getviscol());
2583 if (oap->op_type == OP_APPEND)
2584 --curwin->w_cursor.col;
2585 ve_flags = old_ve_flags;
2586 }
2587#endif
2588 /* Get the info about the block before entering the text */
2589 block_prep(oap, &bd, oap->start.lnum, TRUE);
2590 firstline = ml_get(oap->start.lnum) + bd.textcol;
2591 if (oap->op_type == OP_APPEND)
2592 firstline += bd.textlen;
2593 pre_textlen = (long)STRLEN(firstline);
2594 }
2595
2596 if (oap->op_type == OP_APPEND)
2597 {
2598 if (oap->block_mode
2599#ifdef FEAT_VIRTUALEDIT
2600 && curwin->w_cursor.coladd == 0
2601#endif
2602 )
2603 {
2604 /* Move the cursor to the character right of the block. */
2605 curwin->w_set_curswant = TRUE;
2606 while (*ml_get_cursor() != NUL
2607 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2608 ++curwin->w_cursor.col;
2609 if (bd.is_short && !bd.is_MAX)
2610 {
2611 /* First line was too short, make it longer and adjust the
2612 * values in "bd". */
2613 if (u_save_cursor() == FAIL)
2614 return;
2615 for (i = 0; i < bd.endspaces; ++i)
2616 ins_char(' ');
2617 bd.textlen += bd.endspaces;
2618 }
2619 }
2620 else
2621 {
2622 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002623 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 /* Works just like an 'i'nsert on the next character. */
2626 if (!lineempty(curwin->w_cursor.lnum)
2627 && oap->start_vcol != oap->end_vcol)
2628 inc_cursor();
2629 }
2630 }
2631
2632 edit(NUL, FALSE, (linenr_T)count1);
2633
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002634 /* If user has moved off this line, we don't know what to do, so do
2635 * nothing.
2636 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2637 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 return;
2639
2640 if (oap->block_mode)
2641 {
2642 struct block_def bd2;
2643
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002644 /* The user may have moved the cursor before inserting something, try
2645 * to adjust the block for that. */
Bram Moolenaarcafaa8a2013-11-21 14:40:04 +01002646 if (oap->start.lnum == curbuf->b_op_start.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002647 {
2648 if (oap->op_type == OP_INSERT
2649 && oap->start.col != curbuf->b_op_start.col)
2650 {
2651 oap->start.col = curbuf->b_op_start.col;
2652 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2653 - oap->start_vcol;
2654 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2655 }
2656 else if (oap->op_type == OP_APPEND
2657 && oap->end.col >= curbuf->b_op_start.col)
2658 {
2659 oap->start.col = curbuf->b_op_start.col;
2660 /* reset pre_textlen to the value of OP_INSERT */
2661 pre_textlen += bd.textlen;
2662 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2663 - oap->start_vcol;
2664 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2665 oap->op_type = OP_INSERT;
2666 }
2667 }
2668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 /*
2670 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002671 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 * Don't do this when "$" used, end-of-line will have changed.
2673 */
2674 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2675 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2676 {
2677 if (oap->op_type == OP_APPEND)
2678 {
2679 pre_textlen += bd2.textlen - bd.textlen;
2680 if (bd2.endspaces)
2681 --bd2.textlen;
2682 }
2683 bd.textcol = bd2.textcol;
2684 bd.textlen = bd2.textlen;
2685 }
2686
2687 /*
2688 * Subsequent calls to ml_get() flush the firstline data - take a
2689 * copy of the required string.
2690 */
2691 firstline = ml_get(oap->start.lnum) + bd.textcol;
2692 if (oap->op_type == OP_APPEND)
2693 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002694 if (pre_textlen >= 0
2695 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 ins_text = vim_strnsave(firstline, (int)ins_len);
2698 if (ins_text != NULL)
2699 {
2700 /* block handled here */
2701 if (u_save(oap->start.lnum,
2702 (linenr_T)(oap->end.lnum + 1)) == OK)
2703 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2704 &bd);
2705
2706 curwin->w_cursor.col = oap->start.col;
2707 check_cursor();
2708 vim_free(ins_text);
2709 }
2710 }
2711 }
2712}
2713#endif
2714
2715/*
2716 * op_change - handle a change operation
2717 *
2718 * return TRUE if edit() returns because of a CTRL-O command
2719 */
2720 int
2721op_change(oap)
2722 oparg_T *oap;
2723{
2724 colnr_T l;
2725 int retval;
2726#ifdef FEAT_VISUALEXTRA
2727 long offset;
2728 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002729 long ins_len;
2730 long pre_textlen = 0;
2731 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 char_u *firstline;
2733 char_u *ins_text, *newp, *oldp;
2734 struct block_def bd;
2735#endif
2736
2737 l = oap->start.col;
2738 if (oap->motion_type == MLINE)
2739 {
2740 l = 0;
2741#ifdef FEAT_SMARTINDENT
2742 if (!p_paste && curbuf->b_p_si
2743# ifdef FEAT_CINDENT
2744 && !curbuf->b_p_cin
2745# endif
2746 )
2747 can_si = TRUE; /* It's like opening a new line, do si */
2748#endif
2749 }
2750
2751 /* First delete the text in the region. In an empty buffer only need to
2752 * save for undo */
2753 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2754 {
2755 if (u_save_cursor() == FAIL)
2756 return FALSE;
2757 }
2758 else if (op_delete(oap) == FAIL)
2759 return FALSE;
2760
2761 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2762 && !virtual_op)
2763 inc_cursor();
2764
2765#ifdef FEAT_VISUALEXTRA
2766 /* check for still on same line (<CR> in inserted text meaningless) */
2767 /* skip blank lines too */
2768 if (oap->block_mode)
2769 {
2770# ifdef FEAT_VIRTUALEDIT
2771 /* Add spaces before getting the current line length. */
2772 if (virtual_op && (curwin->w_cursor.coladd > 0
2773 || gchar_cursor() == NUL))
2774 coladvance_force(getviscol());
2775# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002776 firstline = ml_get(oap->start.lnum);
2777 pre_textlen = (long)STRLEN(firstline);
2778 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 bd.textcol = curwin->w_cursor.col;
2780 }
2781#endif
2782
2783#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2784 if (oap->motion_type == MLINE)
2785 fix_indent();
2786#endif
2787
2788 retval = edit(NUL, FALSE, (linenr_T)1);
2789
2790#ifdef FEAT_VISUALEXTRA
2791 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002792 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002794 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002796 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002798 /* Auto-indenting may have changed the indent. If the cursor was past
2799 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002801 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002803 long new_indent = (long)(skipwhite(firstline) - firstline);
2804
2805 pre_textlen += new_indent - pre_indent;
2806 bd.textcol += new_indent - pre_indent;
2807 }
2808
2809 ins_len = (long)STRLEN(firstline) - pre_textlen;
2810 if (ins_len > 0)
2811 {
2812 /* Subsequent calls to ml_get() flush the firstline data - take a
2813 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2815 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002816 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2818 linenr++)
2819 {
2820 block_prep(oap, &bd, linenr, TRUE);
2821 if (!bd.is_short || virtual_op)
2822 {
2823# ifdef FEAT_VIRTUALEDIT
2824 pos_T vpos;
2825
2826 /* If the block starts in virtual space, count the
2827 * initial coladd offset as part of "startspaces" */
2828 if (bd.is_short)
2829 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002830 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833 else
2834 vpos.coladd = 0;
2835# endif
2836 oldp = ml_get(linenr);
2837 newp = alloc_check((unsigned)(STRLEN(oldp)
2838# ifdef FEAT_VIRTUALEDIT
2839 + vpos.coladd
2840# endif
2841 + ins_len + 1));
2842 if (newp == NULL)
2843 continue;
2844 /* copy up to block start */
2845 mch_memmove(newp, oldp, (size_t)bd.textcol);
2846 offset = bd.textcol;
2847# ifdef FEAT_VIRTUALEDIT
2848 copy_spaces(newp + offset, (size_t)vpos.coladd);
2849 offset += vpos.coladd;
2850# endif
2851 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2852 offset += ins_len;
2853 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002854 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 ml_replace(linenr, newp, FALSE);
2856 }
2857 }
2858 check_cursor();
2859
2860 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2861 }
2862 vim_free(ins_text);
2863 }
2864 }
2865#endif
2866
2867 return retval;
2868}
2869
2870/*
2871 * set all the yank registers to empty (called from main())
2872 */
2873 void
2874init_yank()
2875{
2876 int i;
2877
2878 for (i = 0; i < NUM_REGISTERS; ++i)
2879 y_regs[i].y_array = NULL;
2880}
2881
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002882#if defined(EXITFREE) || defined(PROTO)
2883 void
2884clear_registers()
2885{
2886 int i;
2887
2888 for (i = 0; i < NUM_REGISTERS; ++i)
2889 {
2890 y_current = &y_regs[i];
2891 if (y_current->y_array != NULL)
2892 free_yank_all();
2893 }
2894}
2895#endif
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897/*
2898 * Free "n" lines from the current yank register.
2899 * Called for normal freeing and in case of error.
2900 */
2901 static void
2902free_yank(n)
2903 long n;
2904{
2905 if (y_current->y_array != NULL)
2906 {
2907 long i;
2908
2909 for (i = n; --i >= 0; )
2910 {
2911#ifdef AMIGA /* only for very slow machines */
2912 if ((i & 1023) == 1023) /* this may take a while */
2913 {
2914 /*
2915 * This message should never cause a hit-return message.
2916 * Overwrite this message with any next message.
2917 */
2918 ++no_wait_return;
2919 smsg((char_u *)_("freeing %ld lines"), i + 1);
2920 --no_wait_return;
2921 msg_didout = FALSE;
2922 msg_col = 0;
2923 }
2924#endif
2925 vim_free(y_current->y_array[i]);
2926 }
2927 vim_free(y_current->y_array);
2928 y_current->y_array = NULL;
2929#ifdef AMIGA
2930 if (n >= 1000)
2931 MSG("");
2932#endif
2933 }
2934}
2935
2936 static void
2937free_yank_all()
2938{
2939 free_yank(y_current->y_size);
2940}
2941
2942/*
2943 * Yank the text between "oap->start" and "oap->end" into a yank register.
2944 * If we are to append (uppercase register), we first yank into a new yank
2945 * register and then concatenate the old and the new one (so we keep the old
2946 * one in case of out-of-memory).
2947 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002948 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
2950 int
2951op_yank(oap, deleting, mess)
2952 oparg_T *oap;
2953 int deleting;
2954 int mess;
2955{
2956 long y_idx; /* index in y_array[] */
2957 struct yankreg *curr; /* copy of y_current */
2958 struct yankreg newreg; /* new yank register when appending */
2959 char_u **new_ptr;
2960 linenr_T lnum; /* current line number */
2961 long j;
2962 int yanktype = oap->motion_type;
2963 long yanklines = oap->line_count;
2964 linenr_T yankendlnum = oap->end.lnum;
2965 char_u *p;
2966 char_u *pnew;
2967 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002968#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002969 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 /* check for read-only register */
2973 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2974 {
2975 beep_flush();
2976 return FAIL;
2977 }
2978 if (oap->regname == '_') /* black hole: nothing to do */
2979 return OK;
2980
2981#ifdef FEAT_CLIPBOARD
2982 if (!clip_star.available && oap->regname == '*')
2983 oap->regname = 0;
2984 else if (!clip_plus.available && oap->regname == '+')
2985 oap->regname = 0;
2986#endif
2987
2988 if (!deleting) /* op_delete() already set y_current */
2989 get_yank_register(oap->regname, TRUE);
2990
2991 curr = y_current;
2992 /* append to existing contents */
2993 if (y_append && y_current->y_array != NULL)
2994 y_current = &newreg;
2995 else
2996 free_yank_all(); /* free previously yanked lines */
2997
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002998 /*
2999 * If the cursor was in column 1 before and after the movement, and the
3000 * operator is not inclusive, the yank is always linewise.
3001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if ( oap->motion_type == MCHAR
3003 && oap->start.col == 0
3004 && !oap->inclusive
3005#ifdef FEAT_VISUAL
3006 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003007 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#endif
3009 && oap->end.col == 0
3010 && yanklines > 1)
3011 {
3012 yanktype = MLINE;
3013 --yankendlnum;
3014 --yanklines;
3015 }
3016
3017 y_current->y_size = yanklines;
3018 y_current->y_type = yanktype; /* set the yank register type */
3019#ifdef FEAT_VISUAL
3020 y_current->y_width = 0;
3021#endif
3022 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3023 yanklines), TRUE);
3024
3025 if (y_current->y_array == NULL)
3026 {
3027 y_current = curr;
3028 return FAIL;
3029 }
3030
3031 y_idx = 0;
3032 lnum = oap->start.lnum;
3033
3034#ifdef FEAT_VISUAL
3035 if (oap->block_mode)
3036 {
3037 /* Visual block mode */
3038 y_current->y_type = MBLOCK; /* set the yank register type */
3039 y_current->y_width = oap->end_vcol - oap->start_vcol;
3040
3041 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3042 y_current->y_width--;
3043 }
3044#endif
3045
3046 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3047 {
3048 switch (y_current->y_type)
3049 {
3050#ifdef FEAT_VISUAL
3051 case MBLOCK:
3052 block_prep(oap, &bd, lnum, FALSE);
3053 if (yank_copy_line(&bd, y_idx) == FAIL)
3054 goto fail;
3055 break;
3056#endif
3057
3058 case MLINE:
3059 if ((y_current->y_array[y_idx] =
3060 vim_strsave(ml_get(lnum))) == NULL)
3061 goto fail;
3062 break;
3063
3064 case MCHAR:
3065 {
3066 colnr_T startcol = 0, endcol = MAXCOL;
3067#ifdef FEAT_VIRTUALEDIT
3068 int is_oneChar = FALSE;
3069 colnr_T cs, ce;
3070#endif
3071 p = ml_get(lnum);
3072 bd.startspaces = 0;
3073 bd.endspaces = 0;
3074
3075 if (lnum == oap->start.lnum)
3076 {
3077 startcol = oap->start.col;
3078#ifdef FEAT_VIRTUALEDIT
3079 if (virtual_op)
3080 {
3081 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3082 if (ce != cs && oap->start.coladd > 0)
3083 {
3084 /* Part of a tab selected -- but don't
3085 * double-count it. */
3086 bd.startspaces = (ce - cs + 1)
3087 - oap->start.coladd;
3088 startcol++;
3089 }
3090 }
3091#endif
3092 }
3093
3094 if (lnum == oap->end.lnum)
3095 {
3096 endcol = oap->end.col;
3097#ifdef FEAT_VIRTUALEDIT
3098 if (virtual_op)
3099 {
3100 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3101 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3102# ifdef FEAT_MBYTE
3103 /* Don't add space for double-wide
3104 * char; endcol will be on last byte
3105 * of multi-byte char. */
3106 && (*mb_head_off)(p, p + endcol) == 0
3107# endif
3108 ))
3109 {
3110 if (oap->start.lnum == oap->end.lnum
3111 && oap->start.col == oap->end.col)
3112 {
3113 /* Special case: inside a single char */
3114 is_oneChar = TRUE;
3115 bd.startspaces = oap->end.coladd
3116 - oap->start.coladd + oap->inclusive;
3117 endcol = startcol;
3118 }
3119 else
3120 {
3121 bd.endspaces = oap->end.coladd
3122 + oap->inclusive;
3123 endcol -= oap->inclusive;
3124 }
3125 }
3126 }
3127#endif
3128 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003129 if (endcol == MAXCOL)
3130 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (startcol > endcol
3132#ifdef FEAT_VIRTUALEDIT
3133 || is_oneChar
3134#endif
3135 )
3136 bd.textlen = 0;
3137 else
3138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 bd.textlen = endcol - startcol + oap->inclusive;
3140 }
3141 bd.textstart = p + startcol;
3142 if (yank_copy_line(&bd, y_idx) == FAIL)
3143 goto fail;
3144 break;
3145 }
3146 /* NOTREACHED */
3147 }
3148 }
3149
3150 if (curr != y_current) /* append the new block to the old block */
3151 {
3152 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3153 (curr->y_size + y_current->y_size)), TRUE);
3154 if (new_ptr == NULL)
3155 goto fail;
3156 for (j = 0; j < curr->y_size; ++j)
3157 new_ptr[j] = curr->y_array[j];
3158 vim_free(curr->y_array);
3159 curr->y_array = new_ptr;
3160
3161 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3162 curr->y_type = MLINE;
3163
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003164 /* Concatenate the last line of the old block with the first line of
3165 * the new block, unless being Vi compatible. */
3166 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3169 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3170 if (pnew == NULL)
3171 {
3172 y_idx = y_current->y_size - 1;
3173 goto fail;
3174 }
3175 STRCPY(pnew, curr->y_array[--j]);
3176 STRCAT(pnew, y_current->y_array[0]);
3177 vim_free(curr->y_array[j]);
3178 vim_free(y_current->y_array[0]);
3179 curr->y_array[j++] = pnew;
3180 y_idx = 1;
3181 }
3182 else
3183 y_idx = 0;
3184 while (y_idx < y_current->y_size)
3185 curr->y_array[j++] = y_current->y_array[y_idx++];
3186 curr->y_size = j;
3187 vim_free(y_current->y_array);
3188 y_current = curr;
3189 }
3190 if (mess) /* Display message about yank? */
3191 {
3192 if (yanktype == MCHAR
3193#ifdef FEAT_VISUAL
3194 && !oap->block_mode
3195#endif
3196 && yanklines == 1)
3197 yanklines = 0;
3198 /* Some versions of Vi use ">=" here, some don't... */
3199 if (yanklines > p_report)
3200 {
3201 /* redisplay now, so message is not deleted */
3202 update_topline_redraw();
3203 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003204 {
3205#ifdef FEAT_VISUAL
3206 if (oap->block_mode)
3207 MSG(_("block of 1 line yanked"));
3208 else
3209#endif
3210 MSG(_("1 line yanked"));
3211 }
3212#ifdef FEAT_VISUAL
3213 else if (oap->block_mode)
3214 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 else
3217 smsg((char_u *)_("%ld lines yanked"), yanklines);
3218 }
3219 }
3220
3221 /*
3222 * Set "'[" and "']" marks.
3223 */
3224 curbuf->b_op_start = oap->start;
3225 curbuf->b_op_end = oap->end;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003226 if (yanktype == MLINE
3227#ifdef FEAT_VISUAL
3228 && !oap->block_mode
3229#endif
3230 )
3231 {
3232 curbuf->b_op_start.col = 0;
3233 curbuf->b_op_end.col = MAXCOL;
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236#ifdef FEAT_CLIPBOARD
3237 /*
3238 * If we were yanking to the '*' register, send result to clipboard.
3239 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3240 * to the '*' register.
3241 */
3242 if (clip_star.available
3243 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003244 || (!deleting && oap->regname == 0
3245 && (clip_unnamed & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 if (curr != &(y_regs[STAR_REGISTER]))
3248 /* Copy the text from register 0 to the clipboard register. */
3249 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3250
3251 clip_own_selection(&clip_star);
3252 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003253# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003254 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257
3258# ifdef FEAT_X11
3259 /*
3260 * If we were yanking to the '+' register, send result to selection.
3261 * Also copy to the '*' register, in case auto-select is off.
3262 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003263 if (clip_plus.available
3264 && (curr == &(y_regs[PLUS_REGISTER])
3265 || (!deleting && oap->regname == 0
3266 && (clip_unnamed & CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003268 if (curr != &(y_regs[PLUS_REGISTER]))
3269 /* Copy the text from register 0 to the clipboard register. */
3270 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 clip_own_selection(&clip_plus);
3273 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003274 if (!clip_isautosel_star() && !did_star
3275 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
3277 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3278 clip_own_selection(&clip_star);
3279 clip_gen_set_selection(&clip_star);
3280 }
3281 }
3282# endif
3283#endif
3284
3285 return OK;
3286
3287fail: /* free the allocated lines */
3288 free_yank(y_idx + 1);
3289 y_current = curr;
3290 return FAIL;
3291}
3292
3293 static int
3294yank_copy_line(bd, y_idx)
3295 struct block_def *bd;
3296 long y_idx;
3297{
3298 char_u *pnew;
3299
3300 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3301 == NULL)
3302 return FAIL;
3303 y_current->y_array[y_idx] = pnew;
3304 copy_spaces(pnew, (size_t)bd->startspaces);
3305 pnew += bd->startspaces;
3306 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3307 pnew += bd->textlen;
3308 copy_spaces(pnew, (size_t)bd->endspaces);
3309 pnew += bd->endspaces;
3310 *pnew = NUL;
3311 return OK;
3312}
3313
3314#ifdef FEAT_CLIPBOARD
3315/*
3316 * Make a copy of the y_current register to register "reg".
3317 */
3318 static void
3319copy_yank_reg(reg)
3320 struct yankreg *reg;
3321{
3322 struct yankreg *curr = y_current;
3323 long j;
3324
3325 y_current = reg;
3326 free_yank_all();
3327 *y_current = *curr;
3328 y_current->y_array = (char_u **)lalloc_clear(
3329 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3330 if (y_current->y_array == NULL)
3331 y_current->y_size = 0;
3332 else
3333 for (j = 0; j < y_current->y_size; ++j)
3334 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3335 {
3336 free_yank(j);
3337 y_current->y_size = 0;
3338 break;
3339 }
3340 y_current = curr;
3341}
3342#endif
3343
3344/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003345 * Put contents of register "regname" into the text.
3346 * Caller must check "regname" to be valid!
3347 * "flags": PUT_FIXINDENT make indent look nice
3348 * PUT_CURSEND leave cursor after end of new text
3349 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
3351 void
3352do_put(regname, dir, count, flags)
3353 int regname;
3354 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3355 long count;
3356 int flags;
3357{
3358 char_u *ptr;
3359 char_u *newp, *oldp;
3360 int yanklen;
3361 int totlen = 0; /* init for gcc */
3362 linenr_T lnum;
3363 colnr_T col;
3364 long i; /* index in y_array[] */
3365 int y_type;
3366 long y_size;
3367#ifdef FEAT_VISUAL
3368 int oldlen;
3369 long y_width = 0;
3370 colnr_T vcol;
3371 int delcount;
3372 int incr = 0;
3373 long j;
3374 struct block_def bd;
3375#endif
3376 char_u **y_array = NULL;
3377 long nr_lines = 0;
3378 pos_T new_cursor;
3379 int indent;
3380 int orig_indent = 0; /* init for gcc */
3381 int indent_diff = 0; /* init for gcc */
3382 int first_indent = TRUE;
3383 int lendiff = 0;
3384 pos_T old_pos;
3385 char_u *insert_string = NULL;
3386 int allocated = FALSE;
3387 long cnt;
3388
3389#ifdef FEAT_CLIPBOARD
3390 /* Adjust register name for "unnamed" in 'clipboard'. */
3391 adjust_clip_reg(&regname);
3392 (void)may_get_selection(regname);
3393#endif
3394
3395 if (flags & PUT_FIXINDENT)
3396 orig_indent = get_indent();
3397
3398 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3399 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3400
3401 /*
3402 * Using inserted text works differently, because the register includes
3403 * special characters (newlines, etc.).
3404 */
3405 if (regname == '.')
3406 {
3407 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3408 (count == -1 ? 'O' : 'i')), count, FALSE);
3409 /* Putting the text is done later, so can't really move the cursor to
3410 * the next character. Use "l" to simulate it. */
3411 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3412 stuffcharReadbuff('l');
3413 return;
3414 }
3415
3416 /*
3417 * For special registers '%' (file name), '#' (alternate file name) and
3418 * ':' (last command line), etc. we have to create a fake yank register.
3419 */
3420 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3421 {
3422 if (insert_string == NULL)
3423 return;
3424 }
3425
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003426#ifdef FEAT_AUTOCMD
3427 /* Autocommands may be executed when saving lines for undo, which may make
3428 * y_array invalid. Start undo now to avoid that. */
3429 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3430#endif
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (insert_string != NULL)
3433 {
3434 y_type = MCHAR;
3435#ifdef FEAT_EVAL
3436 if (regname == '=')
3437 {
3438 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003439 * characters.
3440 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 for (;;)
3442 {
3443 y_size = 0;
3444 ptr = insert_string;
3445 while (ptr != NULL)
3446 {
3447 if (y_array != NULL)
3448 y_array[y_size] = ptr;
3449 ++y_size;
3450 ptr = vim_strchr(ptr, '\n');
3451 if (ptr != NULL)
3452 {
3453 if (y_array != NULL)
3454 *ptr = NUL;
3455 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003456 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (*ptr == NUL)
3458 {
3459 y_type = MLINE;
3460 break;
3461 }
3462 }
3463 }
3464 if (y_array != NULL)
3465 break;
3466 y_array = (char_u **)alloc((unsigned)
3467 (y_size * sizeof(char_u *)));
3468 if (y_array == NULL)
3469 goto end;
3470 }
3471 }
3472 else
3473#endif
3474 {
3475 y_size = 1; /* use fake one-line yank register */
3476 y_array = &insert_string;
3477 }
3478 }
3479 else
3480 {
3481 get_yank_register(regname, FALSE);
3482
3483 y_type = y_current->y_type;
3484#ifdef FEAT_VISUAL
3485 y_width = y_current->y_width;
3486#endif
3487 y_size = y_current->y_size;
3488 y_array = y_current->y_array;
3489 }
3490
3491#ifdef FEAT_VISUAL
3492 if (y_type == MLINE)
3493 {
3494 if (flags & PUT_LINE_SPLIT)
3495 {
3496 /* "p" or "P" in Visual mode: split the lines to put the text in
3497 * between. */
3498 if (u_save_cursor() == FAIL)
3499 goto end;
3500 ptr = vim_strsave(ml_get_cursor());
3501 if (ptr == NULL)
3502 goto end;
3503 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3504 vim_free(ptr);
3505
3506 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3507 if (ptr == NULL)
3508 goto end;
3509 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3510 ++nr_lines;
3511 dir = FORWARD;
3512 }
3513 if (flags & PUT_LINE_FORWARD)
3514 {
3515 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003516 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 dir = FORWARD;
3518 }
3519 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3520 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3521 }
3522#endif
3523
3524 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3525 y_type = MLINE;
3526
3527 if (y_size == 0 || y_array == NULL)
3528 {
3529 EMSG2(_("E353: Nothing in register %s"),
3530 regname == 0 ? (char_u *)"\"" : transchar(regname));
3531 goto end;
3532 }
3533
3534#ifdef FEAT_VISUAL
3535 if (y_type == MBLOCK)
3536 {
3537 lnum = curwin->w_cursor.lnum + y_size + 1;
3538 if (lnum > curbuf->b_ml.ml_line_count)
3539 lnum = curbuf->b_ml.ml_line_count + 1;
3540 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3541 goto end;
3542 }
3543 else
3544#endif
3545 if (y_type == MLINE)
3546 {
3547 lnum = curwin->w_cursor.lnum;
3548#ifdef FEAT_FOLDING
3549 /* Correct line number for closed fold. Don't move the cursor yet,
3550 * u_save() uses it. */
3551 if (dir == BACKWARD)
3552 (void)hasFolding(lnum, &lnum, NULL);
3553 else
3554 (void)hasFolding(lnum, NULL, &lnum);
3555#endif
3556 if (dir == FORWARD)
3557 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003558 /* In an empty buffer the empty line is going to be replaced, include
3559 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003560 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 goto end;
3562#ifdef FEAT_FOLDING
3563 if (dir == FORWARD)
3564 curwin->w_cursor.lnum = lnum - 1;
3565 else
3566 curwin->w_cursor.lnum = lnum;
3567 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3568#endif
3569 }
3570 else if (u_save_cursor() == FAIL)
3571 goto end;
3572
3573 yanklen = (int)STRLEN(y_array[0]);
3574
3575#ifdef FEAT_VIRTUALEDIT
3576 if (ve_flags == VE_ALL && y_type == MCHAR)
3577 {
3578 if (gchar_cursor() == TAB)
3579 {
3580 /* Don't need to insert spaces when "p" on the last position of a
3581 * tab or "P" on the first position. */
3582 if (dir == FORWARD
3583 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3584 : curwin->w_cursor.coladd > 0)
3585 coladvance_force(getviscol());
3586 else
3587 curwin->w_cursor.coladd = 0;
3588 }
3589 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3590 coladvance_force(getviscol() + (dir == FORWARD));
3591 }
3592#endif
3593
3594 lnum = curwin->w_cursor.lnum;
3595 col = curwin->w_cursor.col;
3596
3597#ifdef FEAT_VISUAL
3598 /*
3599 * Block mode
3600 */
3601 if (y_type == MBLOCK)
3602 {
3603 char c = gchar_cursor();
3604 colnr_T endcol2 = 0;
3605
3606 if (dir == FORWARD && c != NUL)
3607 {
3608#ifdef FEAT_VIRTUALEDIT
3609 if (ve_flags == VE_ALL)
3610 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3611 else
3612#endif
3613 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3614
3615#ifdef FEAT_MBYTE
3616 if (has_mbyte)
3617 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003618 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
3620#endif
3621#ifdef FEAT_VIRTUALEDIT
3622 if (c != TAB || ve_flags != VE_ALL)
3623#endif
3624 ++curwin->w_cursor.col;
3625 ++col;
3626 }
3627 else
3628 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3629
3630#ifdef FEAT_VIRTUALEDIT
3631 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003632 if (ve_flags == VE_ALL
3633 && (curwin->w_cursor.coladd > 0
3634 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
3636 if (dir == FORWARD && c == NUL)
3637 ++col;
3638 if (dir != FORWARD && c != NUL)
3639 ++curwin->w_cursor.col;
3640 if (c == TAB)
3641 {
3642 if (dir == BACKWARD && curwin->w_cursor.col)
3643 curwin->w_cursor.col--;
3644 if (dir == FORWARD && col - 1 == endcol2)
3645 curwin->w_cursor.col++;
3646 }
3647 }
3648 curwin->w_cursor.coladd = 0;
3649#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003650 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 for (i = 0; i < y_size; ++i)
3652 {
3653 int spaces;
3654 char shortline;
3655
3656 bd.startspaces = 0;
3657 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 vcol = 0;
3659 delcount = 0;
3660
3661 /* add a new line */
3662 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3663 {
3664 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3665 (colnr_T)1, FALSE) == FAIL)
3666 break;
3667 ++nr_lines;
3668 }
3669 /* get the old line and advance to the position to insert at */
3670 oldp = ml_get_curline();
3671 oldlen = (int)STRLEN(oldp);
3672 for (ptr = oldp; vcol < col && *ptr; )
3673 {
3674 /* Count a tab for what it's worth (if list mode not on) */
3675 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3676 vcol += incr;
3677 }
3678 bd.textcol = (colnr_T)(ptr - oldp);
3679
3680 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3681
3682 if (vcol < col) /* line too short, padd with spaces */
3683 bd.startspaces = col - vcol;
3684 else if (vcol > col)
3685 {
3686 bd.endspaces = vcol - col;
3687 bd.startspaces = incr - bd.endspaces;
3688 --bd.textcol;
3689 delcount = 1;
3690#ifdef FEAT_MBYTE
3691 if (has_mbyte)
3692 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3693#endif
3694 if (oldp[bd.textcol] != TAB)
3695 {
3696 /* Only a Tab can be split into spaces. Other
3697 * characters will have to be moved to after the
3698 * block, causing misalignment. */
3699 delcount = 0;
3700 bd.endspaces = 0;
3701 }
3702 }
3703
3704 yanklen = (int)STRLEN(y_array[i]);
3705
3706 /* calculate number of spaces required to fill right side of block*/
3707 spaces = y_width + 1;
3708 for (j = 0; j < yanklen; j++)
3709 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3710 if (spaces < 0)
3711 spaces = 0;
3712
3713 /* insert the new text */
3714 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3715 newp = alloc_check((unsigned)totlen + oldlen + 1);
3716 if (newp == NULL)
3717 break;
3718 /* copy part up to cursor to new line */
3719 ptr = newp;
3720 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3721 ptr += bd.textcol;
3722 /* may insert some spaces before the new text */
3723 copy_spaces(ptr, (size_t)bd.startspaces);
3724 ptr += bd.startspaces;
3725 /* insert the new text */
3726 for (j = 0; j < count; ++j)
3727 {
3728 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3729 ptr += yanklen;
3730
3731 /* insert block's trailing spaces only if there's text behind */
3732 if ((j < count - 1 || !shortline) && spaces)
3733 {
3734 copy_spaces(ptr, (size_t)spaces);
3735 ptr += spaces;
3736 }
3737 }
3738 /* may insert some spaces after the new text */
3739 copy_spaces(ptr, (size_t)bd.endspaces);
3740 ptr += bd.endspaces;
3741 /* move the text after the cursor to the end of the line. */
3742 mch_memmove(ptr, oldp + bd.textcol + delcount,
3743 (size_t)(oldlen - bd.textcol - delcount + 1));
3744 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3745
3746 ++curwin->w_cursor.lnum;
3747 if (i == 0)
3748 curwin->w_cursor.col += bd.startspaces;
3749 }
3750
3751 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3752
3753 /* Set '[ mark. */
3754 curbuf->b_op_start = curwin->w_cursor;
3755 curbuf->b_op_start.lnum = lnum;
3756
3757 /* adjust '] mark */
3758 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3759 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003760# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (flags & PUT_CURSEND)
3764 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003765 colnr_T len;
3766
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 curwin->w_cursor = curbuf->b_op_end;
3768 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003769
3770 /* in Insert mode we might be after the NUL, correct for that */
3771 len = (colnr_T)STRLEN(ml_get_curline());
3772 if (curwin->w_cursor.col > len)
3773 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775 else
3776 curwin->w_cursor.lnum = lnum;
3777 }
3778 else
3779#endif
3780 {
3781 /*
3782 * Character or Line mode
3783 */
3784 if (y_type == MCHAR)
3785 {
3786 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3787 * char */
3788 if (dir == FORWARD && gchar_cursor() != NUL)
3789 {
3790#ifdef FEAT_MBYTE
3791 if (has_mbyte)
3792 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003793 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 /* put it on the next of the multi-byte character. */
3796 col += bytelen;
3797 if (yanklen)
3798 {
3799 curwin->w_cursor.col += bytelen;
3800 curbuf->b_op_end.col += bytelen;
3801 }
3802 }
3803 else
3804#endif
3805 {
3806 ++col;
3807 if (yanklen)
3808 {
3809 ++curwin->w_cursor.col;
3810 ++curbuf->b_op_end.col;
3811 }
3812 }
3813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 curbuf->b_op_start = curwin->w_cursor;
3815 }
3816 /*
3817 * Line mode: BACKWARD is the same as FORWARD on the previous line
3818 */
3819 else if (dir == BACKWARD)
3820 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003821 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 /*
3824 * simple case: insert into current line
3825 */
3826 if (y_type == MCHAR && y_size == 1)
3827 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003828 do {
3829 totlen = count * yanklen;
3830 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003832 oldp = ml_get(lnum);
3833 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3834 if (newp == NULL)
3835 goto end; /* alloc() gave an error message */
3836 mch_memmove(newp, oldp, (size_t)col);
3837 ptr = newp + col;
3838 for (i = 0; i < count; ++i)
3839 {
3840 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3841 ptr += yanklen;
3842 }
3843 STRMOVE(ptr, oldp + col);
3844 ml_replace(lnum, newp, FALSE);
3845 /* Place cursor on last putted char. */
3846 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003847 {
3848 /* make sure curwin->w_virtcol is updated */
3849 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003850 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003853#ifdef FEAT_VISUAL
3854 if (VIsual_active)
3855 lnum++;
3856#endif
3857 } while (
3858#ifdef FEAT_VISUAL
3859 VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum
3860#else
3861 FALSE /* stop after 1 paste */
3862#endif
3863 );
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003864
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 curbuf->b_op_end = curwin->w_cursor;
3866 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3867 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3868 ++curwin->w_cursor.col;
3869 changed_bytes(lnum, col);
3870 }
3871 else
3872 {
3873 /*
3874 * Insert at least one line. When y_type is MCHAR, break the first
3875 * line in two.
3876 */
3877 for (cnt = 1; cnt <= count; ++cnt)
3878 {
3879 i = 0;
3880 if (y_type == MCHAR)
3881 {
3882 /*
3883 * Split the current line in two at the insert position.
3884 * First insert y_array[size - 1] in front of second line.
3885 * Then append y_array[0] to first line.
3886 */
3887 lnum = new_cursor.lnum;
3888 ptr = ml_get(lnum) + col;
3889 totlen = (int)STRLEN(y_array[y_size - 1]);
3890 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3891 if (newp == NULL)
3892 goto error;
3893 STRCPY(newp, y_array[y_size - 1]);
3894 STRCAT(newp, ptr);
3895 /* insert second line */
3896 ml_append(lnum, newp, (colnr_T)0, FALSE);
3897 vim_free(newp);
3898
3899 oldp = ml_get(lnum);
3900 newp = alloc_check((unsigned)(col + yanklen + 1));
3901 if (newp == NULL)
3902 goto error;
3903 /* copy first part of line */
3904 mch_memmove(newp, oldp, (size_t)col);
3905 /* append to first line */
3906 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3907 ml_replace(lnum, newp, FALSE);
3908
3909 curwin->w_cursor.lnum = lnum;
3910 i = 1;
3911 }
3912
3913 for (; i < y_size; ++i)
3914 {
3915 if ((y_type != MCHAR || i < y_size - 1)
3916 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3917 == FAIL)
3918 goto error;
3919 lnum++;
3920 ++nr_lines;
3921 if (flags & PUT_FIXINDENT)
3922 {
3923 old_pos = curwin->w_cursor;
3924 curwin->w_cursor.lnum = lnum;
3925 ptr = ml_get(lnum);
3926 if (cnt == count && i == y_size - 1)
3927 lendiff = (int)STRLEN(ptr);
3928#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3929 if (*ptr == '#' && preprocs_left())
3930 indent = 0; /* Leave # lines at start */
3931 else
3932#endif
3933 if (*ptr == NUL)
3934 indent = 0; /* Ignore empty lines */
3935 else if (first_indent)
3936 {
3937 indent_diff = orig_indent - get_indent();
3938 indent = orig_indent;
3939 first_indent = FALSE;
3940 }
3941 else if ((indent = get_indent() + indent_diff) < 0)
3942 indent = 0;
3943 (void)set_indent(indent, 0);
3944 curwin->w_cursor = old_pos;
3945 /* remember how many chars were removed */
3946 if (cnt == count && i == y_size - 1)
3947 lendiff -= (int)STRLEN(ml_get(lnum));
3948 }
3949 }
3950 }
3951
3952error:
3953 /* Adjust marks. */
3954 if (y_type == MLINE)
3955 {
3956 curbuf->b_op_start.col = 0;
3957 if (dir == FORWARD)
3958 curbuf->b_op_start.lnum++;
3959 }
3960 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3961 (linenr_T)MAXLNUM, nr_lines, 0L);
3962
3963 /* note changed text for displaying and folding */
3964 if (y_type == MCHAR)
3965 changed_lines(curwin->w_cursor.lnum, col,
3966 curwin->w_cursor.lnum + 1, nr_lines);
3967 else
3968 changed_lines(curbuf->b_op_start.lnum, 0,
3969 curbuf->b_op_start.lnum, nr_lines);
3970
3971 /* put '] mark at last inserted character */
3972 curbuf->b_op_end.lnum = lnum;
3973 /* correct length for change in indent */
3974 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3975 if (col > 1)
3976 curbuf->b_op_end.col = col - 1;
3977 else
3978 curbuf->b_op_end.col = 0;
3979
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003980 if (flags & PUT_CURSLINE)
3981 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003982 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003983 curwin->w_cursor.lnum = lnum;
3984 beginline(BL_WHITE | BL_FIX);
3985 }
3986 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
3988 /* put cursor after inserted text */
3989 if (y_type == MLINE)
3990 {
3991 if (lnum >= curbuf->b_ml.ml_line_count)
3992 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3993 else
3994 curwin->w_cursor.lnum = lnum + 1;
3995 curwin->w_cursor.col = 0;
3996 }
3997 else
3998 {
3999 curwin->w_cursor.lnum = lnum;
4000 curwin->w_cursor.col = col;
4001 }
4002 }
4003 else if (y_type == MLINE)
4004 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004005 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 curwin->w_cursor.col = 0;
4007 if (dir == FORWARD)
4008 ++curwin->w_cursor.lnum;
4009 beginline(BL_WHITE | BL_FIX);
4010 }
4011 else /* put cursor on first inserted character */
4012 curwin->w_cursor = new_cursor;
4013 }
4014 }
4015
4016 msgmore(nr_lines);
4017 curwin->w_set_curswant = TRUE;
4018
4019end:
4020 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004022 if (regname == '=')
4023 vim_free(y_array);
4024
Bram Moolenaar033d8882013-09-25 23:24:57 +02004025#ifdef FEAT_VISUAL
4026 VIsual_active = FALSE;
4027#endif
4028
Bram Moolenaar677ee682005-01-27 14:41:15 +00004029 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004030 adjust_cursor_eol();
4031}
4032
4033/*
4034 * When the cursor is on the NUL past the end of the line and it should not be
4035 * there move it left.
4036 */
4037 void
4038adjust_cursor_eol()
4039{
4040 if (curwin->w_cursor.col > 0
4041 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004042#ifdef FEAT_VIRTUALEDIT
4043 && (ve_flags & VE_ONEMORE) == 0
4044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 && !(restart_edit || (State & INSERT)))
4046 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004047 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004048 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#ifdef FEAT_VIRTUALEDIT
4051 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004052 {
4053 colnr_T scol, ecol;
4054
4055 /* Coladd is set to the width of the last character. */
4056 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4057 curwin->w_cursor.coladd = ecol - scol + 1;
4058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059#endif
4060 }
4061}
4062
4063#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4064/*
4065 * Return TRUE if lines starting with '#' should be left aligned.
4066 */
4067 int
4068preprocs_left()
4069{
4070 return
4071# ifdef FEAT_SMARTINDENT
4072# ifdef FEAT_CINDENT
4073 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4074# else
4075 curbuf->b_p_si
4076# endif
4077# endif
4078# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004079 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4080 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081# endif
4082 ;
4083}
4084#endif
4085
4086/* Return the character name of the register with the given number */
4087 int
4088get_register_name(num)
4089 int num;
4090{
4091 if (num == -1)
4092 return '"';
4093 else if (num < 10)
4094 return num + '0';
4095 else if (num == DELETION_REGISTER)
4096 return '-';
4097#ifdef FEAT_CLIPBOARD
4098 else if (num == STAR_REGISTER)
4099 return '*';
4100 else if (num == PLUS_REGISTER)
4101 return '+';
4102#endif
4103 else
4104 {
4105#ifdef EBCDIC
4106 int i;
4107
4108 /* EBCDIC is really braindead ... */
4109 i = 'a' + (num - 10);
4110 if (i > 'i')
4111 i += 7;
4112 if (i > 'r')
4113 i += 8;
4114 return i;
4115#else
4116 return num + 'a' - 10;
4117#endif
4118 }
4119}
4120
4121/*
4122 * ":dis" and ":registers": Display the contents of the yank registers.
4123 */
4124 void
4125ex_display(eap)
4126 exarg_T *eap;
4127{
4128 int i, n;
4129 long j;
4130 char_u *p;
4131 struct yankreg *yb;
4132 int name;
4133 int attr;
4134 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004135#ifdef FEAT_MBYTE
4136 int clen;
4137#else
4138# define clen 1
4139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 if (arg != NULL && *arg == NUL)
4142 arg = NULL;
4143 attr = hl_attr(HLF_8);
4144
4145 /* Highlight title */
4146 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4147 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4148 {
4149 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004150 if (arg != NULL && vim_strchr(arg, name) == NULL
4151#ifdef ONE_CLIPBOARD
4152 /* Star register and plus register contain the same thing. */
4153 && (name != '*' || vim_strchr(arg, '+') == NULL)
4154#endif
4155 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 continue; /* did not ask for this register */
4157
4158#ifdef FEAT_CLIPBOARD
4159 /* Adjust register name for "unnamed" in 'clipboard'.
4160 * When it's a clipboard register, fill it with the current contents
4161 * of the clipboard. */
4162 adjust_clip_reg(&name);
4163 (void)may_get_selection(name);
4164#endif
4165
4166 if (i == -1)
4167 {
4168 if (y_previous != NULL)
4169 yb = y_previous;
4170 else
4171 yb = &(y_regs[0]);
4172 }
4173 else
4174 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004175
4176#ifdef FEAT_EVAL
4177 if (name == MB_TOLOWER(redir_reg)
4178 || (redir_reg == '"' && yb == y_previous))
4179 continue; /* do not list register being written to, the
4180 * pointer can be freed */
4181#endif
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (yb->y_array != NULL)
4184 {
4185 msg_putchar('\n');
4186 msg_putchar('"');
4187 msg_putchar(name);
4188 MSG_PUTS(" ");
4189
4190 n = (int)Columns - 6;
4191 for (j = 0; j < yb->y_size && n > 1; ++j)
4192 {
4193 if (j)
4194 {
4195 MSG_PUTS_ATTR("^J", attr);
4196 n -= 2;
4197 }
4198 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004201 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004202#endif
4203 msg_outtrans_len(p, clen);
4204#ifdef FEAT_MBYTE
4205 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#endif
4207 }
4208 }
4209 if (n > 1 && yb->y_type == MLINE)
4210 MSG_PUTS_ATTR("^J", attr);
4211 out_flush(); /* show one line at a time */
4212 }
4213 ui_breakcheck();
4214 }
4215
4216 /*
4217 * display last inserted text
4218 */
4219 if ((p = get_last_insert()) != NULL
4220 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4221 {
4222 MSG_PUTS("\n\". ");
4223 dis_msg(p, TRUE);
4224 }
4225
4226 /*
4227 * display last command line
4228 */
4229 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4230 && !got_int)
4231 {
4232 MSG_PUTS("\n\": ");
4233 dis_msg(last_cmdline, FALSE);
4234 }
4235
4236 /*
4237 * display current file name
4238 */
4239 if (curbuf->b_fname != NULL
4240 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4241 {
4242 MSG_PUTS("\n\"% ");
4243 dis_msg(curbuf->b_fname, FALSE);
4244 }
4245
4246 /*
4247 * display alternate file name
4248 */
4249 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4250 {
4251 char_u *fname;
4252 linenr_T dummy;
4253
4254 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4255 {
4256 MSG_PUTS("\n\"# ");
4257 dis_msg(fname, FALSE);
4258 }
4259 }
4260
4261 /*
4262 * display last search pattern
4263 */
4264 if (last_search_pat() != NULL
4265 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4266 {
4267 MSG_PUTS("\n\"/ ");
4268 dis_msg(last_search_pat(), FALSE);
4269 }
4270
4271#ifdef FEAT_EVAL
4272 /*
4273 * display last used expression
4274 */
4275 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4276 && !got_int)
4277 {
4278 MSG_PUTS("\n\"= ");
4279 dis_msg(expr_line, FALSE);
4280 }
4281#endif
4282}
4283
4284/*
4285 * display a string for do_dis()
4286 * truncate at end of screen line
4287 */
4288 static void
4289dis_msg(p, skip_esc)
4290 char_u *p;
4291 int skip_esc; /* if TRUE, ignore trailing ESC */
4292{
4293 int n;
4294#ifdef FEAT_MBYTE
4295 int l;
4296#endif
4297
4298 n = (int)Columns - 6;
4299 while (*p != NUL
4300 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4301 && (n -= ptr2cells(p)) >= 0)
4302 {
4303#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004304 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
4306 msg_outtrans_len(p, l);
4307 p += l;
4308 }
4309 else
4310#endif
4311 msg_outtrans_len(p++, 1);
4312 }
4313 ui_breakcheck();
4314}
4315
Bram Moolenaar81340392012-06-06 16:12:59 +02004316#if defined(FEAT_COMMENTS) || defined(PROTO)
4317/*
4318 * If "process" is TRUE and the line begins with a comment leader (possibly
4319 * after some white space), return a pointer to the text after it. Put a boolean
4320 * value indicating whether the line ends with an unclosed comment in
4321 * "is_comment".
4322 * line - line to be processed,
4323 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004324 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004325 * include_space - whether to also skip space following the comment leader,
4326 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004327 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004328 */
4329 static char_u *
4330skip_comment(line, process, include_space, is_comment)
4331 char_u *line;
4332 int process;
4333 int include_space;
4334 int *is_comment;
4335{
4336 char_u *comment_flags = NULL;
4337 int lead_len;
4338 int leader_offset = get_last_leader_offset(line, &comment_flags);
4339
4340 *is_comment = FALSE;
4341 if (leader_offset != -1)
4342 {
4343 /* Let's check whether the line ends with an unclosed comment.
4344 * If the last comment leader has COM_END in flags, there's no comment.
4345 */
4346 while (*comment_flags)
4347 {
4348 if (*comment_flags == COM_END
4349 || *comment_flags == ':')
4350 break;
4351 ++comment_flags;
4352 }
4353 if (*comment_flags != COM_END)
4354 *is_comment = TRUE;
4355 }
4356
4357 if (process == FALSE)
4358 return line;
4359
4360 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4361
4362 if (lead_len == 0)
4363 return line;
4364
4365 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004366 * - COM_END,
4367 * - colon,
4368 * whichever comes first.
4369 */
4370 while (*comment_flags)
4371 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004372 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004373 || *comment_flags == ':')
4374 {
4375 break;
4376 }
4377 ++comment_flags;
4378 }
4379
4380 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004381 * starting with a closing part of a three-part comment. That's good,
4382 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004383 */
4384 if (*comment_flags == ':' || *comment_flags == NUL)
4385 line += lead_len;
4386
4387 return line;
4388}
4389#endif
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004392 * Join 'count' lines (minimal 2) at cursor position.
4393 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaar81340392012-06-06 16:12:59 +02004394 * Set "use_formatoptions" to FALSE when e.g. processing
4395 * backspace and comment leaders should not be removed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004397 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 */
4399 int
Bram Moolenaar81340392012-06-06 16:12:59 +02004400do_join(count, insert_space, save_undo, use_formatoptions)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004401 long count;
4402 int insert_space;
4403 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004404 int use_formatoptions UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004406 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004407 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004408 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004410 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004411 int endcurr1 = NUL;
4412 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004413 int currsize = 0; /* size of the current line */
4414 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004416 colnr_T col = 0;
4417 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004418#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004419 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004420 int remove_comments = (use_formatoptions == TRUE)
4421 && has_format_option(FO_REMOVE_COMS);
4422 int prev_was_comment;
4423#endif
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004426 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4427 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004430 /* Allocate an array to store the number of spaces inserted before each
4431 * line. We will use it to pre-compute the length of the new line and the
4432 * proper placement of each original line in the new one. */
4433 spaces = lalloc_clear((long_u)count, TRUE);
4434 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004436#if defined(FEAT_COMMENTS) || defined(PROTO)
4437 if (remove_comments)
4438 {
4439 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4440 if (comments == NULL)
4441 {
4442 vim_free(spaces);
4443 return FAIL;
4444 }
4445 }
4446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004449 * Don't move anything, just compute the final line length
4450 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004452 for (t = 0; t < count; ++t)
4453 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004454 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar81340392012-06-06 16:12:59 +02004455#if defined(FEAT_COMMENTS) || defined(PROTO)
4456 if (remove_comments)
4457 {
4458 /* We don't want to remove the comment leader if the
4459 * previous line is not a comment. */
4460 if (t > 0 && prev_was_comment)
4461 {
4462
4463 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4464 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004465 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004466 curr = new_curr;
4467 }
4468 else
4469 curr = skip_comment(curr, FALSE, insert_space,
4470 &prev_was_comment);
4471 }
4472#endif
4473
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004474 if (insert_space && t > 0)
4475 {
4476 curr = skipwhite(curr);
4477 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4478#ifdef FEAT_MBYTE
4479 && (!has_format_option(FO_MBYTE_JOIN)
4480 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4481 && (!has_format_option(FO_MBYTE_JOIN2)
4482 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4483#endif
4484 )
4485 {
4486 /* don't add a space if the line is ending in a space */
4487 if (endcurr1 == ' ')
4488 endcurr1 = endcurr2;
4489 else
4490 ++spaces[t];
4491 /* extra space when 'joinspaces' set and line ends in '.' */
4492 if ( p_js
4493 && (endcurr1 == '.'
4494 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4495 && (endcurr1 == '?' || endcurr1 == '!'))))
4496 ++spaces[t];
4497 }
4498 }
4499 currsize = (int)STRLEN(curr);
4500 sumsize += currsize + spaces[t];
4501 endcurr1 = endcurr2 = NUL;
4502 if (insert_space && currsize > 0)
4503 {
4504#ifdef FEAT_MBYTE
4505 if (has_mbyte)
4506 {
4507 cend = curr + currsize;
4508 mb_ptr_back(curr, cend);
4509 endcurr1 = (*mb_ptr2char)(cend);
4510 if (cend > curr)
4511 {
4512 mb_ptr_back(curr, cend);
4513 endcurr2 = (*mb_ptr2char)(cend);
4514 }
4515 }
4516 else
4517#endif
4518 {
4519 endcurr1 = *(curr + currsize - 1);
4520 if (currsize > 1)
4521 endcurr2 = *(curr + currsize - 2);
4522 }
4523 }
4524 line_breakcheck();
4525 if (got_int)
4526 {
4527 ret = FAIL;
4528 goto theend;
4529 }
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004532 /* store the column position before last line */
4533 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004535 /* allocate the space for the new line */
4536 newp = alloc_check((unsigned)(sumsize + 1));
4537 cend = newp + sumsize;
4538 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004540 /*
4541 * Move affected lines to the new long one.
4542 *
4543 * Move marks from each deleted line to the joined line, adjusting the
4544 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4545 * should not really be a problem.
4546 */
4547 for (t = count - 1; ; --t)
4548 {
4549 cend -= currsize;
4550 mch_memmove(cend, curr, (size_t)currsize);
4551 if (spaces[t] > 0)
4552 {
4553 cend -= spaces[t];
4554 copy_spaces(cend, (size_t)(spaces[t]));
4555 }
4556 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004557 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004558 if (t == 0)
4559 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004560 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004561#if defined(FEAT_COMMENTS) || defined(PROTO)
4562 if (remove_comments)
4563 curr += comments[t - 1];
4564#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004565 if (insert_space && t > 1)
4566 curr = skipwhite(curr);
4567 currsize = (int)STRLEN(curr);
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4570
4571 /* Only report the change in the first line here, del_lines() will report
4572 * the deleted line. */
4573 changed_lines(curwin->w_cursor.lnum, currsize,
4574 curwin->w_cursor.lnum + 1, 0L);
4575
4576 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004577 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 * briefly, and then move it back. After del_lines() the cursor may
4579 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 */
4581 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004583 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 curwin->w_cursor.lnum = t;
4585
4586 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004587 * Set the cursor column:
4588 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004589 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004591 curwin->w_cursor.col =
4592 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_VIRTUALEDIT
4596 curwin->w_cursor.coladd = 0;
4597#endif
4598 curwin->w_set_curswant = TRUE;
4599
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004600theend:
4601 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004602#if defined(FEAT_COMMENTS) || defined(PROTO)
4603 if (remove_comments)
4604 vim_free(comments);
4605#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004606 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607}
4608
4609#ifdef FEAT_COMMENTS
4610/*
4611 * Return TRUE if the two comment leaders given are the same. "lnum" is
4612 * the first line. White-space is ignored. Note that the whole of
4613 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4614 */
4615 static int
4616same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4617 linenr_T lnum;
4618 int leader1_len;
4619 char_u *leader1_flags;
4620 int leader2_len;
4621 char_u *leader2_flags;
4622{
4623 int idx1 = 0, idx2 = 0;
4624 char_u *p;
4625 char_u *line1;
4626 char_u *line2;
4627
4628 if (leader1_len == 0)
4629 return (leader2_len == 0);
4630
4631 /*
4632 * If first leader has 'f' flag, the lines can be joined only if the
4633 * second line does not have a leader.
4634 * If first leader has 'e' flag, the lines can never be joined.
4635 * If fist leader has 's' flag, the lines can only be joined if there is
4636 * some text after it and the second line has the 'm' flag.
4637 */
4638 if (leader1_flags != NULL)
4639 {
4640 for (p = leader1_flags; *p && *p != ':'; ++p)
4641 {
4642 if (*p == COM_FIRST)
4643 return (leader2_len == 0);
4644 if (*p == COM_END)
4645 return FALSE;
4646 if (*p == COM_START)
4647 {
4648 if (*(ml_get(lnum) + leader1_len) == NUL)
4649 return FALSE;
4650 if (leader2_flags == NULL || leader2_len == 0)
4651 return FALSE;
4652 for (p = leader2_flags; *p && *p != ':'; ++p)
4653 if (*p == COM_MIDDLE)
4654 return TRUE;
4655 return FALSE;
4656 }
4657 }
4658 }
4659
4660 /*
4661 * Get current line and next line, compare the leaders.
4662 * The first line has to be saved, only one line can be locked at a time.
4663 */
4664 line1 = vim_strsave(ml_get(lnum));
4665 if (line1 != NULL)
4666 {
4667 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4668 ;
4669 line2 = ml_get(lnum + 1);
4670 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4671 {
4672 if (!vim_iswhite(line2[idx2]))
4673 {
4674 if (line1[idx1++] != line2[idx2])
4675 break;
4676 }
4677 else
4678 while (vim_iswhite(line1[idx1]))
4679 ++idx1;
4680 }
4681 vim_free(line1);
4682 }
4683 return (idx2 == leader2_len && idx1 == leader1_len);
4684}
4685#endif
4686
4687/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004688 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 */
4690 void
4691op_format(oap, keep_cursor)
4692 oparg_T *oap;
4693 int keep_cursor; /* keep cursor on same text char */
4694{
4695 long old_line_count = curbuf->b_ml.ml_line_count;
4696
4697 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4698 * can put it back there. */
4699 curwin->w_cursor = oap->cursor_start;
4700
4701 if (u_save((linenr_T)(oap->start.lnum - 1),
4702 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4703 return;
4704 curwin->w_cursor = oap->start;
4705
4706#ifdef FEAT_VISUAL
4707 if (oap->is_VIsual)
4708 /* When there is no change: need to remove the Visual selection */
4709 redraw_curbuf_later(INVERTED);
4710#endif
4711
4712 /* Set '[ mark at the start of the formatted area */
4713 curbuf->b_op_start = oap->start;
4714
4715 /* For "gw" remember the cursor position and put it back below (adjusted
4716 * for joined and split lines). */
4717 if (keep_cursor)
4718 saved_cursor = oap->cursor_start;
4719
Bram Moolenaar81a82092008-03-12 16:27:00 +00004720 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
4722 /*
4723 * Leave the cursor at the first non-blank of the last formatted line.
4724 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4725 * line, so "." will do the next lines.
4726 */
4727 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4728 ++curwin->w_cursor.lnum;
4729 beginline(BL_WHITE | BL_FIX);
4730 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4731 msgmore(old_line_count);
4732
4733 /* put '] mark on the end of the formatted area */
4734 curbuf->b_op_end = curwin->w_cursor;
4735
4736 if (keep_cursor)
4737 {
4738 curwin->w_cursor = saved_cursor;
4739 saved_cursor.lnum = 0;
4740 }
4741
4742#ifdef FEAT_VISUAL
4743 if (oap->is_VIsual)
4744 {
4745 win_T *wp;
4746
4747 FOR_ALL_WINDOWS(wp)
4748 {
4749 if (wp->w_old_cursor_lnum != 0)
4750 {
4751 /* When lines have been inserted or deleted, adjust the end of
4752 * the Visual area to be redrawn. */
4753 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4754 wp->w_old_cursor_lnum += old_line_count;
4755 else
4756 wp->w_old_visual_lnum += old_line_count;
4757 }
4758 }
4759 }
4760#endif
4761}
4762
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004763#if defined(FEAT_EVAL) || defined(PROTO)
4764/*
4765 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4766 */
4767 void
4768op_formatexpr(oap)
4769 oparg_T *oap;
4770{
4771# ifdef FEAT_VISUAL
4772 if (oap->is_VIsual)
4773 /* When there is no change: need to remove the Visual selection */
4774 redraw_curbuf_later(INVERTED);
4775# endif
4776
Bram Moolenaar700303e2010-07-11 17:35:50 +02004777 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4778 /* As documented: when 'formatexpr' returns non-zero fall back to
4779 * internal formatting. */
4780 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004781}
4782
4783 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004784fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004785 linenr_T lnum;
4786 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004787 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004788{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004789 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4790 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004791 int r;
4792
4793 /*
4794 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004795 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004796 */
4797 set_vim_var_nr(VV_LNUM, lnum);
4798 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004799 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004800
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004801 /*
4802 * Evaluate the function.
4803 */
4804 if (use_sandbox)
4805 ++sandbox;
4806 r = eval_to_number(curbuf->b_p_fex);
4807 if (use_sandbox)
4808 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004809
4810 set_vim_var_string(VV_CHAR, NULL, -1);
4811
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004812 return r;
4813}
4814#endif
4815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816/*
4817 * Format "line_count" lines, starting at the cursor position.
4818 * When "line_count" is negative, format until the end of the paragraph.
4819 * Lines after the cursor line are saved for undo, caller must have saved the
4820 * first line.
4821 */
4822 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004823format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004825 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
4827 int max_len;
4828 int is_not_par; /* current line not part of parag. */
4829 int next_is_not_par; /* next line not part of paragraph */
4830 int is_end_par; /* at end of paragraph */
4831 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4832 int next_is_start_par = FALSE;
4833#ifdef FEAT_COMMENTS
4834 int leader_len = 0; /* leader len of current line */
4835 int next_leader_len; /* leader len of next line */
4836 char_u *leader_flags = NULL; /* flags for leader of current line */
4837 char_u *next_leader_flags; /* flags for leader of next line */
4838 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004839 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840#endif
4841 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004842 int second_indent = -1; /* indent for second line (comment
4843 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 int do_second_indent;
4845 int do_number_indent;
4846 int do_trail_white;
4847 int first_par_line = TRUE;
4848 int smd_save;
4849 long count;
4850 int need_set_indent = TRUE; /* set indent of next paragraph */
4851 int force_format = FALSE;
4852 int old_State = State;
4853
4854 /* length of a line to force formatting: 3 * 'tw' */
4855 max_len = comp_textwidth(TRUE) * 3;
4856
4857 /* check for 'q', '2' and '1' in 'formatoptions' */
4858#ifdef FEAT_COMMENTS
4859 do_comments = has_format_option(FO_Q_COMS);
4860#endif
4861 do_second_indent = has_format_option(FO_Q_SECOND);
4862 do_number_indent = has_format_option(FO_Q_NUMBER);
4863 do_trail_white = has_format_option(FO_WHITE_PAR);
4864
4865 /*
4866 * Get info about the previous and current line.
4867 */
4868 if (curwin->w_cursor.lnum > 1)
4869 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4870#ifdef FEAT_COMMENTS
4871 , &leader_len, &leader_flags, do_comments
4872#endif
4873 );
4874 else
4875 is_not_par = TRUE;
4876 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4877#ifdef FEAT_COMMENTS
4878 , &next_leader_len, &next_leader_flags, do_comments
4879#endif
4880 );
4881 is_end_par = (is_not_par || next_is_not_par);
4882 if (!is_end_par && do_trail_white)
4883 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4884
4885 curwin->w_cursor.lnum--;
4886 for (count = line_count; count != 0 && !got_int; --count)
4887 {
4888 /*
4889 * Advance to next paragraph.
4890 */
4891 if (advance)
4892 {
4893 curwin->w_cursor.lnum++;
4894 prev_is_end_par = is_end_par;
4895 is_not_par = next_is_not_par;
4896#ifdef FEAT_COMMENTS
4897 leader_len = next_leader_len;
4898 leader_flags = next_leader_flags;
4899#endif
4900 }
4901
4902 /*
4903 * The last line to be formatted.
4904 */
4905 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4906 {
4907 next_is_not_par = TRUE;
4908#ifdef FEAT_COMMENTS
4909 next_leader_len = 0;
4910 next_leader_flags = NULL;
4911#endif
4912 }
4913 else
4914 {
4915 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4916#ifdef FEAT_COMMENTS
4917 , &next_leader_len, &next_leader_flags, do_comments
4918#endif
4919 );
4920 if (do_number_indent)
4921 next_is_start_par =
4922 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4923 }
4924 advance = TRUE;
4925 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4926 if (!is_end_par && do_trail_white)
4927 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4928
4929 /*
4930 * Skip lines that are not in a paragraph.
4931 */
4932 if (is_not_par)
4933 {
4934 if (line_count < 0)
4935 break;
4936 }
4937 else
4938 {
4939 /*
4940 * For the first line of a paragraph, check indent of second line.
4941 * Don't do this for comments and empty lines.
4942 */
4943 if (first_par_line
4944 && (do_second_indent || do_number_indent)
4945 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004946 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004948 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4949 {
4950#ifdef FEAT_COMMENTS
4951 if (leader_len == 0 && next_leader_len == 0)
4952 {
4953 /* no comment found */
4954#endif
4955 second_indent =
4956 get_indent_lnum(curwin->w_cursor.lnum + 1);
4957#ifdef FEAT_COMMENTS
4958 }
4959 else
4960 {
4961 second_indent = next_leader_len;
4962 do_comments_list = 1;
4963 }
4964#endif
4965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004967 {
4968#ifdef FEAT_COMMENTS
4969 if (leader_len == 0 && next_leader_len == 0)
4970 {
4971 /* no comment found */
4972#endif
4973 second_indent =
4974 get_number_indent(curwin->w_cursor.lnum);
4975#ifdef FEAT_COMMENTS
4976 }
4977 else
4978 {
4979 /* get_number_indent() is now "comment aware"... */
4980 second_indent =
4981 get_number_indent(curwin->w_cursor.lnum);
4982 do_comments_list = 1;
4983 }
4984#endif
4985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987
4988 /*
4989 * When the comment leader changes, it's the end of the paragraph.
4990 */
4991 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4992#ifdef FEAT_COMMENTS
4993 || !same_leader(curwin->w_cursor.lnum,
4994 leader_len, leader_flags,
4995 next_leader_len, next_leader_flags)
4996#endif
4997 )
4998 is_end_par = TRUE;
4999
5000 /*
5001 * If we have got to the end of a paragraph, or the line is
5002 * getting long, format it.
5003 */
5004 if (is_end_par || force_format)
5005 {
5006 if (need_set_indent)
5007 /* replace indent in first line with minimal number of
5008 * tabs and spaces, according to current options */
5009 (void)set_indent(get_indent(), SIN_CHANGED);
5010
5011 /* put cursor on last non-space */
5012 State = NORMAL; /* don't go past end-of-line */
5013 coladvance((colnr_T)MAXCOL);
5014 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5015 dec_cursor();
5016
5017 /* do the formatting, without 'showmode' */
5018 State = INSERT; /* for open_line() */
5019 smd_save = p_smd;
5020 p_smd = FALSE;
5021 insertchar(NUL, INSCHAR_FORMAT
5022#ifdef FEAT_COMMENTS
5023 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005024 + (do_comments && do_comments_list
5025 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005027 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 State = old_State;
5029 p_smd = smd_save;
5030 second_indent = -1;
5031 /* at end of par.: need to set indent of next par. */
5032 need_set_indent = is_end_par;
5033 if (is_end_par)
5034 {
5035 /* When called with a negative line count, break at the
5036 * end of the paragraph. */
5037 if (line_count < 0)
5038 break;
5039 first_par_line = TRUE;
5040 }
5041 force_format = FALSE;
5042 }
5043
5044 /*
5045 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005046 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 */
5048 if (!is_end_par)
5049 {
5050 advance = FALSE;
5051 curwin->w_cursor.lnum++;
5052 curwin->w_cursor.col = 0;
5053 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005057 {
5058 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5060 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005061 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005063 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5064 {
5065 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005066 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005067
5068 if (indent > 0)
5069 {
5070 (void)del_bytes(indent, FALSE, FALSE);
5071 mark_col_adjust(curwin->w_cursor.lnum,
5072 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005073 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 curwin->w_cursor.lnum--;
Bram Moolenaar81340392012-06-06 16:12:59 +02005076 if (do_join(2, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 beep_flush();
5079 break;
5080 }
5081 first_par_line = FALSE;
5082 /* If the line is getting long, format it next time */
5083 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5084 force_format = TRUE;
5085 else
5086 force_format = FALSE;
5087 }
5088 }
5089 line_breakcheck();
5090 }
5091}
5092
5093/*
5094 * Return TRUE if line "lnum" ends in a white character.
5095 */
5096 static int
5097ends_in_white(lnum)
5098 linenr_T lnum;
5099{
5100 char_u *s = ml_get(lnum);
5101 size_t l;
5102
5103 if (*s == NUL)
5104 return FALSE;
5105 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5106 * invocation may call function multiple times". */
5107 l = STRLEN(s) - 1;
5108 return vim_iswhite(s[l]);
5109}
5110
5111/*
5112 * Blank lines, and lines containing only the comment leader, are left
5113 * untouched by the formatting. The function returns TRUE in this
5114 * case. It also returns TRUE when a line starts with the end of a comment
5115 * ('e' in comment flags), so that this line is skipped, and not joined to the
5116 * previous line. A new paragraph starts after a blank line, or when the
5117 * comment leader changes -- webb.
5118 */
5119#ifdef FEAT_COMMENTS
5120 static int
5121fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5122 linenr_T lnum;
5123 int *leader_len;
5124 char_u **leader_flags;
5125 int do_comments;
5126{
5127 char_u *flags = NULL; /* init for GCC */
5128 char_u *ptr;
5129
5130 ptr = ml_get(lnum);
5131 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005132 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 else
5134 *leader_len = 0;
5135
5136 if (*leader_len > 0)
5137 {
5138 /*
5139 * Search for 'e' flag in comment leader flags.
5140 */
5141 flags = *leader_flags;
5142 while (*flags && *flags != ':' && *flags != COM_END)
5143 ++flags;
5144 }
5145
5146 return (*skipwhite(ptr + *leader_len) == NUL
5147 || (*leader_len > 0 && *flags == COM_END)
5148 || startPS(lnum, NUL, FALSE));
5149}
5150#else
5151 static int
5152fmt_check_par(lnum)
5153 linenr_T lnum;
5154{
5155 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5156}
5157#endif
5158
5159/*
5160 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5161 * previous line is in the same paragraph. Used for auto-formatting.
5162 */
5163 int
5164paragraph_start(lnum)
5165 linenr_T lnum;
5166{
5167 char_u *p;
5168#ifdef FEAT_COMMENTS
5169 int leader_len = 0; /* leader len of current line */
5170 char_u *leader_flags = NULL; /* flags for leader of current line */
5171 int next_leader_len; /* leader len of next line */
5172 char_u *next_leader_flags; /* flags for leader of next line */
5173 int do_comments; /* format comments */
5174#endif
5175
5176 if (lnum <= 1)
5177 return TRUE; /* start of the file */
5178
5179 p = ml_get(lnum - 1);
5180 if (*p == NUL)
5181 return TRUE; /* after empty line */
5182
5183#ifdef FEAT_COMMENTS
5184 do_comments = has_format_option(FO_Q_COMS);
5185#endif
5186 if (fmt_check_par(lnum - 1
5187#ifdef FEAT_COMMENTS
5188 , &leader_len, &leader_flags, do_comments
5189#endif
5190 ))
5191 return TRUE; /* after non-paragraph line */
5192
5193 if (fmt_check_par(lnum
5194#ifdef FEAT_COMMENTS
5195 , &next_leader_len, &next_leader_flags, do_comments
5196#endif
5197 ))
5198 return TRUE; /* "lnum" is not a paragraph line */
5199
5200 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5201 return TRUE; /* missing trailing space in previous line. */
5202
5203 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5204 return TRUE; /* numbered item starts in "lnum". */
5205
5206#ifdef FEAT_COMMENTS
5207 if (!same_leader(lnum - 1, leader_len, leader_flags,
5208 next_leader_len, next_leader_flags))
5209 return TRUE; /* change of comment leader. */
5210#endif
5211
5212 return FALSE;
5213}
5214
5215#ifdef FEAT_VISUAL
5216/*
5217 * prepare a few things for block mode yank/delete/tilde
5218 *
5219 * for delete:
5220 * - textlen includes the first/last char to be (partly) deleted
5221 * - start/endspaces is the number of columns that are taken by the
5222 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005223 * deleted.
5224 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 * - textlen includes the first/last char to be wholly yanked
5226 * - start/endspaces is the number of columns of the first/last yanked char
5227 * that are to be yanked.
5228 */
5229 static void
5230block_prep(oap, bdp, lnum, is_del)
5231 oparg_T *oap;
5232 struct block_def *bdp;
5233 linenr_T lnum;
5234 int is_del;
5235{
5236 int incr = 0;
5237 char_u *pend;
5238 char_u *pstart;
5239 char_u *line;
5240 char_u *prev_pstart;
5241 char_u *prev_pend;
5242
5243 bdp->startspaces = 0;
5244 bdp->endspaces = 0;
5245 bdp->textlen = 0;
5246 bdp->start_vcol = 0;
5247 bdp->end_vcol = 0;
5248#ifdef FEAT_VISUALEXTRA
5249 bdp->is_short = FALSE;
5250 bdp->is_oneChar = FALSE;
5251 bdp->pre_whitesp = 0;
5252 bdp->pre_whitesp_c = 0;
5253 bdp->end_char_vcols = 0;
5254#endif
5255 bdp->start_char_vcols = 0;
5256
5257 line = ml_get(lnum);
5258 pstart = line;
5259 prev_pstart = line;
5260 while (bdp->start_vcol < oap->start_vcol && *pstart)
5261 {
5262 /* Count a tab for what it's worth (if list mode not on) */
5263 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
5264 bdp->start_vcol += incr;
5265#ifdef FEAT_VISUALEXTRA
5266 if (vim_iswhite(*pstart))
5267 {
5268 bdp->pre_whitesp += incr;
5269 bdp->pre_whitesp_c++;
5270 }
5271 else
5272 {
5273 bdp->pre_whitesp = 0;
5274 bdp->pre_whitesp_c = 0;
5275 }
5276#endif
5277 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005278 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 bdp->start_char_vcols = incr;
5281 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5282 {
5283 bdp->end_vcol = bdp->start_vcol;
5284#ifdef FEAT_VISUALEXTRA
5285 bdp->is_short = TRUE;
5286#endif
5287 if (!is_del || oap->op_type == OP_APPEND)
5288 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5289 }
5290 else
5291 {
5292 /* notice: this converts partly selected Multibyte characters to
5293 * spaces, too. */
5294 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5295 if (is_del && bdp->startspaces)
5296 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5297 pend = pstart;
5298 bdp->end_vcol = bdp->start_vcol;
5299 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5300 {
5301#ifdef FEAT_VISUALEXTRA
5302 bdp->is_oneChar = TRUE;
5303#endif
5304 if (oap->op_type == OP_INSERT)
5305 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5306 else if (oap->op_type == OP_APPEND)
5307 {
5308 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5309 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5310 }
5311 else
5312 {
5313 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5314 if (is_del && oap->op_type != OP_LSHIFT)
5315 {
5316 /* just putting the sum of those two into
5317 * bdp->startspaces doesn't work for Visual replace,
5318 * so we have to split the tab in two */
5319 bdp->startspaces = bdp->start_char_vcols
5320 - (bdp->start_vcol - oap->start_vcol);
5321 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5322 }
5323 }
5324 }
5325 else
5326 {
5327 prev_pend = pend;
5328 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5329 {
5330 /* Count a tab for what it's worth (if list mode not on) */
5331 prev_pend = pend;
5332 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
5333 bdp->end_vcol += incr;
5334 }
5335 if (bdp->end_vcol <= oap->end_vcol
5336 && (!is_del
5337 || oap->op_type == OP_APPEND
5338 || oap->op_type == OP_REPLACE)) /* line too short */
5339 {
5340#ifdef FEAT_VISUALEXTRA
5341 bdp->is_short = TRUE;
5342#endif
5343 /* Alternative: include spaces to fill up the block.
5344 * Disadvantage: can lead to trailing spaces when the line is
5345 * short where the text is put */
5346 /* if (!is_del || oap->op_type == OP_APPEND) */
5347 if (oap->op_type == OP_APPEND || virtual_op)
5348 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005349 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 else
5351 bdp->endspaces = 0; /* replace doesn't add characters */
5352 }
5353 else if (bdp->end_vcol > oap->end_vcol)
5354 {
5355 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5356 if (!is_del && bdp->endspaces)
5357 {
5358 bdp->endspaces = incr - bdp->endspaces;
5359 if (pend != pstart)
5360 pend = prev_pend;
5361 }
5362 }
5363 }
5364#ifdef FEAT_VISUALEXTRA
5365 bdp->end_char_vcols = incr;
5366#endif
5367 if (is_del && bdp->startspaces)
5368 pstart = prev_pstart;
5369 bdp->textlen = (int)(pend - pstart);
5370 }
5371 bdp->textcol = (colnr_T) (pstart - line);
5372 bdp->textstart = pstart;
5373}
5374#endif /* FEAT_VISUAL */
5375
5376#ifdef FEAT_RIGHTLEFT
5377static void reverse_line __ARGS((char_u *s));
5378
5379 static void
5380reverse_line(s)
5381 char_u *s;
5382{
5383 int i, j;
5384 char_u c;
5385
5386 if ((i = (int)STRLEN(s) - 1) <= 0)
5387 return;
5388
5389 curwin->w_cursor.col = i - curwin->w_cursor.col;
5390 for (j = 0; j < i; j++, i--)
5391 {
5392 c = s[i]; s[i] = s[j]; s[j] = c;
5393 }
5394}
5395
5396# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5397#else
5398# define RLADDSUBFIX(ptr)
5399#endif
5400
5401/*
5402 * add or subtract 'Prenum1' from a number in a line
5403 * 'command' is CTRL-A for add, CTRL-X for subtract
5404 *
5405 * return FAIL for failure, OK otherwise
5406 */
5407 int
5408do_addsub(command, Prenum1)
5409 int command;
5410 linenr_T Prenum1;
5411{
5412 int col;
5413 char_u *buf1;
5414 char_u buf2[NUMBUFLEN];
5415 int hex; /* 'X' or 'x': hex; '0': octal */
5416 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005417 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 long_u oldn;
5419 char_u *ptr;
5420 int c;
5421 int length = 0; /* character length of the number */
5422 int todel;
5423 int dohex;
5424 int dooct;
5425 int doalp;
5426 int firstdigit;
5427 int negative;
5428 int subtract;
5429
5430 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5431 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5432 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5433
5434 ptr = ml_get_curline();
5435 RLADDSUBFIX(ptr);
5436
5437 /*
5438 * First check if we are on a hexadecimal number, after the "0x".
5439 */
5440 col = curwin->w_cursor.col;
5441 if (dohex)
5442 while (col > 0 && vim_isxdigit(ptr[col]))
5443 --col;
5444 if ( dohex
5445 && col > 0
5446 && (ptr[col] == 'X'
5447 || ptr[col] == 'x')
5448 && ptr[col - 1] == '0'
5449 && vim_isxdigit(ptr[col + 1]))
5450 {
5451 /*
5452 * Found hexadecimal number, move to its start.
5453 */
5454 --col;
5455 }
5456 else
5457 {
5458 /*
5459 * Search forward and then backward to find the start of number.
5460 */
5461 col = curwin->w_cursor.col;
5462
5463 while (ptr[col] != NUL
5464 && !vim_isdigit(ptr[col])
5465 && !(doalp && ASCII_ISALPHA(ptr[col])))
5466 ++col;
5467
5468 while (col > 0
5469 && vim_isdigit(ptr[col - 1])
5470 && !(doalp && ASCII_ISALPHA(ptr[col])))
5471 --col;
5472 }
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 /*
5475 * If a number was found, and saving for undo works, replace the number.
5476 */
5477 firstdigit = ptr[col];
5478 RLADDSUBFIX(ptr);
5479 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5480 || u_save_cursor() != OK)
5481 {
5482 beep_flush();
5483 return FAIL;
5484 }
5485
5486 /* get ptr again, because u_save() may have changed it */
5487 ptr = ml_get_curline();
5488 RLADDSUBFIX(ptr);
5489
5490 if (doalp && ASCII_ISALPHA(firstdigit))
5491 {
5492 /* decrement or increment alphabetic character */
5493 if (command == Ctrl_X)
5494 {
5495 if (CharOrd(firstdigit) < Prenum1)
5496 {
5497 if (isupper(firstdigit))
5498 firstdigit = 'A';
5499 else
5500 firstdigit = 'a';
5501 }
5502 else
5503#ifdef EBCDIC
5504 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5505#else
5506 firstdigit -= Prenum1;
5507#endif
5508 }
5509 else
5510 {
5511 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5512 {
5513 if (isupper(firstdigit))
5514 firstdigit = 'Z';
5515 else
5516 firstdigit = 'z';
5517 }
5518 else
5519#ifdef EBCDIC
5520 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5521#else
5522 firstdigit += Prenum1;
5523#endif
5524 }
5525 curwin->w_cursor.col = col;
5526 (void)del_char(FALSE);
5527 ins_char(firstdigit);
5528 }
5529 else
5530 {
5531 negative = FALSE;
5532 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5533 {
5534 --col;
5535 negative = TRUE;
5536 }
5537
5538 /* get the number value (unsigned) */
5539 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5540
5541 /* ignore leading '-' for hex and octal numbers */
5542 if (hex && negative)
5543 {
5544 ++col;
5545 --length;
5546 negative = FALSE;
5547 }
5548
5549 /* add or subtract */
5550 subtract = FALSE;
5551 if (command == Ctrl_X)
5552 subtract ^= TRUE;
5553 if (negative)
5554 subtract ^= TRUE;
5555
5556 oldn = n;
5557 if (subtract)
5558 n -= (unsigned long)Prenum1;
5559 else
5560 n += (unsigned long)Prenum1;
5561
5562 /* handle wraparound for decimal numbers */
5563 if (!hex)
5564 {
5565 if (subtract)
5566 {
5567 if (n > oldn)
5568 {
5569 n = 1 + (n ^ (unsigned long)-1);
5570 negative ^= TRUE;
5571 }
5572 }
5573 else /* add */
5574 {
5575 if (n < oldn)
5576 {
5577 n = (n ^ (unsigned long)-1);
5578 negative ^= TRUE;
5579 }
5580 }
5581 if (n == 0)
5582 negative = FALSE;
5583 }
5584
5585 /*
5586 * Delete the old number.
5587 */
5588 curwin->w_cursor.col = col;
5589 todel = length;
5590 c = gchar_cursor();
5591 /*
5592 * Don't include the '-' in the length, only the length of the part
5593 * after it is kept the same.
5594 */
5595 if (c == '-')
5596 --length;
5597 while (todel-- > 0)
5598 {
5599 if (c < 0x100 && isalpha(c))
5600 {
5601 if (isupper(c))
5602 hexupper = TRUE;
5603 else
5604 hexupper = FALSE;
5605 }
5606 /* del_char() will mark line needing displaying */
5607 (void)del_char(FALSE);
5608 c = gchar_cursor();
5609 }
5610
5611 /*
5612 * Prepare the leading characters in buf1[].
5613 * When there are many leading zeros it could be very long. Allocate
5614 * a bit too much.
5615 */
5616 buf1 = alloc((unsigned)length + NUMBUFLEN);
5617 if (buf1 == NULL)
5618 return FAIL;
5619 ptr = buf1;
5620 if (negative)
5621 {
5622 *ptr++ = '-';
5623 }
5624 if (hex)
5625 {
5626 *ptr++ = '0';
5627 --length;
5628 }
5629 if (hex == 'x' || hex == 'X')
5630 {
5631 *ptr++ = hex;
5632 --length;
5633 }
5634
5635 /*
5636 * Put the number characters in buf2[].
5637 */
5638 if (hex == 0)
5639 sprintf((char *)buf2, "%lu", n);
5640 else if (hex == '0')
5641 sprintf((char *)buf2, "%lo", n);
5642 else if (hex && hexupper)
5643 sprintf((char *)buf2, "%lX", n);
5644 else
5645 sprintf((char *)buf2, "%lx", n);
5646 length -= (int)STRLEN(buf2);
5647
5648 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005649 * Adjust number of zeros to the new number of digits, so the
5650 * total length of the number remains the same.
5651 * Don't do this when
5652 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005654 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 while (length-- > 0)
5656 *ptr++ = '0';
5657 *ptr = NUL;
5658 STRCAT(buf1, buf2);
5659 ins_str(buf1); /* insert the new number */
5660 vim_free(buf1);
5661 }
5662 --curwin->w_cursor.col;
5663 curwin->w_set_curswant = TRUE;
5664#ifdef FEAT_RIGHTLEFT
5665 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5666 RLADDSUBFIX(ptr);
5667#endif
5668 return OK;
5669}
5670
5671#ifdef FEAT_VIMINFO
5672 int
5673read_viminfo_register(virp, force)
5674 vir_T *virp;
5675 int force;
5676{
5677 int eof;
5678 int do_it = TRUE;
5679 int size;
5680 int limit;
5681 int i;
5682 int set_prev = FALSE;
5683 char_u *str;
5684 char_u **array = NULL;
5685
5686 /* We only get here (hopefully) if line[0] == '"' */
5687 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005688
5689 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (*str == '"')
5691 {
5692 set_prev = TRUE;
5693 str++;
5694 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (!ASCII_ISALNUM(*str) && *str != '-')
5697 {
5698 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5699 return TRUE; /* too many errors, pretend end-of-file */
5700 do_it = FALSE;
5701 }
5702 get_yank_register(*str++, FALSE);
5703 if (!force && y_current->y_array != NULL)
5704 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005705
5706 if (*str == '@')
5707 {
5708 /* "x@: register x used for @@ */
5709 if (force || execreg_lastc == NUL)
5710 execreg_lastc = str[-1];
5711 }
5712
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 size = 0;
5714 limit = 100; /* Optimized for registers containing <= 100 lines */
5715 if (do_it)
5716 {
5717 if (set_prev)
5718 y_previous = y_current;
5719 vim_free(y_current->y_array);
5720 array = y_current->y_array =
5721 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005722 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (STRNCMP(str, "CHAR", 4) == 0)
5724 y_current->y_type = MCHAR;
5725#ifdef FEAT_VISUAL
5726 else if (STRNCMP(str, "BLOCK", 5) == 0)
5727 y_current->y_type = MBLOCK;
5728#endif
5729 else
5730 y_current->y_type = MLINE;
5731 /* get the block width; if it's missing we get a zero, which is OK */
5732 str = skipwhite(skiptowhite(str));
5733#ifdef FEAT_VISUAL
5734 y_current->y_width = getdigits(&str);
5735#else
5736 (void)getdigits(&str);
5737#endif
5738 }
5739
5740 while (!(eof = viminfo_readline(virp))
5741 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5742 {
5743 if (do_it)
5744 {
5745 if (size >= limit)
5746 {
5747 y_current->y_array = (char_u **)
5748 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5749 for (i = 0; i < limit; i++)
5750 y_current->y_array[i] = array[i];
5751 vim_free(array);
5752 limit *= 2;
5753 array = y_current->y_array;
5754 }
5755 str = viminfo_readstring(virp, 1, TRUE);
5756 if (str != NULL)
5757 array[size++] = str;
5758 else
5759 do_it = FALSE;
5760 }
5761 }
5762 if (do_it)
5763 {
5764 if (size == 0)
5765 {
5766 vim_free(array);
5767 y_current->y_array = NULL;
5768 }
5769 else if (size < limit)
5770 {
5771 y_current->y_array =
5772 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5773 for (i = 0; i < size; i++)
5774 y_current->y_array[i] = array[i];
5775 vim_free(array);
5776 }
5777 y_current->y_size = size;
5778 }
5779 return eof;
5780}
5781
5782 void
5783write_viminfo_registers(fp)
5784 FILE *fp;
5785{
5786 int i, j;
5787 char_u *type;
5788 char_u c;
5789 int num_lines;
5790 int max_num_lines;
5791 int max_kbyte;
5792 long len;
5793
Bram Moolenaar64404472010-06-26 06:24:45 +02005794 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796 /* Get '<' value, use old '"' value if '<' is not found. */
5797 max_num_lines = get_viminfo_parameter('<');
5798 if (max_num_lines < 0)
5799 max_num_lines = get_viminfo_parameter('"');
5800 if (max_num_lines == 0)
5801 return;
5802 max_kbyte = get_viminfo_parameter('s');
5803 if (max_kbyte == 0)
5804 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 for (i = 0; i < NUM_REGISTERS; i++)
5807 {
5808 if (y_regs[i].y_array == NULL)
5809 continue;
5810#ifdef FEAT_CLIPBOARD
5811 /* Skip '*'/'+' register, we don't want them back next time */
5812 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5813 continue;
5814#endif
5815#ifdef FEAT_DND
5816 /* Neither do we want the '~' register */
5817 if (i == TILDE_REGISTER)
5818 continue;
5819#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005820 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005822 if (num_lines == 0
5823 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005824 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005825 continue;
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 if (max_kbyte > 0)
5828 {
5829 /* Skip register if there is more text than the maximum size. */
5830 len = 0;
5831 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005832 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (len > (long)max_kbyte * 1024L)
5834 continue;
5835 }
5836
5837 switch (y_regs[i].y_type)
5838 {
5839 case MLINE:
5840 type = (char_u *)"LINE";
5841 break;
5842 case MCHAR:
5843 type = (char_u *)"CHAR";
5844 break;
5845#ifdef FEAT_VISUAL
5846 case MBLOCK:
5847 type = (char_u *)"BLOCK";
5848 break;
5849#endif
5850 default:
5851 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005852 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 emsg(IObuff);
5854 type = (char_u *)"LINE";
5855 break;
5856 }
5857 if (y_previous == &y_regs[i])
5858 fprintf(fp, "\"");
5859 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005860 fprintf(fp, "\"%c", c);
5861 if (c == execreg_lastc)
5862 fprintf(fp, "@");
5863 fprintf(fp, "\t%s\t%d\n", type,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#ifdef FEAT_VISUAL
5865 (int)y_regs[i].y_width
5866#else
5867 0
5868#endif
5869 );
5870
5871 /* If max_num_lines < 0, then we save ALL the lines in the register */
5872 if (max_num_lines > 0 && num_lines > max_num_lines)
5873 num_lines = max_num_lines;
5874 for (j = 0; j < num_lines; j++)
5875 {
5876 putc('\t', fp);
5877 viminfo_writestring(fp, y_regs[i].y_array[j]);
5878 }
5879 }
5880}
5881#endif /* FEAT_VIMINFO */
5882
5883#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5884/*
5885 * SELECTION / PRIMARY ('*')
5886 *
5887 * Text selection stuff that uses the GUI selection register '*'. When using a
5888 * GUI this may be text from another window, otherwise it is the last text we
5889 * had highlighted with VIsual mode. With mouse support, clicking the middle
5890 * button performs the paste, otherwise you will need to do <"*p>. "
5891 * If not under X, it is synonymous with the clipboard register '+'.
5892 *
5893 * X CLIPBOARD ('+')
5894 *
5895 * Text selection stuff that uses the GUI clipboard register '+'.
5896 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5897 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5898 * otherwise you will need to do <"+p>. "
5899 * If not under X, it is synonymous with the selection register '*'.
5900 */
5901
5902/*
5903 * Routine to export any final X selection we had to the environment
5904 * so that the text is still available after vim has exited. X selections
5905 * only exist while the owning application exists, so we write to the
5906 * permanent (while X runs) store CUT_BUFFER0.
5907 * Dump the CLIPBOARD selection if we own it (it's logically the more
5908 * 'permanent' of the two), otherwise the PRIMARY one.
5909 * For now, use a hard-coded sanity limit of 1Mb of data.
5910 */
5911#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5912 void
5913x11_export_final_selection()
5914{
5915 Display *dpy;
5916 char_u *str = NULL;
5917 long_u len = 0;
5918 int motion_type = -1;
5919
5920# ifdef FEAT_GUI
5921 if (gui.in_use)
5922 dpy = X_DISPLAY;
5923 else
5924# endif
5925# ifdef FEAT_XCLIPBOARD
5926 dpy = xterm_dpy;
5927# else
5928 return;
5929# endif
5930
5931 /* Get selection to export */
5932 if (clip_plus.owned)
5933 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5934 else if (clip_star.owned)
5935 motion_type = clip_convert_selection(&str, &len, &clip_star);
5936
5937 /* Check it's OK */
5938 if (dpy != NULL && str != NULL && motion_type >= 0
5939 && len < 1024*1024 && len > 0)
5940 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005941#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005942 int ok = TRUE;
5943
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005944 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5945 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5946 * encoding conversion usually doesn't work, so keep the text as-is.
5947 */
5948 if (has_mbyte)
5949 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005950 vimconv_T vc;
5951
5952 vc.vc_type = CONV_NONE;
5953 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5954 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005955 int intlen = len;
5956 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005957
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005958 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005959 conv_str = string_convert(&vc, str, &intlen);
5960 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005961 if (conv_str != NULL)
5962 {
5963 vim_free(str);
5964 str = conv_str;
5965 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005966 else
5967 {
5968 ok = FALSE;
5969 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005970 convert_setup(&vc, NULL, NULL);
5971 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005972 else
5973 {
5974 ok = FALSE;
5975 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005976 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005977
5978 /* Do not store the string if conversion failed. Better to use any
5979 * other selection than garbled text. */
5980 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005981#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005982 {
5983 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5984 XFlush(dpy);
5985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 }
5987
5988 vim_free(str);
5989}
5990#endif
5991
5992 void
5993clip_free_selection(cbd)
5994 VimClipboard *cbd;
5995{
5996 struct yankreg *y_ptr = y_current;
5997
5998 if (cbd == &clip_plus)
5999 y_current = &y_regs[PLUS_REGISTER];
6000 else
6001 y_current = &y_regs[STAR_REGISTER];
6002 free_yank_all();
6003 y_current->y_size = 0;
6004 y_current = y_ptr;
6005}
6006
6007/*
6008 * Get the selected text and put it in the gui selection register '*' or '+'.
6009 */
6010 void
6011clip_get_selection(cbd)
6012 VimClipboard *cbd;
6013{
6014 struct yankreg *old_y_previous, *old_y_current;
6015 pos_T old_cursor;
6016#ifdef FEAT_VISUAL
6017 pos_T old_visual;
6018 int old_visual_mode;
6019#endif
6020 colnr_T old_curswant;
6021 int old_set_curswant;
6022 pos_T old_op_start, old_op_end;
6023 oparg_T oa;
6024 cmdarg_T ca;
6025
6026 if (cbd->owned)
6027 {
6028 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6029 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6030 return;
6031
6032 /* Get the text between clip_star.start & clip_star.end */
6033 old_y_previous = y_previous;
6034 old_y_current = y_current;
6035 old_cursor = curwin->w_cursor;
6036 old_curswant = curwin->w_curswant;
6037 old_set_curswant = curwin->w_set_curswant;
6038 old_op_start = curbuf->b_op_start;
6039 old_op_end = curbuf->b_op_end;
6040#ifdef FEAT_VISUAL
6041 old_visual = VIsual;
6042 old_visual_mode = VIsual_mode;
6043#endif
6044 clear_oparg(&oa);
6045 oa.regname = (cbd == &clip_plus ? '+' : '*');
6046 oa.op_type = OP_YANK;
6047 vim_memset(&ca, 0, sizeof(ca));
6048 ca.oap = &oa;
6049 ca.cmdchar = 'y';
6050 ca.count1 = 1;
6051 ca.retval = CA_NO_ADJ_OP_END;
6052 do_pending_operator(&ca, 0, TRUE);
6053 y_previous = old_y_previous;
6054 y_current = old_y_current;
6055 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006056 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 curwin->w_curswant = old_curswant;
6058 curwin->w_set_curswant = old_set_curswant;
6059 curbuf->b_op_start = old_op_start;
6060 curbuf->b_op_end = old_op_end;
6061#ifdef FEAT_VISUAL
6062 VIsual = old_visual;
6063 VIsual_mode = old_visual_mode;
6064#endif
6065 }
6066 else
6067 {
6068 clip_free_selection(cbd);
6069
6070 /* Try to get selected text from another window */
6071 clip_gen_request_selection(cbd);
6072 }
6073}
6074
Bram Moolenaard44347f2011-06-19 01:14:29 +02006075/*
6076 * Convert from the GUI selection string into the '*'/'+' register.
6077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 void
6079clip_yank_selection(type, str, len, cbd)
6080 int type;
6081 char_u *str;
6082 long len;
6083 VimClipboard *cbd;
6084{
6085 struct yankreg *y_ptr;
6086
6087 if (cbd == &clip_plus)
6088 y_ptr = &y_regs[PLUS_REGISTER];
6089 else
6090 y_ptr = &y_regs[STAR_REGISTER];
6091
6092 clip_free_selection(cbd);
6093
6094 str_to_reg(y_ptr, type, str, len, 0L);
6095}
6096
6097/*
6098 * Convert the '*'/'+' register into a GUI selection string returned in *str
6099 * with length *len.
6100 * Returns the motion type, or -1 for failure.
6101 */
6102 int
6103clip_convert_selection(str, len, cbd)
6104 char_u **str;
6105 long_u *len;
6106 VimClipboard *cbd;
6107{
6108 char_u *p;
6109 int lnum;
6110 int i, j;
6111 int_u eolsize;
6112 struct yankreg *y_ptr;
6113
6114 if (cbd == &clip_plus)
6115 y_ptr = &y_regs[PLUS_REGISTER];
6116 else
6117 y_ptr = &y_regs[STAR_REGISTER];
6118
6119#ifdef USE_CRNL
6120 eolsize = 2;
6121#else
6122 eolsize = 1;
6123#endif
6124
6125 *str = NULL;
6126 *len = 0;
6127 if (y_ptr->y_array == NULL)
6128 return -1;
6129
6130 for (i = 0; i < y_ptr->y_size; i++)
6131 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6132
6133 /*
6134 * Don't want newline character at end of last line if we're in MCHAR mode.
6135 */
6136 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6137 *len -= eolsize;
6138
6139 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6140 if (p == NULL)
6141 return -1;
6142 lnum = 0;
6143 for (i = 0, j = 0; i < (int)*len; i++, j++)
6144 {
6145 if (y_ptr->y_array[lnum][j] == '\n')
6146 p[i] = NUL;
6147 else if (y_ptr->y_array[lnum][j] == NUL)
6148 {
6149#ifdef USE_CRNL
6150 p[i++] = '\r';
6151#endif
6152#ifdef USE_CR
6153 p[i] = '\r';
6154#else
6155 p[i] = '\n';
6156#endif
6157 lnum++;
6158 j = -1;
6159 }
6160 else
6161 p[i] = y_ptr->y_array[lnum][j];
6162 }
6163 return y_ptr->y_type;
6164}
6165
6166
6167# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
6168/*
6169 * If we have written to a clipboard register, send the text to the clipboard.
6170 */
6171 static void
6172may_set_selection()
6173{
6174 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6175 {
6176 clip_own_selection(&clip_star);
6177 clip_gen_set_selection(&clip_star);
6178 }
6179 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6180 {
6181 clip_own_selection(&clip_plus);
6182 clip_gen_set_selection(&clip_plus);
6183 }
6184}
6185# endif
6186
6187#endif /* FEAT_CLIPBOARD || PROTO */
6188
6189
6190#if defined(FEAT_DND) || defined(PROTO)
6191/*
6192 * Replace the contents of the '~' register with str.
6193 */
6194 void
6195dnd_yank_drag_data(str, len)
6196 char_u *str;
6197 long len;
6198{
6199 struct yankreg *curr;
6200
6201 curr = y_current;
6202 y_current = &y_regs[TILDE_REGISTER];
6203 free_yank_all();
6204 str_to_reg(y_current, MCHAR, str, len, 0L);
6205 y_current = curr;
6206}
6207#endif
6208
6209
6210#if defined(FEAT_EVAL) || defined(PROTO)
6211/*
6212 * Return the type of a register.
6213 * Used for getregtype()
6214 * Returns MAUTO for error.
6215 */
6216 char_u
6217get_reg_type(regname, reglen)
6218 int regname;
6219 long *reglen;
6220{
6221 switch (regname)
6222 {
6223 case '%': /* file name */
6224 case '#': /* alternate file name */
6225 case '=': /* expression */
6226 case ':': /* last command line */
6227 case '/': /* last search-pattern */
6228 case '.': /* last inserted text */
6229#ifdef FEAT_SEARCHPATH
6230 case Ctrl_F: /* Filename under cursor */
6231 case Ctrl_P: /* Path under cursor, expand via "path" */
6232#endif
6233 case Ctrl_W: /* word under cursor */
6234 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6235 case '_': /* black hole: always empty */
6236 return MCHAR;
6237 }
6238
6239#ifdef FEAT_CLIPBOARD
6240 regname = may_get_selection(regname);
6241#endif
6242
6243 /* Should we check for a valid name? */
6244 get_yank_register(regname, FALSE);
6245
6246 if (y_current->y_array != NULL)
6247 {
6248#ifdef FEAT_VISUAL
6249 if (reglen != NULL && y_current->y_type == MBLOCK)
6250 *reglen = y_current->y_width;
6251#endif
6252 return y_current->y_type;
6253 }
6254 return MAUTO;
6255}
6256
6257/*
6258 * Return the contents of a register as a single allocated string.
6259 * Used for "@r" in expressions and for getreg().
6260 * Returns NULL for error.
6261 */
6262 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00006263get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00006265 int allowexpr; /* allow "=" register */
6266 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
6268 long i;
6269 char_u *retval;
6270 int allocated;
6271 long len;
6272
6273 /* Don't allow using an expression register inside an expression */
6274 if (regname == '=')
6275 {
6276 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00006277 {
6278 if (expr_src)
6279 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00006281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 return NULL;
6283 }
6284
6285 if (regname == '@') /* "@@" is used for unnamed register */
6286 regname = '"';
6287
6288 /* check for valid regname */
6289 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6290 return NULL;
6291
6292#ifdef FEAT_CLIPBOARD
6293 regname = may_get_selection(regname);
6294#endif
6295
6296 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6297 {
6298 if (retval == NULL)
6299 return NULL;
6300 if (!allocated)
6301 retval = vim_strsave(retval);
6302 return retval;
6303 }
6304
6305 get_yank_register(regname, FALSE);
6306 if (y_current->y_array == NULL)
6307 return NULL;
6308
6309 /*
6310 * Compute length of resulting string.
6311 */
6312 len = 0;
6313 for (i = 0; i < y_current->y_size; ++i)
6314 {
6315 len += (long)STRLEN(y_current->y_array[i]);
6316 /*
6317 * Insert a newline between lines and after last line if
6318 * y_type is MLINE.
6319 */
6320 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6321 ++len;
6322 }
6323
6324 retval = lalloc(len + 1, TRUE);
6325
6326 /*
6327 * Copy the lines of the yank register into the string.
6328 */
6329 if (retval != NULL)
6330 {
6331 len = 0;
6332 for (i = 0; i < y_current->y_size; ++i)
6333 {
6334 STRCPY(retval + len, y_current->y_array[i]);
6335 len += (long)STRLEN(retval + len);
6336
6337 /*
6338 * Insert a NL between lines and after the last line if y_type is
6339 * MLINE.
6340 */
6341 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6342 retval[len++] = '\n';
6343 }
6344 retval[len] = NUL;
6345 }
6346
6347 return retval;
6348}
6349
6350/*
6351 * Store string "str" in register "name".
6352 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6353 * If "must_append" is TRUE, always append to the register. Otherwise append
6354 * if "name" is an uppercase letter.
6355 * Note: "maxlen" and "must_append" don't work for the "/" register.
6356 * Careful: 'str' is modified, you may have to use a copy!
6357 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6358 */
6359 void
6360write_reg_contents(name, str, maxlen, must_append)
6361 int name;
6362 char_u *str;
6363 int maxlen;
6364 int must_append;
6365{
6366 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6367}
6368
6369 void
6370write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6371 int name;
6372 char_u *str;
6373 int maxlen;
6374 int must_append;
6375 int yank_type;
6376 long block_len;
6377{
6378 struct yankreg *old_y_previous, *old_y_current;
6379 long len;
6380
Bram Moolenaare7566042005-06-17 22:00:15 +00006381 if (maxlen >= 0)
6382 len = maxlen;
6383 else
6384 len = (long)STRLEN(str);
6385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 /* Special case: '/' search pattern */
6387 if (name == '/')
6388 {
6389 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6390 return;
6391 }
6392
Bram Moolenaare7566042005-06-17 22:00:15 +00006393#ifdef FEAT_EVAL
6394 if (name == '=')
6395 {
6396 char_u *p, *s;
6397
6398 p = vim_strnsave(str, (int)len);
6399 if (p == NULL)
6400 return;
6401 if (must_append)
6402 {
6403 s = concat_str(get_expr_line_src(), p);
6404 vim_free(p);
6405 p = s;
6406
6407 }
6408 set_expr_line(p);
6409 return;
6410 }
6411#endif
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6414 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006415 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 return;
6417 }
6418
6419 if (name == '_') /* black hole: nothing to do */
6420 return;
6421
6422 /* Don't want to change the current (unnamed) register */
6423 old_y_previous = y_previous;
6424 old_y_current = y_current;
6425
6426 get_yank_register(name, TRUE);
6427 if (!y_append && !must_append)
6428 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429#ifndef FEAT_VISUAL
6430 /* Just in case - make sure we don't use MBLOCK */
6431 if (yank_type == MBLOCK)
6432 yank_type = MAUTO;
6433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 str_to_reg(y_current, yank_type, str, len, block_len);
6435
6436# ifdef FEAT_CLIPBOARD
6437 /* Send text of clipboard register to the clipboard. */
6438 may_set_selection();
6439# endif
6440
6441 /* ':let @" = "val"' should change the meaning of the "" register */
6442 if (name != '"')
6443 y_previous = old_y_previous;
6444 y_current = old_y_current;
6445}
6446#endif /* FEAT_EVAL */
6447
6448#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6449/*
6450 * Put a string into a register. When the register is not empty, the string
6451 * is appended.
6452 */
6453 static void
Bram Moolenaard44347f2011-06-19 01:14:29 +02006454str_to_reg(y_ptr, yank_type, str, len, blocklen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006456 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 char_u *str; /* string to put in register */
6458 long len; /* length of string */
6459 long blocklen; /* width of Visual block */
6460{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006461 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 int lnum;
6463 long start;
6464 long i;
6465 int extra;
6466 int newlines; /* number of lines added */
6467 int extraline = 0; /* extra line at the end */
6468 int append = FALSE; /* append to last line in register */
6469 char_u *s;
6470 char_u **pp;
6471#ifdef FEAT_VISUAL
6472 long maxlen;
6473#endif
6474
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006475 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 y_ptr->y_size = 0;
6477
Bram Moolenaard44347f2011-06-19 01:14:29 +02006478 if (yank_type == MAUTO)
6479 type = ((len > 0 && (str[len - 1] == NL || str[len - 1] == CAR))
6480 ? MLINE : MCHAR);
6481 else
6482 type = yank_type;
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 /*
6485 * Count the number of lines within the string
6486 */
6487 newlines = 0;
6488 for (i = 0; i < len; i++)
6489 if (str[i] == '\n')
6490 ++newlines;
6491 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6492 {
6493 extraline = 1;
6494 ++newlines; /* count extra newline at the end */
6495 }
6496 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6497 {
6498 append = TRUE;
6499 --newlines; /* uncount newline when appending first line */
6500 }
6501
6502 /*
6503 * Allocate an array to hold the pointers to the new register lines.
6504 * If the register was not empty, move the existing lines to the new array.
6505 */
6506 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6507 * sizeof(char_u *), TRUE);
6508 if (pp == NULL) /* out of memory */
6509 return;
6510 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6511 pp[lnum] = y_ptr->y_array[lnum];
6512 vim_free(y_ptr->y_array);
6513 y_ptr->y_array = pp;
6514#ifdef FEAT_VISUAL
6515 maxlen = 0;
6516#endif
6517
6518 /*
6519 * Find the end of each line and save it into the array.
6520 */
6521 for (start = 0; start < len + extraline; start += i + 1)
6522 {
6523 for (i = start; i < len; ++i) /* find the end of the line */
6524 if (str[i] == '\n')
6525 break;
6526 i -= start; /* i is now length of line */
6527#ifdef FEAT_VISUAL
6528 if (i > maxlen)
6529 maxlen = i;
6530#endif
6531 if (append)
6532 {
6533 --lnum;
6534 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6535 }
6536 else
6537 extra = 0;
6538 s = alloc((unsigned)(i + extra + 1));
6539 if (s == NULL)
6540 break;
6541 if (extra)
6542 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6543 if (append)
6544 vim_free(y_ptr->y_array[lnum]);
6545 if (i)
6546 mch_memmove(s + extra, str + start, (size_t)i);
6547 extra += i;
6548 s[extra] = NUL;
6549 y_ptr->y_array[lnum++] = s;
6550 while (--extra >= 0)
6551 {
6552 if (*s == NUL)
6553 *s = '\n'; /* replace NUL with newline */
6554 ++s;
6555 }
6556 append = FALSE; /* only first line is appended */
6557 }
6558 y_ptr->y_type = type;
6559 y_ptr->y_size = lnum;
6560# ifdef FEAT_VISUAL
6561 if (type == MBLOCK)
6562 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6563 else
6564 y_ptr->y_width = 0;
6565# endif
6566}
6567#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6568
6569 void
6570clear_oparg(oap)
6571 oparg_T *oap;
6572{
6573 vim_memset(oap, 0, sizeof(oparg_T));
6574}
6575
Bram Moolenaar7c626922005-02-07 22:01:03 +00006576static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006579 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 *
6581 * "Words" are counted by looking for boundaries between non-space and
6582 * space characters. (it seems to produce results that match 'wc'.)
6583 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006584 * Return value is byte count; word count for the line is added to "*wc".
6585 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 *
6587 * The function will only examine the first "limit" characters in the
6588 * line, stopping if it encounters an end-of-line (NUL byte). In that
6589 * case, eol_size will be added to the character count to account for
6590 * the size of the EOL character.
6591 */
6592 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006593line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 char_u *line;
6595 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006596 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 long limit;
6598 int eol_size;
6599{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006600 long i;
6601 long words = 0;
6602 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 int is_word = 0;
6604
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006605 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 {
6607 if (is_word)
6608 {
6609 if (vim_isspace(line[i]))
6610 {
6611 words++;
6612 is_word = 0;
6613 }
6614 }
6615 else if (!vim_isspace(line[i]))
6616 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006617 ++chars;
6618#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006619 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006620#else
6621 ++i;
6622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 }
6624
6625 if (is_word)
6626 words++;
6627 *wc += words;
6628
6629 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006630 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006633 chars += eol_size;
6634 }
6635 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 return i;
6637}
6638
6639/*
6640 * Give some info about the position of the cursor (for "g CTRL-G").
6641 * In Visual mode, give some info about the selected region. (In this case,
6642 * the *_count_cursor variables store running totals for the selection.)
6643 */
6644 void
6645cursor_pos_info()
6646{
6647 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006648 char_u buf1[50];
6649 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006651 long byte_count = 0;
6652 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 long char_count = 0;
6654 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 long word_count = 0;
6656 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006657 int eol_size;
6658 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659#ifdef FEAT_VISUAL
6660 long line_count_selected = 0;
6661 pos_T min_pos, max_pos;
6662 oparg_T oparg;
6663 struct block_def bd;
6664#endif
6665
6666 /*
6667 * Compute the length of the file in characters.
6668 */
6669 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6670 {
6671 MSG(_(no_lines_msg));
6672 }
6673 else
6674 {
6675 if (get_fileformat(curbuf) == EOL_DOS)
6676 eol_size = 2;
6677 else
6678 eol_size = 1;
6679
6680#ifdef FEAT_VISUAL
6681 if (VIsual_active)
6682 {
6683 if (lt(VIsual, curwin->w_cursor))
6684 {
6685 min_pos = VIsual;
6686 max_pos = curwin->w_cursor;
6687 }
6688 else
6689 {
6690 min_pos = curwin->w_cursor;
6691 max_pos = VIsual;
6692 }
6693 if (*p_sel == 'e' && max_pos.col > 0)
6694 --max_pos.col;
6695
6696 if (VIsual_mode == Ctrl_V)
6697 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006698#ifdef FEAT_LINEBREAK
6699 char_u * saved_sbr = p_sbr;
6700
6701 /* Make 'sbr' empty for a moment to get the correct size. */
6702 p_sbr = empty_option;
6703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 oparg.is_VIsual = 1;
6705 oparg.block_mode = TRUE;
6706 oparg.op_type = OP_NOP;
6707 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006708 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006709#ifdef FEAT_LINEBREAK
6710 p_sbr = saved_sbr;
6711#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006712 if (curwin->w_curswant == MAXCOL)
6713 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 /* Swap the start, end vcol if needed */
6715 if (oparg.end_vcol < oparg.start_vcol)
6716 {
6717 oparg.end_vcol += oparg.start_vcol;
6718 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6719 oparg.end_vcol -= oparg.start_vcol;
6720 }
6721 }
6722 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6723 }
6724#endif
6725
6726 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6727 {
6728 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006729 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
6731 ui_breakcheck();
6732 if (got_int)
6733 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006734 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 }
6736
6737#ifdef FEAT_VISUAL
6738 /* Do extra processing for VIsual mode. */
6739 if (VIsual_active
6740 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6741 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006742 char_u *s = NULL;
6743 long len = 0L;
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 switch (VIsual_mode)
6746 {
6747 case Ctrl_V:
6748# ifdef FEAT_VIRTUALEDIT
6749 virtual_op = virtual_active();
6750# endif
6751 block_prep(&oparg, &bd, lnum, 0);
6752# ifdef FEAT_VIRTUALEDIT
6753 virtual_op = MAYBE;
6754# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006755 s = bd.textstart;
6756 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 break;
6758 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006759 s = ml_get(lnum);
6760 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 break;
6762 case 'v':
6763 {
6764 colnr_T start_col = (lnum == min_pos.lnum)
6765 ? min_pos.col : 0;
6766 colnr_T end_col = (lnum == max_pos.lnum)
6767 ? max_pos.col - start_col + 1 : MAXCOL;
6768
Bram Moolenaardef9e822004-12-31 20:58:58 +00006769 s = ml_get(lnum) + start_col;
6770 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772 break;
6773 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006774 if (s != NULL)
6775 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006776 byte_count_cursor += line_count_info(s, &word_count_cursor,
6777 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006778 if (lnum == curbuf->b_ml.ml_line_count
6779 && !curbuf->b_p_eol
6780 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006781 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006782 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 }
6785 else
6786#endif
6787 {
6788 /* In non-visual mode, check for the line the cursor is on */
6789 if (lnum == curwin->w_cursor.lnum)
6790 {
6791 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006792 char_count_cursor += char_count;
6793 byte_count_cursor = byte_count +
6794 line_count_info(ml_get(lnum),
6795 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 (long)(curwin->w_cursor.col + 1), eol_size);
6797 }
6798 }
6799 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006800 byte_count += line_count_info(ml_get(lnum), &word_count,
6801 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803
6804 /* Correction for when last line doesn't have an EOL. */
6805 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006806 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
6808#ifdef FEAT_VISUAL
6809 if (VIsual_active)
6810 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006811 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
6813 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006814 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006815 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6817 }
6818 else
6819 buf1[0] = NUL;
6820
Bram Moolenaar7c626922005-02-07 22:01:03 +00006821 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006822 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006823 vim_snprintf((char *)IObuff, IOSIZE,
6824 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 buf1, line_count_selected,
6826 (long)curbuf->b_ml.ml_line_count,
6827 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006828 byte_count_cursor, byte_count);
6829 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006830 vim_snprintf((char *)IObuff, IOSIZE,
6831 _("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 +00006832 buf1, line_count_selected,
6833 (long)curbuf->b_ml.ml_line_count,
6834 word_count_cursor, word_count,
6835 char_count_cursor, char_count,
6836 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838 else
6839#endif
6840 {
6841 p = ml_get_curline();
6842 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006843 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 (int)curwin->w_virtcol + 1);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006845 col_print(buf2, sizeof(buf2), (int)STRLEN(p), linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846
Bram Moolenaar7c626922005-02-07 22:01:03 +00006847 if (char_count_cursor == byte_count_cursor
6848 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006849 vim_snprintf((char *)IObuff, IOSIZE,
6850 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 (char *)buf1, (char *)buf2,
6852 (long)curwin->w_cursor.lnum,
6853 (long)curbuf->b_ml.ml_line_count,
6854 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006855 byte_count_cursor, byte_count);
6856 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006857 vim_snprintf((char *)IObuff, IOSIZE,
6858 _("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 +00006859 (char *)buf1, (char *)buf2,
6860 (long)curwin->w_cursor.lnum,
6861 (long)curbuf->b_ml.ml_line_count,
6862 word_count_cursor, word_count,
6863 char_count_cursor, char_count,
6864 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866
6867#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006868 byte_count = bomb_size();
6869 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006871 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872#endif
6873 /* Don't shorten this message, the user asked for it. */
6874 p = p_shm;
6875 p_shm = (char_u *)"";
6876 msg(IObuff);
6877 p_shm = p;
6878 }
6879}