blob: ab3d636cdc6f3d44029da17bd7c939762801dece [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
53 * Each yank register is an array of pointers to lines.
54 */
55static struct yankreg
56{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
60#ifdef FEAT_VISUAL
61 colnr_T y_width; /* only set if y_type == MBLOCK */
62#endif
63} y_regs[NUM_REGISTERS];
64
65static struct yankreg *y_current; /* ptr to current yankreg */
66static int y_append; /* TRUE when appending */
67static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
68
69/*
70 * structure used by block_prep, op_delete and op_yank for blockwise operators
71 * also op_change, op_shift, op_insert, op_replace - AKelly
72 */
73struct block_def
74{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000075 int startspaces; /* 'extra' cols before first char */
76 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000078 char_u *textstart; /* pointer to 1st char (partially) in block */
79 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 colnr_T start_vcol; /* start col of 1st char wholly inside block */
81 colnr_T end_vcol; /* start col of 1st char wholly after block */
82#ifdef FEAT_VISUALEXTRA
83 int is_short; /* TRUE if line is too short to fit in block */
84 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
85 int is_oneChar; /* TRUE if block within one character */
86 int pre_whitesp; /* screen cols of ws before block */
87 int pre_whitesp_c; /* chars of ws before block */
88 colnr_T end_char_vcols; /* number of vcols of post-block char */
89#endif
90 colnr_T start_char_vcols; /* number of vcols of pre-block char */
91};
92
93#ifdef FEAT_VISUALEXTRA
94static void shift_block __ARGS((oparg_T *oap, int amount));
95static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
96#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000097static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000098static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000099static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
100 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#ifdef FEAT_MBYTE
103static void mb_adjust_opend __ARGS((oparg_T *oap));
104#endif
105static void free_yank __ARGS((long));
106static void free_yank_all __ARGS((void));
107static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
108#ifdef FEAT_CLIPBOARD
109static void copy_yank_reg __ARGS((struct yankreg *reg));
110# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
111static void may_set_selection __ARGS((void));
112# endif
113#endif
114static void dis_msg __ARGS((char_u *p, int skip_esc));
Bram Moolenaar81340392012-06-06 16:12:59 +0200115#if defined(FEAT_COMMENTS) || defined(PROTO)
116static char_u *skip_comment __ARGS((char_u *line, int process, int include_space, int *is_comment));
117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_VISUAL
119static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
120#endif
121#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
122static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
123#endif
124static int ends_in_white __ARGS((linenr_T lnum));
125#ifdef FEAT_COMMENTS
126static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
127static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
128#else
129static int fmt_check_par __ARGS((linenr_T));
130#endif
131
132/*
133 * The names of operators.
134 * IMPORTANT: Index must correspond with defines in vim.h!!!
135 * The third field indicates whether the operator always works on lines.
136 */
137static char opchars[][3] =
138{
139 {NUL, NUL, FALSE}, /* OP_NOP */
140 {'d', NUL, FALSE}, /* OP_DELETE */
141 {'y', NUL, FALSE}, /* OP_YANK */
142 {'c', NUL, FALSE}, /* OP_CHANGE */
143 {'<', NUL, TRUE}, /* OP_LSHIFT */
144 {'>', NUL, TRUE}, /* OP_RSHIFT */
145 {'!', NUL, TRUE}, /* OP_FILTER */
146 {'g', '~', FALSE}, /* OP_TILDE */
147 {'=', NUL, TRUE}, /* OP_INDENT */
148 {'g', 'q', TRUE}, /* OP_FORMAT */
149 {':', NUL, TRUE}, /* OP_COLON */
150 {'g', 'U', FALSE}, /* OP_UPPER */
151 {'g', 'u', FALSE}, /* OP_LOWER */
152 {'J', NUL, TRUE}, /* DO_JOIN */
153 {'g', 'J', TRUE}, /* DO_JOIN_NS */
154 {'g', '?', FALSE}, /* OP_ROT13 */
155 {'r', NUL, FALSE}, /* OP_REPLACE */
156 {'I', NUL, FALSE}, /* OP_INSERT */
157 {'A', NUL, FALSE}, /* OP_APPEND */
158 {'z', 'f', TRUE}, /* OP_FOLD */
159 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
160 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
161 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
162 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
163 {'z', 'd', TRUE}, /* OP_FOLDDEL */
164 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
165 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000166 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167};
168
169/*
170 * Translate a command name into an operator type.
171 * Must only be called with a valid operator name!
172 */
173 int
174get_op_type(char1, char2)
175 int char1;
176 int char2;
177{
178 int i;
179
180 if (char1 == 'r') /* ignore second character */
181 return OP_REPLACE;
182 if (char1 == '~') /* when tilde is an operator */
183 return OP_TILDE;
184 for (i = 0; ; ++i)
185 if (opchars[i][0] == char1 && opchars[i][1] == char2)
186 break;
187 return i;
188}
189
190#if defined(FEAT_VISUAL) || defined(PROTO)
191/*
192 * Return TRUE if operator "op" always works on whole lines.
193 */
194 int
195op_on_lines(op)
196 int op;
197{
198 return opchars[op][2];
199}
200#endif
201
202/*
203 * Get first operator command character.
204 * Returns 'g' or 'z' if there is another command character.
205 */
206 int
207get_op_char(optype)
208 int optype;
209{
210 return opchars[optype][0];
211}
212
213/*
214 * Get second operator command character.
215 */
216 int
217get_extra_op_char(optype)
218 int optype;
219{
220 return opchars[optype][1];
221}
222
223/*
224 * op_shift - handle a shift operation
225 */
226 void
227op_shift(oap, curs_top, amount)
228 oparg_T *oap;
229 int curs_top;
230 int amount;
231{
232 long i;
233 int first_char;
234 char_u *s;
235#ifdef FEAT_VISUAL
236 int block_col = 0;
237#endif
238
239 if (u_save((linenr_T)(oap->start.lnum - 1),
240 (linenr_T)(oap->end.lnum + 1)) == FAIL)
241 return;
242
243#ifdef FEAT_VISUAL
244 if (oap->block_mode)
245 block_col = curwin->w_cursor.col;
246#endif
247
248 for (i = oap->line_count; --i >= 0; )
249 {
250 first_char = *ml_get_curline();
251 if (first_char == NUL) /* empty line */
252 curwin->w_cursor.col = 0;
253#ifdef FEAT_VISUALEXTRA
254 else if (oap->block_mode)
255 shift_block(oap, amount);
256#endif
257 else
258 /* Move the line right if it doesn't start with '#', 'smartindent'
259 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
260#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
261 if (first_char != '#' || !preprocs_left())
262#endif
263 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000264 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 ++curwin->w_cursor.lnum;
267 }
268
269 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
270
271#ifdef FEAT_VISUAL
272 if (oap->block_mode)
273 {
274 curwin->w_cursor.lnum = oap->start.lnum;
275 curwin->w_cursor.col = block_col;
276 }
277 else
278#endif
279 if (curs_top) /* put cursor on first line, for ">>" */
280 {
281 curwin->w_cursor.lnum = oap->start.lnum;
282 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
283 }
284 else
285 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
286
287 if (oap->line_count > p_report)
288 {
289 if (oap->op_type == OP_RSHIFT)
290 s = (char_u *)">";
291 else
292 s = (char_u *)"<";
293 if (oap->line_count == 1)
294 {
295 if (amount == 1)
296 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
297 else
298 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
299 }
300 else
301 {
302 if (amount == 1)
303 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
304 oap->line_count, s);
305 else
306 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
307 oap->line_count, s, amount);
308 }
309 msg(IObuff);
310 }
311
312 /*
313 * Set "'[" and "']" marks.
314 */
315 curbuf->b_op_start = oap->start;
316 curbuf->b_op_end.lnum = oap->end.lnum;
317 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
318 if (curbuf->b_op_end.col > 0)
319 --curbuf->b_op_end.col;
320}
321
322/*
323 * shift the current line one shiftwidth left (if left != 0) or right
324 * leaves cursor on first blank in the line
325 */
326 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000327shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 int left;
329 int round;
330 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000331 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332{
333 int count;
334 int i, j;
Bram Moolenaar14f24742012-08-08 18:01:05 +0200335 int p_sw = (int)get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
337 count = get_indent(); /* get current indent */
338
339 if (round) /* round off indent */
340 {
341 i = count / p_sw; /* number of p_sw rounded down */
342 j = count % p_sw; /* extra spaces */
343 if (j && left) /* first remove extra spaces */
344 --amount;
345 if (left)
346 {
347 i -= amount;
348 if (i < 0)
349 i = 0;
350 }
351 else
352 i += amount;
353 count = i * p_sw;
354 }
355 else /* original vi indent */
356 {
357 if (left)
358 {
359 count -= p_sw * amount;
360 if (count < 0)
361 count = 0;
362 }
363 else
364 count += p_sw * amount;
365 }
366
367 /* Set new indent */
368#ifdef FEAT_VREPLACE
369 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000370 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 else
372#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000373 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374}
375
376#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
377/*
378 * Shift one line of the current block one shiftwidth right or left.
379 * Leaves cursor on first character in block.
380 */
381 static void
382shift_block(oap, amount)
383 oparg_T *oap;
384 int amount;
385{
386 int left = (oap->op_type == OP_LSHIFT);
387 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000388 int total;
389 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 int oldcol = curwin->w_cursor.col;
Bram Moolenaar14f24742012-08-08 18:01:05 +0200391 int p_sw = (int)get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 int p_ts = (int)curbuf->b_p_ts;
393 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000395 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 int i = 0, j = 0;
397 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#ifdef FEAT_RIGHTLEFT
399 int old_p_ri = p_ri;
400
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200401 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#endif
403
404 State = INSERT; /* don't want REPLACE for State */
405 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
406 if (bd.is_short)
407 return;
408
409 /* total is number of screen columns to be inserted/removed */
410 total = amount * p_sw;
411 oldp = ml_get_curline();
412
413 if (!left)
414 {
415 /*
416 * 1. Get start vcol
417 * 2. Total ws vcols
418 * 3. Divvy into TABs & spp
419 * 4. Construct new string
420 */
421 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
422 ws_vcol = bd.start_vcol - bd.pre_whitesp;
423 if (bd.startspaces)
424 {
425#ifdef FEAT_MBYTE
426 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000427 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000430 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432 for ( ; vim_iswhite(*bd.textstart); )
433 {
434 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
435 total += incr;
436 bd.start_vcol += incr;
437 }
438 /* OK, now total=all the VWS reqd, and textstart points at the 1st
439 * non-ws char in the block. */
440 if (!curbuf->b_p_et)
441 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
442 if (i)
443 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
444 else
445 j = total;
446 /* if we're splitting a TAB, allow for it */
447 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
448 len = (int)STRLEN(bd.textstart) + 1;
449 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
450 if (newp == NULL)
451 return;
452 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
453 mch_memmove(newp, oldp, (size_t)bd.textcol);
454 copy_chars(newp + bd.textcol, (size_t)i, TAB);
455 copy_spaces(newp + bd.textcol + i, (size_t)j);
456 /* the end */
457 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
458 }
459 else /* left */
460 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000461 colnr_T destination_col; /* column to which text in block will
462 be shifted */
463 char_u *verbatim_copy_end; /* end of the part of the line which is
464 copied verbatim */
465 colnr_T verbatim_copy_width;/* the (displayed) width of this part
466 of line */
467 unsigned fill; /* nr of spaces that replace a TAB */
468 unsigned new_line_len; /* the length of the line after the
469 block shift */
470 size_t block_space_width;
471 size_t shift_amount;
472 char_u *non_white = bd.textstart;
473 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000475 /*
476 * Firstly, let's find the first non-whitespace character that is
477 * displayed after the block's start column and the character's column
478 * number. Also, let's calculate the width of all the whitespace
479 * characters that are displayed in the block and precede the searched
480 * non-whitespace character.
481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000483 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
484 * the part of which is displayed at the block's beginning. Let's start
485 * searching from the next character. */
486 if (bd.startspaces)
487 mb_ptr_adv(non_white);
488
489 /* The character's column is in "bd.start_vcol". */
490 non_white_col = bd.start_vcol;
491
492 while (vim_iswhite(*non_white))
493 {
494 incr = lbr_chartabsize_adv(&non_white, non_white_col);
495 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 }
497
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000498 block_space_width = non_white_col - oap->start_vcol;
499 /* We will shift by "total" or "block_space_width", whichever is less.
500 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000501 shift_amount = (block_space_width < (size_t)total
502 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000503
504 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000505 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000506
507 /* Now let's find out how much of the beginning of the line we can
508 * reuse without modification. */
509 verbatim_copy_end = bd.textstart;
510 verbatim_copy_width = bd.start_vcol;
511
512 /* If "bd.startspaces" is set, "bd.textstart" points to the character
513 * preceding the block. We have to subtract its width to obtain its
514 * column number. */
515 if (bd.startspaces)
516 verbatim_copy_width -= bd.start_char_vcols;
517 while (verbatim_copy_width < destination_col)
518 {
519 incr = lbr_chartabsize(verbatim_copy_end, verbatim_copy_width);
520 if (verbatim_copy_width + incr > destination_col)
521 break;
522 verbatim_copy_width += incr;
523 mb_ptr_adv(verbatim_copy_end);
524 }
525
526 /* If "destination_col" is different from the width of the initial
527 * part of the line that will be copied, it means we encountered a tab
528 * character, which we will have to partly replace with spaces. */
529 fill = destination_col - verbatim_copy_width;
530
531 /* The replacement line will consist of:
532 * - the beginning of the original line up to "verbatim_copy_end",
533 * - "fill" number of spaces,
534 * - the rest of the line, pointed to by non_white. */
535 new_line_len = (unsigned)(verbatim_copy_end - oldp)
536 + fill
537 + (unsigned)STRLEN(non_white) + 1;
538
539 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (newp == NULL)
541 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000542 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
543 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
544 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 /* replace the line */
547 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
548 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
549 State = oldstate;
550 curwin->w_cursor.col = oldcol;
551#ifdef FEAT_RIGHTLEFT
552 p_ri = old_p_ri;
553#endif
554}
555#endif
556
557#ifdef FEAT_VISUALEXTRA
558/*
559 * Insert string "s" (b_insert ? before : after) block :AKelly
560 * Caller must prepare for undo.
561 */
562 static void
563block_insert(oap, s, b_insert, bdp)
564 oparg_T *oap;
565 char_u *s;
566 int b_insert;
567 struct block_def *bdp;
568{
569 int p_ts;
570 int count = 0; /* extra spaces to replace a cut TAB */
571 int spaces = 0; /* non-zero if cutting a TAB */
572 colnr_T offset; /* pointer along new line */
573 unsigned s_len; /* STRLEN(s) */
574 char_u *newp, *oldp; /* new, old lines */
575 linenr_T lnum; /* loop var */
576 int oldstate = State;
577
578 State = INSERT; /* don't want REPLACE for State */
579 s_len = (unsigned)STRLEN(s);
580
581 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
582 {
583 block_prep(oap, bdp, lnum, TRUE);
584 if (bdp->is_short && b_insert)
585 continue; /* OP_INSERT, line ends before block start */
586
587 oldp = ml_get(lnum);
588
589 if (b_insert)
590 {
591 p_ts = bdp->start_char_vcols;
592 spaces = bdp->startspaces;
593 if (spaces != 0)
594 count = p_ts - 1; /* we're cutting a TAB */
595 offset = bdp->textcol;
596 }
597 else /* append */
598 {
599 p_ts = bdp->end_char_vcols;
600 if (!bdp->is_short) /* spaces = padding after block */
601 {
602 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
603 if (spaces != 0)
604 count = p_ts - 1; /* we're cutting a TAB */
605 offset = bdp->textcol + bdp->textlen - (spaces != 0);
606 }
607 else /* spaces = padding to block edge */
608 {
609 /* if $ used, just append to EOL (ie spaces==0) */
610 if (!bdp->is_MAX)
611 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
612 count = spaces;
613 offset = bdp->textcol + bdp->textlen;
614 }
615 }
616
617 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
618 if (newp == NULL)
619 continue;
620
621 /* copy up to shifted part */
622 mch_memmove(newp, oldp, (size_t)(offset));
623 oldp += offset;
624
625 /* insert pre-padding */
626 copy_spaces(newp + offset, (size_t)spaces);
627
628 /* copy the new text */
629 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
630 offset += s_len;
631
632 if (spaces && !bdp->is_short)
633 {
634 /* insert post-padding */
635 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
636 /* We're splitting a TAB, don't copy it. */
637 oldp++;
638 /* We allowed for that TAB, remember this now */
639 count++;
640 }
641
642 if (spaces > 0)
643 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000644 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 ml_replace(lnum, newp, FALSE);
647
648 if (lnum == oap->end.lnum)
649 {
650 /* Set "']" mark to the end of the block instead of the end of
651 * the insert in the first line. */
652 curbuf->b_op_end.lnum = oap->end.lnum;
653 curbuf->b_op_end.col = offset;
654 }
655 } /* for all lnum */
656
657 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
658
659 State = oldstate;
660}
661#endif
662
663#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
664/*
665 * op_reindent - handle reindenting a block of lines.
666 */
667 void
668op_reindent(oap, how)
669 oparg_T *oap;
670 int (*how) __ARGS((void));
671{
672 long i;
673 char_u *l;
674 int count;
675 linenr_T first_changed = 0;
676 linenr_T last_changed = 0;
677 linenr_T start_lnum = curwin->w_cursor.lnum;
678
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000679 /* Don't even try when 'modifiable' is off. */
680 if (!curbuf->b_p_ma)
681 {
682 EMSG(_(e_modifiable));
683 return;
684 }
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 for (i = oap->line_count; --i >= 0 && !got_int; )
687 {
688 /* it's a slow thing to do, so give feedback so there's no worry that
689 * the computer's just hung. */
690
691 if (i > 1
692 && (i % 50 == 0 || i == oap->line_count - 1)
693 && oap->line_count > p_report)
694 smsg((char_u *)_("%ld lines to indent... "), i);
695
696 /*
697 * Be vi-compatible: For lisp indenting the first line is not
698 * indented, unless there is only one line.
699 */
700#ifdef FEAT_LISP
701 if (i != oap->line_count - 1 || oap->line_count == 1
702 || how != get_lisp_indent)
703#endif
704 {
705 l = skipwhite(ml_get_curline());
706 if (*l == NUL) /* empty or blank line */
707 count = 0;
708 else
709 count = how(); /* get the indent for this line */
710
711 if (set_indent(count, SIN_UNDO))
712 {
713 /* did change the indent, call changed_lines() later */
714 if (first_changed == 0)
715 first_changed = curwin->w_cursor.lnum;
716 last_changed = curwin->w_cursor.lnum;
717 }
718 }
719 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000720 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
722
723 /* put cursor on first non-blank of indented line */
724 curwin->w_cursor.lnum = start_lnum;
725 beginline(BL_SOL | BL_FIX);
726
727 /* Mark changed lines so that they will be redrawn. When Visual
728 * highlighting was present, need to continue until the last line. When
729 * there is no change still need to remove the Visual highlighting. */
730 if (last_changed != 0)
731 changed_lines(first_changed, 0,
732#ifdef FEAT_VISUAL
733 oap->is_VIsual ? start_lnum + oap->line_count :
734#endif
735 last_changed + 1, 0L);
736#ifdef FEAT_VISUAL
737 else if (oap->is_VIsual)
738 redraw_curbuf_later(INVERTED);
739#endif
740
741 if (oap->line_count > p_report)
742 {
743 i = oap->line_count - (i + 1);
744 if (i == 1)
745 MSG(_("1 line indented "));
746 else
747 smsg((char_u *)_("%ld lines indented "), i);
748 }
749 /* set '[ and '] marks */
750 curbuf->b_op_start = oap->start;
751 curbuf->b_op_end = oap->end;
752}
753#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
754
755#if defined(FEAT_EVAL) || defined(PROTO)
756/*
757 * Keep the last expression line here, for repeating.
758 */
759static char_u *expr_line = NULL;
760
761/*
762 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
763 * Returns '=' when OK, NUL otherwise.
764 */
765 int
766get_expr_register()
767{
768 char_u *new_line;
769
770 new_line = getcmdline('=', 0L, 0);
771 if (new_line == NULL)
772 return NUL;
773 if (*new_line == NUL) /* use previous line */
774 vim_free(new_line);
775 else
776 set_expr_line(new_line);
777 return '=';
778}
779
780/*
781 * Set the expression for the '=' register.
782 * Argument must be an allocated string.
783 */
784 void
785set_expr_line(new_line)
786 char_u *new_line;
787{
788 vim_free(expr_line);
789 expr_line = new_line;
790}
791
792/*
793 * Get the result of the '=' register expression.
794 * Returns a pointer to allocated memory, or NULL for failure.
795 */
796 char_u *
797get_expr_line()
798{
799 char_u *expr_copy;
800 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000801 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 if (expr_line == NULL)
804 return NULL;
805
806 /* Make a copy of the expression, because evaluating it may cause it to be
807 * changed. */
808 expr_copy = vim_strsave(expr_line);
809 if (expr_copy == NULL)
810 return NULL;
811
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000812 /* When we are invoked recursively limit the evaluation to 10 levels.
813 * Then return the string as-is. */
814 if (nested >= 10)
815 return expr_copy;
816
817 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000818 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000819 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 vim_free(expr_copy);
821 return rv;
822}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000823
824/*
825 * Get the '=' register expression itself, without evaluating it.
826 */
827 char_u *
828get_expr_line_src()
829{
830 if (expr_line == NULL)
831 return NULL;
832 return vim_strsave(expr_line);
833}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#endif /* FEAT_EVAL */
835
836/*
837 * Check if 'regname' is a valid name of a yank register.
838 * Note: There is no check for 0 (default register), caller should do this
839 */
840 int
841valid_yank_reg(regname, writing)
842 int regname;
843 int writing; /* if TRUE check for writable registers */
844{
845 if ( (regname > 0 && ASCII_ISALNUM(regname))
846 || (!writing && vim_strchr((char_u *)
847#ifdef FEAT_EVAL
848 "/.%#:="
849#else
850 "/.%#:"
851#endif
852 , regname) != NULL)
853 || regname == '"'
854 || regname == '-'
855 || regname == '_'
856#ifdef FEAT_CLIPBOARD
857 || regname == '*'
858 || regname == '+'
859#endif
860#ifdef FEAT_DND
861 || (!writing && regname == '~')
862#endif
863 )
864 return TRUE;
865 return FALSE;
866}
867
868/*
869 * Set y_current and y_append, according to the value of "regname".
870 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000871 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 *
873 * If regname is 0 and writing, use register 0
874 * If regname is 0 and reading, use previous register
875 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000876 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877get_yank_register(regname, writing)
878 int regname;
879 int writing;
880{
881 int i;
882
883 y_append = FALSE;
884 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
885 {
886 y_current = y_previous;
887 return;
888 }
889 i = regname;
890 if (VIM_ISDIGIT(i))
891 i -= '0';
892 else if (ASCII_ISLOWER(i))
893 i = CharOrdLow(i) + 10;
894 else if (ASCII_ISUPPER(i))
895 {
896 i = CharOrdUp(i) + 10;
897 y_append = TRUE;
898 }
899 else if (regname == '-')
900 i = DELETION_REGISTER;
901#ifdef FEAT_CLIPBOARD
902 /* When selection is not available, use register 0 instead of '*' */
903 else if (clip_star.available && regname == '*')
904 i = STAR_REGISTER;
905 /* When clipboard is not available, use register 0 instead of '+' */
906 else if (clip_plus.available && regname == '+')
907 i = PLUS_REGISTER;
908#endif
909#ifdef FEAT_DND
910 else if (!writing && regname == '~')
911 i = TILDE_REGISTER;
912#endif
913 else /* not 0-9, a-z, A-Z or '-': use register 0 */
914 i = 0;
915 y_current = &(y_regs[i]);
916 if (writing) /* remember the register we write into for do_put() */
917 y_previous = y_current;
918}
919
Bram Moolenaar8299df92004-07-10 09:47:34 +0000920#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921/*
922 * When "regname" is a clipboard register, obtain the selection. If it's not
923 * available return zero, otherwise return "regname".
924 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000925 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926may_get_selection(regname)
927 int regname;
928{
929 if (regname == '*')
930 {
931 if (!clip_star.available)
932 regname = 0;
933 else
934 clip_get_selection(&clip_star);
935 }
936 else if (regname == '+')
937 {
938 if (!clip_plus.available)
939 regname = 0;
940 else
941 clip_get_selection(&clip_plus);
942 }
943 return regname;
944}
945#endif
946
947#if defined(FEAT_VISUAL) || defined(PROTO)
948/*
949 * Obtain the contents of a "normal" register. The register is made empty.
950 * The returned pointer has allocated memory, use put_register() later.
951 */
952 void *
953get_register(name, copy)
954 int name;
955 int copy; /* make a copy, if FALSE make register empty. */
956{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000957 struct yankreg *reg;
958 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
960#ifdef FEAT_CLIPBOARD
961 /* When Visual area changed, may have to update selection. Obtain the
962 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000963 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200965 if (clip_isautosel_star())
966 clip_update_selection(&clip_star);
967 may_get_selection(name);
968 }
969 if (name == '+' && clip_plus.available)
970 {
971 if (clip_isautosel_plus())
972 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 may_get_selection(name);
974 }
975#endif
976
977 get_yank_register(name, 0);
978 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
979 if (reg != NULL)
980 {
981 *reg = *y_current;
982 if (copy)
983 {
984 /* If we run out of memory some or all of the lines are empty. */
985 if (reg->y_size == 0)
986 reg->y_array = NULL;
987 else
988 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
989 * reg->y_size));
990 if (reg->y_array != NULL)
991 {
992 for (i = 0; i < reg->y_size; ++i)
993 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
994 }
995 }
996 else
997 y_current->y_array = NULL;
998 }
999 return (void *)reg;
1000}
1001
1002/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001003 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 */
1005 void
1006put_register(name, reg)
1007 int name;
1008 void *reg;
1009{
1010 get_yank_register(name, 0);
1011 free_yank_all();
1012 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001013 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015# ifdef FEAT_CLIPBOARD
1016 /* Send text written to clipboard register to the clipboard. */
1017 may_set_selection();
1018# endif
1019}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001020
1021 void
1022free_register(reg)
1023 void *reg;
1024{
1025 struct yankreg tmp;
1026
1027 tmp = *y_current;
1028 *y_current = *(struct yankreg *)reg;
1029 free_yank_all();
1030 vim_free(reg);
1031 *y_current = tmp;
1032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#endif
1034
1035#if defined(FEAT_MOUSE) || defined(PROTO)
1036/*
1037 * return TRUE if the current yank register has type MLINE
1038 */
1039 int
1040yank_register_mline(regname)
1041 int regname;
1042{
1043 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1044 return FALSE;
1045 if (regname == '_') /* black hole is always empty */
1046 return FALSE;
1047 get_yank_register(regname, FALSE);
1048 return (y_current->y_type == MLINE);
1049}
1050#endif
1051
1052/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001053 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001055 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
1057 int
1058do_record(c)
1059 int c;
1060{
Bram Moolenaard55de222007-05-06 13:38:48 +00001061 char_u *p;
1062 static int regname;
1063 struct yankreg *old_y_previous, *old_y_current;
1064 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
1066 if (Recording == FALSE) /* start recording */
1067 {
1068 /* registers 0-9, a-z and " are allowed */
1069 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1070 retval = FAIL;
1071 else
1072 {
1073 Recording = TRUE;
1074 showmode();
1075 regname = c;
1076 retval = OK;
1077 }
1078 }
1079 else /* stop recording */
1080 {
1081 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001082 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1083 * needs to be removed again to put it in a register. exec_reg then
1084 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
1086 Recording = FALSE;
1087 MSG("");
1088 p = get_recorded();
1089 if (p == NULL)
1090 retval = FAIL;
1091 else
1092 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001093 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1094 vim_unescape_csi(p);
1095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 /*
1097 * We don't want to change the default register here, so save and
1098 * restore the current register name.
1099 */
1100 old_y_previous = y_previous;
1101 old_y_current = y_current;
1102
1103 retval = stuff_yank(regname, p);
1104
1105 y_previous = old_y_previous;
1106 y_current = old_y_current;
1107 }
1108 }
1109 return retval;
1110}
1111
1112/*
1113 * Stuff string "p" into yank register "regname" as a single line (append if
1114 * uppercase). "p" must have been alloced.
1115 *
1116 * return FAIL for failure, OK otherwise
1117 */
1118 static int
1119stuff_yank(regname, p)
1120 int regname;
1121 char_u *p;
1122{
1123 char_u *lp;
1124 char_u **pp;
1125
1126 /* check for read-only register */
1127 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1128 {
1129 vim_free(p);
1130 return FAIL;
1131 }
1132 if (regname == '_') /* black hole: don't do anything */
1133 {
1134 vim_free(p);
1135 return OK;
1136 }
1137 get_yank_register(regname, TRUE);
1138 if (y_append && y_current->y_array != NULL)
1139 {
1140 pp = &(y_current->y_array[y_current->y_size - 1]);
1141 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1142 if (lp == NULL)
1143 {
1144 vim_free(p);
1145 return FAIL;
1146 }
1147 STRCPY(lp, *pp);
1148 STRCAT(lp, p);
1149 vim_free(p);
1150 vim_free(*pp);
1151 *pp = lp;
1152 }
1153 else
1154 {
1155 free_yank_all();
1156 if ((y_current->y_array =
1157 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1158 {
1159 vim_free(p);
1160 return FAIL;
1161 }
1162 y_current->y_array[0] = p;
1163 y_current->y_size = 1;
1164 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1165 }
1166 return OK;
1167}
1168
Bram Moolenaar42b94362009-05-26 16:12:37 +00001169static int execreg_lastc = NUL;
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171/*
1172 * execute a yank register: copy it into the stuff buffer
1173 *
1174 * return FAIL for failure, OK otherwise
1175 */
1176 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001177do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 int regname;
1179 int colon; /* insert ':' before each line */
1180 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001181 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 long i;
1184 char_u *p;
1185 int retval = OK;
1186 int remap;
1187
1188 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001189 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001190 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001191 {
1192 EMSG(_("E748: No previously used register"));
1193 return FAIL;
1194 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001195 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 /* check for valid regname */
1198 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001199 {
1200 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001202 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001203 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
1205#ifdef FEAT_CLIPBOARD
1206 regname = may_get_selection(regname);
1207#endif
1208
1209 if (regname == '_') /* black hole: don't stuff anything */
1210 return OK;
1211
1212#ifdef FEAT_CMDHIST
1213 if (regname == ':') /* use last command line */
1214 {
1215 if (last_cmdline == NULL)
1216 {
1217 EMSG(_(e_nolastcmd));
1218 return FAIL;
1219 }
1220 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1221 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001222 /* Escape all control characters with a CTRL-V */
1223 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001224 (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 +00001225 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001226 {
1227 /* When in Visual mode "'<,'>" will be prepended to the command.
1228 * Remove it when it's already there. */
1229 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001230 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001231 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001232 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001233 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001234 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236#endif
1237#ifdef FEAT_EVAL
1238 else if (regname == '=')
1239 {
1240 p = get_expr_line();
1241 if (p == NULL)
1242 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001243 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 vim_free(p);
1245 }
1246#endif
1247 else if (regname == '.') /* use last inserted text */
1248 {
1249 p = get_last_insert_save();
1250 if (p == NULL)
1251 {
1252 EMSG(_(e_noinstext));
1253 return FAIL;
1254 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001255 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 vim_free(p);
1257 }
1258 else
1259 {
1260 get_yank_register(regname, FALSE);
1261 if (y_current->y_array == NULL)
1262 return FAIL;
1263
1264 /* Disallow remaping for ":@r". */
1265 remap = colon ? REMAP_NONE : REMAP_YES;
1266
1267 /*
1268 * Insert lines into typeahead buffer, from last one to first one.
1269 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001270 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 for (i = y_current->y_size; --i >= 0; )
1272 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001273 char_u *escaped;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /* insert NL between lines and after last line if type is MLINE */
1276 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1277 || addcr)
1278 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001279 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 return FAIL;
1281 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001282 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1283 if (escaped == NULL)
1284 return FAIL;
1285 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1286 vim_free(escaped);
1287 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001289 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 == FAIL)
1291 return FAIL;
1292 }
1293 Exec_reg = TRUE; /* disable the 'q' command */
1294 }
1295 return retval;
1296}
1297
1298/*
1299 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1300 * used only after other typeahead has been processed.
1301 */
1302 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001303put_reedit_in_typebuf(silent)
1304 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
1306 char_u buf[3];
1307
1308 if (restart_edit != NUL)
1309 {
1310 if (restart_edit == 'V')
1311 {
1312 buf[0] = 'g';
1313 buf[1] = 'R';
1314 buf[2] = NUL;
1315 }
1316 else
1317 {
1318 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1319 buf[1] = NUL;
1320 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001321 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 restart_edit = NUL;
1323 }
1324}
1325
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001326/*
1327 * Insert register contents "s" into the typeahead buffer, so that it will be
1328 * executed again.
1329 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1330 * no remapping.
1331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001333put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001335 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001337 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339 int retval = OK;
1340
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001341 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001343 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001345 {
1346 char_u *p;
1347
1348 if (esc)
1349 p = vim_strsave_escape_csi(s);
1350 else
1351 p = s;
1352 if (p == NULL)
1353 retval = FAIL;
1354 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001355 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1356 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001357 if (esc)
1358 vim_free(p);
1359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001361 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 return retval;
1363}
1364
1365/*
1366 * Insert a yank register: copy it into the Read buffer.
1367 * Used by CTRL-R command and middle mouse button in insert mode.
1368 *
1369 * return FAIL for failure, OK otherwise
1370 */
1371 int
1372insert_reg(regname, literally)
1373 int regname;
1374 int literally; /* insert literally, not as if typed */
1375{
1376 long i;
1377 int retval = OK;
1378 char_u *arg;
1379 int allocated;
1380
1381 /*
1382 * It is possible to get into an endless loop by having CTRL-R a in
1383 * register a and then, in insert mode, doing CTRL-R a.
1384 * If you hit CTRL-C, the loop will be broken here.
1385 */
1386 ui_breakcheck();
1387 if (got_int)
1388 return FAIL;
1389
1390 /* check for valid regname */
1391 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1392 return FAIL;
1393
1394#ifdef FEAT_CLIPBOARD
1395 regname = may_get_selection(regname);
1396#endif
1397
1398 if (regname == '.') /* insert last inserted text */
1399 retval = stuff_inserted(NUL, 1L, TRUE);
1400 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1401 {
1402 if (arg == NULL)
1403 return FAIL;
1404 stuffescaped(arg, literally);
1405 if (allocated)
1406 vim_free(arg);
1407 }
1408 else /* name or number register */
1409 {
1410 get_yank_register(regname, FALSE);
1411 if (y_current->y_array == NULL)
1412 retval = FAIL;
1413 else
1414 {
1415 for (i = 0; i < y_current->y_size; ++i)
1416 {
1417 stuffescaped(y_current->y_array[i], literally);
1418 /*
1419 * Insert a newline between lines and after last line if
1420 * y_type is MLINE.
1421 */
1422 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1423 stuffcharReadbuff('\n');
1424 }
1425 }
1426 }
1427
1428 return retval;
1429}
1430
1431/*
1432 * Stuff a string into the typeahead buffer, such that edit() will insert it
1433 * literally ("literally" TRUE) or interpret is as typed characters.
1434 */
1435 static void
1436stuffescaped(arg, literally)
1437 char_u *arg;
1438 int literally;
1439{
1440 int c;
1441 char_u *start;
1442
1443 while (*arg != NUL)
1444 {
1445 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1446 * stuff K_SPECIAL to get the effect of a special key when "literally"
1447 * is TRUE. */
1448 start = arg;
1449 while ((*arg >= ' '
1450#ifndef EBCDIC
1451 && *arg < DEL /* EBCDIC: chars above space are normal */
1452#endif
1453 )
1454 || (*arg == K_SPECIAL && !literally))
1455 ++arg;
1456 if (arg > start)
1457 stuffReadbuffLen(start, (long)(arg - start));
1458
1459 /* stuff a single special character */
1460 if (*arg != NUL)
1461 {
1462#ifdef FEAT_MBYTE
1463 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001464 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 else
1466#endif
1467 c = *arg++;
1468 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1469 stuffcharReadbuff(Ctrl_V);
1470 stuffcharReadbuff(c);
1471 }
1472 }
1473}
1474
1475/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001476 * If "regname" is a special register, return TRUE and store a pointer to its
1477 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001479 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480get_spec_reg(regname, argp, allocated, errmsg)
1481 int regname;
1482 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001483 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int errmsg; /* give error message when failing */
1485{
1486 int cnt;
1487
1488 *argp = NULL;
1489 *allocated = FALSE;
1490 switch (regname)
1491 {
1492 case '%': /* file name */
1493 if (errmsg)
1494 check_fname(); /* will give emsg if not set */
1495 *argp = curbuf->b_fname;
1496 return TRUE;
1497
1498 case '#': /* alternate file name */
1499 *argp = getaltfname(errmsg); /* may give emsg if not set */
1500 return TRUE;
1501
1502#ifdef FEAT_EVAL
1503 case '=': /* result of expression */
1504 *argp = get_expr_line();
1505 *allocated = TRUE;
1506 return TRUE;
1507#endif
1508
1509 case ':': /* last command line */
1510 if (last_cmdline == NULL && errmsg)
1511 EMSG(_(e_nolastcmd));
1512 *argp = last_cmdline;
1513 return TRUE;
1514
1515 case '/': /* last search-pattern */
1516 if (last_search_pat() == NULL && errmsg)
1517 EMSG(_(e_noprevre));
1518 *argp = last_search_pat();
1519 return TRUE;
1520
1521 case '.': /* last inserted text */
1522 *argp = get_last_insert_save();
1523 *allocated = TRUE;
1524 if (*argp == NULL && errmsg)
1525 EMSG(_(e_noinstext));
1526 return TRUE;
1527
1528#ifdef FEAT_SEARCHPATH
1529 case Ctrl_F: /* Filename under cursor */
1530 case Ctrl_P: /* Path under cursor, expand via "path" */
1531 if (!errmsg)
1532 return FALSE;
1533 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001534 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 *allocated = TRUE;
1536 return TRUE;
1537#endif
1538
1539 case Ctrl_W: /* word under cursor */
1540 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1541 if (!errmsg)
1542 return FALSE;
1543 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1544 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1545 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1546 *allocated = TRUE;
1547 return TRUE;
1548
1549 case '_': /* black hole: always empty */
1550 *argp = (char_u *)"";
1551 return TRUE;
1552 }
1553
1554 return FALSE;
1555}
1556
1557/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001558 * Paste a yank register into the command line.
1559 * Only for non-special registers.
1560 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 * insert_reg() can't be used here, because special characters from the
1562 * register contents will be interpreted as commands.
1563 *
1564 * return FAIL for failure, OK otherwise
1565 */
1566 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001567cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 int regname;
1569 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001570 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 get_yank_register(regname, FALSE);
1575 if (y_current->y_array == NULL)
1576 return FAIL;
1577
1578 for (i = 0; i < y_current->y_size; ++i)
1579 {
1580 cmdline_paste_str(y_current->y_array[i], literally);
1581
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001582 /* Insert ^M between lines and after last line if type is MLINE.
1583 * Don't do this when "remcr" is TRUE and the next line is empty. */
1584 if (y_current->y_type == MLINE
1585 || (i < y_current->y_size - 1
1586 && !(remcr
1587 && i == y_current->y_size - 2
1588 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 cmdline_paste_str((char_u *)"\r", literally);
1590
1591 /* Check for CTRL-C, in case someone tries to paste a few thousand
1592 * lines and gets bored. */
1593 ui_breakcheck();
1594 if (got_int)
1595 return FAIL;
1596 }
1597 return OK;
1598}
1599
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1601/*
1602 * Adjust the register name pointed to with "rp" for the clipboard being
1603 * used always and the clipboard being available.
1604 */
1605 void
1606adjust_clip_reg(rp)
1607 int *rp;
1608{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001609 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1610 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
1611 if (*rp == 0 && clip_unnamed != 0)
1612 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
1613 ? '+' : '*';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (!clip_star.available && *rp == '*')
1615 *rp = 0;
1616 if (!clip_plus.available && *rp == '+')
1617 *rp = 0;
1618}
1619#endif
1620
1621/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001622 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001624 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 */
1626 int
1627op_delete(oap)
1628 oparg_T *oap;
1629{
1630 int n;
1631 linenr_T lnum;
1632 char_u *ptr;
1633#ifdef FEAT_VISUAL
1634 char_u *newp, *oldp;
1635 struct block_def bd;
1636#endif
1637 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1638 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001639 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1642 return OK;
1643
1644 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1645 if (oap->empty)
1646 return u_save_cursor();
1647
1648 if (!curbuf->b_p_ma)
1649 {
1650 EMSG(_(e_modifiable));
1651 return FAIL;
1652 }
1653
1654#ifdef FEAT_CLIPBOARD
1655 adjust_clip_reg(&oap->regname);
1656#endif
1657
1658#ifdef FEAT_MBYTE
1659 if (has_mbyte)
1660 mb_adjust_opend(oap);
1661#endif
1662
Bram Moolenaard04b7502010-07-08 22:27:55 +02001663 /*
1664 * Imitate the strange Vi behaviour: If the delete spans more than one
1665 * line and motion_type == MCHAR and the result is a blank line, make the
1666 * delete linewise. Don't do this for the change command or Visual mode.
1667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if ( oap->motion_type == MCHAR
1669#ifdef FEAT_VISUAL
1670 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001671 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
1673 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001674 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 && oap->op_type == OP_DELETE)
1676 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001677 ptr = ml_get(oap->end.lnum) + oap->end.col;
1678 if (*ptr != NUL)
1679 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 ptr = skipwhite(ptr);
1681 if (*ptr == NUL && inindent(0))
1682 oap->motion_type = MLINE;
1683 }
1684
Bram Moolenaard04b7502010-07-08 22:27:55 +02001685 /*
1686 * Check for trying to delete (e.g. "D") in an empty line.
1687 * Note: For the change operator it is ok.
1688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if ( oap->motion_type == MCHAR
1690 && oap->line_count == 1
1691 && oap->op_type == OP_DELETE
1692 && *ml_get(oap->start.lnum) == NUL)
1693 {
1694 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001695 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 * 'cpoptions' (Vi compatible).
1697 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001698#ifdef FEAT_VIRTUALEDIT
1699 if (virtual_op)
1700 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1701 * marks as if it happened. */
1702 goto setmarks;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1705 beep_flush();
1706 return OK;
1707 }
1708
Bram Moolenaard04b7502010-07-08 22:27:55 +02001709 /*
1710 * Do a yank of whatever we're about to delete.
1711 * If a yank register was specified, put the deleted text into that
1712 * register. For the black hole register '_' don't yank anything.
1713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (oap->regname != '_')
1715 {
1716 if (oap->regname != 0)
1717 {
1718 /* check for read-only register */
1719 if (!valid_yank_reg(oap->regname, TRUE))
1720 {
1721 beep_flush();
1722 return OK;
1723 }
1724 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1725 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1726 did_yank = TRUE;
1727 }
1728
1729 /*
1730 * Put deleted text into register 1 and shift number registers if the
1731 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001732 * Use the register name from before adjust_clip_reg() may have
1733 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001735 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 || oap->line_count > 1 || oap->use_reg_one)
1737 {
1738 y_current = &y_regs[9];
1739 free_yank_all(); /* free register nine */
1740 for (n = 9; n > 1; --n)
1741 y_regs[n] = y_regs[n - 1];
1742 y_previous = y_current = &y_regs[1];
1743 y_regs[1].y_array = NULL; /* set register one to empty */
1744 if (op_yank(oap, TRUE, FALSE) == OK)
1745 did_yank = TRUE;
1746 }
1747
Bram Moolenaar84298db2012-04-20 13:46:08 +02001748 /* Yank into small delete register when no named register specified
1749 * and the delete is within one line. */
1750 if ((
1751#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001752 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1753 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001754#endif
1755 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 && oap->line_count == 1)
1757 {
1758 oap->regname = '-';
1759 get_yank_register(oap->regname, TRUE);
1760 if (op_yank(oap, TRUE, FALSE) == OK)
1761 did_yank = TRUE;
1762 oap->regname = 0;
1763 }
1764
1765 /*
1766 * If there's too much stuff to fit in the yank register, then get a
1767 * confirmation before doing the delete. This is crude, but simple.
1768 * And it avoids doing a delete of something we can't put back if we
1769 * want.
1770 */
1771 if (!did_yank)
1772 {
1773 int msg_silent_save = msg_silent;
1774
1775 msg_silent = 0; /* must display the prompt */
1776 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1777 msg_silent = msg_silent_save;
1778 if (n != 'y')
1779 {
1780 EMSG(_(e_abort));
1781 return FAIL;
1782 }
1783 }
1784 }
1785
1786#ifdef FEAT_VISUAL
Bram Moolenaard04b7502010-07-08 22:27:55 +02001787 /*
1788 * block mode delete
1789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (oap->block_mode)
1791 {
1792 if (u_save((linenr_T)(oap->start.lnum - 1),
1793 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1794 return FAIL;
1795
1796 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1797 {
1798 block_prep(oap, &bd, lnum, TRUE);
1799 if (bd.textlen == 0) /* nothing to delete */
1800 continue;
1801
1802 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1803 if (lnum == curwin->w_cursor.lnum)
1804 {
1805 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1806# ifdef FEAT_VIRTUALEDIT
1807 curwin->w_cursor.coladd = 0;
1808# endif
1809 }
1810
1811 /* n == number of chars deleted
1812 * If we delete a TAB, it may be replaced by several characters.
1813 * Thus the number of characters may increase!
1814 */
1815 n = bd.textlen - bd.startspaces - bd.endspaces;
1816 oldp = ml_get(lnum);
1817 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1818 if (newp == NULL)
1819 continue;
1820 /* copy up to deleted part */
1821 mch_memmove(newp, oldp, (size_t)bd.textcol);
1822 /* insert spaces */
1823 copy_spaces(newp + bd.textcol,
1824 (size_t)(bd.startspaces + bd.endspaces));
1825 /* copy the part after the deleted part */
1826 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001827 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 /* replace the line */
1829 ml_replace(lnum, newp, FALSE);
1830 }
1831
1832 check_cursor_col();
1833 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1834 oap->end.lnum + 1, 0L);
1835 oap->line_count = 0; /* no lines deleted */
1836 }
1837 else
1838#endif
1839 if (oap->motion_type == MLINE)
1840 {
1841 if (oap->op_type == OP_CHANGE)
1842 {
1843 /* Delete the lines except the first one. Temporarily move the
1844 * cursor to the next line. Save the current line number, if the
1845 * last line is deleted it may be changed.
1846 */
1847 if (oap->line_count > 1)
1848 {
1849 lnum = curwin->w_cursor.lnum;
1850 ++curwin->w_cursor.lnum;
1851 del_lines((long)(oap->line_count - 1), TRUE);
1852 curwin->w_cursor.lnum = lnum;
1853 }
1854 if (u_save_cursor() == FAIL)
1855 return FAIL;
1856 if (curbuf->b_p_ai) /* don't delete indent */
1857 {
1858 beginline(BL_WHITE); /* cursor on first non-white */
1859 did_ai = TRUE; /* delete the indent when ESC hit */
1860 ai_col = curwin->w_cursor.col;
1861 }
1862 else
1863 beginline(0); /* cursor in column 0 */
1864 truncate_line(FALSE); /* delete the rest of the line */
1865 /* leave cursor past last char in line */
1866 if (oap->line_count > 1)
1867 u_clearline(); /* "U" command not possible after "2cc" */
1868 }
1869 else
1870 {
1871 del_lines(oap->line_count, TRUE);
1872 beginline(BL_WHITE | BL_FIX);
1873 u_clearline(); /* "U" command not possible after "dd" */
1874 }
1875 }
1876 else
1877 {
1878#ifdef FEAT_VIRTUALEDIT
1879 if (virtual_op)
1880 {
1881 int endcol = 0;
1882
1883 /* For virtualedit: break the tabs that are partly included. */
1884 if (gchar_pos(&oap->start) == '\t')
1885 {
1886 if (u_save_cursor() == FAIL) /* save first line for undo */
1887 return FAIL;
1888 if (oap->line_count == 1)
1889 endcol = getviscol2(oap->end.col, oap->end.coladd);
1890 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1891 oap->start = curwin->w_cursor;
1892 if (oap->line_count == 1)
1893 {
1894 coladvance(endcol);
1895 oap->end.col = curwin->w_cursor.col;
1896 oap->end.coladd = curwin->w_cursor.coladd;
1897 curwin->w_cursor = oap->start;
1898 }
1899 }
1900
1901 /* Break a tab only when it's included in the area. */
1902 if (gchar_pos(&oap->end) == '\t'
1903 && (int)oap->end.coladd < oap->inclusive)
1904 {
1905 /* save last line for undo */
1906 if (u_save((linenr_T)(oap->end.lnum - 1),
1907 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1908 return FAIL;
1909 curwin->w_cursor = oap->end;
1910 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1911 oap->end = curwin->w_cursor;
1912 curwin->w_cursor = oap->start;
1913 }
1914 }
1915#endif
1916
1917 if (oap->line_count == 1) /* delete characters within one line */
1918 {
1919 if (u_save_cursor() == FAIL) /* save line for undo */
1920 return FAIL;
1921
1922 /* if 'cpoptions' contains '$', display '$' at end of change */
1923 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1924 && oap->op_type == OP_CHANGE
1925 && oap->end.lnum == curwin->w_cursor.lnum
1926#ifdef FEAT_VISUAL
1927 && !oap->is_VIsual
1928#endif
1929 )
1930 display_dollar(oap->end.col - !oap->inclusive);
1931
1932 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1933
1934#ifdef FEAT_VIRTUALEDIT
1935 if (virtual_op)
1936 {
1937 /* fix up things for virtualedit-delete:
1938 * break the tabs which are going to get in our way
1939 */
1940 char_u *curline = ml_get_curline();
1941 int len = (int)STRLEN(curline);
1942
1943 if (oap->end.coladd != 0
1944 && (int)oap->end.col >= len - 1
1945 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1946 n++;
1947 /* Delete at least one char (e.g, when on a control char). */
1948 if (n == 0 && oap->start.coladd != oap->end.coladd)
1949 n = 1;
1950
1951 /* When deleted a char in the line, reset coladd. */
1952 if (gchar_cursor() != NUL)
1953 curwin->w_cursor.coladd = 0;
1954 }
1955#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001956 if (oap->op_type == OP_DELETE
1957 && oap->inclusive
1958 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001959 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1960 {
1961 /* Special case: gH<Del> deletes the last line. */
1962 del_lines(1L, FALSE);
1963 }
1964 else
1965 {
1966 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001967#ifdef FEAT_VISUAL
1968 && !oap->is_VIsual
1969#endif
1970 );
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
1973 else /* delete characters between lines */
1974 {
1975 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001976 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978 /* save deleted and changed lines for undo */
1979 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1980 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1981 return FAIL;
1982
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001983 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 truncate_line(TRUE); /* delete from cursor to end of line */
1985
1986 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1987 ++curwin->w_cursor.lnum;
1988 del_lines((long)(oap->line_count - 2), FALSE);
1989
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001990 if (delete_last_line)
1991 oap->end.lnum = curbuf->b_ml.ml_line_count;
1992
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001993 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001994 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001995 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1996 {
1997 /* Special case: gH<Del> deletes the last line. */
1998 del_lines(1L, FALSE);
1999 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01002000 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2001 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002002 }
2003 else
2004 {
2005 /* delete from start of line until op_end */
2006 curwin->w_cursor.col = 0;
2007 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002008#ifdef FEAT_VISUAL
2009 && !oap->is_VIsual
2010#endif
2011 );
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002012 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2013 }
2014 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar81340392012-06-06 16:12:59 +02002015 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017 }
2018
2019 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2020
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002021#ifdef FEAT_VIRTUALEDIT
2022setmarks:
2023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#ifdef FEAT_VISUAL
2025 if (oap->block_mode)
2026 {
2027 curbuf->b_op_end.lnum = oap->end.lnum;
2028 curbuf->b_op_end.col = oap->start.col;
2029 }
2030 else
2031#endif
2032 curbuf->b_op_end = oap->start;
2033 curbuf->b_op_start = oap->start;
2034
2035 return OK;
2036}
2037
2038#ifdef FEAT_MBYTE
2039/*
2040 * Adjust end of operating area for ending on a multi-byte character.
2041 * Used for deletion.
2042 */
2043 static void
2044mb_adjust_opend(oap)
2045 oparg_T *oap;
2046{
2047 char_u *p;
2048
2049 if (oap->inclusive)
2050 {
2051 p = ml_get(oap->end.lnum);
2052 oap->end.col += mb_tail_off(p, p + oap->end.col);
2053 }
2054}
2055#endif
2056
2057#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2058/*
2059 * Replace a whole area with one character.
2060 */
2061 int
2062op_replace(oap, c)
2063 oparg_T *oap;
2064 int c;
2065{
2066 int n, numc;
2067#ifdef FEAT_MBYTE
2068 int num_chars;
2069#endif
2070 char_u *newp, *oldp;
2071 size_t oldlen;
2072 struct block_def bd;
2073
2074 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2075 return OK; /* nothing to do */
2076
2077#ifdef FEAT_MBYTE
2078 if (has_mbyte)
2079 mb_adjust_opend(oap);
2080#endif
2081
2082 if (u_save((linenr_T)(oap->start.lnum - 1),
2083 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2084 return FAIL;
2085
2086 /*
2087 * block mode replace
2088 */
2089 if (oap->block_mode)
2090 {
2091 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2092 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2093 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002094 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2096 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2097 continue; /* nothing to replace */
2098
2099 /* n == number of extra chars required
2100 * If we split a TAB, it may be replaced by several characters.
2101 * Thus the number of characters may increase!
2102 */
2103#ifdef FEAT_VIRTUALEDIT
2104 /* If the range starts in virtual space, count the initial
2105 * coladd offset as part of "startspaces" */
2106 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2107 {
2108 pos_T vpos;
2109
Bram Moolenaara1381de2009-11-03 15:44:21 +00002110 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 getvpos(&vpos, oap->start_vcol);
2112 bd.startspaces += vpos.coladd;
2113 n = bd.startspaces;
2114 }
2115 else
2116#endif
2117 /* allow for pre spaces */
2118 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2119
2120 /* allow for post spp */
2121 n += (bd.endspaces
2122#ifdef FEAT_VIRTUALEDIT
2123 && !bd.is_oneChar
2124#endif
2125 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2126 /* Figure out how many characters to replace. */
2127 numc = oap->end_vcol - oap->start_vcol + 1;
2128 if (bd.is_short && (!virtual_op || bd.is_MAX))
2129 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2130
2131#ifdef FEAT_MBYTE
2132 /* A double-wide character can be replaced only up to half the
2133 * times. */
2134 if ((*mb_char2cells)(c) > 1)
2135 {
2136 if ((numc & 1) && !bd.is_short)
2137 {
2138 ++bd.endspaces;
2139 ++n;
2140 }
2141 numc = numc / 2;
2142 }
2143
2144 /* Compute bytes needed, move character count to num_chars. */
2145 num_chars = numc;
2146 numc *= (*mb_char2len)(c);
2147#endif
2148 /* oldlen includes textlen, so don't double count */
2149 n += numc - bd.textlen;
2150
2151 oldp = ml_get_curline();
2152 oldlen = STRLEN(oldp);
2153 newp = alloc_check((unsigned)oldlen + 1 + n);
2154 if (newp == NULL)
2155 continue;
2156 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2157 /* copy up to deleted part */
2158 mch_memmove(newp, oldp, (size_t)bd.textcol);
2159 oldp += bd.textcol + bd.textlen;
2160 /* insert pre-spaces */
2161 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2162 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2163#ifdef FEAT_MBYTE
2164 if (has_mbyte)
2165 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002166 n = (int)STRLEN(newp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 while (--num_chars >= 0)
2168 n += (*mb_char2bytes)(c, newp + n);
2169 }
2170 else
2171#endif
2172 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2173 if (!bd.is_short)
2174 {
2175 /* insert post-spaces */
2176 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2177 /* copy the part after the changed part */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002178 STRMOVE(newp + STRLEN(newp), oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 /* replace the line */
2181 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2182 }
2183 }
2184 else
2185 {
2186 /*
2187 * MCHAR and MLINE motion replace.
2188 */
2189 if (oap->motion_type == MLINE)
2190 {
2191 oap->start.col = 0;
2192 curwin->w_cursor.col = 0;
2193 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2194 if (oap->end.col)
2195 --oap->end.col;
2196 }
2197 else if (!oap->inclusive)
2198 dec(&(oap->end));
2199
2200 while (ltoreq(curwin->w_cursor, oap->end))
2201 {
2202 n = gchar_cursor();
2203 if (n != NUL)
2204 {
2205#ifdef FEAT_MBYTE
2206 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2207 {
2208 /* This is slow, but it handles replacing a single-byte
2209 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002210 if (curwin->w_cursor.lnum == oap->end.lnum)
2211 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 n = State;
2213 State = REPLACE;
2214 ins_char(c);
2215 State = n;
2216 /* Backup to the replaced character. */
2217 dec_cursor();
2218 }
2219 else
2220#endif
2221 {
2222#ifdef FEAT_VIRTUALEDIT
2223 if (n == TAB)
2224 {
2225 int end_vcol = 0;
2226
2227 if (curwin->w_cursor.lnum == oap->end.lnum)
2228 {
2229 /* oap->end has to be recalculated when
2230 * the tab breaks */
2231 end_vcol = getviscol2(oap->end.col,
2232 oap->end.coladd);
2233 }
2234 coladvance_force(getviscol());
2235 if (curwin->w_cursor.lnum == oap->end.lnum)
2236 getvpos(&oap->end, end_vcol);
2237 }
2238#endif
2239 pchar(curwin->w_cursor, c);
2240 }
2241 }
2242#ifdef FEAT_VIRTUALEDIT
2243 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2244 {
2245 int virtcols = oap->end.coladd;
2246
2247 if (curwin->w_cursor.lnum == oap->start.lnum
2248 && oap->start.col == oap->end.col && oap->start.coladd)
2249 virtcols -= oap->start.coladd;
2250
2251 /* oap->end has been trimmed so it's effectively inclusive;
2252 * as a result an extra +1 must be counted so we don't
2253 * trample the NUL byte. */
2254 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2255 curwin->w_cursor.col -= (virtcols + 1);
2256 for (; virtcols >= 0; virtcols--)
2257 {
2258 pchar(curwin->w_cursor, c);
2259 if (inc(&curwin->w_cursor) == -1)
2260 break;
2261 }
2262 }
2263#endif
2264
2265 /* Advance to next character, stop at the end of the file. */
2266 if (inc_cursor() == -1)
2267 break;
2268 }
2269 }
2270
2271 curwin->w_cursor = oap->start;
2272 check_cursor();
2273 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2274
2275 /* Set "'[" and "']" marks. */
2276 curbuf->b_op_start = oap->start;
2277 curbuf->b_op_end = oap->end;
2278
2279 return OK;
2280}
2281#endif
2282
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002283static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285/*
2286 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2287 */
2288 void
2289op_tilde(oap)
2290 oparg_T *oap;
2291{
2292 pos_T pos;
2293#ifdef FEAT_VISUAL
2294 struct block_def bd;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002295#endif
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002296 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 if (u_save((linenr_T)(oap->start.lnum - 1),
2299 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2300 return;
2301
2302 pos = oap->start;
2303#ifdef FEAT_VISUAL
2304 if (oap->block_mode) /* Visual block mode */
2305 {
2306 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2307 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002308 int one_change;
2309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 block_prep(oap, &bd, pos.lnum, FALSE);
2311 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002312 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2313 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002316 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2319
Bram Moolenaar009b2592004-10-24 19:18:58 +00002320 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2321 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002323 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325# endif
2326 }
2327 if (did_change)
2328 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2329 }
2330 else /* not block mode */
2331#endif
2332 {
2333 if (oap->motion_type == MLINE)
2334 {
2335 oap->start.col = 0;
2336 pos.col = 0;
2337 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2338 if (oap->end.col)
2339 --oap->end.col;
2340 }
2341 else if (!oap->inclusive)
2342 dec(&(oap->end));
2343
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002344 if (pos.lnum == oap->end.lnum)
2345 did_change = swapchars(oap->op_type, &pos,
2346 oap->end.col - pos.col + 1);
2347 else
2348 for (;;)
2349 {
2350 did_change |= swapchars(oap->op_type, &pos,
2351 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2352 (int)STRLEN(ml_get_pos(&pos)));
2353 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2354 break;
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if (did_change)
2357 {
2358 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2359 0L);
2360#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002361 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 char_u *ptr;
2364 int count;
2365
2366 pos = oap->start;
2367 while (pos.lnum < oap->end.lnum)
2368 {
2369 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002370 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002371 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002373 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 pos.col = 0;
2375 pos.lnum++;
2376 }
2377 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2378 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002379 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002381 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
2383#endif
2384 }
2385 }
2386
2387#ifdef FEAT_VISUAL
2388 if (!did_change && oap->is_VIsual)
2389 /* No change: need to remove the Visual selection */
2390 redraw_curbuf_later(INVERTED);
2391#endif
2392
2393 /*
2394 * Set '[ and '] marks.
2395 */
2396 curbuf->b_op_start = oap->start;
2397 curbuf->b_op_end = oap->end;
2398
2399 if (oap->line_count > p_report)
2400 {
2401 if (oap->line_count == 1)
2402 MSG(_("1 line changed"));
2403 else
2404 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2405 }
2406}
2407
2408/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002409 * Invoke swapchar() on "length" bytes at position "pos".
2410 * "pos" is advanced to just after the changed characters.
2411 * "length" is rounded up to include the whole last multi-byte character.
2412 * Also works correctly when the number of bytes changes.
2413 * Returns TRUE if some character was changed.
2414 */
2415 static int
2416swapchars(op_type, pos, length)
2417 int op_type;
2418 pos_T *pos;
2419 int length;
2420{
2421 int todo;
2422 int did_change = 0;
2423
2424 for (todo = length; todo > 0; --todo)
2425 {
2426# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002427 if (has_mbyte)
2428 /* we're counting bytes, not characters */
2429 todo -= (*mb_ptr2len)(ml_get_pos(pos)) - 1;
2430# endif
2431 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002432 if (inc(pos) == -1) /* at end of file */
2433 break;
2434 }
2435 return did_change;
2436}
2437
2438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * If op_type == OP_UPPER: make uppercase,
2440 * if op_type == OP_LOWER: make lowercase,
2441 * if op_type == OP_ROT13: do rot13 encoding,
2442 * else swap case of character at 'pos'
2443 * returns TRUE when something actually changed.
2444 */
2445 int
2446swapchar(op_type, pos)
2447 int op_type;
2448 pos_T *pos;
2449{
2450 int c;
2451 int nc;
2452
2453 c = gchar_pos(pos);
2454
2455 /* Only do rot13 encoding for ASCII characters. */
2456 if (c >= 0x80 && op_type == OP_ROT13)
2457 return FALSE;
2458
2459#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002460 if (op_type == OP_UPPER && c == 0xdf
2461 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002462 {
2463 pos_T sp = curwin->w_cursor;
2464
2465 /* Special handling of German sharp s: change to "SS". */
2466 curwin->w_cursor = *pos;
2467 del_char(FALSE);
2468 ins_char('S');
2469 ins_char('S');
2470 curwin->w_cursor = sp;
2471 inc(pos);
2472 }
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2475 return FALSE;
2476#endif
2477 nc = c;
2478 if (MB_ISLOWER(c))
2479 {
2480 if (op_type == OP_ROT13)
2481 nc = ROT13(c, 'a');
2482 else if (op_type != OP_LOWER)
2483 nc = MB_TOUPPER(c);
2484 }
2485 else if (MB_ISUPPER(c))
2486 {
2487 if (op_type == OP_ROT13)
2488 nc = ROT13(c, 'A');
2489 else if (op_type != OP_UPPER)
2490 nc = MB_TOLOWER(c);
2491 }
2492 if (nc != c)
2493 {
2494#ifdef FEAT_MBYTE
2495 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2496 {
2497 pos_T sp = curwin->w_cursor;
2498
2499 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002500 /* don't use del_char(), it also removes composing chars */
2501 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 ins_char(nc);
2503 curwin->w_cursor = sp;
2504 }
2505 else
2506#endif
2507 pchar(*pos, nc);
2508 return TRUE;
2509 }
2510 return FALSE;
2511}
2512
2513#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2514/*
2515 * op_insert - Insert and append operators for Visual mode.
2516 */
2517 void
2518op_insert(oap, count1)
2519 oparg_T *oap;
2520 long count1;
2521{
2522 long ins_len, pre_textlen = 0;
2523 char_u *firstline, *ins_text;
2524 struct block_def bd;
2525 int i;
2526
2527 /* edit() changes this - record it for OP_APPEND */
2528 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2529
2530 /* vis block is still marked. Get rid of it now. */
2531 curwin->w_cursor.lnum = oap->start.lnum;
2532 update_screen(INVERTED);
2533
2534 if (oap->block_mode)
2535 {
2536#ifdef FEAT_VIRTUALEDIT
2537 /* When 'virtualedit' is used, need to insert the extra spaces before
2538 * doing block_prep(). When only "block" is used, virtual edit is
2539 * already disabled, but still need it when calling
2540 * coladvance_force(). */
2541 if (curwin->w_cursor.coladd > 0)
2542 {
2543 int old_ve_flags = ve_flags;
2544
2545 ve_flags = VE_ALL;
2546 if (u_save_cursor() == FAIL)
2547 return;
2548 coladvance_force(oap->op_type == OP_APPEND
2549 ? oap->end_vcol + 1 : getviscol());
2550 if (oap->op_type == OP_APPEND)
2551 --curwin->w_cursor.col;
2552 ve_flags = old_ve_flags;
2553 }
2554#endif
2555 /* Get the info about the block before entering the text */
2556 block_prep(oap, &bd, oap->start.lnum, TRUE);
2557 firstline = ml_get(oap->start.lnum) + bd.textcol;
2558 if (oap->op_type == OP_APPEND)
2559 firstline += bd.textlen;
2560 pre_textlen = (long)STRLEN(firstline);
2561 }
2562
2563 if (oap->op_type == OP_APPEND)
2564 {
2565 if (oap->block_mode
2566#ifdef FEAT_VIRTUALEDIT
2567 && curwin->w_cursor.coladd == 0
2568#endif
2569 )
2570 {
2571 /* Move the cursor to the character right of the block. */
2572 curwin->w_set_curswant = TRUE;
2573 while (*ml_get_cursor() != NUL
2574 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2575 ++curwin->w_cursor.col;
2576 if (bd.is_short && !bd.is_MAX)
2577 {
2578 /* First line was too short, make it longer and adjust the
2579 * values in "bd". */
2580 if (u_save_cursor() == FAIL)
2581 return;
2582 for (i = 0; i < bd.endspaces; ++i)
2583 ins_char(' ');
2584 bd.textlen += bd.endspaces;
2585 }
2586 }
2587 else
2588 {
2589 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002590 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 /* Works just like an 'i'nsert on the next character. */
2593 if (!lineempty(curwin->w_cursor.lnum)
2594 && oap->start_vcol != oap->end_vcol)
2595 inc_cursor();
2596 }
2597 }
2598
2599 edit(NUL, FALSE, (linenr_T)count1);
2600
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002601 /* If user has moved off this line, we don't know what to do, so do
2602 * nothing.
2603 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2604 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 return;
2606
2607 if (oap->block_mode)
2608 {
2609 struct block_def bd2;
2610
2611 /*
2612 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002613 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 * Don't do this when "$" used, end-of-line will have changed.
2615 */
2616 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2617 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2618 {
2619 if (oap->op_type == OP_APPEND)
2620 {
2621 pre_textlen += bd2.textlen - bd.textlen;
2622 if (bd2.endspaces)
2623 --bd2.textlen;
2624 }
2625 bd.textcol = bd2.textcol;
2626 bd.textlen = bd2.textlen;
2627 }
2628
2629 /*
2630 * Subsequent calls to ml_get() flush the firstline data - take a
2631 * copy of the required string.
2632 */
2633 firstline = ml_get(oap->start.lnum) + bd.textcol;
2634 if (oap->op_type == OP_APPEND)
2635 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002636 if (pre_textlen >= 0
2637 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 ins_text = vim_strnsave(firstline, (int)ins_len);
2640 if (ins_text != NULL)
2641 {
2642 /* block handled here */
2643 if (u_save(oap->start.lnum,
2644 (linenr_T)(oap->end.lnum + 1)) == OK)
2645 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2646 &bd);
2647
2648 curwin->w_cursor.col = oap->start.col;
2649 check_cursor();
2650 vim_free(ins_text);
2651 }
2652 }
2653 }
2654}
2655#endif
2656
2657/*
2658 * op_change - handle a change operation
2659 *
2660 * return TRUE if edit() returns because of a CTRL-O command
2661 */
2662 int
2663op_change(oap)
2664 oparg_T *oap;
2665{
2666 colnr_T l;
2667 int retval;
2668#ifdef FEAT_VISUALEXTRA
2669 long offset;
2670 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002671 long ins_len;
2672 long pre_textlen = 0;
2673 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 char_u *firstline;
2675 char_u *ins_text, *newp, *oldp;
2676 struct block_def bd;
2677#endif
2678
2679 l = oap->start.col;
2680 if (oap->motion_type == MLINE)
2681 {
2682 l = 0;
2683#ifdef FEAT_SMARTINDENT
2684 if (!p_paste && curbuf->b_p_si
2685# ifdef FEAT_CINDENT
2686 && !curbuf->b_p_cin
2687# endif
2688 )
2689 can_si = TRUE; /* It's like opening a new line, do si */
2690#endif
2691 }
2692
2693 /* First delete the text in the region. In an empty buffer only need to
2694 * save for undo */
2695 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2696 {
2697 if (u_save_cursor() == FAIL)
2698 return FALSE;
2699 }
2700 else if (op_delete(oap) == FAIL)
2701 return FALSE;
2702
2703 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2704 && !virtual_op)
2705 inc_cursor();
2706
2707#ifdef FEAT_VISUALEXTRA
2708 /* check for still on same line (<CR> in inserted text meaningless) */
2709 /* skip blank lines too */
2710 if (oap->block_mode)
2711 {
2712# ifdef FEAT_VIRTUALEDIT
2713 /* Add spaces before getting the current line length. */
2714 if (virtual_op && (curwin->w_cursor.coladd > 0
2715 || gchar_cursor() == NUL))
2716 coladvance_force(getviscol());
2717# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002718 firstline = ml_get(oap->start.lnum);
2719 pre_textlen = (long)STRLEN(firstline);
2720 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 bd.textcol = curwin->w_cursor.col;
2722 }
2723#endif
2724
2725#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2726 if (oap->motion_type == MLINE)
2727 fix_indent();
2728#endif
2729
2730 retval = edit(NUL, FALSE, (linenr_T)1);
2731
2732#ifdef FEAT_VISUALEXTRA
2733 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002734 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002736 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002738 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002740 /* Auto-indenting may have changed the indent. If the cursor was past
2741 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002743 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002745 long new_indent = (long)(skipwhite(firstline) - firstline);
2746
2747 pre_textlen += new_indent - pre_indent;
2748 bd.textcol += new_indent - pre_indent;
2749 }
2750
2751 ins_len = (long)STRLEN(firstline) - pre_textlen;
2752 if (ins_len > 0)
2753 {
2754 /* Subsequent calls to ml_get() flush the firstline data - take a
2755 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2757 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002758 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2760 linenr++)
2761 {
2762 block_prep(oap, &bd, linenr, TRUE);
2763 if (!bd.is_short || virtual_op)
2764 {
2765# ifdef FEAT_VIRTUALEDIT
2766 pos_T vpos;
2767
2768 /* If the block starts in virtual space, count the
2769 * initial coladd offset as part of "startspaces" */
2770 if (bd.is_short)
2771 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002772 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 else
2776 vpos.coladd = 0;
2777# endif
2778 oldp = ml_get(linenr);
2779 newp = alloc_check((unsigned)(STRLEN(oldp)
2780# ifdef FEAT_VIRTUALEDIT
2781 + vpos.coladd
2782# endif
2783 + ins_len + 1));
2784 if (newp == NULL)
2785 continue;
2786 /* copy up to block start */
2787 mch_memmove(newp, oldp, (size_t)bd.textcol);
2788 offset = bd.textcol;
2789# ifdef FEAT_VIRTUALEDIT
2790 copy_spaces(newp + offset, (size_t)vpos.coladd);
2791 offset += vpos.coladd;
2792# endif
2793 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2794 offset += ins_len;
2795 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002796 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 ml_replace(linenr, newp, FALSE);
2798 }
2799 }
2800 check_cursor();
2801
2802 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2803 }
2804 vim_free(ins_text);
2805 }
2806 }
2807#endif
2808
2809 return retval;
2810}
2811
2812/*
2813 * set all the yank registers to empty (called from main())
2814 */
2815 void
2816init_yank()
2817{
2818 int i;
2819
2820 for (i = 0; i < NUM_REGISTERS; ++i)
2821 y_regs[i].y_array = NULL;
2822}
2823
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002824#if defined(EXITFREE) || defined(PROTO)
2825 void
2826clear_registers()
2827{
2828 int i;
2829
2830 for (i = 0; i < NUM_REGISTERS; ++i)
2831 {
2832 y_current = &y_regs[i];
2833 if (y_current->y_array != NULL)
2834 free_yank_all();
2835 }
2836}
2837#endif
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839/*
2840 * Free "n" lines from the current yank register.
2841 * Called for normal freeing and in case of error.
2842 */
2843 static void
2844free_yank(n)
2845 long n;
2846{
2847 if (y_current->y_array != NULL)
2848 {
2849 long i;
2850
2851 for (i = n; --i >= 0; )
2852 {
2853#ifdef AMIGA /* only for very slow machines */
2854 if ((i & 1023) == 1023) /* this may take a while */
2855 {
2856 /*
2857 * This message should never cause a hit-return message.
2858 * Overwrite this message with any next message.
2859 */
2860 ++no_wait_return;
2861 smsg((char_u *)_("freeing %ld lines"), i + 1);
2862 --no_wait_return;
2863 msg_didout = FALSE;
2864 msg_col = 0;
2865 }
2866#endif
2867 vim_free(y_current->y_array[i]);
2868 }
2869 vim_free(y_current->y_array);
2870 y_current->y_array = NULL;
2871#ifdef AMIGA
2872 if (n >= 1000)
2873 MSG("");
2874#endif
2875 }
2876}
2877
2878 static void
2879free_yank_all()
2880{
2881 free_yank(y_current->y_size);
2882}
2883
2884/*
2885 * Yank the text between "oap->start" and "oap->end" into a yank register.
2886 * If we are to append (uppercase register), we first yank into a new yank
2887 * register and then concatenate the old and the new one (so we keep the old
2888 * one in case of out-of-memory).
2889 *
2890 * return FAIL for failure, OK otherwise
2891 */
2892 int
2893op_yank(oap, deleting, mess)
2894 oparg_T *oap;
2895 int deleting;
2896 int mess;
2897{
2898 long y_idx; /* index in y_array[] */
2899 struct yankreg *curr; /* copy of y_current */
2900 struct yankreg newreg; /* new yank register when appending */
2901 char_u **new_ptr;
2902 linenr_T lnum; /* current line number */
2903 long j;
2904 int yanktype = oap->motion_type;
2905 long yanklines = oap->line_count;
2906 linenr_T yankendlnum = oap->end.lnum;
2907 char_u *p;
2908 char_u *pnew;
2909 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002910#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002911 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 /* check for read-only register */
2915 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2916 {
2917 beep_flush();
2918 return FAIL;
2919 }
2920 if (oap->regname == '_') /* black hole: nothing to do */
2921 return OK;
2922
2923#ifdef FEAT_CLIPBOARD
2924 if (!clip_star.available && oap->regname == '*')
2925 oap->regname = 0;
2926 else if (!clip_plus.available && oap->regname == '+')
2927 oap->regname = 0;
2928#endif
2929
2930 if (!deleting) /* op_delete() already set y_current */
2931 get_yank_register(oap->regname, TRUE);
2932
2933 curr = y_current;
2934 /* append to existing contents */
2935 if (y_append && y_current->y_array != NULL)
2936 y_current = &newreg;
2937 else
2938 free_yank_all(); /* free previously yanked lines */
2939
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002940 /*
2941 * If the cursor was in column 1 before and after the movement, and the
2942 * operator is not inclusive, the yank is always linewise.
2943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if ( oap->motion_type == MCHAR
2945 && oap->start.col == 0
2946 && !oap->inclusive
2947#ifdef FEAT_VISUAL
2948 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002949 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950#endif
2951 && oap->end.col == 0
2952 && yanklines > 1)
2953 {
2954 yanktype = MLINE;
2955 --yankendlnum;
2956 --yanklines;
2957 }
2958
2959 y_current->y_size = yanklines;
2960 y_current->y_type = yanktype; /* set the yank register type */
2961#ifdef FEAT_VISUAL
2962 y_current->y_width = 0;
2963#endif
2964 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2965 yanklines), TRUE);
2966
2967 if (y_current->y_array == NULL)
2968 {
2969 y_current = curr;
2970 return FAIL;
2971 }
2972
2973 y_idx = 0;
2974 lnum = oap->start.lnum;
2975
2976#ifdef FEAT_VISUAL
2977 if (oap->block_mode)
2978 {
2979 /* Visual block mode */
2980 y_current->y_type = MBLOCK; /* set the yank register type */
2981 y_current->y_width = oap->end_vcol - oap->start_vcol;
2982
2983 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2984 y_current->y_width--;
2985 }
2986#endif
2987
2988 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2989 {
2990 switch (y_current->y_type)
2991 {
2992#ifdef FEAT_VISUAL
2993 case MBLOCK:
2994 block_prep(oap, &bd, lnum, FALSE);
2995 if (yank_copy_line(&bd, y_idx) == FAIL)
2996 goto fail;
2997 break;
2998#endif
2999
3000 case MLINE:
3001 if ((y_current->y_array[y_idx] =
3002 vim_strsave(ml_get(lnum))) == NULL)
3003 goto fail;
3004 break;
3005
3006 case MCHAR:
3007 {
3008 colnr_T startcol = 0, endcol = MAXCOL;
3009#ifdef FEAT_VIRTUALEDIT
3010 int is_oneChar = FALSE;
3011 colnr_T cs, ce;
3012#endif
3013 p = ml_get(lnum);
3014 bd.startspaces = 0;
3015 bd.endspaces = 0;
3016
3017 if (lnum == oap->start.lnum)
3018 {
3019 startcol = oap->start.col;
3020#ifdef FEAT_VIRTUALEDIT
3021 if (virtual_op)
3022 {
3023 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3024 if (ce != cs && oap->start.coladd > 0)
3025 {
3026 /* Part of a tab selected -- but don't
3027 * double-count it. */
3028 bd.startspaces = (ce - cs + 1)
3029 - oap->start.coladd;
3030 startcol++;
3031 }
3032 }
3033#endif
3034 }
3035
3036 if (lnum == oap->end.lnum)
3037 {
3038 endcol = oap->end.col;
3039#ifdef FEAT_VIRTUALEDIT
3040 if (virtual_op)
3041 {
3042 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3043 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3044# ifdef FEAT_MBYTE
3045 /* Don't add space for double-wide
3046 * char; endcol will be on last byte
3047 * of multi-byte char. */
3048 && (*mb_head_off)(p, p + endcol) == 0
3049# endif
3050 ))
3051 {
3052 if (oap->start.lnum == oap->end.lnum
3053 && oap->start.col == oap->end.col)
3054 {
3055 /* Special case: inside a single char */
3056 is_oneChar = TRUE;
3057 bd.startspaces = oap->end.coladd
3058 - oap->start.coladd + oap->inclusive;
3059 endcol = startcol;
3060 }
3061 else
3062 {
3063 bd.endspaces = oap->end.coladd
3064 + oap->inclusive;
3065 endcol -= oap->inclusive;
3066 }
3067 }
3068 }
3069#endif
3070 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003071 if (endcol == MAXCOL)
3072 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (startcol > endcol
3074#ifdef FEAT_VIRTUALEDIT
3075 || is_oneChar
3076#endif
3077 )
3078 bd.textlen = 0;
3079 else
3080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 bd.textlen = endcol - startcol + oap->inclusive;
3082 }
3083 bd.textstart = p + startcol;
3084 if (yank_copy_line(&bd, y_idx) == FAIL)
3085 goto fail;
3086 break;
3087 }
3088 /* NOTREACHED */
3089 }
3090 }
3091
3092 if (curr != y_current) /* append the new block to the old block */
3093 {
3094 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3095 (curr->y_size + y_current->y_size)), TRUE);
3096 if (new_ptr == NULL)
3097 goto fail;
3098 for (j = 0; j < curr->y_size; ++j)
3099 new_ptr[j] = curr->y_array[j];
3100 vim_free(curr->y_array);
3101 curr->y_array = new_ptr;
3102
3103 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3104 curr->y_type = MLINE;
3105
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003106 /* Concatenate the last line of the old block with the first line of
3107 * the new block, unless being Vi compatible. */
3108 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3111 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3112 if (pnew == NULL)
3113 {
3114 y_idx = y_current->y_size - 1;
3115 goto fail;
3116 }
3117 STRCPY(pnew, curr->y_array[--j]);
3118 STRCAT(pnew, y_current->y_array[0]);
3119 vim_free(curr->y_array[j]);
3120 vim_free(y_current->y_array[0]);
3121 curr->y_array[j++] = pnew;
3122 y_idx = 1;
3123 }
3124 else
3125 y_idx = 0;
3126 while (y_idx < y_current->y_size)
3127 curr->y_array[j++] = y_current->y_array[y_idx++];
3128 curr->y_size = j;
3129 vim_free(y_current->y_array);
3130 y_current = curr;
3131 }
3132 if (mess) /* Display message about yank? */
3133 {
3134 if (yanktype == MCHAR
3135#ifdef FEAT_VISUAL
3136 && !oap->block_mode
3137#endif
3138 && yanklines == 1)
3139 yanklines = 0;
3140 /* Some versions of Vi use ">=" here, some don't... */
3141 if (yanklines > p_report)
3142 {
3143 /* redisplay now, so message is not deleted */
3144 update_topline_redraw();
3145 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003146 {
3147#ifdef FEAT_VISUAL
3148 if (oap->block_mode)
3149 MSG(_("block of 1 line yanked"));
3150 else
3151#endif
3152 MSG(_("1 line yanked"));
3153 }
3154#ifdef FEAT_VISUAL
3155 else if (oap->block_mode)
3156 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 else
3159 smsg((char_u *)_("%ld lines yanked"), yanklines);
3160 }
3161 }
3162
3163 /*
3164 * Set "'[" and "']" marks.
3165 */
3166 curbuf->b_op_start = oap->start;
3167 curbuf->b_op_end = oap->end;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003168 if (yanktype == MLINE
3169#ifdef FEAT_VISUAL
3170 && !oap->block_mode
3171#endif
3172 )
3173 {
3174 curbuf->b_op_start.col = 0;
3175 curbuf->b_op_end.col = MAXCOL;
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178#ifdef FEAT_CLIPBOARD
3179 /*
3180 * If we were yanking to the '*' register, send result to clipboard.
3181 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3182 * to the '*' register.
3183 */
3184 if (clip_star.available
3185 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003186 || (!deleting && oap->regname == 0
3187 && (clip_unnamed & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 {
3189 if (curr != &(y_regs[STAR_REGISTER]))
3190 /* Copy the text from register 0 to the clipboard register. */
3191 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3192
3193 clip_own_selection(&clip_star);
3194 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003195# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003196 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199
3200# ifdef FEAT_X11
3201 /*
3202 * If we were yanking to the '+' register, send result to selection.
3203 * Also copy to the '*' register, in case auto-select is off.
3204 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003205 if (clip_plus.available
3206 && (curr == &(y_regs[PLUS_REGISTER])
3207 || (!deleting && oap->regname == 0
3208 && (clip_unnamed & CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003210 if (curr != &(y_regs[PLUS_REGISTER]))
3211 /* Copy the text from register 0 to the clipboard register. */
3212 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 clip_own_selection(&clip_plus);
3215 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003216 if (!clip_isautosel_star() && !did_star
3217 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3220 clip_own_selection(&clip_star);
3221 clip_gen_set_selection(&clip_star);
3222 }
3223 }
3224# endif
3225#endif
3226
3227 return OK;
3228
3229fail: /* free the allocated lines */
3230 free_yank(y_idx + 1);
3231 y_current = curr;
3232 return FAIL;
3233}
3234
3235 static int
3236yank_copy_line(bd, y_idx)
3237 struct block_def *bd;
3238 long y_idx;
3239{
3240 char_u *pnew;
3241
3242 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3243 == NULL)
3244 return FAIL;
3245 y_current->y_array[y_idx] = pnew;
3246 copy_spaces(pnew, (size_t)bd->startspaces);
3247 pnew += bd->startspaces;
3248 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3249 pnew += bd->textlen;
3250 copy_spaces(pnew, (size_t)bd->endspaces);
3251 pnew += bd->endspaces;
3252 *pnew = NUL;
3253 return OK;
3254}
3255
3256#ifdef FEAT_CLIPBOARD
3257/*
3258 * Make a copy of the y_current register to register "reg".
3259 */
3260 static void
3261copy_yank_reg(reg)
3262 struct yankreg *reg;
3263{
3264 struct yankreg *curr = y_current;
3265 long j;
3266
3267 y_current = reg;
3268 free_yank_all();
3269 *y_current = *curr;
3270 y_current->y_array = (char_u **)lalloc_clear(
3271 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3272 if (y_current->y_array == NULL)
3273 y_current->y_size = 0;
3274 else
3275 for (j = 0; j < y_current->y_size; ++j)
3276 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3277 {
3278 free_yank(j);
3279 y_current->y_size = 0;
3280 break;
3281 }
3282 y_current = curr;
3283}
3284#endif
3285
3286/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003287 * Put contents of register "regname" into the text.
3288 * Caller must check "regname" to be valid!
3289 * "flags": PUT_FIXINDENT make indent look nice
3290 * PUT_CURSEND leave cursor after end of new text
3291 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 */
3293 void
3294do_put(regname, dir, count, flags)
3295 int regname;
3296 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3297 long count;
3298 int flags;
3299{
3300 char_u *ptr;
3301 char_u *newp, *oldp;
3302 int yanklen;
3303 int totlen = 0; /* init for gcc */
3304 linenr_T lnum;
3305 colnr_T col;
3306 long i; /* index in y_array[] */
3307 int y_type;
3308 long y_size;
3309#ifdef FEAT_VISUAL
3310 int oldlen;
3311 long y_width = 0;
3312 colnr_T vcol;
3313 int delcount;
3314 int incr = 0;
3315 long j;
3316 struct block_def bd;
3317#endif
3318 char_u **y_array = NULL;
3319 long nr_lines = 0;
3320 pos_T new_cursor;
3321 int indent;
3322 int orig_indent = 0; /* init for gcc */
3323 int indent_diff = 0; /* init for gcc */
3324 int first_indent = TRUE;
3325 int lendiff = 0;
3326 pos_T old_pos;
3327 char_u *insert_string = NULL;
3328 int allocated = FALSE;
3329 long cnt;
3330
3331#ifdef FEAT_CLIPBOARD
3332 /* Adjust register name for "unnamed" in 'clipboard'. */
3333 adjust_clip_reg(&regname);
3334 (void)may_get_selection(regname);
3335#endif
3336
3337 if (flags & PUT_FIXINDENT)
3338 orig_indent = get_indent();
3339
3340 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3341 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3342
3343 /*
3344 * Using inserted text works differently, because the register includes
3345 * special characters (newlines, etc.).
3346 */
3347 if (regname == '.')
3348 {
3349 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3350 (count == -1 ? 'O' : 'i')), count, FALSE);
3351 /* Putting the text is done later, so can't really move the cursor to
3352 * the next character. Use "l" to simulate it. */
3353 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3354 stuffcharReadbuff('l');
3355 return;
3356 }
3357
3358 /*
3359 * For special registers '%' (file name), '#' (alternate file name) and
3360 * ':' (last command line), etc. we have to create a fake yank register.
3361 */
3362 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3363 {
3364 if (insert_string == NULL)
3365 return;
3366 }
3367
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003368#ifdef FEAT_AUTOCMD
3369 /* Autocommands may be executed when saving lines for undo, which may make
3370 * y_array invalid. Start undo now to avoid that. */
3371 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3372#endif
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (insert_string != NULL)
3375 {
3376 y_type = MCHAR;
3377#ifdef FEAT_EVAL
3378 if (regname == '=')
3379 {
3380 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003381 * characters.
3382 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 for (;;)
3384 {
3385 y_size = 0;
3386 ptr = insert_string;
3387 while (ptr != NULL)
3388 {
3389 if (y_array != NULL)
3390 y_array[y_size] = ptr;
3391 ++y_size;
3392 ptr = vim_strchr(ptr, '\n');
3393 if (ptr != NULL)
3394 {
3395 if (y_array != NULL)
3396 *ptr = NUL;
3397 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003398 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (*ptr == NUL)
3400 {
3401 y_type = MLINE;
3402 break;
3403 }
3404 }
3405 }
3406 if (y_array != NULL)
3407 break;
3408 y_array = (char_u **)alloc((unsigned)
3409 (y_size * sizeof(char_u *)));
3410 if (y_array == NULL)
3411 goto end;
3412 }
3413 }
3414 else
3415#endif
3416 {
3417 y_size = 1; /* use fake one-line yank register */
3418 y_array = &insert_string;
3419 }
3420 }
3421 else
3422 {
3423 get_yank_register(regname, FALSE);
3424
3425 y_type = y_current->y_type;
3426#ifdef FEAT_VISUAL
3427 y_width = y_current->y_width;
3428#endif
3429 y_size = y_current->y_size;
3430 y_array = y_current->y_array;
3431 }
3432
3433#ifdef FEAT_VISUAL
3434 if (y_type == MLINE)
3435 {
3436 if (flags & PUT_LINE_SPLIT)
3437 {
3438 /* "p" or "P" in Visual mode: split the lines to put the text in
3439 * between. */
3440 if (u_save_cursor() == FAIL)
3441 goto end;
3442 ptr = vim_strsave(ml_get_cursor());
3443 if (ptr == NULL)
3444 goto end;
3445 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3446 vim_free(ptr);
3447
3448 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3449 if (ptr == NULL)
3450 goto end;
3451 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3452 ++nr_lines;
3453 dir = FORWARD;
3454 }
3455 if (flags & PUT_LINE_FORWARD)
3456 {
3457 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003458 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 dir = FORWARD;
3460 }
3461 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3462 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3463 }
3464#endif
3465
3466 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3467 y_type = MLINE;
3468
3469 if (y_size == 0 || y_array == NULL)
3470 {
3471 EMSG2(_("E353: Nothing in register %s"),
3472 regname == 0 ? (char_u *)"\"" : transchar(regname));
3473 goto end;
3474 }
3475
3476#ifdef FEAT_VISUAL
3477 if (y_type == MBLOCK)
3478 {
3479 lnum = curwin->w_cursor.lnum + y_size + 1;
3480 if (lnum > curbuf->b_ml.ml_line_count)
3481 lnum = curbuf->b_ml.ml_line_count + 1;
3482 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3483 goto end;
3484 }
3485 else
3486#endif
3487 if (y_type == MLINE)
3488 {
3489 lnum = curwin->w_cursor.lnum;
3490#ifdef FEAT_FOLDING
3491 /* Correct line number for closed fold. Don't move the cursor yet,
3492 * u_save() uses it. */
3493 if (dir == BACKWARD)
3494 (void)hasFolding(lnum, &lnum, NULL);
3495 else
3496 (void)hasFolding(lnum, NULL, &lnum);
3497#endif
3498 if (dir == FORWARD)
3499 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003500 /* In an empty buffer the empty line is going to be replaced, include
3501 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003502 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 goto end;
3504#ifdef FEAT_FOLDING
3505 if (dir == FORWARD)
3506 curwin->w_cursor.lnum = lnum - 1;
3507 else
3508 curwin->w_cursor.lnum = lnum;
3509 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3510#endif
3511 }
3512 else if (u_save_cursor() == FAIL)
3513 goto end;
3514
3515 yanklen = (int)STRLEN(y_array[0]);
3516
3517#ifdef FEAT_VIRTUALEDIT
3518 if (ve_flags == VE_ALL && y_type == MCHAR)
3519 {
3520 if (gchar_cursor() == TAB)
3521 {
3522 /* Don't need to insert spaces when "p" on the last position of a
3523 * tab or "P" on the first position. */
3524 if (dir == FORWARD
3525 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3526 : curwin->w_cursor.coladd > 0)
3527 coladvance_force(getviscol());
3528 else
3529 curwin->w_cursor.coladd = 0;
3530 }
3531 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3532 coladvance_force(getviscol() + (dir == FORWARD));
3533 }
3534#endif
3535
3536 lnum = curwin->w_cursor.lnum;
3537 col = curwin->w_cursor.col;
3538
3539#ifdef FEAT_VISUAL
3540 /*
3541 * Block mode
3542 */
3543 if (y_type == MBLOCK)
3544 {
3545 char c = gchar_cursor();
3546 colnr_T endcol2 = 0;
3547
3548 if (dir == FORWARD && c != NUL)
3549 {
3550#ifdef FEAT_VIRTUALEDIT
3551 if (ve_flags == VE_ALL)
3552 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3553 else
3554#endif
3555 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3556
3557#ifdef FEAT_MBYTE
3558 if (has_mbyte)
3559 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003560 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
3562#endif
3563#ifdef FEAT_VIRTUALEDIT
3564 if (c != TAB || ve_flags != VE_ALL)
3565#endif
3566 ++curwin->w_cursor.col;
3567 ++col;
3568 }
3569 else
3570 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3571
3572#ifdef FEAT_VIRTUALEDIT
3573 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003574 if (ve_flags == VE_ALL
3575 && (curwin->w_cursor.coladd > 0
3576 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
3578 if (dir == FORWARD && c == NUL)
3579 ++col;
3580 if (dir != FORWARD && c != NUL)
3581 ++curwin->w_cursor.col;
3582 if (c == TAB)
3583 {
3584 if (dir == BACKWARD && curwin->w_cursor.col)
3585 curwin->w_cursor.col--;
3586 if (dir == FORWARD && col - 1 == endcol2)
3587 curwin->w_cursor.col++;
3588 }
3589 }
3590 curwin->w_cursor.coladd = 0;
3591#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003592 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 for (i = 0; i < y_size; ++i)
3594 {
3595 int spaces;
3596 char shortline;
3597
3598 bd.startspaces = 0;
3599 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 vcol = 0;
3601 delcount = 0;
3602
3603 /* add a new line */
3604 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3605 {
3606 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3607 (colnr_T)1, FALSE) == FAIL)
3608 break;
3609 ++nr_lines;
3610 }
3611 /* get the old line and advance to the position to insert at */
3612 oldp = ml_get_curline();
3613 oldlen = (int)STRLEN(oldp);
3614 for (ptr = oldp; vcol < col && *ptr; )
3615 {
3616 /* Count a tab for what it's worth (if list mode not on) */
3617 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3618 vcol += incr;
3619 }
3620 bd.textcol = (colnr_T)(ptr - oldp);
3621
3622 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3623
3624 if (vcol < col) /* line too short, padd with spaces */
3625 bd.startspaces = col - vcol;
3626 else if (vcol > col)
3627 {
3628 bd.endspaces = vcol - col;
3629 bd.startspaces = incr - bd.endspaces;
3630 --bd.textcol;
3631 delcount = 1;
3632#ifdef FEAT_MBYTE
3633 if (has_mbyte)
3634 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3635#endif
3636 if (oldp[bd.textcol] != TAB)
3637 {
3638 /* Only a Tab can be split into spaces. Other
3639 * characters will have to be moved to after the
3640 * block, causing misalignment. */
3641 delcount = 0;
3642 bd.endspaces = 0;
3643 }
3644 }
3645
3646 yanklen = (int)STRLEN(y_array[i]);
3647
3648 /* calculate number of spaces required to fill right side of block*/
3649 spaces = y_width + 1;
3650 for (j = 0; j < yanklen; j++)
3651 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3652 if (spaces < 0)
3653 spaces = 0;
3654
3655 /* insert the new text */
3656 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3657 newp = alloc_check((unsigned)totlen + oldlen + 1);
3658 if (newp == NULL)
3659 break;
3660 /* copy part up to cursor to new line */
3661 ptr = newp;
3662 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3663 ptr += bd.textcol;
3664 /* may insert some spaces before the new text */
3665 copy_spaces(ptr, (size_t)bd.startspaces);
3666 ptr += bd.startspaces;
3667 /* insert the new text */
3668 for (j = 0; j < count; ++j)
3669 {
3670 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3671 ptr += yanklen;
3672
3673 /* insert block's trailing spaces only if there's text behind */
3674 if ((j < count - 1 || !shortline) && spaces)
3675 {
3676 copy_spaces(ptr, (size_t)spaces);
3677 ptr += spaces;
3678 }
3679 }
3680 /* may insert some spaces after the new text */
3681 copy_spaces(ptr, (size_t)bd.endspaces);
3682 ptr += bd.endspaces;
3683 /* move the text after the cursor to the end of the line. */
3684 mch_memmove(ptr, oldp + bd.textcol + delcount,
3685 (size_t)(oldlen - bd.textcol - delcount + 1));
3686 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3687
3688 ++curwin->w_cursor.lnum;
3689 if (i == 0)
3690 curwin->w_cursor.col += bd.startspaces;
3691 }
3692
3693 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3694
3695 /* Set '[ mark. */
3696 curbuf->b_op_start = curwin->w_cursor;
3697 curbuf->b_op_start.lnum = lnum;
3698
3699 /* adjust '] mark */
3700 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3701 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003702# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (flags & PUT_CURSEND)
3706 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003707 colnr_T len;
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 curwin->w_cursor = curbuf->b_op_end;
3710 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003711
3712 /* in Insert mode we might be after the NUL, correct for that */
3713 len = (colnr_T)STRLEN(ml_get_curline());
3714 if (curwin->w_cursor.col > len)
3715 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 else
3718 curwin->w_cursor.lnum = lnum;
3719 }
3720 else
3721#endif
3722 {
3723 /*
3724 * Character or Line mode
3725 */
3726 if (y_type == MCHAR)
3727 {
3728 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3729 * char */
3730 if (dir == FORWARD && gchar_cursor() != NUL)
3731 {
3732#ifdef FEAT_MBYTE
3733 if (has_mbyte)
3734 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003735 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737 /* put it on the next of the multi-byte character. */
3738 col += bytelen;
3739 if (yanklen)
3740 {
3741 curwin->w_cursor.col += bytelen;
3742 curbuf->b_op_end.col += bytelen;
3743 }
3744 }
3745 else
3746#endif
3747 {
3748 ++col;
3749 if (yanklen)
3750 {
3751 ++curwin->w_cursor.col;
3752 ++curbuf->b_op_end.col;
3753 }
3754 }
3755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 curbuf->b_op_start = curwin->w_cursor;
3757 }
3758 /*
3759 * Line mode: BACKWARD is the same as FORWARD on the previous line
3760 */
3761 else if (dir == BACKWARD)
3762 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003763 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /*
3766 * simple case: insert into current line
3767 */
3768 if (y_type == MCHAR && y_size == 1)
3769 {
3770 totlen = count * yanklen;
3771 if (totlen)
3772 {
3773 oldp = ml_get(lnum);
3774 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3775 if (newp == NULL)
3776 goto end; /* alloc() will give error message */
3777 mch_memmove(newp, oldp, (size_t)col);
3778 ptr = newp + col;
3779 for (i = 0; i < count; ++i)
3780 {
3781 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3782 ptr += yanklen;
3783 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003784 STRMOVE(ptr, oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 ml_replace(lnum, newp, FALSE);
3786 /* Put cursor on last putted char. */
3787 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3788 }
3789 curbuf->b_op_end = curwin->w_cursor;
3790 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3791 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3792 ++curwin->w_cursor.col;
3793 changed_bytes(lnum, col);
3794 }
3795 else
3796 {
3797 /*
3798 * Insert at least one line. When y_type is MCHAR, break the first
3799 * line in two.
3800 */
3801 for (cnt = 1; cnt <= count; ++cnt)
3802 {
3803 i = 0;
3804 if (y_type == MCHAR)
3805 {
3806 /*
3807 * Split the current line in two at the insert position.
3808 * First insert y_array[size - 1] in front of second line.
3809 * Then append y_array[0] to first line.
3810 */
3811 lnum = new_cursor.lnum;
3812 ptr = ml_get(lnum) + col;
3813 totlen = (int)STRLEN(y_array[y_size - 1]);
3814 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3815 if (newp == NULL)
3816 goto error;
3817 STRCPY(newp, y_array[y_size - 1]);
3818 STRCAT(newp, ptr);
3819 /* insert second line */
3820 ml_append(lnum, newp, (colnr_T)0, FALSE);
3821 vim_free(newp);
3822
3823 oldp = ml_get(lnum);
3824 newp = alloc_check((unsigned)(col + yanklen + 1));
3825 if (newp == NULL)
3826 goto error;
3827 /* copy first part of line */
3828 mch_memmove(newp, oldp, (size_t)col);
3829 /* append to first line */
3830 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3831 ml_replace(lnum, newp, FALSE);
3832
3833 curwin->w_cursor.lnum = lnum;
3834 i = 1;
3835 }
3836
3837 for (; i < y_size; ++i)
3838 {
3839 if ((y_type != MCHAR || i < y_size - 1)
3840 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3841 == FAIL)
3842 goto error;
3843 lnum++;
3844 ++nr_lines;
3845 if (flags & PUT_FIXINDENT)
3846 {
3847 old_pos = curwin->w_cursor;
3848 curwin->w_cursor.lnum = lnum;
3849 ptr = ml_get(lnum);
3850 if (cnt == count && i == y_size - 1)
3851 lendiff = (int)STRLEN(ptr);
3852#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3853 if (*ptr == '#' && preprocs_left())
3854 indent = 0; /* Leave # lines at start */
3855 else
3856#endif
3857 if (*ptr == NUL)
3858 indent = 0; /* Ignore empty lines */
3859 else if (first_indent)
3860 {
3861 indent_diff = orig_indent - get_indent();
3862 indent = orig_indent;
3863 first_indent = FALSE;
3864 }
3865 else if ((indent = get_indent() + indent_diff) < 0)
3866 indent = 0;
3867 (void)set_indent(indent, 0);
3868 curwin->w_cursor = old_pos;
3869 /* remember how many chars were removed */
3870 if (cnt == count && i == y_size - 1)
3871 lendiff -= (int)STRLEN(ml_get(lnum));
3872 }
3873 }
3874 }
3875
3876error:
3877 /* Adjust marks. */
3878 if (y_type == MLINE)
3879 {
3880 curbuf->b_op_start.col = 0;
3881 if (dir == FORWARD)
3882 curbuf->b_op_start.lnum++;
3883 }
3884 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3885 (linenr_T)MAXLNUM, nr_lines, 0L);
3886
3887 /* note changed text for displaying and folding */
3888 if (y_type == MCHAR)
3889 changed_lines(curwin->w_cursor.lnum, col,
3890 curwin->w_cursor.lnum + 1, nr_lines);
3891 else
3892 changed_lines(curbuf->b_op_start.lnum, 0,
3893 curbuf->b_op_start.lnum, nr_lines);
3894
3895 /* put '] mark at last inserted character */
3896 curbuf->b_op_end.lnum = lnum;
3897 /* correct length for change in indent */
3898 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3899 if (col > 1)
3900 curbuf->b_op_end.col = col - 1;
3901 else
3902 curbuf->b_op_end.col = 0;
3903
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003904 if (flags & PUT_CURSLINE)
3905 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003906 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003907 curwin->w_cursor.lnum = lnum;
3908 beginline(BL_WHITE | BL_FIX);
3909 }
3910 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
3912 /* put cursor after inserted text */
3913 if (y_type == MLINE)
3914 {
3915 if (lnum >= curbuf->b_ml.ml_line_count)
3916 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3917 else
3918 curwin->w_cursor.lnum = lnum + 1;
3919 curwin->w_cursor.col = 0;
3920 }
3921 else
3922 {
3923 curwin->w_cursor.lnum = lnum;
3924 curwin->w_cursor.col = col;
3925 }
3926 }
3927 else if (y_type == MLINE)
3928 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003929 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 curwin->w_cursor.col = 0;
3931 if (dir == FORWARD)
3932 ++curwin->w_cursor.lnum;
3933 beginline(BL_WHITE | BL_FIX);
3934 }
3935 else /* put cursor on first inserted character */
3936 curwin->w_cursor = new_cursor;
3937 }
3938 }
3939
3940 msgmore(nr_lines);
3941 curwin->w_set_curswant = TRUE;
3942
3943end:
3944 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003946 if (regname == '=')
3947 vim_free(y_array);
3948
Bram Moolenaar677ee682005-01-27 14:41:15 +00003949 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003950 adjust_cursor_eol();
3951}
3952
3953/*
3954 * When the cursor is on the NUL past the end of the line and it should not be
3955 * there move it left.
3956 */
3957 void
3958adjust_cursor_eol()
3959{
3960 if (curwin->w_cursor.col > 0
3961 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003962#ifdef FEAT_VIRTUALEDIT
3963 && (ve_flags & VE_ONEMORE) == 0
3964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 && !(restart_edit || (State & INSERT)))
3966 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003967 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003968 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970#ifdef FEAT_VIRTUALEDIT
3971 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003972 {
3973 colnr_T scol, ecol;
3974
3975 /* Coladd is set to the width of the last character. */
3976 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3977 curwin->w_cursor.coladd = ecol - scol + 1;
3978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979#endif
3980 }
3981}
3982
3983#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3984/*
3985 * Return TRUE if lines starting with '#' should be left aligned.
3986 */
3987 int
3988preprocs_left()
3989{
3990 return
3991# ifdef FEAT_SMARTINDENT
3992# ifdef FEAT_CINDENT
3993 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3994# else
3995 curbuf->b_p_si
3996# endif
3997# endif
3998# ifdef FEAT_CINDENT
3999 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
4000# endif
4001 ;
4002}
4003#endif
4004
4005/* Return the character name of the register with the given number */
4006 int
4007get_register_name(num)
4008 int num;
4009{
4010 if (num == -1)
4011 return '"';
4012 else if (num < 10)
4013 return num + '0';
4014 else if (num == DELETION_REGISTER)
4015 return '-';
4016#ifdef FEAT_CLIPBOARD
4017 else if (num == STAR_REGISTER)
4018 return '*';
4019 else if (num == PLUS_REGISTER)
4020 return '+';
4021#endif
4022 else
4023 {
4024#ifdef EBCDIC
4025 int i;
4026
4027 /* EBCDIC is really braindead ... */
4028 i = 'a' + (num - 10);
4029 if (i > 'i')
4030 i += 7;
4031 if (i > 'r')
4032 i += 8;
4033 return i;
4034#else
4035 return num + 'a' - 10;
4036#endif
4037 }
4038}
4039
4040/*
4041 * ":dis" and ":registers": Display the contents of the yank registers.
4042 */
4043 void
4044ex_display(eap)
4045 exarg_T *eap;
4046{
4047 int i, n;
4048 long j;
4049 char_u *p;
4050 struct yankreg *yb;
4051 int name;
4052 int attr;
4053 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004054#ifdef FEAT_MBYTE
4055 int clen;
4056#else
4057# define clen 1
4058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 if (arg != NULL && *arg == NUL)
4061 arg = NULL;
4062 attr = hl_attr(HLF_8);
4063
4064 /* Highlight title */
4065 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4066 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4067 {
4068 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004069 if (arg != NULL && vim_strchr(arg, name) == NULL
4070#ifdef ONE_CLIPBOARD
4071 /* Star register and plus register contain the same thing. */
4072 && (name != '*' || vim_strchr(arg, '+') == NULL)
4073#endif
4074 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 continue; /* did not ask for this register */
4076
4077#ifdef FEAT_CLIPBOARD
4078 /* Adjust register name for "unnamed" in 'clipboard'.
4079 * When it's a clipboard register, fill it with the current contents
4080 * of the clipboard. */
4081 adjust_clip_reg(&name);
4082 (void)may_get_selection(name);
4083#endif
4084
4085 if (i == -1)
4086 {
4087 if (y_previous != NULL)
4088 yb = y_previous;
4089 else
4090 yb = &(y_regs[0]);
4091 }
4092 else
4093 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004094
4095#ifdef FEAT_EVAL
4096 if (name == MB_TOLOWER(redir_reg)
4097 || (redir_reg == '"' && yb == y_previous))
4098 continue; /* do not list register being written to, the
4099 * pointer can be freed */
4100#endif
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (yb->y_array != NULL)
4103 {
4104 msg_putchar('\n');
4105 msg_putchar('"');
4106 msg_putchar(name);
4107 MSG_PUTS(" ");
4108
4109 n = (int)Columns - 6;
4110 for (j = 0; j < yb->y_size && n > 1; ++j)
4111 {
4112 if (j)
4113 {
4114 MSG_PUTS_ATTR("^J", attr);
4115 n -= 2;
4116 }
4117 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004120 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004121#endif
4122 msg_outtrans_len(p, clen);
4123#ifdef FEAT_MBYTE
4124 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125#endif
4126 }
4127 }
4128 if (n > 1 && yb->y_type == MLINE)
4129 MSG_PUTS_ATTR("^J", attr);
4130 out_flush(); /* show one line at a time */
4131 }
4132 ui_breakcheck();
4133 }
4134
4135 /*
4136 * display last inserted text
4137 */
4138 if ((p = get_last_insert()) != NULL
4139 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4140 {
4141 MSG_PUTS("\n\". ");
4142 dis_msg(p, TRUE);
4143 }
4144
4145 /*
4146 * display last command line
4147 */
4148 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4149 && !got_int)
4150 {
4151 MSG_PUTS("\n\": ");
4152 dis_msg(last_cmdline, FALSE);
4153 }
4154
4155 /*
4156 * display current file name
4157 */
4158 if (curbuf->b_fname != NULL
4159 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4160 {
4161 MSG_PUTS("\n\"% ");
4162 dis_msg(curbuf->b_fname, FALSE);
4163 }
4164
4165 /*
4166 * display alternate file name
4167 */
4168 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4169 {
4170 char_u *fname;
4171 linenr_T dummy;
4172
4173 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4174 {
4175 MSG_PUTS("\n\"# ");
4176 dis_msg(fname, FALSE);
4177 }
4178 }
4179
4180 /*
4181 * display last search pattern
4182 */
4183 if (last_search_pat() != NULL
4184 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4185 {
4186 MSG_PUTS("\n\"/ ");
4187 dis_msg(last_search_pat(), FALSE);
4188 }
4189
4190#ifdef FEAT_EVAL
4191 /*
4192 * display last used expression
4193 */
4194 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4195 && !got_int)
4196 {
4197 MSG_PUTS("\n\"= ");
4198 dis_msg(expr_line, FALSE);
4199 }
4200#endif
4201}
4202
4203/*
4204 * display a string for do_dis()
4205 * truncate at end of screen line
4206 */
4207 static void
4208dis_msg(p, skip_esc)
4209 char_u *p;
4210 int skip_esc; /* if TRUE, ignore trailing ESC */
4211{
4212 int n;
4213#ifdef FEAT_MBYTE
4214 int l;
4215#endif
4216
4217 n = (int)Columns - 6;
4218 while (*p != NUL
4219 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4220 && (n -= ptr2cells(p)) >= 0)
4221 {
4222#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004223 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
4225 msg_outtrans_len(p, l);
4226 p += l;
4227 }
4228 else
4229#endif
4230 msg_outtrans_len(p++, 1);
4231 }
4232 ui_breakcheck();
4233}
4234
Bram Moolenaar81340392012-06-06 16:12:59 +02004235#if defined(FEAT_COMMENTS) || defined(PROTO)
4236/*
4237 * If "process" is TRUE and the line begins with a comment leader (possibly
4238 * after some white space), return a pointer to the text after it. Put a boolean
4239 * value indicating whether the line ends with an unclosed comment in
4240 * "is_comment".
4241 * line - line to be processed,
4242 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004243 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004244 * include_space - whether to also skip space following the comment leader,
4245 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004246 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004247 */
4248 static char_u *
4249skip_comment(line, process, include_space, is_comment)
4250 char_u *line;
4251 int process;
4252 int include_space;
4253 int *is_comment;
4254{
4255 char_u *comment_flags = NULL;
4256 int lead_len;
4257 int leader_offset = get_last_leader_offset(line, &comment_flags);
4258
4259 *is_comment = FALSE;
4260 if (leader_offset != -1)
4261 {
4262 /* Let's check whether the line ends with an unclosed comment.
4263 * If the last comment leader has COM_END in flags, there's no comment.
4264 */
4265 while (*comment_flags)
4266 {
4267 if (*comment_flags == COM_END
4268 || *comment_flags == ':')
4269 break;
4270 ++comment_flags;
4271 }
4272 if (*comment_flags != COM_END)
4273 *is_comment = TRUE;
4274 }
4275
4276 if (process == FALSE)
4277 return line;
4278
4279 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4280
4281 if (lead_len == 0)
4282 return line;
4283
4284 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004285 * - COM_END,
4286 * - colon,
4287 * whichever comes first.
4288 */
4289 while (*comment_flags)
4290 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004291 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004292 || *comment_flags == ':')
4293 {
4294 break;
4295 }
4296 ++comment_flags;
4297 }
4298
4299 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004300 * starting with a closing part of a three-part comment. That's good,
4301 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004302 */
4303 if (*comment_flags == ':' || *comment_flags == NUL)
4304 line += lead_len;
4305
4306 return line;
4307}
4308#endif
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004311 * Join 'count' lines (minimal 2) at cursor position.
4312 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaar81340392012-06-06 16:12:59 +02004313 * Set "use_formatoptions" to FALSE when e.g. processing
4314 * backspace and comment leaders should not be removed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004316 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 */
4318 int
Bram Moolenaar81340392012-06-06 16:12:59 +02004319do_join(count, insert_space, save_undo, use_formatoptions)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004320 long count;
4321 int insert_space;
4322 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004323 int use_formatoptions UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004325 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004326 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004327 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004329 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004330 int endcurr1 = NUL;
4331 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004332 int currsize = 0; /* size of the current line */
4333 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004335 colnr_T col = 0;
4336 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004337#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004338 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004339 int remove_comments = (use_formatoptions == TRUE)
4340 && has_format_option(FO_REMOVE_COMS);
4341 int prev_was_comment;
4342#endif
4343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004345 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4346 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004349 /* Allocate an array to store the number of spaces inserted before each
4350 * line. We will use it to pre-compute the length of the new line and the
4351 * proper placement of each original line in the new one. */
4352 spaces = lalloc_clear((long_u)count, TRUE);
4353 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004355#if defined(FEAT_COMMENTS) || defined(PROTO)
4356 if (remove_comments)
4357 {
4358 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4359 if (comments == NULL)
4360 {
4361 vim_free(spaces);
4362 return FAIL;
4363 }
4364 }
4365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004368 * Don't move anything, just compute the final line length
4369 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004371 for (t = 0; t < count; ++t)
4372 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004373 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar81340392012-06-06 16:12:59 +02004374#if defined(FEAT_COMMENTS) || defined(PROTO)
4375 if (remove_comments)
4376 {
4377 /* We don't want to remove the comment leader if the
4378 * previous line is not a comment. */
4379 if (t > 0 && prev_was_comment)
4380 {
4381
4382 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4383 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004384 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004385 curr = new_curr;
4386 }
4387 else
4388 curr = skip_comment(curr, FALSE, insert_space,
4389 &prev_was_comment);
4390 }
4391#endif
4392
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004393 if (insert_space && t > 0)
4394 {
4395 curr = skipwhite(curr);
4396 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4397#ifdef FEAT_MBYTE
4398 && (!has_format_option(FO_MBYTE_JOIN)
4399 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4400 && (!has_format_option(FO_MBYTE_JOIN2)
4401 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4402#endif
4403 )
4404 {
4405 /* don't add a space if the line is ending in a space */
4406 if (endcurr1 == ' ')
4407 endcurr1 = endcurr2;
4408 else
4409 ++spaces[t];
4410 /* extra space when 'joinspaces' set and line ends in '.' */
4411 if ( p_js
4412 && (endcurr1 == '.'
4413 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4414 && (endcurr1 == '?' || endcurr1 == '!'))))
4415 ++spaces[t];
4416 }
4417 }
4418 currsize = (int)STRLEN(curr);
4419 sumsize += currsize + spaces[t];
4420 endcurr1 = endcurr2 = NUL;
4421 if (insert_space && currsize > 0)
4422 {
4423#ifdef FEAT_MBYTE
4424 if (has_mbyte)
4425 {
4426 cend = curr + currsize;
4427 mb_ptr_back(curr, cend);
4428 endcurr1 = (*mb_ptr2char)(cend);
4429 if (cend > curr)
4430 {
4431 mb_ptr_back(curr, cend);
4432 endcurr2 = (*mb_ptr2char)(cend);
4433 }
4434 }
4435 else
4436#endif
4437 {
4438 endcurr1 = *(curr + currsize - 1);
4439 if (currsize > 1)
4440 endcurr2 = *(curr + currsize - 2);
4441 }
4442 }
4443 line_breakcheck();
4444 if (got_int)
4445 {
4446 ret = FAIL;
4447 goto theend;
4448 }
4449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004451 /* store the column position before last line */
4452 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004454 /* allocate the space for the new line */
4455 newp = alloc_check((unsigned)(sumsize + 1));
4456 cend = newp + sumsize;
4457 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004459 /*
4460 * Move affected lines to the new long one.
4461 *
4462 * Move marks from each deleted line to the joined line, adjusting the
4463 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4464 * should not really be a problem.
4465 */
4466 for (t = count - 1; ; --t)
4467 {
4468 cend -= currsize;
4469 mch_memmove(cend, curr, (size_t)currsize);
4470 if (spaces[t] > 0)
4471 {
4472 cend -= spaces[t];
4473 copy_spaces(cend, (size_t)(spaces[t]));
4474 }
4475 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004476 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004477 if (t == 0)
4478 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004479 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004480#if defined(FEAT_COMMENTS) || defined(PROTO)
4481 if (remove_comments)
4482 curr += comments[t - 1];
4483#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004484 if (insert_space && t > 1)
4485 curr = skipwhite(curr);
4486 currsize = (int)STRLEN(curr);
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4489
4490 /* Only report the change in the first line here, del_lines() will report
4491 * the deleted line. */
4492 changed_lines(curwin->w_cursor.lnum, currsize,
4493 curwin->w_cursor.lnum + 1, 0L);
4494
4495 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004496 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 * briefly, and then move it back. After del_lines() the cursor may
4498 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 */
4500 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004502 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 curwin->w_cursor.lnum = t;
4504
4505 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004506 * Set the cursor column:
4507 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004508 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004510 curwin->w_cursor.col =
4511 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514#ifdef FEAT_VIRTUALEDIT
4515 curwin->w_cursor.coladd = 0;
4516#endif
4517 curwin->w_set_curswant = TRUE;
4518
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004519theend:
4520 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004521#if defined(FEAT_COMMENTS) || defined(PROTO)
4522 if (remove_comments)
4523 vim_free(comments);
4524#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004525 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526}
4527
4528#ifdef FEAT_COMMENTS
4529/*
4530 * Return TRUE if the two comment leaders given are the same. "lnum" is
4531 * the first line. White-space is ignored. Note that the whole of
4532 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4533 */
4534 static int
4535same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4536 linenr_T lnum;
4537 int leader1_len;
4538 char_u *leader1_flags;
4539 int leader2_len;
4540 char_u *leader2_flags;
4541{
4542 int idx1 = 0, idx2 = 0;
4543 char_u *p;
4544 char_u *line1;
4545 char_u *line2;
4546
4547 if (leader1_len == 0)
4548 return (leader2_len == 0);
4549
4550 /*
4551 * If first leader has 'f' flag, the lines can be joined only if the
4552 * second line does not have a leader.
4553 * If first leader has 'e' flag, the lines can never be joined.
4554 * If fist leader has 's' flag, the lines can only be joined if there is
4555 * some text after it and the second line has the 'm' flag.
4556 */
4557 if (leader1_flags != NULL)
4558 {
4559 for (p = leader1_flags; *p && *p != ':'; ++p)
4560 {
4561 if (*p == COM_FIRST)
4562 return (leader2_len == 0);
4563 if (*p == COM_END)
4564 return FALSE;
4565 if (*p == COM_START)
4566 {
4567 if (*(ml_get(lnum) + leader1_len) == NUL)
4568 return FALSE;
4569 if (leader2_flags == NULL || leader2_len == 0)
4570 return FALSE;
4571 for (p = leader2_flags; *p && *p != ':'; ++p)
4572 if (*p == COM_MIDDLE)
4573 return TRUE;
4574 return FALSE;
4575 }
4576 }
4577 }
4578
4579 /*
4580 * Get current line and next line, compare the leaders.
4581 * The first line has to be saved, only one line can be locked at a time.
4582 */
4583 line1 = vim_strsave(ml_get(lnum));
4584 if (line1 != NULL)
4585 {
4586 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4587 ;
4588 line2 = ml_get(lnum + 1);
4589 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4590 {
4591 if (!vim_iswhite(line2[idx2]))
4592 {
4593 if (line1[idx1++] != line2[idx2])
4594 break;
4595 }
4596 else
4597 while (vim_iswhite(line1[idx1]))
4598 ++idx1;
4599 }
4600 vim_free(line1);
4601 }
4602 return (idx2 == leader2_len && idx1 == leader1_len);
4603}
4604#endif
4605
4606/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004607 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 */
4609 void
4610op_format(oap, keep_cursor)
4611 oparg_T *oap;
4612 int keep_cursor; /* keep cursor on same text char */
4613{
4614 long old_line_count = curbuf->b_ml.ml_line_count;
4615
4616 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4617 * can put it back there. */
4618 curwin->w_cursor = oap->cursor_start;
4619
4620 if (u_save((linenr_T)(oap->start.lnum - 1),
4621 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4622 return;
4623 curwin->w_cursor = oap->start;
4624
4625#ifdef FEAT_VISUAL
4626 if (oap->is_VIsual)
4627 /* When there is no change: need to remove the Visual selection */
4628 redraw_curbuf_later(INVERTED);
4629#endif
4630
4631 /* Set '[ mark at the start of the formatted area */
4632 curbuf->b_op_start = oap->start;
4633
4634 /* For "gw" remember the cursor position and put it back below (adjusted
4635 * for joined and split lines). */
4636 if (keep_cursor)
4637 saved_cursor = oap->cursor_start;
4638
Bram Moolenaar81a82092008-03-12 16:27:00 +00004639 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
4641 /*
4642 * Leave the cursor at the first non-blank of the last formatted line.
4643 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4644 * line, so "." will do the next lines.
4645 */
4646 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4647 ++curwin->w_cursor.lnum;
4648 beginline(BL_WHITE | BL_FIX);
4649 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4650 msgmore(old_line_count);
4651
4652 /* put '] mark on the end of the formatted area */
4653 curbuf->b_op_end = curwin->w_cursor;
4654
4655 if (keep_cursor)
4656 {
4657 curwin->w_cursor = saved_cursor;
4658 saved_cursor.lnum = 0;
4659 }
4660
4661#ifdef FEAT_VISUAL
4662 if (oap->is_VIsual)
4663 {
4664 win_T *wp;
4665
4666 FOR_ALL_WINDOWS(wp)
4667 {
4668 if (wp->w_old_cursor_lnum != 0)
4669 {
4670 /* When lines have been inserted or deleted, adjust the end of
4671 * the Visual area to be redrawn. */
4672 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4673 wp->w_old_cursor_lnum += old_line_count;
4674 else
4675 wp->w_old_visual_lnum += old_line_count;
4676 }
4677 }
4678 }
4679#endif
4680}
4681
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004682#if defined(FEAT_EVAL) || defined(PROTO)
4683/*
4684 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4685 */
4686 void
4687op_formatexpr(oap)
4688 oparg_T *oap;
4689{
4690# ifdef FEAT_VISUAL
4691 if (oap->is_VIsual)
4692 /* When there is no change: need to remove the Visual selection */
4693 redraw_curbuf_later(INVERTED);
4694# endif
4695
Bram Moolenaar700303e2010-07-11 17:35:50 +02004696 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4697 /* As documented: when 'formatexpr' returns non-zero fall back to
4698 * internal formatting. */
4699 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004700}
4701
4702 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004703fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004704 linenr_T lnum;
4705 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004706 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004707{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004708 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4709 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004710 int r;
4711
4712 /*
4713 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004714 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004715 */
4716 set_vim_var_nr(VV_LNUM, lnum);
4717 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004718 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004719
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004720 /*
4721 * Evaluate the function.
4722 */
4723 if (use_sandbox)
4724 ++sandbox;
4725 r = eval_to_number(curbuf->b_p_fex);
4726 if (use_sandbox)
4727 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004728
4729 set_vim_var_string(VV_CHAR, NULL, -1);
4730
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004731 return r;
4732}
4733#endif
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735/*
4736 * Format "line_count" lines, starting at the cursor position.
4737 * When "line_count" is negative, format until the end of the paragraph.
4738 * Lines after the cursor line are saved for undo, caller must have saved the
4739 * first line.
4740 */
4741 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004742format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004744 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 int max_len;
4747 int is_not_par; /* current line not part of parag. */
4748 int next_is_not_par; /* next line not part of paragraph */
4749 int is_end_par; /* at end of paragraph */
4750 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4751 int next_is_start_par = FALSE;
4752#ifdef FEAT_COMMENTS
4753 int leader_len = 0; /* leader len of current line */
4754 int next_leader_len; /* leader len of next line */
4755 char_u *leader_flags = NULL; /* flags for leader of current line */
4756 char_u *next_leader_flags; /* flags for leader of next line */
4757 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004758 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
4760 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004761 int second_indent = -1; /* indent for second line (comment
4762 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 int do_second_indent;
4764 int do_number_indent;
4765 int do_trail_white;
4766 int first_par_line = TRUE;
4767 int smd_save;
4768 long count;
4769 int need_set_indent = TRUE; /* set indent of next paragraph */
4770 int force_format = FALSE;
4771 int old_State = State;
4772
4773 /* length of a line to force formatting: 3 * 'tw' */
4774 max_len = comp_textwidth(TRUE) * 3;
4775
4776 /* check for 'q', '2' and '1' in 'formatoptions' */
4777#ifdef FEAT_COMMENTS
4778 do_comments = has_format_option(FO_Q_COMS);
4779#endif
4780 do_second_indent = has_format_option(FO_Q_SECOND);
4781 do_number_indent = has_format_option(FO_Q_NUMBER);
4782 do_trail_white = has_format_option(FO_WHITE_PAR);
4783
4784 /*
4785 * Get info about the previous and current line.
4786 */
4787 if (curwin->w_cursor.lnum > 1)
4788 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4789#ifdef FEAT_COMMENTS
4790 , &leader_len, &leader_flags, do_comments
4791#endif
4792 );
4793 else
4794 is_not_par = TRUE;
4795 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4796#ifdef FEAT_COMMENTS
4797 , &next_leader_len, &next_leader_flags, do_comments
4798#endif
4799 );
4800 is_end_par = (is_not_par || next_is_not_par);
4801 if (!is_end_par && do_trail_white)
4802 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4803
4804 curwin->w_cursor.lnum--;
4805 for (count = line_count; count != 0 && !got_int; --count)
4806 {
4807 /*
4808 * Advance to next paragraph.
4809 */
4810 if (advance)
4811 {
4812 curwin->w_cursor.lnum++;
4813 prev_is_end_par = is_end_par;
4814 is_not_par = next_is_not_par;
4815#ifdef FEAT_COMMENTS
4816 leader_len = next_leader_len;
4817 leader_flags = next_leader_flags;
4818#endif
4819 }
4820
4821 /*
4822 * The last line to be formatted.
4823 */
4824 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4825 {
4826 next_is_not_par = TRUE;
4827#ifdef FEAT_COMMENTS
4828 next_leader_len = 0;
4829 next_leader_flags = NULL;
4830#endif
4831 }
4832 else
4833 {
4834 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4835#ifdef FEAT_COMMENTS
4836 , &next_leader_len, &next_leader_flags, do_comments
4837#endif
4838 );
4839 if (do_number_indent)
4840 next_is_start_par =
4841 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4842 }
4843 advance = TRUE;
4844 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4845 if (!is_end_par && do_trail_white)
4846 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4847
4848 /*
4849 * Skip lines that are not in a paragraph.
4850 */
4851 if (is_not_par)
4852 {
4853 if (line_count < 0)
4854 break;
4855 }
4856 else
4857 {
4858 /*
4859 * For the first line of a paragraph, check indent of second line.
4860 * Don't do this for comments and empty lines.
4861 */
4862 if (first_par_line
4863 && (do_second_indent || do_number_indent)
4864 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004865 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004867 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4868 {
4869#ifdef FEAT_COMMENTS
4870 if (leader_len == 0 && next_leader_len == 0)
4871 {
4872 /* no comment found */
4873#endif
4874 second_indent =
4875 get_indent_lnum(curwin->w_cursor.lnum + 1);
4876#ifdef FEAT_COMMENTS
4877 }
4878 else
4879 {
4880 second_indent = next_leader_len;
4881 do_comments_list = 1;
4882 }
4883#endif
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004886 {
4887#ifdef FEAT_COMMENTS
4888 if (leader_len == 0 && next_leader_len == 0)
4889 {
4890 /* no comment found */
4891#endif
4892 second_indent =
4893 get_number_indent(curwin->w_cursor.lnum);
4894#ifdef FEAT_COMMENTS
4895 }
4896 else
4897 {
4898 /* get_number_indent() is now "comment aware"... */
4899 second_indent =
4900 get_number_indent(curwin->w_cursor.lnum);
4901 do_comments_list = 1;
4902 }
4903#endif
4904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906
4907 /*
4908 * When the comment leader changes, it's the end of the paragraph.
4909 */
4910 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4911#ifdef FEAT_COMMENTS
4912 || !same_leader(curwin->w_cursor.lnum,
4913 leader_len, leader_flags,
4914 next_leader_len, next_leader_flags)
4915#endif
4916 )
4917 is_end_par = TRUE;
4918
4919 /*
4920 * If we have got to the end of a paragraph, or the line is
4921 * getting long, format it.
4922 */
4923 if (is_end_par || force_format)
4924 {
4925 if (need_set_indent)
4926 /* replace indent in first line with minimal number of
4927 * tabs and spaces, according to current options */
4928 (void)set_indent(get_indent(), SIN_CHANGED);
4929
4930 /* put cursor on last non-space */
4931 State = NORMAL; /* don't go past end-of-line */
4932 coladvance((colnr_T)MAXCOL);
4933 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4934 dec_cursor();
4935
4936 /* do the formatting, without 'showmode' */
4937 State = INSERT; /* for open_line() */
4938 smd_save = p_smd;
4939 p_smd = FALSE;
4940 insertchar(NUL, INSCHAR_FORMAT
4941#ifdef FEAT_COMMENTS
4942 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004943 + (do_comments && do_comments_list
4944 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004946 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 State = old_State;
4948 p_smd = smd_save;
4949 second_indent = -1;
4950 /* at end of par.: need to set indent of next par. */
4951 need_set_indent = is_end_par;
4952 if (is_end_par)
4953 {
4954 /* When called with a negative line count, break at the
4955 * end of the paragraph. */
4956 if (line_count < 0)
4957 break;
4958 first_par_line = TRUE;
4959 }
4960 force_format = FALSE;
4961 }
4962
4963 /*
4964 * When still in same paragraph, join the lines together. But
4965 * first delete the comment leader from the second line.
4966 */
4967 if (!is_end_par)
4968 {
4969 advance = FALSE;
4970 curwin->w_cursor.lnum++;
4971 curwin->w_cursor.col = 0;
4972 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#ifdef FEAT_COMMENTS
Bram Moolenaare3226be2005-12-18 22:10:00 +00004975 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (next_leader_len > 0)
4977 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4978 (long)-next_leader_len);
4979#endif
4980 curwin->w_cursor.lnum--;
Bram Moolenaar81340392012-06-06 16:12:59 +02004981 if (do_join(2, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 beep_flush();
4984 break;
4985 }
4986 first_par_line = FALSE;
4987 /* If the line is getting long, format it next time */
4988 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4989 force_format = TRUE;
4990 else
4991 force_format = FALSE;
4992 }
4993 }
4994 line_breakcheck();
4995 }
4996}
4997
4998/*
4999 * Return TRUE if line "lnum" ends in a white character.
5000 */
5001 static int
5002ends_in_white(lnum)
5003 linenr_T lnum;
5004{
5005 char_u *s = ml_get(lnum);
5006 size_t l;
5007
5008 if (*s == NUL)
5009 return FALSE;
5010 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5011 * invocation may call function multiple times". */
5012 l = STRLEN(s) - 1;
5013 return vim_iswhite(s[l]);
5014}
5015
5016/*
5017 * Blank lines, and lines containing only the comment leader, are left
5018 * untouched by the formatting. The function returns TRUE in this
5019 * case. It also returns TRUE when a line starts with the end of a comment
5020 * ('e' in comment flags), so that this line is skipped, and not joined to the
5021 * previous line. A new paragraph starts after a blank line, or when the
5022 * comment leader changes -- webb.
5023 */
5024#ifdef FEAT_COMMENTS
5025 static int
5026fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5027 linenr_T lnum;
5028 int *leader_len;
5029 char_u **leader_flags;
5030 int do_comments;
5031{
5032 char_u *flags = NULL; /* init for GCC */
5033 char_u *ptr;
5034
5035 ptr = ml_get(lnum);
5036 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005037 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 else
5039 *leader_len = 0;
5040
5041 if (*leader_len > 0)
5042 {
5043 /*
5044 * Search for 'e' flag in comment leader flags.
5045 */
5046 flags = *leader_flags;
5047 while (*flags && *flags != ':' && *flags != COM_END)
5048 ++flags;
5049 }
5050
5051 return (*skipwhite(ptr + *leader_len) == NUL
5052 || (*leader_len > 0 && *flags == COM_END)
5053 || startPS(lnum, NUL, FALSE));
5054}
5055#else
5056 static int
5057fmt_check_par(lnum)
5058 linenr_T lnum;
5059{
5060 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5061}
5062#endif
5063
5064/*
5065 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5066 * previous line is in the same paragraph. Used for auto-formatting.
5067 */
5068 int
5069paragraph_start(lnum)
5070 linenr_T lnum;
5071{
5072 char_u *p;
5073#ifdef FEAT_COMMENTS
5074 int leader_len = 0; /* leader len of current line */
5075 char_u *leader_flags = NULL; /* flags for leader of current line */
5076 int next_leader_len; /* leader len of next line */
5077 char_u *next_leader_flags; /* flags for leader of next line */
5078 int do_comments; /* format comments */
5079#endif
5080
5081 if (lnum <= 1)
5082 return TRUE; /* start of the file */
5083
5084 p = ml_get(lnum - 1);
5085 if (*p == NUL)
5086 return TRUE; /* after empty line */
5087
5088#ifdef FEAT_COMMENTS
5089 do_comments = has_format_option(FO_Q_COMS);
5090#endif
5091 if (fmt_check_par(lnum - 1
5092#ifdef FEAT_COMMENTS
5093 , &leader_len, &leader_flags, do_comments
5094#endif
5095 ))
5096 return TRUE; /* after non-paragraph line */
5097
5098 if (fmt_check_par(lnum
5099#ifdef FEAT_COMMENTS
5100 , &next_leader_len, &next_leader_flags, do_comments
5101#endif
5102 ))
5103 return TRUE; /* "lnum" is not a paragraph line */
5104
5105 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5106 return TRUE; /* missing trailing space in previous line. */
5107
5108 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5109 return TRUE; /* numbered item starts in "lnum". */
5110
5111#ifdef FEAT_COMMENTS
5112 if (!same_leader(lnum - 1, leader_len, leader_flags,
5113 next_leader_len, next_leader_flags))
5114 return TRUE; /* change of comment leader. */
5115#endif
5116
5117 return FALSE;
5118}
5119
5120#ifdef FEAT_VISUAL
5121/*
5122 * prepare a few things for block mode yank/delete/tilde
5123 *
5124 * for delete:
5125 * - textlen includes the first/last char to be (partly) deleted
5126 * - start/endspaces is the number of columns that are taken by the
5127 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005128 * deleted.
5129 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 * - textlen includes the first/last char to be wholly yanked
5131 * - start/endspaces is the number of columns of the first/last yanked char
5132 * that are to be yanked.
5133 */
5134 static void
5135block_prep(oap, bdp, lnum, is_del)
5136 oparg_T *oap;
5137 struct block_def *bdp;
5138 linenr_T lnum;
5139 int is_del;
5140{
5141 int incr = 0;
5142 char_u *pend;
5143 char_u *pstart;
5144 char_u *line;
5145 char_u *prev_pstart;
5146 char_u *prev_pend;
5147
5148 bdp->startspaces = 0;
5149 bdp->endspaces = 0;
5150 bdp->textlen = 0;
5151 bdp->start_vcol = 0;
5152 bdp->end_vcol = 0;
5153#ifdef FEAT_VISUALEXTRA
5154 bdp->is_short = FALSE;
5155 bdp->is_oneChar = FALSE;
5156 bdp->pre_whitesp = 0;
5157 bdp->pre_whitesp_c = 0;
5158 bdp->end_char_vcols = 0;
5159#endif
5160 bdp->start_char_vcols = 0;
5161
5162 line = ml_get(lnum);
5163 pstart = line;
5164 prev_pstart = line;
5165 while (bdp->start_vcol < oap->start_vcol && *pstart)
5166 {
5167 /* Count a tab for what it's worth (if list mode not on) */
5168 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
5169 bdp->start_vcol += incr;
5170#ifdef FEAT_VISUALEXTRA
5171 if (vim_iswhite(*pstart))
5172 {
5173 bdp->pre_whitesp += incr;
5174 bdp->pre_whitesp_c++;
5175 }
5176 else
5177 {
5178 bdp->pre_whitesp = 0;
5179 bdp->pre_whitesp_c = 0;
5180 }
5181#endif
5182 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005183 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 bdp->start_char_vcols = incr;
5186 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5187 {
5188 bdp->end_vcol = bdp->start_vcol;
5189#ifdef FEAT_VISUALEXTRA
5190 bdp->is_short = TRUE;
5191#endif
5192 if (!is_del || oap->op_type == OP_APPEND)
5193 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5194 }
5195 else
5196 {
5197 /* notice: this converts partly selected Multibyte characters to
5198 * spaces, too. */
5199 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5200 if (is_del && bdp->startspaces)
5201 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5202 pend = pstart;
5203 bdp->end_vcol = bdp->start_vcol;
5204 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5205 {
5206#ifdef FEAT_VISUALEXTRA
5207 bdp->is_oneChar = TRUE;
5208#endif
5209 if (oap->op_type == OP_INSERT)
5210 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5211 else if (oap->op_type == OP_APPEND)
5212 {
5213 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5214 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5215 }
5216 else
5217 {
5218 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5219 if (is_del && oap->op_type != OP_LSHIFT)
5220 {
5221 /* just putting the sum of those two into
5222 * bdp->startspaces doesn't work for Visual replace,
5223 * so we have to split the tab in two */
5224 bdp->startspaces = bdp->start_char_vcols
5225 - (bdp->start_vcol - oap->start_vcol);
5226 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5227 }
5228 }
5229 }
5230 else
5231 {
5232 prev_pend = pend;
5233 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5234 {
5235 /* Count a tab for what it's worth (if list mode not on) */
5236 prev_pend = pend;
5237 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
5238 bdp->end_vcol += incr;
5239 }
5240 if (bdp->end_vcol <= oap->end_vcol
5241 && (!is_del
5242 || oap->op_type == OP_APPEND
5243 || oap->op_type == OP_REPLACE)) /* line too short */
5244 {
5245#ifdef FEAT_VISUALEXTRA
5246 bdp->is_short = TRUE;
5247#endif
5248 /* Alternative: include spaces to fill up the block.
5249 * Disadvantage: can lead to trailing spaces when the line is
5250 * short where the text is put */
5251 /* if (!is_del || oap->op_type == OP_APPEND) */
5252 if (oap->op_type == OP_APPEND || virtual_op)
5253 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005254 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 else
5256 bdp->endspaces = 0; /* replace doesn't add characters */
5257 }
5258 else if (bdp->end_vcol > oap->end_vcol)
5259 {
5260 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5261 if (!is_del && bdp->endspaces)
5262 {
5263 bdp->endspaces = incr - bdp->endspaces;
5264 if (pend != pstart)
5265 pend = prev_pend;
5266 }
5267 }
5268 }
5269#ifdef FEAT_VISUALEXTRA
5270 bdp->end_char_vcols = incr;
5271#endif
5272 if (is_del && bdp->startspaces)
5273 pstart = prev_pstart;
5274 bdp->textlen = (int)(pend - pstart);
5275 }
5276 bdp->textcol = (colnr_T) (pstart - line);
5277 bdp->textstart = pstart;
5278}
5279#endif /* FEAT_VISUAL */
5280
5281#ifdef FEAT_RIGHTLEFT
5282static void reverse_line __ARGS((char_u *s));
5283
5284 static void
5285reverse_line(s)
5286 char_u *s;
5287{
5288 int i, j;
5289 char_u c;
5290
5291 if ((i = (int)STRLEN(s) - 1) <= 0)
5292 return;
5293
5294 curwin->w_cursor.col = i - curwin->w_cursor.col;
5295 for (j = 0; j < i; j++, i--)
5296 {
5297 c = s[i]; s[i] = s[j]; s[j] = c;
5298 }
5299}
5300
5301# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5302#else
5303# define RLADDSUBFIX(ptr)
5304#endif
5305
5306/*
5307 * add or subtract 'Prenum1' from a number in a line
5308 * 'command' is CTRL-A for add, CTRL-X for subtract
5309 *
5310 * return FAIL for failure, OK otherwise
5311 */
5312 int
5313do_addsub(command, Prenum1)
5314 int command;
5315 linenr_T Prenum1;
5316{
5317 int col;
5318 char_u *buf1;
5319 char_u buf2[NUMBUFLEN];
5320 int hex; /* 'X' or 'x': hex; '0': octal */
5321 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005322 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 long_u oldn;
5324 char_u *ptr;
5325 int c;
5326 int length = 0; /* character length of the number */
5327 int todel;
5328 int dohex;
5329 int dooct;
5330 int doalp;
5331 int firstdigit;
5332 int negative;
5333 int subtract;
5334
5335 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5336 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5337 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5338
5339 ptr = ml_get_curline();
5340 RLADDSUBFIX(ptr);
5341
5342 /*
5343 * First check if we are on a hexadecimal number, after the "0x".
5344 */
5345 col = curwin->w_cursor.col;
5346 if (dohex)
5347 while (col > 0 && vim_isxdigit(ptr[col]))
5348 --col;
5349 if ( dohex
5350 && col > 0
5351 && (ptr[col] == 'X'
5352 || ptr[col] == 'x')
5353 && ptr[col - 1] == '0'
5354 && vim_isxdigit(ptr[col + 1]))
5355 {
5356 /*
5357 * Found hexadecimal number, move to its start.
5358 */
5359 --col;
5360 }
5361 else
5362 {
5363 /*
5364 * Search forward and then backward to find the start of number.
5365 */
5366 col = curwin->w_cursor.col;
5367
5368 while (ptr[col] != NUL
5369 && !vim_isdigit(ptr[col])
5370 && !(doalp && ASCII_ISALPHA(ptr[col])))
5371 ++col;
5372
5373 while (col > 0
5374 && vim_isdigit(ptr[col - 1])
5375 && !(doalp && ASCII_ISALPHA(ptr[col])))
5376 --col;
5377 }
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 /*
5380 * If a number was found, and saving for undo works, replace the number.
5381 */
5382 firstdigit = ptr[col];
5383 RLADDSUBFIX(ptr);
5384 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5385 || u_save_cursor() != OK)
5386 {
5387 beep_flush();
5388 return FAIL;
5389 }
5390
5391 /* get ptr again, because u_save() may have changed it */
5392 ptr = ml_get_curline();
5393 RLADDSUBFIX(ptr);
5394
5395 if (doalp && ASCII_ISALPHA(firstdigit))
5396 {
5397 /* decrement or increment alphabetic character */
5398 if (command == Ctrl_X)
5399 {
5400 if (CharOrd(firstdigit) < Prenum1)
5401 {
5402 if (isupper(firstdigit))
5403 firstdigit = 'A';
5404 else
5405 firstdigit = 'a';
5406 }
5407 else
5408#ifdef EBCDIC
5409 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5410#else
5411 firstdigit -= Prenum1;
5412#endif
5413 }
5414 else
5415 {
5416 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5417 {
5418 if (isupper(firstdigit))
5419 firstdigit = 'Z';
5420 else
5421 firstdigit = 'z';
5422 }
5423 else
5424#ifdef EBCDIC
5425 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5426#else
5427 firstdigit += Prenum1;
5428#endif
5429 }
5430 curwin->w_cursor.col = col;
5431 (void)del_char(FALSE);
5432 ins_char(firstdigit);
5433 }
5434 else
5435 {
5436 negative = FALSE;
5437 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5438 {
5439 --col;
5440 negative = TRUE;
5441 }
5442
5443 /* get the number value (unsigned) */
5444 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5445
5446 /* ignore leading '-' for hex and octal numbers */
5447 if (hex && negative)
5448 {
5449 ++col;
5450 --length;
5451 negative = FALSE;
5452 }
5453
5454 /* add or subtract */
5455 subtract = FALSE;
5456 if (command == Ctrl_X)
5457 subtract ^= TRUE;
5458 if (negative)
5459 subtract ^= TRUE;
5460
5461 oldn = n;
5462 if (subtract)
5463 n -= (unsigned long)Prenum1;
5464 else
5465 n += (unsigned long)Prenum1;
5466
5467 /* handle wraparound for decimal numbers */
5468 if (!hex)
5469 {
5470 if (subtract)
5471 {
5472 if (n > oldn)
5473 {
5474 n = 1 + (n ^ (unsigned long)-1);
5475 negative ^= TRUE;
5476 }
5477 }
5478 else /* add */
5479 {
5480 if (n < oldn)
5481 {
5482 n = (n ^ (unsigned long)-1);
5483 negative ^= TRUE;
5484 }
5485 }
5486 if (n == 0)
5487 negative = FALSE;
5488 }
5489
5490 /*
5491 * Delete the old number.
5492 */
5493 curwin->w_cursor.col = col;
5494 todel = length;
5495 c = gchar_cursor();
5496 /*
5497 * Don't include the '-' in the length, only the length of the part
5498 * after it is kept the same.
5499 */
5500 if (c == '-')
5501 --length;
5502 while (todel-- > 0)
5503 {
5504 if (c < 0x100 && isalpha(c))
5505 {
5506 if (isupper(c))
5507 hexupper = TRUE;
5508 else
5509 hexupper = FALSE;
5510 }
5511 /* del_char() will mark line needing displaying */
5512 (void)del_char(FALSE);
5513 c = gchar_cursor();
5514 }
5515
5516 /*
5517 * Prepare the leading characters in buf1[].
5518 * When there are many leading zeros it could be very long. Allocate
5519 * a bit too much.
5520 */
5521 buf1 = alloc((unsigned)length + NUMBUFLEN);
5522 if (buf1 == NULL)
5523 return FAIL;
5524 ptr = buf1;
5525 if (negative)
5526 {
5527 *ptr++ = '-';
5528 }
5529 if (hex)
5530 {
5531 *ptr++ = '0';
5532 --length;
5533 }
5534 if (hex == 'x' || hex == 'X')
5535 {
5536 *ptr++ = hex;
5537 --length;
5538 }
5539
5540 /*
5541 * Put the number characters in buf2[].
5542 */
5543 if (hex == 0)
5544 sprintf((char *)buf2, "%lu", n);
5545 else if (hex == '0')
5546 sprintf((char *)buf2, "%lo", n);
5547 else if (hex && hexupper)
5548 sprintf((char *)buf2, "%lX", n);
5549 else
5550 sprintf((char *)buf2, "%lx", n);
5551 length -= (int)STRLEN(buf2);
5552
5553 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005554 * Adjust number of zeros to the new number of digits, so the
5555 * total length of the number remains the same.
5556 * Don't do this when
5557 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005559 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 while (length-- > 0)
5561 *ptr++ = '0';
5562 *ptr = NUL;
5563 STRCAT(buf1, buf2);
5564 ins_str(buf1); /* insert the new number */
5565 vim_free(buf1);
5566 }
5567 --curwin->w_cursor.col;
5568 curwin->w_set_curswant = TRUE;
5569#ifdef FEAT_RIGHTLEFT
5570 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5571 RLADDSUBFIX(ptr);
5572#endif
5573 return OK;
5574}
5575
5576#ifdef FEAT_VIMINFO
5577 int
5578read_viminfo_register(virp, force)
5579 vir_T *virp;
5580 int force;
5581{
5582 int eof;
5583 int do_it = TRUE;
5584 int size;
5585 int limit;
5586 int i;
5587 int set_prev = FALSE;
5588 char_u *str;
5589 char_u **array = NULL;
5590
5591 /* We only get here (hopefully) if line[0] == '"' */
5592 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005593
5594 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 if (*str == '"')
5596 {
5597 set_prev = TRUE;
5598 str++;
5599 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (!ASCII_ISALNUM(*str) && *str != '-')
5602 {
5603 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5604 return TRUE; /* too many errors, pretend end-of-file */
5605 do_it = FALSE;
5606 }
5607 get_yank_register(*str++, FALSE);
5608 if (!force && y_current->y_array != NULL)
5609 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005610
5611 if (*str == '@')
5612 {
5613 /* "x@: register x used for @@ */
5614 if (force || execreg_lastc == NUL)
5615 execreg_lastc = str[-1];
5616 }
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 size = 0;
5619 limit = 100; /* Optimized for registers containing <= 100 lines */
5620 if (do_it)
5621 {
5622 if (set_prev)
5623 y_previous = y_current;
5624 vim_free(y_current->y_array);
5625 array = y_current->y_array =
5626 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005627 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 if (STRNCMP(str, "CHAR", 4) == 0)
5629 y_current->y_type = MCHAR;
5630#ifdef FEAT_VISUAL
5631 else if (STRNCMP(str, "BLOCK", 5) == 0)
5632 y_current->y_type = MBLOCK;
5633#endif
5634 else
5635 y_current->y_type = MLINE;
5636 /* get the block width; if it's missing we get a zero, which is OK */
5637 str = skipwhite(skiptowhite(str));
5638#ifdef FEAT_VISUAL
5639 y_current->y_width = getdigits(&str);
5640#else
5641 (void)getdigits(&str);
5642#endif
5643 }
5644
5645 while (!(eof = viminfo_readline(virp))
5646 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5647 {
5648 if (do_it)
5649 {
5650 if (size >= limit)
5651 {
5652 y_current->y_array = (char_u **)
5653 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5654 for (i = 0; i < limit; i++)
5655 y_current->y_array[i] = array[i];
5656 vim_free(array);
5657 limit *= 2;
5658 array = y_current->y_array;
5659 }
5660 str = viminfo_readstring(virp, 1, TRUE);
5661 if (str != NULL)
5662 array[size++] = str;
5663 else
5664 do_it = FALSE;
5665 }
5666 }
5667 if (do_it)
5668 {
5669 if (size == 0)
5670 {
5671 vim_free(array);
5672 y_current->y_array = NULL;
5673 }
5674 else if (size < limit)
5675 {
5676 y_current->y_array =
5677 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5678 for (i = 0; i < size; i++)
5679 y_current->y_array[i] = array[i];
5680 vim_free(array);
5681 }
5682 y_current->y_size = size;
5683 }
5684 return eof;
5685}
5686
5687 void
5688write_viminfo_registers(fp)
5689 FILE *fp;
5690{
5691 int i, j;
5692 char_u *type;
5693 char_u c;
5694 int num_lines;
5695 int max_num_lines;
5696 int max_kbyte;
5697 long len;
5698
Bram Moolenaar64404472010-06-26 06:24:45 +02005699 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701 /* Get '<' value, use old '"' value if '<' is not found. */
5702 max_num_lines = get_viminfo_parameter('<');
5703 if (max_num_lines < 0)
5704 max_num_lines = get_viminfo_parameter('"');
5705 if (max_num_lines == 0)
5706 return;
5707 max_kbyte = get_viminfo_parameter('s');
5708 if (max_kbyte == 0)
5709 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 for (i = 0; i < NUM_REGISTERS; i++)
5712 {
5713 if (y_regs[i].y_array == NULL)
5714 continue;
5715#ifdef FEAT_CLIPBOARD
5716 /* Skip '*'/'+' register, we don't want them back next time */
5717 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5718 continue;
5719#endif
5720#ifdef FEAT_DND
5721 /* Neither do we want the '~' register */
5722 if (i == TILDE_REGISTER)
5723 continue;
5724#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005725 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005727 if (num_lines == 0
5728 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005729 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005730 continue;
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if (max_kbyte > 0)
5733 {
5734 /* Skip register if there is more text than the maximum size. */
5735 len = 0;
5736 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005737 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (len > (long)max_kbyte * 1024L)
5739 continue;
5740 }
5741
5742 switch (y_regs[i].y_type)
5743 {
5744 case MLINE:
5745 type = (char_u *)"LINE";
5746 break;
5747 case MCHAR:
5748 type = (char_u *)"CHAR";
5749 break;
5750#ifdef FEAT_VISUAL
5751 case MBLOCK:
5752 type = (char_u *)"BLOCK";
5753 break;
5754#endif
5755 default:
5756 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005757 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 emsg(IObuff);
5759 type = (char_u *)"LINE";
5760 break;
5761 }
5762 if (y_previous == &y_regs[i])
5763 fprintf(fp, "\"");
5764 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005765 fprintf(fp, "\"%c", c);
5766 if (c == execreg_lastc)
5767 fprintf(fp, "@");
5768 fprintf(fp, "\t%s\t%d\n", type,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_VISUAL
5770 (int)y_regs[i].y_width
5771#else
5772 0
5773#endif
5774 );
5775
5776 /* If max_num_lines < 0, then we save ALL the lines in the register */
5777 if (max_num_lines > 0 && num_lines > max_num_lines)
5778 num_lines = max_num_lines;
5779 for (j = 0; j < num_lines; j++)
5780 {
5781 putc('\t', fp);
5782 viminfo_writestring(fp, y_regs[i].y_array[j]);
5783 }
5784 }
5785}
5786#endif /* FEAT_VIMINFO */
5787
5788#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5789/*
5790 * SELECTION / PRIMARY ('*')
5791 *
5792 * Text selection stuff that uses the GUI selection register '*'. When using a
5793 * GUI this may be text from another window, otherwise it is the last text we
5794 * had highlighted with VIsual mode. With mouse support, clicking the middle
5795 * button performs the paste, otherwise you will need to do <"*p>. "
5796 * If not under X, it is synonymous with the clipboard register '+'.
5797 *
5798 * X CLIPBOARD ('+')
5799 *
5800 * Text selection stuff that uses the GUI clipboard register '+'.
5801 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5802 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5803 * otherwise you will need to do <"+p>. "
5804 * If not under X, it is synonymous with the selection register '*'.
5805 */
5806
5807/*
5808 * Routine to export any final X selection we had to the environment
5809 * so that the text is still available after vim has exited. X selections
5810 * only exist while the owning application exists, so we write to the
5811 * permanent (while X runs) store CUT_BUFFER0.
5812 * Dump the CLIPBOARD selection if we own it (it's logically the more
5813 * 'permanent' of the two), otherwise the PRIMARY one.
5814 * For now, use a hard-coded sanity limit of 1Mb of data.
5815 */
5816#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5817 void
5818x11_export_final_selection()
5819{
5820 Display *dpy;
5821 char_u *str = NULL;
5822 long_u len = 0;
5823 int motion_type = -1;
5824
5825# ifdef FEAT_GUI
5826 if (gui.in_use)
5827 dpy = X_DISPLAY;
5828 else
5829# endif
5830# ifdef FEAT_XCLIPBOARD
5831 dpy = xterm_dpy;
5832# else
5833 return;
5834# endif
5835
5836 /* Get selection to export */
5837 if (clip_plus.owned)
5838 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5839 else if (clip_star.owned)
5840 motion_type = clip_convert_selection(&str, &len, &clip_star);
5841
5842 /* Check it's OK */
5843 if (dpy != NULL && str != NULL && motion_type >= 0
5844 && len < 1024*1024 && len > 0)
5845 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005846#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005847 int ok = TRUE;
5848
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005849 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5850 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5851 * encoding conversion usually doesn't work, so keep the text as-is.
5852 */
5853 if (has_mbyte)
5854 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005855 vimconv_T vc;
5856
5857 vc.vc_type = CONV_NONE;
5858 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5859 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005860 int intlen = len;
5861 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005862
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005863 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005864 conv_str = string_convert(&vc, str, &intlen);
5865 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005866 if (conv_str != NULL)
5867 {
5868 vim_free(str);
5869 str = conv_str;
5870 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005871 else
5872 {
5873 ok = FALSE;
5874 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005875 convert_setup(&vc, NULL, NULL);
5876 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005877 else
5878 {
5879 ok = FALSE;
5880 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005881 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005882
5883 /* Do not store the string if conversion failed. Better to use any
5884 * other selection than garbled text. */
5885 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005886#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005887 {
5888 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5889 XFlush(dpy);
5890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
5892
5893 vim_free(str);
5894}
5895#endif
5896
5897 void
5898clip_free_selection(cbd)
5899 VimClipboard *cbd;
5900{
5901 struct yankreg *y_ptr = y_current;
5902
5903 if (cbd == &clip_plus)
5904 y_current = &y_regs[PLUS_REGISTER];
5905 else
5906 y_current = &y_regs[STAR_REGISTER];
5907 free_yank_all();
5908 y_current->y_size = 0;
5909 y_current = y_ptr;
5910}
5911
5912/*
5913 * Get the selected text and put it in the gui selection register '*' or '+'.
5914 */
5915 void
5916clip_get_selection(cbd)
5917 VimClipboard *cbd;
5918{
5919 struct yankreg *old_y_previous, *old_y_current;
5920 pos_T old_cursor;
5921#ifdef FEAT_VISUAL
5922 pos_T old_visual;
5923 int old_visual_mode;
5924#endif
5925 colnr_T old_curswant;
5926 int old_set_curswant;
5927 pos_T old_op_start, old_op_end;
5928 oparg_T oa;
5929 cmdarg_T ca;
5930
5931 if (cbd->owned)
5932 {
5933 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5934 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5935 return;
5936
5937 /* Get the text between clip_star.start & clip_star.end */
5938 old_y_previous = y_previous;
5939 old_y_current = y_current;
5940 old_cursor = curwin->w_cursor;
5941 old_curswant = curwin->w_curswant;
5942 old_set_curswant = curwin->w_set_curswant;
5943 old_op_start = curbuf->b_op_start;
5944 old_op_end = curbuf->b_op_end;
5945#ifdef FEAT_VISUAL
5946 old_visual = VIsual;
5947 old_visual_mode = VIsual_mode;
5948#endif
5949 clear_oparg(&oa);
5950 oa.regname = (cbd == &clip_plus ? '+' : '*');
5951 oa.op_type = OP_YANK;
5952 vim_memset(&ca, 0, sizeof(ca));
5953 ca.oap = &oa;
5954 ca.cmdchar = 'y';
5955 ca.count1 = 1;
5956 ca.retval = CA_NO_ADJ_OP_END;
5957 do_pending_operator(&ca, 0, TRUE);
5958 y_previous = old_y_previous;
5959 y_current = old_y_current;
5960 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005961 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 curwin->w_curswant = old_curswant;
5963 curwin->w_set_curswant = old_set_curswant;
5964 curbuf->b_op_start = old_op_start;
5965 curbuf->b_op_end = old_op_end;
5966#ifdef FEAT_VISUAL
5967 VIsual = old_visual;
5968 VIsual_mode = old_visual_mode;
5969#endif
5970 }
5971 else
5972 {
5973 clip_free_selection(cbd);
5974
5975 /* Try to get selected text from another window */
5976 clip_gen_request_selection(cbd);
5977 }
5978}
5979
Bram Moolenaard44347f2011-06-19 01:14:29 +02005980/*
5981 * Convert from the GUI selection string into the '*'/'+' register.
5982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 void
5984clip_yank_selection(type, str, len, cbd)
5985 int type;
5986 char_u *str;
5987 long len;
5988 VimClipboard *cbd;
5989{
5990 struct yankreg *y_ptr;
5991
5992 if (cbd == &clip_plus)
5993 y_ptr = &y_regs[PLUS_REGISTER];
5994 else
5995 y_ptr = &y_regs[STAR_REGISTER];
5996
5997 clip_free_selection(cbd);
5998
5999 str_to_reg(y_ptr, type, str, len, 0L);
6000}
6001
6002/*
6003 * Convert the '*'/'+' register into a GUI selection string returned in *str
6004 * with length *len.
6005 * Returns the motion type, or -1 for failure.
6006 */
6007 int
6008clip_convert_selection(str, len, cbd)
6009 char_u **str;
6010 long_u *len;
6011 VimClipboard *cbd;
6012{
6013 char_u *p;
6014 int lnum;
6015 int i, j;
6016 int_u eolsize;
6017 struct yankreg *y_ptr;
6018
6019 if (cbd == &clip_plus)
6020 y_ptr = &y_regs[PLUS_REGISTER];
6021 else
6022 y_ptr = &y_regs[STAR_REGISTER];
6023
6024#ifdef USE_CRNL
6025 eolsize = 2;
6026#else
6027 eolsize = 1;
6028#endif
6029
6030 *str = NULL;
6031 *len = 0;
6032 if (y_ptr->y_array == NULL)
6033 return -1;
6034
6035 for (i = 0; i < y_ptr->y_size; i++)
6036 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6037
6038 /*
6039 * Don't want newline character at end of last line if we're in MCHAR mode.
6040 */
6041 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6042 *len -= eolsize;
6043
6044 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6045 if (p == NULL)
6046 return -1;
6047 lnum = 0;
6048 for (i = 0, j = 0; i < (int)*len; i++, j++)
6049 {
6050 if (y_ptr->y_array[lnum][j] == '\n')
6051 p[i] = NUL;
6052 else if (y_ptr->y_array[lnum][j] == NUL)
6053 {
6054#ifdef USE_CRNL
6055 p[i++] = '\r';
6056#endif
6057#ifdef USE_CR
6058 p[i] = '\r';
6059#else
6060 p[i] = '\n';
6061#endif
6062 lnum++;
6063 j = -1;
6064 }
6065 else
6066 p[i] = y_ptr->y_array[lnum][j];
6067 }
6068 return y_ptr->y_type;
6069}
6070
6071
6072# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
6073/*
6074 * If we have written to a clipboard register, send the text to the clipboard.
6075 */
6076 static void
6077may_set_selection()
6078{
6079 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6080 {
6081 clip_own_selection(&clip_star);
6082 clip_gen_set_selection(&clip_star);
6083 }
6084 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6085 {
6086 clip_own_selection(&clip_plus);
6087 clip_gen_set_selection(&clip_plus);
6088 }
6089}
6090# endif
6091
6092#endif /* FEAT_CLIPBOARD || PROTO */
6093
6094
6095#if defined(FEAT_DND) || defined(PROTO)
6096/*
6097 * Replace the contents of the '~' register with str.
6098 */
6099 void
6100dnd_yank_drag_data(str, len)
6101 char_u *str;
6102 long len;
6103{
6104 struct yankreg *curr;
6105
6106 curr = y_current;
6107 y_current = &y_regs[TILDE_REGISTER];
6108 free_yank_all();
6109 str_to_reg(y_current, MCHAR, str, len, 0L);
6110 y_current = curr;
6111}
6112#endif
6113
6114
6115#if defined(FEAT_EVAL) || defined(PROTO)
6116/*
6117 * Return the type of a register.
6118 * Used for getregtype()
6119 * Returns MAUTO for error.
6120 */
6121 char_u
6122get_reg_type(regname, reglen)
6123 int regname;
6124 long *reglen;
6125{
6126 switch (regname)
6127 {
6128 case '%': /* file name */
6129 case '#': /* alternate file name */
6130 case '=': /* expression */
6131 case ':': /* last command line */
6132 case '/': /* last search-pattern */
6133 case '.': /* last inserted text */
6134#ifdef FEAT_SEARCHPATH
6135 case Ctrl_F: /* Filename under cursor */
6136 case Ctrl_P: /* Path under cursor, expand via "path" */
6137#endif
6138 case Ctrl_W: /* word under cursor */
6139 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6140 case '_': /* black hole: always empty */
6141 return MCHAR;
6142 }
6143
6144#ifdef FEAT_CLIPBOARD
6145 regname = may_get_selection(regname);
6146#endif
6147
6148 /* Should we check for a valid name? */
6149 get_yank_register(regname, FALSE);
6150
6151 if (y_current->y_array != NULL)
6152 {
6153#ifdef FEAT_VISUAL
6154 if (reglen != NULL && y_current->y_type == MBLOCK)
6155 *reglen = y_current->y_width;
6156#endif
6157 return y_current->y_type;
6158 }
6159 return MAUTO;
6160}
6161
6162/*
6163 * Return the contents of a register as a single allocated string.
6164 * Used for "@r" in expressions and for getreg().
6165 * Returns NULL for error.
6166 */
6167 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00006168get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00006170 int allowexpr; /* allow "=" register */
6171 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 long i;
6174 char_u *retval;
6175 int allocated;
6176 long len;
6177
6178 /* Don't allow using an expression register inside an expression */
6179 if (regname == '=')
6180 {
6181 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00006182 {
6183 if (expr_src)
6184 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00006186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 return NULL;
6188 }
6189
6190 if (regname == '@') /* "@@" is used for unnamed register */
6191 regname = '"';
6192
6193 /* check for valid regname */
6194 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6195 return NULL;
6196
6197#ifdef FEAT_CLIPBOARD
6198 regname = may_get_selection(regname);
6199#endif
6200
6201 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6202 {
6203 if (retval == NULL)
6204 return NULL;
6205 if (!allocated)
6206 retval = vim_strsave(retval);
6207 return retval;
6208 }
6209
6210 get_yank_register(regname, FALSE);
6211 if (y_current->y_array == NULL)
6212 return NULL;
6213
6214 /*
6215 * Compute length of resulting string.
6216 */
6217 len = 0;
6218 for (i = 0; i < y_current->y_size; ++i)
6219 {
6220 len += (long)STRLEN(y_current->y_array[i]);
6221 /*
6222 * Insert a newline between lines and after last line if
6223 * y_type is MLINE.
6224 */
6225 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6226 ++len;
6227 }
6228
6229 retval = lalloc(len + 1, TRUE);
6230
6231 /*
6232 * Copy the lines of the yank register into the string.
6233 */
6234 if (retval != NULL)
6235 {
6236 len = 0;
6237 for (i = 0; i < y_current->y_size; ++i)
6238 {
6239 STRCPY(retval + len, y_current->y_array[i]);
6240 len += (long)STRLEN(retval + len);
6241
6242 /*
6243 * Insert a NL between lines and after the last line if y_type is
6244 * MLINE.
6245 */
6246 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6247 retval[len++] = '\n';
6248 }
6249 retval[len] = NUL;
6250 }
6251
6252 return retval;
6253}
6254
6255/*
6256 * Store string "str" in register "name".
6257 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6258 * If "must_append" is TRUE, always append to the register. Otherwise append
6259 * if "name" is an uppercase letter.
6260 * Note: "maxlen" and "must_append" don't work for the "/" register.
6261 * Careful: 'str' is modified, you may have to use a copy!
6262 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6263 */
6264 void
6265write_reg_contents(name, str, maxlen, must_append)
6266 int name;
6267 char_u *str;
6268 int maxlen;
6269 int must_append;
6270{
6271 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6272}
6273
6274 void
6275write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6276 int name;
6277 char_u *str;
6278 int maxlen;
6279 int must_append;
6280 int yank_type;
6281 long block_len;
6282{
6283 struct yankreg *old_y_previous, *old_y_current;
6284 long len;
6285
Bram Moolenaare7566042005-06-17 22:00:15 +00006286 if (maxlen >= 0)
6287 len = maxlen;
6288 else
6289 len = (long)STRLEN(str);
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 /* Special case: '/' search pattern */
6292 if (name == '/')
6293 {
6294 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6295 return;
6296 }
6297
Bram Moolenaare7566042005-06-17 22:00:15 +00006298#ifdef FEAT_EVAL
6299 if (name == '=')
6300 {
6301 char_u *p, *s;
6302
6303 p = vim_strnsave(str, (int)len);
6304 if (p == NULL)
6305 return;
6306 if (must_append)
6307 {
6308 s = concat_str(get_expr_line_src(), p);
6309 vim_free(p);
6310 p = s;
6311
6312 }
6313 set_expr_line(p);
6314 return;
6315 }
6316#endif
6317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6319 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006320 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 return;
6322 }
6323
6324 if (name == '_') /* black hole: nothing to do */
6325 return;
6326
6327 /* Don't want to change the current (unnamed) register */
6328 old_y_previous = y_previous;
6329 old_y_current = y_current;
6330
6331 get_yank_register(name, TRUE);
6332 if (!y_append && !must_append)
6333 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334#ifndef FEAT_VISUAL
6335 /* Just in case - make sure we don't use MBLOCK */
6336 if (yank_type == MBLOCK)
6337 yank_type = MAUTO;
6338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 str_to_reg(y_current, yank_type, str, len, block_len);
6340
6341# ifdef FEAT_CLIPBOARD
6342 /* Send text of clipboard register to the clipboard. */
6343 may_set_selection();
6344# endif
6345
6346 /* ':let @" = "val"' should change the meaning of the "" register */
6347 if (name != '"')
6348 y_previous = old_y_previous;
6349 y_current = old_y_current;
6350}
6351#endif /* FEAT_EVAL */
6352
6353#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6354/*
6355 * Put a string into a register. When the register is not empty, the string
6356 * is appended.
6357 */
6358 static void
Bram Moolenaard44347f2011-06-19 01:14:29 +02006359str_to_reg(y_ptr, yank_type, str, len, blocklen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006361 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 char_u *str; /* string to put in register */
6363 long len; /* length of string */
6364 long blocklen; /* width of Visual block */
6365{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006366 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 int lnum;
6368 long start;
6369 long i;
6370 int extra;
6371 int newlines; /* number of lines added */
6372 int extraline = 0; /* extra line at the end */
6373 int append = FALSE; /* append to last line in register */
6374 char_u *s;
6375 char_u **pp;
6376#ifdef FEAT_VISUAL
6377 long maxlen;
6378#endif
6379
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006380 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 y_ptr->y_size = 0;
6382
Bram Moolenaard44347f2011-06-19 01:14:29 +02006383 if (yank_type == MAUTO)
6384 type = ((len > 0 && (str[len - 1] == NL || str[len - 1] == CAR))
6385 ? MLINE : MCHAR);
6386 else
6387 type = yank_type;
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 /*
6390 * Count the number of lines within the string
6391 */
6392 newlines = 0;
6393 for (i = 0; i < len; i++)
6394 if (str[i] == '\n')
6395 ++newlines;
6396 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6397 {
6398 extraline = 1;
6399 ++newlines; /* count extra newline at the end */
6400 }
6401 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6402 {
6403 append = TRUE;
6404 --newlines; /* uncount newline when appending first line */
6405 }
6406
6407 /*
6408 * Allocate an array to hold the pointers to the new register lines.
6409 * If the register was not empty, move the existing lines to the new array.
6410 */
6411 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6412 * sizeof(char_u *), TRUE);
6413 if (pp == NULL) /* out of memory */
6414 return;
6415 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6416 pp[lnum] = y_ptr->y_array[lnum];
6417 vim_free(y_ptr->y_array);
6418 y_ptr->y_array = pp;
6419#ifdef FEAT_VISUAL
6420 maxlen = 0;
6421#endif
6422
6423 /*
6424 * Find the end of each line and save it into the array.
6425 */
6426 for (start = 0; start < len + extraline; start += i + 1)
6427 {
6428 for (i = start; i < len; ++i) /* find the end of the line */
6429 if (str[i] == '\n')
6430 break;
6431 i -= start; /* i is now length of line */
6432#ifdef FEAT_VISUAL
6433 if (i > maxlen)
6434 maxlen = i;
6435#endif
6436 if (append)
6437 {
6438 --lnum;
6439 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6440 }
6441 else
6442 extra = 0;
6443 s = alloc((unsigned)(i + extra + 1));
6444 if (s == NULL)
6445 break;
6446 if (extra)
6447 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6448 if (append)
6449 vim_free(y_ptr->y_array[lnum]);
6450 if (i)
6451 mch_memmove(s + extra, str + start, (size_t)i);
6452 extra += i;
6453 s[extra] = NUL;
6454 y_ptr->y_array[lnum++] = s;
6455 while (--extra >= 0)
6456 {
6457 if (*s == NUL)
6458 *s = '\n'; /* replace NUL with newline */
6459 ++s;
6460 }
6461 append = FALSE; /* only first line is appended */
6462 }
6463 y_ptr->y_type = type;
6464 y_ptr->y_size = lnum;
6465# ifdef FEAT_VISUAL
6466 if (type == MBLOCK)
6467 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6468 else
6469 y_ptr->y_width = 0;
6470# endif
6471}
6472#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6473
6474 void
6475clear_oparg(oap)
6476 oparg_T *oap;
6477{
6478 vim_memset(oap, 0, sizeof(oparg_T));
6479}
6480
Bram Moolenaar7c626922005-02-07 22:01:03 +00006481static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
6483/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006484 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 *
6486 * "Words" are counted by looking for boundaries between non-space and
6487 * space characters. (it seems to produce results that match 'wc'.)
6488 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006489 * Return value is byte count; word count for the line is added to "*wc".
6490 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 *
6492 * The function will only examine the first "limit" characters in the
6493 * line, stopping if it encounters an end-of-line (NUL byte). In that
6494 * case, eol_size will be added to the character count to account for
6495 * the size of the EOL character.
6496 */
6497 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006498line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 char_u *line;
6500 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006501 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 long limit;
6503 int eol_size;
6504{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006505 long i;
6506 long words = 0;
6507 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 int is_word = 0;
6509
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006510 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
6512 if (is_word)
6513 {
6514 if (vim_isspace(line[i]))
6515 {
6516 words++;
6517 is_word = 0;
6518 }
6519 }
6520 else if (!vim_isspace(line[i]))
6521 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006522 ++chars;
6523#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006524 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006525#else
6526 ++i;
6527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 }
6529
6530 if (is_word)
6531 words++;
6532 *wc += words;
6533
6534 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006535 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006538 chars += eol_size;
6539 }
6540 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 return i;
6542}
6543
6544/*
6545 * Give some info about the position of the cursor (for "g CTRL-G").
6546 * In Visual mode, give some info about the selected region. (In this case,
6547 * the *_count_cursor variables store running totals for the selection.)
6548 */
6549 void
6550cursor_pos_info()
6551{
6552 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006553 char_u buf1[50];
6554 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006556 long byte_count = 0;
6557 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 long char_count = 0;
6559 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 long word_count = 0;
6561 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006562 int eol_size;
6563 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564#ifdef FEAT_VISUAL
6565 long line_count_selected = 0;
6566 pos_T min_pos, max_pos;
6567 oparg_T oparg;
6568 struct block_def bd;
6569#endif
6570
6571 /*
6572 * Compute the length of the file in characters.
6573 */
6574 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6575 {
6576 MSG(_(no_lines_msg));
6577 }
6578 else
6579 {
6580 if (get_fileformat(curbuf) == EOL_DOS)
6581 eol_size = 2;
6582 else
6583 eol_size = 1;
6584
6585#ifdef FEAT_VISUAL
6586 if (VIsual_active)
6587 {
6588 if (lt(VIsual, curwin->w_cursor))
6589 {
6590 min_pos = VIsual;
6591 max_pos = curwin->w_cursor;
6592 }
6593 else
6594 {
6595 min_pos = curwin->w_cursor;
6596 max_pos = VIsual;
6597 }
6598 if (*p_sel == 'e' && max_pos.col > 0)
6599 --max_pos.col;
6600
6601 if (VIsual_mode == Ctrl_V)
6602 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006603#ifdef FEAT_LINEBREAK
6604 char_u * saved_sbr = p_sbr;
6605
6606 /* Make 'sbr' empty for a moment to get the correct size. */
6607 p_sbr = empty_option;
6608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 oparg.is_VIsual = 1;
6610 oparg.block_mode = TRUE;
6611 oparg.op_type = OP_NOP;
6612 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006613 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006614#ifdef FEAT_LINEBREAK
6615 p_sbr = saved_sbr;
6616#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006617 if (curwin->w_curswant == MAXCOL)
6618 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 /* Swap the start, end vcol if needed */
6620 if (oparg.end_vcol < oparg.start_vcol)
6621 {
6622 oparg.end_vcol += oparg.start_vcol;
6623 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6624 oparg.end_vcol -= oparg.start_vcol;
6625 }
6626 }
6627 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6628 }
6629#endif
6630
6631 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6632 {
6633 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006634 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
6636 ui_breakcheck();
6637 if (got_int)
6638 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006639 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641
6642#ifdef FEAT_VISUAL
6643 /* Do extra processing for VIsual mode. */
6644 if (VIsual_active
6645 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6646 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006647 char_u *s = NULL;
6648 long len = 0L;
6649
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 switch (VIsual_mode)
6651 {
6652 case Ctrl_V:
6653# ifdef FEAT_VIRTUALEDIT
6654 virtual_op = virtual_active();
6655# endif
6656 block_prep(&oparg, &bd, lnum, 0);
6657# ifdef FEAT_VIRTUALEDIT
6658 virtual_op = MAYBE;
6659# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006660 s = bd.textstart;
6661 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 break;
6663 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006664 s = ml_get(lnum);
6665 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 break;
6667 case 'v':
6668 {
6669 colnr_T start_col = (lnum == min_pos.lnum)
6670 ? min_pos.col : 0;
6671 colnr_T end_col = (lnum == max_pos.lnum)
6672 ? max_pos.col - start_col + 1 : MAXCOL;
6673
Bram Moolenaardef9e822004-12-31 20:58:58 +00006674 s = ml_get(lnum) + start_col;
6675 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 }
6677 break;
6678 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006679 if (s != NULL)
6680 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006681 byte_count_cursor += line_count_info(s, &word_count_cursor,
6682 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006683 if (lnum == curbuf->b_ml.ml_line_count
6684 && !curbuf->b_p_eol
6685 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006686 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006687 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690 else
6691#endif
6692 {
6693 /* In non-visual mode, check for the line the cursor is on */
6694 if (lnum == curwin->w_cursor.lnum)
6695 {
6696 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006697 char_count_cursor += char_count;
6698 byte_count_cursor = byte_count +
6699 line_count_info(ml_get(lnum),
6700 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 (long)(curwin->w_cursor.col + 1), eol_size);
6702 }
6703 }
6704 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006705 byte_count += line_count_info(ml_get(lnum), &word_count,
6706 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708
6709 /* Correction for when last line doesn't have an EOL. */
6710 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006711 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713#ifdef FEAT_VISUAL
6714 if (VIsual_active)
6715 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006716 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 {
6718 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006719 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006720 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6722 }
6723 else
6724 buf1[0] = NUL;
6725
Bram Moolenaar7c626922005-02-07 22:01:03 +00006726 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006727 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006728 vim_snprintf((char *)IObuff, IOSIZE,
6729 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 buf1, line_count_selected,
6731 (long)curbuf->b_ml.ml_line_count,
6732 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006733 byte_count_cursor, byte_count);
6734 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006735 vim_snprintf((char *)IObuff, IOSIZE,
6736 _("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 +00006737 buf1, line_count_selected,
6738 (long)curbuf->b_ml.ml_line_count,
6739 word_count_cursor, word_count,
6740 char_count_cursor, char_count,
6741 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 else
6744#endif
6745 {
6746 p = ml_get_curline();
6747 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006748 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 (int)curwin->w_virtcol + 1);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006750 col_print(buf2, sizeof(buf2), (int)STRLEN(p), linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
Bram Moolenaar7c626922005-02-07 22:01:03 +00006752 if (char_count_cursor == byte_count_cursor
6753 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006754 vim_snprintf((char *)IObuff, IOSIZE,
6755 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 (char *)buf1, (char *)buf2,
6757 (long)curwin->w_cursor.lnum,
6758 (long)curbuf->b_ml.ml_line_count,
6759 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006760 byte_count_cursor, byte_count);
6761 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006762 vim_snprintf((char *)IObuff, IOSIZE,
6763 _("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 +00006764 (char *)buf1, (char *)buf2,
6765 (long)curwin->w_cursor.lnum,
6766 (long)curbuf->b_ml.ml_line_count,
6767 word_count_cursor, word_count,
6768 char_count_cursor, char_count,
6769 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771
6772#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006773 byte_count = bomb_size();
6774 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006776 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777#endif
6778 /* Don't shorten this message, the user asked for it. */
6779 p = p_shm;
6780 p_shm = (char_u *)"";
6781 msg(IObuff);
6782 p_shm = p;
6783 }
6784}