blob: 86408a49448e6578f5a4c555294da541c04a6bfd [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
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200612#ifdef FEAT_MBYTE
613 if (has_mbyte && spaces > 0)
614 {
615 /* Avoid starting halfway a multi-byte character. */
616 if (b_insert)
617 {
618 int off = (*mb_head_off)(oldp, oldp + offset + spaces);
619 spaces -= off;
620 count -= off;
621 }
622 else
623 {
624 int off = (*mb_off_next)(oldp, oldp + offset);
625 offset += off;
626 spaces = 0;
627 count = 0;
628 }
629 }
630#endif
631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
633 if (newp == NULL)
634 continue;
635
636 /* copy up to shifted part */
637 mch_memmove(newp, oldp, (size_t)(offset));
638 oldp += offset;
639
640 /* insert pre-padding */
641 copy_spaces(newp + offset, (size_t)spaces);
642
643 /* copy the new text */
644 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
645 offset += s_len;
646
647 if (spaces && !bdp->is_short)
648 {
649 /* insert post-padding */
650 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
651 /* We're splitting a TAB, don't copy it. */
652 oldp++;
653 /* We allowed for that TAB, remember this now */
654 count++;
655 }
656
657 if (spaces > 0)
658 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000659 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 ml_replace(lnum, newp, FALSE);
662
663 if (lnum == oap->end.lnum)
664 {
665 /* Set "']" mark to the end of the block instead of the end of
666 * the insert in the first line. */
667 curbuf->b_op_end.lnum = oap->end.lnum;
668 curbuf->b_op_end.col = offset;
669 }
670 } /* for all lnum */
671
672 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
673
674 State = oldstate;
675}
676#endif
677
678#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
679/*
680 * op_reindent - handle reindenting a block of lines.
681 */
682 void
683op_reindent(oap, how)
684 oparg_T *oap;
685 int (*how) __ARGS((void));
686{
687 long i;
688 char_u *l;
689 int count;
690 linenr_T first_changed = 0;
691 linenr_T last_changed = 0;
692 linenr_T start_lnum = curwin->w_cursor.lnum;
693
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000694 /* Don't even try when 'modifiable' is off. */
695 if (!curbuf->b_p_ma)
696 {
697 EMSG(_(e_modifiable));
698 return;
699 }
700
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 for (i = oap->line_count; --i >= 0 && !got_int; )
702 {
703 /* it's a slow thing to do, so give feedback so there's no worry that
704 * the computer's just hung. */
705
706 if (i > 1
707 && (i % 50 == 0 || i == oap->line_count - 1)
708 && oap->line_count > p_report)
709 smsg((char_u *)_("%ld lines to indent... "), i);
710
711 /*
712 * Be vi-compatible: For lisp indenting the first line is not
713 * indented, unless there is only one line.
714 */
715#ifdef FEAT_LISP
716 if (i != oap->line_count - 1 || oap->line_count == 1
717 || how != get_lisp_indent)
718#endif
719 {
720 l = skipwhite(ml_get_curline());
721 if (*l == NUL) /* empty or blank line */
722 count = 0;
723 else
724 count = how(); /* get the indent for this line */
725
726 if (set_indent(count, SIN_UNDO))
727 {
728 /* did change the indent, call changed_lines() later */
729 if (first_changed == 0)
730 first_changed = curwin->w_cursor.lnum;
731 last_changed = curwin->w_cursor.lnum;
732 }
733 }
734 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000735 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737
738 /* put cursor on first non-blank of indented line */
739 curwin->w_cursor.lnum = start_lnum;
740 beginline(BL_SOL | BL_FIX);
741
742 /* Mark changed lines so that they will be redrawn. When Visual
743 * highlighting was present, need to continue until the last line. When
744 * there is no change still need to remove the Visual highlighting. */
745 if (last_changed != 0)
746 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 else if (oap->is_VIsual)
750 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
752 if (oap->line_count > p_report)
753 {
754 i = oap->line_count - (i + 1);
755 if (i == 1)
756 MSG(_("1 line indented "));
757 else
758 smsg((char_u *)_("%ld lines indented "), i);
759 }
760 /* set '[ and '] marks */
761 curbuf->b_op_start = oap->start;
762 curbuf->b_op_end = oap->end;
763}
764#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
765
766#if defined(FEAT_EVAL) || defined(PROTO)
767/*
768 * Keep the last expression line here, for repeating.
769 */
770static char_u *expr_line = NULL;
771
772/*
773 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
774 * Returns '=' when OK, NUL otherwise.
775 */
776 int
777get_expr_register()
778{
779 char_u *new_line;
780
781 new_line = getcmdline('=', 0L, 0);
782 if (new_line == NULL)
783 return NUL;
784 if (*new_line == NUL) /* use previous line */
785 vim_free(new_line);
786 else
787 set_expr_line(new_line);
788 return '=';
789}
790
791/*
792 * Set the expression for the '=' register.
793 * Argument must be an allocated string.
794 */
795 void
796set_expr_line(new_line)
797 char_u *new_line;
798{
799 vim_free(expr_line);
800 expr_line = new_line;
801}
802
803/*
804 * Get the result of the '=' register expression.
805 * Returns a pointer to allocated memory, or NULL for failure.
806 */
807 char_u *
808get_expr_line()
809{
810 char_u *expr_copy;
811 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000812 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 if (expr_line == NULL)
815 return NULL;
816
817 /* Make a copy of the expression, because evaluating it may cause it to be
818 * changed. */
819 expr_copy = vim_strsave(expr_line);
820 if (expr_copy == NULL)
821 return NULL;
822
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000823 /* When we are invoked recursively limit the evaluation to 10 levels.
824 * Then return the string as-is. */
825 if (nested >= 10)
826 return expr_copy;
827
828 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000830 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 vim_free(expr_copy);
832 return rv;
833}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000834
835/*
836 * Get the '=' register expression itself, without evaluating it.
837 */
838 char_u *
839get_expr_line_src()
840{
841 if (expr_line == NULL)
842 return NULL;
843 return vim_strsave(expr_line);
844}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#endif /* FEAT_EVAL */
846
847/*
848 * Check if 'regname' is a valid name of a yank register.
849 * Note: There is no check for 0 (default register), caller should do this
850 */
851 int
852valid_yank_reg(regname, writing)
853 int regname;
854 int writing; /* if TRUE check for writable registers */
855{
856 if ( (regname > 0 && ASCII_ISALNUM(regname))
857 || (!writing && vim_strchr((char_u *)
858#ifdef FEAT_EVAL
859 "/.%#:="
860#else
861 "/.%#:"
862#endif
863 , regname) != NULL)
864 || regname == '"'
865 || regname == '-'
866 || regname == '_'
867#ifdef FEAT_CLIPBOARD
868 || regname == '*'
869 || regname == '+'
870#endif
871#ifdef FEAT_DND
872 || (!writing && regname == '~')
873#endif
874 )
875 return TRUE;
876 return FALSE;
877}
878
879/*
880 * Set y_current and y_append, according to the value of "regname".
881 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000882 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 *
884 * If regname is 0 and writing, use register 0
885 * If regname is 0 and reading, use previous register
886 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000887 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888get_yank_register(regname, writing)
889 int regname;
890 int writing;
891{
892 int i;
893
894 y_append = FALSE;
895 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
896 {
897 y_current = y_previous;
898 return;
899 }
900 i = regname;
901 if (VIM_ISDIGIT(i))
902 i -= '0';
903 else if (ASCII_ISLOWER(i))
904 i = CharOrdLow(i) + 10;
905 else if (ASCII_ISUPPER(i))
906 {
907 i = CharOrdUp(i) + 10;
908 y_append = TRUE;
909 }
910 else if (regname == '-')
911 i = DELETION_REGISTER;
912#ifdef FEAT_CLIPBOARD
913 /* When selection is not available, use register 0 instead of '*' */
914 else if (clip_star.available && regname == '*')
915 i = STAR_REGISTER;
916 /* When clipboard is not available, use register 0 instead of '+' */
917 else if (clip_plus.available && regname == '+')
918 i = PLUS_REGISTER;
919#endif
920#ifdef FEAT_DND
921 else if (!writing && regname == '~')
922 i = TILDE_REGISTER;
923#endif
924 else /* not 0-9, a-z, A-Z or '-': use register 0 */
925 i = 0;
926 y_current = &(y_regs[i]);
927 if (writing) /* remember the register we write into for do_put() */
928 y_previous = y_current;
929}
930
Bram Moolenaar8299df92004-07-10 09:47:34 +0000931#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932/*
933 * When "regname" is a clipboard register, obtain the selection. If it's not
934 * available return zero, otherwise return "regname".
935 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000936 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937may_get_selection(regname)
938 int regname;
939{
940 if (regname == '*')
941 {
942 if (!clip_star.available)
943 regname = 0;
944 else
945 clip_get_selection(&clip_star);
946 }
947 else if (regname == '+')
948 {
949 if (!clip_plus.available)
950 regname = 0;
951 else
952 clip_get_selection(&clip_plus);
953 }
954 return regname;
955}
956#endif
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958/*
959 * Obtain the contents of a "normal" register. The register is made empty.
960 * The returned pointer has allocated memory, use put_register() later.
961 */
962 void *
963get_register(name, copy)
964 int name;
965 int copy; /* make a copy, if FALSE make register empty. */
966{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000967 struct yankreg *reg;
968 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970#ifdef FEAT_CLIPBOARD
971 /* When Visual area changed, may have to update selection. Obtain the
972 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000973 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200975 if (clip_isautosel_star())
976 clip_update_selection(&clip_star);
977 may_get_selection(name);
978 }
979 if (name == '+' && clip_plus.available)
980 {
981 if (clip_isautosel_plus())
982 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 may_get_selection(name);
984 }
985#endif
986
987 get_yank_register(name, 0);
988 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
989 if (reg != NULL)
990 {
991 *reg = *y_current;
992 if (copy)
993 {
994 /* If we run out of memory some or all of the lines are empty. */
995 if (reg->y_size == 0)
996 reg->y_array = NULL;
997 else
998 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
999 * reg->y_size));
1000 if (reg->y_array != NULL)
1001 {
1002 for (i = 0; i < reg->y_size; ++i)
1003 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1004 }
1005 }
1006 else
1007 y_current->y_array = NULL;
1008 }
1009 return (void *)reg;
1010}
1011
1012/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001013 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 */
1015 void
1016put_register(name, reg)
1017 int name;
1018 void *reg;
1019{
1020 get_yank_register(name, 0);
1021 free_yank_all();
1022 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001023 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001025#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 /* Send text written to clipboard register to the clipboard. */
1027 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001030
1031 void
1032free_register(reg)
1033 void *reg;
1034{
1035 struct yankreg tmp;
1036
1037 tmp = *y_current;
1038 *y_current = *(struct yankreg *)reg;
1039 free_yank_all();
1040 vim_free(reg);
1041 *y_current = tmp;
1042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044#if defined(FEAT_MOUSE) || defined(PROTO)
1045/*
1046 * return TRUE if the current yank register has type MLINE
1047 */
1048 int
1049yank_register_mline(regname)
1050 int regname;
1051{
1052 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1053 return FALSE;
1054 if (regname == '_') /* black hole is always empty */
1055 return FALSE;
1056 get_yank_register(regname, FALSE);
1057 return (y_current->y_type == MLINE);
1058}
1059#endif
1060
1061/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001062 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001064 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 */
1066 int
1067do_record(c)
1068 int c;
1069{
Bram Moolenaard55de222007-05-06 13:38:48 +00001070 char_u *p;
1071 static int regname;
1072 struct yankreg *old_y_previous, *old_y_current;
1073 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 if (Recording == FALSE) /* start recording */
1076 {
1077 /* registers 0-9, a-z and " are allowed */
1078 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1079 retval = FAIL;
1080 else
1081 {
1082 Recording = TRUE;
1083 showmode();
1084 regname = c;
1085 retval = OK;
1086 }
1087 }
1088 else /* stop recording */
1089 {
1090 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001091 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1092 * needs to be removed again to put it in a register. exec_reg then
1093 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
1095 Recording = FALSE;
1096 MSG("");
1097 p = get_recorded();
1098 if (p == NULL)
1099 retval = FAIL;
1100 else
1101 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001102 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1103 vim_unescape_csi(p);
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 /*
1106 * We don't want to change the default register here, so save and
1107 * restore the current register name.
1108 */
1109 old_y_previous = y_previous;
1110 old_y_current = y_current;
1111
1112 retval = stuff_yank(regname, p);
1113
1114 y_previous = old_y_previous;
1115 y_current = old_y_current;
1116 }
1117 }
1118 return retval;
1119}
1120
1121/*
1122 * Stuff string "p" into yank register "regname" as a single line (append if
1123 * uppercase). "p" must have been alloced.
1124 *
1125 * return FAIL for failure, OK otherwise
1126 */
1127 static int
1128stuff_yank(regname, p)
1129 int regname;
1130 char_u *p;
1131{
1132 char_u *lp;
1133 char_u **pp;
1134
1135 /* check for read-only register */
1136 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1137 {
1138 vim_free(p);
1139 return FAIL;
1140 }
1141 if (regname == '_') /* black hole: don't do anything */
1142 {
1143 vim_free(p);
1144 return OK;
1145 }
1146 get_yank_register(regname, TRUE);
1147 if (y_append && y_current->y_array != NULL)
1148 {
1149 pp = &(y_current->y_array[y_current->y_size - 1]);
1150 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1151 if (lp == NULL)
1152 {
1153 vim_free(p);
1154 return FAIL;
1155 }
1156 STRCPY(lp, *pp);
1157 STRCAT(lp, p);
1158 vim_free(p);
1159 vim_free(*pp);
1160 *pp = lp;
1161 }
1162 else
1163 {
1164 free_yank_all();
1165 if ((y_current->y_array =
1166 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1167 {
1168 vim_free(p);
1169 return FAIL;
1170 }
1171 y_current->y_array[0] = p;
1172 y_current->y_size = 1;
1173 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1174 }
1175 return OK;
1176}
1177
Bram Moolenaar42b94362009-05-26 16:12:37 +00001178static int execreg_lastc = NUL;
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180/*
1181 * execute a yank register: copy it into the stuff buffer
1182 *
1183 * return FAIL for failure, OK otherwise
1184 */
1185 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001186do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 int regname;
1188 int colon; /* insert ':' before each line */
1189 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001190 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 long i;
1193 char_u *p;
1194 int retval = OK;
1195 int remap;
1196
1197 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001198 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001199 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001200 {
1201 EMSG(_("E748: No previously used register"));
1202 return FAIL;
1203 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001204 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 /* check for valid regname */
1207 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001208 {
1209 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001211 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001212 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214#ifdef FEAT_CLIPBOARD
1215 regname = may_get_selection(regname);
1216#endif
1217
1218 if (regname == '_') /* black hole: don't stuff anything */
1219 return OK;
1220
1221#ifdef FEAT_CMDHIST
1222 if (regname == ':') /* use last command line */
1223 {
1224 if (last_cmdline == NULL)
1225 {
1226 EMSG(_(e_nolastcmd));
1227 return FAIL;
1228 }
1229 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1230 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001231 /* Escape all control characters with a CTRL-V */
1232 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001233 (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 +00001234 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001235 {
1236 /* When in Visual mode "'<,'>" will be prepended to the command.
1237 * Remove it when it's already there. */
1238 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001239 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001240 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001241 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001242 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001243 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245#endif
1246#ifdef FEAT_EVAL
1247 else if (regname == '=')
1248 {
1249 p = get_expr_line();
1250 if (p == NULL)
1251 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001252 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 vim_free(p);
1254 }
1255#endif
1256 else if (regname == '.') /* use last inserted text */
1257 {
1258 p = get_last_insert_save();
1259 if (p == NULL)
1260 {
1261 EMSG(_(e_noinstext));
1262 return FAIL;
1263 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001264 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 vim_free(p);
1266 }
1267 else
1268 {
1269 get_yank_register(regname, FALSE);
1270 if (y_current->y_array == NULL)
1271 return FAIL;
1272
1273 /* Disallow remaping for ":@r". */
1274 remap = colon ? REMAP_NONE : REMAP_YES;
1275
1276 /*
1277 * Insert lines into typeahead buffer, from last one to first one.
1278 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001279 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 for (i = y_current->y_size; --i >= 0; )
1281 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001282 char_u *escaped;
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 /* insert NL between lines and after last line if type is MLINE */
1285 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1286 || addcr)
1287 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001288 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 return FAIL;
1290 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001291 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1292 if (escaped == NULL)
1293 return FAIL;
1294 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1295 vim_free(escaped);
1296 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001298 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 == FAIL)
1300 return FAIL;
1301 }
1302 Exec_reg = TRUE; /* disable the 'q' command */
1303 }
1304 return retval;
1305}
1306
1307/*
1308 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1309 * used only after other typeahead has been processed.
1310 */
1311 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001312put_reedit_in_typebuf(silent)
1313 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 char_u buf[3];
1316
1317 if (restart_edit != NUL)
1318 {
1319 if (restart_edit == 'V')
1320 {
1321 buf[0] = 'g';
1322 buf[1] = 'R';
1323 buf[2] = NUL;
1324 }
1325 else
1326 {
1327 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1328 buf[1] = NUL;
1329 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001330 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 restart_edit = NUL;
1332 }
1333}
1334
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001335/*
1336 * Insert register contents "s" into the typeahead buffer, so that it will be
1337 * executed again.
1338 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1339 * no remapping.
1340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001342put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001344 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001346 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
1348 int retval = OK;
1349
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001350 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001352 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001354 {
1355 char_u *p;
1356
1357 if (esc)
1358 p = vim_strsave_escape_csi(s);
1359 else
1360 p = s;
1361 if (p == NULL)
1362 retval = FAIL;
1363 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001364 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1365 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001366 if (esc)
1367 vim_free(p);
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001370 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 return retval;
1372}
1373
1374/*
1375 * Insert a yank register: copy it into the Read buffer.
1376 * Used by CTRL-R command and middle mouse button in insert mode.
1377 *
1378 * return FAIL for failure, OK otherwise
1379 */
1380 int
1381insert_reg(regname, literally)
1382 int regname;
1383 int literally; /* insert literally, not as if typed */
1384{
1385 long i;
1386 int retval = OK;
1387 char_u *arg;
1388 int allocated;
1389
1390 /*
1391 * It is possible to get into an endless loop by having CTRL-R a in
1392 * register a and then, in insert mode, doing CTRL-R a.
1393 * If you hit CTRL-C, the loop will be broken here.
1394 */
1395 ui_breakcheck();
1396 if (got_int)
1397 return FAIL;
1398
1399 /* check for valid regname */
1400 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1401 return FAIL;
1402
1403#ifdef FEAT_CLIPBOARD
1404 regname = may_get_selection(regname);
1405#endif
1406
1407 if (regname == '.') /* insert last inserted text */
1408 retval = stuff_inserted(NUL, 1L, TRUE);
1409 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1410 {
1411 if (arg == NULL)
1412 return FAIL;
1413 stuffescaped(arg, literally);
1414 if (allocated)
1415 vim_free(arg);
1416 }
1417 else /* name or number register */
1418 {
1419 get_yank_register(regname, FALSE);
1420 if (y_current->y_array == NULL)
1421 retval = FAIL;
1422 else
1423 {
1424 for (i = 0; i < y_current->y_size; ++i)
1425 {
1426 stuffescaped(y_current->y_array[i], literally);
1427 /*
1428 * Insert a newline between lines and after last line if
1429 * y_type is MLINE.
1430 */
1431 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1432 stuffcharReadbuff('\n');
1433 }
1434 }
1435 }
1436
1437 return retval;
1438}
1439
1440/*
1441 * Stuff a string into the typeahead buffer, such that edit() will insert it
1442 * literally ("literally" TRUE) or interpret is as typed characters.
1443 */
1444 static void
1445stuffescaped(arg, literally)
1446 char_u *arg;
1447 int literally;
1448{
1449 int c;
1450 char_u *start;
1451
1452 while (*arg != NUL)
1453 {
1454 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1455 * stuff K_SPECIAL to get the effect of a special key when "literally"
1456 * is TRUE. */
1457 start = arg;
1458 while ((*arg >= ' '
1459#ifndef EBCDIC
1460 && *arg < DEL /* EBCDIC: chars above space are normal */
1461#endif
1462 )
1463 || (*arg == K_SPECIAL && !literally))
1464 ++arg;
1465 if (arg > start)
1466 stuffReadbuffLen(start, (long)(arg - start));
1467
1468 /* stuff a single special character */
1469 if (*arg != NUL)
1470 {
1471#ifdef FEAT_MBYTE
1472 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001473 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 else
1475#endif
1476 c = *arg++;
1477 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1478 stuffcharReadbuff(Ctrl_V);
1479 stuffcharReadbuff(c);
1480 }
1481 }
1482}
1483
1484/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001485 * If "regname" is a special register, return TRUE and store a pointer to its
1486 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001488 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489get_spec_reg(regname, argp, allocated, errmsg)
1490 int regname;
1491 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001492 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 int errmsg; /* give error message when failing */
1494{
1495 int cnt;
1496
1497 *argp = NULL;
1498 *allocated = FALSE;
1499 switch (regname)
1500 {
1501 case '%': /* file name */
1502 if (errmsg)
1503 check_fname(); /* will give emsg if not set */
1504 *argp = curbuf->b_fname;
1505 return TRUE;
1506
1507 case '#': /* alternate file name */
1508 *argp = getaltfname(errmsg); /* may give emsg if not set */
1509 return TRUE;
1510
1511#ifdef FEAT_EVAL
1512 case '=': /* result of expression */
1513 *argp = get_expr_line();
1514 *allocated = TRUE;
1515 return TRUE;
1516#endif
1517
1518 case ':': /* last command line */
1519 if (last_cmdline == NULL && errmsg)
1520 EMSG(_(e_nolastcmd));
1521 *argp = last_cmdline;
1522 return TRUE;
1523
1524 case '/': /* last search-pattern */
1525 if (last_search_pat() == NULL && errmsg)
1526 EMSG(_(e_noprevre));
1527 *argp = last_search_pat();
1528 return TRUE;
1529
1530 case '.': /* last inserted text */
1531 *argp = get_last_insert_save();
1532 *allocated = TRUE;
1533 if (*argp == NULL && errmsg)
1534 EMSG(_(e_noinstext));
1535 return TRUE;
1536
1537#ifdef FEAT_SEARCHPATH
1538 case Ctrl_F: /* Filename under cursor */
1539 case Ctrl_P: /* Path under cursor, expand via "path" */
1540 if (!errmsg)
1541 return FALSE;
1542 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001543 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 *allocated = TRUE;
1545 return TRUE;
1546#endif
1547
1548 case Ctrl_W: /* word under cursor */
1549 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1550 if (!errmsg)
1551 return FALSE;
1552 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1553 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1554 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1555 *allocated = TRUE;
1556 return TRUE;
1557
1558 case '_': /* black hole: always empty */
1559 *argp = (char_u *)"";
1560 return TRUE;
1561 }
1562
1563 return FALSE;
1564}
1565
1566/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001567 * Paste a yank register into the command line.
1568 * Only for non-special registers.
1569 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 * insert_reg() can't be used here, because special characters from the
1571 * register contents will be interpreted as commands.
1572 *
1573 * return FAIL for failure, OK otherwise
1574 */
1575 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001576cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 int regname;
1578 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001579 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 get_yank_register(regname, FALSE);
1584 if (y_current->y_array == NULL)
1585 return FAIL;
1586
1587 for (i = 0; i < y_current->y_size; ++i)
1588 {
1589 cmdline_paste_str(y_current->y_array[i], literally);
1590
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001591 /* Insert ^M between lines and after last line if type is MLINE.
1592 * Don't do this when "remcr" is TRUE and the next line is empty. */
1593 if (y_current->y_type == MLINE
1594 || (i < y_current->y_size - 1
1595 && !(remcr
1596 && i == y_current->y_size - 2
1597 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 cmdline_paste_str((char_u *)"\r", literally);
1599
1600 /* Check for CTRL-C, in case someone tries to paste a few thousand
1601 * lines and gets bored. */
1602 ui_breakcheck();
1603 if (got_int)
1604 return FAIL;
1605 }
1606 return OK;
1607}
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1610/*
1611 * Adjust the register name pointed to with "rp" for the clipboard being
1612 * used always and the clipboard being available.
1613 */
1614 void
1615adjust_clip_reg(rp)
1616 int *rp;
1617{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001618 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1619 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001620 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1621 {
1622 if (clip_unnamed != 0)
1623 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001624 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001625 else
1626 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1627 ? '+' : '*';
1628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (!clip_star.available && *rp == '*')
1630 *rp = 0;
1631 if (!clip_plus.available && *rp == '+')
1632 *rp = 0;
1633}
1634#endif
1635
1636/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001637 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001639 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 */
1641 int
1642op_delete(oap)
1643 oparg_T *oap;
1644{
1645 int n;
1646 linenr_T lnum;
1647 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 char_u *newp, *oldp;
1649 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1651 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001652 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1655 return OK;
1656
1657 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1658 if (oap->empty)
1659 return u_save_cursor();
1660
1661 if (!curbuf->b_p_ma)
1662 {
1663 EMSG(_(e_modifiable));
1664 return FAIL;
1665 }
1666
1667#ifdef FEAT_CLIPBOARD
1668 adjust_clip_reg(&oap->regname);
1669#endif
1670
1671#ifdef FEAT_MBYTE
1672 if (has_mbyte)
1673 mb_adjust_opend(oap);
1674#endif
1675
Bram Moolenaard04b7502010-07-08 22:27:55 +02001676 /*
1677 * Imitate the strange Vi behaviour: If the delete spans more than one
1678 * line and motion_type == MCHAR and the result is a blank line, make the
1679 * delete linewise. Don't do this for the change command or Visual mode.
1680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001683 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001685 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 && oap->op_type == OP_DELETE)
1687 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001688 ptr = ml_get(oap->end.lnum) + oap->end.col;
1689 if (*ptr != NUL)
1690 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 ptr = skipwhite(ptr);
1692 if (*ptr == NUL && inindent(0))
1693 oap->motion_type = MLINE;
1694 }
1695
Bram Moolenaard04b7502010-07-08 22:27:55 +02001696 /*
1697 * Check for trying to delete (e.g. "D") in an empty line.
1698 * Note: For the change operator it is ok.
1699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if ( oap->motion_type == MCHAR
1701 && oap->line_count == 1
1702 && oap->op_type == OP_DELETE
1703 && *ml_get(oap->start.lnum) == NUL)
1704 {
1705 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001706 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 * 'cpoptions' (Vi compatible).
1708 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001709#ifdef FEAT_VIRTUALEDIT
1710 if (virtual_op)
1711 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1712 * marks as if it happened. */
1713 goto setmarks;
1714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1716 beep_flush();
1717 return OK;
1718 }
1719
Bram Moolenaard04b7502010-07-08 22:27:55 +02001720 /*
1721 * Do a yank of whatever we're about to delete.
1722 * If a yank register was specified, put the deleted text into that
1723 * register. For the black hole register '_' don't yank anything.
1724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (oap->regname != '_')
1726 {
1727 if (oap->regname != 0)
1728 {
1729 /* check for read-only register */
1730 if (!valid_yank_reg(oap->regname, TRUE))
1731 {
1732 beep_flush();
1733 return OK;
1734 }
1735 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1736 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1737 did_yank = TRUE;
1738 }
1739
1740 /*
1741 * Put deleted text into register 1 and shift number registers if the
1742 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001743 * Use the register name from before adjust_clip_reg() may have
1744 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001746 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 || oap->line_count > 1 || oap->use_reg_one)
1748 {
1749 y_current = &y_regs[9];
1750 free_yank_all(); /* free register nine */
1751 for (n = 9; n > 1; --n)
1752 y_regs[n] = y_regs[n - 1];
1753 y_previous = y_current = &y_regs[1];
1754 y_regs[1].y_array = NULL; /* set register one to empty */
1755 if (op_yank(oap, TRUE, FALSE) == OK)
1756 did_yank = TRUE;
1757 }
1758
Bram Moolenaar84298db2012-04-20 13:46:08 +02001759 /* Yank into small delete register when no named register specified
1760 * and the delete is within one line. */
1761 if ((
1762#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001763 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1764 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001765#endif
1766 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 && oap->line_count == 1)
1768 {
1769 oap->regname = '-';
1770 get_yank_register(oap->regname, TRUE);
1771 if (op_yank(oap, TRUE, FALSE) == OK)
1772 did_yank = TRUE;
1773 oap->regname = 0;
1774 }
1775
1776 /*
1777 * If there's too much stuff to fit in the yank register, then get a
1778 * confirmation before doing the delete. This is crude, but simple.
1779 * And it avoids doing a delete of something we can't put back if we
1780 * want.
1781 */
1782 if (!did_yank)
1783 {
1784 int msg_silent_save = msg_silent;
1785
1786 msg_silent = 0; /* must display the prompt */
1787 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1788 msg_silent = msg_silent_save;
1789 if (n != 'y')
1790 {
1791 EMSG(_(e_abort));
1792 return FAIL;
1793 }
1794 }
1795 }
1796
Bram Moolenaard04b7502010-07-08 22:27:55 +02001797 /*
1798 * block mode delete
1799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (oap->block_mode)
1801 {
1802 if (u_save((linenr_T)(oap->start.lnum - 1),
1803 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1804 return FAIL;
1805
1806 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1807 {
1808 block_prep(oap, &bd, lnum, TRUE);
1809 if (bd.textlen == 0) /* nothing to delete */
1810 continue;
1811
1812 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1813 if (lnum == curwin->w_cursor.lnum)
1814 {
1815 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1816# ifdef FEAT_VIRTUALEDIT
1817 curwin->w_cursor.coladd = 0;
1818# endif
1819 }
1820
1821 /* n == number of chars deleted
1822 * If we delete a TAB, it may be replaced by several characters.
1823 * Thus the number of characters may increase!
1824 */
1825 n = bd.textlen - bd.startspaces - bd.endspaces;
1826 oldp = ml_get(lnum);
1827 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1828 if (newp == NULL)
1829 continue;
1830 /* copy up to deleted part */
1831 mch_memmove(newp, oldp, (size_t)bd.textcol);
1832 /* insert spaces */
1833 copy_spaces(newp + bd.textcol,
1834 (size_t)(bd.startspaces + bd.endspaces));
1835 /* copy the part after the deleted part */
1836 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001837 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 /* replace the line */
1839 ml_replace(lnum, newp, FALSE);
1840 }
1841
1842 check_cursor_col();
1843 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1844 oap->end.lnum + 1, 0L);
1845 oap->line_count = 0; /* no lines deleted */
1846 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001847 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849 if (oap->op_type == OP_CHANGE)
1850 {
1851 /* Delete the lines except the first one. Temporarily move the
1852 * cursor to the next line. Save the current line number, if the
1853 * last line is deleted it may be changed.
1854 */
1855 if (oap->line_count > 1)
1856 {
1857 lnum = curwin->w_cursor.lnum;
1858 ++curwin->w_cursor.lnum;
1859 del_lines((long)(oap->line_count - 1), TRUE);
1860 curwin->w_cursor.lnum = lnum;
1861 }
1862 if (u_save_cursor() == FAIL)
1863 return FAIL;
1864 if (curbuf->b_p_ai) /* don't delete indent */
1865 {
1866 beginline(BL_WHITE); /* cursor on first non-white */
1867 did_ai = TRUE; /* delete the indent when ESC hit */
1868 ai_col = curwin->w_cursor.col;
1869 }
1870 else
1871 beginline(0); /* cursor in column 0 */
1872 truncate_line(FALSE); /* delete the rest of the line */
1873 /* leave cursor past last char in line */
1874 if (oap->line_count > 1)
1875 u_clearline(); /* "U" command not possible after "2cc" */
1876 }
1877 else
1878 {
1879 del_lines(oap->line_count, TRUE);
1880 beginline(BL_WHITE | BL_FIX);
1881 u_clearline(); /* "U" command not possible after "dd" */
1882 }
1883 }
1884 else
1885 {
1886#ifdef FEAT_VIRTUALEDIT
1887 if (virtual_op)
1888 {
1889 int endcol = 0;
1890
1891 /* For virtualedit: break the tabs that are partly included. */
1892 if (gchar_pos(&oap->start) == '\t')
1893 {
1894 if (u_save_cursor() == FAIL) /* save first line for undo */
1895 return FAIL;
1896 if (oap->line_count == 1)
1897 endcol = getviscol2(oap->end.col, oap->end.coladd);
1898 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1899 oap->start = curwin->w_cursor;
1900 if (oap->line_count == 1)
1901 {
1902 coladvance(endcol);
1903 oap->end.col = curwin->w_cursor.col;
1904 oap->end.coladd = curwin->w_cursor.coladd;
1905 curwin->w_cursor = oap->start;
1906 }
1907 }
1908
1909 /* Break a tab only when it's included in the area. */
1910 if (gchar_pos(&oap->end) == '\t'
1911 && (int)oap->end.coladd < oap->inclusive)
1912 {
1913 /* save last line for undo */
1914 if (u_save((linenr_T)(oap->end.lnum - 1),
1915 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1916 return FAIL;
1917 curwin->w_cursor = oap->end;
1918 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1919 oap->end = curwin->w_cursor;
1920 curwin->w_cursor = oap->start;
1921 }
1922 }
1923#endif
1924
1925 if (oap->line_count == 1) /* delete characters within one line */
1926 {
1927 if (u_save_cursor() == FAIL) /* save line for undo */
1928 return FAIL;
1929
1930 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001931 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 && oap->op_type == OP_CHANGE
1933 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001934 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 display_dollar(oap->end.col - !oap->inclusive);
1936
1937 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1938
1939#ifdef FEAT_VIRTUALEDIT
1940 if (virtual_op)
1941 {
1942 /* fix up things for virtualedit-delete:
1943 * break the tabs which are going to get in our way
1944 */
1945 char_u *curline = ml_get_curline();
1946 int len = (int)STRLEN(curline);
1947
1948 if (oap->end.coladd != 0
1949 && (int)oap->end.col >= len - 1
1950 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1951 n++;
1952 /* Delete at least one char (e.g, when on a control char). */
1953 if (n == 0 && oap->start.coladd != oap->end.coladd)
1954 n = 1;
1955
1956 /* When deleted a char in the line, reset coladd. */
1957 if (gchar_cursor() != NUL)
1958 curwin->w_cursor.coladd = 0;
1959 }
1960#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001961 if (oap->op_type == OP_DELETE
1962 && oap->inclusive
1963 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001964 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1965 {
1966 /* Special case: gH<Del> deletes the last line. */
1967 del_lines(1L, FALSE);
1968 }
1969 else
1970 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001971 (void)del_bytes((long)n, !virtual_op,
1972 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975 else /* delete characters between lines */
1976 {
1977 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001978 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 /* save deleted and changed lines for undo */
1981 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1982 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1983 return FAIL;
1984
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001985 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 truncate_line(TRUE); /* delete from cursor to end of line */
1987
1988 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1989 ++curwin->w_cursor.lnum;
1990 del_lines((long)(oap->line_count - 2), FALSE);
1991
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001992 if (delete_last_line)
1993 oap->end.lnum = curbuf->b_ml.ml_line_count;
1994
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001995 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001996 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001997 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1998 {
1999 /* Special case: gH<Del> deletes the last line. */
2000 del_lines(1L, FALSE);
2001 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01002002 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2003 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002004 }
2005 else
2006 {
2007 /* delete from start of line until op_end */
2008 curwin->w_cursor.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002009 (void)del_bytes((long)n, !virtual_op,
2010 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002011 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2012 }
2013 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002014 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 }
2017
2018 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2019
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002020#ifdef FEAT_VIRTUALEDIT
2021setmarks:
2022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (oap->block_mode)
2024 {
2025 curbuf->b_op_end.lnum = oap->end.lnum;
2026 curbuf->b_op_end.col = oap->start.col;
2027 }
2028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 curbuf->b_op_end = oap->start;
2030 curbuf->b_op_start = oap->start;
2031
2032 return OK;
2033}
2034
2035#ifdef FEAT_MBYTE
2036/*
2037 * Adjust end of operating area for ending on a multi-byte character.
2038 * Used for deletion.
2039 */
2040 static void
2041mb_adjust_opend(oap)
2042 oparg_T *oap;
2043{
2044 char_u *p;
2045
2046 if (oap->inclusive)
2047 {
2048 p = ml_get(oap->end.lnum);
2049 oap->end.col += mb_tail_off(p, p + oap->end.col);
2050 }
2051}
2052#endif
2053
2054#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2055/*
2056 * Replace a whole area with one character.
2057 */
2058 int
2059op_replace(oap, c)
2060 oparg_T *oap;
2061 int c;
2062{
2063 int n, numc;
2064#ifdef FEAT_MBYTE
2065 int num_chars;
2066#endif
2067 char_u *newp, *oldp;
2068 size_t oldlen;
2069 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002070 char_u *after_p = NULL;
2071 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2074 return OK; /* nothing to do */
2075
Bram Moolenaard9820532013-11-04 01:41:17 +01002076 if (had_ctrl_v_cr)
2077 c = (c == -1 ? '\r' : '\n');
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#ifdef FEAT_MBYTE
2080 if (has_mbyte)
2081 mb_adjust_opend(oap);
2082#endif
2083
2084 if (u_save((linenr_T)(oap->start.lnum - 1),
2085 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2086 return FAIL;
2087
2088 /*
2089 * block mode replace
2090 */
2091 if (oap->block_mode)
2092 {
2093 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2094 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2095 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002096 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2098 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2099 continue; /* nothing to replace */
2100
2101 /* n == number of extra chars required
2102 * If we split a TAB, it may be replaced by several characters.
2103 * Thus the number of characters may increase!
2104 */
2105#ifdef FEAT_VIRTUALEDIT
2106 /* If the range starts in virtual space, count the initial
2107 * coladd offset as part of "startspaces" */
2108 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2109 {
2110 pos_T vpos;
2111
Bram Moolenaara1381de2009-11-03 15:44:21 +00002112 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 getvpos(&vpos, oap->start_vcol);
2114 bd.startspaces += vpos.coladd;
2115 n = bd.startspaces;
2116 }
2117 else
2118#endif
2119 /* allow for pre spaces */
2120 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2121
2122 /* allow for post spp */
2123 n += (bd.endspaces
2124#ifdef FEAT_VIRTUALEDIT
2125 && !bd.is_oneChar
2126#endif
2127 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2128 /* Figure out how many characters to replace. */
2129 numc = oap->end_vcol - oap->start_vcol + 1;
2130 if (bd.is_short && (!virtual_op || bd.is_MAX))
2131 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2132
2133#ifdef FEAT_MBYTE
2134 /* A double-wide character can be replaced only up to half the
2135 * times. */
2136 if ((*mb_char2cells)(c) > 1)
2137 {
2138 if ((numc & 1) && !bd.is_short)
2139 {
2140 ++bd.endspaces;
2141 ++n;
2142 }
2143 numc = numc / 2;
2144 }
2145
2146 /* Compute bytes needed, move character count to num_chars. */
2147 num_chars = numc;
2148 numc *= (*mb_char2len)(c);
2149#endif
2150 /* oldlen includes textlen, so don't double count */
2151 n += numc - bd.textlen;
2152
2153 oldp = ml_get_curline();
2154 oldlen = STRLEN(oldp);
2155 newp = alloc_check((unsigned)oldlen + 1 + n);
2156 if (newp == NULL)
2157 continue;
2158 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2159 /* copy up to deleted part */
2160 mch_memmove(newp, oldp, (size_t)bd.textcol);
2161 oldp += bd.textcol + bd.textlen;
2162 /* insert pre-spaces */
2163 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2164 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002165 /* -1/-2 is used for entering CR literally. */
2166 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002168#ifdef FEAT_MBYTE
2169 if (has_mbyte)
2170 {
2171 n = (int)STRLEN(newp);
2172 while (--num_chars >= 0)
2173 n += (*mb_char2bytes)(c, newp + n);
2174 }
2175 else
2176#endif
2177 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2178 if (!bd.is_short)
2179 {
2180 /* insert post-spaces */
2181 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2182 /* copy the part after the changed part */
2183 STRMOVE(newp + STRLEN(newp), oldp);
2184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002188 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002189 after_p = alloc_check(
2190 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002191 if (after_p != NULL)
2192 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194 /* replace the line */
2195 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002196 if (after_p != NULL)
2197 {
2198 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2199 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2200 oap->end.lnum++;
2201 vim_free(after_p);
2202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 }
2205 else
2206 {
2207 /*
2208 * MCHAR and MLINE motion replace.
2209 */
2210 if (oap->motion_type == MLINE)
2211 {
2212 oap->start.col = 0;
2213 curwin->w_cursor.col = 0;
2214 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2215 if (oap->end.col)
2216 --oap->end.col;
2217 }
2218 else if (!oap->inclusive)
2219 dec(&(oap->end));
2220
2221 while (ltoreq(curwin->w_cursor, oap->end))
2222 {
2223 n = gchar_cursor();
2224 if (n != NUL)
2225 {
2226#ifdef FEAT_MBYTE
2227 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2228 {
2229 /* This is slow, but it handles replacing a single-byte
2230 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002231 if (curwin->w_cursor.lnum == oap->end.lnum)
2232 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 n = State;
2234 State = REPLACE;
2235 ins_char(c);
2236 State = n;
2237 /* Backup to the replaced character. */
2238 dec_cursor();
2239 }
2240 else
2241#endif
2242 {
2243#ifdef FEAT_VIRTUALEDIT
2244 if (n == TAB)
2245 {
2246 int end_vcol = 0;
2247
2248 if (curwin->w_cursor.lnum == oap->end.lnum)
2249 {
2250 /* oap->end has to be recalculated when
2251 * the tab breaks */
2252 end_vcol = getviscol2(oap->end.col,
2253 oap->end.coladd);
2254 }
2255 coladvance_force(getviscol());
2256 if (curwin->w_cursor.lnum == oap->end.lnum)
2257 getvpos(&oap->end, end_vcol);
2258 }
2259#endif
2260 pchar(curwin->w_cursor, c);
2261 }
2262 }
2263#ifdef FEAT_VIRTUALEDIT
2264 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2265 {
2266 int virtcols = oap->end.coladd;
2267
2268 if (curwin->w_cursor.lnum == oap->start.lnum
2269 && oap->start.col == oap->end.col && oap->start.coladd)
2270 virtcols -= oap->start.coladd;
2271
2272 /* oap->end has been trimmed so it's effectively inclusive;
2273 * as a result an extra +1 must be counted so we don't
2274 * trample the NUL byte. */
2275 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2276 curwin->w_cursor.col -= (virtcols + 1);
2277 for (; virtcols >= 0; virtcols--)
2278 {
2279 pchar(curwin->w_cursor, c);
2280 if (inc(&curwin->w_cursor) == -1)
2281 break;
2282 }
2283 }
2284#endif
2285
2286 /* Advance to next character, stop at the end of the file. */
2287 if (inc_cursor() == -1)
2288 break;
2289 }
2290 }
2291
2292 curwin->w_cursor = oap->start;
2293 check_cursor();
2294 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2295
2296 /* Set "'[" and "']" marks. */
2297 curbuf->b_op_start = oap->start;
2298 curbuf->b_op_end = oap->end;
2299
2300 return OK;
2301}
2302#endif
2303
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002304static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2308 */
2309 void
2310op_tilde(oap)
2311 oparg_T *oap;
2312{
2313 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002315 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316
2317 if (u_save((linenr_T)(oap->start.lnum - 1),
2318 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2319 return;
2320
2321 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (oap->block_mode) /* Visual block mode */
2323 {
2324 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2325 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002326 int one_change;
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 block_prep(oap, &bd, pos.lnum, FALSE);
2329 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002330 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2331 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002332
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002333#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002334 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
2336 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2337
Bram Moolenaar009b2592004-10-24 19:18:58 +00002338 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2339 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002341 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345 if (did_change)
2346 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2347 }
2348 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 if (oap->motion_type == MLINE)
2351 {
2352 oap->start.col = 0;
2353 pos.col = 0;
2354 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2355 if (oap->end.col)
2356 --oap->end.col;
2357 }
2358 else if (!oap->inclusive)
2359 dec(&(oap->end));
2360
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002361 if (pos.lnum == oap->end.lnum)
2362 did_change = swapchars(oap->op_type, &pos,
2363 oap->end.col - pos.col + 1);
2364 else
2365 for (;;)
2366 {
2367 did_change |= swapchars(oap->op_type, &pos,
2368 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2369 (int)STRLEN(ml_get_pos(&pos)));
2370 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2371 break;
2372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (did_change)
2374 {
2375 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2376 0L);
2377#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002378 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
2380 char_u *ptr;
2381 int count;
2382
2383 pos = oap->start;
2384 while (pos.lnum < oap->end.lnum)
2385 {
2386 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002387 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002388 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002390 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 pos.col = 0;
2392 pos.lnum++;
2393 }
2394 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2395 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002396 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002398 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400#endif
2401 }
2402 }
2403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (!did_change && oap->is_VIsual)
2405 /* No change: need to remove the Visual selection */
2406 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 /*
2409 * Set '[ and '] marks.
2410 */
2411 curbuf->b_op_start = oap->start;
2412 curbuf->b_op_end = oap->end;
2413
2414 if (oap->line_count > p_report)
2415 {
2416 if (oap->line_count == 1)
2417 MSG(_("1 line changed"));
2418 else
2419 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2420 }
2421}
2422
2423/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002424 * Invoke swapchar() on "length" bytes at position "pos".
2425 * "pos" is advanced to just after the changed characters.
2426 * "length" is rounded up to include the whole last multi-byte character.
2427 * Also works correctly when the number of bytes changes.
2428 * Returns TRUE if some character was changed.
2429 */
2430 static int
2431swapchars(op_type, pos, length)
2432 int op_type;
2433 pos_T *pos;
2434 int length;
2435{
2436 int todo;
2437 int did_change = 0;
2438
2439 for (todo = length; todo > 0; --todo)
2440 {
2441# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002442 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002443 {
2444 int len = (*mb_ptr2len)(ml_get_pos(pos));
2445
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002446 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002447 if (len > 0)
2448 todo -= len - 1;
2449 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002450# endif
2451 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002452 if (inc(pos) == -1) /* at end of file */
2453 break;
2454 }
2455 return did_change;
2456}
2457
2458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * If op_type == OP_UPPER: make uppercase,
2460 * if op_type == OP_LOWER: make lowercase,
2461 * if op_type == OP_ROT13: do rot13 encoding,
2462 * else swap case of character at 'pos'
2463 * returns TRUE when something actually changed.
2464 */
2465 int
2466swapchar(op_type, pos)
2467 int op_type;
2468 pos_T *pos;
2469{
2470 int c;
2471 int nc;
2472
2473 c = gchar_pos(pos);
2474
2475 /* Only do rot13 encoding for ASCII characters. */
2476 if (c >= 0x80 && op_type == OP_ROT13)
2477 return FALSE;
2478
2479#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002480 if (op_type == OP_UPPER && c == 0xdf
2481 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002482 {
2483 pos_T sp = curwin->w_cursor;
2484
2485 /* Special handling of German sharp s: change to "SS". */
2486 curwin->w_cursor = *pos;
2487 del_char(FALSE);
2488 ins_char('S');
2489 ins_char('S');
2490 curwin->w_cursor = sp;
2491 inc(pos);
2492 }
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2495 return FALSE;
2496#endif
2497 nc = c;
2498 if (MB_ISLOWER(c))
2499 {
2500 if (op_type == OP_ROT13)
2501 nc = ROT13(c, 'a');
2502 else if (op_type != OP_LOWER)
2503 nc = MB_TOUPPER(c);
2504 }
2505 else if (MB_ISUPPER(c))
2506 {
2507 if (op_type == OP_ROT13)
2508 nc = ROT13(c, 'A');
2509 else if (op_type != OP_UPPER)
2510 nc = MB_TOLOWER(c);
2511 }
2512 if (nc != c)
2513 {
2514#ifdef FEAT_MBYTE
2515 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2516 {
2517 pos_T sp = curwin->w_cursor;
2518
2519 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002520 /* don't use del_char(), it also removes composing chars */
2521 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 ins_char(nc);
2523 curwin->w_cursor = sp;
2524 }
2525 else
2526#endif
2527 pchar(*pos, nc);
2528 return TRUE;
2529 }
2530 return FALSE;
2531}
2532
2533#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2534/*
2535 * op_insert - Insert and append operators for Visual mode.
2536 */
2537 void
2538op_insert(oap, count1)
2539 oparg_T *oap;
2540 long count1;
2541{
2542 long ins_len, pre_textlen = 0;
2543 char_u *firstline, *ins_text;
2544 struct block_def bd;
2545 int i;
2546
2547 /* edit() changes this - record it for OP_APPEND */
2548 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2549
2550 /* vis block is still marked. Get rid of it now. */
2551 curwin->w_cursor.lnum = oap->start.lnum;
2552 update_screen(INVERTED);
2553
2554 if (oap->block_mode)
2555 {
2556#ifdef FEAT_VIRTUALEDIT
2557 /* When 'virtualedit' is used, need to insert the extra spaces before
2558 * doing block_prep(). When only "block" is used, virtual edit is
2559 * already disabled, but still need it when calling
2560 * coladvance_force(). */
2561 if (curwin->w_cursor.coladd > 0)
2562 {
2563 int old_ve_flags = ve_flags;
2564
2565 ve_flags = VE_ALL;
2566 if (u_save_cursor() == FAIL)
2567 return;
2568 coladvance_force(oap->op_type == OP_APPEND
2569 ? oap->end_vcol + 1 : getviscol());
2570 if (oap->op_type == OP_APPEND)
2571 --curwin->w_cursor.col;
2572 ve_flags = old_ve_flags;
2573 }
2574#endif
2575 /* Get the info about the block before entering the text */
2576 block_prep(oap, &bd, oap->start.lnum, TRUE);
2577 firstline = ml_get(oap->start.lnum) + bd.textcol;
2578 if (oap->op_type == OP_APPEND)
2579 firstline += bd.textlen;
2580 pre_textlen = (long)STRLEN(firstline);
2581 }
2582
2583 if (oap->op_type == OP_APPEND)
2584 {
2585 if (oap->block_mode
2586#ifdef FEAT_VIRTUALEDIT
2587 && curwin->w_cursor.coladd == 0
2588#endif
2589 )
2590 {
2591 /* Move the cursor to the character right of the block. */
2592 curwin->w_set_curswant = TRUE;
2593 while (*ml_get_cursor() != NUL
2594 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2595 ++curwin->w_cursor.col;
2596 if (bd.is_short && !bd.is_MAX)
2597 {
2598 /* First line was too short, make it longer and adjust the
2599 * values in "bd". */
2600 if (u_save_cursor() == FAIL)
2601 return;
2602 for (i = 0; i < bd.endspaces; ++i)
2603 ins_char(' ');
2604 bd.textlen += bd.endspaces;
2605 }
2606 }
2607 else
2608 {
2609 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002610 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 /* Works just like an 'i'nsert on the next character. */
2613 if (!lineempty(curwin->w_cursor.lnum)
2614 && oap->start_vcol != oap->end_vcol)
2615 inc_cursor();
2616 }
2617 }
2618
2619 edit(NUL, FALSE, (linenr_T)count1);
2620
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002621 /* If user has moved off this line, we don't know what to do, so do
2622 * nothing.
2623 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2624 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return;
2626
2627 if (oap->block_mode)
2628 {
2629 struct block_def bd2;
2630
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002631 /* The user may have moved the cursor before inserting something, try
2632 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002633 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002634 {
2635 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002636 && oap->start.col
2637#ifdef FEAT_VIRTUALEDIT
2638 + oap->start.coladd
2639#endif
2640 != curbuf->b_op_start_orig.col
2641#ifdef FEAT_VIRTUALEDIT
2642 + curbuf->b_op_start_orig.coladd
2643#endif
2644 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002645 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002646 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002647 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2648 - oap->start_vcol;
2649 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2650 }
2651 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002652 && oap->end.col
2653#ifdef FEAT_VIRTUALEDIT
2654 + oap->end.coladd
2655#endif
2656 >= curbuf->b_op_start_orig.col
2657#ifdef FEAT_VIRTUALEDIT
2658 + curbuf->b_op_start_orig.coladd
2659#endif
2660 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002661 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002662 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002663 /* reset pre_textlen to the value of OP_INSERT */
2664 pre_textlen += bd.textlen;
2665 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2666 - oap->start_vcol;
2667 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2668 oap->op_type = OP_INSERT;
2669 }
2670 }
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /*
2673 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002674 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 * Don't do this when "$" used, end-of-line will have changed.
2676 */
2677 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2678 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2679 {
2680 if (oap->op_type == OP_APPEND)
2681 {
2682 pre_textlen += bd2.textlen - bd.textlen;
2683 if (bd2.endspaces)
2684 --bd2.textlen;
2685 }
2686 bd.textcol = bd2.textcol;
2687 bd.textlen = bd2.textlen;
2688 }
2689
2690 /*
2691 * Subsequent calls to ml_get() flush the firstline data - take a
2692 * copy of the required string.
2693 */
2694 firstline = ml_get(oap->start.lnum) + bd.textcol;
2695 if (oap->op_type == OP_APPEND)
2696 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002697 if (pre_textlen >= 0
2698 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 ins_text = vim_strnsave(firstline, (int)ins_len);
2701 if (ins_text != NULL)
2702 {
2703 /* block handled here */
2704 if (u_save(oap->start.lnum,
2705 (linenr_T)(oap->end.lnum + 1)) == OK)
2706 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2707 &bd);
2708
2709 curwin->w_cursor.col = oap->start.col;
2710 check_cursor();
2711 vim_free(ins_text);
2712 }
2713 }
2714 }
2715}
2716#endif
2717
2718/*
2719 * op_change - handle a change operation
2720 *
2721 * return TRUE if edit() returns because of a CTRL-O command
2722 */
2723 int
2724op_change(oap)
2725 oparg_T *oap;
2726{
2727 colnr_T l;
2728 int retval;
2729#ifdef FEAT_VISUALEXTRA
2730 long offset;
2731 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002732 long ins_len;
2733 long pre_textlen = 0;
2734 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 char_u *firstline;
2736 char_u *ins_text, *newp, *oldp;
2737 struct block_def bd;
2738#endif
2739
2740 l = oap->start.col;
2741 if (oap->motion_type == MLINE)
2742 {
2743 l = 0;
2744#ifdef FEAT_SMARTINDENT
2745 if (!p_paste && curbuf->b_p_si
2746# ifdef FEAT_CINDENT
2747 && !curbuf->b_p_cin
2748# endif
2749 )
2750 can_si = TRUE; /* It's like opening a new line, do si */
2751#endif
2752 }
2753
2754 /* First delete the text in the region. In an empty buffer only need to
2755 * save for undo */
2756 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2757 {
2758 if (u_save_cursor() == FAIL)
2759 return FALSE;
2760 }
2761 else if (op_delete(oap) == FAIL)
2762 return FALSE;
2763
2764 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2765 && !virtual_op)
2766 inc_cursor();
2767
2768#ifdef FEAT_VISUALEXTRA
2769 /* check for still on same line (<CR> in inserted text meaningless) */
2770 /* skip blank lines too */
2771 if (oap->block_mode)
2772 {
2773# ifdef FEAT_VIRTUALEDIT
2774 /* Add spaces before getting the current line length. */
2775 if (virtual_op && (curwin->w_cursor.coladd > 0
2776 || gchar_cursor() == NUL))
2777 coladvance_force(getviscol());
2778# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002779 firstline = ml_get(oap->start.lnum);
2780 pre_textlen = (long)STRLEN(firstline);
2781 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 bd.textcol = curwin->w_cursor.col;
2783 }
2784#endif
2785
2786#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2787 if (oap->motion_type == MLINE)
2788 fix_indent();
2789#endif
2790
2791 retval = edit(NUL, FALSE, (linenr_T)1);
2792
2793#ifdef FEAT_VISUALEXTRA
2794 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002795 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002797 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002799 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002801 /* Auto-indenting may have changed the indent. If the cursor was past
2802 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002804 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002806 long new_indent = (long)(skipwhite(firstline) - firstline);
2807
2808 pre_textlen += new_indent - pre_indent;
2809 bd.textcol += new_indent - pre_indent;
2810 }
2811
2812 ins_len = (long)STRLEN(firstline) - pre_textlen;
2813 if (ins_len > 0)
2814 {
2815 /* Subsequent calls to ml_get() flush the firstline data - take a
2816 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2818 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002819 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2821 linenr++)
2822 {
2823 block_prep(oap, &bd, linenr, TRUE);
2824 if (!bd.is_short || virtual_op)
2825 {
2826# ifdef FEAT_VIRTUALEDIT
2827 pos_T vpos;
2828
2829 /* If the block starts in virtual space, count the
2830 * initial coladd offset as part of "startspaces" */
2831 if (bd.is_short)
2832 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002833 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
2836 else
2837 vpos.coladd = 0;
2838# endif
2839 oldp = ml_get(linenr);
2840 newp = alloc_check((unsigned)(STRLEN(oldp)
2841# ifdef FEAT_VIRTUALEDIT
2842 + vpos.coladd
2843# endif
2844 + ins_len + 1));
2845 if (newp == NULL)
2846 continue;
2847 /* copy up to block start */
2848 mch_memmove(newp, oldp, (size_t)bd.textcol);
2849 offset = bd.textcol;
2850# ifdef FEAT_VIRTUALEDIT
2851 copy_spaces(newp + offset, (size_t)vpos.coladd);
2852 offset += vpos.coladd;
2853# endif
2854 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2855 offset += ins_len;
2856 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002857 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 ml_replace(linenr, newp, FALSE);
2859 }
2860 }
2861 check_cursor();
2862
2863 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2864 }
2865 vim_free(ins_text);
2866 }
2867 }
2868#endif
2869
2870 return retval;
2871}
2872
2873/*
2874 * set all the yank registers to empty (called from main())
2875 */
2876 void
2877init_yank()
2878{
2879 int i;
2880
2881 for (i = 0; i < NUM_REGISTERS; ++i)
2882 y_regs[i].y_array = NULL;
2883}
2884
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002885#if defined(EXITFREE) || defined(PROTO)
2886 void
2887clear_registers()
2888{
2889 int i;
2890
2891 for (i = 0; i < NUM_REGISTERS; ++i)
2892 {
2893 y_current = &y_regs[i];
2894 if (y_current->y_array != NULL)
2895 free_yank_all();
2896 }
2897}
2898#endif
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900/*
2901 * Free "n" lines from the current yank register.
2902 * Called for normal freeing and in case of error.
2903 */
2904 static void
2905free_yank(n)
2906 long n;
2907{
2908 if (y_current->y_array != NULL)
2909 {
2910 long i;
2911
2912 for (i = n; --i >= 0; )
2913 {
2914#ifdef AMIGA /* only for very slow machines */
2915 if ((i & 1023) == 1023) /* this may take a while */
2916 {
2917 /*
2918 * This message should never cause a hit-return message.
2919 * Overwrite this message with any next message.
2920 */
2921 ++no_wait_return;
2922 smsg((char_u *)_("freeing %ld lines"), i + 1);
2923 --no_wait_return;
2924 msg_didout = FALSE;
2925 msg_col = 0;
2926 }
2927#endif
2928 vim_free(y_current->y_array[i]);
2929 }
2930 vim_free(y_current->y_array);
2931 y_current->y_array = NULL;
2932#ifdef AMIGA
2933 if (n >= 1000)
2934 MSG("");
2935#endif
2936 }
2937}
2938
2939 static void
2940free_yank_all()
2941{
2942 free_yank(y_current->y_size);
2943}
2944
2945/*
2946 * Yank the text between "oap->start" and "oap->end" into a yank register.
2947 * If we are to append (uppercase register), we first yank into a new yank
2948 * register and then concatenate the old and the new one (so we keep the old
2949 * one in case of out-of-memory).
2950 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002951 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 */
2953 int
2954op_yank(oap, deleting, mess)
2955 oparg_T *oap;
2956 int deleting;
2957 int mess;
2958{
2959 long y_idx; /* index in y_array[] */
2960 struct yankreg *curr; /* copy of y_current */
2961 struct yankreg newreg; /* new yank register when appending */
2962 char_u **new_ptr;
2963 linenr_T lnum; /* current line number */
2964 long j;
2965 int yanktype = oap->motion_type;
2966 long yanklines = oap->line_count;
2967 linenr_T yankendlnum = oap->end.lnum;
2968 char_u *p;
2969 char_u *pnew;
2970 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002971#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002972 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975 /* check for read-only register */
2976 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2977 {
2978 beep_flush();
2979 return FAIL;
2980 }
2981 if (oap->regname == '_') /* black hole: nothing to do */
2982 return OK;
2983
2984#ifdef FEAT_CLIPBOARD
2985 if (!clip_star.available && oap->regname == '*')
2986 oap->regname = 0;
2987 else if (!clip_plus.available && oap->regname == '+')
2988 oap->regname = 0;
2989#endif
2990
2991 if (!deleting) /* op_delete() already set y_current */
2992 get_yank_register(oap->regname, TRUE);
2993
2994 curr = y_current;
2995 /* append to existing contents */
2996 if (y_append && y_current->y_array != NULL)
2997 y_current = &newreg;
2998 else
2999 free_yank_all(); /* free previously yanked lines */
3000
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003001 /*
3002 * If the cursor was in column 1 before and after the movement, and the
3003 * operator is not inclusive, the yank is always linewise.
3004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if ( oap->motion_type == MCHAR
3006 && oap->start.col == 0
3007 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003009 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 && oap->end.col == 0
3011 && yanklines > 1)
3012 {
3013 yanktype = MLINE;
3014 --yankendlnum;
3015 --yanklines;
3016 }
3017
3018 y_current->y_size = yanklines;
3019 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3022 yanklines), TRUE);
3023
3024 if (y_current->y_array == NULL)
3025 {
3026 y_current = curr;
3027 return FAIL;
3028 }
3029
3030 y_idx = 0;
3031 lnum = oap->start.lnum;
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (oap->block_mode)
3034 {
3035 /* Visual block mode */
3036 y_current->y_type = MBLOCK; /* set the yank register type */
3037 y_current->y_width = oap->end_vcol - oap->start_vcol;
3038
3039 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3040 y_current->y_width--;
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3044 {
3045 switch (y_current->y_type)
3046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 case MBLOCK:
3048 block_prep(oap, &bd, lnum, FALSE);
3049 if (yank_copy_line(&bd, y_idx) == FAIL)
3050 goto fail;
3051 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 case MLINE:
3054 if ((y_current->y_array[y_idx] =
3055 vim_strsave(ml_get(lnum))) == NULL)
3056 goto fail;
3057 break;
3058
3059 case MCHAR:
3060 {
3061 colnr_T startcol = 0, endcol = MAXCOL;
3062#ifdef FEAT_VIRTUALEDIT
3063 int is_oneChar = FALSE;
3064 colnr_T cs, ce;
3065#endif
3066 p = ml_get(lnum);
3067 bd.startspaces = 0;
3068 bd.endspaces = 0;
3069
3070 if (lnum == oap->start.lnum)
3071 {
3072 startcol = oap->start.col;
3073#ifdef FEAT_VIRTUALEDIT
3074 if (virtual_op)
3075 {
3076 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3077 if (ce != cs && oap->start.coladd > 0)
3078 {
3079 /* Part of a tab selected -- but don't
3080 * double-count it. */
3081 bd.startspaces = (ce - cs + 1)
3082 - oap->start.coladd;
3083 startcol++;
3084 }
3085 }
3086#endif
3087 }
3088
3089 if (lnum == oap->end.lnum)
3090 {
3091 endcol = oap->end.col;
3092#ifdef FEAT_VIRTUALEDIT
3093 if (virtual_op)
3094 {
3095 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3096 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3097# ifdef FEAT_MBYTE
3098 /* Don't add space for double-wide
3099 * char; endcol will be on last byte
3100 * of multi-byte char. */
3101 && (*mb_head_off)(p, p + endcol) == 0
3102# endif
3103 ))
3104 {
3105 if (oap->start.lnum == oap->end.lnum
3106 && oap->start.col == oap->end.col)
3107 {
3108 /* Special case: inside a single char */
3109 is_oneChar = TRUE;
3110 bd.startspaces = oap->end.coladd
3111 - oap->start.coladd + oap->inclusive;
3112 endcol = startcol;
3113 }
3114 else
3115 {
3116 bd.endspaces = oap->end.coladd
3117 + oap->inclusive;
3118 endcol -= oap->inclusive;
3119 }
3120 }
3121 }
3122#endif
3123 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003124 if (endcol == MAXCOL)
3125 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (startcol > endcol
3127#ifdef FEAT_VIRTUALEDIT
3128 || is_oneChar
3129#endif
3130 )
3131 bd.textlen = 0;
3132 else
3133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 bd.textlen = endcol - startcol + oap->inclusive;
3135 }
3136 bd.textstart = p + startcol;
3137 if (yank_copy_line(&bd, y_idx) == FAIL)
3138 goto fail;
3139 break;
3140 }
3141 /* NOTREACHED */
3142 }
3143 }
3144
3145 if (curr != y_current) /* append the new block to the old block */
3146 {
3147 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3148 (curr->y_size + y_current->y_size)), TRUE);
3149 if (new_ptr == NULL)
3150 goto fail;
3151 for (j = 0; j < curr->y_size; ++j)
3152 new_ptr[j] = curr->y_array[j];
3153 vim_free(curr->y_array);
3154 curr->y_array = new_ptr;
3155
3156 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3157 curr->y_type = MLINE;
3158
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003159 /* Concatenate the last line of the old block with the first line of
3160 * the new block, unless being Vi compatible. */
3161 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3164 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3165 if (pnew == NULL)
3166 {
3167 y_idx = y_current->y_size - 1;
3168 goto fail;
3169 }
3170 STRCPY(pnew, curr->y_array[--j]);
3171 STRCAT(pnew, y_current->y_array[0]);
3172 vim_free(curr->y_array[j]);
3173 vim_free(y_current->y_array[0]);
3174 curr->y_array[j++] = pnew;
3175 y_idx = 1;
3176 }
3177 else
3178 y_idx = 0;
3179 while (y_idx < y_current->y_size)
3180 curr->y_array[j++] = y_current->y_array[y_idx++];
3181 curr->y_size = j;
3182 vim_free(y_current->y_array);
3183 y_current = curr;
3184 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003185 if (curwin->w_p_rnu)
3186 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (mess) /* Display message about yank? */
3188 {
3189 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 && yanklines == 1)
3192 yanklines = 0;
3193 /* Some versions of Vi use ">=" here, some don't... */
3194 if (yanklines > p_report)
3195 {
3196 /* redisplay now, so message is not deleted */
3197 update_topline_redraw();
3198 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003199 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003200 if (oap->block_mode)
3201 MSG(_("block of 1 line yanked"));
3202 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003203 MSG(_("1 line yanked"));
3204 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003205 else if (oap->block_mode)
3206 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 else
3208 smsg((char_u *)_("%ld lines yanked"), yanklines);
3209 }
3210 }
3211
3212 /*
3213 * Set "'[" and "']" marks.
3214 */
3215 curbuf->b_op_start = oap->start;
3216 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003217 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003218 {
3219 curbuf->b_op_start.col = 0;
3220 curbuf->b_op_end.col = MAXCOL;
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223#ifdef FEAT_CLIPBOARD
3224 /*
3225 * If we were yanking to the '*' register, send result to clipboard.
3226 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3227 * to the '*' register.
3228 */
3229 if (clip_star.available
3230 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003231 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003232 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 if (curr != &(y_regs[STAR_REGISTER]))
3235 /* Copy the text from register 0 to the clipboard register. */
3236 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3237
3238 clip_own_selection(&clip_star);
3239 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003240# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003241 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244
3245# ifdef FEAT_X11
3246 /*
3247 * If we were yanking to the '+' register, send result to selection.
3248 * Also copy to the '*' register, in case auto-select is off.
3249 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003250 if (clip_plus.available
3251 && (curr == &(y_regs[PLUS_REGISTER])
3252 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003253 && ((clip_unnamed | clip_unnamed_saved) &
3254 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003256 if (curr != &(y_regs[PLUS_REGISTER]))
3257 /* Copy the text from register 0 to the clipboard register. */
3258 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 clip_own_selection(&clip_plus);
3261 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003262 if (!clip_isautosel_star() && !did_star
3263 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3266 clip_own_selection(&clip_star);
3267 clip_gen_set_selection(&clip_star);
3268 }
3269 }
3270# endif
3271#endif
3272
3273 return OK;
3274
3275fail: /* free the allocated lines */
3276 free_yank(y_idx + 1);
3277 y_current = curr;
3278 return FAIL;
3279}
3280
3281 static int
3282yank_copy_line(bd, y_idx)
3283 struct block_def *bd;
3284 long y_idx;
3285{
3286 char_u *pnew;
3287
3288 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3289 == NULL)
3290 return FAIL;
3291 y_current->y_array[y_idx] = pnew;
3292 copy_spaces(pnew, (size_t)bd->startspaces);
3293 pnew += bd->startspaces;
3294 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3295 pnew += bd->textlen;
3296 copy_spaces(pnew, (size_t)bd->endspaces);
3297 pnew += bd->endspaces;
3298 *pnew = NUL;
3299 return OK;
3300}
3301
3302#ifdef FEAT_CLIPBOARD
3303/*
3304 * Make a copy of the y_current register to register "reg".
3305 */
3306 static void
3307copy_yank_reg(reg)
3308 struct yankreg *reg;
3309{
3310 struct yankreg *curr = y_current;
3311 long j;
3312
3313 y_current = reg;
3314 free_yank_all();
3315 *y_current = *curr;
3316 y_current->y_array = (char_u **)lalloc_clear(
3317 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3318 if (y_current->y_array == NULL)
3319 y_current->y_size = 0;
3320 else
3321 for (j = 0; j < y_current->y_size; ++j)
3322 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3323 {
3324 free_yank(j);
3325 y_current->y_size = 0;
3326 break;
3327 }
3328 y_current = curr;
3329}
3330#endif
3331
3332/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003333 * Put contents of register "regname" into the text.
3334 * Caller must check "regname" to be valid!
3335 * "flags": PUT_FIXINDENT make indent look nice
3336 * PUT_CURSEND leave cursor after end of new text
3337 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
3339 void
3340do_put(regname, dir, count, flags)
3341 int regname;
3342 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3343 long count;
3344 int flags;
3345{
3346 char_u *ptr;
3347 char_u *newp, *oldp;
3348 int yanklen;
3349 int totlen = 0; /* init for gcc */
3350 linenr_T lnum;
3351 colnr_T col;
3352 long i; /* index in y_array[] */
3353 int y_type;
3354 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 int oldlen;
3356 long y_width = 0;
3357 colnr_T vcol;
3358 int delcount;
3359 int incr = 0;
3360 long j;
3361 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 char_u **y_array = NULL;
3363 long nr_lines = 0;
3364 pos_T new_cursor;
3365 int indent;
3366 int orig_indent = 0; /* init for gcc */
3367 int indent_diff = 0; /* init for gcc */
3368 int first_indent = TRUE;
3369 int lendiff = 0;
3370 pos_T old_pos;
3371 char_u *insert_string = NULL;
3372 int allocated = FALSE;
3373 long cnt;
3374
3375#ifdef FEAT_CLIPBOARD
3376 /* Adjust register name for "unnamed" in 'clipboard'. */
3377 adjust_clip_reg(&regname);
3378 (void)may_get_selection(regname);
3379#endif
3380
3381 if (flags & PUT_FIXINDENT)
3382 orig_indent = get_indent();
3383
3384 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3385 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3386
3387 /*
3388 * Using inserted text works differently, because the register includes
3389 * special characters (newlines, etc.).
3390 */
3391 if (regname == '.')
3392 {
3393 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3394 (count == -1 ? 'O' : 'i')), count, FALSE);
3395 /* Putting the text is done later, so can't really move the cursor to
3396 * the next character. Use "l" to simulate it. */
3397 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3398 stuffcharReadbuff('l');
3399 return;
3400 }
3401
3402 /*
3403 * For special registers '%' (file name), '#' (alternate file name) and
3404 * ':' (last command line), etc. we have to create a fake yank register.
3405 */
3406 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3407 {
3408 if (insert_string == NULL)
3409 return;
3410 }
3411
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003412#ifdef FEAT_AUTOCMD
3413 /* Autocommands may be executed when saving lines for undo, which may make
3414 * y_array invalid. Start undo now to avoid that. */
3415 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3416#endif
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (insert_string != NULL)
3419 {
3420 y_type = MCHAR;
3421#ifdef FEAT_EVAL
3422 if (regname == '=')
3423 {
3424 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003425 * characters.
3426 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 for (;;)
3428 {
3429 y_size = 0;
3430 ptr = insert_string;
3431 while (ptr != NULL)
3432 {
3433 if (y_array != NULL)
3434 y_array[y_size] = ptr;
3435 ++y_size;
3436 ptr = vim_strchr(ptr, '\n');
3437 if (ptr != NULL)
3438 {
3439 if (y_array != NULL)
3440 *ptr = NUL;
3441 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003442 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (*ptr == NUL)
3444 {
3445 y_type = MLINE;
3446 break;
3447 }
3448 }
3449 }
3450 if (y_array != NULL)
3451 break;
3452 y_array = (char_u **)alloc((unsigned)
3453 (y_size * sizeof(char_u *)));
3454 if (y_array == NULL)
3455 goto end;
3456 }
3457 }
3458 else
3459#endif
3460 {
3461 y_size = 1; /* use fake one-line yank register */
3462 y_array = &insert_string;
3463 }
3464 }
3465 else
3466 {
3467 get_yank_register(regname, FALSE);
3468
3469 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 y_size = y_current->y_size;
3472 y_array = y_current->y_array;
3473 }
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (y_type == MLINE)
3476 {
3477 if (flags & PUT_LINE_SPLIT)
3478 {
3479 /* "p" or "P" in Visual mode: split the lines to put the text in
3480 * between. */
3481 if (u_save_cursor() == FAIL)
3482 goto end;
3483 ptr = vim_strsave(ml_get_cursor());
3484 if (ptr == NULL)
3485 goto end;
3486 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3487 vim_free(ptr);
3488
3489 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3490 if (ptr == NULL)
3491 goto end;
3492 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3493 ++nr_lines;
3494 dir = FORWARD;
3495 }
3496 if (flags & PUT_LINE_FORWARD)
3497 {
3498 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003499 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 dir = FORWARD;
3501 }
3502 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3503 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3507 y_type = MLINE;
3508
3509 if (y_size == 0 || y_array == NULL)
3510 {
3511 EMSG2(_("E353: Nothing in register %s"),
3512 regname == 0 ? (char_u *)"\"" : transchar(regname));
3513 goto end;
3514 }
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (y_type == MBLOCK)
3517 {
3518 lnum = curwin->w_cursor.lnum + y_size + 1;
3519 if (lnum > curbuf->b_ml.ml_line_count)
3520 lnum = curbuf->b_ml.ml_line_count + 1;
3521 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3522 goto end;
3523 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003524 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 lnum = curwin->w_cursor.lnum;
3527#ifdef FEAT_FOLDING
3528 /* Correct line number for closed fold. Don't move the cursor yet,
3529 * u_save() uses it. */
3530 if (dir == BACKWARD)
3531 (void)hasFolding(lnum, &lnum, NULL);
3532 else
3533 (void)hasFolding(lnum, NULL, &lnum);
3534#endif
3535 if (dir == FORWARD)
3536 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003537 /* In an empty buffer the empty line is going to be replaced, include
3538 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003539 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 goto end;
3541#ifdef FEAT_FOLDING
3542 if (dir == FORWARD)
3543 curwin->w_cursor.lnum = lnum - 1;
3544 else
3545 curwin->w_cursor.lnum = lnum;
3546 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3547#endif
3548 }
3549 else if (u_save_cursor() == FAIL)
3550 goto end;
3551
3552 yanklen = (int)STRLEN(y_array[0]);
3553
3554#ifdef FEAT_VIRTUALEDIT
3555 if (ve_flags == VE_ALL && y_type == MCHAR)
3556 {
3557 if (gchar_cursor() == TAB)
3558 {
3559 /* Don't need to insert spaces when "p" on the last position of a
3560 * tab or "P" on the first position. */
3561 if (dir == FORWARD
3562 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3563 : curwin->w_cursor.coladd > 0)
3564 coladvance_force(getviscol());
3565 else
3566 curwin->w_cursor.coladd = 0;
3567 }
3568 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3569 coladvance_force(getviscol() + (dir == FORWARD));
3570 }
3571#endif
3572
3573 lnum = curwin->w_cursor.lnum;
3574 col = curwin->w_cursor.col;
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 /*
3577 * Block mode
3578 */
3579 if (y_type == MBLOCK)
3580 {
3581 char c = gchar_cursor();
3582 colnr_T endcol2 = 0;
3583
3584 if (dir == FORWARD && c != NUL)
3585 {
3586#ifdef FEAT_VIRTUALEDIT
3587 if (ve_flags == VE_ALL)
3588 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3589 else
3590#endif
3591 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3592
3593#ifdef FEAT_MBYTE
3594 if (has_mbyte)
3595 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003596 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 else
3598#endif
3599#ifdef FEAT_VIRTUALEDIT
3600 if (c != TAB || ve_flags != VE_ALL)
3601#endif
3602 ++curwin->w_cursor.col;
3603 ++col;
3604 }
3605 else
3606 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3607
3608#ifdef FEAT_VIRTUALEDIT
3609 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003610 if (ve_flags == VE_ALL
3611 && (curwin->w_cursor.coladd > 0
3612 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 if (dir == FORWARD && c == NUL)
3615 ++col;
3616 if (dir != FORWARD && c != NUL)
3617 ++curwin->w_cursor.col;
3618 if (c == TAB)
3619 {
3620 if (dir == BACKWARD && curwin->w_cursor.col)
3621 curwin->w_cursor.col--;
3622 if (dir == FORWARD && col - 1 == endcol2)
3623 curwin->w_cursor.col++;
3624 }
3625 }
3626 curwin->w_cursor.coladd = 0;
3627#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003628 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 for (i = 0; i < y_size; ++i)
3630 {
3631 int spaces;
3632 char shortline;
3633
3634 bd.startspaces = 0;
3635 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 vcol = 0;
3637 delcount = 0;
3638
3639 /* add a new line */
3640 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3641 {
3642 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3643 (colnr_T)1, FALSE) == FAIL)
3644 break;
3645 ++nr_lines;
3646 }
3647 /* get the old line and advance to the position to insert at */
3648 oldp = ml_get_curline();
3649 oldlen = (int)STRLEN(oldp);
3650 for (ptr = oldp; vcol < col && *ptr; )
3651 {
3652 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003653 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 vcol += incr;
3655 }
3656 bd.textcol = (colnr_T)(ptr - oldp);
3657
3658 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3659
3660 if (vcol < col) /* line too short, padd with spaces */
3661 bd.startspaces = col - vcol;
3662 else if (vcol > col)
3663 {
3664 bd.endspaces = vcol - col;
3665 bd.startspaces = incr - bd.endspaces;
3666 --bd.textcol;
3667 delcount = 1;
3668#ifdef FEAT_MBYTE
3669 if (has_mbyte)
3670 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3671#endif
3672 if (oldp[bd.textcol] != TAB)
3673 {
3674 /* Only a Tab can be split into spaces. Other
3675 * characters will have to be moved to after the
3676 * block, causing misalignment. */
3677 delcount = 0;
3678 bd.endspaces = 0;
3679 }
3680 }
3681
3682 yanklen = (int)STRLEN(y_array[i]);
3683
3684 /* calculate number of spaces required to fill right side of block*/
3685 spaces = y_width + 1;
3686 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003687 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (spaces < 0)
3689 spaces = 0;
3690
3691 /* insert the new text */
3692 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3693 newp = alloc_check((unsigned)totlen + oldlen + 1);
3694 if (newp == NULL)
3695 break;
3696 /* copy part up to cursor to new line */
3697 ptr = newp;
3698 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3699 ptr += bd.textcol;
3700 /* may insert some spaces before the new text */
3701 copy_spaces(ptr, (size_t)bd.startspaces);
3702 ptr += bd.startspaces;
3703 /* insert the new text */
3704 for (j = 0; j < count; ++j)
3705 {
3706 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3707 ptr += yanklen;
3708
3709 /* insert block's trailing spaces only if there's text behind */
3710 if ((j < count - 1 || !shortline) && spaces)
3711 {
3712 copy_spaces(ptr, (size_t)spaces);
3713 ptr += spaces;
3714 }
3715 }
3716 /* may insert some spaces after the new text */
3717 copy_spaces(ptr, (size_t)bd.endspaces);
3718 ptr += bd.endspaces;
3719 /* move the text after the cursor to the end of the line. */
3720 mch_memmove(ptr, oldp + bd.textcol + delcount,
3721 (size_t)(oldlen - bd.textcol - delcount + 1));
3722 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3723
3724 ++curwin->w_cursor.lnum;
3725 if (i == 0)
3726 curwin->w_cursor.col += bd.startspaces;
3727 }
3728
3729 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3730
3731 /* Set '[ mark. */
3732 curbuf->b_op_start = curwin->w_cursor;
3733 curbuf->b_op_start.lnum = lnum;
3734
3735 /* adjust '] mark */
3736 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3737 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003738# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (flags & PUT_CURSEND)
3742 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003743 colnr_T len;
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 curwin->w_cursor = curbuf->b_op_end;
3746 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003747
3748 /* in Insert mode we might be after the NUL, correct for that */
3749 len = (colnr_T)STRLEN(ml_get_curline());
3750 if (curwin->w_cursor.col > len)
3751 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753 else
3754 curwin->w_cursor.lnum = lnum;
3755 }
3756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758 /*
3759 * Character or Line mode
3760 */
3761 if (y_type == MCHAR)
3762 {
3763 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3764 * char */
3765 if (dir == FORWARD && gchar_cursor() != NUL)
3766 {
3767#ifdef FEAT_MBYTE
3768 if (has_mbyte)
3769 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003770 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 /* put it on the next of the multi-byte character. */
3773 col += bytelen;
3774 if (yanklen)
3775 {
3776 curwin->w_cursor.col += bytelen;
3777 curbuf->b_op_end.col += bytelen;
3778 }
3779 }
3780 else
3781#endif
3782 {
3783 ++col;
3784 if (yanklen)
3785 {
3786 ++curwin->w_cursor.col;
3787 ++curbuf->b_op_end.col;
3788 }
3789 }
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 curbuf->b_op_start = curwin->w_cursor;
3792 }
3793 /*
3794 * Line mode: BACKWARD is the same as FORWARD on the previous line
3795 */
3796 else if (dir == BACKWARD)
3797 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003798 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 /*
3801 * simple case: insert into current line
3802 */
3803 if (y_type == MCHAR && y_size == 1)
3804 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003805 do {
3806 totlen = count * yanklen;
3807 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003809 oldp = ml_get(lnum);
3810 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3811 if (newp == NULL)
3812 goto end; /* alloc() gave an error message */
3813 mch_memmove(newp, oldp, (size_t)col);
3814 ptr = newp + col;
3815 for (i = 0; i < count; ++i)
3816 {
3817 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3818 ptr += yanklen;
3819 }
3820 STRMOVE(ptr, oldp + col);
3821 ml_replace(lnum, newp, FALSE);
3822 /* Place cursor on last putted char. */
3823 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003824 {
3825 /* make sure curwin->w_virtcol is updated */
3826 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003827 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003830 if (VIsual_active)
3831 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003832 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 curbuf->b_op_end = curwin->w_cursor;
3835 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3836 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3837 ++curwin->w_cursor.col;
3838 changed_bytes(lnum, col);
3839 }
3840 else
3841 {
3842 /*
3843 * Insert at least one line. When y_type is MCHAR, break the first
3844 * line in two.
3845 */
3846 for (cnt = 1; cnt <= count; ++cnt)
3847 {
3848 i = 0;
3849 if (y_type == MCHAR)
3850 {
3851 /*
3852 * Split the current line in two at the insert position.
3853 * First insert y_array[size - 1] in front of second line.
3854 * Then append y_array[0] to first line.
3855 */
3856 lnum = new_cursor.lnum;
3857 ptr = ml_get(lnum) + col;
3858 totlen = (int)STRLEN(y_array[y_size - 1]);
3859 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3860 if (newp == NULL)
3861 goto error;
3862 STRCPY(newp, y_array[y_size - 1]);
3863 STRCAT(newp, ptr);
3864 /* insert second line */
3865 ml_append(lnum, newp, (colnr_T)0, FALSE);
3866 vim_free(newp);
3867
3868 oldp = ml_get(lnum);
3869 newp = alloc_check((unsigned)(col + yanklen + 1));
3870 if (newp == NULL)
3871 goto error;
3872 /* copy first part of line */
3873 mch_memmove(newp, oldp, (size_t)col);
3874 /* append to first line */
3875 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3876 ml_replace(lnum, newp, FALSE);
3877
3878 curwin->w_cursor.lnum = lnum;
3879 i = 1;
3880 }
3881
3882 for (; i < y_size; ++i)
3883 {
3884 if ((y_type != MCHAR || i < y_size - 1)
3885 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3886 == FAIL)
3887 goto error;
3888 lnum++;
3889 ++nr_lines;
3890 if (flags & PUT_FIXINDENT)
3891 {
3892 old_pos = curwin->w_cursor;
3893 curwin->w_cursor.lnum = lnum;
3894 ptr = ml_get(lnum);
3895 if (cnt == count && i == y_size - 1)
3896 lendiff = (int)STRLEN(ptr);
3897#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3898 if (*ptr == '#' && preprocs_left())
3899 indent = 0; /* Leave # lines at start */
3900 else
3901#endif
3902 if (*ptr == NUL)
3903 indent = 0; /* Ignore empty lines */
3904 else if (first_indent)
3905 {
3906 indent_diff = orig_indent - get_indent();
3907 indent = orig_indent;
3908 first_indent = FALSE;
3909 }
3910 else if ((indent = get_indent() + indent_diff) < 0)
3911 indent = 0;
3912 (void)set_indent(indent, 0);
3913 curwin->w_cursor = old_pos;
3914 /* remember how many chars were removed */
3915 if (cnt == count && i == y_size - 1)
3916 lendiff -= (int)STRLEN(ml_get(lnum));
3917 }
3918 }
3919 }
3920
3921error:
3922 /* Adjust marks. */
3923 if (y_type == MLINE)
3924 {
3925 curbuf->b_op_start.col = 0;
3926 if (dir == FORWARD)
3927 curbuf->b_op_start.lnum++;
3928 }
3929 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3930 (linenr_T)MAXLNUM, nr_lines, 0L);
3931
3932 /* note changed text for displaying and folding */
3933 if (y_type == MCHAR)
3934 changed_lines(curwin->w_cursor.lnum, col,
3935 curwin->w_cursor.lnum + 1, nr_lines);
3936 else
3937 changed_lines(curbuf->b_op_start.lnum, 0,
3938 curbuf->b_op_start.lnum, nr_lines);
3939
3940 /* put '] mark at last inserted character */
3941 curbuf->b_op_end.lnum = lnum;
3942 /* correct length for change in indent */
3943 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3944 if (col > 1)
3945 curbuf->b_op_end.col = col - 1;
3946 else
3947 curbuf->b_op_end.col = 0;
3948
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003949 if (flags & PUT_CURSLINE)
3950 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003951 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003952 curwin->w_cursor.lnum = lnum;
3953 beginline(BL_WHITE | BL_FIX);
3954 }
3955 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
3957 /* put cursor after inserted text */
3958 if (y_type == MLINE)
3959 {
3960 if (lnum >= curbuf->b_ml.ml_line_count)
3961 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3962 else
3963 curwin->w_cursor.lnum = lnum + 1;
3964 curwin->w_cursor.col = 0;
3965 }
3966 else
3967 {
3968 curwin->w_cursor.lnum = lnum;
3969 curwin->w_cursor.col = col;
3970 }
3971 }
3972 else if (y_type == MLINE)
3973 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003974 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 curwin->w_cursor.col = 0;
3976 if (dir == FORWARD)
3977 ++curwin->w_cursor.lnum;
3978 beginline(BL_WHITE | BL_FIX);
3979 }
3980 else /* put cursor on first inserted character */
3981 curwin->w_cursor = new_cursor;
3982 }
3983 }
3984
3985 msgmore(nr_lines);
3986 curwin->w_set_curswant = TRUE;
3987
3988end:
3989 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003991 if (regname == '=')
3992 vim_free(y_array);
3993
Bram Moolenaar033d8882013-09-25 23:24:57 +02003994 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003995
Bram Moolenaar677ee682005-01-27 14:41:15 +00003996 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003997 adjust_cursor_eol();
3998}
3999
4000/*
4001 * When the cursor is on the NUL past the end of the line and it should not be
4002 * there move it left.
4003 */
4004 void
4005adjust_cursor_eol()
4006{
4007 if (curwin->w_cursor.col > 0
4008 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004009#ifdef FEAT_VIRTUALEDIT
4010 && (ve_flags & VE_ONEMORE) == 0
4011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 && !(restart_edit || (State & INSERT)))
4013 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004014 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004015 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifdef FEAT_VIRTUALEDIT
4018 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004019 {
4020 colnr_T scol, ecol;
4021
4022 /* Coladd is set to the width of the last character. */
4023 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4024 curwin->w_cursor.coladd = ecol - scol + 1;
4025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#endif
4027 }
4028}
4029
4030#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4031/*
4032 * Return TRUE if lines starting with '#' should be left aligned.
4033 */
4034 int
4035preprocs_left()
4036{
4037 return
4038# ifdef FEAT_SMARTINDENT
4039# ifdef FEAT_CINDENT
4040 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4041# else
4042 curbuf->b_p_si
4043# endif
4044# endif
4045# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004046 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4047 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048# endif
4049 ;
4050}
4051#endif
4052
4053/* Return the character name of the register with the given number */
4054 int
4055get_register_name(num)
4056 int num;
4057{
4058 if (num == -1)
4059 return '"';
4060 else if (num < 10)
4061 return num + '0';
4062 else if (num == DELETION_REGISTER)
4063 return '-';
4064#ifdef FEAT_CLIPBOARD
4065 else if (num == STAR_REGISTER)
4066 return '*';
4067 else if (num == PLUS_REGISTER)
4068 return '+';
4069#endif
4070 else
4071 {
4072#ifdef EBCDIC
4073 int i;
4074
4075 /* EBCDIC is really braindead ... */
4076 i = 'a' + (num - 10);
4077 if (i > 'i')
4078 i += 7;
4079 if (i > 'r')
4080 i += 8;
4081 return i;
4082#else
4083 return num + 'a' - 10;
4084#endif
4085 }
4086}
4087
4088/*
4089 * ":dis" and ":registers": Display the contents of the yank registers.
4090 */
4091 void
4092ex_display(eap)
4093 exarg_T *eap;
4094{
4095 int i, n;
4096 long j;
4097 char_u *p;
4098 struct yankreg *yb;
4099 int name;
4100 int attr;
4101 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004102#ifdef FEAT_MBYTE
4103 int clen;
4104#else
4105# define clen 1
4106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 if (arg != NULL && *arg == NUL)
4109 arg = NULL;
4110 attr = hl_attr(HLF_8);
4111
4112 /* Highlight title */
4113 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4114 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4115 {
4116 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004117 if (arg != NULL && vim_strchr(arg, name) == NULL
4118#ifdef ONE_CLIPBOARD
4119 /* Star register and plus register contain the same thing. */
4120 && (name != '*' || vim_strchr(arg, '+') == NULL)
4121#endif
4122 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 continue; /* did not ask for this register */
4124
4125#ifdef FEAT_CLIPBOARD
4126 /* Adjust register name for "unnamed" in 'clipboard'.
4127 * When it's a clipboard register, fill it with the current contents
4128 * of the clipboard. */
4129 adjust_clip_reg(&name);
4130 (void)may_get_selection(name);
4131#endif
4132
4133 if (i == -1)
4134 {
4135 if (y_previous != NULL)
4136 yb = y_previous;
4137 else
4138 yb = &(y_regs[0]);
4139 }
4140 else
4141 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004142
4143#ifdef FEAT_EVAL
4144 if (name == MB_TOLOWER(redir_reg)
4145 || (redir_reg == '"' && yb == y_previous))
4146 continue; /* do not list register being written to, the
4147 * pointer can be freed */
4148#endif
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (yb->y_array != NULL)
4151 {
4152 msg_putchar('\n');
4153 msg_putchar('"');
4154 msg_putchar(name);
4155 MSG_PUTS(" ");
4156
4157 n = (int)Columns - 6;
4158 for (j = 0; j < yb->y_size && n > 1; ++j)
4159 {
4160 if (j)
4161 {
4162 MSG_PUTS_ATTR("^J", attr);
4163 n -= 2;
4164 }
4165 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004168 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004169#endif
4170 msg_outtrans_len(p, clen);
4171#ifdef FEAT_MBYTE
4172 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173#endif
4174 }
4175 }
4176 if (n > 1 && yb->y_type == MLINE)
4177 MSG_PUTS_ATTR("^J", attr);
4178 out_flush(); /* show one line at a time */
4179 }
4180 ui_breakcheck();
4181 }
4182
4183 /*
4184 * display last inserted text
4185 */
4186 if ((p = get_last_insert()) != NULL
4187 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4188 {
4189 MSG_PUTS("\n\". ");
4190 dis_msg(p, TRUE);
4191 }
4192
4193 /*
4194 * display last command line
4195 */
4196 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4197 && !got_int)
4198 {
4199 MSG_PUTS("\n\": ");
4200 dis_msg(last_cmdline, FALSE);
4201 }
4202
4203 /*
4204 * display current file name
4205 */
4206 if (curbuf->b_fname != NULL
4207 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4208 {
4209 MSG_PUTS("\n\"% ");
4210 dis_msg(curbuf->b_fname, FALSE);
4211 }
4212
4213 /*
4214 * display alternate file name
4215 */
4216 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4217 {
4218 char_u *fname;
4219 linenr_T dummy;
4220
4221 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4222 {
4223 MSG_PUTS("\n\"# ");
4224 dis_msg(fname, FALSE);
4225 }
4226 }
4227
4228 /*
4229 * display last search pattern
4230 */
4231 if (last_search_pat() != NULL
4232 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4233 {
4234 MSG_PUTS("\n\"/ ");
4235 dis_msg(last_search_pat(), FALSE);
4236 }
4237
4238#ifdef FEAT_EVAL
4239 /*
4240 * display last used expression
4241 */
4242 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4243 && !got_int)
4244 {
4245 MSG_PUTS("\n\"= ");
4246 dis_msg(expr_line, FALSE);
4247 }
4248#endif
4249}
4250
4251/*
4252 * display a string for do_dis()
4253 * truncate at end of screen line
4254 */
4255 static void
4256dis_msg(p, skip_esc)
4257 char_u *p;
4258 int skip_esc; /* if TRUE, ignore trailing ESC */
4259{
4260 int n;
4261#ifdef FEAT_MBYTE
4262 int l;
4263#endif
4264
4265 n = (int)Columns - 6;
4266 while (*p != NUL
4267 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4268 && (n -= ptr2cells(p)) >= 0)
4269 {
4270#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004271 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
4273 msg_outtrans_len(p, l);
4274 p += l;
4275 }
4276 else
4277#endif
4278 msg_outtrans_len(p++, 1);
4279 }
4280 ui_breakcheck();
4281}
4282
Bram Moolenaar81340392012-06-06 16:12:59 +02004283#if defined(FEAT_COMMENTS) || defined(PROTO)
4284/*
4285 * If "process" is TRUE and the line begins with a comment leader (possibly
4286 * after some white space), return a pointer to the text after it. Put a boolean
4287 * value indicating whether the line ends with an unclosed comment in
4288 * "is_comment".
4289 * line - line to be processed,
4290 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004291 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004292 * include_space - whether to also skip space following the comment leader,
4293 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004294 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004295 */
4296 static char_u *
4297skip_comment(line, process, include_space, is_comment)
4298 char_u *line;
4299 int process;
4300 int include_space;
4301 int *is_comment;
4302{
4303 char_u *comment_flags = NULL;
4304 int lead_len;
4305 int leader_offset = get_last_leader_offset(line, &comment_flags);
4306
4307 *is_comment = FALSE;
4308 if (leader_offset != -1)
4309 {
4310 /* Let's check whether the line ends with an unclosed comment.
4311 * If the last comment leader has COM_END in flags, there's no comment.
4312 */
4313 while (*comment_flags)
4314 {
4315 if (*comment_flags == COM_END
4316 || *comment_flags == ':')
4317 break;
4318 ++comment_flags;
4319 }
4320 if (*comment_flags != COM_END)
4321 *is_comment = TRUE;
4322 }
4323
4324 if (process == FALSE)
4325 return line;
4326
4327 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4328
4329 if (lead_len == 0)
4330 return line;
4331
4332 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004333 * - COM_END,
4334 * - colon,
4335 * whichever comes first.
4336 */
4337 while (*comment_flags)
4338 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004339 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004340 || *comment_flags == ':')
4341 {
4342 break;
4343 }
4344 ++comment_flags;
4345 }
4346
4347 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004348 * starting with a closing part of a three-part comment. That's good,
4349 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004350 */
4351 if (*comment_flags == ':' || *comment_flags == NUL)
4352 line += lead_len;
4353
4354 return line;
4355}
4356#endif
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004359 * Join 'count' lines (minimal 2) at cursor position.
4360 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004361 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4362 * leaders should not be removed.
4363 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4364 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004366 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
4368 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004369do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004370 long count;
4371 int insert_space;
4372 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004373 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004374 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004376 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004377 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004378 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004380 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004381 int endcurr1 = NUL;
4382 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004383 int currsize = 0; /* size of the current line */
4384 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004386 colnr_T col = 0;
4387 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004388#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004389 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004390 int remove_comments = (use_formatoptions == TRUE)
4391 && has_format_option(FO_REMOVE_COMS);
4392 int prev_was_comment;
4393#endif
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004396 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4397 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004400 /* Allocate an array to store the number of spaces inserted before each
4401 * line. We will use it to pre-compute the length of the new line and the
4402 * proper placement of each original line in the new one. */
4403 spaces = lalloc_clear((long_u)count, TRUE);
4404 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004406#if defined(FEAT_COMMENTS) || defined(PROTO)
4407 if (remove_comments)
4408 {
4409 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4410 if (comments == NULL)
4411 {
4412 vim_free(spaces);
4413 return FAIL;
4414 }
4415 }
4416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004419 * Don't move anything, just compute the final line length
4420 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004422 for (t = 0; t < count; ++t)
4423 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004424 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004425 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004426 {
4427 /* Set the '[ mark. */
4428 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4429 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4430 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004431#if defined(FEAT_COMMENTS) || defined(PROTO)
4432 if (remove_comments)
4433 {
4434 /* We don't want to remove the comment leader if the
4435 * previous line is not a comment. */
4436 if (t > 0 && prev_was_comment)
4437 {
4438
4439 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4440 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004441 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004442 curr = new_curr;
4443 }
4444 else
4445 curr = skip_comment(curr, FALSE, insert_space,
4446 &prev_was_comment);
4447 }
4448#endif
4449
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004450 if (insert_space && t > 0)
4451 {
4452 curr = skipwhite(curr);
4453 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4454#ifdef FEAT_MBYTE
4455 && (!has_format_option(FO_MBYTE_JOIN)
4456 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4457 && (!has_format_option(FO_MBYTE_JOIN2)
4458 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4459#endif
4460 )
4461 {
4462 /* don't add a space if the line is ending in a space */
4463 if (endcurr1 == ' ')
4464 endcurr1 = endcurr2;
4465 else
4466 ++spaces[t];
4467 /* extra space when 'joinspaces' set and line ends in '.' */
4468 if ( p_js
4469 && (endcurr1 == '.'
4470 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4471 && (endcurr1 == '?' || endcurr1 == '!'))))
4472 ++spaces[t];
4473 }
4474 }
4475 currsize = (int)STRLEN(curr);
4476 sumsize += currsize + spaces[t];
4477 endcurr1 = endcurr2 = NUL;
4478 if (insert_space && currsize > 0)
4479 {
4480#ifdef FEAT_MBYTE
4481 if (has_mbyte)
4482 {
4483 cend = curr + currsize;
4484 mb_ptr_back(curr, cend);
4485 endcurr1 = (*mb_ptr2char)(cend);
4486 if (cend > curr)
4487 {
4488 mb_ptr_back(curr, cend);
4489 endcurr2 = (*mb_ptr2char)(cend);
4490 }
4491 }
4492 else
4493#endif
4494 {
4495 endcurr1 = *(curr + currsize - 1);
4496 if (currsize > 1)
4497 endcurr2 = *(curr + currsize - 2);
4498 }
4499 }
4500 line_breakcheck();
4501 if (got_int)
4502 {
4503 ret = FAIL;
4504 goto theend;
4505 }
4506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004508 /* store the column position before last line */
4509 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004511 /* allocate the space for the new line */
4512 newp = alloc_check((unsigned)(sumsize + 1));
4513 cend = newp + sumsize;
4514 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004516 /*
4517 * Move affected lines to the new long one.
4518 *
4519 * Move marks from each deleted line to the joined line, adjusting the
4520 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4521 * should not really be a problem.
4522 */
4523 for (t = count - 1; ; --t)
4524 {
4525 cend -= currsize;
4526 mch_memmove(cend, curr, (size_t)currsize);
4527 if (spaces[t] > 0)
4528 {
4529 cend -= spaces[t];
4530 copy_spaces(cend, (size_t)(spaces[t]));
4531 }
4532 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004533 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004534 if (t == 0)
4535 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004536 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004537#if defined(FEAT_COMMENTS) || defined(PROTO)
4538 if (remove_comments)
4539 curr += comments[t - 1];
4540#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004541 if (insert_space && t > 1)
4542 curr = skipwhite(curr);
4543 currsize = (int)STRLEN(curr);
4544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4546
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004547 if (setmark)
4548 {
4549 /* Set the '] mark. */
4550 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4551 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4552 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 /* Only report the change in the first line here, del_lines() will report
4555 * the deleted line. */
4556 changed_lines(curwin->w_cursor.lnum, currsize,
4557 curwin->w_cursor.lnum + 1, 0L);
4558
4559 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004560 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 * briefly, and then move it back. After del_lines() the cursor may
4562 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 */
4564 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004566 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 curwin->w_cursor.lnum = t;
4568
4569 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004570 * Set the cursor column:
4571 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004572 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004574 curwin->w_cursor.col =
4575 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#ifdef FEAT_VIRTUALEDIT
4579 curwin->w_cursor.coladd = 0;
4580#endif
4581 curwin->w_set_curswant = TRUE;
4582
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004583theend:
4584 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004585#if defined(FEAT_COMMENTS) || defined(PROTO)
4586 if (remove_comments)
4587 vim_free(comments);
4588#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004589 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590}
4591
4592#ifdef FEAT_COMMENTS
4593/*
4594 * Return TRUE if the two comment leaders given are the same. "lnum" is
4595 * the first line. White-space is ignored. Note that the whole of
4596 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4597 */
4598 static int
4599same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4600 linenr_T lnum;
4601 int leader1_len;
4602 char_u *leader1_flags;
4603 int leader2_len;
4604 char_u *leader2_flags;
4605{
4606 int idx1 = 0, idx2 = 0;
4607 char_u *p;
4608 char_u *line1;
4609 char_u *line2;
4610
4611 if (leader1_len == 0)
4612 return (leader2_len == 0);
4613
4614 /*
4615 * If first leader has 'f' flag, the lines can be joined only if the
4616 * second line does not have a leader.
4617 * If first leader has 'e' flag, the lines can never be joined.
4618 * If fist leader has 's' flag, the lines can only be joined if there is
4619 * some text after it and the second line has the 'm' flag.
4620 */
4621 if (leader1_flags != NULL)
4622 {
4623 for (p = leader1_flags; *p && *p != ':'; ++p)
4624 {
4625 if (*p == COM_FIRST)
4626 return (leader2_len == 0);
4627 if (*p == COM_END)
4628 return FALSE;
4629 if (*p == COM_START)
4630 {
4631 if (*(ml_get(lnum) + leader1_len) == NUL)
4632 return FALSE;
4633 if (leader2_flags == NULL || leader2_len == 0)
4634 return FALSE;
4635 for (p = leader2_flags; *p && *p != ':'; ++p)
4636 if (*p == COM_MIDDLE)
4637 return TRUE;
4638 return FALSE;
4639 }
4640 }
4641 }
4642
4643 /*
4644 * Get current line and next line, compare the leaders.
4645 * The first line has to be saved, only one line can be locked at a time.
4646 */
4647 line1 = vim_strsave(ml_get(lnum));
4648 if (line1 != NULL)
4649 {
4650 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4651 ;
4652 line2 = ml_get(lnum + 1);
4653 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4654 {
4655 if (!vim_iswhite(line2[idx2]))
4656 {
4657 if (line1[idx1++] != line2[idx2])
4658 break;
4659 }
4660 else
4661 while (vim_iswhite(line1[idx1]))
4662 ++idx1;
4663 }
4664 vim_free(line1);
4665 }
4666 return (idx2 == leader2_len && idx1 == leader1_len);
4667}
4668#endif
4669
4670/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004671 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 */
4673 void
4674op_format(oap, keep_cursor)
4675 oparg_T *oap;
4676 int keep_cursor; /* keep cursor on same text char */
4677{
4678 long old_line_count = curbuf->b_ml.ml_line_count;
4679
4680 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4681 * can put it back there. */
4682 curwin->w_cursor = oap->cursor_start;
4683
4684 if (u_save((linenr_T)(oap->start.lnum - 1),
4685 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4686 return;
4687 curwin->w_cursor = oap->start;
4688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (oap->is_VIsual)
4690 /* When there is no change: need to remove the Visual selection */
4691 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
4693 /* Set '[ mark at the start of the formatted area */
4694 curbuf->b_op_start = oap->start;
4695
4696 /* For "gw" remember the cursor position and put it back below (adjusted
4697 * for joined and split lines). */
4698 if (keep_cursor)
4699 saved_cursor = oap->cursor_start;
4700
Bram Moolenaar81a82092008-03-12 16:27:00 +00004701 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 /*
4704 * Leave the cursor at the first non-blank of the last formatted line.
4705 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4706 * line, so "." will do the next lines.
4707 */
4708 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4709 ++curwin->w_cursor.lnum;
4710 beginline(BL_WHITE | BL_FIX);
4711 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4712 msgmore(old_line_count);
4713
4714 /* put '] mark on the end of the formatted area */
4715 curbuf->b_op_end = curwin->w_cursor;
4716
4717 if (keep_cursor)
4718 {
4719 curwin->w_cursor = saved_cursor;
4720 saved_cursor.lnum = 0;
4721 }
4722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (oap->is_VIsual)
4724 {
4725 win_T *wp;
4726
4727 FOR_ALL_WINDOWS(wp)
4728 {
4729 if (wp->w_old_cursor_lnum != 0)
4730 {
4731 /* When lines have been inserted or deleted, adjust the end of
4732 * the Visual area to be redrawn. */
4733 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4734 wp->w_old_cursor_lnum += old_line_count;
4735 else
4736 wp->w_old_visual_lnum += old_line_count;
4737 }
4738 }
4739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740}
4741
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004742#if defined(FEAT_EVAL) || defined(PROTO)
4743/*
4744 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4745 */
4746 void
4747op_formatexpr(oap)
4748 oparg_T *oap;
4749{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004750 if (oap->is_VIsual)
4751 /* When there is no change: need to remove the Visual selection */
4752 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004753
Bram Moolenaar700303e2010-07-11 17:35:50 +02004754 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4755 /* As documented: when 'formatexpr' returns non-zero fall back to
4756 * internal formatting. */
4757 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004758}
4759
4760 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004761fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004762 linenr_T lnum;
4763 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004764 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004765{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004766 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4767 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004768 int r;
4769
4770 /*
4771 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004772 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004773 */
4774 set_vim_var_nr(VV_LNUM, lnum);
4775 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004776 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004777
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004778 /*
4779 * Evaluate the function.
4780 */
4781 if (use_sandbox)
4782 ++sandbox;
4783 r = eval_to_number(curbuf->b_p_fex);
4784 if (use_sandbox)
4785 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004786
4787 set_vim_var_string(VV_CHAR, NULL, -1);
4788
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004789 return r;
4790}
4791#endif
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793/*
4794 * Format "line_count" lines, starting at the cursor position.
4795 * When "line_count" is negative, format until the end of the paragraph.
4796 * Lines after the cursor line are saved for undo, caller must have saved the
4797 * first line.
4798 */
4799 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004800format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004802 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 int max_len;
4805 int is_not_par; /* current line not part of parag. */
4806 int next_is_not_par; /* next line not part of paragraph */
4807 int is_end_par; /* at end of paragraph */
4808 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4809 int next_is_start_par = FALSE;
4810#ifdef FEAT_COMMENTS
4811 int leader_len = 0; /* leader len of current line */
4812 int next_leader_len; /* leader len of next line */
4813 char_u *leader_flags = NULL; /* flags for leader of current line */
4814 char_u *next_leader_flags; /* flags for leader of next line */
4815 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004816 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#endif
4818 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004819 int second_indent = -1; /* indent for second line (comment
4820 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 int do_second_indent;
4822 int do_number_indent;
4823 int do_trail_white;
4824 int first_par_line = TRUE;
4825 int smd_save;
4826 long count;
4827 int need_set_indent = TRUE; /* set indent of next paragraph */
4828 int force_format = FALSE;
4829 int old_State = State;
4830
4831 /* length of a line to force formatting: 3 * 'tw' */
4832 max_len = comp_textwidth(TRUE) * 3;
4833
4834 /* check for 'q', '2' and '1' in 'formatoptions' */
4835#ifdef FEAT_COMMENTS
4836 do_comments = has_format_option(FO_Q_COMS);
4837#endif
4838 do_second_indent = has_format_option(FO_Q_SECOND);
4839 do_number_indent = has_format_option(FO_Q_NUMBER);
4840 do_trail_white = has_format_option(FO_WHITE_PAR);
4841
4842 /*
4843 * Get info about the previous and current line.
4844 */
4845 if (curwin->w_cursor.lnum > 1)
4846 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4847#ifdef FEAT_COMMENTS
4848 , &leader_len, &leader_flags, do_comments
4849#endif
4850 );
4851 else
4852 is_not_par = TRUE;
4853 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4854#ifdef FEAT_COMMENTS
4855 , &next_leader_len, &next_leader_flags, do_comments
4856#endif
4857 );
4858 is_end_par = (is_not_par || next_is_not_par);
4859 if (!is_end_par && do_trail_white)
4860 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4861
4862 curwin->w_cursor.lnum--;
4863 for (count = line_count; count != 0 && !got_int; --count)
4864 {
4865 /*
4866 * Advance to next paragraph.
4867 */
4868 if (advance)
4869 {
4870 curwin->w_cursor.lnum++;
4871 prev_is_end_par = is_end_par;
4872 is_not_par = next_is_not_par;
4873#ifdef FEAT_COMMENTS
4874 leader_len = next_leader_len;
4875 leader_flags = next_leader_flags;
4876#endif
4877 }
4878
4879 /*
4880 * The last line to be formatted.
4881 */
4882 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4883 {
4884 next_is_not_par = TRUE;
4885#ifdef FEAT_COMMENTS
4886 next_leader_len = 0;
4887 next_leader_flags = NULL;
4888#endif
4889 }
4890 else
4891 {
4892 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4893#ifdef FEAT_COMMENTS
4894 , &next_leader_len, &next_leader_flags, do_comments
4895#endif
4896 );
4897 if (do_number_indent)
4898 next_is_start_par =
4899 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4900 }
4901 advance = TRUE;
4902 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4903 if (!is_end_par && do_trail_white)
4904 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4905
4906 /*
4907 * Skip lines that are not in a paragraph.
4908 */
4909 if (is_not_par)
4910 {
4911 if (line_count < 0)
4912 break;
4913 }
4914 else
4915 {
4916 /*
4917 * For the first line of a paragraph, check indent of second line.
4918 * Don't do this for comments and empty lines.
4919 */
4920 if (first_par_line
4921 && (do_second_indent || do_number_indent)
4922 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004923 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004925 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4926 {
4927#ifdef FEAT_COMMENTS
4928 if (leader_len == 0 && next_leader_len == 0)
4929 {
4930 /* no comment found */
4931#endif
4932 second_indent =
4933 get_indent_lnum(curwin->w_cursor.lnum + 1);
4934#ifdef FEAT_COMMENTS
4935 }
4936 else
4937 {
4938 second_indent = next_leader_len;
4939 do_comments_list = 1;
4940 }
4941#endif
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004944 {
4945#ifdef FEAT_COMMENTS
4946 if (leader_len == 0 && next_leader_len == 0)
4947 {
4948 /* no comment found */
4949#endif
4950 second_indent =
4951 get_number_indent(curwin->w_cursor.lnum);
4952#ifdef FEAT_COMMENTS
4953 }
4954 else
4955 {
4956 /* get_number_indent() is now "comment aware"... */
4957 second_indent =
4958 get_number_indent(curwin->w_cursor.lnum);
4959 do_comments_list = 1;
4960 }
4961#endif
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964
4965 /*
4966 * When the comment leader changes, it's the end of the paragraph.
4967 */
4968 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4969#ifdef FEAT_COMMENTS
4970 || !same_leader(curwin->w_cursor.lnum,
4971 leader_len, leader_flags,
4972 next_leader_len, next_leader_flags)
4973#endif
4974 )
4975 is_end_par = TRUE;
4976
4977 /*
4978 * If we have got to the end of a paragraph, or the line is
4979 * getting long, format it.
4980 */
4981 if (is_end_par || force_format)
4982 {
4983 if (need_set_indent)
4984 /* replace indent in first line with minimal number of
4985 * tabs and spaces, according to current options */
4986 (void)set_indent(get_indent(), SIN_CHANGED);
4987
4988 /* put cursor on last non-space */
4989 State = NORMAL; /* don't go past end-of-line */
4990 coladvance((colnr_T)MAXCOL);
4991 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4992 dec_cursor();
4993
4994 /* do the formatting, without 'showmode' */
4995 State = INSERT; /* for open_line() */
4996 smd_save = p_smd;
4997 p_smd = FALSE;
4998 insertchar(NUL, INSCHAR_FORMAT
4999#ifdef FEAT_COMMENTS
5000 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005001 + (do_comments && do_comments_list
5002 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005004 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 State = old_State;
5006 p_smd = smd_save;
5007 second_indent = -1;
5008 /* at end of par.: need to set indent of next par. */
5009 need_set_indent = is_end_par;
5010 if (is_end_par)
5011 {
5012 /* When called with a negative line count, break at the
5013 * end of the paragraph. */
5014 if (line_count < 0)
5015 break;
5016 first_par_line = TRUE;
5017 }
5018 force_format = FALSE;
5019 }
5020
5021 /*
5022 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005023 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 */
5025 if (!is_end_par)
5026 {
5027 advance = FALSE;
5028 curwin->w_cursor.lnum++;
5029 curwin->w_cursor.col = 0;
5030 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005034 {
5035 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5037 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005038 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005040 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5041 {
5042 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005043 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005044
5045 if (indent > 0)
5046 {
5047 (void)del_bytes(indent, FALSE, FALSE);
5048 mark_col_adjust(curwin->w_cursor.lnum,
5049 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005050 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005053 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 beep_flush();
5056 break;
5057 }
5058 first_par_line = FALSE;
5059 /* If the line is getting long, format it next time */
5060 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5061 force_format = TRUE;
5062 else
5063 force_format = FALSE;
5064 }
5065 }
5066 line_breakcheck();
5067 }
5068}
5069
5070/*
5071 * Return TRUE if line "lnum" ends in a white character.
5072 */
5073 static int
5074ends_in_white(lnum)
5075 linenr_T lnum;
5076{
5077 char_u *s = ml_get(lnum);
5078 size_t l;
5079
5080 if (*s == NUL)
5081 return FALSE;
5082 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5083 * invocation may call function multiple times". */
5084 l = STRLEN(s) - 1;
5085 return vim_iswhite(s[l]);
5086}
5087
5088/*
5089 * Blank lines, and lines containing only the comment leader, are left
5090 * untouched by the formatting. The function returns TRUE in this
5091 * case. It also returns TRUE when a line starts with the end of a comment
5092 * ('e' in comment flags), so that this line is skipped, and not joined to the
5093 * previous line. A new paragraph starts after a blank line, or when the
5094 * comment leader changes -- webb.
5095 */
5096#ifdef FEAT_COMMENTS
5097 static int
5098fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5099 linenr_T lnum;
5100 int *leader_len;
5101 char_u **leader_flags;
5102 int do_comments;
5103{
5104 char_u *flags = NULL; /* init for GCC */
5105 char_u *ptr;
5106
5107 ptr = ml_get(lnum);
5108 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005109 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 else
5111 *leader_len = 0;
5112
5113 if (*leader_len > 0)
5114 {
5115 /*
5116 * Search for 'e' flag in comment leader flags.
5117 */
5118 flags = *leader_flags;
5119 while (*flags && *flags != ':' && *flags != COM_END)
5120 ++flags;
5121 }
5122
5123 return (*skipwhite(ptr + *leader_len) == NUL
5124 || (*leader_len > 0 && *flags == COM_END)
5125 || startPS(lnum, NUL, FALSE));
5126}
5127#else
5128 static int
5129fmt_check_par(lnum)
5130 linenr_T lnum;
5131{
5132 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5133}
5134#endif
5135
5136/*
5137 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5138 * previous line is in the same paragraph. Used for auto-formatting.
5139 */
5140 int
5141paragraph_start(lnum)
5142 linenr_T lnum;
5143{
5144 char_u *p;
5145#ifdef FEAT_COMMENTS
5146 int leader_len = 0; /* leader len of current line */
5147 char_u *leader_flags = NULL; /* flags for leader of current line */
5148 int next_leader_len; /* leader len of next line */
5149 char_u *next_leader_flags; /* flags for leader of next line */
5150 int do_comments; /* format comments */
5151#endif
5152
5153 if (lnum <= 1)
5154 return TRUE; /* start of the file */
5155
5156 p = ml_get(lnum - 1);
5157 if (*p == NUL)
5158 return TRUE; /* after empty line */
5159
5160#ifdef FEAT_COMMENTS
5161 do_comments = has_format_option(FO_Q_COMS);
5162#endif
5163 if (fmt_check_par(lnum - 1
5164#ifdef FEAT_COMMENTS
5165 , &leader_len, &leader_flags, do_comments
5166#endif
5167 ))
5168 return TRUE; /* after non-paragraph line */
5169
5170 if (fmt_check_par(lnum
5171#ifdef FEAT_COMMENTS
5172 , &next_leader_len, &next_leader_flags, do_comments
5173#endif
5174 ))
5175 return TRUE; /* "lnum" is not a paragraph line */
5176
5177 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5178 return TRUE; /* missing trailing space in previous line. */
5179
5180 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5181 return TRUE; /* numbered item starts in "lnum". */
5182
5183#ifdef FEAT_COMMENTS
5184 if (!same_leader(lnum - 1, leader_len, leader_flags,
5185 next_leader_len, next_leader_flags))
5186 return TRUE; /* change of comment leader. */
5187#endif
5188
5189 return FALSE;
5190}
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192/*
5193 * prepare a few things for block mode yank/delete/tilde
5194 *
5195 * for delete:
5196 * - textlen includes the first/last char to be (partly) deleted
5197 * - start/endspaces is the number of columns that are taken by the
5198 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005199 * deleted.
5200 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 * - textlen includes the first/last char to be wholly yanked
5202 * - start/endspaces is the number of columns of the first/last yanked char
5203 * that are to be yanked.
5204 */
5205 static void
5206block_prep(oap, bdp, lnum, is_del)
5207 oparg_T *oap;
5208 struct block_def *bdp;
5209 linenr_T lnum;
5210 int is_del;
5211{
5212 int incr = 0;
5213 char_u *pend;
5214 char_u *pstart;
5215 char_u *line;
5216 char_u *prev_pstart;
5217 char_u *prev_pend;
5218
5219 bdp->startspaces = 0;
5220 bdp->endspaces = 0;
5221 bdp->textlen = 0;
5222 bdp->start_vcol = 0;
5223 bdp->end_vcol = 0;
5224#ifdef FEAT_VISUALEXTRA
5225 bdp->is_short = FALSE;
5226 bdp->is_oneChar = FALSE;
5227 bdp->pre_whitesp = 0;
5228 bdp->pre_whitesp_c = 0;
5229 bdp->end_char_vcols = 0;
5230#endif
5231 bdp->start_char_vcols = 0;
5232
5233 line = ml_get(lnum);
5234 pstart = line;
5235 prev_pstart = line;
5236 while (bdp->start_vcol < oap->start_vcol && *pstart)
5237 {
5238 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005239 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 bdp->start_vcol += incr;
5241#ifdef FEAT_VISUALEXTRA
5242 if (vim_iswhite(*pstart))
5243 {
5244 bdp->pre_whitesp += incr;
5245 bdp->pre_whitesp_c++;
5246 }
5247 else
5248 {
5249 bdp->pre_whitesp = 0;
5250 bdp->pre_whitesp_c = 0;
5251 }
5252#endif
5253 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005254 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
5256 bdp->start_char_vcols = incr;
5257 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5258 {
5259 bdp->end_vcol = bdp->start_vcol;
5260#ifdef FEAT_VISUALEXTRA
5261 bdp->is_short = TRUE;
5262#endif
5263 if (!is_del || oap->op_type == OP_APPEND)
5264 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5265 }
5266 else
5267 {
5268 /* notice: this converts partly selected Multibyte characters to
5269 * spaces, too. */
5270 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5271 if (is_del && bdp->startspaces)
5272 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5273 pend = pstart;
5274 bdp->end_vcol = bdp->start_vcol;
5275 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5276 {
5277#ifdef FEAT_VISUALEXTRA
5278 bdp->is_oneChar = TRUE;
5279#endif
5280 if (oap->op_type == OP_INSERT)
5281 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5282 else if (oap->op_type == OP_APPEND)
5283 {
5284 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5285 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5286 }
5287 else
5288 {
5289 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5290 if (is_del && oap->op_type != OP_LSHIFT)
5291 {
5292 /* just putting the sum of those two into
5293 * bdp->startspaces doesn't work for Visual replace,
5294 * so we have to split the tab in two */
5295 bdp->startspaces = bdp->start_char_vcols
5296 - (bdp->start_vcol - oap->start_vcol);
5297 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5298 }
5299 }
5300 }
5301 else
5302 {
5303 prev_pend = pend;
5304 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5305 {
5306 /* Count a tab for what it's worth (if list mode not on) */
5307 prev_pend = pend;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005308 /* TODO: is passing prev_pend for start of the line OK?
5309 * perhaps it should be "line". */
5310 incr = lbr_chartabsize_adv(prev_pend, &pend,
5311 (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 bdp->end_vcol += incr;
5313 }
5314 if (bdp->end_vcol <= oap->end_vcol
5315 && (!is_del
5316 || oap->op_type == OP_APPEND
5317 || oap->op_type == OP_REPLACE)) /* line too short */
5318 {
5319#ifdef FEAT_VISUALEXTRA
5320 bdp->is_short = TRUE;
5321#endif
5322 /* Alternative: include spaces to fill up the block.
5323 * Disadvantage: can lead to trailing spaces when the line is
5324 * short where the text is put */
5325 /* if (!is_del || oap->op_type == OP_APPEND) */
5326 if (oap->op_type == OP_APPEND || virtual_op)
5327 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005328 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 else
5330 bdp->endspaces = 0; /* replace doesn't add characters */
5331 }
5332 else if (bdp->end_vcol > oap->end_vcol)
5333 {
5334 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5335 if (!is_del && bdp->endspaces)
5336 {
5337 bdp->endspaces = incr - bdp->endspaces;
5338 if (pend != pstart)
5339 pend = prev_pend;
5340 }
5341 }
5342 }
5343#ifdef FEAT_VISUALEXTRA
5344 bdp->end_char_vcols = incr;
5345#endif
5346 if (is_del && bdp->startspaces)
5347 pstart = prev_pstart;
5348 bdp->textlen = (int)(pend - pstart);
5349 }
5350 bdp->textcol = (colnr_T) (pstart - line);
5351 bdp->textstart = pstart;
5352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
5354#ifdef FEAT_RIGHTLEFT
5355static void reverse_line __ARGS((char_u *s));
5356
5357 static void
5358reverse_line(s)
5359 char_u *s;
5360{
5361 int i, j;
5362 char_u c;
5363
5364 if ((i = (int)STRLEN(s) - 1) <= 0)
5365 return;
5366
5367 curwin->w_cursor.col = i - curwin->w_cursor.col;
5368 for (j = 0; j < i; j++, i--)
5369 {
5370 c = s[i]; s[i] = s[j]; s[j] = c;
5371 }
5372}
5373
5374# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5375#else
5376# define RLADDSUBFIX(ptr)
5377#endif
5378
5379/*
5380 * add or subtract 'Prenum1' from a number in a line
5381 * 'command' is CTRL-A for add, CTRL-X for subtract
5382 *
5383 * return FAIL for failure, OK otherwise
5384 */
5385 int
5386do_addsub(command, Prenum1)
5387 int command;
5388 linenr_T Prenum1;
5389{
5390 int col;
5391 char_u *buf1;
5392 char_u buf2[NUMBUFLEN];
5393 int hex; /* 'X' or 'x': hex; '0': octal */
5394 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005395 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 long_u oldn;
5397 char_u *ptr;
5398 int c;
5399 int length = 0; /* character length of the number */
5400 int todel;
5401 int dohex;
5402 int dooct;
5403 int doalp;
5404 int firstdigit;
5405 int negative;
5406 int subtract;
5407
5408 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5409 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5410 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5411
5412 ptr = ml_get_curline();
5413 RLADDSUBFIX(ptr);
5414
5415 /*
5416 * First check if we are on a hexadecimal number, after the "0x".
5417 */
5418 col = curwin->w_cursor.col;
5419 if (dohex)
5420 while (col > 0 && vim_isxdigit(ptr[col]))
5421 --col;
5422 if ( dohex
5423 && col > 0
5424 && (ptr[col] == 'X'
5425 || ptr[col] == 'x')
5426 && ptr[col - 1] == '0'
5427 && vim_isxdigit(ptr[col + 1]))
5428 {
5429 /*
5430 * Found hexadecimal number, move to its start.
5431 */
5432 --col;
5433 }
5434 else
5435 {
5436 /*
5437 * Search forward and then backward to find the start of number.
5438 */
5439 col = curwin->w_cursor.col;
5440
5441 while (ptr[col] != NUL
5442 && !vim_isdigit(ptr[col])
5443 && !(doalp && ASCII_ISALPHA(ptr[col])))
5444 ++col;
5445
5446 while (col > 0
5447 && vim_isdigit(ptr[col - 1])
5448 && !(doalp && ASCII_ISALPHA(ptr[col])))
5449 --col;
5450 }
5451
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 /*
5453 * If a number was found, and saving for undo works, replace the number.
5454 */
5455 firstdigit = ptr[col];
5456 RLADDSUBFIX(ptr);
5457 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5458 || u_save_cursor() != OK)
5459 {
5460 beep_flush();
5461 return FAIL;
5462 }
5463
5464 /* get ptr again, because u_save() may have changed it */
5465 ptr = ml_get_curline();
5466 RLADDSUBFIX(ptr);
5467
5468 if (doalp && ASCII_ISALPHA(firstdigit))
5469 {
5470 /* decrement or increment alphabetic character */
5471 if (command == Ctrl_X)
5472 {
5473 if (CharOrd(firstdigit) < Prenum1)
5474 {
5475 if (isupper(firstdigit))
5476 firstdigit = 'A';
5477 else
5478 firstdigit = 'a';
5479 }
5480 else
5481#ifdef EBCDIC
5482 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5483#else
5484 firstdigit -= Prenum1;
5485#endif
5486 }
5487 else
5488 {
5489 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5490 {
5491 if (isupper(firstdigit))
5492 firstdigit = 'Z';
5493 else
5494 firstdigit = 'z';
5495 }
5496 else
5497#ifdef EBCDIC
5498 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5499#else
5500 firstdigit += Prenum1;
5501#endif
5502 }
5503 curwin->w_cursor.col = col;
5504 (void)del_char(FALSE);
5505 ins_char(firstdigit);
5506 }
5507 else
5508 {
5509 negative = FALSE;
5510 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5511 {
5512 --col;
5513 negative = TRUE;
5514 }
5515
5516 /* get the number value (unsigned) */
5517 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5518
5519 /* ignore leading '-' for hex and octal numbers */
5520 if (hex && negative)
5521 {
5522 ++col;
5523 --length;
5524 negative = FALSE;
5525 }
5526
5527 /* add or subtract */
5528 subtract = FALSE;
5529 if (command == Ctrl_X)
5530 subtract ^= TRUE;
5531 if (negative)
5532 subtract ^= TRUE;
5533
5534 oldn = n;
5535 if (subtract)
5536 n -= (unsigned long)Prenum1;
5537 else
5538 n += (unsigned long)Prenum1;
5539
5540 /* handle wraparound for decimal numbers */
5541 if (!hex)
5542 {
5543 if (subtract)
5544 {
5545 if (n > oldn)
5546 {
5547 n = 1 + (n ^ (unsigned long)-1);
5548 negative ^= TRUE;
5549 }
5550 }
5551 else /* add */
5552 {
5553 if (n < oldn)
5554 {
5555 n = (n ^ (unsigned long)-1);
5556 negative ^= TRUE;
5557 }
5558 }
5559 if (n == 0)
5560 negative = FALSE;
5561 }
5562
5563 /*
5564 * Delete the old number.
5565 */
5566 curwin->w_cursor.col = col;
5567 todel = length;
5568 c = gchar_cursor();
5569 /*
5570 * Don't include the '-' in the length, only the length of the part
5571 * after it is kept the same.
5572 */
5573 if (c == '-')
5574 --length;
5575 while (todel-- > 0)
5576 {
5577 if (c < 0x100 && isalpha(c))
5578 {
5579 if (isupper(c))
5580 hexupper = TRUE;
5581 else
5582 hexupper = FALSE;
5583 }
5584 /* del_char() will mark line needing displaying */
5585 (void)del_char(FALSE);
5586 c = gchar_cursor();
5587 }
5588
5589 /*
5590 * Prepare the leading characters in buf1[].
5591 * When there are many leading zeros it could be very long. Allocate
5592 * a bit too much.
5593 */
5594 buf1 = alloc((unsigned)length + NUMBUFLEN);
5595 if (buf1 == NULL)
5596 return FAIL;
5597 ptr = buf1;
5598 if (negative)
5599 {
5600 *ptr++ = '-';
5601 }
5602 if (hex)
5603 {
5604 *ptr++ = '0';
5605 --length;
5606 }
5607 if (hex == 'x' || hex == 'X')
5608 {
5609 *ptr++ = hex;
5610 --length;
5611 }
5612
5613 /*
5614 * Put the number characters in buf2[].
5615 */
5616 if (hex == 0)
5617 sprintf((char *)buf2, "%lu", n);
5618 else if (hex == '0')
5619 sprintf((char *)buf2, "%lo", n);
5620 else if (hex && hexupper)
5621 sprintf((char *)buf2, "%lX", n);
5622 else
5623 sprintf((char *)buf2, "%lx", n);
5624 length -= (int)STRLEN(buf2);
5625
5626 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005627 * Adjust number of zeros to the new number of digits, so the
5628 * total length of the number remains the same.
5629 * Don't do this when
5630 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005632 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 while (length-- > 0)
5634 *ptr++ = '0';
5635 *ptr = NUL;
5636 STRCAT(buf1, buf2);
5637 ins_str(buf1); /* insert the new number */
5638 vim_free(buf1);
5639 }
5640 --curwin->w_cursor.col;
5641 curwin->w_set_curswant = TRUE;
5642#ifdef FEAT_RIGHTLEFT
5643 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5644 RLADDSUBFIX(ptr);
5645#endif
5646 return OK;
5647}
5648
5649#ifdef FEAT_VIMINFO
5650 int
5651read_viminfo_register(virp, force)
5652 vir_T *virp;
5653 int force;
5654{
5655 int eof;
5656 int do_it = TRUE;
5657 int size;
5658 int limit;
5659 int i;
5660 int set_prev = FALSE;
5661 char_u *str;
5662 char_u **array = NULL;
5663
5664 /* We only get here (hopefully) if line[0] == '"' */
5665 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005666
5667 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (*str == '"')
5669 {
5670 set_prev = TRUE;
5671 str++;
5672 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005673
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 if (!ASCII_ISALNUM(*str) && *str != '-')
5675 {
5676 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5677 return TRUE; /* too many errors, pretend end-of-file */
5678 do_it = FALSE;
5679 }
5680 get_yank_register(*str++, FALSE);
5681 if (!force && y_current->y_array != NULL)
5682 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005683
5684 if (*str == '@')
5685 {
5686 /* "x@: register x used for @@ */
5687 if (force || execreg_lastc == NUL)
5688 execreg_lastc = str[-1];
5689 }
5690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 size = 0;
5692 limit = 100; /* Optimized for registers containing <= 100 lines */
5693 if (do_it)
5694 {
5695 if (set_prev)
5696 y_previous = y_current;
5697 vim_free(y_current->y_array);
5698 array = y_current->y_array =
5699 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005700 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 if (STRNCMP(str, "CHAR", 4) == 0)
5702 y_current->y_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 else if (STRNCMP(str, "BLOCK", 5) == 0)
5704 y_current->y_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 else
5706 y_current->y_type = MLINE;
5707 /* get the block width; if it's missing we get a zero, which is OK */
5708 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 y_current->y_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711
5712 while (!(eof = viminfo_readline(virp))
5713 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5714 {
5715 if (do_it)
5716 {
5717 if (size >= limit)
5718 {
5719 y_current->y_array = (char_u **)
5720 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5721 for (i = 0; i < limit; i++)
5722 y_current->y_array[i] = array[i];
5723 vim_free(array);
5724 limit *= 2;
5725 array = y_current->y_array;
5726 }
5727 str = viminfo_readstring(virp, 1, TRUE);
5728 if (str != NULL)
5729 array[size++] = str;
5730 else
5731 do_it = FALSE;
5732 }
5733 }
5734 if (do_it)
5735 {
5736 if (size == 0)
5737 {
5738 vim_free(array);
5739 y_current->y_array = NULL;
5740 }
5741 else if (size < limit)
5742 {
5743 y_current->y_array =
5744 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5745 for (i = 0; i < size; i++)
5746 y_current->y_array[i] = array[i];
5747 vim_free(array);
5748 }
5749 y_current->y_size = size;
5750 }
5751 return eof;
5752}
5753
5754 void
5755write_viminfo_registers(fp)
5756 FILE *fp;
5757{
5758 int i, j;
5759 char_u *type;
5760 char_u c;
5761 int num_lines;
5762 int max_num_lines;
5763 int max_kbyte;
5764 long len;
5765
Bram Moolenaar64404472010-06-26 06:24:45 +02005766 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 /* Get '<' value, use old '"' value if '<' is not found. */
5769 max_num_lines = get_viminfo_parameter('<');
5770 if (max_num_lines < 0)
5771 max_num_lines = get_viminfo_parameter('"');
5772 if (max_num_lines == 0)
5773 return;
5774 max_kbyte = get_viminfo_parameter('s');
5775 if (max_kbyte == 0)
5776 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 for (i = 0; i < NUM_REGISTERS; i++)
5779 {
5780 if (y_regs[i].y_array == NULL)
5781 continue;
5782#ifdef FEAT_CLIPBOARD
5783 /* Skip '*'/'+' register, we don't want them back next time */
5784 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5785 continue;
5786#endif
5787#ifdef FEAT_DND
5788 /* Neither do we want the '~' register */
5789 if (i == TILDE_REGISTER)
5790 continue;
5791#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005792 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005794 if (num_lines == 0
5795 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005796 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005797 continue;
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (max_kbyte > 0)
5800 {
5801 /* Skip register if there is more text than the maximum size. */
5802 len = 0;
5803 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005804 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if (len > (long)max_kbyte * 1024L)
5806 continue;
5807 }
5808
5809 switch (y_regs[i].y_type)
5810 {
5811 case MLINE:
5812 type = (char_u *)"LINE";
5813 break;
5814 case MCHAR:
5815 type = (char_u *)"CHAR";
5816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 case MBLOCK:
5818 type = (char_u *)"BLOCK";
5819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 default:
5821 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005822 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 emsg(IObuff);
5824 type = (char_u *)"LINE";
5825 break;
5826 }
5827 if (y_previous == &y_regs[i])
5828 fprintf(fp, "\"");
5829 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005830 fprintf(fp, "\"%c", c);
5831 if (c == execreg_lastc)
5832 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005833 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835 /* If max_num_lines < 0, then we save ALL the lines in the register */
5836 if (max_num_lines > 0 && num_lines > max_num_lines)
5837 num_lines = max_num_lines;
5838 for (j = 0; j < num_lines; j++)
5839 {
5840 putc('\t', fp);
5841 viminfo_writestring(fp, y_regs[i].y_array[j]);
5842 }
5843 }
5844}
5845#endif /* FEAT_VIMINFO */
5846
5847#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5848/*
5849 * SELECTION / PRIMARY ('*')
5850 *
5851 * Text selection stuff that uses the GUI selection register '*'. When using a
5852 * GUI this may be text from another window, otherwise it is the last text we
5853 * had highlighted with VIsual mode. With mouse support, clicking the middle
5854 * button performs the paste, otherwise you will need to do <"*p>. "
5855 * If not under X, it is synonymous with the clipboard register '+'.
5856 *
5857 * X CLIPBOARD ('+')
5858 *
5859 * Text selection stuff that uses the GUI clipboard register '+'.
5860 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5861 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5862 * otherwise you will need to do <"+p>. "
5863 * If not under X, it is synonymous with the selection register '*'.
5864 */
5865
5866/*
5867 * Routine to export any final X selection we had to the environment
5868 * so that the text is still available after vim has exited. X selections
5869 * only exist while the owning application exists, so we write to the
5870 * permanent (while X runs) store CUT_BUFFER0.
5871 * Dump the CLIPBOARD selection if we own it (it's logically the more
5872 * 'permanent' of the two), otherwise the PRIMARY one.
5873 * For now, use a hard-coded sanity limit of 1Mb of data.
5874 */
5875#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5876 void
5877x11_export_final_selection()
5878{
5879 Display *dpy;
5880 char_u *str = NULL;
5881 long_u len = 0;
5882 int motion_type = -1;
5883
5884# ifdef FEAT_GUI
5885 if (gui.in_use)
5886 dpy = X_DISPLAY;
5887 else
5888# endif
5889# ifdef FEAT_XCLIPBOARD
5890 dpy = xterm_dpy;
5891# else
5892 return;
5893# endif
5894
5895 /* Get selection to export */
5896 if (clip_plus.owned)
5897 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5898 else if (clip_star.owned)
5899 motion_type = clip_convert_selection(&str, &len, &clip_star);
5900
5901 /* Check it's OK */
5902 if (dpy != NULL && str != NULL && motion_type >= 0
5903 && len < 1024*1024 && len > 0)
5904 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005905#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005906 int ok = TRUE;
5907
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005908 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5909 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5910 * encoding conversion usually doesn't work, so keep the text as-is.
5911 */
5912 if (has_mbyte)
5913 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005914 vimconv_T vc;
5915
5916 vc.vc_type = CONV_NONE;
5917 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5918 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005919 int intlen = len;
5920 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005921
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005922 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005923 conv_str = string_convert(&vc, str, &intlen);
5924 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005925 if (conv_str != NULL)
5926 {
5927 vim_free(str);
5928 str = conv_str;
5929 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005930 else
5931 {
5932 ok = FALSE;
5933 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005934 convert_setup(&vc, NULL, NULL);
5935 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005936 else
5937 {
5938 ok = FALSE;
5939 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005940 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005941
5942 /* Do not store the string if conversion failed. Better to use any
5943 * other selection than garbled text. */
5944 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005945#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005946 {
5947 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5948 XFlush(dpy);
5949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 }
5951
5952 vim_free(str);
5953}
5954#endif
5955
5956 void
5957clip_free_selection(cbd)
5958 VimClipboard *cbd;
5959{
5960 struct yankreg *y_ptr = y_current;
5961
5962 if (cbd == &clip_plus)
5963 y_current = &y_regs[PLUS_REGISTER];
5964 else
5965 y_current = &y_regs[STAR_REGISTER];
5966 free_yank_all();
5967 y_current->y_size = 0;
5968 y_current = y_ptr;
5969}
5970
5971/*
5972 * Get the selected text and put it in the gui selection register '*' or '+'.
5973 */
5974 void
5975clip_get_selection(cbd)
5976 VimClipboard *cbd;
5977{
5978 struct yankreg *old_y_previous, *old_y_current;
5979 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 pos_T old_visual;
5981 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 colnr_T old_curswant;
5983 int old_set_curswant;
5984 pos_T old_op_start, old_op_end;
5985 oparg_T oa;
5986 cmdarg_T ca;
5987
5988 if (cbd->owned)
5989 {
5990 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5991 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5992 return;
5993
5994 /* Get the text between clip_star.start & clip_star.end */
5995 old_y_previous = y_previous;
5996 old_y_current = y_current;
5997 old_cursor = curwin->w_cursor;
5998 old_curswant = curwin->w_curswant;
5999 old_set_curswant = curwin->w_set_curswant;
6000 old_op_start = curbuf->b_op_start;
6001 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 old_visual = VIsual;
6003 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 clear_oparg(&oa);
6005 oa.regname = (cbd == &clip_plus ? '+' : '*');
6006 oa.op_type = OP_YANK;
6007 vim_memset(&ca, 0, sizeof(ca));
6008 ca.oap = &oa;
6009 ca.cmdchar = 'y';
6010 ca.count1 = 1;
6011 ca.retval = CA_NO_ADJ_OP_END;
6012 do_pending_operator(&ca, 0, TRUE);
6013 y_previous = old_y_previous;
6014 y_current = old_y_current;
6015 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006016 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 curwin->w_curswant = old_curswant;
6018 curwin->w_set_curswant = old_set_curswant;
6019 curbuf->b_op_start = old_op_start;
6020 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 VIsual = old_visual;
6022 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
6024 else
6025 {
6026 clip_free_selection(cbd);
6027
6028 /* Try to get selected text from another window */
6029 clip_gen_request_selection(cbd);
6030 }
6031}
6032
Bram Moolenaard44347f2011-06-19 01:14:29 +02006033/*
6034 * Convert from the GUI selection string into the '*'/'+' register.
6035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 void
6037clip_yank_selection(type, str, len, cbd)
6038 int type;
6039 char_u *str;
6040 long len;
6041 VimClipboard *cbd;
6042{
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 clip_free_selection(cbd);
6051
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006052 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053}
6054
6055/*
6056 * Convert the '*'/'+' register into a GUI selection string returned in *str
6057 * with length *len.
6058 * Returns the motion type, or -1 for failure.
6059 */
6060 int
6061clip_convert_selection(str, len, cbd)
6062 char_u **str;
6063 long_u *len;
6064 VimClipboard *cbd;
6065{
6066 char_u *p;
6067 int lnum;
6068 int i, j;
6069 int_u eolsize;
6070 struct yankreg *y_ptr;
6071
6072 if (cbd == &clip_plus)
6073 y_ptr = &y_regs[PLUS_REGISTER];
6074 else
6075 y_ptr = &y_regs[STAR_REGISTER];
6076
6077#ifdef USE_CRNL
6078 eolsize = 2;
6079#else
6080 eolsize = 1;
6081#endif
6082
6083 *str = NULL;
6084 *len = 0;
6085 if (y_ptr->y_array == NULL)
6086 return -1;
6087
6088 for (i = 0; i < y_ptr->y_size; i++)
6089 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6090
6091 /*
6092 * Don't want newline character at end of last line if we're in MCHAR mode.
6093 */
6094 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6095 *len -= eolsize;
6096
6097 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6098 if (p == NULL)
6099 return -1;
6100 lnum = 0;
6101 for (i = 0, j = 0; i < (int)*len; i++, j++)
6102 {
6103 if (y_ptr->y_array[lnum][j] == '\n')
6104 p[i] = NUL;
6105 else if (y_ptr->y_array[lnum][j] == NUL)
6106 {
6107#ifdef USE_CRNL
6108 p[i++] = '\r';
6109#endif
6110#ifdef USE_CR
6111 p[i] = '\r';
6112#else
6113 p[i] = '\n';
6114#endif
6115 lnum++;
6116 j = -1;
6117 }
6118 else
6119 p[i] = y_ptr->y_array[lnum][j];
6120 }
6121 return y_ptr->y_type;
6122}
6123
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125/*
6126 * If we have written to a clipboard register, send the text to the clipboard.
6127 */
6128 static void
6129may_set_selection()
6130{
6131 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6132 {
6133 clip_own_selection(&clip_star);
6134 clip_gen_set_selection(&clip_star);
6135 }
6136 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6137 {
6138 clip_own_selection(&clip_plus);
6139 clip_gen_set_selection(&clip_plus);
6140 }
6141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143#endif /* FEAT_CLIPBOARD || PROTO */
6144
6145
6146#if defined(FEAT_DND) || defined(PROTO)
6147/*
6148 * Replace the contents of the '~' register with str.
6149 */
6150 void
6151dnd_yank_drag_data(str, len)
6152 char_u *str;
6153 long len;
6154{
6155 struct yankreg *curr;
6156
6157 curr = y_current;
6158 y_current = &y_regs[TILDE_REGISTER];
6159 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006160 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 y_current = curr;
6162}
6163#endif
6164
6165
6166#if defined(FEAT_EVAL) || defined(PROTO)
6167/*
6168 * Return the type of a register.
6169 * Used for getregtype()
6170 * Returns MAUTO for error.
6171 */
6172 char_u
6173get_reg_type(regname, reglen)
6174 int regname;
6175 long *reglen;
6176{
6177 switch (regname)
6178 {
6179 case '%': /* file name */
6180 case '#': /* alternate file name */
6181 case '=': /* expression */
6182 case ':': /* last command line */
6183 case '/': /* last search-pattern */
6184 case '.': /* last inserted text */
6185#ifdef FEAT_SEARCHPATH
6186 case Ctrl_F: /* Filename under cursor */
6187 case Ctrl_P: /* Path under cursor, expand via "path" */
6188#endif
6189 case Ctrl_W: /* word under cursor */
6190 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6191 case '_': /* black hole: always empty */
6192 return MCHAR;
6193 }
6194
6195#ifdef FEAT_CLIPBOARD
6196 regname = may_get_selection(regname);
6197#endif
6198
Bram Moolenaar32b92012014-01-14 12:33:36 +01006199 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6200 return MAUTO;
6201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 get_yank_register(regname, FALSE);
6203
6204 if (y_current->y_array != NULL)
6205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 if (reglen != NULL && y_current->y_type == MBLOCK)
6207 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 return y_current->y_type;
6209 }
6210 return MAUTO;
6211}
6212
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006213static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6214
6215/*
6216 * When "flags" has GREG_LIST return a list with text "s".
6217 * Otherwise just return "s".
6218 */
6219 static char_u *
6220getreg_wrap_one_line(s, flags)
6221 char_u *s;
6222 int flags;
6223{
6224 if (flags & GREG_LIST)
6225 {
6226 list_T *list = list_alloc();
6227
6228 if (list != NULL)
6229 {
6230 if (list_append_string(list, NULL, -1) == FAIL)
6231 {
6232 list_free(list, TRUE);
6233 return NULL;
6234 }
6235 list->lv_first->li_tv.vval.v_string = s;
6236 }
6237 return (char_u *)list;
6238 }
6239 return s;
6240}
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242/*
6243 * Return the contents of a register as a single allocated string.
6244 * Used for "@r" in expressions and for getreg().
6245 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006246 * Flags:
6247 * GREG_NO_EXPR Do not allow expression register
6248 * GREG_EXPR_SRC For the expression register: return expression itself,
6249 * not the result of its evaluation.
6250 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 */
6252 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006253get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006255 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 long i;
6258 char_u *retval;
6259 int allocated;
6260 long len;
6261
6262 /* Don't allow using an expression register inside an expression */
6263 if (regname == '=')
6264 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006265 if (flags & GREG_NO_EXPR)
6266 return NULL;
6267 if (flags & GREG_EXPR_SRC)
6268 return getreg_wrap_one_line(get_expr_line_src(), flags);
6269 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271
6272 if (regname == '@') /* "@@" is used for unnamed register */
6273 regname = '"';
6274
6275 /* check for valid regname */
6276 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6277 return NULL;
6278
6279#ifdef FEAT_CLIPBOARD
6280 regname = may_get_selection(regname);
6281#endif
6282
6283 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6284 {
6285 if (retval == NULL)
6286 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006287 if (allocated)
6288 return getreg_wrap_one_line(retval, flags);
6289 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 }
6291
6292 get_yank_register(regname, FALSE);
6293 if (y_current->y_array == NULL)
6294 return NULL;
6295
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006296 if (flags & GREG_LIST)
6297 {
6298 list_T *list = list_alloc();
6299 int error = FALSE;
6300
6301 if (list == NULL)
6302 return NULL;
6303 for (i = 0; i < y_current->y_size; ++i)
6304 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6305 error = TRUE;
6306 if (error)
6307 {
6308 list_free(list, TRUE);
6309 return NULL;
6310 }
6311 return (char_u *)list;
6312 }
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 /*
6315 * Compute length of resulting string.
6316 */
6317 len = 0;
6318 for (i = 0; i < y_current->y_size; ++i)
6319 {
6320 len += (long)STRLEN(y_current->y_array[i]);
6321 /*
6322 * Insert a newline between lines and after last line if
6323 * y_type is MLINE.
6324 */
6325 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6326 ++len;
6327 }
6328
6329 retval = lalloc(len + 1, TRUE);
6330
6331 /*
6332 * Copy the lines of the yank register into the string.
6333 */
6334 if (retval != NULL)
6335 {
6336 len = 0;
6337 for (i = 0; i < y_current->y_size; ++i)
6338 {
6339 STRCPY(retval + len, y_current->y_array[i]);
6340 len += (long)STRLEN(retval + len);
6341
6342 /*
6343 * Insert a NL between lines and after the last line if y_type is
6344 * MLINE.
6345 */
6346 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6347 retval[len++] = '\n';
6348 }
6349 retval[len] = NUL;
6350 }
6351
6352 return retval;
6353}
6354
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006355 static int
6356init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6357 int name;
6358 struct yankreg **old_y_previous;
6359 struct yankreg **old_y_current;
6360 int must_append;
6361 int *yank_type UNUSED;
6362{
6363 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6364 {
6365 emsg_invreg(name);
6366 return FAIL;
6367 }
6368
6369 /* Don't want to change the current (unnamed) register */
6370 *old_y_previous = y_previous;
6371 *old_y_current = y_current;
6372
6373 get_yank_register(name, TRUE);
6374 if (!y_append && !must_append)
6375 free_yank_all();
6376 return OK;
6377}
6378
6379 static void
6380finish_write_reg(name, old_y_previous, old_y_current)
6381 int name;
6382 struct yankreg *old_y_previous;
6383 struct yankreg *old_y_current;
6384{
6385# ifdef FEAT_CLIPBOARD
6386 /* Send text of clipboard register to the clipboard. */
6387 may_set_selection();
6388# endif
6389
6390 /* ':let @" = "val"' should change the meaning of the "" register */
6391 if (name != '"')
6392 y_previous = old_y_previous;
6393 y_current = old_y_current;
6394}
6395
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396/*
6397 * Store string "str" in register "name".
6398 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6399 * If "must_append" is TRUE, always append to the register. Otherwise append
6400 * if "name" is an uppercase letter.
6401 * Note: "maxlen" and "must_append" don't work for the "/" register.
6402 * Careful: 'str' is modified, you may have to use a copy!
6403 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6404 */
6405 void
6406write_reg_contents(name, str, maxlen, must_append)
6407 int name;
6408 char_u *str;
6409 int maxlen;
6410 int must_append;
6411{
6412 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6413}
6414
6415 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006416write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6417 int name;
6418 char_u **strings;
6419 int maxlen UNUSED;
6420 int must_append;
6421 int yank_type;
6422 long block_len;
6423{
6424 struct yankreg *old_y_previous, *old_y_current;
6425
6426 if (name == '/'
6427#ifdef FEAT_EVAL
6428 || name == '='
6429#endif
6430 )
6431 {
6432 char_u *s;
6433
6434 if (strings[0] == NULL)
6435 s = (char_u *)"";
6436 else if (strings[1] != NULL)
6437 {
6438 EMSG(_("E883: search pattern and expression register may not "
6439 "contain two or more lines"));
6440 return;
6441 }
6442 else
6443 s = strings[0];
6444 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6445 return;
6446 }
6447
6448 if (name == '_') /* black hole: nothing to do */
6449 return;
6450
6451 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6452 &yank_type) == FAIL)
6453 return;
6454
6455 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6456
6457 finish_write_reg(name, old_y_previous, old_y_current);
6458}
6459
6460 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6462 int name;
6463 char_u *str;
6464 int maxlen;
6465 int must_append;
6466 int yank_type;
6467 long block_len;
6468{
6469 struct yankreg *old_y_previous, *old_y_current;
6470 long len;
6471
Bram Moolenaare7566042005-06-17 22:00:15 +00006472 if (maxlen >= 0)
6473 len = maxlen;
6474 else
6475 len = (long)STRLEN(str);
6476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 /* Special case: '/' search pattern */
6478 if (name == '/')
6479 {
6480 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6481 return;
6482 }
6483
Bram Moolenaare7566042005-06-17 22:00:15 +00006484#ifdef FEAT_EVAL
6485 if (name == '=')
6486 {
6487 char_u *p, *s;
6488
6489 p = vim_strnsave(str, (int)len);
6490 if (p == NULL)
6491 return;
6492 if (must_append)
6493 {
6494 s = concat_str(get_expr_line_src(), p);
6495 vim_free(p);
6496 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006497 }
6498 set_expr_line(p);
6499 return;
6500 }
6501#endif
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (name == '_') /* black hole: nothing to do */
6504 return;
6505
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006506 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6507 &yank_type) == FAIL)
6508 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006510 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006512 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513}
6514#endif /* FEAT_EVAL */
6515
6516#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6517/*
6518 * Put a string into a register. When the register is not empty, the string
6519 * is appended.
6520 */
6521 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006522str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006524 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 char_u *str; /* string to put in register */
6526 long len; /* length of string */
6527 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006528 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006530 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 int lnum;
6532 long start;
6533 long i;
6534 int extra;
6535 int newlines; /* number of lines added */
6536 int extraline = 0; /* extra line at the end */
6537 int append = FALSE; /* append to last line in register */
6538 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006539 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006543 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 y_ptr->y_size = 0;
6545
Bram Moolenaard44347f2011-06-19 01:14:29 +02006546 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006547 type = ((str_list || (len > 0 && (str[len - 1] == NL
6548 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006549 ? MLINE : MCHAR);
6550 else
6551 type = yank_type;
6552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 /*
6554 * Count the number of lines within the string
6555 */
6556 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006557 if (str_list)
6558 {
6559 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006562 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006564 for (i = 0; i < len; i++)
6565 if (str[i] == '\n')
6566 ++newlines;
6567 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6568 {
6569 extraline = 1;
6570 ++newlines; /* count extra newline at the end */
6571 }
6572 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6573 {
6574 append = TRUE;
6575 --newlines; /* uncount newline when appending first line */
6576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 }
6578
6579 /*
6580 * Allocate an array to hold the pointers to the new register lines.
6581 * If the register was not empty, move the existing lines to the new array.
6582 */
6583 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6584 * sizeof(char_u *), TRUE);
6585 if (pp == NULL) /* out of memory */
6586 return;
6587 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6588 pp[lnum] = y_ptr->y_array[lnum];
6589 vim_free(y_ptr->y_array);
6590 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 /*
6594 * Find the end of each line and save it into the array.
6595 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006596 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006598 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6599 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006600 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006601 pp[lnum] = vim_strnsave(*ss, i);
6602 if (i > maxlen)
6603 maxlen = i;
6604 }
6605 }
6606 else
6607 {
6608 for (start = 0; start < len + extraline; start += i + 1)
6609 {
6610 for (i = start; i < len; ++i) /* find the end of the line */
6611 if (str[i] == '\n')
6612 break;
6613 i -= start; /* i is now length of line */
6614 if (i > maxlen)
6615 maxlen = i;
6616 if (append)
6617 {
6618 --lnum;
6619 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6620 }
6621 else
6622 extra = 0;
6623 s = alloc((unsigned)(i + extra + 1));
6624 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006626 if (extra)
6627 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6628 if (append)
6629 vim_free(y_ptr->y_array[lnum]);
6630 if (i)
6631 mch_memmove(s + extra, str + start, (size_t)i);
6632 extra += i;
6633 s[extra] = NUL;
6634 y_ptr->y_array[lnum++] = s;
6635 while (--extra >= 0)
6636 {
6637 if (*s == NUL)
6638 *s = '\n'; /* replace NUL with newline */
6639 ++s;
6640 }
6641 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644 y_ptr->y_type = type;
6645 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (type == MBLOCK)
6647 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6648 else
6649 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650}
6651#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6652
6653 void
6654clear_oparg(oap)
6655 oparg_T *oap;
6656{
6657 vim_memset(oap, 0, sizeof(oparg_T));
6658}
6659
Bram Moolenaar7c626922005-02-07 22:01:03 +00006660static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006663 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 *
6665 * "Words" are counted by looking for boundaries between non-space and
6666 * space characters. (it seems to produce results that match 'wc'.)
6667 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006668 * Return value is byte count; word count for the line is added to "*wc".
6669 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 *
6671 * The function will only examine the first "limit" characters in the
6672 * line, stopping if it encounters an end-of-line (NUL byte). In that
6673 * case, eol_size will be added to the character count to account for
6674 * the size of the EOL character.
6675 */
6676 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006677line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 char_u *line;
6679 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006680 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 long limit;
6682 int eol_size;
6683{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006684 long i;
6685 long words = 0;
6686 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 int is_word = 0;
6688
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006689 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 {
6691 if (is_word)
6692 {
6693 if (vim_isspace(line[i]))
6694 {
6695 words++;
6696 is_word = 0;
6697 }
6698 }
6699 else if (!vim_isspace(line[i]))
6700 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006701 ++chars;
6702#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006703 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006704#else
6705 ++i;
6706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708
6709 if (is_word)
6710 words++;
6711 *wc += words;
6712
6713 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006714 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006717 chars += eol_size;
6718 }
6719 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 return i;
6721}
6722
6723/*
6724 * Give some info about the position of the cursor (for "g CTRL-G").
6725 * In Visual mode, give some info about the selected region. (In this case,
6726 * the *_count_cursor variables store running totals for the selection.)
6727 */
6728 void
6729cursor_pos_info()
6730{
6731 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006732 char_u buf1[50];
6733 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006735 long byte_count = 0;
6736 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 long char_count = 0;
6738 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 long word_count = 0;
6740 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006741 int eol_size;
6742 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 long line_count_selected = 0;
6744 pos_T min_pos, max_pos;
6745 oparg_T oparg;
6746 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
6748 /*
6749 * Compute the length of the file in characters.
6750 */
6751 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6752 {
6753 MSG(_(no_lines_msg));
6754 }
6755 else
6756 {
6757 if (get_fileformat(curbuf) == EOL_DOS)
6758 eol_size = 2;
6759 else
6760 eol_size = 1;
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (VIsual_active)
6763 {
6764 if (lt(VIsual, curwin->w_cursor))
6765 {
6766 min_pos = VIsual;
6767 max_pos = curwin->w_cursor;
6768 }
6769 else
6770 {
6771 min_pos = curwin->w_cursor;
6772 max_pos = VIsual;
6773 }
6774 if (*p_sel == 'e' && max_pos.col > 0)
6775 --max_pos.col;
6776
6777 if (VIsual_mode == Ctrl_V)
6778 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006779#ifdef FEAT_LINEBREAK
6780 char_u * saved_sbr = p_sbr;
6781
6782 /* Make 'sbr' empty for a moment to get the correct size. */
6783 p_sbr = empty_option;
6784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 oparg.is_VIsual = 1;
6786 oparg.block_mode = TRUE;
6787 oparg.op_type = OP_NOP;
6788 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006789 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006790#ifdef FEAT_LINEBREAK
6791 p_sbr = saved_sbr;
6792#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006793 if (curwin->w_curswant == MAXCOL)
6794 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 /* Swap the start, end vcol if needed */
6796 if (oparg.end_vcol < oparg.start_vcol)
6797 {
6798 oparg.end_vcol += oparg.start_vcol;
6799 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6800 oparg.end_vcol -= oparg.start_vcol;
6801 }
6802 }
6803 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6807 {
6808 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006809 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
6811 ui_breakcheck();
6812 if (got_int)
6813 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006814 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 /* Do extra processing for VIsual mode. */
6818 if (VIsual_active
6819 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6820 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006821 char_u *s = NULL;
6822 long len = 0L;
6823
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 switch (VIsual_mode)
6825 {
6826 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006827#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006831#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006833#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006834 s = bd.textstart;
6835 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 break;
6837 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006838 s = ml_get(lnum);
6839 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 break;
6841 case 'v':
6842 {
6843 colnr_T start_col = (lnum == min_pos.lnum)
6844 ? min_pos.col : 0;
6845 colnr_T end_col = (lnum == max_pos.lnum)
6846 ? max_pos.col - start_col + 1 : MAXCOL;
6847
Bram Moolenaardef9e822004-12-31 20:58:58 +00006848 s = ml_get(lnum) + start_col;
6849 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 break;
6852 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006853 if (s != NULL)
6854 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006855 byte_count_cursor += line_count_info(s, &word_count_cursor,
6856 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006857 if (lnum == curbuf->b_ml.ml_line_count
6858 && !curbuf->b_p_eol
6859 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006860 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006861 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
6866 /* In non-visual mode, check for the line the cursor is on */
6867 if (lnum == curwin->w_cursor.lnum)
6868 {
6869 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006870 char_count_cursor += char_count;
6871 byte_count_cursor = byte_count +
6872 line_count_info(ml_get(lnum),
6873 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 (long)(curwin->w_cursor.col + 1), eol_size);
6875 }
6876 }
6877 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006878 byte_count += line_count_info(ml_get(lnum), &word_count,
6879 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881
6882 /* Correction for when last line doesn't have an EOL. */
6883 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006884 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (VIsual_active)
6887 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006888 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006891 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006892 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6894 }
6895 else
6896 buf1[0] = NUL;
6897
Bram Moolenaar7c626922005-02-07 22:01:03 +00006898 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006899 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006900 vim_snprintf((char *)IObuff, IOSIZE,
6901 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 buf1, line_count_selected,
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 _("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 +00006909 buf1, line_count_selected,
6910 (long)curbuf->b_ml.ml_line_count,
6911 word_count_cursor, word_count,
6912 char_count_cursor, char_count,
6913 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
6915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {
6917 p = ml_get_curline();
6918 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006919 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006921 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
6922 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaar7c626922005-02-07 22:01:03 +00006924 if (char_count_cursor == byte_count_cursor
6925 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006926 vim_snprintf((char *)IObuff, IOSIZE,
6927 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 (char *)buf1, (char *)buf2,
6929 (long)curwin->w_cursor.lnum,
6930 (long)curbuf->b_ml.ml_line_count,
6931 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006932 byte_count_cursor, byte_count);
6933 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006934 vim_snprintf((char *)IObuff, IOSIZE,
6935 _("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 +00006936 (char *)buf1, (char *)buf2,
6937 (long)curwin->w_cursor.lnum,
6938 (long)curbuf->b_ml.ml_line_count,
6939 word_count_cursor, word_count,
6940 char_count_cursor, char_count,
6941 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 }
6943
6944#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006945 byte_count = bomb_size();
6946 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006948 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949#endif
6950 /* Don't shorten this message, the user asked for it. */
6951 p = p_shm;
6952 p_shm = (char_u *)"";
6953 msg(IObuff);
6954 p_shm = p;
6955 }
6956}