blob: 265cf3a8545e1ace888069f39a139e330c9a29dd [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
53 * Each yank register is an array of pointers to lines.
54 */
55static struct yankreg
56{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 colnr_T y_width; /* only set if y_type == MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061} y_regs[NUM_REGISTERS];
62
63static struct yankreg *y_current; /* ptr to current yankreg */
64static int y_append; /* TRUE when appending */
65static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
66
67/*
68 * structure used by block_prep, op_delete and op_yank for blockwise operators
69 * also op_change, op_shift, op_insert, op_replace - AKelly
70 */
71struct block_def
72{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000073 int startspaces; /* 'extra' cols before first char */
74 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000076 char_u *textstart; /* pointer to 1st char (partially) in block */
77 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 colnr_T start_vcol; /* start col of 1st char wholly inside block */
79 colnr_T end_vcol; /* start col of 1st char wholly after block */
80#ifdef FEAT_VISUALEXTRA
81 int is_short; /* TRUE if line is too short to fit in block */
82 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
83 int is_oneChar; /* TRUE if block within one character */
84 int pre_whitesp; /* screen cols of ws before block */
85 int pre_whitesp_c; /* chars of ws before block */
86 colnr_T end_char_vcols; /* number of vcols of post-block char */
87#endif
88 colnr_T start_char_vcols; /* number of vcols of pre-block char */
89};
90
91#ifdef FEAT_VISUALEXTRA
92static void shift_block __ARGS((oparg_T *oap, int amount));
93static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
94#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000095static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000096static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000097static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
98 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +000099static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
101static void mb_adjust_opend __ARGS((oparg_T *oap));
102#endif
103static void free_yank __ARGS((long));
104static void free_yank_all __ARGS((void));
105static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
106#ifdef FEAT_CLIPBOARD
107static void copy_yank_reg __ARGS((struct yankreg *reg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static void may_set_selection __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110static void dis_msg __ARGS((char_u *p, int skip_esc));
Bram Moolenaar81340392012-06-06 16:12:59 +0200111#if defined(FEAT_COMMENTS) || defined(PROTO)
112static char_u *skip_comment __ARGS((char_u *line, int process, int include_space, int *is_comment));
113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar0eac8282014-04-12 12:26:36 +0200116static void str_to_reg __ARGS((struct yankreg *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118static int ends_in_white __ARGS((linenr_T lnum));
119#ifdef FEAT_COMMENTS
120static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
121static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
122#else
123static int fmt_check_par __ARGS((linenr_T));
124#endif
125
126/*
127 * The names of operators.
128 * IMPORTANT: Index must correspond with defines in vim.h!!!
129 * The third field indicates whether the operator always works on lines.
130 */
131static char opchars[][3] =
132{
133 {NUL, NUL, FALSE}, /* OP_NOP */
134 {'d', NUL, FALSE}, /* OP_DELETE */
135 {'y', NUL, FALSE}, /* OP_YANK */
136 {'c', NUL, FALSE}, /* OP_CHANGE */
137 {'<', NUL, TRUE}, /* OP_LSHIFT */
138 {'>', NUL, TRUE}, /* OP_RSHIFT */
139 {'!', NUL, TRUE}, /* OP_FILTER */
140 {'g', '~', FALSE}, /* OP_TILDE */
141 {'=', NUL, TRUE}, /* OP_INDENT */
142 {'g', 'q', TRUE}, /* OP_FORMAT */
143 {':', NUL, TRUE}, /* OP_COLON */
144 {'g', 'U', FALSE}, /* OP_UPPER */
145 {'g', 'u', FALSE}, /* OP_LOWER */
146 {'J', NUL, TRUE}, /* DO_JOIN */
147 {'g', 'J', TRUE}, /* DO_JOIN_NS */
148 {'g', '?', FALSE}, /* OP_ROT13 */
149 {'r', NUL, FALSE}, /* OP_REPLACE */
150 {'I', NUL, FALSE}, /* OP_INSERT */
151 {'A', NUL, FALSE}, /* OP_APPEND */
152 {'z', 'f', TRUE}, /* OP_FOLD */
153 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
154 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
155 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
156 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
157 {'z', 'd', TRUE}, /* OP_FOLDDEL */
158 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
159 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000160 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161};
162
163/*
164 * Translate a command name into an operator type.
165 * Must only be called with a valid operator name!
166 */
167 int
168get_op_type(char1, char2)
169 int char1;
170 int char2;
171{
172 int i;
173
174 if (char1 == 'r') /* ignore second character */
175 return OP_REPLACE;
176 if (char1 == '~') /* when tilde is an operator */
177 return OP_TILDE;
178 for (i = 0; ; ++i)
179 if (opchars[i][0] == char1 && opchars[i][1] == char2)
180 break;
181 return i;
182}
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Return TRUE if operator "op" always works on whole lines.
186 */
187 int
188op_on_lines(op)
189 int op;
190{
191 return opchars[op][2];
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
195 * Get first operator command character.
196 * Returns 'g' or 'z' if there is another command character.
197 */
198 int
199get_op_char(optype)
200 int optype;
201{
202 return opchars[optype][0];
203}
204
205/*
206 * Get second operator command character.
207 */
208 int
209get_extra_op_char(optype)
210 int optype;
211{
212 return opchars[optype][1];
213}
214
215/*
216 * op_shift - handle a shift operation
217 */
218 void
219op_shift(oap, curs_top, amount)
220 oparg_T *oap;
221 int curs_top;
222 int amount;
223{
224 long i;
225 int first_char;
226 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 if (u_save((linenr_T)(oap->start.lnum - 1),
230 (linenr_T)(oap->end.lnum + 1)) == FAIL)
231 return;
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (oap->block_mode)
234 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
236 for (i = oap->line_count; --i >= 0; )
237 {
238 first_char = *ml_get_curline();
239 if (first_char == NUL) /* empty line */
240 curwin->w_cursor.col = 0;
241#ifdef FEAT_VISUALEXTRA
242 else if (oap->block_mode)
243 shift_block(oap, amount);
244#endif
245 else
246 /* Move the line right if it doesn't start with '#', 'smartindent'
247 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
248#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
249 if (first_char != '#' || !preprocs_left())
250#endif
251 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000252 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
254 ++curwin->w_cursor.lnum;
255 }
256
257 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200258#ifdef FEAT_FOLDING
259 /* The cursor line is not in a closed fold */
260 foldOpenCursor();
261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (oap->block_mode)
264 {
265 curwin->w_cursor.lnum = oap->start.lnum;
266 curwin->w_cursor.col = block_col;
267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100268 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 curwin->w_cursor.lnum = oap->start.lnum;
271 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
272 }
273 else
274 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
275
276 if (oap->line_count > p_report)
277 {
278 if (oap->op_type == OP_RSHIFT)
279 s = (char_u *)">";
280 else
281 s = (char_u *)"<";
282 if (oap->line_count == 1)
283 {
284 if (amount == 1)
285 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
286 else
287 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
288 }
289 else
290 {
291 if (amount == 1)
292 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
293 oap->line_count, s);
294 else
295 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
296 oap->line_count, s, amount);
297 }
298 msg(IObuff);
299 }
300
301 /*
302 * Set "'[" and "']" marks.
303 */
304 curbuf->b_op_start = oap->start;
305 curbuf->b_op_end.lnum = oap->end.lnum;
306 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
307 if (curbuf->b_op_end.col > 0)
308 --curbuf->b_op_end.col;
309}
310
311/*
312 * shift the current line one shiftwidth left (if left != 0) or right
313 * leaves cursor on first blank in the line
314 */
315 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000316shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 int left;
318 int round;
319 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000320 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
322 int count;
323 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100324 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
326 count = get_indent(); /* get current indent */
327
328 if (round) /* round off indent */
329 {
330 i = count / p_sw; /* number of p_sw rounded down */
331 j = count % p_sw; /* extra spaces */
332 if (j && left) /* first remove extra spaces */
333 --amount;
334 if (left)
335 {
336 i -= amount;
337 if (i < 0)
338 i = 0;
339 }
340 else
341 i += amount;
342 count = i * p_sw;
343 }
344 else /* original vi indent */
345 {
346 if (left)
347 {
348 count -= p_sw * amount;
349 if (count < 0)
350 count = 0;
351 }
352 else
353 count += p_sw * amount;
354 }
355
356 /* Set new indent */
357#ifdef FEAT_VREPLACE
358 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000359 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 else
361#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000362 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363}
364
365#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
366/*
367 * Shift one line of the current block one shiftwidth right or left.
368 * Leaves cursor on first character in block.
369 */
370 static void
371shift_block(oap, amount)
372 oparg_T *oap;
373 int amount;
374{
375 int left = (oap->op_type == OP_LSHIFT);
376 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000377 int total;
378 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100380 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int p_ts = (int)curbuf->b_p_ts;
382 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int i = 0, j = 0;
386 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_RIGHTLEFT
388 int old_p_ri = p_ri;
389
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200390 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392
393 State = INSERT; /* don't want REPLACE for State */
394 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
395 if (bd.is_short)
396 return;
397
398 /* total is number of screen columns to be inserted/removed */
399 total = amount * p_sw;
400 oldp = ml_get_curline();
401
402 if (!left)
403 {
404 /*
405 * 1. Get start vcol
406 * 2. Total ws vcols
407 * 3. Divvy into TABs & spp
408 * 4. Construct new string
409 */
410 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
411 ws_vcol = bd.start_vcol - bd.pre_whitesp;
412 if (bd.startspaces)
413 {
414#ifdef FEAT_MBYTE
415 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000416 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000419 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 for ( ; vim_iswhite(*bd.textstart); )
422 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200423 /* TODO: is passing bd.textstart for start of the line OK? */
424 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
425 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 total += incr;
427 bd.start_vcol += incr;
428 }
429 /* OK, now total=all the VWS reqd, and textstart points at the 1st
430 * non-ws char in the block. */
431 if (!curbuf->b_p_et)
432 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
433 if (i)
434 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
435 else
436 j = total;
437 /* if we're splitting a TAB, allow for it */
438 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
439 len = (int)STRLEN(bd.textstart) + 1;
440 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
441 if (newp == NULL)
442 return;
443 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
444 mch_memmove(newp, oldp, (size_t)bd.textcol);
445 copy_chars(newp + bd.textcol, (size_t)i, TAB);
446 copy_spaces(newp + bd.textcol + i, (size_t)j);
447 /* the end */
448 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
449 }
450 else /* left */
451 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000452 colnr_T destination_col; /* column to which text in block will
453 be shifted */
454 char_u *verbatim_copy_end; /* end of the part of the line which is
455 copied verbatim */
456 colnr_T verbatim_copy_width;/* the (displayed) width of this part
457 of line */
458 unsigned fill; /* nr of spaces that replace a TAB */
459 unsigned new_line_len; /* the length of the line after the
460 block shift */
461 size_t block_space_width;
462 size_t shift_amount;
463 char_u *non_white = bd.textstart;
464 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000466 /*
467 * Firstly, let's find the first non-whitespace character that is
468 * displayed after the block's start column and the character's column
469 * number. Also, let's calculate the width of all the whitespace
470 * characters that are displayed in the block and precede the searched
471 * non-whitespace character.
472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000474 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
475 * the part of which is displayed at the block's beginning. Let's start
476 * searching from the next character. */
477 if (bd.startspaces)
478 mb_ptr_adv(non_white);
479
480 /* The character's column is in "bd.start_vcol". */
481 non_white_col = bd.start_vcol;
482
483 while (vim_iswhite(*non_white))
484 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200485 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000486 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 }
488
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000489 block_space_width = non_white_col - oap->start_vcol;
490 /* We will shift by "total" or "block_space_width", whichever is less.
491 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000492 shift_amount = (block_space_width < (size_t)total
493 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494
495 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000496 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497
498 /* Now let's find out how much of the beginning of the line we can
499 * reuse without modification. */
500 verbatim_copy_end = bd.textstart;
501 verbatim_copy_width = bd.start_vcol;
502
503 /* If "bd.startspaces" is set, "bd.textstart" points to the character
504 * preceding the block. We have to subtract its width to obtain its
505 * column number. */
506 if (bd.startspaces)
507 verbatim_copy_width -= bd.start_char_vcols;
508 while (verbatim_copy_width < destination_col)
509 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 char_u *line = verbatim_copy_end;
511
512 /* TODO: is passing verbatim_copy_end for start of the line OK? */
513 incr = lbr_chartabsize(line, verbatim_copy_end,
514 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515 if (verbatim_copy_width + incr > destination_col)
516 break;
517 verbatim_copy_width += incr;
518 mb_ptr_adv(verbatim_copy_end);
519 }
520
521 /* If "destination_col" is different from the width of the initial
522 * part of the line that will be copied, it means we encountered a tab
523 * character, which we will have to partly replace with spaces. */
524 fill = destination_col - verbatim_copy_width;
525
526 /* The replacement line will consist of:
527 * - the beginning of the original line up to "verbatim_copy_end",
528 * - "fill" number of spaces,
529 * - the rest of the line, pointed to by non_white. */
530 new_line_len = (unsigned)(verbatim_copy_end - oldp)
531 + fill
532 + (unsigned)STRLEN(non_white) + 1;
533
534 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (newp == NULL)
536 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000537 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
538 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
539 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 /* replace the line */
542 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
543 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
544 State = oldstate;
545 curwin->w_cursor.col = oldcol;
546#ifdef FEAT_RIGHTLEFT
547 p_ri = old_p_ri;
548#endif
549}
550#endif
551
552#ifdef FEAT_VISUALEXTRA
553/*
554 * Insert string "s" (b_insert ? before : after) block :AKelly
555 * Caller must prepare for undo.
556 */
557 static void
558block_insert(oap, s, b_insert, bdp)
559 oparg_T *oap;
560 char_u *s;
561 int b_insert;
562 struct block_def *bdp;
563{
564 int p_ts;
565 int count = 0; /* extra spaces to replace a cut TAB */
566 int spaces = 0; /* non-zero if cutting a TAB */
567 colnr_T offset; /* pointer along new line */
568 unsigned s_len; /* STRLEN(s) */
569 char_u *newp, *oldp; /* new, old lines */
570 linenr_T lnum; /* loop var */
571 int oldstate = State;
572
573 State = INSERT; /* don't want REPLACE for State */
574 s_len = (unsigned)STRLEN(s);
575
576 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
577 {
578 block_prep(oap, bdp, lnum, TRUE);
579 if (bdp->is_short && b_insert)
580 continue; /* OP_INSERT, line ends before block start */
581
582 oldp = ml_get(lnum);
583
584 if (b_insert)
585 {
586 p_ts = bdp->start_char_vcols;
587 spaces = bdp->startspaces;
588 if (spaces != 0)
589 count = p_ts - 1; /* we're cutting a TAB */
590 offset = bdp->textcol;
591 }
592 else /* append */
593 {
594 p_ts = bdp->end_char_vcols;
595 if (!bdp->is_short) /* spaces = padding after block */
596 {
597 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
598 if (spaces != 0)
599 count = p_ts - 1; /* we're cutting a TAB */
600 offset = bdp->textcol + bdp->textlen - (spaces != 0);
601 }
602 else /* spaces = padding to block edge */
603 {
604 /* if $ used, just append to EOL (ie spaces==0) */
605 if (!bdp->is_MAX)
606 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
607 count = spaces;
608 offset = bdp->textcol + bdp->textlen;
609 }
610 }
611
612 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
613 if (newp == NULL)
614 continue;
615
616 /* copy up to shifted part */
617 mch_memmove(newp, oldp, (size_t)(offset));
618 oldp += offset;
619
620 /* insert pre-padding */
621 copy_spaces(newp + offset, (size_t)spaces);
622
623 /* copy the new text */
624 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
625 offset += s_len;
626
627 if (spaces && !bdp->is_short)
628 {
629 /* insert post-padding */
630 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
631 /* We're splitting a TAB, don't copy it. */
632 oldp++;
633 /* We allowed for that TAB, remember this now */
634 count++;
635 }
636
637 if (spaces > 0)
638 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000639 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 ml_replace(lnum, newp, FALSE);
642
643 if (lnum == oap->end.lnum)
644 {
645 /* Set "']" mark to the end of the block instead of the end of
646 * the insert in the first line. */
647 curbuf->b_op_end.lnum = oap->end.lnum;
648 curbuf->b_op_end.col = offset;
649 }
650 } /* for all lnum */
651
652 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
653
654 State = oldstate;
655}
656#endif
657
658#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
659/*
660 * op_reindent - handle reindenting a block of lines.
661 */
662 void
663op_reindent(oap, how)
664 oparg_T *oap;
665 int (*how) __ARGS((void));
666{
667 long i;
668 char_u *l;
669 int count;
670 linenr_T first_changed = 0;
671 linenr_T last_changed = 0;
672 linenr_T start_lnum = curwin->w_cursor.lnum;
673
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000674 /* Don't even try when 'modifiable' is off. */
675 if (!curbuf->b_p_ma)
676 {
677 EMSG(_(e_modifiable));
678 return;
679 }
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 for (i = oap->line_count; --i >= 0 && !got_int; )
682 {
683 /* it's a slow thing to do, so give feedback so there's no worry that
684 * the computer's just hung. */
685
686 if (i > 1
687 && (i % 50 == 0 || i == oap->line_count - 1)
688 && oap->line_count > p_report)
689 smsg((char_u *)_("%ld lines to indent... "), i);
690
691 /*
692 * Be vi-compatible: For lisp indenting the first line is not
693 * indented, unless there is only one line.
694 */
695#ifdef FEAT_LISP
696 if (i != oap->line_count - 1 || oap->line_count == 1
697 || how != get_lisp_indent)
698#endif
699 {
700 l = skipwhite(ml_get_curline());
701 if (*l == NUL) /* empty or blank line */
702 count = 0;
703 else
704 count = how(); /* get the indent for this line */
705
706 if (set_indent(count, SIN_UNDO))
707 {
708 /* did change the indent, call changed_lines() later */
709 if (first_changed == 0)
710 first_changed = curwin->w_cursor.lnum;
711 last_changed = curwin->w_cursor.lnum;
712 }
713 }
714 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000715 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717
718 /* put cursor on first non-blank of indented line */
719 curwin->w_cursor.lnum = start_lnum;
720 beginline(BL_SOL | BL_FIX);
721
722 /* Mark changed lines so that they will be redrawn. When Visual
723 * highlighting was present, need to continue until the last line. When
724 * there is no change still need to remove the Visual highlighting. */
725 if (last_changed != 0)
726 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 else if (oap->is_VIsual)
730 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732 if (oap->line_count > p_report)
733 {
734 i = oap->line_count - (i + 1);
735 if (i == 1)
736 MSG(_("1 line indented "));
737 else
738 smsg((char_u *)_("%ld lines indented "), i);
739 }
740 /* set '[ and '] marks */
741 curbuf->b_op_start = oap->start;
742 curbuf->b_op_end = oap->end;
743}
744#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
745
746#if defined(FEAT_EVAL) || defined(PROTO)
747/*
748 * Keep the last expression line here, for repeating.
749 */
750static char_u *expr_line = NULL;
751
752/*
753 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
754 * Returns '=' when OK, NUL otherwise.
755 */
756 int
757get_expr_register()
758{
759 char_u *new_line;
760
761 new_line = getcmdline('=', 0L, 0);
762 if (new_line == NULL)
763 return NUL;
764 if (*new_line == NUL) /* use previous line */
765 vim_free(new_line);
766 else
767 set_expr_line(new_line);
768 return '=';
769}
770
771/*
772 * Set the expression for the '=' register.
773 * Argument must be an allocated string.
774 */
775 void
776set_expr_line(new_line)
777 char_u *new_line;
778{
779 vim_free(expr_line);
780 expr_line = new_line;
781}
782
783/*
784 * Get the result of the '=' register expression.
785 * Returns a pointer to allocated memory, or NULL for failure.
786 */
787 char_u *
788get_expr_line()
789{
790 char_u *expr_copy;
791 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000792 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 if (expr_line == NULL)
795 return NULL;
796
797 /* Make a copy of the expression, because evaluating it may cause it to be
798 * changed. */
799 expr_copy = vim_strsave(expr_line);
800 if (expr_copy == NULL)
801 return NULL;
802
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000803 /* When we are invoked recursively limit the evaluation to 10 levels.
804 * Then return the string as-is. */
805 if (nested >= 10)
806 return expr_copy;
807
808 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000809 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000810 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 vim_free(expr_copy);
812 return rv;
813}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000814
815/*
816 * Get the '=' register expression itself, without evaluating it.
817 */
818 char_u *
819get_expr_line_src()
820{
821 if (expr_line == NULL)
822 return NULL;
823 return vim_strsave(expr_line);
824}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#endif /* FEAT_EVAL */
826
827/*
828 * Check if 'regname' is a valid name of a yank register.
829 * Note: There is no check for 0 (default register), caller should do this
830 */
831 int
832valid_yank_reg(regname, writing)
833 int regname;
834 int writing; /* if TRUE check for writable registers */
835{
836 if ( (regname > 0 && ASCII_ISALNUM(regname))
837 || (!writing && vim_strchr((char_u *)
838#ifdef FEAT_EVAL
839 "/.%#:="
840#else
841 "/.%#:"
842#endif
843 , regname) != NULL)
844 || regname == '"'
845 || regname == '-'
846 || regname == '_'
847#ifdef FEAT_CLIPBOARD
848 || regname == '*'
849 || regname == '+'
850#endif
851#ifdef FEAT_DND
852 || (!writing && regname == '~')
853#endif
854 )
855 return TRUE;
856 return FALSE;
857}
858
859/*
860 * Set y_current and y_append, according to the value of "regname".
861 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000862 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 *
864 * If regname is 0 and writing, use register 0
865 * If regname is 0 and reading, use previous register
866 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000867 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868get_yank_register(regname, writing)
869 int regname;
870 int writing;
871{
872 int i;
873
874 y_append = FALSE;
875 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
876 {
877 y_current = y_previous;
878 return;
879 }
880 i = regname;
881 if (VIM_ISDIGIT(i))
882 i -= '0';
883 else if (ASCII_ISLOWER(i))
884 i = CharOrdLow(i) + 10;
885 else if (ASCII_ISUPPER(i))
886 {
887 i = CharOrdUp(i) + 10;
888 y_append = TRUE;
889 }
890 else if (regname == '-')
891 i = DELETION_REGISTER;
892#ifdef FEAT_CLIPBOARD
893 /* When selection is not available, use register 0 instead of '*' */
894 else if (clip_star.available && regname == '*')
895 i = STAR_REGISTER;
896 /* When clipboard is not available, use register 0 instead of '+' */
897 else if (clip_plus.available && regname == '+')
898 i = PLUS_REGISTER;
899#endif
900#ifdef FEAT_DND
901 else if (!writing && regname == '~')
902 i = TILDE_REGISTER;
903#endif
904 else /* not 0-9, a-z, A-Z or '-': use register 0 */
905 i = 0;
906 y_current = &(y_regs[i]);
907 if (writing) /* remember the register we write into for do_put() */
908 y_previous = y_current;
909}
910
Bram Moolenaar8299df92004-07-10 09:47:34 +0000911#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912/*
913 * When "regname" is a clipboard register, obtain the selection. If it's not
914 * available return zero, otherwise return "regname".
915 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000916 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917may_get_selection(regname)
918 int regname;
919{
920 if (regname == '*')
921 {
922 if (!clip_star.available)
923 regname = 0;
924 else
925 clip_get_selection(&clip_star);
926 }
927 else if (regname == '+')
928 {
929 if (!clip_plus.available)
930 regname = 0;
931 else
932 clip_get_selection(&clip_plus);
933 }
934 return regname;
935}
936#endif
937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * Obtain the contents of a "normal" register. The register is made empty.
940 * The returned pointer has allocated memory, use put_register() later.
941 */
942 void *
943get_register(name, copy)
944 int name;
945 int copy; /* make a copy, if FALSE make register empty. */
946{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000947 struct yankreg *reg;
948 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950#ifdef FEAT_CLIPBOARD
951 /* When Visual area changed, may have to update selection. Obtain the
952 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000953 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200955 if (clip_isautosel_star())
956 clip_update_selection(&clip_star);
957 may_get_selection(name);
958 }
959 if (name == '+' && clip_plus.available)
960 {
961 if (clip_isautosel_plus())
962 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 may_get_selection(name);
964 }
965#endif
966
967 get_yank_register(name, 0);
968 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
969 if (reg != NULL)
970 {
971 *reg = *y_current;
972 if (copy)
973 {
974 /* If we run out of memory some or all of the lines are empty. */
975 if (reg->y_size == 0)
976 reg->y_array = NULL;
977 else
978 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
979 * reg->y_size));
980 if (reg->y_array != NULL)
981 {
982 for (i = 0; i < reg->y_size; ++i)
983 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
984 }
985 }
986 else
987 y_current->y_array = NULL;
988 }
989 return (void *)reg;
990}
991
992/*
Bram Moolenaar0a307462007-12-01 20:13:05 +0000993 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 */
995 void
996put_register(name, reg)
997 int name;
998 void *reg;
999{
1000 get_yank_register(name, 0);
1001 free_yank_all();
1002 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001003 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001005#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /* Send text written to clipboard register to the clipboard. */
1007 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001010
1011 void
1012free_register(reg)
1013 void *reg;
1014{
1015 struct yankreg tmp;
1016
1017 tmp = *y_current;
1018 *y_current = *(struct yankreg *)reg;
1019 free_yank_all();
1020 vim_free(reg);
1021 *y_current = tmp;
1022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024#if defined(FEAT_MOUSE) || defined(PROTO)
1025/*
1026 * return TRUE if the current yank register has type MLINE
1027 */
1028 int
1029yank_register_mline(regname)
1030 int regname;
1031{
1032 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1033 return FALSE;
1034 if (regname == '_') /* black hole is always empty */
1035 return FALSE;
1036 get_yank_register(regname, FALSE);
1037 return (y_current->y_type == MLINE);
1038}
1039#endif
1040
1041/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001042 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001044 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 */
1046 int
1047do_record(c)
1048 int c;
1049{
Bram Moolenaard55de222007-05-06 13:38:48 +00001050 char_u *p;
1051 static int regname;
1052 struct yankreg *old_y_previous, *old_y_current;
1053 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
1055 if (Recording == FALSE) /* start recording */
1056 {
1057 /* registers 0-9, a-z and " are allowed */
1058 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1059 retval = FAIL;
1060 else
1061 {
1062 Recording = TRUE;
1063 showmode();
1064 regname = c;
1065 retval = OK;
1066 }
1067 }
1068 else /* stop recording */
1069 {
1070 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1072 * needs to be removed again to put it in a register. exec_reg then
1073 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 Recording = FALSE;
1076 MSG("");
1077 p = get_recorded();
1078 if (p == NULL)
1079 retval = FAIL;
1080 else
1081 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001082 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1083 vim_unescape_csi(p);
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 /*
1086 * We don't want to change the default register here, so save and
1087 * restore the current register name.
1088 */
1089 old_y_previous = y_previous;
1090 old_y_current = y_current;
1091
1092 retval = stuff_yank(regname, p);
1093
1094 y_previous = old_y_previous;
1095 y_current = old_y_current;
1096 }
1097 }
1098 return retval;
1099}
1100
1101/*
1102 * Stuff string "p" into yank register "regname" as a single line (append if
1103 * uppercase). "p" must have been alloced.
1104 *
1105 * return FAIL for failure, OK otherwise
1106 */
1107 static int
1108stuff_yank(regname, p)
1109 int regname;
1110 char_u *p;
1111{
1112 char_u *lp;
1113 char_u **pp;
1114
1115 /* check for read-only register */
1116 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1117 {
1118 vim_free(p);
1119 return FAIL;
1120 }
1121 if (regname == '_') /* black hole: don't do anything */
1122 {
1123 vim_free(p);
1124 return OK;
1125 }
1126 get_yank_register(regname, TRUE);
1127 if (y_append && y_current->y_array != NULL)
1128 {
1129 pp = &(y_current->y_array[y_current->y_size - 1]);
1130 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1131 if (lp == NULL)
1132 {
1133 vim_free(p);
1134 return FAIL;
1135 }
1136 STRCPY(lp, *pp);
1137 STRCAT(lp, p);
1138 vim_free(p);
1139 vim_free(*pp);
1140 *pp = lp;
1141 }
1142 else
1143 {
1144 free_yank_all();
1145 if ((y_current->y_array =
1146 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1147 {
1148 vim_free(p);
1149 return FAIL;
1150 }
1151 y_current->y_array[0] = p;
1152 y_current->y_size = 1;
1153 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1154 }
1155 return OK;
1156}
1157
Bram Moolenaar42b94362009-05-26 16:12:37 +00001158static int execreg_lastc = NUL;
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160/*
1161 * execute a yank register: copy it into the stuff buffer
1162 *
1163 * return FAIL for failure, OK otherwise
1164 */
1165 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001166do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 int regname;
1168 int colon; /* insert ':' before each line */
1169 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001170 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 long i;
1173 char_u *p;
1174 int retval = OK;
1175 int remap;
1176
1177 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001178 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001179 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001180 {
1181 EMSG(_("E748: No previously used register"));
1182 return FAIL;
1183 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001184 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 /* check for valid regname */
1187 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001188 {
1189 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001191 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001192 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
1194#ifdef FEAT_CLIPBOARD
1195 regname = may_get_selection(regname);
1196#endif
1197
1198 if (regname == '_') /* black hole: don't stuff anything */
1199 return OK;
1200
1201#ifdef FEAT_CMDHIST
1202 if (regname == ':') /* use last command line */
1203 {
1204 if (last_cmdline == NULL)
1205 {
1206 EMSG(_(e_nolastcmd));
1207 return FAIL;
1208 }
1209 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1210 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001211 /* Escape all control characters with a CTRL-V */
1212 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001213 (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 +00001214 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001215 {
1216 /* When in Visual mode "'<,'>" will be prepended to the command.
1217 * Remove it when it's already there. */
1218 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001219 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001220 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001221 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001222 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001223 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225#endif
1226#ifdef FEAT_EVAL
1227 else if (regname == '=')
1228 {
1229 p = get_expr_line();
1230 if (p == NULL)
1231 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001232 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 vim_free(p);
1234 }
1235#endif
1236 else if (regname == '.') /* use last inserted text */
1237 {
1238 p = get_last_insert_save();
1239 if (p == NULL)
1240 {
1241 EMSG(_(e_noinstext));
1242 return FAIL;
1243 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001244 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 vim_free(p);
1246 }
1247 else
1248 {
1249 get_yank_register(regname, FALSE);
1250 if (y_current->y_array == NULL)
1251 return FAIL;
1252
1253 /* Disallow remaping for ":@r". */
1254 remap = colon ? REMAP_NONE : REMAP_YES;
1255
1256 /*
1257 * Insert lines into typeahead buffer, from last one to first one.
1258 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001259 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 for (i = y_current->y_size; --i >= 0; )
1261 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001262 char_u *escaped;
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /* insert NL between lines and after last line if type is MLINE */
1265 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1266 || addcr)
1267 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001268 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 return FAIL;
1270 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001271 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1272 if (escaped == NULL)
1273 return FAIL;
1274 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1275 vim_free(escaped);
1276 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001278 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 == FAIL)
1280 return FAIL;
1281 }
1282 Exec_reg = TRUE; /* disable the 'q' command */
1283 }
1284 return retval;
1285}
1286
1287/*
1288 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1289 * used only after other typeahead has been processed.
1290 */
1291 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001292put_reedit_in_typebuf(silent)
1293 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 char_u buf[3];
1296
1297 if (restart_edit != NUL)
1298 {
1299 if (restart_edit == 'V')
1300 {
1301 buf[0] = 'g';
1302 buf[1] = 'R';
1303 buf[2] = NUL;
1304 }
1305 else
1306 {
1307 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1308 buf[1] = NUL;
1309 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001310 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 restart_edit = NUL;
1312 }
1313}
1314
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001315/*
1316 * Insert register contents "s" into the typeahead buffer, so that it will be
1317 * executed again.
1318 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1319 * no remapping.
1320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001322put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001324 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001326 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 int retval = OK;
1329
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001330 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001332 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001334 {
1335 char_u *p;
1336
1337 if (esc)
1338 p = vim_strsave_escape_csi(s);
1339 else
1340 p = s;
1341 if (p == NULL)
1342 retval = FAIL;
1343 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001344 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1345 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001346 if (esc)
1347 vim_free(p);
1348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001350 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 return retval;
1352}
1353
1354/*
1355 * Insert a yank register: copy it into the Read buffer.
1356 * Used by CTRL-R command and middle mouse button in insert mode.
1357 *
1358 * return FAIL for failure, OK otherwise
1359 */
1360 int
1361insert_reg(regname, literally)
1362 int regname;
1363 int literally; /* insert literally, not as if typed */
1364{
1365 long i;
1366 int retval = OK;
1367 char_u *arg;
1368 int allocated;
1369
1370 /*
1371 * It is possible to get into an endless loop by having CTRL-R a in
1372 * register a and then, in insert mode, doing CTRL-R a.
1373 * If you hit CTRL-C, the loop will be broken here.
1374 */
1375 ui_breakcheck();
1376 if (got_int)
1377 return FAIL;
1378
1379 /* check for valid regname */
1380 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1381 return FAIL;
1382
1383#ifdef FEAT_CLIPBOARD
1384 regname = may_get_selection(regname);
1385#endif
1386
1387 if (regname == '.') /* insert last inserted text */
1388 retval = stuff_inserted(NUL, 1L, TRUE);
1389 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1390 {
1391 if (arg == NULL)
1392 return FAIL;
1393 stuffescaped(arg, literally);
1394 if (allocated)
1395 vim_free(arg);
1396 }
1397 else /* name or number register */
1398 {
1399 get_yank_register(regname, FALSE);
1400 if (y_current->y_array == NULL)
1401 retval = FAIL;
1402 else
1403 {
1404 for (i = 0; i < y_current->y_size; ++i)
1405 {
1406 stuffescaped(y_current->y_array[i], literally);
1407 /*
1408 * Insert a newline between lines and after last line if
1409 * y_type is MLINE.
1410 */
1411 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1412 stuffcharReadbuff('\n');
1413 }
1414 }
1415 }
1416
1417 return retval;
1418}
1419
1420/*
1421 * Stuff a string into the typeahead buffer, such that edit() will insert it
1422 * literally ("literally" TRUE) or interpret is as typed characters.
1423 */
1424 static void
1425stuffescaped(arg, literally)
1426 char_u *arg;
1427 int literally;
1428{
1429 int c;
1430 char_u *start;
1431
1432 while (*arg != NUL)
1433 {
1434 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1435 * stuff K_SPECIAL to get the effect of a special key when "literally"
1436 * is TRUE. */
1437 start = arg;
1438 while ((*arg >= ' '
1439#ifndef EBCDIC
1440 && *arg < DEL /* EBCDIC: chars above space are normal */
1441#endif
1442 )
1443 || (*arg == K_SPECIAL && !literally))
1444 ++arg;
1445 if (arg > start)
1446 stuffReadbuffLen(start, (long)(arg - start));
1447
1448 /* stuff a single special character */
1449 if (*arg != NUL)
1450 {
1451#ifdef FEAT_MBYTE
1452 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001453 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 else
1455#endif
1456 c = *arg++;
1457 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1458 stuffcharReadbuff(Ctrl_V);
1459 stuffcharReadbuff(c);
1460 }
1461 }
1462}
1463
1464/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001465 * If "regname" is a special register, return TRUE and store a pointer to its
1466 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001468 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469get_spec_reg(regname, argp, allocated, errmsg)
1470 int regname;
1471 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001472 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 int errmsg; /* give error message when failing */
1474{
1475 int cnt;
1476
1477 *argp = NULL;
1478 *allocated = FALSE;
1479 switch (regname)
1480 {
1481 case '%': /* file name */
1482 if (errmsg)
1483 check_fname(); /* will give emsg if not set */
1484 *argp = curbuf->b_fname;
1485 return TRUE;
1486
1487 case '#': /* alternate file name */
1488 *argp = getaltfname(errmsg); /* may give emsg if not set */
1489 return TRUE;
1490
1491#ifdef FEAT_EVAL
1492 case '=': /* result of expression */
1493 *argp = get_expr_line();
1494 *allocated = TRUE;
1495 return TRUE;
1496#endif
1497
1498 case ':': /* last command line */
1499 if (last_cmdline == NULL && errmsg)
1500 EMSG(_(e_nolastcmd));
1501 *argp = last_cmdline;
1502 return TRUE;
1503
1504 case '/': /* last search-pattern */
1505 if (last_search_pat() == NULL && errmsg)
1506 EMSG(_(e_noprevre));
1507 *argp = last_search_pat();
1508 return TRUE;
1509
1510 case '.': /* last inserted text */
1511 *argp = get_last_insert_save();
1512 *allocated = TRUE;
1513 if (*argp == NULL && errmsg)
1514 EMSG(_(e_noinstext));
1515 return TRUE;
1516
1517#ifdef FEAT_SEARCHPATH
1518 case Ctrl_F: /* Filename under cursor */
1519 case Ctrl_P: /* Path under cursor, expand via "path" */
1520 if (!errmsg)
1521 return FALSE;
1522 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001523 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 *allocated = TRUE;
1525 return TRUE;
1526#endif
1527
1528 case Ctrl_W: /* word under cursor */
1529 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1530 if (!errmsg)
1531 return FALSE;
1532 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1533 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1534 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1535 *allocated = TRUE;
1536 return TRUE;
1537
1538 case '_': /* black hole: always empty */
1539 *argp = (char_u *)"";
1540 return TRUE;
1541 }
1542
1543 return FALSE;
1544}
1545
1546/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001547 * Paste a yank register into the command line.
1548 * Only for non-special registers.
1549 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 * insert_reg() can't be used here, because special characters from the
1551 * register contents will be interpreted as commands.
1552 *
1553 * return FAIL for failure, OK otherwise
1554 */
1555 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001556cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 int regname;
1558 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001559 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
1563 get_yank_register(regname, FALSE);
1564 if (y_current->y_array == NULL)
1565 return FAIL;
1566
1567 for (i = 0; i < y_current->y_size; ++i)
1568 {
1569 cmdline_paste_str(y_current->y_array[i], literally);
1570
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001571 /* Insert ^M between lines and after last line if type is MLINE.
1572 * Don't do this when "remcr" is TRUE and the next line is empty. */
1573 if (y_current->y_type == MLINE
1574 || (i < y_current->y_size - 1
1575 && !(remcr
1576 && i == y_current->y_size - 2
1577 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 cmdline_paste_str((char_u *)"\r", literally);
1579
1580 /* Check for CTRL-C, in case someone tries to paste a few thousand
1581 * lines and gets bored. */
1582 ui_breakcheck();
1583 if (got_int)
1584 return FAIL;
1585 }
1586 return OK;
1587}
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1590/*
1591 * Adjust the register name pointed to with "rp" for the clipboard being
1592 * used always and the clipboard being available.
1593 */
1594 void
1595adjust_clip_reg(rp)
1596 int *rp;
1597{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001598 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1599 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
1600 if (*rp == 0 && clip_unnamed != 0)
1601 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
1602 ? '+' : '*';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (!clip_star.available && *rp == '*')
1604 *rp = 0;
1605 if (!clip_plus.available && *rp == '+')
1606 *rp = 0;
1607}
1608#endif
1609
1610/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001611 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001613 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 */
1615 int
1616op_delete(oap)
1617 oparg_T *oap;
1618{
1619 int n;
1620 linenr_T lnum;
1621 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 char_u *newp, *oldp;
1623 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1625 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001626 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1629 return OK;
1630
1631 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1632 if (oap->empty)
1633 return u_save_cursor();
1634
1635 if (!curbuf->b_p_ma)
1636 {
1637 EMSG(_(e_modifiable));
1638 return FAIL;
1639 }
1640
1641#ifdef FEAT_CLIPBOARD
1642 adjust_clip_reg(&oap->regname);
1643#endif
1644
1645#ifdef FEAT_MBYTE
1646 if (has_mbyte)
1647 mb_adjust_opend(oap);
1648#endif
1649
Bram Moolenaard04b7502010-07-08 22:27:55 +02001650 /*
1651 * Imitate the strange Vi behaviour: If the delete spans more than one
1652 * line and motion_type == MCHAR and the result is a blank line, make the
1653 * delete linewise. Don't do this for the change command or Visual mode.
1654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001657 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001659 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 && oap->op_type == OP_DELETE)
1661 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001662 ptr = ml_get(oap->end.lnum) + oap->end.col;
1663 if (*ptr != NUL)
1664 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 ptr = skipwhite(ptr);
1666 if (*ptr == NUL && inindent(0))
1667 oap->motion_type = MLINE;
1668 }
1669
Bram Moolenaard04b7502010-07-08 22:27:55 +02001670 /*
1671 * Check for trying to delete (e.g. "D") in an empty line.
1672 * Note: For the change operator it is ok.
1673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if ( oap->motion_type == MCHAR
1675 && oap->line_count == 1
1676 && oap->op_type == OP_DELETE
1677 && *ml_get(oap->start.lnum) == NUL)
1678 {
1679 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001680 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 * 'cpoptions' (Vi compatible).
1682 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001683#ifdef FEAT_VIRTUALEDIT
1684 if (virtual_op)
1685 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1686 * marks as if it happened. */
1687 goto setmarks;
1688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1690 beep_flush();
1691 return OK;
1692 }
1693
Bram Moolenaard04b7502010-07-08 22:27:55 +02001694 /*
1695 * Do a yank of whatever we're about to delete.
1696 * If a yank register was specified, put the deleted text into that
1697 * register. For the black hole register '_' don't yank anything.
1698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if (oap->regname != '_')
1700 {
1701 if (oap->regname != 0)
1702 {
1703 /* check for read-only register */
1704 if (!valid_yank_reg(oap->regname, TRUE))
1705 {
1706 beep_flush();
1707 return OK;
1708 }
1709 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1710 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1711 did_yank = TRUE;
1712 }
1713
1714 /*
1715 * Put deleted text into register 1 and shift number registers if the
1716 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001717 * Use the register name from before adjust_clip_reg() may have
1718 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001720 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 || oap->line_count > 1 || oap->use_reg_one)
1722 {
1723 y_current = &y_regs[9];
1724 free_yank_all(); /* free register nine */
1725 for (n = 9; n > 1; --n)
1726 y_regs[n] = y_regs[n - 1];
1727 y_previous = y_current = &y_regs[1];
1728 y_regs[1].y_array = NULL; /* set register one to empty */
1729 if (op_yank(oap, TRUE, FALSE) == OK)
1730 did_yank = TRUE;
1731 }
1732
Bram Moolenaar84298db2012-04-20 13:46:08 +02001733 /* Yank into small delete register when no named register specified
1734 * and the delete is within one line. */
1735 if ((
1736#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001737 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1738 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001739#endif
1740 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 && oap->line_count == 1)
1742 {
1743 oap->regname = '-';
1744 get_yank_register(oap->regname, TRUE);
1745 if (op_yank(oap, TRUE, FALSE) == OK)
1746 did_yank = TRUE;
1747 oap->regname = 0;
1748 }
1749
1750 /*
1751 * If there's too much stuff to fit in the yank register, then get a
1752 * confirmation before doing the delete. This is crude, but simple.
1753 * And it avoids doing a delete of something we can't put back if we
1754 * want.
1755 */
1756 if (!did_yank)
1757 {
1758 int msg_silent_save = msg_silent;
1759
1760 msg_silent = 0; /* must display the prompt */
1761 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1762 msg_silent = msg_silent_save;
1763 if (n != 'y')
1764 {
1765 EMSG(_(e_abort));
1766 return FAIL;
1767 }
1768 }
1769 }
1770
Bram Moolenaard04b7502010-07-08 22:27:55 +02001771 /*
1772 * block mode delete
1773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (oap->block_mode)
1775 {
1776 if (u_save((linenr_T)(oap->start.lnum - 1),
1777 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1778 return FAIL;
1779
1780 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1781 {
1782 block_prep(oap, &bd, lnum, TRUE);
1783 if (bd.textlen == 0) /* nothing to delete */
1784 continue;
1785
1786 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1787 if (lnum == curwin->w_cursor.lnum)
1788 {
1789 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1790# ifdef FEAT_VIRTUALEDIT
1791 curwin->w_cursor.coladd = 0;
1792# endif
1793 }
1794
1795 /* n == number of chars deleted
1796 * If we delete a TAB, it may be replaced by several characters.
1797 * Thus the number of characters may increase!
1798 */
1799 n = bd.textlen - bd.startspaces - bd.endspaces;
1800 oldp = ml_get(lnum);
1801 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1802 if (newp == NULL)
1803 continue;
1804 /* copy up to deleted part */
1805 mch_memmove(newp, oldp, (size_t)bd.textcol);
1806 /* insert spaces */
1807 copy_spaces(newp + bd.textcol,
1808 (size_t)(bd.startspaces + bd.endspaces));
1809 /* copy the part after the deleted part */
1810 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001811 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 /* replace the line */
1813 ml_replace(lnum, newp, FALSE);
1814 }
1815
1816 check_cursor_col();
1817 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1818 oap->end.lnum + 1, 0L);
1819 oap->line_count = 0; /* no lines deleted */
1820 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001821 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
1823 if (oap->op_type == OP_CHANGE)
1824 {
1825 /* Delete the lines except the first one. Temporarily move the
1826 * cursor to the next line. Save the current line number, if the
1827 * last line is deleted it may be changed.
1828 */
1829 if (oap->line_count > 1)
1830 {
1831 lnum = curwin->w_cursor.lnum;
1832 ++curwin->w_cursor.lnum;
1833 del_lines((long)(oap->line_count - 1), TRUE);
1834 curwin->w_cursor.lnum = lnum;
1835 }
1836 if (u_save_cursor() == FAIL)
1837 return FAIL;
1838 if (curbuf->b_p_ai) /* don't delete indent */
1839 {
1840 beginline(BL_WHITE); /* cursor on first non-white */
1841 did_ai = TRUE; /* delete the indent when ESC hit */
1842 ai_col = curwin->w_cursor.col;
1843 }
1844 else
1845 beginline(0); /* cursor in column 0 */
1846 truncate_line(FALSE); /* delete the rest of the line */
1847 /* leave cursor past last char in line */
1848 if (oap->line_count > 1)
1849 u_clearline(); /* "U" command not possible after "2cc" */
1850 }
1851 else
1852 {
1853 del_lines(oap->line_count, TRUE);
1854 beginline(BL_WHITE | BL_FIX);
1855 u_clearline(); /* "U" command not possible after "dd" */
1856 }
1857 }
1858 else
1859 {
1860#ifdef FEAT_VIRTUALEDIT
1861 if (virtual_op)
1862 {
1863 int endcol = 0;
1864
1865 /* For virtualedit: break the tabs that are partly included. */
1866 if (gchar_pos(&oap->start) == '\t')
1867 {
1868 if (u_save_cursor() == FAIL) /* save first line for undo */
1869 return FAIL;
1870 if (oap->line_count == 1)
1871 endcol = getviscol2(oap->end.col, oap->end.coladd);
1872 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1873 oap->start = curwin->w_cursor;
1874 if (oap->line_count == 1)
1875 {
1876 coladvance(endcol);
1877 oap->end.col = curwin->w_cursor.col;
1878 oap->end.coladd = curwin->w_cursor.coladd;
1879 curwin->w_cursor = oap->start;
1880 }
1881 }
1882
1883 /* Break a tab only when it's included in the area. */
1884 if (gchar_pos(&oap->end) == '\t'
1885 && (int)oap->end.coladd < oap->inclusive)
1886 {
1887 /* save last line for undo */
1888 if (u_save((linenr_T)(oap->end.lnum - 1),
1889 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1890 return FAIL;
1891 curwin->w_cursor = oap->end;
1892 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1893 oap->end = curwin->w_cursor;
1894 curwin->w_cursor = oap->start;
1895 }
1896 }
1897#endif
1898
1899 if (oap->line_count == 1) /* delete characters within one line */
1900 {
1901 if (u_save_cursor() == FAIL) /* save line for undo */
1902 return FAIL;
1903
1904 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001905 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 && oap->op_type == OP_CHANGE
1907 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001908 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 display_dollar(oap->end.col - !oap->inclusive);
1910
1911 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1912
1913#ifdef FEAT_VIRTUALEDIT
1914 if (virtual_op)
1915 {
1916 /* fix up things for virtualedit-delete:
1917 * break the tabs which are going to get in our way
1918 */
1919 char_u *curline = ml_get_curline();
1920 int len = (int)STRLEN(curline);
1921
1922 if (oap->end.coladd != 0
1923 && (int)oap->end.col >= len - 1
1924 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1925 n++;
1926 /* Delete at least one char (e.g, when on a control char). */
1927 if (n == 0 && oap->start.coladd != oap->end.coladd)
1928 n = 1;
1929
1930 /* When deleted a char in the line, reset coladd. */
1931 if (gchar_cursor() != NUL)
1932 curwin->w_cursor.coladd = 0;
1933 }
1934#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001935 if (oap->op_type == OP_DELETE
1936 && oap->inclusive
1937 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001938 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1939 {
1940 /* Special case: gH<Del> deletes the last line. */
1941 del_lines(1L, FALSE);
1942 }
1943 else
1944 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001945 (void)del_bytes((long)n, !virtual_op,
1946 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else /* delete characters between lines */
1950 {
1951 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001952 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954 /* save deleted and changed lines for undo */
1955 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1956 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1957 return FAIL;
1958
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001959 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 truncate_line(TRUE); /* delete from cursor to end of line */
1961
1962 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1963 ++curwin->w_cursor.lnum;
1964 del_lines((long)(oap->line_count - 2), FALSE);
1965
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001966 if (delete_last_line)
1967 oap->end.lnum = curbuf->b_ml.ml_line_count;
1968
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001969 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001970 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001971 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1972 {
1973 /* Special case: gH<Del> deletes the last line. */
1974 del_lines(1L, FALSE);
1975 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01001976 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1977 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001978 }
1979 else
1980 {
1981 /* delete from start of line until op_end */
1982 curwin->w_cursor.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001983 (void)del_bytes((long)n, !virtual_op,
1984 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001985 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1986 }
1987 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001988 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 }
1991
1992 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1993
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001994#ifdef FEAT_VIRTUALEDIT
1995setmarks:
1996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (oap->block_mode)
1998 {
1999 curbuf->b_op_end.lnum = oap->end.lnum;
2000 curbuf->b_op_end.col = oap->start.col;
2001 }
2002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 curbuf->b_op_end = oap->start;
2004 curbuf->b_op_start = oap->start;
2005
2006 return OK;
2007}
2008
2009#ifdef FEAT_MBYTE
2010/*
2011 * Adjust end of operating area for ending on a multi-byte character.
2012 * Used for deletion.
2013 */
2014 static void
2015mb_adjust_opend(oap)
2016 oparg_T *oap;
2017{
2018 char_u *p;
2019
2020 if (oap->inclusive)
2021 {
2022 p = ml_get(oap->end.lnum);
2023 oap->end.col += mb_tail_off(p, p + oap->end.col);
2024 }
2025}
2026#endif
2027
2028#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2029/*
2030 * Replace a whole area with one character.
2031 */
2032 int
2033op_replace(oap, c)
2034 oparg_T *oap;
2035 int c;
2036{
2037 int n, numc;
2038#ifdef FEAT_MBYTE
2039 int num_chars;
2040#endif
2041 char_u *newp, *oldp;
2042 size_t oldlen;
2043 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002044 char_u *after_p = NULL;
2045 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2048 return OK; /* nothing to do */
2049
Bram Moolenaard9820532013-11-04 01:41:17 +01002050 if (had_ctrl_v_cr)
2051 c = (c == -1 ? '\r' : '\n');
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053#ifdef FEAT_MBYTE
2054 if (has_mbyte)
2055 mb_adjust_opend(oap);
2056#endif
2057
2058 if (u_save((linenr_T)(oap->start.lnum - 1),
2059 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2060 return FAIL;
2061
2062 /*
2063 * block mode replace
2064 */
2065 if (oap->block_mode)
2066 {
2067 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2068 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2069 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002070 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2072 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2073 continue; /* nothing to replace */
2074
2075 /* n == number of extra chars required
2076 * If we split a TAB, it may be replaced by several characters.
2077 * Thus the number of characters may increase!
2078 */
2079#ifdef FEAT_VIRTUALEDIT
2080 /* If the range starts in virtual space, count the initial
2081 * coladd offset as part of "startspaces" */
2082 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2083 {
2084 pos_T vpos;
2085
Bram Moolenaara1381de2009-11-03 15:44:21 +00002086 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 getvpos(&vpos, oap->start_vcol);
2088 bd.startspaces += vpos.coladd;
2089 n = bd.startspaces;
2090 }
2091 else
2092#endif
2093 /* allow for pre spaces */
2094 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2095
2096 /* allow for post spp */
2097 n += (bd.endspaces
2098#ifdef FEAT_VIRTUALEDIT
2099 && !bd.is_oneChar
2100#endif
2101 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2102 /* Figure out how many characters to replace. */
2103 numc = oap->end_vcol - oap->start_vcol + 1;
2104 if (bd.is_short && (!virtual_op || bd.is_MAX))
2105 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2106
2107#ifdef FEAT_MBYTE
2108 /* A double-wide character can be replaced only up to half the
2109 * times. */
2110 if ((*mb_char2cells)(c) > 1)
2111 {
2112 if ((numc & 1) && !bd.is_short)
2113 {
2114 ++bd.endspaces;
2115 ++n;
2116 }
2117 numc = numc / 2;
2118 }
2119
2120 /* Compute bytes needed, move character count to num_chars. */
2121 num_chars = numc;
2122 numc *= (*mb_char2len)(c);
2123#endif
2124 /* oldlen includes textlen, so don't double count */
2125 n += numc - bd.textlen;
2126
2127 oldp = ml_get_curline();
2128 oldlen = STRLEN(oldp);
2129 newp = alloc_check((unsigned)oldlen + 1 + n);
2130 if (newp == NULL)
2131 continue;
2132 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2133 /* copy up to deleted part */
2134 mch_memmove(newp, oldp, (size_t)bd.textcol);
2135 oldp += bd.textcol + bd.textlen;
2136 /* insert pre-spaces */
2137 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2138 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002139 /* -1/-2 is used for entering CR literally. */
2140 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002142#ifdef FEAT_MBYTE
2143 if (has_mbyte)
2144 {
2145 n = (int)STRLEN(newp);
2146 while (--num_chars >= 0)
2147 n += (*mb_char2bytes)(c, newp + n);
2148 }
2149 else
2150#endif
2151 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2152 if (!bd.is_short)
2153 {
2154 /* insert post-spaces */
2155 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2156 /* copy the part after the changed part */
2157 STRMOVE(newp + STRLEN(newp), oldp);
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
2160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002162 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002163 after_p = alloc_check(
2164 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002165 if (after_p != NULL)
2166 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
2168 /* replace the line */
2169 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002170 if (after_p != NULL)
2171 {
2172 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2173 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2174 oap->end.lnum++;
2175 vim_free(after_p);
2176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178 }
2179 else
2180 {
2181 /*
2182 * MCHAR and MLINE motion replace.
2183 */
2184 if (oap->motion_type == MLINE)
2185 {
2186 oap->start.col = 0;
2187 curwin->w_cursor.col = 0;
2188 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2189 if (oap->end.col)
2190 --oap->end.col;
2191 }
2192 else if (!oap->inclusive)
2193 dec(&(oap->end));
2194
2195 while (ltoreq(curwin->w_cursor, oap->end))
2196 {
2197 n = gchar_cursor();
2198 if (n != NUL)
2199 {
2200#ifdef FEAT_MBYTE
2201 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2202 {
2203 /* This is slow, but it handles replacing a single-byte
2204 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002205 if (curwin->w_cursor.lnum == oap->end.lnum)
2206 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 n = State;
2208 State = REPLACE;
2209 ins_char(c);
2210 State = n;
2211 /* Backup to the replaced character. */
2212 dec_cursor();
2213 }
2214 else
2215#endif
2216 {
2217#ifdef FEAT_VIRTUALEDIT
2218 if (n == TAB)
2219 {
2220 int end_vcol = 0;
2221
2222 if (curwin->w_cursor.lnum == oap->end.lnum)
2223 {
2224 /* oap->end has to be recalculated when
2225 * the tab breaks */
2226 end_vcol = getviscol2(oap->end.col,
2227 oap->end.coladd);
2228 }
2229 coladvance_force(getviscol());
2230 if (curwin->w_cursor.lnum == oap->end.lnum)
2231 getvpos(&oap->end, end_vcol);
2232 }
2233#endif
2234 pchar(curwin->w_cursor, c);
2235 }
2236 }
2237#ifdef FEAT_VIRTUALEDIT
2238 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2239 {
2240 int virtcols = oap->end.coladd;
2241
2242 if (curwin->w_cursor.lnum == oap->start.lnum
2243 && oap->start.col == oap->end.col && oap->start.coladd)
2244 virtcols -= oap->start.coladd;
2245
2246 /* oap->end has been trimmed so it's effectively inclusive;
2247 * as a result an extra +1 must be counted so we don't
2248 * trample the NUL byte. */
2249 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2250 curwin->w_cursor.col -= (virtcols + 1);
2251 for (; virtcols >= 0; virtcols--)
2252 {
2253 pchar(curwin->w_cursor, c);
2254 if (inc(&curwin->w_cursor) == -1)
2255 break;
2256 }
2257 }
2258#endif
2259
2260 /* Advance to next character, stop at the end of the file. */
2261 if (inc_cursor() == -1)
2262 break;
2263 }
2264 }
2265
2266 curwin->w_cursor = oap->start;
2267 check_cursor();
2268 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2269
2270 /* Set "'[" and "']" marks. */
2271 curbuf->b_op_start = oap->start;
2272 curbuf->b_op_end = oap->end;
2273
2274 return OK;
2275}
2276#endif
2277
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002278static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280/*
2281 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2282 */
2283 void
2284op_tilde(oap)
2285 oparg_T *oap;
2286{
2287 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002289 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 if (u_save((linenr_T)(oap->start.lnum - 1),
2292 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2293 return;
2294
2295 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (oap->block_mode) /* Visual block mode */
2297 {
2298 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2299 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002300 int one_change;
2301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 block_prep(oap, &bd, pos.lnum, FALSE);
2303 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002304 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2305 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002306
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002307#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002308 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2311
Bram Moolenaar009b2592004-10-24 19:18:58 +00002312 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2313 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002315 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319 if (did_change)
2320 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2321 }
2322 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 if (oap->motion_type == MLINE)
2325 {
2326 oap->start.col = 0;
2327 pos.col = 0;
2328 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2329 if (oap->end.col)
2330 --oap->end.col;
2331 }
2332 else if (!oap->inclusive)
2333 dec(&(oap->end));
2334
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002335 if (pos.lnum == oap->end.lnum)
2336 did_change = swapchars(oap->op_type, &pos,
2337 oap->end.col - pos.col + 1);
2338 else
2339 for (;;)
2340 {
2341 did_change |= swapchars(oap->op_type, &pos,
2342 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2343 (int)STRLEN(ml_get_pos(&pos)));
2344 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2345 break;
2346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (did_change)
2348 {
2349 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2350 0L);
2351#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002352 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 char_u *ptr;
2355 int count;
2356
2357 pos = oap->start;
2358 while (pos.lnum < oap->end.lnum)
2359 {
2360 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002361 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002362 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002364 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 pos.col = 0;
2366 pos.lnum++;
2367 }
2368 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2369 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002370 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002372 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374#endif
2375 }
2376 }
2377
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (!did_change && oap->is_VIsual)
2379 /* No change: need to remove the Visual selection */
2380 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382 /*
2383 * Set '[ and '] marks.
2384 */
2385 curbuf->b_op_start = oap->start;
2386 curbuf->b_op_end = oap->end;
2387
2388 if (oap->line_count > p_report)
2389 {
2390 if (oap->line_count == 1)
2391 MSG(_("1 line changed"));
2392 else
2393 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2394 }
2395}
2396
2397/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002398 * Invoke swapchar() on "length" bytes at position "pos".
2399 * "pos" is advanced to just after the changed characters.
2400 * "length" is rounded up to include the whole last multi-byte character.
2401 * Also works correctly when the number of bytes changes.
2402 * Returns TRUE if some character was changed.
2403 */
2404 static int
2405swapchars(op_type, pos, length)
2406 int op_type;
2407 pos_T *pos;
2408 int length;
2409{
2410 int todo;
2411 int did_change = 0;
2412
2413 for (todo = length; todo > 0; --todo)
2414 {
2415# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002416 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002417 {
2418 int len = (*mb_ptr2len)(ml_get_pos(pos));
2419
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002420 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002421 if (len > 0)
2422 todo -= len - 1;
2423 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002424# endif
2425 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002426 if (inc(pos) == -1) /* at end of file */
2427 break;
2428 }
2429 return did_change;
2430}
2431
2432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 * If op_type == OP_UPPER: make uppercase,
2434 * if op_type == OP_LOWER: make lowercase,
2435 * if op_type == OP_ROT13: do rot13 encoding,
2436 * else swap case of character at 'pos'
2437 * returns TRUE when something actually changed.
2438 */
2439 int
2440swapchar(op_type, pos)
2441 int op_type;
2442 pos_T *pos;
2443{
2444 int c;
2445 int nc;
2446
2447 c = gchar_pos(pos);
2448
2449 /* Only do rot13 encoding for ASCII characters. */
2450 if (c >= 0x80 && op_type == OP_ROT13)
2451 return FALSE;
2452
2453#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002454 if (op_type == OP_UPPER && c == 0xdf
2455 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002456 {
2457 pos_T sp = curwin->w_cursor;
2458
2459 /* Special handling of German sharp s: change to "SS". */
2460 curwin->w_cursor = *pos;
2461 del_char(FALSE);
2462 ins_char('S');
2463 ins_char('S');
2464 curwin->w_cursor = sp;
2465 inc(pos);
2466 }
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2469 return FALSE;
2470#endif
2471 nc = c;
2472 if (MB_ISLOWER(c))
2473 {
2474 if (op_type == OP_ROT13)
2475 nc = ROT13(c, 'a');
2476 else if (op_type != OP_LOWER)
2477 nc = MB_TOUPPER(c);
2478 }
2479 else if (MB_ISUPPER(c))
2480 {
2481 if (op_type == OP_ROT13)
2482 nc = ROT13(c, 'A');
2483 else if (op_type != OP_UPPER)
2484 nc = MB_TOLOWER(c);
2485 }
2486 if (nc != c)
2487 {
2488#ifdef FEAT_MBYTE
2489 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2490 {
2491 pos_T sp = curwin->w_cursor;
2492
2493 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002494 /* don't use del_char(), it also removes composing chars */
2495 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 ins_char(nc);
2497 curwin->w_cursor = sp;
2498 }
2499 else
2500#endif
2501 pchar(*pos, nc);
2502 return TRUE;
2503 }
2504 return FALSE;
2505}
2506
2507#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2508/*
2509 * op_insert - Insert and append operators for Visual mode.
2510 */
2511 void
2512op_insert(oap, count1)
2513 oparg_T *oap;
2514 long count1;
2515{
2516 long ins_len, pre_textlen = 0;
2517 char_u *firstline, *ins_text;
2518 struct block_def bd;
2519 int i;
2520
2521 /* edit() changes this - record it for OP_APPEND */
2522 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2523
2524 /* vis block is still marked. Get rid of it now. */
2525 curwin->w_cursor.lnum = oap->start.lnum;
2526 update_screen(INVERTED);
2527
2528 if (oap->block_mode)
2529 {
2530#ifdef FEAT_VIRTUALEDIT
2531 /* When 'virtualedit' is used, need to insert the extra spaces before
2532 * doing block_prep(). When only "block" is used, virtual edit is
2533 * already disabled, but still need it when calling
2534 * coladvance_force(). */
2535 if (curwin->w_cursor.coladd > 0)
2536 {
2537 int old_ve_flags = ve_flags;
2538
2539 ve_flags = VE_ALL;
2540 if (u_save_cursor() == FAIL)
2541 return;
2542 coladvance_force(oap->op_type == OP_APPEND
2543 ? oap->end_vcol + 1 : getviscol());
2544 if (oap->op_type == OP_APPEND)
2545 --curwin->w_cursor.col;
2546 ve_flags = old_ve_flags;
2547 }
2548#endif
2549 /* Get the info about the block before entering the text */
2550 block_prep(oap, &bd, oap->start.lnum, TRUE);
2551 firstline = ml_get(oap->start.lnum) + bd.textcol;
2552 if (oap->op_type == OP_APPEND)
2553 firstline += bd.textlen;
2554 pre_textlen = (long)STRLEN(firstline);
2555 }
2556
2557 if (oap->op_type == OP_APPEND)
2558 {
2559 if (oap->block_mode
2560#ifdef FEAT_VIRTUALEDIT
2561 && curwin->w_cursor.coladd == 0
2562#endif
2563 )
2564 {
2565 /* Move the cursor to the character right of the block. */
2566 curwin->w_set_curswant = TRUE;
2567 while (*ml_get_cursor() != NUL
2568 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2569 ++curwin->w_cursor.col;
2570 if (bd.is_short && !bd.is_MAX)
2571 {
2572 /* First line was too short, make it longer and adjust the
2573 * values in "bd". */
2574 if (u_save_cursor() == FAIL)
2575 return;
2576 for (i = 0; i < bd.endspaces; ++i)
2577 ins_char(' ');
2578 bd.textlen += bd.endspaces;
2579 }
2580 }
2581 else
2582 {
2583 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002584 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586 /* Works just like an 'i'nsert on the next character. */
2587 if (!lineempty(curwin->w_cursor.lnum)
2588 && oap->start_vcol != oap->end_vcol)
2589 inc_cursor();
2590 }
2591 }
2592
2593 edit(NUL, FALSE, (linenr_T)count1);
2594
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002595 /* If user has moved off this line, we don't know what to do, so do
2596 * nothing.
2597 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2598 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 return;
2600
2601 if (oap->block_mode)
2602 {
2603 struct block_def bd2;
2604
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002605 /* The user may have moved the cursor before inserting something, try
2606 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002607 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002608 {
2609 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002610 && oap->start.col
2611#ifdef FEAT_VIRTUALEDIT
2612 + oap->start.coladd
2613#endif
2614 != curbuf->b_op_start_orig.col
2615#ifdef FEAT_VIRTUALEDIT
2616 + curbuf->b_op_start_orig.coladd
2617#endif
2618 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002619 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002620 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002621 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2622 - oap->start_vcol;
2623 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2624 }
2625 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002626 && oap->end.col
2627#ifdef FEAT_VIRTUALEDIT
2628 + oap->end.coladd
2629#endif
2630 >= curbuf->b_op_start_orig.col
2631#ifdef FEAT_VIRTUALEDIT
2632 + curbuf->b_op_start_orig.coladd
2633#endif
2634 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002635 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002636 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002637 /* reset pre_textlen to the value of OP_INSERT */
2638 pre_textlen += bd.textlen;
2639 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2640 - oap->start_vcol;
2641 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2642 oap->op_type = OP_INSERT;
2643 }
2644 }
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 /*
2647 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002648 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 * Don't do this when "$" used, end-of-line will have changed.
2650 */
2651 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2652 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2653 {
2654 if (oap->op_type == OP_APPEND)
2655 {
2656 pre_textlen += bd2.textlen - bd.textlen;
2657 if (bd2.endspaces)
2658 --bd2.textlen;
2659 }
2660 bd.textcol = bd2.textcol;
2661 bd.textlen = bd2.textlen;
2662 }
2663
2664 /*
2665 * Subsequent calls to ml_get() flush the firstline data - take a
2666 * copy of the required string.
2667 */
2668 firstline = ml_get(oap->start.lnum) + bd.textcol;
2669 if (oap->op_type == OP_APPEND)
2670 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002671 if (pre_textlen >= 0
2672 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 ins_text = vim_strnsave(firstline, (int)ins_len);
2675 if (ins_text != NULL)
2676 {
2677 /* block handled here */
2678 if (u_save(oap->start.lnum,
2679 (linenr_T)(oap->end.lnum + 1)) == OK)
2680 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2681 &bd);
2682
2683 curwin->w_cursor.col = oap->start.col;
2684 check_cursor();
2685 vim_free(ins_text);
2686 }
2687 }
2688 }
2689}
2690#endif
2691
2692/*
2693 * op_change - handle a change operation
2694 *
2695 * return TRUE if edit() returns because of a CTRL-O command
2696 */
2697 int
2698op_change(oap)
2699 oparg_T *oap;
2700{
2701 colnr_T l;
2702 int retval;
2703#ifdef FEAT_VISUALEXTRA
2704 long offset;
2705 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002706 long ins_len;
2707 long pre_textlen = 0;
2708 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 char_u *firstline;
2710 char_u *ins_text, *newp, *oldp;
2711 struct block_def bd;
2712#endif
2713
2714 l = oap->start.col;
2715 if (oap->motion_type == MLINE)
2716 {
2717 l = 0;
2718#ifdef FEAT_SMARTINDENT
2719 if (!p_paste && curbuf->b_p_si
2720# ifdef FEAT_CINDENT
2721 && !curbuf->b_p_cin
2722# endif
2723 )
2724 can_si = TRUE; /* It's like opening a new line, do si */
2725#endif
2726 }
2727
2728 /* First delete the text in the region. In an empty buffer only need to
2729 * save for undo */
2730 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2731 {
2732 if (u_save_cursor() == FAIL)
2733 return FALSE;
2734 }
2735 else if (op_delete(oap) == FAIL)
2736 return FALSE;
2737
2738 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2739 && !virtual_op)
2740 inc_cursor();
2741
2742#ifdef FEAT_VISUALEXTRA
2743 /* check for still on same line (<CR> in inserted text meaningless) */
2744 /* skip blank lines too */
2745 if (oap->block_mode)
2746 {
2747# ifdef FEAT_VIRTUALEDIT
2748 /* Add spaces before getting the current line length. */
2749 if (virtual_op && (curwin->w_cursor.coladd > 0
2750 || gchar_cursor() == NUL))
2751 coladvance_force(getviscol());
2752# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002753 firstline = ml_get(oap->start.lnum);
2754 pre_textlen = (long)STRLEN(firstline);
2755 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 bd.textcol = curwin->w_cursor.col;
2757 }
2758#endif
2759
2760#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2761 if (oap->motion_type == MLINE)
2762 fix_indent();
2763#endif
2764
2765 retval = edit(NUL, FALSE, (linenr_T)1);
2766
2767#ifdef FEAT_VISUALEXTRA
2768 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002769 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002771 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002773 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002775 /* Auto-indenting may have changed the indent. If the cursor was past
2776 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002778 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002780 long new_indent = (long)(skipwhite(firstline) - firstline);
2781
2782 pre_textlen += new_indent - pre_indent;
2783 bd.textcol += new_indent - pre_indent;
2784 }
2785
2786 ins_len = (long)STRLEN(firstline) - pre_textlen;
2787 if (ins_len > 0)
2788 {
2789 /* Subsequent calls to ml_get() flush the firstline data - take a
2790 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2792 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002793 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2795 linenr++)
2796 {
2797 block_prep(oap, &bd, linenr, TRUE);
2798 if (!bd.is_short || virtual_op)
2799 {
2800# ifdef FEAT_VIRTUALEDIT
2801 pos_T vpos;
2802
2803 /* If the block starts in virtual space, count the
2804 * initial coladd offset as part of "startspaces" */
2805 if (bd.is_short)
2806 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002807 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810 else
2811 vpos.coladd = 0;
2812# endif
2813 oldp = ml_get(linenr);
2814 newp = alloc_check((unsigned)(STRLEN(oldp)
2815# ifdef FEAT_VIRTUALEDIT
2816 + vpos.coladd
2817# endif
2818 + ins_len + 1));
2819 if (newp == NULL)
2820 continue;
2821 /* copy up to block start */
2822 mch_memmove(newp, oldp, (size_t)bd.textcol);
2823 offset = bd.textcol;
2824# ifdef FEAT_VIRTUALEDIT
2825 copy_spaces(newp + offset, (size_t)vpos.coladd);
2826 offset += vpos.coladd;
2827# endif
2828 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2829 offset += ins_len;
2830 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002831 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 ml_replace(linenr, newp, FALSE);
2833 }
2834 }
2835 check_cursor();
2836
2837 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2838 }
2839 vim_free(ins_text);
2840 }
2841 }
2842#endif
2843
2844 return retval;
2845}
2846
2847/*
2848 * set all the yank registers to empty (called from main())
2849 */
2850 void
2851init_yank()
2852{
2853 int i;
2854
2855 for (i = 0; i < NUM_REGISTERS; ++i)
2856 y_regs[i].y_array = NULL;
2857}
2858
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002859#if defined(EXITFREE) || defined(PROTO)
2860 void
2861clear_registers()
2862{
2863 int i;
2864
2865 for (i = 0; i < NUM_REGISTERS; ++i)
2866 {
2867 y_current = &y_regs[i];
2868 if (y_current->y_array != NULL)
2869 free_yank_all();
2870 }
2871}
2872#endif
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874/*
2875 * Free "n" lines from the current yank register.
2876 * Called for normal freeing and in case of error.
2877 */
2878 static void
2879free_yank(n)
2880 long n;
2881{
2882 if (y_current->y_array != NULL)
2883 {
2884 long i;
2885
2886 for (i = n; --i >= 0; )
2887 {
2888#ifdef AMIGA /* only for very slow machines */
2889 if ((i & 1023) == 1023) /* this may take a while */
2890 {
2891 /*
2892 * This message should never cause a hit-return message.
2893 * Overwrite this message with any next message.
2894 */
2895 ++no_wait_return;
2896 smsg((char_u *)_("freeing %ld lines"), i + 1);
2897 --no_wait_return;
2898 msg_didout = FALSE;
2899 msg_col = 0;
2900 }
2901#endif
2902 vim_free(y_current->y_array[i]);
2903 }
2904 vim_free(y_current->y_array);
2905 y_current->y_array = NULL;
2906#ifdef AMIGA
2907 if (n >= 1000)
2908 MSG("");
2909#endif
2910 }
2911}
2912
2913 static void
2914free_yank_all()
2915{
2916 free_yank(y_current->y_size);
2917}
2918
2919/*
2920 * Yank the text between "oap->start" and "oap->end" into a yank register.
2921 * If we are to append (uppercase register), we first yank into a new yank
2922 * register and then concatenate the old and the new one (so we keep the old
2923 * one in case of out-of-memory).
2924 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002925 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
2927 int
2928op_yank(oap, deleting, mess)
2929 oparg_T *oap;
2930 int deleting;
2931 int mess;
2932{
2933 long y_idx; /* index in y_array[] */
2934 struct yankreg *curr; /* copy of y_current */
2935 struct yankreg newreg; /* new yank register when appending */
2936 char_u **new_ptr;
2937 linenr_T lnum; /* current line number */
2938 long j;
2939 int yanktype = oap->motion_type;
2940 long yanklines = oap->line_count;
2941 linenr_T yankendlnum = oap->end.lnum;
2942 char_u *p;
2943 char_u *pnew;
2944 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002945#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002946 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /* check for read-only register */
2950 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2951 {
2952 beep_flush();
2953 return FAIL;
2954 }
2955 if (oap->regname == '_') /* black hole: nothing to do */
2956 return OK;
2957
2958#ifdef FEAT_CLIPBOARD
2959 if (!clip_star.available && oap->regname == '*')
2960 oap->regname = 0;
2961 else if (!clip_plus.available && oap->regname == '+')
2962 oap->regname = 0;
2963#endif
2964
2965 if (!deleting) /* op_delete() already set y_current */
2966 get_yank_register(oap->regname, TRUE);
2967
2968 curr = y_current;
2969 /* append to existing contents */
2970 if (y_append && y_current->y_array != NULL)
2971 y_current = &newreg;
2972 else
2973 free_yank_all(); /* free previously yanked lines */
2974
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002975 /*
2976 * If the cursor was in column 1 before and after the movement, and the
2977 * operator is not inclusive, the yank is always linewise.
2978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if ( oap->motion_type == MCHAR
2980 && oap->start.col == 0
2981 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002983 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 && oap->end.col == 0
2985 && yanklines > 1)
2986 {
2987 yanktype = MLINE;
2988 --yankendlnum;
2989 --yanklines;
2990 }
2991
2992 y_current->y_size = yanklines;
2993 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2996 yanklines), TRUE);
2997
2998 if (y_current->y_array == NULL)
2999 {
3000 y_current = curr;
3001 return FAIL;
3002 }
3003
3004 y_idx = 0;
3005 lnum = oap->start.lnum;
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (oap->block_mode)
3008 {
3009 /* Visual block mode */
3010 y_current->y_type = MBLOCK; /* set the yank register type */
3011 y_current->y_width = oap->end_vcol - oap->start_vcol;
3012
3013 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3014 y_current->y_width--;
3015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
3017 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3018 {
3019 switch (y_current->y_type)
3020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 case MBLOCK:
3022 block_prep(oap, &bd, lnum, FALSE);
3023 if (yank_copy_line(&bd, y_idx) == FAIL)
3024 goto fail;
3025 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027 case MLINE:
3028 if ((y_current->y_array[y_idx] =
3029 vim_strsave(ml_get(lnum))) == NULL)
3030 goto fail;
3031 break;
3032
3033 case MCHAR:
3034 {
3035 colnr_T startcol = 0, endcol = MAXCOL;
3036#ifdef FEAT_VIRTUALEDIT
3037 int is_oneChar = FALSE;
3038 colnr_T cs, ce;
3039#endif
3040 p = ml_get(lnum);
3041 bd.startspaces = 0;
3042 bd.endspaces = 0;
3043
3044 if (lnum == oap->start.lnum)
3045 {
3046 startcol = oap->start.col;
3047#ifdef FEAT_VIRTUALEDIT
3048 if (virtual_op)
3049 {
3050 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3051 if (ce != cs && oap->start.coladd > 0)
3052 {
3053 /* Part of a tab selected -- but don't
3054 * double-count it. */
3055 bd.startspaces = (ce - cs + 1)
3056 - oap->start.coladd;
3057 startcol++;
3058 }
3059 }
3060#endif
3061 }
3062
3063 if (lnum == oap->end.lnum)
3064 {
3065 endcol = oap->end.col;
3066#ifdef FEAT_VIRTUALEDIT
3067 if (virtual_op)
3068 {
3069 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3070 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3071# ifdef FEAT_MBYTE
3072 /* Don't add space for double-wide
3073 * char; endcol will be on last byte
3074 * of multi-byte char. */
3075 && (*mb_head_off)(p, p + endcol) == 0
3076# endif
3077 ))
3078 {
3079 if (oap->start.lnum == oap->end.lnum
3080 && oap->start.col == oap->end.col)
3081 {
3082 /* Special case: inside a single char */
3083 is_oneChar = TRUE;
3084 bd.startspaces = oap->end.coladd
3085 - oap->start.coladd + oap->inclusive;
3086 endcol = startcol;
3087 }
3088 else
3089 {
3090 bd.endspaces = oap->end.coladd
3091 + oap->inclusive;
3092 endcol -= oap->inclusive;
3093 }
3094 }
3095 }
3096#endif
3097 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003098 if (endcol == MAXCOL)
3099 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (startcol > endcol
3101#ifdef FEAT_VIRTUALEDIT
3102 || is_oneChar
3103#endif
3104 )
3105 bd.textlen = 0;
3106 else
3107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 bd.textlen = endcol - startcol + oap->inclusive;
3109 }
3110 bd.textstart = p + startcol;
3111 if (yank_copy_line(&bd, y_idx) == FAIL)
3112 goto fail;
3113 break;
3114 }
3115 /* NOTREACHED */
3116 }
3117 }
3118
3119 if (curr != y_current) /* append the new block to the old block */
3120 {
3121 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3122 (curr->y_size + y_current->y_size)), TRUE);
3123 if (new_ptr == NULL)
3124 goto fail;
3125 for (j = 0; j < curr->y_size; ++j)
3126 new_ptr[j] = curr->y_array[j];
3127 vim_free(curr->y_array);
3128 curr->y_array = new_ptr;
3129
3130 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3131 curr->y_type = MLINE;
3132
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003133 /* Concatenate the last line of the old block with the first line of
3134 * the new block, unless being Vi compatible. */
3135 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
3137 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3138 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3139 if (pnew == NULL)
3140 {
3141 y_idx = y_current->y_size - 1;
3142 goto fail;
3143 }
3144 STRCPY(pnew, curr->y_array[--j]);
3145 STRCAT(pnew, y_current->y_array[0]);
3146 vim_free(curr->y_array[j]);
3147 vim_free(y_current->y_array[0]);
3148 curr->y_array[j++] = pnew;
3149 y_idx = 1;
3150 }
3151 else
3152 y_idx = 0;
3153 while (y_idx < y_current->y_size)
3154 curr->y_array[j++] = y_current->y_array[y_idx++];
3155 curr->y_size = j;
3156 vim_free(y_current->y_array);
3157 y_current = curr;
3158 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003159 if (curwin->w_p_rnu)
3160 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (mess) /* Display message about yank? */
3162 {
3163 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 && yanklines == 1)
3166 yanklines = 0;
3167 /* Some versions of Vi use ">=" here, some don't... */
3168 if (yanklines > p_report)
3169 {
3170 /* redisplay now, so message is not deleted */
3171 update_topline_redraw();
3172 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003173 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003174 if (oap->block_mode)
3175 MSG(_("block of 1 line yanked"));
3176 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003177 MSG(_("1 line yanked"));
3178 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003179 else if (oap->block_mode)
3180 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 else
3182 smsg((char_u *)_("%ld lines yanked"), yanklines);
3183 }
3184 }
3185
3186 /*
3187 * Set "'[" and "']" marks.
3188 */
3189 curbuf->b_op_start = oap->start;
3190 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003191 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003192 {
3193 curbuf->b_op_start.col = 0;
3194 curbuf->b_op_end.col = MAXCOL;
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197#ifdef FEAT_CLIPBOARD
3198 /*
3199 * If we were yanking to the '*' register, send result to clipboard.
3200 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3201 * to the '*' register.
3202 */
3203 if (clip_star.available
3204 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003205 || (!deleting && oap->regname == 0
3206 && (clip_unnamed & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 if (curr != &(y_regs[STAR_REGISTER]))
3209 /* Copy the text from register 0 to the clipboard register. */
3210 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3211
3212 clip_own_selection(&clip_star);
3213 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003214# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003215 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218
3219# ifdef FEAT_X11
3220 /*
3221 * If we were yanking to the '+' register, send result to selection.
3222 * Also copy to the '*' register, in case auto-select is off.
3223 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003224 if (clip_plus.available
3225 && (curr == &(y_regs[PLUS_REGISTER])
3226 || (!deleting && oap->regname == 0
3227 && (clip_unnamed & CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003229 if (curr != &(y_regs[PLUS_REGISTER]))
3230 /* Copy the text from register 0 to the clipboard register. */
3231 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 clip_own_selection(&clip_plus);
3234 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003235 if (!clip_isautosel_star() && !did_star
3236 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3239 clip_own_selection(&clip_star);
3240 clip_gen_set_selection(&clip_star);
3241 }
3242 }
3243# endif
3244#endif
3245
3246 return OK;
3247
3248fail: /* free the allocated lines */
3249 free_yank(y_idx + 1);
3250 y_current = curr;
3251 return FAIL;
3252}
3253
3254 static int
3255yank_copy_line(bd, y_idx)
3256 struct block_def *bd;
3257 long y_idx;
3258{
3259 char_u *pnew;
3260
3261 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3262 == NULL)
3263 return FAIL;
3264 y_current->y_array[y_idx] = pnew;
3265 copy_spaces(pnew, (size_t)bd->startspaces);
3266 pnew += bd->startspaces;
3267 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3268 pnew += bd->textlen;
3269 copy_spaces(pnew, (size_t)bd->endspaces);
3270 pnew += bd->endspaces;
3271 *pnew = NUL;
3272 return OK;
3273}
3274
3275#ifdef FEAT_CLIPBOARD
3276/*
3277 * Make a copy of the y_current register to register "reg".
3278 */
3279 static void
3280copy_yank_reg(reg)
3281 struct yankreg *reg;
3282{
3283 struct yankreg *curr = y_current;
3284 long j;
3285
3286 y_current = reg;
3287 free_yank_all();
3288 *y_current = *curr;
3289 y_current->y_array = (char_u **)lalloc_clear(
3290 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3291 if (y_current->y_array == NULL)
3292 y_current->y_size = 0;
3293 else
3294 for (j = 0; j < y_current->y_size; ++j)
3295 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3296 {
3297 free_yank(j);
3298 y_current->y_size = 0;
3299 break;
3300 }
3301 y_current = curr;
3302}
3303#endif
3304
3305/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003306 * Put contents of register "regname" into the text.
3307 * Caller must check "regname" to be valid!
3308 * "flags": PUT_FIXINDENT make indent look nice
3309 * PUT_CURSEND leave cursor after end of new text
3310 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 */
3312 void
3313do_put(regname, dir, count, flags)
3314 int regname;
3315 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3316 long count;
3317 int flags;
3318{
3319 char_u *ptr;
3320 char_u *newp, *oldp;
3321 int yanklen;
3322 int totlen = 0; /* init for gcc */
3323 linenr_T lnum;
3324 colnr_T col;
3325 long i; /* index in y_array[] */
3326 int y_type;
3327 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 int oldlen;
3329 long y_width = 0;
3330 colnr_T vcol;
3331 int delcount;
3332 int incr = 0;
3333 long j;
3334 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 char_u **y_array = NULL;
3336 long nr_lines = 0;
3337 pos_T new_cursor;
3338 int indent;
3339 int orig_indent = 0; /* init for gcc */
3340 int indent_diff = 0; /* init for gcc */
3341 int first_indent = TRUE;
3342 int lendiff = 0;
3343 pos_T old_pos;
3344 char_u *insert_string = NULL;
3345 int allocated = FALSE;
3346 long cnt;
3347
3348#ifdef FEAT_CLIPBOARD
3349 /* Adjust register name for "unnamed" in 'clipboard'. */
3350 adjust_clip_reg(&regname);
3351 (void)may_get_selection(regname);
3352#endif
3353
3354 if (flags & PUT_FIXINDENT)
3355 orig_indent = get_indent();
3356
3357 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3358 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3359
3360 /*
3361 * Using inserted text works differently, because the register includes
3362 * special characters (newlines, etc.).
3363 */
3364 if (regname == '.')
3365 {
3366 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3367 (count == -1 ? 'O' : 'i')), count, FALSE);
3368 /* Putting the text is done later, so can't really move the cursor to
3369 * the next character. Use "l" to simulate it. */
3370 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3371 stuffcharReadbuff('l');
3372 return;
3373 }
3374
3375 /*
3376 * For special registers '%' (file name), '#' (alternate file name) and
3377 * ':' (last command line), etc. we have to create a fake yank register.
3378 */
3379 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3380 {
3381 if (insert_string == NULL)
3382 return;
3383 }
3384
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003385#ifdef FEAT_AUTOCMD
3386 /* Autocommands may be executed when saving lines for undo, which may make
3387 * y_array invalid. Start undo now to avoid that. */
3388 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3389#endif
3390
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (insert_string != NULL)
3392 {
3393 y_type = MCHAR;
3394#ifdef FEAT_EVAL
3395 if (regname == '=')
3396 {
3397 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003398 * characters.
3399 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 for (;;)
3401 {
3402 y_size = 0;
3403 ptr = insert_string;
3404 while (ptr != NULL)
3405 {
3406 if (y_array != NULL)
3407 y_array[y_size] = ptr;
3408 ++y_size;
3409 ptr = vim_strchr(ptr, '\n');
3410 if (ptr != NULL)
3411 {
3412 if (y_array != NULL)
3413 *ptr = NUL;
3414 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003415 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (*ptr == NUL)
3417 {
3418 y_type = MLINE;
3419 break;
3420 }
3421 }
3422 }
3423 if (y_array != NULL)
3424 break;
3425 y_array = (char_u **)alloc((unsigned)
3426 (y_size * sizeof(char_u *)));
3427 if (y_array == NULL)
3428 goto end;
3429 }
3430 }
3431 else
3432#endif
3433 {
3434 y_size = 1; /* use fake one-line yank register */
3435 y_array = &insert_string;
3436 }
3437 }
3438 else
3439 {
3440 get_yank_register(regname, FALSE);
3441
3442 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 y_size = y_current->y_size;
3445 y_array = y_current->y_array;
3446 }
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (y_type == MLINE)
3449 {
3450 if (flags & PUT_LINE_SPLIT)
3451 {
3452 /* "p" or "P" in Visual mode: split the lines to put the text in
3453 * between. */
3454 if (u_save_cursor() == FAIL)
3455 goto end;
3456 ptr = vim_strsave(ml_get_cursor());
3457 if (ptr == NULL)
3458 goto end;
3459 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3460 vim_free(ptr);
3461
3462 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3463 if (ptr == NULL)
3464 goto end;
3465 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3466 ++nr_lines;
3467 dir = FORWARD;
3468 }
3469 if (flags & PUT_LINE_FORWARD)
3470 {
3471 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003472 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 dir = FORWARD;
3474 }
3475 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3476 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3480 y_type = MLINE;
3481
3482 if (y_size == 0 || y_array == NULL)
3483 {
3484 EMSG2(_("E353: Nothing in register %s"),
3485 regname == 0 ? (char_u *)"\"" : transchar(regname));
3486 goto end;
3487 }
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (y_type == MBLOCK)
3490 {
3491 lnum = curwin->w_cursor.lnum + y_size + 1;
3492 if (lnum > curbuf->b_ml.ml_line_count)
3493 lnum = curbuf->b_ml.ml_line_count + 1;
3494 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3495 goto end;
3496 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003497 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 lnum = curwin->w_cursor.lnum;
3500#ifdef FEAT_FOLDING
3501 /* Correct line number for closed fold. Don't move the cursor yet,
3502 * u_save() uses it. */
3503 if (dir == BACKWARD)
3504 (void)hasFolding(lnum, &lnum, NULL);
3505 else
3506 (void)hasFolding(lnum, NULL, &lnum);
3507#endif
3508 if (dir == FORWARD)
3509 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003510 /* In an empty buffer the empty line is going to be replaced, include
3511 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003512 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 goto end;
3514#ifdef FEAT_FOLDING
3515 if (dir == FORWARD)
3516 curwin->w_cursor.lnum = lnum - 1;
3517 else
3518 curwin->w_cursor.lnum = lnum;
3519 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3520#endif
3521 }
3522 else if (u_save_cursor() == FAIL)
3523 goto end;
3524
3525 yanklen = (int)STRLEN(y_array[0]);
3526
3527#ifdef FEAT_VIRTUALEDIT
3528 if (ve_flags == VE_ALL && y_type == MCHAR)
3529 {
3530 if (gchar_cursor() == TAB)
3531 {
3532 /* Don't need to insert spaces when "p" on the last position of a
3533 * tab or "P" on the first position. */
3534 if (dir == FORWARD
3535 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3536 : curwin->w_cursor.coladd > 0)
3537 coladvance_force(getviscol());
3538 else
3539 curwin->w_cursor.coladd = 0;
3540 }
3541 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3542 coladvance_force(getviscol() + (dir == FORWARD));
3543 }
3544#endif
3545
3546 lnum = curwin->w_cursor.lnum;
3547 col = curwin->w_cursor.col;
3548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 /*
3550 * Block mode
3551 */
3552 if (y_type == MBLOCK)
3553 {
3554 char c = gchar_cursor();
3555 colnr_T endcol2 = 0;
3556
3557 if (dir == FORWARD && c != NUL)
3558 {
3559#ifdef FEAT_VIRTUALEDIT
3560 if (ve_flags == VE_ALL)
3561 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3562 else
3563#endif
3564 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3565
3566#ifdef FEAT_MBYTE
3567 if (has_mbyte)
3568 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003569 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 else
3571#endif
3572#ifdef FEAT_VIRTUALEDIT
3573 if (c != TAB || ve_flags != VE_ALL)
3574#endif
3575 ++curwin->w_cursor.col;
3576 ++col;
3577 }
3578 else
3579 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3580
3581#ifdef FEAT_VIRTUALEDIT
3582 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003583 if (ve_flags == VE_ALL
3584 && (curwin->w_cursor.coladd > 0
3585 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 if (dir == FORWARD && c == NUL)
3588 ++col;
3589 if (dir != FORWARD && c != NUL)
3590 ++curwin->w_cursor.col;
3591 if (c == TAB)
3592 {
3593 if (dir == BACKWARD && curwin->w_cursor.col)
3594 curwin->w_cursor.col--;
3595 if (dir == FORWARD && col - 1 == endcol2)
3596 curwin->w_cursor.col++;
3597 }
3598 }
3599 curwin->w_cursor.coladd = 0;
3600#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003601 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 for (i = 0; i < y_size; ++i)
3603 {
3604 int spaces;
3605 char shortline;
3606
3607 bd.startspaces = 0;
3608 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 vcol = 0;
3610 delcount = 0;
3611
3612 /* add a new line */
3613 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3614 {
3615 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3616 (colnr_T)1, FALSE) == FAIL)
3617 break;
3618 ++nr_lines;
3619 }
3620 /* get the old line and advance to the position to insert at */
3621 oldp = ml_get_curline();
3622 oldlen = (int)STRLEN(oldp);
3623 for (ptr = oldp; vcol < col && *ptr; )
3624 {
3625 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003626 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 vcol += incr;
3628 }
3629 bd.textcol = (colnr_T)(ptr - oldp);
3630
3631 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3632
3633 if (vcol < col) /* line too short, padd with spaces */
3634 bd.startspaces = col - vcol;
3635 else if (vcol > col)
3636 {
3637 bd.endspaces = vcol - col;
3638 bd.startspaces = incr - bd.endspaces;
3639 --bd.textcol;
3640 delcount = 1;
3641#ifdef FEAT_MBYTE
3642 if (has_mbyte)
3643 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3644#endif
3645 if (oldp[bd.textcol] != TAB)
3646 {
3647 /* Only a Tab can be split into spaces. Other
3648 * characters will have to be moved to after the
3649 * block, causing misalignment. */
3650 delcount = 0;
3651 bd.endspaces = 0;
3652 }
3653 }
3654
3655 yanklen = (int)STRLEN(y_array[i]);
3656
3657 /* calculate number of spaces required to fill right side of block*/
3658 spaces = y_width + 1;
3659 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003660 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (spaces < 0)
3662 spaces = 0;
3663
3664 /* insert the new text */
3665 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3666 newp = alloc_check((unsigned)totlen + oldlen + 1);
3667 if (newp == NULL)
3668 break;
3669 /* copy part up to cursor to new line */
3670 ptr = newp;
3671 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3672 ptr += bd.textcol;
3673 /* may insert some spaces before the new text */
3674 copy_spaces(ptr, (size_t)bd.startspaces);
3675 ptr += bd.startspaces;
3676 /* insert the new text */
3677 for (j = 0; j < count; ++j)
3678 {
3679 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3680 ptr += yanklen;
3681
3682 /* insert block's trailing spaces only if there's text behind */
3683 if ((j < count - 1 || !shortline) && spaces)
3684 {
3685 copy_spaces(ptr, (size_t)spaces);
3686 ptr += spaces;
3687 }
3688 }
3689 /* may insert some spaces after the new text */
3690 copy_spaces(ptr, (size_t)bd.endspaces);
3691 ptr += bd.endspaces;
3692 /* move the text after the cursor to the end of the line. */
3693 mch_memmove(ptr, oldp + bd.textcol + delcount,
3694 (size_t)(oldlen - bd.textcol - delcount + 1));
3695 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3696
3697 ++curwin->w_cursor.lnum;
3698 if (i == 0)
3699 curwin->w_cursor.col += bd.startspaces;
3700 }
3701
3702 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3703
3704 /* Set '[ mark. */
3705 curbuf->b_op_start = curwin->w_cursor;
3706 curbuf->b_op_start.lnum = lnum;
3707
3708 /* adjust '] mark */
3709 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3710 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003711# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (flags & PUT_CURSEND)
3715 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003716 colnr_T len;
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 curwin->w_cursor = curbuf->b_op_end;
3719 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003720
3721 /* in Insert mode we might be after the NUL, correct for that */
3722 len = (colnr_T)STRLEN(ml_get_curline());
3723 if (curwin->w_cursor.col > len)
3724 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 else
3727 curwin->w_cursor.lnum = lnum;
3728 }
3729 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731 /*
3732 * Character or Line mode
3733 */
3734 if (y_type == MCHAR)
3735 {
3736 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3737 * char */
3738 if (dir == FORWARD && gchar_cursor() != NUL)
3739 {
3740#ifdef FEAT_MBYTE
3741 if (has_mbyte)
3742 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003743 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 /* put it on the next of the multi-byte character. */
3746 col += bytelen;
3747 if (yanklen)
3748 {
3749 curwin->w_cursor.col += bytelen;
3750 curbuf->b_op_end.col += bytelen;
3751 }
3752 }
3753 else
3754#endif
3755 {
3756 ++col;
3757 if (yanklen)
3758 {
3759 ++curwin->w_cursor.col;
3760 ++curbuf->b_op_end.col;
3761 }
3762 }
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 curbuf->b_op_start = curwin->w_cursor;
3765 }
3766 /*
3767 * Line mode: BACKWARD is the same as FORWARD on the previous line
3768 */
3769 else if (dir == BACKWARD)
3770 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003771 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * simple case: insert into current line
3775 */
3776 if (y_type == MCHAR && y_size == 1)
3777 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003778 do {
3779 totlen = count * yanklen;
3780 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003782 oldp = ml_get(lnum);
3783 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3784 if (newp == NULL)
3785 goto end; /* alloc() gave an error message */
3786 mch_memmove(newp, oldp, (size_t)col);
3787 ptr = newp + col;
3788 for (i = 0; i < count; ++i)
3789 {
3790 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3791 ptr += yanklen;
3792 }
3793 STRMOVE(ptr, oldp + col);
3794 ml_replace(lnum, newp, FALSE);
3795 /* Place cursor on last putted char. */
3796 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003797 {
3798 /* make sure curwin->w_virtcol is updated */
3799 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003800 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003803 if (VIsual_active)
3804 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003805 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 curbuf->b_op_end = curwin->w_cursor;
3808 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3809 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3810 ++curwin->w_cursor.col;
3811 changed_bytes(lnum, col);
3812 }
3813 else
3814 {
3815 /*
3816 * Insert at least one line. When y_type is MCHAR, break the first
3817 * line in two.
3818 */
3819 for (cnt = 1; cnt <= count; ++cnt)
3820 {
3821 i = 0;
3822 if (y_type == MCHAR)
3823 {
3824 /*
3825 * Split the current line in two at the insert position.
3826 * First insert y_array[size - 1] in front of second line.
3827 * Then append y_array[0] to first line.
3828 */
3829 lnum = new_cursor.lnum;
3830 ptr = ml_get(lnum) + col;
3831 totlen = (int)STRLEN(y_array[y_size - 1]);
3832 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3833 if (newp == NULL)
3834 goto error;
3835 STRCPY(newp, y_array[y_size - 1]);
3836 STRCAT(newp, ptr);
3837 /* insert second line */
3838 ml_append(lnum, newp, (colnr_T)0, FALSE);
3839 vim_free(newp);
3840
3841 oldp = ml_get(lnum);
3842 newp = alloc_check((unsigned)(col + yanklen + 1));
3843 if (newp == NULL)
3844 goto error;
3845 /* copy first part of line */
3846 mch_memmove(newp, oldp, (size_t)col);
3847 /* append to first line */
3848 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3849 ml_replace(lnum, newp, FALSE);
3850
3851 curwin->w_cursor.lnum = lnum;
3852 i = 1;
3853 }
3854
3855 for (; i < y_size; ++i)
3856 {
3857 if ((y_type != MCHAR || i < y_size - 1)
3858 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3859 == FAIL)
3860 goto error;
3861 lnum++;
3862 ++nr_lines;
3863 if (flags & PUT_FIXINDENT)
3864 {
3865 old_pos = curwin->w_cursor;
3866 curwin->w_cursor.lnum = lnum;
3867 ptr = ml_get(lnum);
3868 if (cnt == count && i == y_size - 1)
3869 lendiff = (int)STRLEN(ptr);
3870#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3871 if (*ptr == '#' && preprocs_left())
3872 indent = 0; /* Leave # lines at start */
3873 else
3874#endif
3875 if (*ptr == NUL)
3876 indent = 0; /* Ignore empty lines */
3877 else if (first_indent)
3878 {
3879 indent_diff = orig_indent - get_indent();
3880 indent = orig_indent;
3881 first_indent = FALSE;
3882 }
3883 else if ((indent = get_indent() + indent_diff) < 0)
3884 indent = 0;
3885 (void)set_indent(indent, 0);
3886 curwin->w_cursor = old_pos;
3887 /* remember how many chars were removed */
3888 if (cnt == count && i == y_size - 1)
3889 lendiff -= (int)STRLEN(ml_get(lnum));
3890 }
3891 }
3892 }
3893
3894error:
3895 /* Adjust marks. */
3896 if (y_type == MLINE)
3897 {
3898 curbuf->b_op_start.col = 0;
3899 if (dir == FORWARD)
3900 curbuf->b_op_start.lnum++;
3901 }
3902 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3903 (linenr_T)MAXLNUM, nr_lines, 0L);
3904
3905 /* note changed text for displaying and folding */
3906 if (y_type == MCHAR)
3907 changed_lines(curwin->w_cursor.lnum, col,
3908 curwin->w_cursor.lnum + 1, nr_lines);
3909 else
3910 changed_lines(curbuf->b_op_start.lnum, 0,
3911 curbuf->b_op_start.lnum, nr_lines);
3912
3913 /* put '] mark at last inserted character */
3914 curbuf->b_op_end.lnum = lnum;
3915 /* correct length for change in indent */
3916 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3917 if (col > 1)
3918 curbuf->b_op_end.col = col - 1;
3919 else
3920 curbuf->b_op_end.col = 0;
3921
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003922 if (flags & PUT_CURSLINE)
3923 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003924 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003925 curwin->w_cursor.lnum = lnum;
3926 beginline(BL_WHITE | BL_FIX);
3927 }
3928 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 /* put cursor after inserted text */
3931 if (y_type == MLINE)
3932 {
3933 if (lnum >= curbuf->b_ml.ml_line_count)
3934 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3935 else
3936 curwin->w_cursor.lnum = lnum + 1;
3937 curwin->w_cursor.col = 0;
3938 }
3939 else
3940 {
3941 curwin->w_cursor.lnum = lnum;
3942 curwin->w_cursor.col = col;
3943 }
3944 }
3945 else if (y_type == MLINE)
3946 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003947 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 curwin->w_cursor.col = 0;
3949 if (dir == FORWARD)
3950 ++curwin->w_cursor.lnum;
3951 beginline(BL_WHITE | BL_FIX);
3952 }
3953 else /* put cursor on first inserted character */
3954 curwin->w_cursor = new_cursor;
3955 }
3956 }
3957
3958 msgmore(nr_lines);
3959 curwin->w_set_curswant = TRUE;
3960
3961end:
3962 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003964 if (regname == '=')
3965 vim_free(y_array);
3966
Bram Moolenaar033d8882013-09-25 23:24:57 +02003967 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003968
Bram Moolenaar677ee682005-01-27 14:41:15 +00003969 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003970 adjust_cursor_eol();
3971}
3972
3973/*
3974 * When the cursor is on the NUL past the end of the line and it should not be
3975 * there move it left.
3976 */
3977 void
3978adjust_cursor_eol()
3979{
3980 if (curwin->w_cursor.col > 0
3981 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003982#ifdef FEAT_VIRTUALEDIT
3983 && (ve_flags & VE_ONEMORE) == 0
3984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 && !(restart_edit || (State & INSERT)))
3986 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003987 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003988 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990#ifdef FEAT_VIRTUALEDIT
3991 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003992 {
3993 colnr_T scol, ecol;
3994
3995 /* Coladd is set to the width of the last character. */
3996 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3997 curwin->w_cursor.coladd = ecol - scol + 1;
3998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#endif
4000 }
4001}
4002
4003#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4004/*
4005 * Return TRUE if lines starting with '#' should be left aligned.
4006 */
4007 int
4008preprocs_left()
4009{
4010 return
4011# ifdef FEAT_SMARTINDENT
4012# ifdef FEAT_CINDENT
4013 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4014# else
4015 curbuf->b_p_si
4016# endif
4017# endif
4018# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004019 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4020 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021# endif
4022 ;
4023}
4024#endif
4025
4026/* Return the character name of the register with the given number */
4027 int
4028get_register_name(num)
4029 int num;
4030{
4031 if (num == -1)
4032 return '"';
4033 else if (num < 10)
4034 return num + '0';
4035 else if (num == DELETION_REGISTER)
4036 return '-';
4037#ifdef FEAT_CLIPBOARD
4038 else if (num == STAR_REGISTER)
4039 return '*';
4040 else if (num == PLUS_REGISTER)
4041 return '+';
4042#endif
4043 else
4044 {
4045#ifdef EBCDIC
4046 int i;
4047
4048 /* EBCDIC is really braindead ... */
4049 i = 'a' + (num - 10);
4050 if (i > 'i')
4051 i += 7;
4052 if (i > 'r')
4053 i += 8;
4054 return i;
4055#else
4056 return num + 'a' - 10;
4057#endif
4058 }
4059}
4060
4061/*
4062 * ":dis" and ":registers": Display the contents of the yank registers.
4063 */
4064 void
4065ex_display(eap)
4066 exarg_T *eap;
4067{
4068 int i, n;
4069 long j;
4070 char_u *p;
4071 struct yankreg *yb;
4072 int name;
4073 int attr;
4074 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004075#ifdef FEAT_MBYTE
4076 int clen;
4077#else
4078# define clen 1
4079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 if (arg != NULL && *arg == NUL)
4082 arg = NULL;
4083 attr = hl_attr(HLF_8);
4084
4085 /* Highlight title */
4086 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4087 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4088 {
4089 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004090 if (arg != NULL && vim_strchr(arg, name) == NULL
4091#ifdef ONE_CLIPBOARD
4092 /* Star register and plus register contain the same thing. */
4093 && (name != '*' || vim_strchr(arg, '+') == NULL)
4094#endif
4095 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 continue; /* did not ask for this register */
4097
4098#ifdef FEAT_CLIPBOARD
4099 /* Adjust register name for "unnamed" in 'clipboard'.
4100 * When it's a clipboard register, fill it with the current contents
4101 * of the clipboard. */
4102 adjust_clip_reg(&name);
4103 (void)may_get_selection(name);
4104#endif
4105
4106 if (i == -1)
4107 {
4108 if (y_previous != NULL)
4109 yb = y_previous;
4110 else
4111 yb = &(y_regs[0]);
4112 }
4113 else
4114 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004115
4116#ifdef FEAT_EVAL
4117 if (name == MB_TOLOWER(redir_reg)
4118 || (redir_reg == '"' && yb == y_previous))
4119 continue; /* do not list register being written to, the
4120 * pointer can be freed */
4121#endif
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (yb->y_array != NULL)
4124 {
4125 msg_putchar('\n');
4126 msg_putchar('"');
4127 msg_putchar(name);
4128 MSG_PUTS(" ");
4129
4130 n = (int)Columns - 6;
4131 for (j = 0; j < yb->y_size && n > 1; ++j)
4132 {
4133 if (j)
4134 {
4135 MSG_PUTS_ATTR("^J", attr);
4136 n -= 2;
4137 }
4138 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004141 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004142#endif
4143 msg_outtrans_len(p, clen);
4144#ifdef FEAT_MBYTE
4145 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146#endif
4147 }
4148 }
4149 if (n > 1 && yb->y_type == MLINE)
4150 MSG_PUTS_ATTR("^J", attr);
4151 out_flush(); /* show one line at a time */
4152 }
4153 ui_breakcheck();
4154 }
4155
4156 /*
4157 * display last inserted text
4158 */
4159 if ((p = get_last_insert()) != NULL
4160 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4161 {
4162 MSG_PUTS("\n\". ");
4163 dis_msg(p, TRUE);
4164 }
4165
4166 /*
4167 * display last command line
4168 */
4169 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4170 && !got_int)
4171 {
4172 MSG_PUTS("\n\": ");
4173 dis_msg(last_cmdline, FALSE);
4174 }
4175
4176 /*
4177 * display current file name
4178 */
4179 if (curbuf->b_fname != NULL
4180 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4181 {
4182 MSG_PUTS("\n\"% ");
4183 dis_msg(curbuf->b_fname, FALSE);
4184 }
4185
4186 /*
4187 * display alternate file name
4188 */
4189 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4190 {
4191 char_u *fname;
4192 linenr_T dummy;
4193
4194 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4195 {
4196 MSG_PUTS("\n\"# ");
4197 dis_msg(fname, FALSE);
4198 }
4199 }
4200
4201 /*
4202 * display last search pattern
4203 */
4204 if (last_search_pat() != NULL
4205 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4206 {
4207 MSG_PUTS("\n\"/ ");
4208 dis_msg(last_search_pat(), FALSE);
4209 }
4210
4211#ifdef FEAT_EVAL
4212 /*
4213 * display last used expression
4214 */
4215 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4216 && !got_int)
4217 {
4218 MSG_PUTS("\n\"= ");
4219 dis_msg(expr_line, FALSE);
4220 }
4221#endif
4222}
4223
4224/*
4225 * display a string for do_dis()
4226 * truncate at end of screen line
4227 */
4228 static void
4229dis_msg(p, skip_esc)
4230 char_u *p;
4231 int skip_esc; /* if TRUE, ignore trailing ESC */
4232{
4233 int n;
4234#ifdef FEAT_MBYTE
4235 int l;
4236#endif
4237
4238 n = (int)Columns - 6;
4239 while (*p != NUL
4240 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4241 && (n -= ptr2cells(p)) >= 0)
4242 {
4243#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004244 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 msg_outtrans_len(p, l);
4247 p += l;
4248 }
4249 else
4250#endif
4251 msg_outtrans_len(p++, 1);
4252 }
4253 ui_breakcheck();
4254}
4255
Bram Moolenaar81340392012-06-06 16:12:59 +02004256#if defined(FEAT_COMMENTS) || defined(PROTO)
4257/*
4258 * If "process" is TRUE and the line begins with a comment leader (possibly
4259 * after some white space), return a pointer to the text after it. Put a boolean
4260 * value indicating whether the line ends with an unclosed comment in
4261 * "is_comment".
4262 * line - line to be processed,
4263 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004264 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004265 * include_space - whether to also skip space following the comment leader,
4266 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004267 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004268 */
4269 static char_u *
4270skip_comment(line, process, include_space, is_comment)
4271 char_u *line;
4272 int process;
4273 int include_space;
4274 int *is_comment;
4275{
4276 char_u *comment_flags = NULL;
4277 int lead_len;
4278 int leader_offset = get_last_leader_offset(line, &comment_flags);
4279
4280 *is_comment = FALSE;
4281 if (leader_offset != -1)
4282 {
4283 /* Let's check whether the line ends with an unclosed comment.
4284 * If the last comment leader has COM_END in flags, there's no comment.
4285 */
4286 while (*comment_flags)
4287 {
4288 if (*comment_flags == COM_END
4289 || *comment_flags == ':')
4290 break;
4291 ++comment_flags;
4292 }
4293 if (*comment_flags != COM_END)
4294 *is_comment = TRUE;
4295 }
4296
4297 if (process == FALSE)
4298 return line;
4299
4300 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4301
4302 if (lead_len == 0)
4303 return line;
4304
4305 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004306 * - COM_END,
4307 * - colon,
4308 * whichever comes first.
4309 */
4310 while (*comment_flags)
4311 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004312 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004313 || *comment_flags == ':')
4314 {
4315 break;
4316 }
4317 ++comment_flags;
4318 }
4319
4320 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004321 * starting with a closing part of a three-part comment. That's good,
4322 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004323 */
4324 if (*comment_flags == ':' || *comment_flags == NUL)
4325 line += lead_len;
4326
4327 return line;
4328}
4329#endif
4330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004332 * Join 'count' lines (minimal 2) at cursor position.
4333 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004334 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4335 * leaders should not be removed.
4336 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4337 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004339 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 */
4341 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004342do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004343 long count;
4344 int insert_space;
4345 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004346 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004347 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004349 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004350 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004351 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004353 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004354 int endcurr1 = NUL;
4355 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004356 int currsize = 0; /* size of the current line */
4357 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004359 colnr_T col = 0;
4360 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004361#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004362 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004363 int remove_comments = (use_formatoptions == TRUE)
4364 && has_format_option(FO_REMOVE_COMS);
4365 int prev_was_comment;
4366#endif
4367
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004369 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4370 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004373 /* Allocate an array to store the number of spaces inserted before each
4374 * line. We will use it to pre-compute the length of the new line and the
4375 * proper placement of each original line in the new one. */
4376 spaces = lalloc_clear((long_u)count, TRUE);
4377 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004379#if defined(FEAT_COMMENTS) || defined(PROTO)
4380 if (remove_comments)
4381 {
4382 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4383 if (comments == NULL)
4384 {
4385 vim_free(spaces);
4386 return FAIL;
4387 }
4388 }
4389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004392 * Don't move anything, just compute the final line length
4393 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004395 for (t = 0; t < count; ++t)
4396 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004397 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004398 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004399 {
4400 /* Set the '[ mark. */
4401 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4402 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4403 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004404#if defined(FEAT_COMMENTS) || defined(PROTO)
4405 if (remove_comments)
4406 {
4407 /* We don't want to remove the comment leader if the
4408 * previous line is not a comment. */
4409 if (t > 0 && prev_was_comment)
4410 {
4411
4412 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4413 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004414 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004415 curr = new_curr;
4416 }
4417 else
4418 curr = skip_comment(curr, FALSE, insert_space,
4419 &prev_was_comment);
4420 }
4421#endif
4422
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004423 if (insert_space && t > 0)
4424 {
4425 curr = skipwhite(curr);
4426 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4427#ifdef FEAT_MBYTE
4428 && (!has_format_option(FO_MBYTE_JOIN)
4429 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4430 && (!has_format_option(FO_MBYTE_JOIN2)
4431 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4432#endif
4433 )
4434 {
4435 /* don't add a space if the line is ending in a space */
4436 if (endcurr1 == ' ')
4437 endcurr1 = endcurr2;
4438 else
4439 ++spaces[t];
4440 /* extra space when 'joinspaces' set and line ends in '.' */
4441 if ( p_js
4442 && (endcurr1 == '.'
4443 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4444 && (endcurr1 == '?' || endcurr1 == '!'))))
4445 ++spaces[t];
4446 }
4447 }
4448 currsize = (int)STRLEN(curr);
4449 sumsize += currsize + spaces[t];
4450 endcurr1 = endcurr2 = NUL;
4451 if (insert_space && currsize > 0)
4452 {
4453#ifdef FEAT_MBYTE
4454 if (has_mbyte)
4455 {
4456 cend = curr + currsize;
4457 mb_ptr_back(curr, cend);
4458 endcurr1 = (*mb_ptr2char)(cend);
4459 if (cend > curr)
4460 {
4461 mb_ptr_back(curr, cend);
4462 endcurr2 = (*mb_ptr2char)(cend);
4463 }
4464 }
4465 else
4466#endif
4467 {
4468 endcurr1 = *(curr + currsize - 1);
4469 if (currsize > 1)
4470 endcurr2 = *(curr + currsize - 2);
4471 }
4472 }
4473 line_breakcheck();
4474 if (got_int)
4475 {
4476 ret = FAIL;
4477 goto theend;
4478 }
4479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004481 /* store the column position before last line */
4482 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004484 /* allocate the space for the new line */
4485 newp = alloc_check((unsigned)(sumsize + 1));
4486 cend = newp + sumsize;
4487 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004489 /*
4490 * Move affected lines to the new long one.
4491 *
4492 * Move marks from each deleted line to the joined line, adjusting the
4493 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4494 * should not really be a problem.
4495 */
4496 for (t = count - 1; ; --t)
4497 {
4498 cend -= currsize;
4499 mch_memmove(cend, curr, (size_t)currsize);
4500 if (spaces[t] > 0)
4501 {
4502 cend -= spaces[t];
4503 copy_spaces(cend, (size_t)(spaces[t]));
4504 }
4505 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004506 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004507 if (t == 0)
4508 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004509 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004510#if defined(FEAT_COMMENTS) || defined(PROTO)
4511 if (remove_comments)
4512 curr += comments[t - 1];
4513#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004514 if (insert_space && t > 1)
4515 curr = skipwhite(curr);
4516 currsize = (int)STRLEN(curr);
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4519
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004520 if (setmark)
4521 {
4522 /* Set the '] mark. */
4523 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4524 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4525 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 /* Only report the change in the first line here, del_lines() will report
4528 * the deleted line. */
4529 changed_lines(curwin->w_cursor.lnum, currsize,
4530 curwin->w_cursor.lnum + 1, 0L);
4531
4532 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004533 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 * briefly, and then move it back. After del_lines() the cursor may
4535 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 */
4537 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004539 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 curwin->w_cursor.lnum = t;
4541
4542 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004543 * Set the cursor column:
4544 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004545 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004547 curwin->w_cursor.col =
4548 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#ifdef FEAT_VIRTUALEDIT
4552 curwin->w_cursor.coladd = 0;
4553#endif
4554 curwin->w_set_curswant = TRUE;
4555
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004556theend:
4557 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004558#if defined(FEAT_COMMENTS) || defined(PROTO)
4559 if (remove_comments)
4560 vim_free(comments);
4561#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004562 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563}
4564
4565#ifdef FEAT_COMMENTS
4566/*
4567 * Return TRUE if the two comment leaders given are the same. "lnum" is
4568 * the first line. White-space is ignored. Note that the whole of
4569 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4570 */
4571 static int
4572same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4573 linenr_T lnum;
4574 int leader1_len;
4575 char_u *leader1_flags;
4576 int leader2_len;
4577 char_u *leader2_flags;
4578{
4579 int idx1 = 0, idx2 = 0;
4580 char_u *p;
4581 char_u *line1;
4582 char_u *line2;
4583
4584 if (leader1_len == 0)
4585 return (leader2_len == 0);
4586
4587 /*
4588 * If first leader has 'f' flag, the lines can be joined only if the
4589 * second line does not have a leader.
4590 * If first leader has 'e' flag, the lines can never be joined.
4591 * If fist leader has 's' flag, the lines can only be joined if there is
4592 * some text after it and the second line has the 'm' flag.
4593 */
4594 if (leader1_flags != NULL)
4595 {
4596 for (p = leader1_flags; *p && *p != ':'; ++p)
4597 {
4598 if (*p == COM_FIRST)
4599 return (leader2_len == 0);
4600 if (*p == COM_END)
4601 return FALSE;
4602 if (*p == COM_START)
4603 {
4604 if (*(ml_get(lnum) + leader1_len) == NUL)
4605 return FALSE;
4606 if (leader2_flags == NULL || leader2_len == 0)
4607 return FALSE;
4608 for (p = leader2_flags; *p && *p != ':'; ++p)
4609 if (*p == COM_MIDDLE)
4610 return TRUE;
4611 return FALSE;
4612 }
4613 }
4614 }
4615
4616 /*
4617 * Get current line and next line, compare the leaders.
4618 * The first line has to be saved, only one line can be locked at a time.
4619 */
4620 line1 = vim_strsave(ml_get(lnum));
4621 if (line1 != NULL)
4622 {
4623 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4624 ;
4625 line2 = ml_get(lnum + 1);
4626 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4627 {
4628 if (!vim_iswhite(line2[idx2]))
4629 {
4630 if (line1[idx1++] != line2[idx2])
4631 break;
4632 }
4633 else
4634 while (vim_iswhite(line1[idx1]))
4635 ++idx1;
4636 }
4637 vim_free(line1);
4638 }
4639 return (idx2 == leader2_len && idx1 == leader1_len);
4640}
4641#endif
4642
4643/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004644 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 */
4646 void
4647op_format(oap, keep_cursor)
4648 oparg_T *oap;
4649 int keep_cursor; /* keep cursor on same text char */
4650{
4651 long old_line_count = curbuf->b_ml.ml_line_count;
4652
4653 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4654 * can put it back there. */
4655 curwin->w_cursor = oap->cursor_start;
4656
4657 if (u_save((linenr_T)(oap->start.lnum - 1),
4658 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4659 return;
4660 curwin->w_cursor = oap->start;
4661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (oap->is_VIsual)
4663 /* When there is no change: need to remove the Visual selection */
4664 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666 /* Set '[ mark at the start of the formatted area */
4667 curbuf->b_op_start = oap->start;
4668
4669 /* For "gw" remember the cursor position and put it back below (adjusted
4670 * for joined and split lines). */
4671 if (keep_cursor)
4672 saved_cursor = oap->cursor_start;
4673
Bram Moolenaar81a82092008-03-12 16:27:00 +00004674 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 /*
4677 * Leave the cursor at the first non-blank of the last formatted line.
4678 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4679 * line, so "." will do the next lines.
4680 */
4681 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4682 ++curwin->w_cursor.lnum;
4683 beginline(BL_WHITE | BL_FIX);
4684 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4685 msgmore(old_line_count);
4686
4687 /* put '] mark on the end of the formatted area */
4688 curbuf->b_op_end = curwin->w_cursor;
4689
4690 if (keep_cursor)
4691 {
4692 curwin->w_cursor = saved_cursor;
4693 saved_cursor.lnum = 0;
4694 }
4695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (oap->is_VIsual)
4697 {
4698 win_T *wp;
4699
4700 FOR_ALL_WINDOWS(wp)
4701 {
4702 if (wp->w_old_cursor_lnum != 0)
4703 {
4704 /* When lines have been inserted or deleted, adjust the end of
4705 * the Visual area to be redrawn. */
4706 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4707 wp->w_old_cursor_lnum += old_line_count;
4708 else
4709 wp->w_old_visual_lnum += old_line_count;
4710 }
4711 }
4712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713}
4714
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004715#if defined(FEAT_EVAL) || defined(PROTO)
4716/*
4717 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4718 */
4719 void
4720op_formatexpr(oap)
4721 oparg_T *oap;
4722{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004723 if (oap->is_VIsual)
4724 /* When there is no change: need to remove the Visual selection */
4725 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004726
Bram Moolenaar700303e2010-07-11 17:35:50 +02004727 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4728 /* As documented: when 'formatexpr' returns non-zero fall back to
4729 * internal formatting. */
4730 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004731}
4732
4733 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004734fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004735 linenr_T lnum;
4736 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004737 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004738{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004739 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4740 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004741 int r;
4742
4743 /*
4744 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004745 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004746 */
4747 set_vim_var_nr(VV_LNUM, lnum);
4748 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004749 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004750
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004751 /*
4752 * Evaluate the function.
4753 */
4754 if (use_sandbox)
4755 ++sandbox;
4756 r = eval_to_number(curbuf->b_p_fex);
4757 if (use_sandbox)
4758 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004759
4760 set_vim_var_string(VV_CHAR, NULL, -1);
4761
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004762 return r;
4763}
4764#endif
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766/*
4767 * Format "line_count" lines, starting at the cursor position.
4768 * When "line_count" is negative, format until the end of the paragraph.
4769 * Lines after the cursor line are saved for undo, caller must have saved the
4770 * first line.
4771 */
4772 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004773format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004775 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
4777 int max_len;
4778 int is_not_par; /* current line not part of parag. */
4779 int next_is_not_par; /* next line not part of paragraph */
4780 int is_end_par; /* at end of paragraph */
4781 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4782 int next_is_start_par = FALSE;
4783#ifdef FEAT_COMMENTS
4784 int leader_len = 0; /* leader len of current line */
4785 int next_leader_len; /* leader len of next line */
4786 char_u *leader_flags = NULL; /* flags for leader of current line */
4787 char_u *next_leader_flags; /* flags for leader of next line */
4788 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004789 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790#endif
4791 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004792 int second_indent = -1; /* indent for second line (comment
4793 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 int do_second_indent;
4795 int do_number_indent;
4796 int do_trail_white;
4797 int first_par_line = TRUE;
4798 int smd_save;
4799 long count;
4800 int need_set_indent = TRUE; /* set indent of next paragraph */
4801 int force_format = FALSE;
4802 int old_State = State;
4803
4804 /* length of a line to force formatting: 3 * 'tw' */
4805 max_len = comp_textwidth(TRUE) * 3;
4806
4807 /* check for 'q', '2' and '1' in 'formatoptions' */
4808#ifdef FEAT_COMMENTS
4809 do_comments = has_format_option(FO_Q_COMS);
4810#endif
4811 do_second_indent = has_format_option(FO_Q_SECOND);
4812 do_number_indent = has_format_option(FO_Q_NUMBER);
4813 do_trail_white = has_format_option(FO_WHITE_PAR);
4814
4815 /*
4816 * Get info about the previous and current line.
4817 */
4818 if (curwin->w_cursor.lnum > 1)
4819 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4820#ifdef FEAT_COMMENTS
4821 , &leader_len, &leader_flags, do_comments
4822#endif
4823 );
4824 else
4825 is_not_par = TRUE;
4826 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4827#ifdef FEAT_COMMENTS
4828 , &next_leader_len, &next_leader_flags, do_comments
4829#endif
4830 );
4831 is_end_par = (is_not_par || next_is_not_par);
4832 if (!is_end_par && do_trail_white)
4833 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4834
4835 curwin->w_cursor.lnum--;
4836 for (count = line_count; count != 0 && !got_int; --count)
4837 {
4838 /*
4839 * Advance to next paragraph.
4840 */
4841 if (advance)
4842 {
4843 curwin->w_cursor.lnum++;
4844 prev_is_end_par = is_end_par;
4845 is_not_par = next_is_not_par;
4846#ifdef FEAT_COMMENTS
4847 leader_len = next_leader_len;
4848 leader_flags = next_leader_flags;
4849#endif
4850 }
4851
4852 /*
4853 * The last line to be formatted.
4854 */
4855 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4856 {
4857 next_is_not_par = TRUE;
4858#ifdef FEAT_COMMENTS
4859 next_leader_len = 0;
4860 next_leader_flags = NULL;
4861#endif
4862 }
4863 else
4864 {
4865 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4866#ifdef FEAT_COMMENTS
4867 , &next_leader_len, &next_leader_flags, do_comments
4868#endif
4869 );
4870 if (do_number_indent)
4871 next_is_start_par =
4872 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4873 }
4874 advance = TRUE;
4875 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4876 if (!is_end_par && do_trail_white)
4877 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4878
4879 /*
4880 * Skip lines that are not in a paragraph.
4881 */
4882 if (is_not_par)
4883 {
4884 if (line_count < 0)
4885 break;
4886 }
4887 else
4888 {
4889 /*
4890 * For the first line of a paragraph, check indent of second line.
4891 * Don't do this for comments and empty lines.
4892 */
4893 if (first_par_line
4894 && (do_second_indent || do_number_indent)
4895 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004896 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004898 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4899 {
4900#ifdef FEAT_COMMENTS
4901 if (leader_len == 0 && next_leader_len == 0)
4902 {
4903 /* no comment found */
4904#endif
4905 second_indent =
4906 get_indent_lnum(curwin->w_cursor.lnum + 1);
4907#ifdef FEAT_COMMENTS
4908 }
4909 else
4910 {
4911 second_indent = next_leader_len;
4912 do_comments_list = 1;
4913 }
4914#endif
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004917 {
4918#ifdef FEAT_COMMENTS
4919 if (leader_len == 0 && next_leader_len == 0)
4920 {
4921 /* no comment found */
4922#endif
4923 second_indent =
4924 get_number_indent(curwin->w_cursor.lnum);
4925#ifdef FEAT_COMMENTS
4926 }
4927 else
4928 {
4929 /* get_number_indent() is now "comment aware"... */
4930 second_indent =
4931 get_number_indent(curwin->w_cursor.lnum);
4932 do_comments_list = 1;
4933 }
4934#endif
4935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937
4938 /*
4939 * When the comment leader changes, it's the end of the paragraph.
4940 */
4941 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4942#ifdef FEAT_COMMENTS
4943 || !same_leader(curwin->w_cursor.lnum,
4944 leader_len, leader_flags,
4945 next_leader_len, next_leader_flags)
4946#endif
4947 )
4948 is_end_par = TRUE;
4949
4950 /*
4951 * If we have got to the end of a paragraph, or the line is
4952 * getting long, format it.
4953 */
4954 if (is_end_par || force_format)
4955 {
4956 if (need_set_indent)
4957 /* replace indent in first line with minimal number of
4958 * tabs and spaces, according to current options */
4959 (void)set_indent(get_indent(), SIN_CHANGED);
4960
4961 /* put cursor on last non-space */
4962 State = NORMAL; /* don't go past end-of-line */
4963 coladvance((colnr_T)MAXCOL);
4964 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4965 dec_cursor();
4966
4967 /* do the formatting, without 'showmode' */
4968 State = INSERT; /* for open_line() */
4969 smd_save = p_smd;
4970 p_smd = FALSE;
4971 insertchar(NUL, INSCHAR_FORMAT
4972#ifdef FEAT_COMMENTS
4973 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004974 + (do_comments && do_comments_list
4975 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004977 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 State = old_State;
4979 p_smd = smd_save;
4980 second_indent = -1;
4981 /* at end of par.: need to set indent of next par. */
4982 need_set_indent = is_end_par;
4983 if (is_end_par)
4984 {
4985 /* When called with a negative line count, break at the
4986 * end of the paragraph. */
4987 if (line_count < 0)
4988 break;
4989 first_par_line = TRUE;
4990 }
4991 force_format = FALSE;
4992 }
4993
4994 /*
4995 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004996 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
4998 if (!is_end_par)
4999 {
5000 advance = FALSE;
5001 curwin->w_cursor.lnum++;
5002 curwin->w_cursor.col = 0;
5003 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005007 {
5008 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5010 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005011 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005013 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5014 {
5015 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005016 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005017
5018 if (indent > 0)
5019 {
5020 (void)del_bytes(indent, FALSE, FALSE);
5021 mark_col_adjust(curwin->w_cursor.lnum,
5022 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005023 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005026 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
5028 beep_flush();
5029 break;
5030 }
5031 first_par_line = FALSE;
5032 /* If the line is getting long, format it next time */
5033 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5034 force_format = TRUE;
5035 else
5036 force_format = FALSE;
5037 }
5038 }
5039 line_breakcheck();
5040 }
5041}
5042
5043/*
5044 * Return TRUE if line "lnum" ends in a white character.
5045 */
5046 static int
5047ends_in_white(lnum)
5048 linenr_T lnum;
5049{
5050 char_u *s = ml_get(lnum);
5051 size_t l;
5052
5053 if (*s == NUL)
5054 return FALSE;
5055 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5056 * invocation may call function multiple times". */
5057 l = STRLEN(s) - 1;
5058 return vim_iswhite(s[l]);
5059}
5060
5061/*
5062 * Blank lines, and lines containing only the comment leader, are left
5063 * untouched by the formatting. The function returns TRUE in this
5064 * case. It also returns TRUE when a line starts with the end of a comment
5065 * ('e' in comment flags), so that this line is skipped, and not joined to the
5066 * previous line. A new paragraph starts after a blank line, or when the
5067 * comment leader changes -- webb.
5068 */
5069#ifdef FEAT_COMMENTS
5070 static int
5071fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5072 linenr_T lnum;
5073 int *leader_len;
5074 char_u **leader_flags;
5075 int do_comments;
5076{
5077 char_u *flags = NULL; /* init for GCC */
5078 char_u *ptr;
5079
5080 ptr = ml_get(lnum);
5081 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005082 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else
5084 *leader_len = 0;
5085
5086 if (*leader_len > 0)
5087 {
5088 /*
5089 * Search for 'e' flag in comment leader flags.
5090 */
5091 flags = *leader_flags;
5092 while (*flags && *flags != ':' && *flags != COM_END)
5093 ++flags;
5094 }
5095
5096 return (*skipwhite(ptr + *leader_len) == NUL
5097 || (*leader_len > 0 && *flags == COM_END)
5098 || startPS(lnum, NUL, FALSE));
5099}
5100#else
5101 static int
5102fmt_check_par(lnum)
5103 linenr_T lnum;
5104{
5105 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5106}
5107#endif
5108
5109/*
5110 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5111 * previous line is in the same paragraph. Used for auto-formatting.
5112 */
5113 int
5114paragraph_start(lnum)
5115 linenr_T lnum;
5116{
5117 char_u *p;
5118#ifdef FEAT_COMMENTS
5119 int leader_len = 0; /* leader len of current line */
5120 char_u *leader_flags = NULL; /* flags for leader of current line */
5121 int next_leader_len; /* leader len of next line */
5122 char_u *next_leader_flags; /* flags for leader of next line */
5123 int do_comments; /* format comments */
5124#endif
5125
5126 if (lnum <= 1)
5127 return TRUE; /* start of the file */
5128
5129 p = ml_get(lnum - 1);
5130 if (*p == NUL)
5131 return TRUE; /* after empty line */
5132
5133#ifdef FEAT_COMMENTS
5134 do_comments = has_format_option(FO_Q_COMS);
5135#endif
5136 if (fmt_check_par(lnum - 1
5137#ifdef FEAT_COMMENTS
5138 , &leader_len, &leader_flags, do_comments
5139#endif
5140 ))
5141 return TRUE; /* after non-paragraph line */
5142
5143 if (fmt_check_par(lnum
5144#ifdef FEAT_COMMENTS
5145 , &next_leader_len, &next_leader_flags, do_comments
5146#endif
5147 ))
5148 return TRUE; /* "lnum" is not a paragraph line */
5149
5150 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5151 return TRUE; /* missing trailing space in previous line. */
5152
5153 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5154 return TRUE; /* numbered item starts in "lnum". */
5155
5156#ifdef FEAT_COMMENTS
5157 if (!same_leader(lnum - 1, leader_len, leader_flags,
5158 next_leader_len, next_leader_flags))
5159 return TRUE; /* change of comment leader. */
5160#endif
5161
5162 return FALSE;
5163}
5164
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165/*
5166 * prepare a few things for block mode yank/delete/tilde
5167 *
5168 * for delete:
5169 * - textlen includes the first/last char to be (partly) deleted
5170 * - start/endspaces is the number of columns that are taken by the
5171 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005172 * deleted.
5173 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 * - textlen includes the first/last char to be wholly yanked
5175 * - start/endspaces is the number of columns of the first/last yanked char
5176 * that are to be yanked.
5177 */
5178 static void
5179block_prep(oap, bdp, lnum, is_del)
5180 oparg_T *oap;
5181 struct block_def *bdp;
5182 linenr_T lnum;
5183 int is_del;
5184{
5185 int incr = 0;
5186 char_u *pend;
5187 char_u *pstart;
5188 char_u *line;
5189 char_u *prev_pstart;
5190 char_u *prev_pend;
5191
5192 bdp->startspaces = 0;
5193 bdp->endspaces = 0;
5194 bdp->textlen = 0;
5195 bdp->start_vcol = 0;
5196 bdp->end_vcol = 0;
5197#ifdef FEAT_VISUALEXTRA
5198 bdp->is_short = FALSE;
5199 bdp->is_oneChar = FALSE;
5200 bdp->pre_whitesp = 0;
5201 bdp->pre_whitesp_c = 0;
5202 bdp->end_char_vcols = 0;
5203#endif
5204 bdp->start_char_vcols = 0;
5205
5206 line = ml_get(lnum);
5207 pstart = line;
5208 prev_pstart = line;
5209 while (bdp->start_vcol < oap->start_vcol && *pstart)
5210 {
5211 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005212 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 bdp->start_vcol += incr;
5214#ifdef FEAT_VISUALEXTRA
5215 if (vim_iswhite(*pstart))
5216 {
5217 bdp->pre_whitesp += incr;
5218 bdp->pre_whitesp_c++;
5219 }
5220 else
5221 {
5222 bdp->pre_whitesp = 0;
5223 bdp->pre_whitesp_c = 0;
5224 }
5225#endif
5226 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005227 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 bdp->start_char_vcols = incr;
5230 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5231 {
5232 bdp->end_vcol = bdp->start_vcol;
5233#ifdef FEAT_VISUALEXTRA
5234 bdp->is_short = TRUE;
5235#endif
5236 if (!is_del || oap->op_type == OP_APPEND)
5237 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5238 }
5239 else
5240 {
5241 /* notice: this converts partly selected Multibyte characters to
5242 * spaces, too. */
5243 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5244 if (is_del && bdp->startspaces)
5245 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5246 pend = pstart;
5247 bdp->end_vcol = bdp->start_vcol;
5248 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5249 {
5250#ifdef FEAT_VISUALEXTRA
5251 bdp->is_oneChar = TRUE;
5252#endif
5253 if (oap->op_type == OP_INSERT)
5254 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5255 else if (oap->op_type == OP_APPEND)
5256 {
5257 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5258 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5259 }
5260 else
5261 {
5262 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5263 if (is_del && oap->op_type != OP_LSHIFT)
5264 {
5265 /* just putting the sum of those two into
5266 * bdp->startspaces doesn't work for Visual replace,
5267 * so we have to split the tab in two */
5268 bdp->startspaces = bdp->start_char_vcols
5269 - (bdp->start_vcol - oap->start_vcol);
5270 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5271 }
5272 }
5273 }
5274 else
5275 {
5276 prev_pend = pend;
5277 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5278 {
5279 /* Count a tab for what it's worth (if list mode not on) */
5280 prev_pend = pend;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005281 /* TODO: is passing prev_pend for start of the line OK?
5282 * perhaps it should be "line". */
5283 incr = lbr_chartabsize_adv(prev_pend, &pend,
5284 (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 bdp->end_vcol += incr;
5286 }
5287 if (bdp->end_vcol <= oap->end_vcol
5288 && (!is_del
5289 || oap->op_type == OP_APPEND
5290 || oap->op_type == OP_REPLACE)) /* line too short */
5291 {
5292#ifdef FEAT_VISUALEXTRA
5293 bdp->is_short = TRUE;
5294#endif
5295 /* Alternative: include spaces to fill up the block.
5296 * Disadvantage: can lead to trailing spaces when the line is
5297 * short where the text is put */
5298 /* if (!is_del || oap->op_type == OP_APPEND) */
5299 if (oap->op_type == OP_APPEND || virtual_op)
5300 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005301 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 else
5303 bdp->endspaces = 0; /* replace doesn't add characters */
5304 }
5305 else if (bdp->end_vcol > oap->end_vcol)
5306 {
5307 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5308 if (!is_del && bdp->endspaces)
5309 {
5310 bdp->endspaces = incr - bdp->endspaces;
5311 if (pend != pstart)
5312 pend = prev_pend;
5313 }
5314 }
5315 }
5316#ifdef FEAT_VISUALEXTRA
5317 bdp->end_char_vcols = incr;
5318#endif
5319 if (is_del && bdp->startspaces)
5320 pstart = prev_pstart;
5321 bdp->textlen = (int)(pend - pstart);
5322 }
5323 bdp->textcol = (colnr_T) (pstart - line);
5324 bdp->textstart = pstart;
5325}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327#ifdef FEAT_RIGHTLEFT
5328static void reverse_line __ARGS((char_u *s));
5329
5330 static void
5331reverse_line(s)
5332 char_u *s;
5333{
5334 int i, j;
5335 char_u c;
5336
5337 if ((i = (int)STRLEN(s) - 1) <= 0)
5338 return;
5339
5340 curwin->w_cursor.col = i - curwin->w_cursor.col;
5341 for (j = 0; j < i; j++, i--)
5342 {
5343 c = s[i]; s[i] = s[j]; s[j] = c;
5344 }
5345}
5346
5347# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5348#else
5349# define RLADDSUBFIX(ptr)
5350#endif
5351
5352/*
5353 * add or subtract 'Prenum1' from a number in a line
5354 * 'command' is CTRL-A for add, CTRL-X for subtract
5355 *
5356 * return FAIL for failure, OK otherwise
5357 */
5358 int
5359do_addsub(command, Prenum1)
5360 int command;
5361 linenr_T Prenum1;
5362{
5363 int col;
5364 char_u *buf1;
5365 char_u buf2[NUMBUFLEN];
5366 int hex; /* 'X' or 'x': hex; '0': octal */
5367 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005368 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 long_u oldn;
5370 char_u *ptr;
5371 int c;
5372 int length = 0; /* character length of the number */
5373 int todel;
5374 int dohex;
5375 int dooct;
5376 int doalp;
5377 int firstdigit;
5378 int negative;
5379 int subtract;
5380
5381 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5382 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5383 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5384
5385 ptr = ml_get_curline();
5386 RLADDSUBFIX(ptr);
5387
5388 /*
5389 * First check if we are on a hexadecimal number, after the "0x".
5390 */
5391 col = curwin->w_cursor.col;
5392 if (dohex)
5393 while (col > 0 && vim_isxdigit(ptr[col]))
5394 --col;
5395 if ( dohex
5396 && col > 0
5397 && (ptr[col] == 'X'
5398 || ptr[col] == 'x')
5399 && ptr[col - 1] == '0'
5400 && vim_isxdigit(ptr[col + 1]))
5401 {
5402 /*
5403 * Found hexadecimal number, move to its start.
5404 */
5405 --col;
5406 }
5407 else
5408 {
5409 /*
5410 * Search forward and then backward to find the start of number.
5411 */
5412 col = curwin->w_cursor.col;
5413
5414 while (ptr[col] != NUL
5415 && !vim_isdigit(ptr[col])
5416 && !(doalp && ASCII_ISALPHA(ptr[col])))
5417 ++col;
5418
5419 while (col > 0
5420 && vim_isdigit(ptr[col - 1])
5421 && !(doalp && ASCII_ISALPHA(ptr[col])))
5422 --col;
5423 }
5424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 /*
5426 * If a number was found, and saving for undo works, replace the number.
5427 */
5428 firstdigit = ptr[col];
5429 RLADDSUBFIX(ptr);
5430 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5431 || u_save_cursor() != OK)
5432 {
5433 beep_flush();
5434 return FAIL;
5435 }
5436
5437 /* get ptr again, because u_save() may have changed it */
5438 ptr = ml_get_curline();
5439 RLADDSUBFIX(ptr);
5440
5441 if (doalp && ASCII_ISALPHA(firstdigit))
5442 {
5443 /* decrement or increment alphabetic character */
5444 if (command == Ctrl_X)
5445 {
5446 if (CharOrd(firstdigit) < Prenum1)
5447 {
5448 if (isupper(firstdigit))
5449 firstdigit = 'A';
5450 else
5451 firstdigit = 'a';
5452 }
5453 else
5454#ifdef EBCDIC
5455 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5456#else
5457 firstdigit -= Prenum1;
5458#endif
5459 }
5460 else
5461 {
5462 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5463 {
5464 if (isupper(firstdigit))
5465 firstdigit = 'Z';
5466 else
5467 firstdigit = 'z';
5468 }
5469 else
5470#ifdef EBCDIC
5471 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5472#else
5473 firstdigit += Prenum1;
5474#endif
5475 }
5476 curwin->w_cursor.col = col;
5477 (void)del_char(FALSE);
5478 ins_char(firstdigit);
5479 }
5480 else
5481 {
5482 negative = FALSE;
5483 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5484 {
5485 --col;
5486 negative = TRUE;
5487 }
5488
5489 /* get the number value (unsigned) */
5490 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5491
5492 /* ignore leading '-' for hex and octal numbers */
5493 if (hex && negative)
5494 {
5495 ++col;
5496 --length;
5497 negative = FALSE;
5498 }
5499
5500 /* add or subtract */
5501 subtract = FALSE;
5502 if (command == Ctrl_X)
5503 subtract ^= TRUE;
5504 if (negative)
5505 subtract ^= TRUE;
5506
5507 oldn = n;
5508 if (subtract)
5509 n -= (unsigned long)Prenum1;
5510 else
5511 n += (unsigned long)Prenum1;
5512
5513 /* handle wraparound for decimal numbers */
5514 if (!hex)
5515 {
5516 if (subtract)
5517 {
5518 if (n > oldn)
5519 {
5520 n = 1 + (n ^ (unsigned long)-1);
5521 negative ^= TRUE;
5522 }
5523 }
5524 else /* add */
5525 {
5526 if (n < oldn)
5527 {
5528 n = (n ^ (unsigned long)-1);
5529 negative ^= TRUE;
5530 }
5531 }
5532 if (n == 0)
5533 negative = FALSE;
5534 }
5535
5536 /*
5537 * Delete the old number.
5538 */
5539 curwin->w_cursor.col = col;
5540 todel = length;
5541 c = gchar_cursor();
5542 /*
5543 * Don't include the '-' in the length, only the length of the part
5544 * after it is kept the same.
5545 */
5546 if (c == '-')
5547 --length;
5548 while (todel-- > 0)
5549 {
5550 if (c < 0x100 && isalpha(c))
5551 {
5552 if (isupper(c))
5553 hexupper = TRUE;
5554 else
5555 hexupper = FALSE;
5556 }
5557 /* del_char() will mark line needing displaying */
5558 (void)del_char(FALSE);
5559 c = gchar_cursor();
5560 }
5561
5562 /*
5563 * Prepare the leading characters in buf1[].
5564 * When there are many leading zeros it could be very long. Allocate
5565 * a bit too much.
5566 */
5567 buf1 = alloc((unsigned)length + NUMBUFLEN);
5568 if (buf1 == NULL)
5569 return FAIL;
5570 ptr = buf1;
5571 if (negative)
5572 {
5573 *ptr++ = '-';
5574 }
5575 if (hex)
5576 {
5577 *ptr++ = '0';
5578 --length;
5579 }
5580 if (hex == 'x' || hex == 'X')
5581 {
5582 *ptr++ = hex;
5583 --length;
5584 }
5585
5586 /*
5587 * Put the number characters in buf2[].
5588 */
5589 if (hex == 0)
5590 sprintf((char *)buf2, "%lu", n);
5591 else if (hex == '0')
5592 sprintf((char *)buf2, "%lo", n);
5593 else if (hex && hexupper)
5594 sprintf((char *)buf2, "%lX", n);
5595 else
5596 sprintf((char *)buf2, "%lx", n);
5597 length -= (int)STRLEN(buf2);
5598
5599 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005600 * Adjust number of zeros to the new number of digits, so the
5601 * total length of the number remains the same.
5602 * Don't do this when
5603 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005605 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 while (length-- > 0)
5607 *ptr++ = '0';
5608 *ptr = NUL;
5609 STRCAT(buf1, buf2);
5610 ins_str(buf1); /* insert the new number */
5611 vim_free(buf1);
5612 }
5613 --curwin->w_cursor.col;
5614 curwin->w_set_curswant = TRUE;
5615#ifdef FEAT_RIGHTLEFT
5616 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5617 RLADDSUBFIX(ptr);
5618#endif
5619 return OK;
5620}
5621
5622#ifdef FEAT_VIMINFO
5623 int
5624read_viminfo_register(virp, force)
5625 vir_T *virp;
5626 int force;
5627{
5628 int eof;
5629 int do_it = TRUE;
5630 int size;
5631 int limit;
5632 int i;
5633 int set_prev = FALSE;
5634 char_u *str;
5635 char_u **array = NULL;
5636
5637 /* We only get here (hopefully) if line[0] == '"' */
5638 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005639
5640 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 if (*str == '"')
5642 {
5643 set_prev = TRUE;
5644 str++;
5645 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 if (!ASCII_ISALNUM(*str) && *str != '-')
5648 {
5649 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5650 return TRUE; /* too many errors, pretend end-of-file */
5651 do_it = FALSE;
5652 }
5653 get_yank_register(*str++, FALSE);
5654 if (!force && y_current->y_array != NULL)
5655 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005656
5657 if (*str == '@')
5658 {
5659 /* "x@: register x used for @@ */
5660 if (force || execreg_lastc == NUL)
5661 execreg_lastc = str[-1];
5662 }
5663
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 size = 0;
5665 limit = 100; /* Optimized for registers containing <= 100 lines */
5666 if (do_it)
5667 {
5668 if (set_prev)
5669 y_previous = y_current;
5670 vim_free(y_current->y_array);
5671 array = y_current->y_array =
5672 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005673 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 if (STRNCMP(str, "CHAR", 4) == 0)
5675 y_current->y_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 else if (STRNCMP(str, "BLOCK", 5) == 0)
5677 y_current->y_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679 y_current->y_type = MLINE;
5680 /* get the block width; if it's missing we get a zero, which is OK */
5681 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 y_current->y_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684
5685 while (!(eof = viminfo_readline(virp))
5686 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5687 {
5688 if (do_it)
5689 {
5690 if (size >= limit)
5691 {
5692 y_current->y_array = (char_u **)
5693 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5694 for (i = 0; i < limit; i++)
5695 y_current->y_array[i] = array[i];
5696 vim_free(array);
5697 limit *= 2;
5698 array = y_current->y_array;
5699 }
5700 str = viminfo_readstring(virp, 1, TRUE);
5701 if (str != NULL)
5702 array[size++] = str;
5703 else
5704 do_it = FALSE;
5705 }
5706 }
5707 if (do_it)
5708 {
5709 if (size == 0)
5710 {
5711 vim_free(array);
5712 y_current->y_array = NULL;
5713 }
5714 else if (size < limit)
5715 {
5716 y_current->y_array =
5717 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5718 for (i = 0; i < size; i++)
5719 y_current->y_array[i] = array[i];
5720 vim_free(array);
5721 }
5722 y_current->y_size = size;
5723 }
5724 return eof;
5725}
5726
5727 void
5728write_viminfo_registers(fp)
5729 FILE *fp;
5730{
5731 int i, j;
5732 char_u *type;
5733 char_u c;
5734 int num_lines;
5735 int max_num_lines;
5736 int max_kbyte;
5737 long len;
5738
Bram Moolenaar64404472010-06-26 06:24:45 +02005739 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
5741 /* Get '<' value, use old '"' value if '<' is not found. */
5742 max_num_lines = get_viminfo_parameter('<');
5743 if (max_num_lines < 0)
5744 max_num_lines = get_viminfo_parameter('"');
5745 if (max_num_lines == 0)
5746 return;
5747 max_kbyte = get_viminfo_parameter('s');
5748 if (max_kbyte == 0)
5749 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 for (i = 0; i < NUM_REGISTERS; i++)
5752 {
5753 if (y_regs[i].y_array == NULL)
5754 continue;
5755#ifdef FEAT_CLIPBOARD
5756 /* Skip '*'/'+' register, we don't want them back next time */
5757 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5758 continue;
5759#endif
5760#ifdef FEAT_DND
5761 /* Neither do we want the '~' register */
5762 if (i == TILDE_REGISTER)
5763 continue;
5764#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005765 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005767 if (num_lines == 0
5768 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005769 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005770 continue;
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (max_kbyte > 0)
5773 {
5774 /* Skip register if there is more text than the maximum size. */
5775 len = 0;
5776 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005777 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 if (len > (long)max_kbyte * 1024L)
5779 continue;
5780 }
5781
5782 switch (y_regs[i].y_type)
5783 {
5784 case MLINE:
5785 type = (char_u *)"LINE";
5786 break;
5787 case MCHAR:
5788 type = (char_u *)"CHAR";
5789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 case MBLOCK:
5791 type = (char_u *)"BLOCK";
5792 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 default:
5794 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005795 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 emsg(IObuff);
5797 type = (char_u *)"LINE";
5798 break;
5799 }
5800 if (y_previous == &y_regs[i])
5801 fprintf(fp, "\"");
5802 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005803 fprintf(fp, "\"%c", c);
5804 if (c == execreg_lastc)
5805 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005806 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
5808 /* If max_num_lines < 0, then we save ALL the lines in the register */
5809 if (max_num_lines > 0 && num_lines > max_num_lines)
5810 num_lines = max_num_lines;
5811 for (j = 0; j < num_lines; j++)
5812 {
5813 putc('\t', fp);
5814 viminfo_writestring(fp, y_regs[i].y_array[j]);
5815 }
5816 }
5817}
5818#endif /* FEAT_VIMINFO */
5819
5820#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5821/*
5822 * SELECTION / PRIMARY ('*')
5823 *
5824 * Text selection stuff that uses the GUI selection register '*'. When using a
5825 * GUI this may be text from another window, otherwise it is the last text we
5826 * had highlighted with VIsual mode. With mouse support, clicking the middle
5827 * button performs the paste, otherwise you will need to do <"*p>. "
5828 * If not under X, it is synonymous with the clipboard register '+'.
5829 *
5830 * X CLIPBOARD ('+')
5831 *
5832 * Text selection stuff that uses the GUI clipboard register '+'.
5833 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5834 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5835 * otherwise you will need to do <"+p>. "
5836 * If not under X, it is synonymous with the selection register '*'.
5837 */
5838
5839/*
5840 * Routine to export any final X selection we had to the environment
5841 * so that the text is still available after vim has exited. X selections
5842 * only exist while the owning application exists, so we write to the
5843 * permanent (while X runs) store CUT_BUFFER0.
5844 * Dump the CLIPBOARD selection if we own it (it's logically the more
5845 * 'permanent' of the two), otherwise the PRIMARY one.
5846 * For now, use a hard-coded sanity limit of 1Mb of data.
5847 */
5848#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5849 void
5850x11_export_final_selection()
5851{
5852 Display *dpy;
5853 char_u *str = NULL;
5854 long_u len = 0;
5855 int motion_type = -1;
5856
5857# ifdef FEAT_GUI
5858 if (gui.in_use)
5859 dpy = X_DISPLAY;
5860 else
5861# endif
5862# ifdef FEAT_XCLIPBOARD
5863 dpy = xterm_dpy;
5864# else
5865 return;
5866# endif
5867
5868 /* Get selection to export */
5869 if (clip_plus.owned)
5870 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5871 else if (clip_star.owned)
5872 motion_type = clip_convert_selection(&str, &len, &clip_star);
5873
5874 /* Check it's OK */
5875 if (dpy != NULL && str != NULL && motion_type >= 0
5876 && len < 1024*1024 && len > 0)
5877 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005878#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005879 int ok = TRUE;
5880
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005881 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5882 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5883 * encoding conversion usually doesn't work, so keep the text as-is.
5884 */
5885 if (has_mbyte)
5886 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005887 vimconv_T vc;
5888
5889 vc.vc_type = CONV_NONE;
5890 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5891 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005892 int intlen = len;
5893 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005894
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005895 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005896 conv_str = string_convert(&vc, str, &intlen);
5897 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005898 if (conv_str != NULL)
5899 {
5900 vim_free(str);
5901 str = conv_str;
5902 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005903 else
5904 {
5905 ok = FALSE;
5906 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005907 convert_setup(&vc, NULL, NULL);
5908 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005909 else
5910 {
5911 ok = FALSE;
5912 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005913 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005914
5915 /* Do not store the string if conversion failed. Better to use any
5916 * other selection than garbled text. */
5917 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005918#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005919 {
5920 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5921 XFlush(dpy);
5922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
5924
5925 vim_free(str);
5926}
5927#endif
5928
5929 void
5930clip_free_selection(cbd)
5931 VimClipboard *cbd;
5932{
5933 struct yankreg *y_ptr = y_current;
5934
5935 if (cbd == &clip_plus)
5936 y_current = &y_regs[PLUS_REGISTER];
5937 else
5938 y_current = &y_regs[STAR_REGISTER];
5939 free_yank_all();
5940 y_current->y_size = 0;
5941 y_current = y_ptr;
5942}
5943
5944/*
5945 * Get the selected text and put it in the gui selection register '*' or '+'.
5946 */
5947 void
5948clip_get_selection(cbd)
5949 VimClipboard *cbd;
5950{
5951 struct yankreg *old_y_previous, *old_y_current;
5952 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 pos_T old_visual;
5954 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 colnr_T old_curswant;
5956 int old_set_curswant;
5957 pos_T old_op_start, old_op_end;
5958 oparg_T oa;
5959 cmdarg_T ca;
5960
5961 if (cbd->owned)
5962 {
5963 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5964 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5965 return;
5966
5967 /* Get the text between clip_star.start & clip_star.end */
5968 old_y_previous = y_previous;
5969 old_y_current = y_current;
5970 old_cursor = curwin->w_cursor;
5971 old_curswant = curwin->w_curswant;
5972 old_set_curswant = curwin->w_set_curswant;
5973 old_op_start = curbuf->b_op_start;
5974 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 old_visual = VIsual;
5976 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 clear_oparg(&oa);
5978 oa.regname = (cbd == &clip_plus ? '+' : '*');
5979 oa.op_type = OP_YANK;
5980 vim_memset(&ca, 0, sizeof(ca));
5981 ca.oap = &oa;
5982 ca.cmdchar = 'y';
5983 ca.count1 = 1;
5984 ca.retval = CA_NO_ADJ_OP_END;
5985 do_pending_operator(&ca, 0, TRUE);
5986 y_previous = old_y_previous;
5987 y_current = old_y_current;
5988 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005989 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 curwin->w_curswant = old_curswant;
5991 curwin->w_set_curswant = old_set_curswant;
5992 curbuf->b_op_start = old_op_start;
5993 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 VIsual = old_visual;
5995 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 }
5997 else
5998 {
5999 clip_free_selection(cbd);
6000
6001 /* Try to get selected text from another window */
6002 clip_gen_request_selection(cbd);
6003 }
6004}
6005
Bram Moolenaard44347f2011-06-19 01:14:29 +02006006/*
6007 * Convert from the GUI selection string into the '*'/'+' register.
6008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 void
6010clip_yank_selection(type, str, len, cbd)
6011 int type;
6012 char_u *str;
6013 long len;
6014 VimClipboard *cbd;
6015{
6016 struct yankreg *y_ptr;
6017
6018 if (cbd == &clip_plus)
6019 y_ptr = &y_regs[PLUS_REGISTER];
6020 else
6021 y_ptr = &y_regs[STAR_REGISTER];
6022
6023 clip_free_selection(cbd);
6024
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006025 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026}
6027
6028/*
6029 * Convert the '*'/'+' register into a GUI selection string returned in *str
6030 * with length *len.
6031 * Returns the motion type, or -1 for failure.
6032 */
6033 int
6034clip_convert_selection(str, len, cbd)
6035 char_u **str;
6036 long_u *len;
6037 VimClipboard *cbd;
6038{
6039 char_u *p;
6040 int lnum;
6041 int i, j;
6042 int_u eolsize;
6043 struct yankreg *y_ptr;
6044
6045 if (cbd == &clip_plus)
6046 y_ptr = &y_regs[PLUS_REGISTER];
6047 else
6048 y_ptr = &y_regs[STAR_REGISTER];
6049
6050#ifdef USE_CRNL
6051 eolsize = 2;
6052#else
6053 eolsize = 1;
6054#endif
6055
6056 *str = NULL;
6057 *len = 0;
6058 if (y_ptr->y_array == NULL)
6059 return -1;
6060
6061 for (i = 0; i < y_ptr->y_size; i++)
6062 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6063
6064 /*
6065 * Don't want newline character at end of last line if we're in MCHAR mode.
6066 */
6067 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6068 *len -= eolsize;
6069
6070 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6071 if (p == NULL)
6072 return -1;
6073 lnum = 0;
6074 for (i = 0, j = 0; i < (int)*len; i++, j++)
6075 {
6076 if (y_ptr->y_array[lnum][j] == '\n')
6077 p[i] = NUL;
6078 else if (y_ptr->y_array[lnum][j] == NUL)
6079 {
6080#ifdef USE_CRNL
6081 p[i++] = '\r';
6082#endif
6083#ifdef USE_CR
6084 p[i] = '\r';
6085#else
6086 p[i] = '\n';
6087#endif
6088 lnum++;
6089 j = -1;
6090 }
6091 else
6092 p[i] = y_ptr->y_array[lnum][j];
6093 }
6094 return y_ptr->y_type;
6095}
6096
6097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098/*
6099 * If we have written to a clipboard register, send the text to the clipboard.
6100 */
6101 static void
6102may_set_selection()
6103{
6104 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6105 {
6106 clip_own_selection(&clip_star);
6107 clip_gen_set_selection(&clip_star);
6108 }
6109 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6110 {
6111 clip_own_selection(&clip_plus);
6112 clip_gen_set_selection(&clip_plus);
6113 }
6114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116#endif /* FEAT_CLIPBOARD || PROTO */
6117
6118
6119#if defined(FEAT_DND) || defined(PROTO)
6120/*
6121 * Replace the contents of the '~' register with str.
6122 */
6123 void
6124dnd_yank_drag_data(str, len)
6125 char_u *str;
6126 long len;
6127{
6128 struct yankreg *curr;
6129
6130 curr = y_current;
6131 y_current = &y_regs[TILDE_REGISTER];
6132 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006133 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 y_current = curr;
6135}
6136#endif
6137
6138
6139#if defined(FEAT_EVAL) || defined(PROTO)
6140/*
6141 * Return the type of a register.
6142 * Used for getregtype()
6143 * Returns MAUTO for error.
6144 */
6145 char_u
6146get_reg_type(regname, reglen)
6147 int regname;
6148 long *reglen;
6149{
6150 switch (regname)
6151 {
6152 case '%': /* file name */
6153 case '#': /* alternate file name */
6154 case '=': /* expression */
6155 case ':': /* last command line */
6156 case '/': /* last search-pattern */
6157 case '.': /* last inserted text */
6158#ifdef FEAT_SEARCHPATH
6159 case Ctrl_F: /* Filename under cursor */
6160 case Ctrl_P: /* Path under cursor, expand via "path" */
6161#endif
6162 case Ctrl_W: /* word under cursor */
6163 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6164 case '_': /* black hole: always empty */
6165 return MCHAR;
6166 }
6167
6168#ifdef FEAT_CLIPBOARD
6169 regname = may_get_selection(regname);
6170#endif
6171
Bram Moolenaar32b92012014-01-14 12:33:36 +01006172 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6173 return MAUTO;
6174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 get_yank_register(regname, FALSE);
6176
6177 if (y_current->y_array != NULL)
6178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (reglen != NULL && y_current->y_type == MBLOCK)
6180 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 return y_current->y_type;
6182 }
6183 return MAUTO;
6184}
6185
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006186static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6187
6188/*
6189 * When "flags" has GREG_LIST return a list with text "s".
6190 * Otherwise just return "s".
6191 */
6192 static char_u *
6193getreg_wrap_one_line(s, flags)
6194 char_u *s;
6195 int flags;
6196{
6197 if (flags & GREG_LIST)
6198 {
6199 list_T *list = list_alloc();
6200
6201 if (list != NULL)
6202 {
6203 if (list_append_string(list, NULL, -1) == FAIL)
6204 {
6205 list_free(list, TRUE);
6206 return NULL;
6207 }
6208 list->lv_first->li_tv.vval.v_string = s;
6209 }
6210 return (char_u *)list;
6211 }
6212 return s;
6213}
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215/*
6216 * Return the contents of a register as a single allocated string.
6217 * Used for "@r" in expressions and for getreg().
6218 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006219 * Flags:
6220 * GREG_NO_EXPR Do not allow expression register
6221 * GREG_EXPR_SRC For the expression register: return expression itself,
6222 * not the result of its evaluation.
6223 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 */
6225 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006226get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006228 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 long i;
6231 char_u *retval;
6232 int allocated;
6233 long len;
6234
6235 /* Don't allow using an expression register inside an expression */
6236 if (regname == '=')
6237 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006238 if (flags & GREG_NO_EXPR)
6239 return NULL;
6240 if (flags & GREG_EXPR_SRC)
6241 return getreg_wrap_one_line(get_expr_line_src(), flags);
6242 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244
6245 if (regname == '@') /* "@@" is used for unnamed register */
6246 regname = '"';
6247
6248 /* check for valid regname */
6249 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6250 return NULL;
6251
6252#ifdef FEAT_CLIPBOARD
6253 regname = may_get_selection(regname);
6254#endif
6255
6256 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6257 {
6258 if (retval == NULL)
6259 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006260 if (allocated)
6261 return getreg_wrap_one_line(retval, flags);
6262 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 }
6264
6265 get_yank_register(regname, FALSE);
6266 if (y_current->y_array == NULL)
6267 return NULL;
6268
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006269 if (flags & GREG_LIST)
6270 {
6271 list_T *list = list_alloc();
6272 int error = FALSE;
6273
6274 if (list == NULL)
6275 return NULL;
6276 for (i = 0; i < y_current->y_size; ++i)
6277 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6278 error = TRUE;
6279 if (error)
6280 {
6281 list_free(list, TRUE);
6282 return NULL;
6283 }
6284 return (char_u *)list;
6285 }
6286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 /*
6288 * Compute length of resulting string.
6289 */
6290 len = 0;
6291 for (i = 0; i < y_current->y_size; ++i)
6292 {
6293 len += (long)STRLEN(y_current->y_array[i]);
6294 /*
6295 * Insert a newline between lines and after last line if
6296 * y_type is MLINE.
6297 */
6298 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6299 ++len;
6300 }
6301
6302 retval = lalloc(len + 1, TRUE);
6303
6304 /*
6305 * Copy the lines of the yank register into the string.
6306 */
6307 if (retval != NULL)
6308 {
6309 len = 0;
6310 for (i = 0; i < y_current->y_size; ++i)
6311 {
6312 STRCPY(retval + len, y_current->y_array[i]);
6313 len += (long)STRLEN(retval + len);
6314
6315 /*
6316 * Insert a NL between lines and after the last line if y_type is
6317 * MLINE.
6318 */
6319 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6320 retval[len++] = '\n';
6321 }
6322 retval[len] = NUL;
6323 }
6324
6325 return retval;
6326}
6327
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006328 static int
6329init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6330 int name;
6331 struct yankreg **old_y_previous;
6332 struct yankreg **old_y_current;
6333 int must_append;
6334 int *yank_type UNUSED;
6335{
6336 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6337 {
6338 emsg_invreg(name);
6339 return FAIL;
6340 }
6341
6342 /* Don't want to change the current (unnamed) register */
6343 *old_y_previous = y_previous;
6344 *old_y_current = y_current;
6345
6346 get_yank_register(name, TRUE);
6347 if (!y_append && !must_append)
6348 free_yank_all();
6349 return OK;
6350}
6351
6352 static void
6353finish_write_reg(name, old_y_previous, old_y_current)
6354 int name;
6355 struct yankreg *old_y_previous;
6356 struct yankreg *old_y_current;
6357{
6358# ifdef FEAT_CLIPBOARD
6359 /* Send text of clipboard register to the clipboard. */
6360 may_set_selection();
6361# endif
6362
6363 /* ':let @" = "val"' should change the meaning of the "" register */
6364 if (name != '"')
6365 y_previous = old_y_previous;
6366 y_current = old_y_current;
6367}
6368
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369/*
6370 * Store string "str" in register "name".
6371 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6372 * If "must_append" is TRUE, always append to the register. Otherwise append
6373 * if "name" is an uppercase letter.
6374 * Note: "maxlen" and "must_append" don't work for the "/" register.
6375 * Careful: 'str' is modified, you may have to use a copy!
6376 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6377 */
6378 void
6379write_reg_contents(name, str, maxlen, must_append)
6380 int name;
6381 char_u *str;
6382 int maxlen;
6383 int must_append;
6384{
6385 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6386}
6387
6388 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006389write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6390 int name;
6391 char_u **strings;
6392 int maxlen UNUSED;
6393 int must_append;
6394 int yank_type;
6395 long block_len;
6396{
6397 struct yankreg *old_y_previous, *old_y_current;
6398
6399 if (name == '/'
6400#ifdef FEAT_EVAL
6401 || name == '='
6402#endif
6403 )
6404 {
6405 char_u *s;
6406
6407 if (strings[0] == NULL)
6408 s = (char_u *)"";
6409 else if (strings[1] != NULL)
6410 {
6411 EMSG(_("E883: search pattern and expression register may not "
6412 "contain two or more lines"));
6413 return;
6414 }
6415 else
6416 s = strings[0];
6417 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6418 return;
6419 }
6420
6421 if (name == '_') /* black hole: nothing to do */
6422 return;
6423
6424 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6425 &yank_type) == FAIL)
6426 return;
6427
6428 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6429
6430 finish_write_reg(name, old_y_previous, old_y_current);
6431}
6432
6433 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6435 int name;
6436 char_u *str;
6437 int maxlen;
6438 int must_append;
6439 int yank_type;
6440 long block_len;
6441{
6442 struct yankreg *old_y_previous, *old_y_current;
6443 long len;
6444
Bram Moolenaare7566042005-06-17 22:00:15 +00006445 if (maxlen >= 0)
6446 len = maxlen;
6447 else
6448 len = (long)STRLEN(str);
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 /* Special case: '/' search pattern */
6451 if (name == '/')
6452 {
6453 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6454 return;
6455 }
6456
Bram Moolenaare7566042005-06-17 22:00:15 +00006457#ifdef FEAT_EVAL
6458 if (name == '=')
6459 {
6460 char_u *p, *s;
6461
6462 p = vim_strnsave(str, (int)len);
6463 if (p == NULL)
6464 return;
6465 if (must_append)
6466 {
6467 s = concat_str(get_expr_line_src(), p);
6468 vim_free(p);
6469 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006470 }
6471 set_expr_line(p);
6472 return;
6473 }
6474#endif
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (name == '_') /* black hole: nothing to do */
6477 return;
6478
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006479 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6480 &yank_type) == FAIL)
6481 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006483 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006485 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486}
6487#endif /* FEAT_EVAL */
6488
6489#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6490/*
6491 * Put a string into a register. When the register is not empty, the string
6492 * is appended.
6493 */
6494 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006495str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006497 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 char_u *str; /* string to put in register */
6499 long len; /* length of string */
6500 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006501 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006503 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 int lnum;
6505 long start;
6506 long i;
6507 int extra;
6508 int newlines; /* number of lines added */
6509 int extraline = 0; /* extra line at the end */
6510 int append = FALSE; /* append to last line in register */
6511 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006512 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006516 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 y_ptr->y_size = 0;
6518
Bram Moolenaard44347f2011-06-19 01:14:29 +02006519 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006520 type = ((str_list || (len > 0 && (str[len - 1] == NL
6521 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006522 ? MLINE : MCHAR);
6523 else
6524 type = yank_type;
6525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 /*
6527 * Count the number of lines within the string
6528 */
6529 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006530 if (str_list)
6531 {
6532 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006535 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006537 for (i = 0; i < len; i++)
6538 if (str[i] == '\n')
6539 ++newlines;
6540 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6541 {
6542 extraline = 1;
6543 ++newlines; /* count extra newline at the end */
6544 }
6545 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6546 {
6547 append = TRUE;
6548 --newlines; /* uncount newline when appending first line */
6549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551
6552 /*
6553 * Allocate an array to hold the pointers to the new register lines.
6554 * If the register was not empty, move the existing lines to the new array.
6555 */
6556 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6557 * sizeof(char_u *), TRUE);
6558 if (pp == NULL) /* out of memory */
6559 return;
6560 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6561 pp[lnum] = y_ptr->y_array[lnum];
6562 vim_free(y_ptr->y_array);
6563 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
6566 /*
6567 * Find the end of each line and save it into the array.
6568 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006569 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006571 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6572 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006573 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006574 pp[lnum] = vim_strnsave(*ss, i);
6575 if (i > maxlen)
6576 maxlen = i;
6577 }
6578 }
6579 else
6580 {
6581 for (start = 0; start < len + extraline; start += i + 1)
6582 {
6583 for (i = start; i < len; ++i) /* find the end of the line */
6584 if (str[i] == '\n')
6585 break;
6586 i -= start; /* i is now length of line */
6587 if (i > maxlen)
6588 maxlen = i;
6589 if (append)
6590 {
6591 --lnum;
6592 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6593 }
6594 else
6595 extra = 0;
6596 s = alloc((unsigned)(i + extra + 1));
6597 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006599 if (extra)
6600 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6601 if (append)
6602 vim_free(y_ptr->y_array[lnum]);
6603 if (i)
6604 mch_memmove(s + extra, str + start, (size_t)i);
6605 extra += i;
6606 s[extra] = NUL;
6607 y_ptr->y_array[lnum++] = s;
6608 while (--extra >= 0)
6609 {
6610 if (*s == NUL)
6611 *s = '\n'; /* replace NUL with newline */
6612 ++s;
6613 }
6614 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
6617 y_ptr->y_type = type;
6618 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (type == MBLOCK)
6620 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6621 else
6622 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6625
6626 void
6627clear_oparg(oap)
6628 oparg_T *oap;
6629{
6630 vim_memset(oap, 0, sizeof(oparg_T));
6631}
6632
Bram Moolenaar7c626922005-02-07 22:01:03 +00006633static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006636 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 *
6638 * "Words" are counted by looking for boundaries between non-space and
6639 * space characters. (it seems to produce results that match 'wc'.)
6640 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006641 * Return value is byte count; word count for the line is added to "*wc".
6642 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 *
6644 * The function will only examine the first "limit" characters in the
6645 * line, stopping if it encounters an end-of-line (NUL byte). In that
6646 * case, eol_size will be added to the character count to account for
6647 * the size of the EOL character.
6648 */
6649 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006650line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 char_u *line;
6652 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006653 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 long limit;
6655 int eol_size;
6656{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006657 long i;
6658 long words = 0;
6659 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 int is_word = 0;
6661
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006662 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
6664 if (is_word)
6665 {
6666 if (vim_isspace(line[i]))
6667 {
6668 words++;
6669 is_word = 0;
6670 }
6671 }
6672 else if (!vim_isspace(line[i]))
6673 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006674 ++chars;
6675#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006676 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006677#else
6678 ++i;
6679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 }
6681
6682 if (is_word)
6683 words++;
6684 *wc += words;
6685
6686 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006687 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006690 chars += eol_size;
6691 }
6692 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 return i;
6694}
6695
6696/*
6697 * Give some info about the position of the cursor (for "g CTRL-G").
6698 * In Visual mode, give some info about the selected region. (In this case,
6699 * the *_count_cursor variables store running totals for the selection.)
6700 */
6701 void
6702cursor_pos_info()
6703{
6704 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006705 char_u buf1[50];
6706 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006708 long byte_count = 0;
6709 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 long char_count = 0;
6711 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 long word_count = 0;
6713 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006714 int eol_size;
6715 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 long line_count_selected = 0;
6717 pos_T min_pos, max_pos;
6718 oparg_T oparg;
6719 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 /*
6722 * Compute the length of the file in characters.
6723 */
6724 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6725 {
6726 MSG(_(no_lines_msg));
6727 }
6728 else
6729 {
6730 if (get_fileformat(curbuf) == EOL_DOS)
6731 eol_size = 2;
6732 else
6733 eol_size = 1;
6734
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 if (VIsual_active)
6736 {
6737 if (lt(VIsual, curwin->w_cursor))
6738 {
6739 min_pos = VIsual;
6740 max_pos = curwin->w_cursor;
6741 }
6742 else
6743 {
6744 min_pos = curwin->w_cursor;
6745 max_pos = VIsual;
6746 }
6747 if (*p_sel == 'e' && max_pos.col > 0)
6748 --max_pos.col;
6749
6750 if (VIsual_mode == Ctrl_V)
6751 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006752#ifdef FEAT_LINEBREAK
6753 char_u * saved_sbr = p_sbr;
6754
6755 /* Make 'sbr' empty for a moment to get the correct size. */
6756 p_sbr = empty_option;
6757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 oparg.is_VIsual = 1;
6759 oparg.block_mode = TRUE;
6760 oparg.op_type = OP_NOP;
6761 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006762 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006763#ifdef FEAT_LINEBREAK
6764 p_sbr = saved_sbr;
6765#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006766 if (curwin->w_curswant == MAXCOL)
6767 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 /* Swap the start, end vcol if needed */
6769 if (oparg.end_vcol < oparg.start_vcol)
6770 {
6771 oparg.end_vcol += oparg.start_vcol;
6772 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6773 oparg.end_vcol -= oparg.start_vcol;
6774 }
6775 }
6776 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
6779 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6780 {
6781 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006782 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {
6784 ui_breakcheck();
6785 if (got_int)
6786 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006787 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 }
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 /* Do extra processing for VIsual mode. */
6791 if (VIsual_active
6792 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6793 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006794 char_u *s = NULL;
6795 long len = 0L;
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 switch (VIsual_mode)
6798 {
6799 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006800#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006804#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006806#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006807 s = bd.textstart;
6808 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 break;
6810 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006811 s = ml_get(lnum);
6812 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 break;
6814 case 'v':
6815 {
6816 colnr_T start_col = (lnum == min_pos.lnum)
6817 ? min_pos.col : 0;
6818 colnr_T end_col = (lnum == max_pos.lnum)
6819 ? max_pos.col - start_col + 1 : MAXCOL;
6820
Bram Moolenaardef9e822004-12-31 20:58:58 +00006821 s = ml_get(lnum) + start_col;
6822 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
6824 break;
6825 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006826 if (s != NULL)
6827 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006828 byte_count_cursor += line_count_info(s, &word_count_cursor,
6829 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006830 if (lnum == curbuf->b_ml.ml_line_count
6831 && !curbuf->b_p_eol
6832 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006833 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006834 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {
6839 /* In non-visual mode, check for the line the cursor is on */
6840 if (lnum == curwin->w_cursor.lnum)
6841 {
6842 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006843 char_count_cursor += char_count;
6844 byte_count_cursor = byte_count +
6845 line_count_info(ml_get(lnum),
6846 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 (long)(curwin->w_cursor.col + 1), eol_size);
6848 }
6849 }
6850 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006851 byte_count += line_count_info(ml_get(lnum), &word_count,
6852 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 }
6854
6855 /* Correction for when last line doesn't have an EOL. */
6856 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006857 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 if (VIsual_active)
6860 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006861 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006864 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006865 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6867 }
6868 else
6869 buf1[0] = NUL;
6870
Bram Moolenaar7c626922005-02-07 22:01:03 +00006871 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006872 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006873 vim_snprintf((char *)IObuff, IOSIZE,
6874 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 buf1, line_count_selected,
6876 (long)curbuf->b_ml.ml_line_count,
6877 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006878 byte_count_cursor, byte_count);
6879 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006880 vim_snprintf((char *)IObuff, IOSIZE,
6881 _("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 +00006882 buf1, line_count_selected,
6883 (long)curbuf->b_ml.ml_line_count,
6884 word_count_cursor, word_count,
6885 char_count_cursor, char_count,
6886 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 p = ml_get_curline();
6891 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006892 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006894 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
6895 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
Bram Moolenaar7c626922005-02-07 22:01:03 +00006897 if (char_count_cursor == byte_count_cursor
6898 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006899 vim_snprintf((char *)IObuff, IOSIZE,
6900 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 (char *)buf1, (char *)buf2,
6902 (long)curwin->w_cursor.lnum,
6903 (long)curbuf->b_ml.ml_line_count,
6904 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006905 byte_count_cursor, byte_count);
6906 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006907 vim_snprintf((char *)IObuff, IOSIZE,
6908 _("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 +00006909 (char *)buf1, (char *)buf2,
6910 (long)curwin->w_cursor.lnum,
6911 (long)curbuf->b_ml.ml_line_count,
6912 word_count_cursor, word_count,
6913 char_count_cursor, char_count,
6914 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916
6917#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006918 byte_count = bomb_size();
6919 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006921 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922#endif
6923 /* Don't shorten this message, the user asked for it. */
6924 p = p_shm;
6925 p_shm = (char_u *)"";
6926 msg(IObuff);
6927 p_shm = p;
6928 }
6929}