blob: 2b400916f74cb17ec29293499239c651a813d2d2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
53 * Each yank register is an array of pointers to lines.
54 */
55static struct yankreg
56{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 colnr_T y_width; /* only set if y_type == MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061} y_regs[NUM_REGISTERS];
62
63static struct yankreg *y_current; /* ptr to current yankreg */
64static int y_append; /* TRUE when appending */
65static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
66
67/*
68 * structure used by block_prep, op_delete and op_yank for blockwise operators
69 * also op_change, op_shift, op_insert, op_replace - AKelly
70 */
71struct block_def
72{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000073 int startspaces; /* 'extra' cols before first char */
74 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000076 char_u *textstart; /* pointer to 1st char (partially) in block */
77 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 colnr_T start_vcol; /* start col of 1st char wholly inside block */
79 colnr_T end_vcol; /* start col of 1st char wholly after block */
80#ifdef FEAT_VISUALEXTRA
81 int is_short; /* TRUE if line is too short to fit in block */
82 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
83 int is_oneChar; /* TRUE if block within one character */
84 int pre_whitesp; /* screen cols of ws before block */
85 int pre_whitesp_c; /* chars of ws before block */
86 colnr_T end_char_vcols; /* number of vcols of post-block char */
87#endif
88 colnr_T start_char_vcols; /* number of vcols of pre-block char */
89};
90
91#ifdef FEAT_VISUALEXTRA
92static void shift_block __ARGS((oparg_T *oap, int amount));
93static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
94#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000095static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000096static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000097static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
98 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +000099static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
101static void mb_adjust_opend __ARGS((oparg_T *oap));
102#endif
103static void free_yank __ARGS((long));
104static void free_yank_all __ARGS((void));
105static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
106#ifdef FEAT_CLIPBOARD
107static void copy_yank_reg __ARGS((struct yankreg *reg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static void may_set_selection __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110static void dis_msg __ARGS((char_u *p, int skip_esc));
Bram Moolenaar81340392012-06-06 16:12:59 +0200111#if defined(FEAT_COMMENTS) || defined(PROTO)
112static char_u *skip_comment __ARGS((char_u *line, int process, int include_space, int *is_comment));
113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar0eac8282014-04-12 12:26:36 +0200116static void str_to_reg __ARGS((struct yankreg *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118static int ends_in_white __ARGS((linenr_T lnum));
119#ifdef FEAT_COMMENTS
120static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
121static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
122#else
123static int fmt_check_par __ARGS((linenr_T));
124#endif
125
126/*
127 * The names of operators.
128 * IMPORTANT: Index must correspond with defines in vim.h!!!
129 * The third field indicates whether the operator always works on lines.
130 */
131static char opchars[][3] =
132{
133 {NUL, NUL, FALSE}, /* OP_NOP */
134 {'d', NUL, FALSE}, /* OP_DELETE */
135 {'y', NUL, FALSE}, /* OP_YANK */
136 {'c', NUL, FALSE}, /* OP_CHANGE */
137 {'<', NUL, TRUE}, /* OP_LSHIFT */
138 {'>', NUL, TRUE}, /* OP_RSHIFT */
139 {'!', NUL, TRUE}, /* OP_FILTER */
140 {'g', '~', FALSE}, /* OP_TILDE */
141 {'=', NUL, TRUE}, /* OP_INDENT */
142 {'g', 'q', TRUE}, /* OP_FORMAT */
143 {':', NUL, TRUE}, /* OP_COLON */
144 {'g', 'U', FALSE}, /* OP_UPPER */
145 {'g', 'u', FALSE}, /* OP_LOWER */
146 {'J', NUL, TRUE}, /* DO_JOIN */
147 {'g', 'J', TRUE}, /* DO_JOIN_NS */
148 {'g', '?', FALSE}, /* OP_ROT13 */
149 {'r', NUL, FALSE}, /* OP_REPLACE */
150 {'I', NUL, FALSE}, /* OP_INSERT */
151 {'A', NUL, FALSE}, /* OP_APPEND */
152 {'z', 'f', TRUE}, /* OP_FOLD */
153 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
154 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
155 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
156 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
157 {'z', 'd', TRUE}, /* OP_FOLDDEL */
158 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
159 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000160 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161};
162
163/*
164 * Translate a command name into an operator type.
165 * Must only be called with a valid operator name!
166 */
167 int
168get_op_type(char1, char2)
169 int char1;
170 int char2;
171{
172 int i;
173
174 if (char1 == 'r') /* ignore second character */
175 return OP_REPLACE;
176 if (char1 == '~') /* when tilde is an operator */
177 return OP_TILDE;
178 for (i = 0; ; ++i)
179 if (opchars[i][0] == char1 && opchars[i][1] == char2)
180 break;
181 return i;
182}
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Return TRUE if operator "op" always works on whole lines.
186 */
187 int
188op_on_lines(op)
189 int op;
190{
191 return opchars[op][2];
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
195 * Get first operator command character.
196 * Returns 'g' or 'z' if there is another command character.
197 */
198 int
199get_op_char(optype)
200 int optype;
201{
202 return opchars[optype][0];
203}
204
205/*
206 * Get second operator command character.
207 */
208 int
209get_extra_op_char(optype)
210 int optype;
211{
212 return opchars[optype][1];
213}
214
215/*
216 * op_shift - handle a shift operation
217 */
218 void
219op_shift(oap, curs_top, amount)
220 oparg_T *oap;
221 int curs_top;
222 int amount;
223{
224 long i;
225 int first_char;
226 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 if (u_save((linenr_T)(oap->start.lnum - 1),
230 (linenr_T)(oap->end.lnum + 1)) == FAIL)
231 return;
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (oap->block_mode)
234 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
236 for (i = oap->line_count; --i >= 0; )
237 {
238 first_char = *ml_get_curline();
239 if (first_char == NUL) /* empty line */
240 curwin->w_cursor.col = 0;
241#ifdef FEAT_VISUALEXTRA
242 else if (oap->block_mode)
243 shift_block(oap, amount);
244#endif
245 else
246 /* Move the line right if it doesn't start with '#', 'smartindent'
247 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
248#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
249 if (first_char != '#' || !preprocs_left())
250#endif
251 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000252 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
254 ++curwin->w_cursor.lnum;
255 }
256
257 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200258#ifdef FEAT_FOLDING
259 /* The cursor line is not in a closed fold */
260 foldOpenCursor();
261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (oap->block_mode)
264 {
265 curwin->w_cursor.lnum = oap->start.lnum;
266 curwin->w_cursor.col = block_col;
267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100268 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 curwin->w_cursor.lnum = oap->start.lnum;
271 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
272 }
273 else
274 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
275
276 if (oap->line_count > p_report)
277 {
278 if (oap->op_type == OP_RSHIFT)
279 s = (char_u *)">";
280 else
281 s = (char_u *)"<";
282 if (oap->line_count == 1)
283 {
284 if (amount == 1)
285 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
286 else
287 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
288 }
289 else
290 {
291 if (amount == 1)
292 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
293 oap->line_count, s);
294 else
295 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
296 oap->line_count, s, amount);
297 }
298 msg(IObuff);
299 }
300
301 /*
302 * Set "'[" and "']" marks.
303 */
304 curbuf->b_op_start = oap->start;
305 curbuf->b_op_end.lnum = oap->end.lnum;
306 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
307 if (curbuf->b_op_end.col > 0)
308 --curbuf->b_op_end.col;
309}
310
311/*
312 * shift the current line one shiftwidth left (if left != 0) or right
313 * leaves cursor on first blank in the line
314 */
315 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000316shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 int left;
318 int round;
319 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000320 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
322 int count;
323 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100324 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
326 count = get_indent(); /* get current indent */
327
328 if (round) /* round off indent */
329 {
330 i = count / p_sw; /* number of p_sw rounded down */
331 j = count % p_sw; /* extra spaces */
332 if (j && left) /* first remove extra spaces */
333 --amount;
334 if (left)
335 {
336 i -= amount;
337 if (i < 0)
338 i = 0;
339 }
340 else
341 i += amount;
342 count = i * p_sw;
343 }
344 else /* original vi indent */
345 {
346 if (left)
347 {
348 count -= p_sw * amount;
349 if (count < 0)
350 count = 0;
351 }
352 else
353 count += p_sw * amount;
354 }
355
356 /* Set new indent */
357#ifdef FEAT_VREPLACE
358 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000359 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 else
361#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000362 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363}
364
365#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
366/*
367 * Shift one line of the current block one shiftwidth right or left.
368 * Leaves cursor on first character in block.
369 */
370 static void
371shift_block(oap, amount)
372 oparg_T *oap;
373 int amount;
374{
375 int left = (oap->op_type == OP_LSHIFT);
376 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000377 int total;
378 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100380 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int p_ts = (int)curbuf->b_p_ts;
382 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int i = 0, j = 0;
386 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_RIGHTLEFT
388 int old_p_ri = p_ri;
389
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200390 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392
393 State = INSERT; /* don't want REPLACE for State */
394 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
395 if (bd.is_short)
396 return;
397
398 /* total is number of screen columns to be inserted/removed */
399 total = amount * p_sw;
400 oldp = ml_get_curline();
401
402 if (!left)
403 {
404 /*
405 * 1. Get start vcol
406 * 2. Total ws vcols
407 * 3. Divvy into TABs & spp
408 * 4. Construct new string
409 */
410 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
411 ws_vcol = bd.start_vcol - bd.pre_whitesp;
412 if (bd.startspaces)
413 {
414#ifdef FEAT_MBYTE
415 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000416 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000419 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 for ( ; vim_iswhite(*bd.textstart); )
422 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200423 /* TODO: is passing bd.textstart for start of the line OK? */
424 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
425 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 total += incr;
427 bd.start_vcol += incr;
428 }
429 /* OK, now total=all the VWS reqd, and textstart points at the 1st
430 * non-ws char in the block. */
431 if (!curbuf->b_p_et)
432 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
433 if (i)
434 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
435 else
436 j = total;
437 /* if we're splitting a TAB, allow for it */
438 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
439 len = (int)STRLEN(bd.textstart) + 1;
440 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
441 if (newp == NULL)
442 return;
443 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
444 mch_memmove(newp, oldp, (size_t)bd.textcol);
445 copy_chars(newp + bd.textcol, (size_t)i, TAB);
446 copy_spaces(newp + bd.textcol + i, (size_t)j);
447 /* the end */
448 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
449 }
450 else /* left */
451 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000452 colnr_T destination_col; /* column to which text in block will
453 be shifted */
454 char_u *verbatim_copy_end; /* end of the part of the line which is
455 copied verbatim */
456 colnr_T verbatim_copy_width;/* the (displayed) width of this part
457 of line */
458 unsigned fill; /* nr of spaces that replace a TAB */
459 unsigned new_line_len; /* the length of the line after the
460 block shift */
461 size_t block_space_width;
462 size_t shift_amount;
463 char_u *non_white = bd.textstart;
464 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000466 /*
467 * Firstly, let's find the first non-whitespace character that is
468 * displayed after the block's start column and the character's column
469 * number. Also, let's calculate the width of all the whitespace
470 * characters that are displayed in the block and precede the searched
471 * non-whitespace character.
472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000474 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
475 * the part of which is displayed at the block's beginning. Let's start
476 * searching from the next character. */
477 if (bd.startspaces)
478 mb_ptr_adv(non_white);
479
480 /* The character's column is in "bd.start_vcol". */
481 non_white_col = bd.start_vcol;
482
483 while (vim_iswhite(*non_white))
484 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200485 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000486 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 }
488
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000489 block_space_width = non_white_col - oap->start_vcol;
490 /* We will shift by "total" or "block_space_width", whichever is less.
491 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000492 shift_amount = (block_space_width < (size_t)total
493 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494
495 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000496 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497
498 /* Now let's find out how much of the beginning of the line we can
499 * reuse without modification. */
500 verbatim_copy_end = bd.textstart;
501 verbatim_copy_width = bd.start_vcol;
502
503 /* If "bd.startspaces" is set, "bd.textstart" points to the character
504 * preceding the block. We have to subtract its width to obtain its
505 * column number. */
506 if (bd.startspaces)
507 verbatim_copy_width -= bd.start_char_vcols;
508 while (verbatim_copy_width < destination_col)
509 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 char_u *line = verbatim_copy_end;
511
512 /* TODO: is passing verbatim_copy_end for start of the line OK? */
513 incr = lbr_chartabsize(line, verbatim_copy_end,
514 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515 if (verbatim_copy_width + incr > destination_col)
516 break;
517 verbatim_copy_width += incr;
518 mb_ptr_adv(verbatim_copy_end);
519 }
520
521 /* If "destination_col" is different from the width of the initial
522 * part of the line that will be copied, it means we encountered a tab
523 * character, which we will have to partly replace with spaces. */
524 fill = destination_col - verbatim_copy_width;
525
526 /* The replacement line will consist of:
527 * - the beginning of the original line up to "verbatim_copy_end",
528 * - "fill" number of spaces,
529 * - the rest of the line, pointed to by non_white. */
530 new_line_len = (unsigned)(verbatim_copy_end - oldp)
531 + fill
532 + (unsigned)STRLEN(non_white) + 1;
533
534 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (newp == NULL)
536 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000537 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
538 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
539 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 /* replace the line */
542 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
543 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
544 State = oldstate;
545 curwin->w_cursor.col = oldcol;
546#ifdef FEAT_RIGHTLEFT
547 p_ri = old_p_ri;
548#endif
549}
550#endif
551
552#ifdef FEAT_VISUALEXTRA
553/*
554 * Insert string "s" (b_insert ? before : after) block :AKelly
555 * Caller must prepare for undo.
556 */
557 static void
558block_insert(oap, s, b_insert, bdp)
559 oparg_T *oap;
560 char_u *s;
561 int b_insert;
562 struct block_def *bdp;
563{
564 int p_ts;
565 int count = 0; /* extra spaces to replace a cut TAB */
566 int spaces = 0; /* non-zero if cutting a TAB */
567 colnr_T offset; /* pointer along new line */
568 unsigned s_len; /* STRLEN(s) */
569 char_u *newp, *oldp; /* new, old lines */
570 linenr_T lnum; /* loop var */
571 int oldstate = State;
572
573 State = INSERT; /* don't want REPLACE for State */
574 s_len = (unsigned)STRLEN(s);
575
576 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
577 {
578 block_prep(oap, bdp, lnum, TRUE);
579 if (bdp->is_short && b_insert)
580 continue; /* OP_INSERT, line ends before block start */
581
582 oldp = ml_get(lnum);
583
584 if (b_insert)
585 {
586 p_ts = bdp->start_char_vcols;
587 spaces = bdp->startspaces;
588 if (spaces != 0)
589 count = p_ts - 1; /* we're cutting a TAB */
590 offset = bdp->textcol;
591 }
592 else /* append */
593 {
594 p_ts = bdp->end_char_vcols;
595 if (!bdp->is_short) /* spaces = padding after block */
596 {
597 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
598 if (spaces != 0)
599 count = p_ts - 1; /* we're cutting a TAB */
600 offset = bdp->textcol + bdp->textlen - (spaces != 0);
601 }
602 else /* spaces = padding to block edge */
603 {
604 /* if $ used, just append to EOL (ie spaces==0) */
605 if (!bdp->is_MAX)
606 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
607 count = spaces;
608 offset = bdp->textcol + bdp->textlen;
609 }
610 }
611
612 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
613 if (newp == NULL)
614 continue;
615
616 /* copy up to shifted part */
617 mch_memmove(newp, oldp, (size_t)(offset));
618 oldp += offset;
619
620 /* insert pre-padding */
621 copy_spaces(newp + offset, (size_t)spaces);
622
623 /* copy the new text */
624 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
625 offset += s_len;
626
627 if (spaces && !bdp->is_short)
628 {
629 /* insert post-padding */
630 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
631 /* We're splitting a TAB, don't copy it. */
632 oldp++;
633 /* We allowed for that TAB, remember this now */
634 count++;
635 }
636
637 if (spaces > 0)
638 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000639 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 ml_replace(lnum, newp, FALSE);
642
643 if (lnum == oap->end.lnum)
644 {
645 /* Set "']" mark to the end of the block instead of the end of
646 * the insert in the first line. */
647 curbuf->b_op_end.lnum = oap->end.lnum;
648 curbuf->b_op_end.col = offset;
649 }
650 } /* for all lnum */
651
652 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
653
654 State = oldstate;
655}
656#endif
657
658#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
659/*
660 * op_reindent - handle reindenting a block of lines.
661 */
662 void
663op_reindent(oap, how)
664 oparg_T *oap;
665 int (*how) __ARGS((void));
666{
667 long i;
668 char_u *l;
669 int count;
670 linenr_T first_changed = 0;
671 linenr_T last_changed = 0;
672 linenr_T start_lnum = curwin->w_cursor.lnum;
673
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000674 /* Don't even try when 'modifiable' is off. */
675 if (!curbuf->b_p_ma)
676 {
677 EMSG(_(e_modifiable));
678 return;
679 }
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 for (i = oap->line_count; --i >= 0 && !got_int; )
682 {
683 /* it's a slow thing to do, so give feedback so there's no worry that
684 * the computer's just hung. */
685
686 if (i > 1
687 && (i % 50 == 0 || i == oap->line_count - 1)
688 && oap->line_count > p_report)
689 smsg((char_u *)_("%ld lines to indent... "), i);
690
691 /*
692 * Be vi-compatible: For lisp indenting the first line is not
693 * indented, unless there is only one line.
694 */
695#ifdef FEAT_LISP
696 if (i != oap->line_count - 1 || oap->line_count == 1
697 || how != get_lisp_indent)
698#endif
699 {
700 l = skipwhite(ml_get_curline());
701 if (*l == NUL) /* empty or blank line */
702 count = 0;
703 else
704 count = how(); /* get the indent for this line */
705
706 if (set_indent(count, SIN_UNDO))
707 {
708 /* did change the indent, call changed_lines() later */
709 if (first_changed == 0)
710 first_changed = curwin->w_cursor.lnum;
711 last_changed = curwin->w_cursor.lnum;
712 }
713 }
714 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000715 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717
718 /* put cursor on first non-blank of indented line */
719 curwin->w_cursor.lnum = start_lnum;
720 beginline(BL_SOL | BL_FIX);
721
722 /* Mark changed lines so that they will be redrawn. When Visual
723 * highlighting was present, need to continue until the last line. When
724 * there is no change still need to remove the Visual highlighting. */
725 if (last_changed != 0)
726 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 else if (oap->is_VIsual)
730 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732 if (oap->line_count > p_report)
733 {
734 i = oap->line_count - (i + 1);
735 if (i == 1)
736 MSG(_("1 line indented "));
737 else
738 smsg((char_u *)_("%ld lines indented "), i);
739 }
740 /* set '[ and '] marks */
741 curbuf->b_op_start = oap->start;
742 curbuf->b_op_end = oap->end;
743}
744#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
745
746#if defined(FEAT_EVAL) || defined(PROTO)
747/*
748 * Keep the last expression line here, for repeating.
749 */
750static char_u *expr_line = NULL;
751
752/*
753 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
754 * Returns '=' when OK, NUL otherwise.
755 */
756 int
757get_expr_register()
758{
759 char_u *new_line;
760
761 new_line = getcmdline('=', 0L, 0);
762 if (new_line == NULL)
763 return NUL;
764 if (*new_line == NUL) /* use previous line */
765 vim_free(new_line);
766 else
767 set_expr_line(new_line);
768 return '=';
769}
770
771/*
772 * Set the expression for the '=' register.
773 * Argument must be an allocated string.
774 */
775 void
776set_expr_line(new_line)
777 char_u *new_line;
778{
779 vim_free(expr_line);
780 expr_line = new_line;
781}
782
783/*
784 * Get the result of the '=' register expression.
785 * Returns a pointer to allocated memory, or NULL for failure.
786 */
787 char_u *
788get_expr_line()
789{
790 char_u *expr_copy;
791 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000792 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 if (expr_line == NULL)
795 return NULL;
796
797 /* Make a copy of the expression, because evaluating it may cause it to be
798 * changed. */
799 expr_copy = vim_strsave(expr_line);
800 if (expr_copy == NULL)
801 return NULL;
802
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000803 /* When we are invoked recursively limit the evaluation to 10 levels.
804 * Then return the string as-is. */
805 if (nested >= 10)
806 return expr_copy;
807
808 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000809 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000810 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 vim_free(expr_copy);
812 return rv;
813}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000814
815/*
816 * Get the '=' register expression itself, without evaluating it.
817 */
818 char_u *
819get_expr_line_src()
820{
821 if (expr_line == NULL)
822 return NULL;
823 return vim_strsave(expr_line);
824}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#endif /* FEAT_EVAL */
826
827/*
828 * Check if 'regname' is a valid name of a yank register.
829 * Note: There is no check for 0 (default register), caller should do this
830 */
831 int
832valid_yank_reg(regname, writing)
833 int regname;
834 int writing; /* if TRUE check for writable registers */
835{
836 if ( (regname > 0 && ASCII_ISALNUM(regname))
837 || (!writing && vim_strchr((char_u *)
838#ifdef FEAT_EVAL
839 "/.%#:="
840#else
841 "/.%#:"
842#endif
843 , regname) != NULL)
844 || regname == '"'
845 || regname == '-'
846 || regname == '_'
847#ifdef FEAT_CLIPBOARD
848 || regname == '*'
849 || regname == '+'
850#endif
851#ifdef FEAT_DND
852 || (!writing && regname == '~')
853#endif
854 )
855 return TRUE;
856 return FALSE;
857}
858
859/*
860 * Set y_current and y_append, according to the value of "regname".
861 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000862 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 *
864 * If regname is 0 and writing, use register 0
865 * If regname is 0 and reading, use previous register
866 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000867 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868get_yank_register(regname, writing)
869 int regname;
870 int writing;
871{
872 int i;
873
874 y_append = FALSE;
875 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
876 {
877 y_current = y_previous;
878 return;
879 }
880 i = regname;
881 if (VIM_ISDIGIT(i))
882 i -= '0';
883 else if (ASCII_ISLOWER(i))
884 i = CharOrdLow(i) + 10;
885 else if (ASCII_ISUPPER(i))
886 {
887 i = CharOrdUp(i) + 10;
888 y_append = TRUE;
889 }
890 else if (regname == '-')
891 i = DELETION_REGISTER;
892#ifdef FEAT_CLIPBOARD
893 /* When selection is not available, use register 0 instead of '*' */
894 else if (clip_star.available && regname == '*')
895 i = STAR_REGISTER;
896 /* When clipboard is not available, use register 0 instead of '+' */
897 else if (clip_plus.available && regname == '+')
898 i = PLUS_REGISTER;
899#endif
900#ifdef FEAT_DND
901 else if (!writing && regname == '~')
902 i = TILDE_REGISTER;
903#endif
904 else /* not 0-9, a-z, A-Z or '-': use register 0 */
905 i = 0;
906 y_current = &(y_regs[i]);
907 if (writing) /* remember the register we write into for do_put() */
908 y_previous = y_current;
909}
910
Bram Moolenaar8299df92004-07-10 09:47:34 +0000911#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912/*
913 * When "regname" is a clipboard register, obtain the selection. If it's not
914 * available return zero, otherwise return "regname".
915 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000916 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917may_get_selection(regname)
918 int regname;
919{
920 if (regname == '*')
921 {
922 if (!clip_star.available)
923 regname = 0;
924 else
925 clip_get_selection(&clip_star);
926 }
927 else if (regname == '+')
928 {
929 if (!clip_plus.available)
930 regname = 0;
931 else
932 clip_get_selection(&clip_plus);
933 }
934 return regname;
935}
936#endif
937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * Obtain the contents of a "normal" register. The register is made empty.
940 * The returned pointer has allocated memory, use put_register() later.
941 */
942 void *
943get_register(name, copy)
944 int name;
945 int copy; /* make a copy, if FALSE make register empty. */
946{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000947 struct yankreg *reg;
948 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950#ifdef FEAT_CLIPBOARD
951 /* When Visual area changed, may have to update selection. Obtain the
952 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000953 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200955 if (clip_isautosel_star())
956 clip_update_selection(&clip_star);
957 may_get_selection(name);
958 }
959 if (name == '+' && clip_plus.available)
960 {
961 if (clip_isautosel_plus())
962 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 may_get_selection(name);
964 }
965#endif
966
967 get_yank_register(name, 0);
968 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
969 if (reg != NULL)
970 {
971 *reg = *y_current;
972 if (copy)
973 {
974 /* If we run out of memory some or all of the lines are empty. */
975 if (reg->y_size == 0)
976 reg->y_array = NULL;
977 else
978 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
979 * reg->y_size));
980 if (reg->y_array != NULL)
981 {
982 for (i = 0; i < reg->y_size; ++i)
983 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
984 }
985 }
986 else
987 y_current->y_array = NULL;
988 }
989 return (void *)reg;
990}
991
992/*
Bram Moolenaar0a307462007-12-01 20:13:05 +0000993 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 */
995 void
996put_register(name, reg)
997 int name;
998 void *reg;
999{
1000 get_yank_register(name, 0);
1001 free_yank_all();
1002 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001003 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001005#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /* Send text written to clipboard register to the clipboard. */
1007 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001010
1011 void
1012free_register(reg)
1013 void *reg;
1014{
1015 struct yankreg tmp;
1016
1017 tmp = *y_current;
1018 *y_current = *(struct yankreg *)reg;
1019 free_yank_all();
1020 vim_free(reg);
1021 *y_current = tmp;
1022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024#if defined(FEAT_MOUSE) || defined(PROTO)
1025/*
1026 * return TRUE if the current yank register has type MLINE
1027 */
1028 int
1029yank_register_mline(regname)
1030 int regname;
1031{
1032 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1033 return FALSE;
1034 if (regname == '_') /* black hole is always empty */
1035 return FALSE;
1036 get_yank_register(regname, FALSE);
1037 return (y_current->y_type == MLINE);
1038}
1039#endif
1040
1041/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001042 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001044 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 */
1046 int
1047do_record(c)
1048 int c;
1049{
Bram Moolenaard55de222007-05-06 13:38:48 +00001050 char_u *p;
1051 static int regname;
1052 struct yankreg *old_y_previous, *old_y_current;
1053 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
1055 if (Recording == FALSE) /* start recording */
1056 {
1057 /* registers 0-9, a-z and " are allowed */
1058 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1059 retval = FAIL;
1060 else
1061 {
1062 Recording = TRUE;
1063 showmode();
1064 regname = c;
1065 retval = OK;
1066 }
1067 }
1068 else /* stop recording */
1069 {
1070 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1072 * needs to be removed again to put it in a register. exec_reg then
1073 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 Recording = FALSE;
1076 MSG("");
1077 p = get_recorded();
1078 if (p == NULL)
1079 retval = FAIL;
1080 else
1081 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001082 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1083 vim_unescape_csi(p);
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 /*
1086 * We don't want to change the default register here, so save and
1087 * restore the current register name.
1088 */
1089 old_y_previous = y_previous;
1090 old_y_current = y_current;
1091
1092 retval = stuff_yank(regname, p);
1093
1094 y_previous = old_y_previous;
1095 y_current = old_y_current;
1096 }
1097 }
1098 return retval;
1099}
1100
1101/*
1102 * Stuff string "p" into yank register "regname" as a single line (append if
1103 * uppercase). "p" must have been alloced.
1104 *
1105 * return FAIL for failure, OK otherwise
1106 */
1107 static int
1108stuff_yank(regname, p)
1109 int regname;
1110 char_u *p;
1111{
1112 char_u *lp;
1113 char_u **pp;
1114
1115 /* check for read-only register */
1116 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1117 {
1118 vim_free(p);
1119 return FAIL;
1120 }
1121 if (regname == '_') /* black hole: don't do anything */
1122 {
1123 vim_free(p);
1124 return OK;
1125 }
1126 get_yank_register(regname, TRUE);
1127 if (y_append && y_current->y_array != NULL)
1128 {
1129 pp = &(y_current->y_array[y_current->y_size - 1]);
1130 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1131 if (lp == NULL)
1132 {
1133 vim_free(p);
1134 return FAIL;
1135 }
1136 STRCPY(lp, *pp);
1137 STRCAT(lp, p);
1138 vim_free(p);
1139 vim_free(*pp);
1140 *pp = lp;
1141 }
1142 else
1143 {
1144 free_yank_all();
1145 if ((y_current->y_array =
1146 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1147 {
1148 vim_free(p);
1149 return FAIL;
1150 }
1151 y_current->y_array[0] = p;
1152 y_current->y_size = 1;
1153 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1154 }
1155 return OK;
1156}
1157
Bram Moolenaar42b94362009-05-26 16:12:37 +00001158static int execreg_lastc = NUL;
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160/*
1161 * execute a yank register: copy it into the stuff buffer
1162 *
1163 * return FAIL for failure, OK otherwise
1164 */
1165 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001166do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 int regname;
1168 int colon; /* insert ':' before each line */
1169 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001170 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 long i;
1173 char_u *p;
1174 int retval = OK;
1175 int remap;
1176
1177 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001178 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001179 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001180 {
1181 EMSG(_("E748: No previously used register"));
1182 return FAIL;
1183 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001184 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 /* check for valid regname */
1187 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001188 {
1189 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001191 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001192 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
1194#ifdef FEAT_CLIPBOARD
1195 regname = may_get_selection(regname);
1196#endif
1197
1198 if (regname == '_') /* black hole: don't stuff anything */
1199 return OK;
1200
1201#ifdef FEAT_CMDHIST
1202 if (regname == ':') /* use last command line */
1203 {
1204 if (last_cmdline == NULL)
1205 {
1206 EMSG(_(e_nolastcmd));
1207 return FAIL;
1208 }
1209 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1210 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001211 /* Escape all control characters with a CTRL-V */
1212 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001213 (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001214 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001215 {
1216 /* When in Visual mode "'<,'>" will be prepended to the command.
1217 * Remove it when it's already there. */
1218 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001219 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001220 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001221 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001222 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001223 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225#endif
1226#ifdef FEAT_EVAL
1227 else if (regname == '=')
1228 {
1229 p = get_expr_line();
1230 if (p == NULL)
1231 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001232 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 vim_free(p);
1234 }
1235#endif
1236 else if (regname == '.') /* use last inserted text */
1237 {
1238 p = get_last_insert_save();
1239 if (p == NULL)
1240 {
1241 EMSG(_(e_noinstext));
1242 return FAIL;
1243 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001244 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 vim_free(p);
1246 }
1247 else
1248 {
1249 get_yank_register(regname, FALSE);
1250 if (y_current->y_array == NULL)
1251 return FAIL;
1252
1253 /* Disallow remaping for ":@r". */
1254 remap = colon ? REMAP_NONE : REMAP_YES;
1255
1256 /*
1257 * Insert lines into typeahead buffer, from last one to first one.
1258 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001259 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 for (i = y_current->y_size; --i >= 0; )
1261 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001262 char_u *escaped;
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /* insert NL between lines and after last line if type is MLINE */
1265 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1266 || addcr)
1267 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001268 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 return FAIL;
1270 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001271 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1272 if (escaped == NULL)
1273 return FAIL;
1274 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1275 vim_free(escaped);
1276 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001278 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 == FAIL)
1280 return FAIL;
1281 }
1282 Exec_reg = TRUE; /* disable the 'q' command */
1283 }
1284 return retval;
1285}
1286
1287/*
1288 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1289 * used only after other typeahead has been processed.
1290 */
1291 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001292put_reedit_in_typebuf(silent)
1293 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 char_u buf[3];
1296
1297 if (restart_edit != NUL)
1298 {
1299 if (restart_edit == 'V')
1300 {
1301 buf[0] = 'g';
1302 buf[1] = 'R';
1303 buf[2] = NUL;
1304 }
1305 else
1306 {
1307 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1308 buf[1] = NUL;
1309 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001310 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 restart_edit = NUL;
1312 }
1313}
1314
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001315/*
1316 * Insert register contents "s" into the typeahead buffer, so that it will be
1317 * executed again.
1318 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1319 * no remapping.
1320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001322put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001324 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001326 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 int retval = OK;
1329
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001330 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001332 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001334 {
1335 char_u *p;
1336
1337 if (esc)
1338 p = vim_strsave_escape_csi(s);
1339 else
1340 p = s;
1341 if (p == NULL)
1342 retval = FAIL;
1343 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001344 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1345 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001346 if (esc)
1347 vim_free(p);
1348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001350 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 return retval;
1352}
1353
1354/*
1355 * Insert a yank register: copy it into the Read buffer.
1356 * Used by CTRL-R command and middle mouse button in insert mode.
1357 *
1358 * return FAIL for failure, OK otherwise
1359 */
1360 int
1361insert_reg(regname, literally)
1362 int regname;
1363 int literally; /* insert literally, not as if typed */
1364{
1365 long i;
1366 int retval = OK;
1367 char_u *arg;
1368 int allocated;
1369
1370 /*
1371 * It is possible to get into an endless loop by having CTRL-R a in
1372 * register a and then, in insert mode, doing CTRL-R a.
1373 * If you hit CTRL-C, the loop will be broken here.
1374 */
1375 ui_breakcheck();
1376 if (got_int)
1377 return FAIL;
1378
1379 /* check for valid regname */
1380 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1381 return FAIL;
1382
1383#ifdef FEAT_CLIPBOARD
1384 regname = may_get_selection(regname);
1385#endif
1386
1387 if (regname == '.') /* insert last inserted text */
1388 retval = stuff_inserted(NUL, 1L, TRUE);
1389 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1390 {
1391 if (arg == NULL)
1392 return FAIL;
1393 stuffescaped(arg, literally);
1394 if (allocated)
1395 vim_free(arg);
1396 }
1397 else /* name or number register */
1398 {
1399 get_yank_register(regname, FALSE);
1400 if (y_current->y_array == NULL)
1401 retval = FAIL;
1402 else
1403 {
1404 for (i = 0; i < y_current->y_size; ++i)
1405 {
1406 stuffescaped(y_current->y_array[i], literally);
1407 /*
1408 * Insert a newline between lines and after last line if
1409 * y_type is MLINE.
1410 */
1411 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1412 stuffcharReadbuff('\n');
1413 }
1414 }
1415 }
1416
1417 return retval;
1418}
1419
1420/*
1421 * Stuff a string into the typeahead buffer, such that edit() will insert it
1422 * literally ("literally" TRUE) or interpret is as typed characters.
1423 */
1424 static void
1425stuffescaped(arg, literally)
1426 char_u *arg;
1427 int literally;
1428{
1429 int c;
1430 char_u *start;
1431
1432 while (*arg != NUL)
1433 {
1434 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1435 * stuff K_SPECIAL to get the effect of a special key when "literally"
1436 * is TRUE. */
1437 start = arg;
1438 while ((*arg >= ' '
1439#ifndef EBCDIC
1440 && *arg < DEL /* EBCDIC: chars above space are normal */
1441#endif
1442 )
1443 || (*arg == K_SPECIAL && !literally))
1444 ++arg;
1445 if (arg > start)
1446 stuffReadbuffLen(start, (long)(arg - start));
1447
1448 /* stuff a single special character */
1449 if (*arg != NUL)
1450 {
1451#ifdef FEAT_MBYTE
1452 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001453 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 else
1455#endif
1456 c = *arg++;
1457 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1458 stuffcharReadbuff(Ctrl_V);
1459 stuffcharReadbuff(c);
1460 }
1461 }
1462}
1463
1464/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001465 * If "regname" is a special register, return TRUE and store a pointer to its
1466 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001468 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469get_spec_reg(regname, argp, allocated, errmsg)
1470 int regname;
1471 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001472 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 int errmsg; /* give error message when failing */
1474{
1475 int cnt;
1476
1477 *argp = NULL;
1478 *allocated = FALSE;
1479 switch (regname)
1480 {
1481 case '%': /* file name */
1482 if (errmsg)
1483 check_fname(); /* will give emsg if not set */
1484 *argp = curbuf->b_fname;
1485 return TRUE;
1486
1487 case '#': /* alternate file name */
1488 *argp = getaltfname(errmsg); /* may give emsg if not set */
1489 return TRUE;
1490
1491#ifdef FEAT_EVAL
1492 case '=': /* result of expression */
1493 *argp = get_expr_line();
1494 *allocated = TRUE;
1495 return TRUE;
1496#endif
1497
1498 case ':': /* last command line */
1499 if (last_cmdline == NULL && errmsg)
1500 EMSG(_(e_nolastcmd));
1501 *argp = last_cmdline;
1502 return TRUE;
1503
1504 case '/': /* last search-pattern */
1505 if (last_search_pat() == NULL && errmsg)
1506 EMSG(_(e_noprevre));
1507 *argp = last_search_pat();
1508 return TRUE;
1509
1510 case '.': /* last inserted text */
1511 *argp = get_last_insert_save();
1512 *allocated = TRUE;
1513 if (*argp == NULL && errmsg)
1514 EMSG(_(e_noinstext));
1515 return TRUE;
1516
1517#ifdef FEAT_SEARCHPATH
1518 case Ctrl_F: /* Filename under cursor */
1519 case Ctrl_P: /* Path under cursor, expand via "path" */
1520 if (!errmsg)
1521 return FALSE;
1522 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001523 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 *allocated = TRUE;
1525 return TRUE;
1526#endif
1527
1528 case Ctrl_W: /* word under cursor */
1529 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1530 if (!errmsg)
1531 return FALSE;
1532 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1533 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1534 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1535 *allocated = TRUE;
1536 return TRUE;
1537
1538 case '_': /* black hole: always empty */
1539 *argp = (char_u *)"";
1540 return TRUE;
1541 }
1542
1543 return FALSE;
1544}
1545
1546/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001547 * Paste a yank register into the command line.
1548 * Only for non-special registers.
1549 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 * insert_reg() can't be used here, because special characters from the
1551 * register contents will be interpreted as commands.
1552 *
1553 * return FAIL for failure, OK otherwise
1554 */
1555 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001556cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 int regname;
1558 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001559 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
1563 get_yank_register(regname, FALSE);
1564 if (y_current->y_array == NULL)
1565 return FAIL;
1566
1567 for (i = 0; i < y_current->y_size; ++i)
1568 {
1569 cmdline_paste_str(y_current->y_array[i], literally);
1570
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001571 /* Insert ^M between lines and after last line if type is MLINE.
1572 * Don't do this when "remcr" is TRUE and the next line is empty. */
1573 if (y_current->y_type == MLINE
1574 || (i < y_current->y_size - 1
1575 && !(remcr
1576 && i == y_current->y_size - 2
1577 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 cmdline_paste_str((char_u *)"\r", literally);
1579
1580 /* Check for CTRL-C, in case someone tries to paste a few thousand
1581 * lines and gets bored. */
1582 ui_breakcheck();
1583 if (got_int)
1584 return FAIL;
1585 }
1586 return OK;
1587}
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1590/*
1591 * Adjust the register name pointed to with "rp" for the clipboard being
1592 * used always and the clipboard being available.
1593 */
1594 void
1595adjust_clip_reg(rp)
1596 int *rp;
1597{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001598 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1599 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001600 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1601 {
1602 if (clip_unnamed != 0)
1603 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001604 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001605 else
1606 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1607 ? '+' : '*';
1608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (!clip_star.available && *rp == '*')
1610 *rp = 0;
1611 if (!clip_plus.available && *rp == '+')
1612 *rp = 0;
1613}
1614#endif
1615
1616/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001617 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001619 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
1621 int
1622op_delete(oap)
1623 oparg_T *oap;
1624{
1625 int n;
1626 linenr_T lnum;
1627 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 char_u *newp, *oldp;
1629 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1631 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001632 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1635 return OK;
1636
1637 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1638 if (oap->empty)
1639 return u_save_cursor();
1640
1641 if (!curbuf->b_p_ma)
1642 {
1643 EMSG(_(e_modifiable));
1644 return FAIL;
1645 }
1646
1647#ifdef FEAT_CLIPBOARD
1648 adjust_clip_reg(&oap->regname);
1649#endif
1650
1651#ifdef FEAT_MBYTE
1652 if (has_mbyte)
1653 mb_adjust_opend(oap);
1654#endif
1655
Bram Moolenaard04b7502010-07-08 22:27:55 +02001656 /*
1657 * Imitate the strange Vi behaviour: If the delete spans more than one
1658 * line and motion_type == MCHAR and the result is a blank line, make the
1659 * delete linewise. Don't do this for the change command or Visual mode.
1660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001663 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001665 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 && oap->op_type == OP_DELETE)
1667 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001668 ptr = ml_get(oap->end.lnum) + oap->end.col;
1669 if (*ptr != NUL)
1670 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 ptr = skipwhite(ptr);
1672 if (*ptr == NUL && inindent(0))
1673 oap->motion_type = MLINE;
1674 }
1675
Bram Moolenaard04b7502010-07-08 22:27:55 +02001676 /*
1677 * Check for trying to delete (e.g. "D") in an empty line.
1678 * Note: For the change operator it is ok.
1679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if ( oap->motion_type == MCHAR
1681 && oap->line_count == 1
1682 && oap->op_type == OP_DELETE
1683 && *ml_get(oap->start.lnum) == NUL)
1684 {
1685 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001686 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 * 'cpoptions' (Vi compatible).
1688 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001689#ifdef FEAT_VIRTUALEDIT
1690 if (virtual_op)
1691 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1692 * marks as if it happened. */
1693 goto setmarks;
1694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1696 beep_flush();
1697 return OK;
1698 }
1699
Bram Moolenaard04b7502010-07-08 22:27:55 +02001700 /*
1701 * Do a yank of whatever we're about to delete.
1702 * If a yank register was specified, put the deleted text into that
1703 * register. For the black hole register '_' don't yank anything.
1704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 if (oap->regname != '_')
1706 {
1707 if (oap->regname != 0)
1708 {
1709 /* check for read-only register */
1710 if (!valid_yank_reg(oap->regname, TRUE))
1711 {
1712 beep_flush();
1713 return OK;
1714 }
1715 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1716 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1717 did_yank = TRUE;
1718 }
1719
1720 /*
1721 * Put deleted text into register 1 and shift number registers if the
1722 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001723 * Use the register name from before adjust_clip_reg() may have
1724 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001726 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 || oap->line_count > 1 || oap->use_reg_one)
1728 {
1729 y_current = &y_regs[9];
1730 free_yank_all(); /* free register nine */
1731 for (n = 9; n > 1; --n)
1732 y_regs[n] = y_regs[n - 1];
1733 y_previous = y_current = &y_regs[1];
1734 y_regs[1].y_array = NULL; /* set register one to empty */
1735 if (op_yank(oap, TRUE, FALSE) == OK)
1736 did_yank = TRUE;
1737 }
1738
Bram Moolenaar84298db2012-04-20 13:46:08 +02001739 /* Yank into small delete register when no named register specified
1740 * and the delete is within one line. */
1741 if ((
1742#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001743 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1744 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001745#endif
1746 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 && oap->line_count == 1)
1748 {
1749 oap->regname = '-';
1750 get_yank_register(oap->regname, TRUE);
1751 if (op_yank(oap, TRUE, FALSE) == OK)
1752 did_yank = TRUE;
1753 oap->regname = 0;
1754 }
1755
1756 /*
1757 * If there's too much stuff to fit in the yank register, then get a
1758 * confirmation before doing the delete. This is crude, but simple.
1759 * And it avoids doing a delete of something we can't put back if we
1760 * want.
1761 */
1762 if (!did_yank)
1763 {
1764 int msg_silent_save = msg_silent;
1765
1766 msg_silent = 0; /* must display the prompt */
1767 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1768 msg_silent = msg_silent_save;
1769 if (n != 'y')
1770 {
1771 EMSG(_(e_abort));
1772 return FAIL;
1773 }
1774 }
1775 }
1776
Bram Moolenaard04b7502010-07-08 22:27:55 +02001777 /*
1778 * block mode delete
1779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (oap->block_mode)
1781 {
1782 if (u_save((linenr_T)(oap->start.lnum - 1),
1783 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1784 return FAIL;
1785
1786 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1787 {
1788 block_prep(oap, &bd, lnum, TRUE);
1789 if (bd.textlen == 0) /* nothing to delete */
1790 continue;
1791
1792 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1793 if (lnum == curwin->w_cursor.lnum)
1794 {
1795 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1796# ifdef FEAT_VIRTUALEDIT
1797 curwin->w_cursor.coladd = 0;
1798# endif
1799 }
1800
1801 /* n == number of chars deleted
1802 * If we delete a TAB, it may be replaced by several characters.
1803 * Thus the number of characters may increase!
1804 */
1805 n = bd.textlen - bd.startspaces - bd.endspaces;
1806 oldp = ml_get(lnum);
1807 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1808 if (newp == NULL)
1809 continue;
1810 /* copy up to deleted part */
1811 mch_memmove(newp, oldp, (size_t)bd.textcol);
1812 /* insert spaces */
1813 copy_spaces(newp + bd.textcol,
1814 (size_t)(bd.startspaces + bd.endspaces));
1815 /* copy the part after the deleted part */
1816 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001817 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 /* replace the line */
1819 ml_replace(lnum, newp, FALSE);
1820 }
1821
1822 check_cursor_col();
1823 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1824 oap->end.lnum + 1, 0L);
1825 oap->line_count = 0; /* no lines deleted */
1826 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001827 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 if (oap->op_type == OP_CHANGE)
1830 {
1831 /* Delete the lines except the first one. Temporarily move the
1832 * cursor to the next line. Save the current line number, if the
1833 * last line is deleted it may be changed.
1834 */
1835 if (oap->line_count > 1)
1836 {
1837 lnum = curwin->w_cursor.lnum;
1838 ++curwin->w_cursor.lnum;
1839 del_lines((long)(oap->line_count - 1), TRUE);
1840 curwin->w_cursor.lnum = lnum;
1841 }
1842 if (u_save_cursor() == FAIL)
1843 return FAIL;
1844 if (curbuf->b_p_ai) /* don't delete indent */
1845 {
1846 beginline(BL_WHITE); /* cursor on first non-white */
1847 did_ai = TRUE; /* delete the indent when ESC hit */
1848 ai_col = curwin->w_cursor.col;
1849 }
1850 else
1851 beginline(0); /* cursor in column 0 */
1852 truncate_line(FALSE); /* delete the rest of the line */
1853 /* leave cursor past last char in line */
1854 if (oap->line_count > 1)
1855 u_clearline(); /* "U" command not possible after "2cc" */
1856 }
1857 else
1858 {
1859 del_lines(oap->line_count, TRUE);
1860 beginline(BL_WHITE | BL_FIX);
1861 u_clearline(); /* "U" command not possible after "dd" */
1862 }
1863 }
1864 else
1865 {
1866#ifdef FEAT_VIRTUALEDIT
1867 if (virtual_op)
1868 {
1869 int endcol = 0;
1870
1871 /* For virtualedit: break the tabs that are partly included. */
1872 if (gchar_pos(&oap->start) == '\t')
1873 {
1874 if (u_save_cursor() == FAIL) /* save first line for undo */
1875 return FAIL;
1876 if (oap->line_count == 1)
1877 endcol = getviscol2(oap->end.col, oap->end.coladd);
1878 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1879 oap->start = curwin->w_cursor;
1880 if (oap->line_count == 1)
1881 {
1882 coladvance(endcol);
1883 oap->end.col = curwin->w_cursor.col;
1884 oap->end.coladd = curwin->w_cursor.coladd;
1885 curwin->w_cursor = oap->start;
1886 }
1887 }
1888
1889 /* Break a tab only when it's included in the area. */
1890 if (gchar_pos(&oap->end) == '\t'
1891 && (int)oap->end.coladd < oap->inclusive)
1892 {
1893 /* save last line for undo */
1894 if (u_save((linenr_T)(oap->end.lnum - 1),
1895 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1896 return FAIL;
1897 curwin->w_cursor = oap->end;
1898 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1899 oap->end = curwin->w_cursor;
1900 curwin->w_cursor = oap->start;
1901 }
1902 }
1903#endif
1904
1905 if (oap->line_count == 1) /* delete characters within one line */
1906 {
1907 if (u_save_cursor() == FAIL) /* save line for undo */
1908 return FAIL;
1909
1910 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001911 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 && oap->op_type == OP_CHANGE
1913 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001914 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 display_dollar(oap->end.col - !oap->inclusive);
1916
1917 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1918
1919#ifdef FEAT_VIRTUALEDIT
1920 if (virtual_op)
1921 {
1922 /* fix up things for virtualedit-delete:
1923 * break the tabs which are going to get in our way
1924 */
1925 char_u *curline = ml_get_curline();
1926 int len = (int)STRLEN(curline);
1927
1928 if (oap->end.coladd != 0
1929 && (int)oap->end.col >= len - 1
1930 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1931 n++;
1932 /* Delete at least one char (e.g, when on a control char). */
1933 if (n == 0 && oap->start.coladd != oap->end.coladd)
1934 n = 1;
1935
1936 /* When deleted a char in the line, reset coladd. */
1937 if (gchar_cursor() != NUL)
1938 curwin->w_cursor.coladd = 0;
1939 }
1940#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001941 if (oap->op_type == OP_DELETE
1942 && oap->inclusive
1943 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001944 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1945 {
1946 /* Special case: gH<Del> deletes the last line. */
1947 del_lines(1L, FALSE);
1948 }
1949 else
1950 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001951 (void)del_bytes((long)n, !virtual_op,
1952 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
1955 else /* delete characters between lines */
1956 {
1957 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001958 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 /* save deleted and changed lines for undo */
1961 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1962 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1963 return FAIL;
1964
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001965 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 truncate_line(TRUE); /* delete from cursor to end of line */
1967
1968 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1969 ++curwin->w_cursor.lnum;
1970 del_lines((long)(oap->line_count - 2), FALSE);
1971
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001972 if (delete_last_line)
1973 oap->end.lnum = curbuf->b_ml.ml_line_count;
1974
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001975 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001976 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001977 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1978 {
1979 /* Special case: gH<Del> deletes the last line. */
1980 del_lines(1L, FALSE);
1981 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01001982 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1983 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001984 }
1985 else
1986 {
1987 /* delete from start of line until op_end */
1988 curwin->w_cursor.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001989 (void)del_bytes((long)n, !virtual_op,
1990 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001991 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1992 }
1993 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001994 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
1996 }
1997
1998 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1999
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002000#ifdef FEAT_VIRTUALEDIT
2001setmarks:
2002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (oap->block_mode)
2004 {
2005 curbuf->b_op_end.lnum = oap->end.lnum;
2006 curbuf->b_op_end.col = oap->start.col;
2007 }
2008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 curbuf->b_op_end = oap->start;
2010 curbuf->b_op_start = oap->start;
2011
2012 return OK;
2013}
2014
2015#ifdef FEAT_MBYTE
2016/*
2017 * Adjust end of operating area for ending on a multi-byte character.
2018 * Used for deletion.
2019 */
2020 static void
2021mb_adjust_opend(oap)
2022 oparg_T *oap;
2023{
2024 char_u *p;
2025
2026 if (oap->inclusive)
2027 {
2028 p = ml_get(oap->end.lnum);
2029 oap->end.col += mb_tail_off(p, p + oap->end.col);
2030 }
2031}
2032#endif
2033
2034#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2035/*
2036 * Replace a whole area with one character.
2037 */
2038 int
2039op_replace(oap, c)
2040 oparg_T *oap;
2041 int c;
2042{
2043 int n, numc;
2044#ifdef FEAT_MBYTE
2045 int num_chars;
2046#endif
2047 char_u *newp, *oldp;
2048 size_t oldlen;
2049 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002050 char_u *after_p = NULL;
2051 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
2053 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2054 return OK; /* nothing to do */
2055
Bram Moolenaard9820532013-11-04 01:41:17 +01002056 if (had_ctrl_v_cr)
2057 c = (c == -1 ? '\r' : '\n');
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059#ifdef FEAT_MBYTE
2060 if (has_mbyte)
2061 mb_adjust_opend(oap);
2062#endif
2063
2064 if (u_save((linenr_T)(oap->start.lnum - 1),
2065 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2066 return FAIL;
2067
2068 /*
2069 * block mode replace
2070 */
2071 if (oap->block_mode)
2072 {
2073 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2074 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2075 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002076 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2078 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2079 continue; /* nothing to replace */
2080
2081 /* n == number of extra chars required
2082 * If we split a TAB, it may be replaced by several characters.
2083 * Thus the number of characters may increase!
2084 */
2085#ifdef FEAT_VIRTUALEDIT
2086 /* If the range starts in virtual space, count the initial
2087 * coladd offset as part of "startspaces" */
2088 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2089 {
2090 pos_T vpos;
2091
Bram Moolenaara1381de2009-11-03 15:44:21 +00002092 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 getvpos(&vpos, oap->start_vcol);
2094 bd.startspaces += vpos.coladd;
2095 n = bd.startspaces;
2096 }
2097 else
2098#endif
2099 /* allow for pre spaces */
2100 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2101
2102 /* allow for post spp */
2103 n += (bd.endspaces
2104#ifdef FEAT_VIRTUALEDIT
2105 && !bd.is_oneChar
2106#endif
2107 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2108 /* Figure out how many characters to replace. */
2109 numc = oap->end_vcol - oap->start_vcol + 1;
2110 if (bd.is_short && (!virtual_op || bd.is_MAX))
2111 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2112
2113#ifdef FEAT_MBYTE
2114 /* A double-wide character can be replaced only up to half the
2115 * times. */
2116 if ((*mb_char2cells)(c) > 1)
2117 {
2118 if ((numc & 1) && !bd.is_short)
2119 {
2120 ++bd.endspaces;
2121 ++n;
2122 }
2123 numc = numc / 2;
2124 }
2125
2126 /* Compute bytes needed, move character count to num_chars. */
2127 num_chars = numc;
2128 numc *= (*mb_char2len)(c);
2129#endif
2130 /* oldlen includes textlen, so don't double count */
2131 n += numc - bd.textlen;
2132
2133 oldp = ml_get_curline();
2134 oldlen = STRLEN(oldp);
2135 newp = alloc_check((unsigned)oldlen + 1 + n);
2136 if (newp == NULL)
2137 continue;
2138 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2139 /* copy up to deleted part */
2140 mch_memmove(newp, oldp, (size_t)bd.textcol);
2141 oldp += bd.textcol + bd.textlen;
2142 /* insert pre-spaces */
2143 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2144 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002145 /* -1/-2 is used for entering CR literally. */
2146 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002148#ifdef FEAT_MBYTE
2149 if (has_mbyte)
2150 {
2151 n = (int)STRLEN(newp);
2152 while (--num_chars >= 0)
2153 n += (*mb_char2bytes)(c, newp + n);
2154 }
2155 else
2156#endif
2157 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2158 if (!bd.is_short)
2159 {
2160 /* insert post-spaces */
2161 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2162 /* copy the part after the changed part */
2163 STRMOVE(newp + STRLEN(newp), oldp);
2164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002168 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002169 after_p = alloc_check(
2170 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002171 if (after_p != NULL)
2172 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174 /* replace the line */
2175 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002176 if (after_p != NULL)
2177 {
2178 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2179 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2180 oap->end.lnum++;
2181 vim_free(after_p);
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184 }
2185 else
2186 {
2187 /*
2188 * MCHAR and MLINE motion replace.
2189 */
2190 if (oap->motion_type == MLINE)
2191 {
2192 oap->start.col = 0;
2193 curwin->w_cursor.col = 0;
2194 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2195 if (oap->end.col)
2196 --oap->end.col;
2197 }
2198 else if (!oap->inclusive)
2199 dec(&(oap->end));
2200
2201 while (ltoreq(curwin->w_cursor, oap->end))
2202 {
2203 n = gchar_cursor();
2204 if (n != NUL)
2205 {
2206#ifdef FEAT_MBYTE
2207 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2208 {
2209 /* This is slow, but it handles replacing a single-byte
2210 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002211 if (curwin->w_cursor.lnum == oap->end.lnum)
2212 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 n = State;
2214 State = REPLACE;
2215 ins_char(c);
2216 State = n;
2217 /* Backup to the replaced character. */
2218 dec_cursor();
2219 }
2220 else
2221#endif
2222 {
2223#ifdef FEAT_VIRTUALEDIT
2224 if (n == TAB)
2225 {
2226 int end_vcol = 0;
2227
2228 if (curwin->w_cursor.lnum == oap->end.lnum)
2229 {
2230 /* oap->end has to be recalculated when
2231 * the tab breaks */
2232 end_vcol = getviscol2(oap->end.col,
2233 oap->end.coladd);
2234 }
2235 coladvance_force(getviscol());
2236 if (curwin->w_cursor.lnum == oap->end.lnum)
2237 getvpos(&oap->end, end_vcol);
2238 }
2239#endif
2240 pchar(curwin->w_cursor, c);
2241 }
2242 }
2243#ifdef FEAT_VIRTUALEDIT
2244 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2245 {
2246 int virtcols = oap->end.coladd;
2247
2248 if (curwin->w_cursor.lnum == oap->start.lnum
2249 && oap->start.col == oap->end.col && oap->start.coladd)
2250 virtcols -= oap->start.coladd;
2251
2252 /* oap->end has been trimmed so it's effectively inclusive;
2253 * as a result an extra +1 must be counted so we don't
2254 * trample the NUL byte. */
2255 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2256 curwin->w_cursor.col -= (virtcols + 1);
2257 for (; virtcols >= 0; virtcols--)
2258 {
2259 pchar(curwin->w_cursor, c);
2260 if (inc(&curwin->w_cursor) == -1)
2261 break;
2262 }
2263 }
2264#endif
2265
2266 /* Advance to next character, stop at the end of the file. */
2267 if (inc_cursor() == -1)
2268 break;
2269 }
2270 }
2271
2272 curwin->w_cursor = oap->start;
2273 check_cursor();
2274 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2275
2276 /* Set "'[" and "']" marks. */
2277 curbuf->b_op_start = oap->start;
2278 curbuf->b_op_end = oap->end;
2279
2280 return OK;
2281}
2282#endif
2283
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002284static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286/*
2287 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2288 */
2289 void
2290op_tilde(oap)
2291 oparg_T *oap;
2292{
2293 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002295 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 if (u_save((linenr_T)(oap->start.lnum - 1),
2298 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2299 return;
2300
2301 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (oap->block_mode) /* Visual block mode */
2303 {
2304 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2305 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002306 int one_change;
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 block_prep(oap, &bd, pos.lnum, FALSE);
2309 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002310 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2311 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002312
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002313#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002314 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2317
Bram Moolenaar009b2592004-10-24 19:18:58 +00002318 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2319 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002321 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325 if (did_change)
2326 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2327 }
2328 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 if (oap->motion_type == MLINE)
2331 {
2332 oap->start.col = 0;
2333 pos.col = 0;
2334 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2335 if (oap->end.col)
2336 --oap->end.col;
2337 }
2338 else if (!oap->inclusive)
2339 dec(&(oap->end));
2340
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002341 if (pos.lnum == oap->end.lnum)
2342 did_change = swapchars(oap->op_type, &pos,
2343 oap->end.col - pos.col + 1);
2344 else
2345 for (;;)
2346 {
2347 did_change |= swapchars(oap->op_type, &pos,
2348 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2349 (int)STRLEN(ml_get_pos(&pos)));
2350 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2351 break;
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (did_change)
2354 {
2355 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2356 0L);
2357#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002358 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 char_u *ptr;
2361 int count;
2362
2363 pos = oap->start;
2364 while (pos.lnum < oap->end.lnum)
2365 {
2366 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002367 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002368 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002370 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 pos.col = 0;
2372 pos.lnum++;
2373 }
2374 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2375 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002376 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002378 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
2380#endif
2381 }
2382 }
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (!did_change && oap->is_VIsual)
2385 /* No change: need to remove the Visual selection */
2386 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 /*
2389 * Set '[ and '] marks.
2390 */
2391 curbuf->b_op_start = oap->start;
2392 curbuf->b_op_end = oap->end;
2393
2394 if (oap->line_count > p_report)
2395 {
2396 if (oap->line_count == 1)
2397 MSG(_("1 line changed"));
2398 else
2399 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2400 }
2401}
2402
2403/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002404 * Invoke swapchar() on "length" bytes at position "pos".
2405 * "pos" is advanced to just after the changed characters.
2406 * "length" is rounded up to include the whole last multi-byte character.
2407 * Also works correctly when the number of bytes changes.
2408 * Returns TRUE if some character was changed.
2409 */
2410 static int
2411swapchars(op_type, pos, length)
2412 int op_type;
2413 pos_T *pos;
2414 int length;
2415{
2416 int todo;
2417 int did_change = 0;
2418
2419 for (todo = length; todo > 0; --todo)
2420 {
2421# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002422 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002423 {
2424 int len = (*mb_ptr2len)(ml_get_pos(pos));
2425
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002426 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002427 if (len > 0)
2428 todo -= len - 1;
2429 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002430# endif
2431 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002432 if (inc(pos) == -1) /* at end of file */
2433 break;
2434 }
2435 return did_change;
2436}
2437
2438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * If op_type == OP_UPPER: make uppercase,
2440 * if op_type == OP_LOWER: make lowercase,
2441 * if op_type == OP_ROT13: do rot13 encoding,
2442 * else swap case of character at 'pos'
2443 * returns TRUE when something actually changed.
2444 */
2445 int
2446swapchar(op_type, pos)
2447 int op_type;
2448 pos_T *pos;
2449{
2450 int c;
2451 int nc;
2452
2453 c = gchar_pos(pos);
2454
2455 /* Only do rot13 encoding for ASCII characters. */
2456 if (c >= 0x80 && op_type == OP_ROT13)
2457 return FALSE;
2458
2459#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002460 if (op_type == OP_UPPER && c == 0xdf
2461 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002462 {
2463 pos_T sp = curwin->w_cursor;
2464
2465 /* Special handling of German sharp s: change to "SS". */
2466 curwin->w_cursor = *pos;
2467 del_char(FALSE);
2468 ins_char('S');
2469 ins_char('S');
2470 curwin->w_cursor = sp;
2471 inc(pos);
2472 }
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2475 return FALSE;
2476#endif
2477 nc = c;
2478 if (MB_ISLOWER(c))
2479 {
2480 if (op_type == OP_ROT13)
2481 nc = ROT13(c, 'a');
2482 else if (op_type != OP_LOWER)
2483 nc = MB_TOUPPER(c);
2484 }
2485 else if (MB_ISUPPER(c))
2486 {
2487 if (op_type == OP_ROT13)
2488 nc = ROT13(c, 'A');
2489 else if (op_type != OP_UPPER)
2490 nc = MB_TOLOWER(c);
2491 }
2492 if (nc != c)
2493 {
2494#ifdef FEAT_MBYTE
2495 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2496 {
2497 pos_T sp = curwin->w_cursor;
2498
2499 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002500 /* don't use del_char(), it also removes composing chars */
2501 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 ins_char(nc);
2503 curwin->w_cursor = sp;
2504 }
2505 else
2506#endif
2507 pchar(*pos, nc);
2508 return TRUE;
2509 }
2510 return FALSE;
2511}
2512
2513#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2514/*
2515 * op_insert - Insert and append operators for Visual mode.
2516 */
2517 void
2518op_insert(oap, count1)
2519 oparg_T *oap;
2520 long count1;
2521{
2522 long ins_len, pre_textlen = 0;
2523 char_u *firstline, *ins_text;
2524 struct block_def bd;
2525 int i;
2526
2527 /* edit() changes this - record it for OP_APPEND */
2528 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2529
2530 /* vis block is still marked. Get rid of it now. */
2531 curwin->w_cursor.lnum = oap->start.lnum;
2532 update_screen(INVERTED);
2533
2534 if (oap->block_mode)
2535 {
2536#ifdef FEAT_VIRTUALEDIT
2537 /* When 'virtualedit' is used, need to insert the extra spaces before
2538 * doing block_prep(). When only "block" is used, virtual edit is
2539 * already disabled, but still need it when calling
2540 * coladvance_force(). */
2541 if (curwin->w_cursor.coladd > 0)
2542 {
2543 int old_ve_flags = ve_flags;
2544
2545 ve_flags = VE_ALL;
2546 if (u_save_cursor() == FAIL)
2547 return;
2548 coladvance_force(oap->op_type == OP_APPEND
2549 ? oap->end_vcol + 1 : getviscol());
2550 if (oap->op_type == OP_APPEND)
2551 --curwin->w_cursor.col;
2552 ve_flags = old_ve_flags;
2553 }
2554#endif
2555 /* Get the info about the block before entering the text */
2556 block_prep(oap, &bd, oap->start.lnum, TRUE);
2557 firstline = ml_get(oap->start.lnum) + bd.textcol;
2558 if (oap->op_type == OP_APPEND)
2559 firstline += bd.textlen;
2560 pre_textlen = (long)STRLEN(firstline);
2561 }
2562
2563 if (oap->op_type == OP_APPEND)
2564 {
2565 if (oap->block_mode
2566#ifdef FEAT_VIRTUALEDIT
2567 && curwin->w_cursor.coladd == 0
2568#endif
2569 )
2570 {
2571 /* Move the cursor to the character right of the block. */
2572 curwin->w_set_curswant = TRUE;
2573 while (*ml_get_cursor() != NUL
2574 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2575 ++curwin->w_cursor.col;
2576 if (bd.is_short && !bd.is_MAX)
2577 {
2578 /* First line was too short, make it longer and adjust the
2579 * values in "bd". */
2580 if (u_save_cursor() == FAIL)
2581 return;
2582 for (i = 0; i < bd.endspaces; ++i)
2583 ins_char(' ');
2584 bd.textlen += bd.endspaces;
2585 }
2586 }
2587 else
2588 {
2589 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002590 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 /* Works just like an 'i'nsert on the next character. */
2593 if (!lineempty(curwin->w_cursor.lnum)
2594 && oap->start_vcol != oap->end_vcol)
2595 inc_cursor();
2596 }
2597 }
2598
2599 edit(NUL, FALSE, (linenr_T)count1);
2600
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002601 /* If user has moved off this line, we don't know what to do, so do
2602 * nothing.
2603 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2604 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 return;
2606
2607 if (oap->block_mode)
2608 {
2609 struct block_def bd2;
2610
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002611 /* The user may have moved the cursor before inserting something, try
2612 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002613 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002614 {
2615 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002616 && oap->start.col
2617#ifdef FEAT_VIRTUALEDIT
2618 + oap->start.coladd
2619#endif
2620 != curbuf->b_op_start_orig.col
2621#ifdef FEAT_VIRTUALEDIT
2622 + curbuf->b_op_start_orig.coladd
2623#endif
2624 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002625 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002626 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002627 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2628 - oap->start_vcol;
2629 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2630 }
2631 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002632 && oap->end.col
2633#ifdef FEAT_VIRTUALEDIT
2634 + oap->end.coladd
2635#endif
2636 >= curbuf->b_op_start_orig.col
2637#ifdef FEAT_VIRTUALEDIT
2638 + curbuf->b_op_start_orig.coladd
2639#endif
2640 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002641 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002642 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002643 /* reset pre_textlen to the value of OP_INSERT */
2644 pre_textlen += bd.textlen;
2645 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2646 - oap->start_vcol;
2647 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2648 oap->op_type = OP_INSERT;
2649 }
2650 }
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 /*
2653 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002654 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * Don't do this when "$" used, end-of-line will have changed.
2656 */
2657 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2658 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2659 {
2660 if (oap->op_type == OP_APPEND)
2661 {
2662 pre_textlen += bd2.textlen - bd.textlen;
2663 if (bd2.endspaces)
2664 --bd2.textlen;
2665 }
2666 bd.textcol = bd2.textcol;
2667 bd.textlen = bd2.textlen;
2668 }
2669
2670 /*
2671 * Subsequent calls to ml_get() flush the firstline data - take a
2672 * copy of the required string.
2673 */
2674 firstline = ml_get(oap->start.lnum) + bd.textcol;
2675 if (oap->op_type == OP_APPEND)
2676 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002677 if (pre_textlen >= 0
2678 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
2680 ins_text = vim_strnsave(firstline, (int)ins_len);
2681 if (ins_text != NULL)
2682 {
2683 /* block handled here */
2684 if (u_save(oap->start.lnum,
2685 (linenr_T)(oap->end.lnum + 1)) == OK)
2686 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2687 &bd);
2688
2689 curwin->w_cursor.col = oap->start.col;
2690 check_cursor();
2691 vim_free(ins_text);
2692 }
2693 }
2694 }
2695}
2696#endif
2697
2698/*
2699 * op_change - handle a change operation
2700 *
2701 * return TRUE if edit() returns because of a CTRL-O command
2702 */
2703 int
2704op_change(oap)
2705 oparg_T *oap;
2706{
2707 colnr_T l;
2708 int retval;
2709#ifdef FEAT_VISUALEXTRA
2710 long offset;
2711 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002712 long ins_len;
2713 long pre_textlen = 0;
2714 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 char_u *firstline;
2716 char_u *ins_text, *newp, *oldp;
2717 struct block_def bd;
2718#endif
2719
2720 l = oap->start.col;
2721 if (oap->motion_type == MLINE)
2722 {
2723 l = 0;
2724#ifdef FEAT_SMARTINDENT
2725 if (!p_paste && curbuf->b_p_si
2726# ifdef FEAT_CINDENT
2727 && !curbuf->b_p_cin
2728# endif
2729 )
2730 can_si = TRUE; /* It's like opening a new line, do si */
2731#endif
2732 }
2733
2734 /* First delete the text in the region. In an empty buffer only need to
2735 * save for undo */
2736 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2737 {
2738 if (u_save_cursor() == FAIL)
2739 return FALSE;
2740 }
2741 else if (op_delete(oap) == FAIL)
2742 return FALSE;
2743
2744 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2745 && !virtual_op)
2746 inc_cursor();
2747
2748#ifdef FEAT_VISUALEXTRA
2749 /* check for still on same line (<CR> in inserted text meaningless) */
2750 /* skip blank lines too */
2751 if (oap->block_mode)
2752 {
2753# ifdef FEAT_VIRTUALEDIT
2754 /* Add spaces before getting the current line length. */
2755 if (virtual_op && (curwin->w_cursor.coladd > 0
2756 || gchar_cursor() == NUL))
2757 coladvance_force(getviscol());
2758# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002759 firstline = ml_get(oap->start.lnum);
2760 pre_textlen = (long)STRLEN(firstline);
2761 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 bd.textcol = curwin->w_cursor.col;
2763 }
2764#endif
2765
2766#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2767 if (oap->motion_type == MLINE)
2768 fix_indent();
2769#endif
2770
2771 retval = edit(NUL, FALSE, (linenr_T)1);
2772
2773#ifdef FEAT_VISUALEXTRA
2774 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002775 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002777 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002779 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002781 /* Auto-indenting may have changed the indent. If the cursor was past
2782 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002784 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002786 long new_indent = (long)(skipwhite(firstline) - firstline);
2787
2788 pre_textlen += new_indent - pre_indent;
2789 bd.textcol += new_indent - pre_indent;
2790 }
2791
2792 ins_len = (long)STRLEN(firstline) - pre_textlen;
2793 if (ins_len > 0)
2794 {
2795 /* Subsequent calls to ml_get() flush the firstline data - take a
2796 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2798 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002799 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2801 linenr++)
2802 {
2803 block_prep(oap, &bd, linenr, TRUE);
2804 if (!bd.is_short || virtual_op)
2805 {
2806# ifdef FEAT_VIRTUALEDIT
2807 pos_T vpos;
2808
2809 /* If the block starts in virtual space, count the
2810 * initial coladd offset as part of "startspaces" */
2811 if (bd.is_short)
2812 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002813 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816 else
2817 vpos.coladd = 0;
2818# endif
2819 oldp = ml_get(linenr);
2820 newp = alloc_check((unsigned)(STRLEN(oldp)
2821# ifdef FEAT_VIRTUALEDIT
2822 + vpos.coladd
2823# endif
2824 + ins_len + 1));
2825 if (newp == NULL)
2826 continue;
2827 /* copy up to block start */
2828 mch_memmove(newp, oldp, (size_t)bd.textcol);
2829 offset = bd.textcol;
2830# ifdef FEAT_VIRTUALEDIT
2831 copy_spaces(newp + offset, (size_t)vpos.coladd);
2832 offset += vpos.coladd;
2833# endif
2834 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2835 offset += ins_len;
2836 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002837 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 ml_replace(linenr, newp, FALSE);
2839 }
2840 }
2841 check_cursor();
2842
2843 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2844 }
2845 vim_free(ins_text);
2846 }
2847 }
2848#endif
2849
2850 return retval;
2851}
2852
2853/*
2854 * set all the yank registers to empty (called from main())
2855 */
2856 void
2857init_yank()
2858{
2859 int i;
2860
2861 for (i = 0; i < NUM_REGISTERS; ++i)
2862 y_regs[i].y_array = NULL;
2863}
2864
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002865#if defined(EXITFREE) || defined(PROTO)
2866 void
2867clear_registers()
2868{
2869 int i;
2870
2871 for (i = 0; i < NUM_REGISTERS; ++i)
2872 {
2873 y_current = &y_regs[i];
2874 if (y_current->y_array != NULL)
2875 free_yank_all();
2876 }
2877}
2878#endif
2879
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880/*
2881 * Free "n" lines from the current yank register.
2882 * Called for normal freeing and in case of error.
2883 */
2884 static void
2885free_yank(n)
2886 long n;
2887{
2888 if (y_current->y_array != NULL)
2889 {
2890 long i;
2891
2892 for (i = n; --i >= 0; )
2893 {
2894#ifdef AMIGA /* only for very slow machines */
2895 if ((i & 1023) == 1023) /* this may take a while */
2896 {
2897 /*
2898 * This message should never cause a hit-return message.
2899 * Overwrite this message with any next message.
2900 */
2901 ++no_wait_return;
2902 smsg((char_u *)_("freeing %ld lines"), i + 1);
2903 --no_wait_return;
2904 msg_didout = FALSE;
2905 msg_col = 0;
2906 }
2907#endif
2908 vim_free(y_current->y_array[i]);
2909 }
2910 vim_free(y_current->y_array);
2911 y_current->y_array = NULL;
2912#ifdef AMIGA
2913 if (n >= 1000)
2914 MSG("");
2915#endif
2916 }
2917}
2918
2919 static void
2920free_yank_all()
2921{
2922 free_yank(y_current->y_size);
2923}
2924
2925/*
2926 * Yank the text between "oap->start" and "oap->end" into a yank register.
2927 * If we are to append (uppercase register), we first yank into a new yank
2928 * register and then concatenate the old and the new one (so we keep the old
2929 * one in case of out-of-memory).
2930 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002931 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 */
2933 int
2934op_yank(oap, deleting, mess)
2935 oparg_T *oap;
2936 int deleting;
2937 int mess;
2938{
2939 long y_idx; /* index in y_array[] */
2940 struct yankreg *curr; /* copy of y_current */
2941 struct yankreg newreg; /* new yank register when appending */
2942 char_u **new_ptr;
2943 linenr_T lnum; /* current line number */
2944 long j;
2945 int yanktype = oap->motion_type;
2946 long yanklines = oap->line_count;
2947 linenr_T yankendlnum = oap->end.lnum;
2948 char_u *p;
2949 char_u *pnew;
2950 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002951#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002952 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 /* check for read-only register */
2956 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2957 {
2958 beep_flush();
2959 return FAIL;
2960 }
2961 if (oap->regname == '_') /* black hole: nothing to do */
2962 return OK;
2963
2964#ifdef FEAT_CLIPBOARD
2965 if (!clip_star.available && oap->regname == '*')
2966 oap->regname = 0;
2967 else if (!clip_plus.available && oap->regname == '+')
2968 oap->regname = 0;
2969#endif
2970
2971 if (!deleting) /* op_delete() already set y_current */
2972 get_yank_register(oap->regname, TRUE);
2973
2974 curr = y_current;
2975 /* append to existing contents */
2976 if (y_append && y_current->y_array != NULL)
2977 y_current = &newreg;
2978 else
2979 free_yank_all(); /* free previously yanked lines */
2980
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002981 /*
2982 * If the cursor was in column 1 before and after the movement, and the
2983 * operator is not inclusive, the yank is always linewise.
2984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 if ( oap->motion_type == MCHAR
2986 && oap->start.col == 0
2987 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002989 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 && oap->end.col == 0
2991 && yanklines > 1)
2992 {
2993 yanktype = MLINE;
2994 --yankendlnum;
2995 --yanklines;
2996 }
2997
2998 y_current->y_size = yanklines;
2999 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3002 yanklines), TRUE);
3003
3004 if (y_current->y_array == NULL)
3005 {
3006 y_current = curr;
3007 return FAIL;
3008 }
3009
3010 y_idx = 0;
3011 lnum = oap->start.lnum;
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (oap->block_mode)
3014 {
3015 /* Visual block mode */
3016 y_current->y_type = MBLOCK; /* set the yank register type */
3017 y_current->y_width = oap->end_vcol - oap->start_vcol;
3018
3019 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3020 y_current->y_width--;
3021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3024 {
3025 switch (y_current->y_type)
3026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 case MBLOCK:
3028 block_prep(oap, &bd, lnum, FALSE);
3029 if (yank_copy_line(&bd, y_idx) == FAIL)
3030 goto fail;
3031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 case MLINE:
3034 if ((y_current->y_array[y_idx] =
3035 vim_strsave(ml_get(lnum))) == NULL)
3036 goto fail;
3037 break;
3038
3039 case MCHAR:
3040 {
3041 colnr_T startcol = 0, endcol = MAXCOL;
3042#ifdef FEAT_VIRTUALEDIT
3043 int is_oneChar = FALSE;
3044 colnr_T cs, ce;
3045#endif
3046 p = ml_get(lnum);
3047 bd.startspaces = 0;
3048 bd.endspaces = 0;
3049
3050 if (lnum == oap->start.lnum)
3051 {
3052 startcol = oap->start.col;
3053#ifdef FEAT_VIRTUALEDIT
3054 if (virtual_op)
3055 {
3056 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3057 if (ce != cs && oap->start.coladd > 0)
3058 {
3059 /* Part of a tab selected -- but don't
3060 * double-count it. */
3061 bd.startspaces = (ce - cs + 1)
3062 - oap->start.coladd;
3063 startcol++;
3064 }
3065 }
3066#endif
3067 }
3068
3069 if (lnum == oap->end.lnum)
3070 {
3071 endcol = oap->end.col;
3072#ifdef FEAT_VIRTUALEDIT
3073 if (virtual_op)
3074 {
3075 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3076 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3077# ifdef FEAT_MBYTE
3078 /* Don't add space for double-wide
3079 * char; endcol will be on last byte
3080 * of multi-byte char. */
3081 && (*mb_head_off)(p, p + endcol) == 0
3082# endif
3083 ))
3084 {
3085 if (oap->start.lnum == oap->end.lnum
3086 && oap->start.col == oap->end.col)
3087 {
3088 /* Special case: inside a single char */
3089 is_oneChar = TRUE;
3090 bd.startspaces = oap->end.coladd
3091 - oap->start.coladd + oap->inclusive;
3092 endcol = startcol;
3093 }
3094 else
3095 {
3096 bd.endspaces = oap->end.coladd
3097 + oap->inclusive;
3098 endcol -= oap->inclusive;
3099 }
3100 }
3101 }
3102#endif
3103 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003104 if (endcol == MAXCOL)
3105 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 if (startcol > endcol
3107#ifdef FEAT_VIRTUALEDIT
3108 || is_oneChar
3109#endif
3110 )
3111 bd.textlen = 0;
3112 else
3113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 bd.textlen = endcol - startcol + oap->inclusive;
3115 }
3116 bd.textstart = p + startcol;
3117 if (yank_copy_line(&bd, y_idx) == FAIL)
3118 goto fail;
3119 break;
3120 }
3121 /* NOTREACHED */
3122 }
3123 }
3124
3125 if (curr != y_current) /* append the new block to the old block */
3126 {
3127 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3128 (curr->y_size + y_current->y_size)), TRUE);
3129 if (new_ptr == NULL)
3130 goto fail;
3131 for (j = 0; j < curr->y_size; ++j)
3132 new_ptr[j] = curr->y_array[j];
3133 vim_free(curr->y_array);
3134 curr->y_array = new_ptr;
3135
3136 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3137 curr->y_type = MLINE;
3138
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003139 /* Concatenate the last line of the old block with the first line of
3140 * the new block, unless being Vi compatible. */
3141 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3144 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3145 if (pnew == NULL)
3146 {
3147 y_idx = y_current->y_size - 1;
3148 goto fail;
3149 }
3150 STRCPY(pnew, curr->y_array[--j]);
3151 STRCAT(pnew, y_current->y_array[0]);
3152 vim_free(curr->y_array[j]);
3153 vim_free(y_current->y_array[0]);
3154 curr->y_array[j++] = pnew;
3155 y_idx = 1;
3156 }
3157 else
3158 y_idx = 0;
3159 while (y_idx < y_current->y_size)
3160 curr->y_array[j++] = y_current->y_array[y_idx++];
3161 curr->y_size = j;
3162 vim_free(y_current->y_array);
3163 y_current = curr;
3164 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003165 if (curwin->w_p_rnu)
3166 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (mess) /* Display message about yank? */
3168 {
3169 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 && yanklines == 1)
3172 yanklines = 0;
3173 /* Some versions of Vi use ">=" here, some don't... */
3174 if (yanklines > p_report)
3175 {
3176 /* redisplay now, so message is not deleted */
3177 update_topline_redraw();
3178 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003179 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003180 if (oap->block_mode)
3181 MSG(_("block of 1 line yanked"));
3182 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003183 MSG(_("1 line yanked"));
3184 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003185 else if (oap->block_mode)
3186 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else
3188 smsg((char_u *)_("%ld lines yanked"), yanklines);
3189 }
3190 }
3191
3192 /*
3193 * Set "'[" and "']" marks.
3194 */
3195 curbuf->b_op_start = oap->start;
3196 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003197 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003198 {
3199 curbuf->b_op_start.col = 0;
3200 curbuf->b_op_end.col = MAXCOL;
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203#ifdef FEAT_CLIPBOARD
3204 /*
3205 * If we were yanking to the '*' register, send result to clipboard.
3206 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3207 * to the '*' register.
3208 */
3209 if (clip_star.available
3210 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003211 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003212 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
3214 if (curr != &(y_regs[STAR_REGISTER]))
3215 /* Copy the text from register 0 to the clipboard register. */
3216 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3217
3218 clip_own_selection(&clip_star);
3219 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003220# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003221 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224
3225# ifdef FEAT_X11
3226 /*
3227 * If we were yanking to the '+' register, send result to selection.
3228 * Also copy to the '*' register, in case auto-select is off.
3229 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003230 if (clip_plus.available
3231 && (curr == &(y_regs[PLUS_REGISTER])
3232 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003233 && ((clip_unnamed | clip_unnamed_saved) &
3234 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003236 if (curr != &(y_regs[PLUS_REGISTER]))
3237 /* Copy the text from register 0 to the clipboard register. */
3238 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 clip_own_selection(&clip_plus);
3241 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003242 if (!clip_isautosel_star() && !did_star
3243 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 {
3245 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3246 clip_own_selection(&clip_star);
3247 clip_gen_set_selection(&clip_star);
3248 }
3249 }
3250# endif
3251#endif
3252
3253 return OK;
3254
3255fail: /* free the allocated lines */
3256 free_yank(y_idx + 1);
3257 y_current = curr;
3258 return FAIL;
3259}
3260
3261 static int
3262yank_copy_line(bd, y_idx)
3263 struct block_def *bd;
3264 long y_idx;
3265{
3266 char_u *pnew;
3267
3268 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3269 == NULL)
3270 return FAIL;
3271 y_current->y_array[y_idx] = pnew;
3272 copy_spaces(pnew, (size_t)bd->startspaces);
3273 pnew += bd->startspaces;
3274 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3275 pnew += bd->textlen;
3276 copy_spaces(pnew, (size_t)bd->endspaces);
3277 pnew += bd->endspaces;
3278 *pnew = NUL;
3279 return OK;
3280}
3281
3282#ifdef FEAT_CLIPBOARD
3283/*
3284 * Make a copy of the y_current register to register "reg".
3285 */
3286 static void
3287copy_yank_reg(reg)
3288 struct yankreg *reg;
3289{
3290 struct yankreg *curr = y_current;
3291 long j;
3292
3293 y_current = reg;
3294 free_yank_all();
3295 *y_current = *curr;
3296 y_current->y_array = (char_u **)lalloc_clear(
3297 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3298 if (y_current->y_array == NULL)
3299 y_current->y_size = 0;
3300 else
3301 for (j = 0; j < y_current->y_size; ++j)
3302 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3303 {
3304 free_yank(j);
3305 y_current->y_size = 0;
3306 break;
3307 }
3308 y_current = curr;
3309}
3310#endif
3311
3312/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003313 * Put contents of register "regname" into the text.
3314 * Caller must check "regname" to be valid!
3315 * "flags": PUT_FIXINDENT make indent look nice
3316 * PUT_CURSEND leave cursor after end of new text
3317 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 */
3319 void
3320do_put(regname, dir, count, flags)
3321 int regname;
3322 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3323 long count;
3324 int flags;
3325{
3326 char_u *ptr;
3327 char_u *newp, *oldp;
3328 int yanklen;
3329 int totlen = 0; /* init for gcc */
3330 linenr_T lnum;
3331 colnr_T col;
3332 long i; /* index in y_array[] */
3333 int y_type;
3334 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 int oldlen;
3336 long y_width = 0;
3337 colnr_T vcol;
3338 int delcount;
3339 int incr = 0;
3340 long j;
3341 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 char_u **y_array = NULL;
3343 long nr_lines = 0;
3344 pos_T new_cursor;
3345 int indent;
3346 int orig_indent = 0; /* init for gcc */
3347 int indent_diff = 0; /* init for gcc */
3348 int first_indent = TRUE;
3349 int lendiff = 0;
3350 pos_T old_pos;
3351 char_u *insert_string = NULL;
3352 int allocated = FALSE;
3353 long cnt;
3354
3355#ifdef FEAT_CLIPBOARD
3356 /* Adjust register name for "unnamed" in 'clipboard'. */
3357 adjust_clip_reg(&regname);
3358 (void)may_get_selection(regname);
3359#endif
3360
3361 if (flags & PUT_FIXINDENT)
3362 orig_indent = get_indent();
3363
3364 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3365 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3366
3367 /*
3368 * Using inserted text works differently, because the register includes
3369 * special characters (newlines, etc.).
3370 */
3371 if (regname == '.')
3372 {
3373 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3374 (count == -1 ? 'O' : 'i')), count, FALSE);
3375 /* Putting the text is done later, so can't really move the cursor to
3376 * the next character. Use "l" to simulate it. */
3377 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3378 stuffcharReadbuff('l');
3379 return;
3380 }
3381
3382 /*
3383 * For special registers '%' (file name), '#' (alternate file name) and
3384 * ':' (last command line), etc. we have to create a fake yank register.
3385 */
3386 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3387 {
3388 if (insert_string == NULL)
3389 return;
3390 }
3391
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003392#ifdef FEAT_AUTOCMD
3393 /* Autocommands may be executed when saving lines for undo, which may make
3394 * y_array invalid. Start undo now to avoid that. */
3395 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3396#endif
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (insert_string != NULL)
3399 {
3400 y_type = MCHAR;
3401#ifdef FEAT_EVAL
3402 if (regname == '=')
3403 {
3404 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003405 * characters.
3406 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 for (;;)
3408 {
3409 y_size = 0;
3410 ptr = insert_string;
3411 while (ptr != NULL)
3412 {
3413 if (y_array != NULL)
3414 y_array[y_size] = ptr;
3415 ++y_size;
3416 ptr = vim_strchr(ptr, '\n');
3417 if (ptr != NULL)
3418 {
3419 if (y_array != NULL)
3420 *ptr = NUL;
3421 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003422 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (*ptr == NUL)
3424 {
3425 y_type = MLINE;
3426 break;
3427 }
3428 }
3429 }
3430 if (y_array != NULL)
3431 break;
3432 y_array = (char_u **)alloc((unsigned)
3433 (y_size * sizeof(char_u *)));
3434 if (y_array == NULL)
3435 goto end;
3436 }
3437 }
3438 else
3439#endif
3440 {
3441 y_size = 1; /* use fake one-line yank register */
3442 y_array = &insert_string;
3443 }
3444 }
3445 else
3446 {
3447 get_yank_register(regname, FALSE);
3448
3449 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 y_size = y_current->y_size;
3452 y_array = y_current->y_array;
3453 }
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (y_type == MLINE)
3456 {
3457 if (flags & PUT_LINE_SPLIT)
3458 {
3459 /* "p" or "P" in Visual mode: split the lines to put the text in
3460 * between. */
3461 if (u_save_cursor() == FAIL)
3462 goto end;
3463 ptr = vim_strsave(ml_get_cursor());
3464 if (ptr == NULL)
3465 goto end;
3466 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3467 vim_free(ptr);
3468
3469 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3470 if (ptr == NULL)
3471 goto end;
3472 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3473 ++nr_lines;
3474 dir = FORWARD;
3475 }
3476 if (flags & PUT_LINE_FORWARD)
3477 {
3478 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003479 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 dir = FORWARD;
3481 }
3482 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3483 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3487 y_type = MLINE;
3488
3489 if (y_size == 0 || y_array == NULL)
3490 {
3491 EMSG2(_("E353: Nothing in register %s"),
3492 regname == 0 ? (char_u *)"\"" : transchar(regname));
3493 goto end;
3494 }
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (y_type == MBLOCK)
3497 {
3498 lnum = curwin->w_cursor.lnum + y_size + 1;
3499 if (lnum > curbuf->b_ml.ml_line_count)
3500 lnum = curbuf->b_ml.ml_line_count + 1;
3501 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3502 goto end;
3503 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003504 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 lnum = curwin->w_cursor.lnum;
3507#ifdef FEAT_FOLDING
3508 /* Correct line number for closed fold. Don't move the cursor yet,
3509 * u_save() uses it. */
3510 if (dir == BACKWARD)
3511 (void)hasFolding(lnum, &lnum, NULL);
3512 else
3513 (void)hasFolding(lnum, NULL, &lnum);
3514#endif
3515 if (dir == FORWARD)
3516 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003517 /* In an empty buffer the empty line is going to be replaced, include
3518 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003519 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 goto end;
3521#ifdef FEAT_FOLDING
3522 if (dir == FORWARD)
3523 curwin->w_cursor.lnum = lnum - 1;
3524 else
3525 curwin->w_cursor.lnum = lnum;
3526 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3527#endif
3528 }
3529 else if (u_save_cursor() == FAIL)
3530 goto end;
3531
3532 yanklen = (int)STRLEN(y_array[0]);
3533
3534#ifdef FEAT_VIRTUALEDIT
3535 if (ve_flags == VE_ALL && y_type == MCHAR)
3536 {
3537 if (gchar_cursor() == TAB)
3538 {
3539 /* Don't need to insert spaces when "p" on the last position of a
3540 * tab or "P" on the first position. */
3541 if (dir == FORWARD
3542 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3543 : curwin->w_cursor.coladd > 0)
3544 coladvance_force(getviscol());
3545 else
3546 curwin->w_cursor.coladd = 0;
3547 }
3548 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3549 coladvance_force(getviscol() + (dir == FORWARD));
3550 }
3551#endif
3552
3553 lnum = curwin->w_cursor.lnum;
3554 col = curwin->w_cursor.col;
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /*
3557 * Block mode
3558 */
3559 if (y_type == MBLOCK)
3560 {
3561 char c = gchar_cursor();
3562 colnr_T endcol2 = 0;
3563
3564 if (dir == FORWARD && c != NUL)
3565 {
3566#ifdef FEAT_VIRTUALEDIT
3567 if (ve_flags == VE_ALL)
3568 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3569 else
3570#endif
3571 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3572
3573#ifdef FEAT_MBYTE
3574 if (has_mbyte)
3575 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003576 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 else
3578#endif
3579#ifdef FEAT_VIRTUALEDIT
3580 if (c != TAB || ve_flags != VE_ALL)
3581#endif
3582 ++curwin->w_cursor.col;
3583 ++col;
3584 }
3585 else
3586 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3587
3588#ifdef FEAT_VIRTUALEDIT
3589 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003590 if (ve_flags == VE_ALL
3591 && (curwin->w_cursor.coladd > 0
3592 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 if (dir == FORWARD && c == NUL)
3595 ++col;
3596 if (dir != FORWARD && c != NUL)
3597 ++curwin->w_cursor.col;
3598 if (c == TAB)
3599 {
3600 if (dir == BACKWARD && curwin->w_cursor.col)
3601 curwin->w_cursor.col--;
3602 if (dir == FORWARD && col - 1 == endcol2)
3603 curwin->w_cursor.col++;
3604 }
3605 }
3606 curwin->w_cursor.coladd = 0;
3607#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003608 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 for (i = 0; i < y_size; ++i)
3610 {
3611 int spaces;
3612 char shortline;
3613
3614 bd.startspaces = 0;
3615 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 vcol = 0;
3617 delcount = 0;
3618
3619 /* add a new line */
3620 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3621 {
3622 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3623 (colnr_T)1, FALSE) == FAIL)
3624 break;
3625 ++nr_lines;
3626 }
3627 /* get the old line and advance to the position to insert at */
3628 oldp = ml_get_curline();
3629 oldlen = (int)STRLEN(oldp);
3630 for (ptr = oldp; vcol < col && *ptr; )
3631 {
3632 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003633 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 vcol += incr;
3635 }
3636 bd.textcol = (colnr_T)(ptr - oldp);
3637
3638 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3639
3640 if (vcol < col) /* line too short, padd with spaces */
3641 bd.startspaces = col - vcol;
3642 else if (vcol > col)
3643 {
3644 bd.endspaces = vcol - col;
3645 bd.startspaces = incr - bd.endspaces;
3646 --bd.textcol;
3647 delcount = 1;
3648#ifdef FEAT_MBYTE
3649 if (has_mbyte)
3650 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3651#endif
3652 if (oldp[bd.textcol] != TAB)
3653 {
3654 /* Only a Tab can be split into spaces. Other
3655 * characters will have to be moved to after the
3656 * block, causing misalignment. */
3657 delcount = 0;
3658 bd.endspaces = 0;
3659 }
3660 }
3661
3662 yanklen = (int)STRLEN(y_array[i]);
3663
3664 /* calculate number of spaces required to fill right side of block*/
3665 spaces = y_width + 1;
3666 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003667 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (spaces < 0)
3669 spaces = 0;
3670
3671 /* insert the new text */
3672 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3673 newp = alloc_check((unsigned)totlen + oldlen + 1);
3674 if (newp == NULL)
3675 break;
3676 /* copy part up to cursor to new line */
3677 ptr = newp;
3678 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3679 ptr += bd.textcol;
3680 /* may insert some spaces before the new text */
3681 copy_spaces(ptr, (size_t)bd.startspaces);
3682 ptr += bd.startspaces;
3683 /* insert the new text */
3684 for (j = 0; j < count; ++j)
3685 {
3686 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3687 ptr += yanklen;
3688
3689 /* insert block's trailing spaces only if there's text behind */
3690 if ((j < count - 1 || !shortline) && spaces)
3691 {
3692 copy_spaces(ptr, (size_t)spaces);
3693 ptr += spaces;
3694 }
3695 }
3696 /* may insert some spaces after the new text */
3697 copy_spaces(ptr, (size_t)bd.endspaces);
3698 ptr += bd.endspaces;
3699 /* move the text after the cursor to the end of the line. */
3700 mch_memmove(ptr, oldp + bd.textcol + delcount,
3701 (size_t)(oldlen - bd.textcol - delcount + 1));
3702 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3703
3704 ++curwin->w_cursor.lnum;
3705 if (i == 0)
3706 curwin->w_cursor.col += bd.startspaces;
3707 }
3708
3709 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3710
3711 /* Set '[ mark. */
3712 curbuf->b_op_start = curwin->w_cursor;
3713 curbuf->b_op_start.lnum = lnum;
3714
3715 /* adjust '] mark */
3716 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3717 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003718# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (flags & PUT_CURSEND)
3722 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003723 colnr_T len;
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 curwin->w_cursor = curbuf->b_op_end;
3726 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003727
3728 /* in Insert mode we might be after the NUL, correct for that */
3729 len = (colnr_T)STRLEN(ml_get_curline());
3730 if (curwin->w_cursor.col > len)
3731 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733 else
3734 curwin->w_cursor.lnum = lnum;
3735 }
3736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
3738 /*
3739 * Character or Line mode
3740 */
3741 if (y_type == MCHAR)
3742 {
3743 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3744 * char */
3745 if (dir == FORWARD && gchar_cursor() != NUL)
3746 {
3747#ifdef FEAT_MBYTE
3748 if (has_mbyte)
3749 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003750 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 /* put it on the next of the multi-byte character. */
3753 col += bytelen;
3754 if (yanklen)
3755 {
3756 curwin->w_cursor.col += bytelen;
3757 curbuf->b_op_end.col += bytelen;
3758 }
3759 }
3760 else
3761#endif
3762 {
3763 ++col;
3764 if (yanklen)
3765 {
3766 ++curwin->w_cursor.col;
3767 ++curbuf->b_op_end.col;
3768 }
3769 }
3770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 curbuf->b_op_start = curwin->w_cursor;
3772 }
3773 /*
3774 * Line mode: BACKWARD is the same as FORWARD on the previous line
3775 */
3776 else if (dir == BACKWARD)
3777 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003778 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 /*
3781 * simple case: insert into current line
3782 */
3783 if (y_type == MCHAR && y_size == 1)
3784 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003785 do {
3786 totlen = count * yanklen;
3787 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003789 oldp = ml_get(lnum);
3790 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3791 if (newp == NULL)
3792 goto end; /* alloc() gave an error message */
3793 mch_memmove(newp, oldp, (size_t)col);
3794 ptr = newp + col;
3795 for (i = 0; i < count; ++i)
3796 {
3797 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3798 ptr += yanklen;
3799 }
3800 STRMOVE(ptr, oldp + col);
3801 ml_replace(lnum, newp, FALSE);
3802 /* Place cursor on last putted char. */
3803 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003804 {
3805 /* make sure curwin->w_virtcol is updated */
3806 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003807 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003810 if (VIsual_active)
3811 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003812 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 curbuf->b_op_end = curwin->w_cursor;
3815 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3816 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3817 ++curwin->w_cursor.col;
3818 changed_bytes(lnum, col);
3819 }
3820 else
3821 {
3822 /*
3823 * Insert at least one line. When y_type is MCHAR, break the first
3824 * line in two.
3825 */
3826 for (cnt = 1; cnt <= count; ++cnt)
3827 {
3828 i = 0;
3829 if (y_type == MCHAR)
3830 {
3831 /*
3832 * Split the current line in two at the insert position.
3833 * First insert y_array[size - 1] in front of second line.
3834 * Then append y_array[0] to first line.
3835 */
3836 lnum = new_cursor.lnum;
3837 ptr = ml_get(lnum) + col;
3838 totlen = (int)STRLEN(y_array[y_size - 1]);
3839 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3840 if (newp == NULL)
3841 goto error;
3842 STRCPY(newp, y_array[y_size - 1]);
3843 STRCAT(newp, ptr);
3844 /* insert second line */
3845 ml_append(lnum, newp, (colnr_T)0, FALSE);
3846 vim_free(newp);
3847
3848 oldp = ml_get(lnum);
3849 newp = alloc_check((unsigned)(col + yanklen + 1));
3850 if (newp == NULL)
3851 goto error;
3852 /* copy first part of line */
3853 mch_memmove(newp, oldp, (size_t)col);
3854 /* append to first line */
3855 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3856 ml_replace(lnum, newp, FALSE);
3857
3858 curwin->w_cursor.lnum = lnum;
3859 i = 1;
3860 }
3861
3862 for (; i < y_size; ++i)
3863 {
3864 if ((y_type != MCHAR || i < y_size - 1)
3865 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3866 == FAIL)
3867 goto error;
3868 lnum++;
3869 ++nr_lines;
3870 if (flags & PUT_FIXINDENT)
3871 {
3872 old_pos = curwin->w_cursor;
3873 curwin->w_cursor.lnum = lnum;
3874 ptr = ml_get(lnum);
3875 if (cnt == count && i == y_size - 1)
3876 lendiff = (int)STRLEN(ptr);
3877#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3878 if (*ptr == '#' && preprocs_left())
3879 indent = 0; /* Leave # lines at start */
3880 else
3881#endif
3882 if (*ptr == NUL)
3883 indent = 0; /* Ignore empty lines */
3884 else if (first_indent)
3885 {
3886 indent_diff = orig_indent - get_indent();
3887 indent = orig_indent;
3888 first_indent = FALSE;
3889 }
3890 else if ((indent = get_indent() + indent_diff) < 0)
3891 indent = 0;
3892 (void)set_indent(indent, 0);
3893 curwin->w_cursor = old_pos;
3894 /* remember how many chars were removed */
3895 if (cnt == count && i == y_size - 1)
3896 lendiff -= (int)STRLEN(ml_get(lnum));
3897 }
3898 }
3899 }
3900
3901error:
3902 /* Adjust marks. */
3903 if (y_type == MLINE)
3904 {
3905 curbuf->b_op_start.col = 0;
3906 if (dir == FORWARD)
3907 curbuf->b_op_start.lnum++;
3908 }
3909 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3910 (linenr_T)MAXLNUM, nr_lines, 0L);
3911
3912 /* note changed text for displaying and folding */
3913 if (y_type == MCHAR)
3914 changed_lines(curwin->w_cursor.lnum, col,
3915 curwin->w_cursor.lnum + 1, nr_lines);
3916 else
3917 changed_lines(curbuf->b_op_start.lnum, 0,
3918 curbuf->b_op_start.lnum, nr_lines);
3919
3920 /* put '] mark at last inserted character */
3921 curbuf->b_op_end.lnum = lnum;
3922 /* correct length for change in indent */
3923 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3924 if (col > 1)
3925 curbuf->b_op_end.col = col - 1;
3926 else
3927 curbuf->b_op_end.col = 0;
3928
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003929 if (flags & PUT_CURSLINE)
3930 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003931 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003932 curwin->w_cursor.lnum = lnum;
3933 beginline(BL_WHITE | BL_FIX);
3934 }
3935 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
3937 /* put cursor after inserted text */
3938 if (y_type == MLINE)
3939 {
3940 if (lnum >= curbuf->b_ml.ml_line_count)
3941 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3942 else
3943 curwin->w_cursor.lnum = lnum + 1;
3944 curwin->w_cursor.col = 0;
3945 }
3946 else
3947 {
3948 curwin->w_cursor.lnum = lnum;
3949 curwin->w_cursor.col = col;
3950 }
3951 }
3952 else if (y_type == MLINE)
3953 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003954 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 curwin->w_cursor.col = 0;
3956 if (dir == FORWARD)
3957 ++curwin->w_cursor.lnum;
3958 beginline(BL_WHITE | BL_FIX);
3959 }
3960 else /* put cursor on first inserted character */
3961 curwin->w_cursor = new_cursor;
3962 }
3963 }
3964
3965 msgmore(nr_lines);
3966 curwin->w_set_curswant = TRUE;
3967
3968end:
3969 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003971 if (regname == '=')
3972 vim_free(y_array);
3973
Bram Moolenaar033d8882013-09-25 23:24:57 +02003974 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003975
Bram Moolenaar677ee682005-01-27 14:41:15 +00003976 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003977 adjust_cursor_eol();
3978}
3979
3980/*
3981 * When the cursor is on the NUL past the end of the line and it should not be
3982 * there move it left.
3983 */
3984 void
3985adjust_cursor_eol()
3986{
3987 if (curwin->w_cursor.col > 0
3988 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003989#ifdef FEAT_VIRTUALEDIT
3990 && (ve_flags & VE_ONEMORE) == 0
3991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 && !(restart_edit || (State & INSERT)))
3993 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003994 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003995 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#ifdef FEAT_VIRTUALEDIT
3998 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003999 {
4000 colnr_T scol, ecol;
4001
4002 /* Coladd is set to the width of the last character. */
4003 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4004 curwin->w_cursor.coladd = ecol - scol + 1;
4005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#endif
4007 }
4008}
4009
4010#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4011/*
4012 * Return TRUE if lines starting with '#' should be left aligned.
4013 */
4014 int
4015preprocs_left()
4016{
4017 return
4018# ifdef FEAT_SMARTINDENT
4019# ifdef FEAT_CINDENT
4020 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4021# else
4022 curbuf->b_p_si
4023# endif
4024# endif
4025# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004026 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4027 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028# endif
4029 ;
4030}
4031#endif
4032
4033/* Return the character name of the register with the given number */
4034 int
4035get_register_name(num)
4036 int num;
4037{
4038 if (num == -1)
4039 return '"';
4040 else if (num < 10)
4041 return num + '0';
4042 else if (num == DELETION_REGISTER)
4043 return '-';
4044#ifdef FEAT_CLIPBOARD
4045 else if (num == STAR_REGISTER)
4046 return '*';
4047 else if (num == PLUS_REGISTER)
4048 return '+';
4049#endif
4050 else
4051 {
4052#ifdef EBCDIC
4053 int i;
4054
4055 /* EBCDIC is really braindead ... */
4056 i = 'a' + (num - 10);
4057 if (i > 'i')
4058 i += 7;
4059 if (i > 'r')
4060 i += 8;
4061 return i;
4062#else
4063 return num + 'a' - 10;
4064#endif
4065 }
4066}
4067
4068/*
4069 * ":dis" and ":registers": Display the contents of the yank registers.
4070 */
4071 void
4072ex_display(eap)
4073 exarg_T *eap;
4074{
4075 int i, n;
4076 long j;
4077 char_u *p;
4078 struct yankreg *yb;
4079 int name;
4080 int attr;
4081 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004082#ifdef FEAT_MBYTE
4083 int clen;
4084#else
4085# define clen 1
4086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 if (arg != NULL && *arg == NUL)
4089 arg = NULL;
4090 attr = hl_attr(HLF_8);
4091
4092 /* Highlight title */
4093 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4094 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4095 {
4096 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004097 if (arg != NULL && vim_strchr(arg, name) == NULL
4098#ifdef ONE_CLIPBOARD
4099 /* Star register and plus register contain the same thing. */
4100 && (name != '*' || vim_strchr(arg, '+') == NULL)
4101#endif
4102 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 continue; /* did not ask for this register */
4104
4105#ifdef FEAT_CLIPBOARD
4106 /* Adjust register name for "unnamed" in 'clipboard'.
4107 * When it's a clipboard register, fill it with the current contents
4108 * of the clipboard. */
4109 adjust_clip_reg(&name);
4110 (void)may_get_selection(name);
4111#endif
4112
4113 if (i == -1)
4114 {
4115 if (y_previous != NULL)
4116 yb = y_previous;
4117 else
4118 yb = &(y_regs[0]);
4119 }
4120 else
4121 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004122
4123#ifdef FEAT_EVAL
4124 if (name == MB_TOLOWER(redir_reg)
4125 || (redir_reg == '"' && yb == y_previous))
4126 continue; /* do not list register being written to, the
4127 * pointer can be freed */
4128#endif
4129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (yb->y_array != NULL)
4131 {
4132 msg_putchar('\n');
4133 msg_putchar('"');
4134 msg_putchar(name);
4135 MSG_PUTS(" ");
4136
4137 n = (int)Columns - 6;
4138 for (j = 0; j < yb->y_size && n > 1; ++j)
4139 {
4140 if (j)
4141 {
4142 MSG_PUTS_ATTR("^J", attr);
4143 n -= 2;
4144 }
4145 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004148 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004149#endif
4150 msg_outtrans_len(p, clen);
4151#ifdef FEAT_MBYTE
4152 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#endif
4154 }
4155 }
4156 if (n > 1 && yb->y_type == MLINE)
4157 MSG_PUTS_ATTR("^J", attr);
4158 out_flush(); /* show one line at a time */
4159 }
4160 ui_breakcheck();
4161 }
4162
4163 /*
4164 * display last inserted text
4165 */
4166 if ((p = get_last_insert()) != NULL
4167 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4168 {
4169 MSG_PUTS("\n\". ");
4170 dis_msg(p, TRUE);
4171 }
4172
4173 /*
4174 * display last command line
4175 */
4176 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4177 && !got_int)
4178 {
4179 MSG_PUTS("\n\": ");
4180 dis_msg(last_cmdline, FALSE);
4181 }
4182
4183 /*
4184 * display current file name
4185 */
4186 if (curbuf->b_fname != NULL
4187 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4188 {
4189 MSG_PUTS("\n\"% ");
4190 dis_msg(curbuf->b_fname, FALSE);
4191 }
4192
4193 /*
4194 * display alternate file name
4195 */
4196 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4197 {
4198 char_u *fname;
4199 linenr_T dummy;
4200
4201 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4202 {
4203 MSG_PUTS("\n\"# ");
4204 dis_msg(fname, FALSE);
4205 }
4206 }
4207
4208 /*
4209 * display last search pattern
4210 */
4211 if (last_search_pat() != NULL
4212 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4213 {
4214 MSG_PUTS("\n\"/ ");
4215 dis_msg(last_search_pat(), FALSE);
4216 }
4217
4218#ifdef FEAT_EVAL
4219 /*
4220 * display last used expression
4221 */
4222 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4223 && !got_int)
4224 {
4225 MSG_PUTS("\n\"= ");
4226 dis_msg(expr_line, FALSE);
4227 }
4228#endif
4229}
4230
4231/*
4232 * display a string for do_dis()
4233 * truncate at end of screen line
4234 */
4235 static void
4236dis_msg(p, skip_esc)
4237 char_u *p;
4238 int skip_esc; /* if TRUE, ignore trailing ESC */
4239{
4240 int n;
4241#ifdef FEAT_MBYTE
4242 int l;
4243#endif
4244
4245 n = (int)Columns - 6;
4246 while (*p != NUL
4247 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4248 && (n -= ptr2cells(p)) >= 0)
4249 {
4250#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004251 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 msg_outtrans_len(p, l);
4254 p += l;
4255 }
4256 else
4257#endif
4258 msg_outtrans_len(p++, 1);
4259 }
4260 ui_breakcheck();
4261}
4262
Bram Moolenaar81340392012-06-06 16:12:59 +02004263#if defined(FEAT_COMMENTS) || defined(PROTO)
4264/*
4265 * If "process" is TRUE and the line begins with a comment leader (possibly
4266 * after some white space), return a pointer to the text after it. Put a boolean
4267 * value indicating whether the line ends with an unclosed comment in
4268 * "is_comment".
4269 * line - line to be processed,
4270 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004271 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004272 * include_space - whether to also skip space following the comment leader,
4273 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004274 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004275 */
4276 static char_u *
4277skip_comment(line, process, include_space, is_comment)
4278 char_u *line;
4279 int process;
4280 int include_space;
4281 int *is_comment;
4282{
4283 char_u *comment_flags = NULL;
4284 int lead_len;
4285 int leader_offset = get_last_leader_offset(line, &comment_flags);
4286
4287 *is_comment = FALSE;
4288 if (leader_offset != -1)
4289 {
4290 /* Let's check whether the line ends with an unclosed comment.
4291 * If the last comment leader has COM_END in flags, there's no comment.
4292 */
4293 while (*comment_flags)
4294 {
4295 if (*comment_flags == COM_END
4296 || *comment_flags == ':')
4297 break;
4298 ++comment_flags;
4299 }
4300 if (*comment_flags != COM_END)
4301 *is_comment = TRUE;
4302 }
4303
4304 if (process == FALSE)
4305 return line;
4306
4307 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4308
4309 if (lead_len == 0)
4310 return line;
4311
4312 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004313 * - COM_END,
4314 * - colon,
4315 * whichever comes first.
4316 */
4317 while (*comment_flags)
4318 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004319 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004320 || *comment_flags == ':')
4321 {
4322 break;
4323 }
4324 ++comment_flags;
4325 }
4326
4327 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004328 * starting with a closing part of a three-part comment. That's good,
4329 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004330 */
4331 if (*comment_flags == ':' || *comment_flags == NUL)
4332 line += lead_len;
4333
4334 return line;
4335}
4336#endif
4337
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004339 * Join 'count' lines (minimal 2) at cursor position.
4340 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004341 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4342 * leaders should not be removed.
4343 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4344 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004346 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 */
4348 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004349do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004350 long count;
4351 int insert_space;
4352 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004353 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004354 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004356 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004357 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004358 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004360 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004361 int endcurr1 = NUL;
4362 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004363 int currsize = 0; /* size of the current line */
4364 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004366 colnr_T col = 0;
4367 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004368#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004369 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004370 int remove_comments = (use_formatoptions == TRUE)
4371 && has_format_option(FO_REMOVE_COMS);
4372 int prev_was_comment;
4373#endif
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004376 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4377 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004380 /* Allocate an array to store the number of spaces inserted before each
4381 * line. We will use it to pre-compute the length of the new line and the
4382 * proper placement of each original line in the new one. */
4383 spaces = lalloc_clear((long_u)count, TRUE);
4384 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004386#if defined(FEAT_COMMENTS) || defined(PROTO)
4387 if (remove_comments)
4388 {
4389 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4390 if (comments == NULL)
4391 {
4392 vim_free(spaces);
4393 return FAIL;
4394 }
4395 }
4396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004399 * Don't move anything, just compute the final line length
4400 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004402 for (t = 0; t < count; ++t)
4403 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004404 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004405 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004406 {
4407 /* Set the '[ mark. */
4408 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4409 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4410 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004411#if defined(FEAT_COMMENTS) || defined(PROTO)
4412 if (remove_comments)
4413 {
4414 /* We don't want to remove the comment leader if the
4415 * previous line is not a comment. */
4416 if (t > 0 && prev_was_comment)
4417 {
4418
4419 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4420 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004421 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004422 curr = new_curr;
4423 }
4424 else
4425 curr = skip_comment(curr, FALSE, insert_space,
4426 &prev_was_comment);
4427 }
4428#endif
4429
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004430 if (insert_space && t > 0)
4431 {
4432 curr = skipwhite(curr);
4433 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4434#ifdef FEAT_MBYTE
4435 && (!has_format_option(FO_MBYTE_JOIN)
4436 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4437 && (!has_format_option(FO_MBYTE_JOIN2)
4438 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4439#endif
4440 )
4441 {
4442 /* don't add a space if the line is ending in a space */
4443 if (endcurr1 == ' ')
4444 endcurr1 = endcurr2;
4445 else
4446 ++spaces[t];
4447 /* extra space when 'joinspaces' set and line ends in '.' */
4448 if ( p_js
4449 && (endcurr1 == '.'
4450 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4451 && (endcurr1 == '?' || endcurr1 == '!'))))
4452 ++spaces[t];
4453 }
4454 }
4455 currsize = (int)STRLEN(curr);
4456 sumsize += currsize + spaces[t];
4457 endcurr1 = endcurr2 = NUL;
4458 if (insert_space && currsize > 0)
4459 {
4460#ifdef FEAT_MBYTE
4461 if (has_mbyte)
4462 {
4463 cend = curr + currsize;
4464 mb_ptr_back(curr, cend);
4465 endcurr1 = (*mb_ptr2char)(cend);
4466 if (cend > curr)
4467 {
4468 mb_ptr_back(curr, cend);
4469 endcurr2 = (*mb_ptr2char)(cend);
4470 }
4471 }
4472 else
4473#endif
4474 {
4475 endcurr1 = *(curr + currsize - 1);
4476 if (currsize > 1)
4477 endcurr2 = *(curr + currsize - 2);
4478 }
4479 }
4480 line_breakcheck();
4481 if (got_int)
4482 {
4483 ret = FAIL;
4484 goto theend;
4485 }
4486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004488 /* store the column position before last line */
4489 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004491 /* allocate the space for the new line */
4492 newp = alloc_check((unsigned)(sumsize + 1));
4493 cend = newp + sumsize;
4494 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004496 /*
4497 * Move affected lines to the new long one.
4498 *
4499 * Move marks from each deleted line to the joined line, adjusting the
4500 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4501 * should not really be a problem.
4502 */
4503 for (t = count - 1; ; --t)
4504 {
4505 cend -= currsize;
4506 mch_memmove(cend, curr, (size_t)currsize);
4507 if (spaces[t] > 0)
4508 {
4509 cend -= spaces[t];
4510 copy_spaces(cend, (size_t)(spaces[t]));
4511 }
4512 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004513 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004514 if (t == 0)
4515 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004516 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004517#if defined(FEAT_COMMENTS) || defined(PROTO)
4518 if (remove_comments)
4519 curr += comments[t - 1];
4520#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004521 if (insert_space && t > 1)
4522 curr = skipwhite(curr);
4523 currsize = (int)STRLEN(curr);
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4526
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004527 if (setmark)
4528 {
4529 /* Set the '] mark. */
4530 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4531 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4532 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 /* Only report the change in the first line here, del_lines() will report
4535 * the deleted line. */
4536 changed_lines(curwin->w_cursor.lnum, currsize,
4537 curwin->w_cursor.lnum + 1, 0L);
4538
4539 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004540 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 * briefly, and then move it back. After del_lines() the cursor may
4542 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 */
4544 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004546 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 curwin->w_cursor.lnum = t;
4548
4549 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004550 * Set the cursor column:
4551 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004552 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004554 curwin->w_cursor.col =
4555 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#ifdef FEAT_VIRTUALEDIT
4559 curwin->w_cursor.coladd = 0;
4560#endif
4561 curwin->w_set_curswant = TRUE;
4562
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004563theend:
4564 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004565#if defined(FEAT_COMMENTS) || defined(PROTO)
4566 if (remove_comments)
4567 vim_free(comments);
4568#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004569 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570}
4571
4572#ifdef FEAT_COMMENTS
4573/*
4574 * Return TRUE if the two comment leaders given are the same. "lnum" is
4575 * the first line. White-space is ignored. Note that the whole of
4576 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4577 */
4578 static int
4579same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4580 linenr_T lnum;
4581 int leader1_len;
4582 char_u *leader1_flags;
4583 int leader2_len;
4584 char_u *leader2_flags;
4585{
4586 int idx1 = 0, idx2 = 0;
4587 char_u *p;
4588 char_u *line1;
4589 char_u *line2;
4590
4591 if (leader1_len == 0)
4592 return (leader2_len == 0);
4593
4594 /*
4595 * If first leader has 'f' flag, the lines can be joined only if the
4596 * second line does not have a leader.
4597 * If first leader has 'e' flag, the lines can never be joined.
4598 * If fist leader has 's' flag, the lines can only be joined if there is
4599 * some text after it and the second line has the 'm' flag.
4600 */
4601 if (leader1_flags != NULL)
4602 {
4603 for (p = leader1_flags; *p && *p != ':'; ++p)
4604 {
4605 if (*p == COM_FIRST)
4606 return (leader2_len == 0);
4607 if (*p == COM_END)
4608 return FALSE;
4609 if (*p == COM_START)
4610 {
4611 if (*(ml_get(lnum) + leader1_len) == NUL)
4612 return FALSE;
4613 if (leader2_flags == NULL || leader2_len == 0)
4614 return FALSE;
4615 for (p = leader2_flags; *p && *p != ':'; ++p)
4616 if (*p == COM_MIDDLE)
4617 return TRUE;
4618 return FALSE;
4619 }
4620 }
4621 }
4622
4623 /*
4624 * Get current line and next line, compare the leaders.
4625 * The first line has to be saved, only one line can be locked at a time.
4626 */
4627 line1 = vim_strsave(ml_get(lnum));
4628 if (line1 != NULL)
4629 {
4630 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4631 ;
4632 line2 = ml_get(lnum + 1);
4633 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4634 {
4635 if (!vim_iswhite(line2[idx2]))
4636 {
4637 if (line1[idx1++] != line2[idx2])
4638 break;
4639 }
4640 else
4641 while (vim_iswhite(line1[idx1]))
4642 ++idx1;
4643 }
4644 vim_free(line1);
4645 }
4646 return (idx2 == leader2_len && idx1 == leader1_len);
4647}
4648#endif
4649
4650/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004651 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 */
4653 void
4654op_format(oap, keep_cursor)
4655 oparg_T *oap;
4656 int keep_cursor; /* keep cursor on same text char */
4657{
4658 long old_line_count = curbuf->b_ml.ml_line_count;
4659
4660 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4661 * can put it back there. */
4662 curwin->w_cursor = oap->cursor_start;
4663
4664 if (u_save((linenr_T)(oap->start.lnum - 1),
4665 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4666 return;
4667 curwin->w_cursor = oap->start;
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (oap->is_VIsual)
4670 /* When there is no change: need to remove the Visual selection */
4671 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 /* Set '[ mark at the start of the formatted area */
4674 curbuf->b_op_start = oap->start;
4675
4676 /* For "gw" remember the cursor position and put it back below (adjusted
4677 * for joined and split lines). */
4678 if (keep_cursor)
4679 saved_cursor = oap->cursor_start;
4680
Bram Moolenaar81a82092008-03-12 16:27:00 +00004681 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 /*
4684 * Leave the cursor at the first non-blank of the last formatted line.
4685 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4686 * line, so "." will do the next lines.
4687 */
4688 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4689 ++curwin->w_cursor.lnum;
4690 beginline(BL_WHITE | BL_FIX);
4691 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4692 msgmore(old_line_count);
4693
4694 /* put '] mark on the end of the formatted area */
4695 curbuf->b_op_end = curwin->w_cursor;
4696
4697 if (keep_cursor)
4698 {
4699 curwin->w_cursor = saved_cursor;
4700 saved_cursor.lnum = 0;
4701 }
4702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (oap->is_VIsual)
4704 {
4705 win_T *wp;
4706
4707 FOR_ALL_WINDOWS(wp)
4708 {
4709 if (wp->w_old_cursor_lnum != 0)
4710 {
4711 /* When lines have been inserted or deleted, adjust the end of
4712 * the Visual area to be redrawn. */
4713 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4714 wp->w_old_cursor_lnum += old_line_count;
4715 else
4716 wp->w_old_visual_lnum += old_line_count;
4717 }
4718 }
4719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004722#if defined(FEAT_EVAL) || defined(PROTO)
4723/*
4724 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4725 */
4726 void
4727op_formatexpr(oap)
4728 oparg_T *oap;
4729{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004730 if (oap->is_VIsual)
4731 /* When there is no change: need to remove the Visual selection */
4732 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004733
Bram Moolenaar700303e2010-07-11 17:35:50 +02004734 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4735 /* As documented: when 'formatexpr' returns non-zero fall back to
4736 * internal formatting. */
4737 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004738}
4739
4740 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004741fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004742 linenr_T lnum;
4743 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004744 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004745{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004746 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4747 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004748 int r;
4749
4750 /*
4751 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004752 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004753 */
4754 set_vim_var_nr(VV_LNUM, lnum);
4755 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004756 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004757
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004758 /*
4759 * Evaluate the function.
4760 */
4761 if (use_sandbox)
4762 ++sandbox;
4763 r = eval_to_number(curbuf->b_p_fex);
4764 if (use_sandbox)
4765 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004766
4767 set_vim_var_string(VV_CHAR, NULL, -1);
4768
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004769 return r;
4770}
4771#endif
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773/*
4774 * Format "line_count" lines, starting at the cursor position.
4775 * When "line_count" is negative, format until the end of the paragraph.
4776 * Lines after the cursor line are saved for undo, caller must have saved the
4777 * first line.
4778 */
4779 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004780format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004782 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784 int max_len;
4785 int is_not_par; /* current line not part of parag. */
4786 int next_is_not_par; /* next line not part of paragraph */
4787 int is_end_par; /* at end of paragraph */
4788 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4789 int next_is_start_par = FALSE;
4790#ifdef FEAT_COMMENTS
4791 int leader_len = 0; /* leader len of current line */
4792 int next_leader_len; /* leader len of next line */
4793 char_u *leader_flags = NULL; /* flags for leader of current line */
4794 char_u *next_leader_flags; /* flags for leader of next line */
4795 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004796 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797#endif
4798 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004799 int second_indent = -1; /* indent for second line (comment
4800 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int do_second_indent;
4802 int do_number_indent;
4803 int do_trail_white;
4804 int first_par_line = TRUE;
4805 int smd_save;
4806 long count;
4807 int need_set_indent = TRUE; /* set indent of next paragraph */
4808 int force_format = FALSE;
4809 int old_State = State;
4810
4811 /* length of a line to force formatting: 3 * 'tw' */
4812 max_len = comp_textwidth(TRUE) * 3;
4813
4814 /* check for 'q', '2' and '1' in 'formatoptions' */
4815#ifdef FEAT_COMMENTS
4816 do_comments = has_format_option(FO_Q_COMS);
4817#endif
4818 do_second_indent = has_format_option(FO_Q_SECOND);
4819 do_number_indent = has_format_option(FO_Q_NUMBER);
4820 do_trail_white = has_format_option(FO_WHITE_PAR);
4821
4822 /*
4823 * Get info about the previous and current line.
4824 */
4825 if (curwin->w_cursor.lnum > 1)
4826 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4827#ifdef FEAT_COMMENTS
4828 , &leader_len, &leader_flags, do_comments
4829#endif
4830 );
4831 else
4832 is_not_par = TRUE;
4833 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4834#ifdef FEAT_COMMENTS
4835 , &next_leader_len, &next_leader_flags, do_comments
4836#endif
4837 );
4838 is_end_par = (is_not_par || next_is_not_par);
4839 if (!is_end_par && do_trail_white)
4840 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4841
4842 curwin->w_cursor.lnum--;
4843 for (count = line_count; count != 0 && !got_int; --count)
4844 {
4845 /*
4846 * Advance to next paragraph.
4847 */
4848 if (advance)
4849 {
4850 curwin->w_cursor.lnum++;
4851 prev_is_end_par = is_end_par;
4852 is_not_par = next_is_not_par;
4853#ifdef FEAT_COMMENTS
4854 leader_len = next_leader_len;
4855 leader_flags = next_leader_flags;
4856#endif
4857 }
4858
4859 /*
4860 * The last line to be formatted.
4861 */
4862 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4863 {
4864 next_is_not_par = TRUE;
4865#ifdef FEAT_COMMENTS
4866 next_leader_len = 0;
4867 next_leader_flags = NULL;
4868#endif
4869 }
4870 else
4871 {
4872 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4873#ifdef FEAT_COMMENTS
4874 , &next_leader_len, &next_leader_flags, do_comments
4875#endif
4876 );
4877 if (do_number_indent)
4878 next_is_start_par =
4879 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4880 }
4881 advance = TRUE;
4882 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4883 if (!is_end_par && do_trail_white)
4884 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4885
4886 /*
4887 * Skip lines that are not in a paragraph.
4888 */
4889 if (is_not_par)
4890 {
4891 if (line_count < 0)
4892 break;
4893 }
4894 else
4895 {
4896 /*
4897 * For the first line of a paragraph, check indent of second line.
4898 * Don't do this for comments and empty lines.
4899 */
4900 if (first_par_line
4901 && (do_second_indent || do_number_indent)
4902 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004903 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004905 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4906 {
4907#ifdef FEAT_COMMENTS
4908 if (leader_len == 0 && next_leader_len == 0)
4909 {
4910 /* no comment found */
4911#endif
4912 second_indent =
4913 get_indent_lnum(curwin->w_cursor.lnum + 1);
4914#ifdef FEAT_COMMENTS
4915 }
4916 else
4917 {
4918 second_indent = next_leader_len;
4919 do_comments_list = 1;
4920 }
4921#endif
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004924 {
4925#ifdef FEAT_COMMENTS
4926 if (leader_len == 0 && next_leader_len == 0)
4927 {
4928 /* no comment found */
4929#endif
4930 second_indent =
4931 get_number_indent(curwin->w_cursor.lnum);
4932#ifdef FEAT_COMMENTS
4933 }
4934 else
4935 {
4936 /* get_number_indent() is now "comment aware"... */
4937 second_indent =
4938 get_number_indent(curwin->w_cursor.lnum);
4939 do_comments_list = 1;
4940 }
4941#endif
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944
4945 /*
4946 * When the comment leader changes, it's the end of the paragraph.
4947 */
4948 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4949#ifdef FEAT_COMMENTS
4950 || !same_leader(curwin->w_cursor.lnum,
4951 leader_len, leader_flags,
4952 next_leader_len, next_leader_flags)
4953#endif
4954 )
4955 is_end_par = TRUE;
4956
4957 /*
4958 * If we have got to the end of a paragraph, or the line is
4959 * getting long, format it.
4960 */
4961 if (is_end_par || force_format)
4962 {
4963 if (need_set_indent)
4964 /* replace indent in first line with minimal number of
4965 * tabs and spaces, according to current options */
4966 (void)set_indent(get_indent(), SIN_CHANGED);
4967
4968 /* put cursor on last non-space */
4969 State = NORMAL; /* don't go past end-of-line */
4970 coladvance((colnr_T)MAXCOL);
4971 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4972 dec_cursor();
4973
4974 /* do the formatting, without 'showmode' */
4975 State = INSERT; /* for open_line() */
4976 smd_save = p_smd;
4977 p_smd = FALSE;
4978 insertchar(NUL, INSCHAR_FORMAT
4979#ifdef FEAT_COMMENTS
4980 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004981 + (do_comments && do_comments_list
4982 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004984 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 State = old_State;
4986 p_smd = smd_save;
4987 second_indent = -1;
4988 /* at end of par.: need to set indent of next par. */
4989 need_set_indent = is_end_par;
4990 if (is_end_par)
4991 {
4992 /* When called with a negative line count, break at the
4993 * end of the paragraph. */
4994 if (line_count < 0)
4995 break;
4996 first_par_line = TRUE;
4997 }
4998 force_format = FALSE;
4999 }
5000
5001 /*
5002 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005003 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 */
5005 if (!is_end_par)
5006 {
5007 advance = FALSE;
5008 curwin->w_cursor.lnum++;
5009 curwin->w_cursor.col = 0;
5010 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005014 {
5015 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5017 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005018 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005020 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5021 {
5022 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005023 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005024
5025 if (indent > 0)
5026 {
5027 (void)del_bytes(indent, FALSE, FALSE);
5028 mark_col_adjust(curwin->w_cursor.lnum,
5029 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005030 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005033 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 beep_flush();
5036 break;
5037 }
5038 first_par_line = FALSE;
5039 /* If the line is getting long, format it next time */
5040 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5041 force_format = TRUE;
5042 else
5043 force_format = FALSE;
5044 }
5045 }
5046 line_breakcheck();
5047 }
5048}
5049
5050/*
5051 * Return TRUE if line "lnum" ends in a white character.
5052 */
5053 static int
5054ends_in_white(lnum)
5055 linenr_T lnum;
5056{
5057 char_u *s = ml_get(lnum);
5058 size_t l;
5059
5060 if (*s == NUL)
5061 return FALSE;
5062 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5063 * invocation may call function multiple times". */
5064 l = STRLEN(s) - 1;
5065 return vim_iswhite(s[l]);
5066}
5067
5068/*
5069 * Blank lines, and lines containing only the comment leader, are left
5070 * untouched by the formatting. The function returns TRUE in this
5071 * case. It also returns TRUE when a line starts with the end of a comment
5072 * ('e' in comment flags), so that this line is skipped, and not joined to the
5073 * previous line. A new paragraph starts after a blank line, or when the
5074 * comment leader changes -- webb.
5075 */
5076#ifdef FEAT_COMMENTS
5077 static int
5078fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5079 linenr_T lnum;
5080 int *leader_len;
5081 char_u **leader_flags;
5082 int do_comments;
5083{
5084 char_u *flags = NULL; /* init for GCC */
5085 char_u *ptr;
5086
5087 ptr = ml_get(lnum);
5088 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005089 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 else
5091 *leader_len = 0;
5092
5093 if (*leader_len > 0)
5094 {
5095 /*
5096 * Search for 'e' flag in comment leader flags.
5097 */
5098 flags = *leader_flags;
5099 while (*flags && *flags != ':' && *flags != COM_END)
5100 ++flags;
5101 }
5102
5103 return (*skipwhite(ptr + *leader_len) == NUL
5104 || (*leader_len > 0 && *flags == COM_END)
5105 || startPS(lnum, NUL, FALSE));
5106}
5107#else
5108 static int
5109fmt_check_par(lnum)
5110 linenr_T lnum;
5111{
5112 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5113}
5114#endif
5115
5116/*
5117 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5118 * previous line is in the same paragraph. Used for auto-formatting.
5119 */
5120 int
5121paragraph_start(lnum)
5122 linenr_T lnum;
5123{
5124 char_u *p;
5125#ifdef FEAT_COMMENTS
5126 int leader_len = 0; /* leader len of current line */
5127 char_u *leader_flags = NULL; /* flags for leader of current line */
5128 int next_leader_len; /* leader len of next line */
5129 char_u *next_leader_flags; /* flags for leader of next line */
5130 int do_comments; /* format comments */
5131#endif
5132
5133 if (lnum <= 1)
5134 return TRUE; /* start of the file */
5135
5136 p = ml_get(lnum - 1);
5137 if (*p == NUL)
5138 return TRUE; /* after empty line */
5139
5140#ifdef FEAT_COMMENTS
5141 do_comments = has_format_option(FO_Q_COMS);
5142#endif
5143 if (fmt_check_par(lnum - 1
5144#ifdef FEAT_COMMENTS
5145 , &leader_len, &leader_flags, do_comments
5146#endif
5147 ))
5148 return TRUE; /* after non-paragraph line */
5149
5150 if (fmt_check_par(lnum
5151#ifdef FEAT_COMMENTS
5152 , &next_leader_len, &next_leader_flags, do_comments
5153#endif
5154 ))
5155 return TRUE; /* "lnum" is not a paragraph line */
5156
5157 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5158 return TRUE; /* missing trailing space in previous line. */
5159
5160 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5161 return TRUE; /* numbered item starts in "lnum". */
5162
5163#ifdef FEAT_COMMENTS
5164 if (!same_leader(lnum - 1, leader_len, leader_flags,
5165 next_leader_len, next_leader_flags))
5166 return TRUE; /* change of comment leader. */
5167#endif
5168
5169 return FALSE;
5170}
5171
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172/*
5173 * prepare a few things for block mode yank/delete/tilde
5174 *
5175 * for delete:
5176 * - textlen includes the first/last char to be (partly) deleted
5177 * - start/endspaces is the number of columns that are taken by the
5178 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005179 * deleted.
5180 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 * - textlen includes the first/last char to be wholly yanked
5182 * - start/endspaces is the number of columns of the first/last yanked char
5183 * that are to be yanked.
5184 */
5185 static void
5186block_prep(oap, bdp, lnum, is_del)
5187 oparg_T *oap;
5188 struct block_def *bdp;
5189 linenr_T lnum;
5190 int is_del;
5191{
5192 int incr = 0;
5193 char_u *pend;
5194 char_u *pstart;
5195 char_u *line;
5196 char_u *prev_pstart;
5197 char_u *prev_pend;
5198
5199 bdp->startspaces = 0;
5200 bdp->endspaces = 0;
5201 bdp->textlen = 0;
5202 bdp->start_vcol = 0;
5203 bdp->end_vcol = 0;
5204#ifdef FEAT_VISUALEXTRA
5205 bdp->is_short = FALSE;
5206 bdp->is_oneChar = FALSE;
5207 bdp->pre_whitesp = 0;
5208 bdp->pre_whitesp_c = 0;
5209 bdp->end_char_vcols = 0;
5210#endif
5211 bdp->start_char_vcols = 0;
5212
5213 line = ml_get(lnum);
5214 pstart = line;
5215 prev_pstart = line;
5216 while (bdp->start_vcol < oap->start_vcol && *pstart)
5217 {
5218 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005219 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 bdp->start_vcol += incr;
5221#ifdef FEAT_VISUALEXTRA
5222 if (vim_iswhite(*pstart))
5223 {
5224 bdp->pre_whitesp += incr;
5225 bdp->pre_whitesp_c++;
5226 }
5227 else
5228 {
5229 bdp->pre_whitesp = 0;
5230 bdp->pre_whitesp_c = 0;
5231 }
5232#endif
5233 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005234 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236 bdp->start_char_vcols = incr;
5237 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5238 {
5239 bdp->end_vcol = bdp->start_vcol;
5240#ifdef FEAT_VISUALEXTRA
5241 bdp->is_short = TRUE;
5242#endif
5243 if (!is_del || oap->op_type == OP_APPEND)
5244 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5245 }
5246 else
5247 {
5248 /* notice: this converts partly selected Multibyte characters to
5249 * spaces, too. */
5250 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5251 if (is_del && bdp->startspaces)
5252 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5253 pend = pstart;
5254 bdp->end_vcol = bdp->start_vcol;
5255 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5256 {
5257#ifdef FEAT_VISUALEXTRA
5258 bdp->is_oneChar = TRUE;
5259#endif
5260 if (oap->op_type == OP_INSERT)
5261 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5262 else if (oap->op_type == OP_APPEND)
5263 {
5264 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5265 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5266 }
5267 else
5268 {
5269 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5270 if (is_del && oap->op_type != OP_LSHIFT)
5271 {
5272 /* just putting the sum of those two into
5273 * bdp->startspaces doesn't work for Visual replace,
5274 * so we have to split the tab in two */
5275 bdp->startspaces = bdp->start_char_vcols
5276 - (bdp->start_vcol - oap->start_vcol);
5277 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5278 }
5279 }
5280 }
5281 else
5282 {
5283 prev_pend = pend;
5284 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5285 {
5286 /* Count a tab for what it's worth (if list mode not on) */
5287 prev_pend = pend;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005288 /* TODO: is passing prev_pend for start of the line OK?
5289 * perhaps it should be "line". */
5290 incr = lbr_chartabsize_adv(prev_pend, &pend,
5291 (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 bdp->end_vcol += incr;
5293 }
5294 if (bdp->end_vcol <= oap->end_vcol
5295 && (!is_del
5296 || oap->op_type == OP_APPEND
5297 || oap->op_type == OP_REPLACE)) /* line too short */
5298 {
5299#ifdef FEAT_VISUALEXTRA
5300 bdp->is_short = TRUE;
5301#endif
5302 /* Alternative: include spaces to fill up the block.
5303 * Disadvantage: can lead to trailing spaces when the line is
5304 * short where the text is put */
5305 /* if (!is_del || oap->op_type == OP_APPEND) */
5306 if (oap->op_type == OP_APPEND || virtual_op)
5307 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005308 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 else
5310 bdp->endspaces = 0; /* replace doesn't add characters */
5311 }
5312 else if (bdp->end_vcol > oap->end_vcol)
5313 {
5314 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5315 if (!is_del && bdp->endspaces)
5316 {
5317 bdp->endspaces = incr - bdp->endspaces;
5318 if (pend != pstart)
5319 pend = prev_pend;
5320 }
5321 }
5322 }
5323#ifdef FEAT_VISUALEXTRA
5324 bdp->end_char_vcols = incr;
5325#endif
5326 if (is_del && bdp->startspaces)
5327 pstart = prev_pstart;
5328 bdp->textlen = (int)(pend - pstart);
5329 }
5330 bdp->textcol = (colnr_T) (pstart - line);
5331 bdp->textstart = pstart;
5332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
5334#ifdef FEAT_RIGHTLEFT
5335static void reverse_line __ARGS((char_u *s));
5336
5337 static void
5338reverse_line(s)
5339 char_u *s;
5340{
5341 int i, j;
5342 char_u c;
5343
5344 if ((i = (int)STRLEN(s) - 1) <= 0)
5345 return;
5346
5347 curwin->w_cursor.col = i - curwin->w_cursor.col;
5348 for (j = 0; j < i; j++, i--)
5349 {
5350 c = s[i]; s[i] = s[j]; s[j] = c;
5351 }
5352}
5353
5354# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5355#else
5356# define RLADDSUBFIX(ptr)
5357#endif
5358
5359/*
5360 * add or subtract 'Prenum1' from a number in a line
5361 * 'command' is CTRL-A for add, CTRL-X for subtract
5362 *
5363 * return FAIL for failure, OK otherwise
5364 */
5365 int
5366do_addsub(command, Prenum1)
5367 int command;
5368 linenr_T Prenum1;
5369{
5370 int col;
5371 char_u *buf1;
5372 char_u buf2[NUMBUFLEN];
5373 int hex; /* 'X' or 'x': hex; '0': octal */
5374 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005375 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 long_u oldn;
5377 char_u *ptr;
5378 int c;
5379 int length = 0; /* character length of the number */
5380 int todel;
5381 int dohex;
5382 int dooct;
5383 int doalp;
5384 int firstdigit;
5385 int negative;
5386 int subtract;
5387
5388 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5389 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5390 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5391
5392 ptr = ml_get_curline();
5393 RLADDSUBFIX(ptr);
5394
5395 /*
5396 * First check if we are on a hexadecimal number, after the "0x".
5397 */
5398 col = curwin->w_cursor.col;
5399 if (dohex)
5400 while (col > 0 && vim_isxdigit(ptr[col]))
5401 --col;
5402 if ( dohex
5403 && col > 0
5404 && (ptr[col] == 'X'
5405 || ptr[col] == 'x')
5406 && ptr[col - 1] == '0'
5407 && vim_isxdigit(ptr[col + 1]))
5408 {
5409 /*
5410 * Found hexadecimal number, move to its start.
5411 */
5412 --col;
5413 }
5414 else
5415 {
5416 /*
5417 * Search forward and then backward to find the start of number.
5418 */
5419 col = curwin->w_cursor.col;
5420
5421 while (ptr[col] != NUL
5422 && !vim_isdigit(ptr[col])
5423 && !(doalp && ASCII_ISALPHA(ptr[col])))
5424 ++col;
5425
5426 while (col > 0
5427 && vim_isdigit(ptr[col - 1])
5428 && !(doalp && ASCII_ISALPHA(ptr[col])))
5429 --col;
5430 }
5431
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 /*
5433 * If a number was found, and saving for undo works, replace the number.
5434 */
5435 firstdigit = ptr[col];
5436 RLADDSUBFIX(ptr);
5437 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5438 || u_save_cursor() != OK)
5439 {
5440 beep_flush();
5441 return FAIL;
5442 }
5443
5444 /* get ptr again, because u_save() may have changed it */
5445 ptr = ml_get_curline();
5446 RLADDSUBFIX(ptr);
5447
5448 if (doalp && ASCII_ISALPHA(firstdigit))
5449 {
5450 /* decrement or increment alphabetic character */
5451 if (command == Ctrl_X)
5452 {
5453 if (CharOrd(firstdigit) < Prenum1)
5454 {
5455 if (isupper(firstdigit))
5456 firstdigit = 'A';
5457 else
5458 firstdigit = 'a';
5459 }
5460 else
5461#ifdef EBCDIC
5462 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5463#else
5464 firstdigit -= Prenum1;
5465#endif
5466 }
5467 else
5468 {
5469 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5470 {
5471 if (isupper(firstdigit))
5472 firstdigit = 'Z';
5473 else
5474 firstdigit = 'z';
5475 }
5476 else
5477#ifdef EBCDIC
5478 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5479#else
5480 firstdigit += Prenum1;
5481#endif
5482 }
5483 curwin->w_cursor.col = col;
5484 (void)del_char(FALSE);
5485 ins_char(firstdigit);
5486 }
5487 else
5488 {
5489 negative = FALSE;
5490 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5491 {
5492 --col;
5493 negative = TRUE;
5494 }
5495
5496 /* get the number value (unsigned) */
5497 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5498
5499 /* ignore leading '-' for hex and octal numbers */
5500 if (hex && negative)
5501 {
5502 ++col;
5503 --length;
5504 negative = FALSE;
5505 }
5506
5507 /* add or subtract */
5508 subtract = FALSE;
5509 if (command == Ctrl_X)
5510 subtract ^= TRUE;
5511 if (negative)
5512 subtract ^= TRUE;
5513
5514 oldn = n;
5515 if (subtract)
5516 n -= (unsigned long)Prenum1;
5517 else
5518 n += (unsigned long)Prenum1;
5519
5520 /* handle wraparound for decimal numbers */
5521 if (!hex)
5522 {
5523 if (subtract)
5524 {
5525 if (n > oldn)
5526 {
5527 n = 1 + (n ^ (unsigned long)-1);
5528 negative ^= TRUE;
5529 }
5530 }
5531 else /* add */
5532 {
5533 if (n < oldn)
5534 {
5535 n = (n ^ (unsigned long)-1);
5536 negative ^= TRUE;
5537 }
5538 }
5539 if (n == 0)
5540 negative = FALSE;
5541 }
5542
5543 /*
5544 * Delete the old number.
5545 */
5546 curwin->w_cursor.col = col;
5547 todel = length;
5548 c = gchar_cursor();
5549 /*
5550 * Don't include the '-' in the length, only the length of the part
5551 * after it is kept the same.
5552 */
5553 if (c == '-')
5554 --length;
5555 while (todel-- > 0)
5556 {
5557 if (c < 0x100 && isalpha(c))
5558 {
5559 if (isupper(c))
5560 hexupper = TRUE;
5561 else
5562 hexupper = FALSE;
5563 }
5564 /* del_char() will mark line needing displaying */
5565 (void)del_char(FALSE);
5566 c = gchar_cursor();
5567 }
5568
5569 /*
5570 * Prepare the leading characters in buf1[].
5571 * When there are many leading zeros it could be very long. Allocate
5572 * a bit too much.
5573 */
5574 buf1 = alloc((unsigned)length + NUMBUFLEN);
5575 if (buf1 == NULL)
5576 return FAIL;
5577 ptr = buf1;
5578 if (negative)
5579 {
5580 *ptr++ = '-';
5581 }
5582 if (hex)
5583 {
5584 *ptr++ = '0';
5585 --length;
5586 }
5587 if (hex == 'x' || hex == 'X')
5588 {
5589 *ptr++ = hex;
5590 --length;
5591 }
5592
5593 /*
5594 * Put the number characters in buf2[].
5595 */
5596 if (hex == 0)
5597 sprintf((char *)buf2, "%lu", n);
5598 else if (hex == '0')
5599 sprintf((char *)buf2, "%lo", n);
5600 else if (hex && hexupper)
5601 sprintf((char *)buf2, "%lX", n);
5602 else
5603 sprintf((char *)buf2, "%lx", n);
5604 length -= (int)STRLEN(buf2);
5605
5606 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005607 * Adjust number of zeros to the new number of digits, so the
5608 * total length of the number remains the same.
5609 * Don't do this when
5610 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005612 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 while (length-- > 0)
5614 *ptr++ = '0';
5615 *ptr = NUL;
5616 STRCAT(buf1, buf2);
5617 ins_str(buf1); /* insert the new number */
5618 vim_free(buf1);
5619 }
5620 --curwin->w_cursor.col;
5621 curwin->w_set_curswant = TRUE;
5622#ifdef FEAT_RIGHTLEFT
5623 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5624 RLADDSUBFIX(ptr);
5625#endif
5626 return OK;
5627}
5628
5629#ifdef FEAT_VIMINFO
5630 int
5631read_viminfo_register(virp, force)
5632 vir_T *virp;
5633 int force;
5634{
5635 int eof;
5636 int do_it = TRUE;
5637 int size;
5638 int limit;
5639 int i;
5640 int set_prev = FALSE;
5641 char_u *str;
5642 char_u **array = NULL;
5643
5644 /* We only get here (hopefully) if line[0] == '"' */
5645 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005646
5647 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 if (*str == '"')
5649 {
5650 set_prev = TRUE;
5651 str++;
5652 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (!ASCII_ISALNUM(*str) && *str != '-')
5655 {
5656 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5657 return TRUE; /* too many errors, pretend end-of-file */
5658 do_it = FALSE;
5659 }
5660 get_yank_register(*str++, FALSE);
5661 if (!force && y_current->y_array != NULL)
5662 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005663
5664 if (*str == '@')
5665 {
5666 /* "x@: register x used for @@ */
5667 if (force || execreg_lastc == NUL)
5668 execreg_lastc = str[-1];
5669 }
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 size = 0;
5672 limit = 100; /* Optimized for registers containing <= 100 lines */
5673 if (do_it)
5674 {
5675 if (set_prev)
5676 y_previous = y_current;
5677 vim_free(y_current->y_array);
5678 array = y_current->y_array =
5679 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005680 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (STRNCMP(str, "CHAR", 4) == 0)
5682 y_current->y_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 else if (STRNCMP(str, "BLOCK", 5) == 0)
5684 y_current->y_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 else
5686 y_current->y_type = MLINE;
5687 /* get the block width; if it's missing we get a zero, which is OK */
5688 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 y_current->y_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691
5692 while (!(eof = viminfo_readline(virp))
5693 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5694 {
5695 if (do_it)
5696 {
5697 if (size >= limit)
5698 {
5699 y_current->y_array = (char_u **)
5700 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5701 for (i = 0; i < limit; i++)
5702 y_current->y_array[i] = array[i];
5703 vim_free(array);
5704 limit *= 2;
5705 array = y_current->y_array;
5706 }
5707 str = viminfo_readstring(virp, 1, TRUE);
5708 if (str != NULL)
5709 array[size++] = str;
5710 else
5711 do_it = FALSE;
5712 }
5713 }
5714 if (do_it)
5715 {
5716 if (size == 0)
5717 {
5718 vim_free(array);
5719 y_current->y_array = NULL;
5720 }
5721 else if (size < limit)
5722 {
5723 y_current->y_array =
5724 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5725 for (i = 0; i < size; i++)
5726 y_current->y_array[i] = array[i];
5727 vim_free(array);
5728 }
5729 y_current->y_size = size;
5730 }
5731 return eof;
5732}
5733
5734 void
5735write_viminfo_registers(fp)
5736 FILE *fp;
5737{
5738 int i, j;
5739 char_u *type;
5740 char_u c;
5741 int num_lines;
5742 int max_num_lines;
5743 int max_kbyte;
5744 long len;
5745
Bram Moolenaar64404472010-06-26 06:24:45 +02005746 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 /* Get '<' value, use old '"' value if '<' is not found. */
5749 max_num_lines = get_viminfo_parameter('<');
5750 if (max_num_lines < 0)
5751 max_num_lines = get_viminfo_parameter('"');
5752 if (max_num_lines == 0)
5753 return;
5754 max_kbyte = get_viminfo_parameter('s');
5755 if (max_kbyte == 0)
5756 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 for (i = 0; i < NUM_REGISTERS; i++)
5759 {
5760 if (y_regs[i].y_array == NULL)
5761 continue;
5762#ifdef FEAT_CLIPBOARD
5763 /* Skip '*'/'+' register, we don't want them back next time */
5764 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5765 continue;
5766#endif
5767#ifdef FEAT_DND
5768 /* Neither do we want the '~' register */
5769 if (i == TILDE_REGISTER)
5770 continue;
5771#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005772 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005774 if (num_lines == 0
5775 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005776 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005777 continue;
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 if (max_kbyte > 0)
5780 {
5781 /* Skip register if there is more text than the maximum size. */
5782 len = 0;
5783 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005784 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (len > (long)max_kbyte * 1024L)
5786 continue;
5787 }
5788
5789 switch (y_regs[i].y_type)
5790 {
5791 case MLINE:
5792 type = (char_u *)"LINE";
5793 break;
5794 case MCHAR:
5795 type = (char_u *)"CHAR";
5796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 case MBLOCK:
5798 type = (char_u *)"BLOCK";
5799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 default:
5801 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005802 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 emsg(IObuff);
5804 type = (char_u *)"LINE";
5805 break;
5806 }
5807 if (y_previous == &y_regs[i])
5808 fprintf(fp, "\"");
5809 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005810 fprintf(fp, "\"%c", c);
5811 if (c == execreg_lastc)
5812 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005813 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 /* If max_num_lines < 0, then we save ALL the lines in the register */
5816 if (max_num_lines > 0 && num_lines > max_num_lines)
5817 num_lines = max_num_lines;
5818 for (j = 0; j < num_lines; j++)
5819 {
5820 putc('\t', fp);
5821 viminfo_writestring(fp, y_regs[i].y_array[j]);
5822 }
5823 }
5824}
5825#endif /* FEAT_VIMINFO */
5826
5827#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5828/*
5829 * SELECTION / PRIMARY ('*')
5830 *
5831 * Text selection stuff that uses the GUI selection register '*'. When using a
5832 * GUI this may be text from another window, otherwise it is the last text we
5833 * had highlighted with VIsual mode. With mouse support, clicking the middle
5834 * button performs the paste, otherwise you will need to do <"*p>. "
5835 * If not under X, it is synonymous with the clipboard register '+'.
5836 *
5837 * X CLIPBOARD ('+')
5838 *
5839 * Text selection stuff that uses the GUI clipboard register '+'.
5840 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5841 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5842 * otherwise you will need to do <"+p>. "
5843 * If not under X, it is synonymous with the selection register '*'.
5844 */
5845
5846/*
5847 * Routine to export any final X selection we had to the environment
5848 * so that the text is still available after vim has exited. X selections
5849 * only exist while the owning application exists, so we write to the
5850 * permanent (while X runs) store CUT_BUFFER0.
5851 * Dump the CLIPBOARD selection if we own it (it's logically the more
5852 * 'permanent' of the two), otherwise the PRIMARY one.
5853 * For now, use a hard-coded sanity limit of 1Mb of data.
5854 */
5855#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5856 void
5857x11_export_final_selection()
5858{
5859 Display *dpy;
5860 char_u *str = NULL;
5861 long_u len = 0;
5862 int motion_type = -1;
5863
5864# ifdef FEAT_GUI
5865 if (gui.in_use)
5866 dpy = X_DISPLAY;
5867 else
5868# endif
5869# ifdef FEAT_XCLIPBOARD
5870 dpy = xterm_dpy;
5871# else
5872 return;
5873# endif
5874
5875 /* Get selection to export */
5876 if (clip_plus.owned)
5877 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5878 else if (clip_star.owned)
5879 motion_type = clip_convert_selection(&str, &len, &clip_star);
5880
5881 /* Check it's OK */
5882 if (dpy != NULL && str != NULL && motion_type >= 0
5883 && len < 1024*1024 && len > 0)
5884 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005885#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005886 int ok = TRUE;
5887
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005888 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5889 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5890 * encoding conversion usually doesn't work, so keep the text as-is.
5891 */
5892 if (has_mbyte)
5893 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005894 vimconv_T vc;
5895
5896 vc.vc_type = CONV_NONE;
5897 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5898 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005899 int intlen = len;
5900 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005901
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005902 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005903 conv_str = string_convert(&vc, str, &intlen);
5904 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005905 if (conv_str != NULL)
5906 {
5907 vim_free(str);
5908 str = conv_str;
5909 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005910 else
5911 {
5912 ok = FALSE;
5913 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005914 convert_setup(&vc, NULL, NULL);
5915 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005916 else
5917 {
5918 ok = FALSE;
5919 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005920 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005921
5922 /* Do not store the string if conversion failed. Better to use any
5923 * other selection than garbled text. */
5924 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005925#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005926 {
5927 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5928 XFlush(dpy);
5929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931
5932 vim_free(str);
5933}
5934#endif
5935
5936 void
5937clip_free_selection(cbd)
5938 VimClipboard *cbd;
5939{
5940 struct yankreg *y_ptr = y_current;
5941
5942 if (cbd == &clip_plus)
5943 y_current = &y_regs[PLUS_REGISTER];
5944 else
5945 y_current = &y_regs[STAR_REGISTER];
5946 free_yank_all();
5947 y_current->y_size = 0;
5948 y_current = y_ptr;
5949}
5950
5951/*
5952 * Get the selected text and put it in the gui selection register '*' or '+'.
5953 */
5954 void
5955clip_get_selection(cbd)
5956 VimClipboard *cbd;
5957{
5958 struct yankreg *old_y_previous, *old_y_current;
5959 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 pos_T old_visual;
5961 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 colnr_T old_curswant;
5963 int old_set_curswant;
5964 pos_T old_op_start, old_op_end;
5965 oparg_T oa;
5966 cmdarg_T ca;
5967
5968 if (cbd->owned)
5969 {
5970 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5971 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5972 return;
5973
5974 /* Get the text between clip_star.start & clip_star.end */
5975 old_y_previous = y_previous;
5976 old_y_current = y_current;
5977 old_cursor = curwin->w_cursor;
5978 old_curswant = curwin->w_curswant;
5979 old_set_curswant = curwin->w_set_curswant;
5980 old_op_start = curbuf->b_op_start;
5981 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 old_visual = VIsual;
5983 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 clear_oparg(&oa);
5985 oa.regname = (cbd == &clip_plus ? '+' : '*');
5986 oa.op_type = OP_YANK;
5987 vim_memset(&ca, 0, sizeof(ca));
5988 ca.oap = &oa;
5989 ca.cmdchar = 'y';
5990 ca.count1 = 1;
5991 ca.retval = CA_NO_ADJ_OP_END;
5992 do_pending_operator(&ca, 0, TRUE);
5993 y_previous = old_y_previous;
5994 y_current = old_y_current;
5995 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005996 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 curwin->w_curswant = old_curswant;
5998 curwin->w_set_curswant = old_set_curswant;
5999 curbuf->b_op_start = old_op_start;
6000 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 VIsual = old_visual;
6002 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 }
6004 else
6005 {
6006 clip_free_selection(cbd);
6007
6008 /* Try to get selected text from another window */
6009 clip_gen_request_selection(cbd);
6010 }
6011}
6012
Bram Moolenaard44347f2011-06-19 01:14:29 +02006013/*
6014 * Convert from the GUI selection string into the '*'/'+' register.
6015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 void
6017clip_yank_selection(type, str, len, cbd)
6018 int type;
6019 char_u *str;
6020 long len;
6021 VimClipboard *cbd;
6022{
6023 struct yankreg *y_ptr;
6024
6025 if (cbd == &clip_plus)
6026 y_ptr = &y_regs[PLUS_REGISTER];
6027 else
6028 y_ptr = &y_regs[STAR_REGISTER];
6029
6030 clip_free_selection(cbd);
6031
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006032 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033}
6034
6035/*
6036 * Convert the '*'/'+' register into a GUI selection string returned in *str
6037 * with length *len.
6038 * Returns the motion type, or -1 for failure.
6039 */
6040 int
6041clip_convert_selection(str, len, cbd)
6042 char_u **str;
6043 long_u *len;
6044 VimClipboard *cbd;
6045{
6046 char_u *p;
6047 int lnum;
6048 int i, j;
6049 int_u eolsize;
6050 struct yankreg *y_ptr;
6051
6052 if (cbd == &clip_plus)
6053 y_ptr = &y_regs[PLUS_REGISTER];
6054 else
6055 y_ptr = &y_regs[STAR_REGISTER];
6056
6057#ifdef USE_CRNL
6058 eolsize = 2;
6059#else
6060 eolsize = 1;
6061#endif
6062
6063 *str = NULL;
6064 *len = 0;
6065 if (y_ptr->y_array == NULL)
6066 return -1;
6067
6068 for (i = 0; i < y_ptr->y_size; i++)
6069 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6070
6071 /*
6072 * Don't want newline character at end of last line if we're in MCHAR mode.
6073 */
6074 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6075 *len -= eolsize;
6076
6077 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6078 if (p == NULL)
6079 return -1;
6080 lnum = 0;
6081 for (i = 0, j = 0; i < (int)*len; i++, j++)
6082 {
6083 if (y_ptr->y_array[lnum][j] == '\n')
6084 p[i] = NUL;
6085 else if (y_ptr->y_array[lnum][j] == NUL)
6086 {
6087#ifdef USE_CRNL
6088 p[i++] = '\r';
6089#endif
6090#ifdef USE_CR
6091 p[i] = '\r';
6092#else
6093 p[i] = '\n';
6094#endif
6095 lnum++;
6096 j = -1;
6097 }
6098 else
6099 p[i] = y_ptr->y_array[lnum][j];
6100 }
6101 return y_ptr->y_type;
6102}
6103
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105/*
6106 * If we have written to a clipboard register, send the text to the clipboard.
6107 */
6108 static void
6109may_set_selection()
6110{
6111 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6112 {
6113 clip_own_selection(&clip_star);
6114 clip_gen_set_selection(&clip_star);
6115 }
6116 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6117 {
6118 clip_own_selection(&clip_plus);
6119 clip_gen_set_selection(&clip_plus);
6120 }
6121}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123#endif /* FEAT_CLIPBOARD || PROTO */
6124
6125
6126#if defined(FEAT_DND) || defined(PROTO)
6127/*
6128 * Replace the contents of the '~' register with str.
6129 */
6130 void
6131dnd_yank_drag_data(str, len)
6132 char_u *str;
6133 long len;
6134{
6135 struct yankreg *curr;
6136
6137 curr = y_current;
6138 y_current = &y_regs[TILDE_REGISTER];
6139 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006140 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 y_current = curr;
6142}
6143#endif
6144
6145
6146#if defined(FEAT_EVAL) || defined(PROTO)
6147/*
6148 * Return the type of a register.
6149 * Used for getregtype()
6150 * Returns MAUTO for error.
6151 */
6152 char_u
6153get_reg_type(regname, reglen)
6154 int regname;
6155 long *reglen;
6156{
6157 switch (regname)
6158 {
6159 case '%': /* file name */
6160 case '#': /* alternate file name */
6161 case '=': /* expression */
6162 case ':': /* last command line */
6163 case '/': /* last search-pattern */
6164 case '.': /* last inserted text */
6165#ifdef FEAT_SEARCHPATH
6166 case Ctrl_F: /* Filename under cursor */
6167 case Ctrl_P: /* Path under cursor, expand via "path" */
6168#endif
6169 case Ctrl_W: /* word under cursor */
6170 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6171 case '_': /* black hole: always empty */
6172 return MCHAR;
6173 }
6174
6175#ifdef FEAT_CLIPBOARD
6176 regname = may_get_selection(regname);
6177#endif
6178
Bram Moolenaar32b92012014-01-14 12:33:36 +01006179 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6180 return MAUTO;
6181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 get_yank_register(regname, FALSE);
6183
6184 if (y_current->y_array != NULL)
6185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (reglen != NULL && y_current->y_type == MBLOCK)
6187 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return y_current->y_type;
6189 }
6190 return MAUTO;
6191}
6192
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006193static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6194
6195/*
6196 * When "flags" has GREG_LIST return a list with text "s".
6197 * Otherwise just return "s".
6198 */
6199 static char_u *
6200getreg_wrap_one_line(s, flags)
6201 char_u *s;
6202 int flags;
6203{
6204 if (flags & GREG_LIST)
6205 {
6206 list_T *list = list_alloc();
6207
6208 if (list != NULL)
6209 {
6210 if (list_append_string(list, NULL, -1) == FAIL)
6211 {
6212 list_free(list, TRUE);
6213 return NULL;
6214 }
6215 list->lv_first->li_tv.vval.v_string = s;
6216 }
6217 return (char_u *)list;
6218 }
6219 return s;
6220}
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222/*
6223 * Return the contents of a register as a single allocated string.
6224 * Used for "@r" in expressions and for getreg().
6225 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006226 * Flags:
6227 * GREG_NO_EXPR Do not allow expression register
6228 * GREG_EXPR_SRC For the expression register: return expression itself,
6229 * not the result of its evaluation.
6230 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 */
6232 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006233get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006235 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 long i;
6238 char_u *retval;
6239 int allocated;
6240 long len;
6241
6242 /* Don't allow using an expression register inside an expression */
6243 if (regname == '=')
6244 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006245 if (flags & GREG_NO_EXPR)
6246 return NULL;
6247 if (flags & GREG_EXPR_SRC)
6248 return getreg_wrap_one_line(get_expr_line_src(), flags);
6249 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251
6252 if (regname == '@') /* "@@" is used for unnamed register */
6253 regname = '"';
6254
6255 /* check for valid regname */
6256 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6257 return NULL;
6258
6259#ifdef FEAT_CLIPBOARD
6260 regname = may_get_selection(regname);
6261#endif
6262
6263 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6264 {
6265 if (retval == NULL)
6266 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006267 if (allocated)
6268 return getreg_wrap_one_line(retval, flags);
6269 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271
6272 get_yank_register(regname, FALSE);
6273 if (y_current->y_array == NULL)
6274 return NULL;
6275
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006276 if (flags & GREG_LIST)
6277 {
6278 list_T *list = list_alloc();
6279 int error = FALSE;
6280
6281 if (list == NULL)
6282 return NULL;
6283 for (i = 0; i < y_current->y_size; ++i)
6284 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6285 error = TRUE;
6286 if (error)
6287 {
6288 list_free(list, TRUE);
6289 return NULL;
6290 }
6291 return (char_u *)list;
6292 }
6293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 /*
6295 * Compute length of resulting string.
6296 */
6297 len = 0;
6298 for (i = 0; i < y_current->y_size; ++i)
6299 {
6300 len += (long)STRLEN(y_current->y_array[i]);
6301 /*
6302 * Insert a newline between lines and after last line if
6303 * y_type is MLINE.
6304 */
6305 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6306 ++len;
6307 }
6308
6309 retval = lalloc(len + 1, TRUE);
6310
6311 /*
6312 * Copy the lines of the yank register into the string.
6313 */
6314 if (retval != NULL)
6315 {
6316 len = 0;
6317 for (i = 0; i < y_current->y_size; ++i)
6318 {
6319 STRCPY(retval + len, y_current->y_array[i]);
6320 len += (long)STRLEN(retval + len);
6321
6322 /*
6323 * Insert a NL between lines and after the last line if y_type is
6324 * MLINE.
6325 */
6326 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6327 retval[len++] = '\n';
6328 }
6329 retval[len] = NUL;
6330 }
6331
6332 return retval;
6333}
6334
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006335 static int
6336init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6337 int name;
6338 struct yankreg **old_y_previous;
6339 struct yankreg **old_y_current;
6340 int must_append;
6341 int *yank_type UNUSED;
6342{
6343 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6344 {
6345 emsg_invreg(name);
6346 return FAIL;
6347 }
6348
6349 /* Don't want to change the current (unnamed) register */
6350 *old_y_previous = y_previous;
6351 *old_y_current = y_current;
6352
6353 get_yank_register(name, TRUE);
6354 if (!y_append && !must_append)
6355 free_yank_all();
6356 return OK;
6357}
6358
6359 static void
6360finish_write_reg(name, old_y_previous, old_y_current)
6361 int name;
6362 struct yankreg *old_y_previous;
6363 struct yankreg *old_y_current;
6364{
6365# ifdef FEAT_CLIPBOARD
6366 /* Send text of clipboard register to the clipboard. */
6367 may_set_selection();
6368# endif
6369
6370 /* ':let @" = "val"' should change the meaning of the "" register */
6371 if (name != '"')
6372 y_previous = old_y_previous;
6373 y_current = old_y_current;
6374}
6375
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376/*
6377 * Store string "str" in register "name".
6378 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6379 * If "must_append" is TRUE, always append to the register. Otherwise append
6380 * if "name" is an uppercase letter.
6381 * Note: "maxlen" and "must_append" don't work for the "/" register.
6382 * Careful: 'str' is modified, you may have to use a copy!
6383 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6384 */
6385 void
6386write_reg_contents(name, str, maxlen, must_append)
6387 int name;
6388 char_u *str;
6389 int maxlen;
6390 int must_append;
6391{
6392 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6393}
6394
6395 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006396write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6397 int name;
6398 char_u **strings;
6399 int maxlen UNUSED;
6400 int must_append;
6401 int yank_type;
6402 long block_len;
6403{
6404 struct yankreg *old_y_previous, *old_y_current;
6405
6406 if (name == '/'
6407#ifdef FEAT_EVAL
6408 || name == '='
6409#endif
6410 )
6411 {
6412 char_u *s;
6413
6414 if (strings[0] == NULL)
6415 s = (char_u *)"";
6416 else if (strings[1] != NULL)
6417 {
6418 EMSG(_("E883: search pattern and expression register may not "
6419 "contain two or more lines"));
6420 return;
6421 }
6422 else
6423 s = strings[0];
6424 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6425 return;
6426 }
6427
6428 if (name == '_') /* black hole: nothing to do */
6429 return;
6430
6431 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6432 &yank_type) == FAIL)
6433 return;
6434
6435 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6436
6437 finish_write_reg(name, old_y_previous, old_y_current);
6438}
6439
6440 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6442 int name;
6443 char_u *str;
6444 int maxlen;
6445 int must_append;
6446 int yank_type;
6447 long block_len;
6448{
6449 struct yankreg *old_y_previous, *old_y_current;
6450 long len;
6451
Bram Moolenaare7566042005-06-17 22:00:15 +00006452 if (maxlen >= 0)
6453 len = maxlen;
6454 else
6455 len = (long)STRLEN(str);
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 /* Special case: '/' search pattern */
6458 if (name == '/')
6459 {
6460 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6461 return;
6462 }
6463
Bram Moolenaare7566042005-06-17 22:00:15 +00006464#ifdef FEAT_EVAL
6465 if (name == '=')
6466 {
6467 char_u *p, *s;
6468
6469 p = vim_strnsave(str, (int)len);
6470 if (p == NULL)
6471 return;
6472 if (must_append)
6473 {
6474 s = concat_str(get_expr_line_src(), p);
6475 vim_free(p);
6476 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006477 }
6478 set_expr_line(p);
6479 return;
6480 }
6481#endif
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (name == '_') /* black hole: nothing to do */
6484 return;
6485
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006486 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6487 &yank_type) == FAIL)
6488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006490 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006492 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493}
6494#endif /* FEAT_EVAL */
6495
6496#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6497/*
6498 * Put a string into a register. When the register is not empty, the string
6499 * is appended.
6500 */
6501 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006502str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006504 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 char_u *str; /* string to put in register */
6506 long len; /* length of string */
6507 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006508 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006510 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 int lnum;
6512 long start;
6513 long i;
6514 int extra;
6515 int newlines; /* number of lines added */
6516 int extraline = 0; /* extra line at the end */
6517 int append = FALSE; /* append to last line in register */
6518 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006519 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006523 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 y_ptr->y_size = 0;
6525
Bram Moolenaard44347f2011-06-19 01:14:29 +02006526 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006527 type = ((str_list || (len > 0 && (str[len - 1] == NL
6528 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006529 ? MLINE : MCHAR);
6530 else
6531 type = yank_type;
6532
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 /*
6534 * Count the number of lines within the string
6535 */
6536 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006537 if (str_list)
6538 {
6539 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006544 for (i = 0; i < len; i++)
6545 if (str[i] == '\n')
6546 ++newlines;
6547 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6548 {
6549 extraline = 1;
6550 ++newlines; /* count extra newline at the end */
6551 }
6552 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6553 {
6554 append = TRUE;
6555 --newlines; /* uncount newline when appending first line */
6556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
6558
6559 /*
6560 * Allocate an array to hold the pointers to the new register lines.
6561 * If the register was not empty, move the existing lines to the new array.
6562 */
6563 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6564 * sizeof(char_u *), TRUE);
6565 if (pp == NULL) /* out of memory */
6566 return;
6567 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6568 pp[lnum] = y_ptr->y_array[lnum];
6569 vim_free(y_ptr->y_array);
6570 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 /*
6574 * Find the end of each line and save it into the array.
6575 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006576 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006578 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6579 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006580 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006581 pp[lnum] = vim_strnsave(*ss, i);
6582 if (i > maxlen)
6583 maxlen = i;
6584 }
6585 }
6586 else
6587 {
6588 for (start = 0; start < len + extraline; start += i + 1)
6589 {
6590 for (i = start; i < len; ++i) /* find the end of the line */
6591 if (str[i] == '\n')
6592 break;
6593 i -= start; /* i is now length of line */
6594 if (i > maxlen)
6595 maxlen = i;
6596 if (append)
6597 {
6598 --lnum;
6599 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6600 }
6601 else
6602 extra = 0;
6603 s = alloc((unsigned)(i + extra + 1));
6604 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006606 if (extra)
6607 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6608 if (append)
6609 vim_free(y_ptr->y_array[lnum]);
6610 if (i)
6611 mch_memmove(s + extra, str + start, (size_t)i);
6612 extra += i;
6613 s[extra] = NUL;
6614 y_ptr->y_array[lnum++] = s;
6615 while (--extra >= 0)
6616 {
6617 if (*s == NUL)
6618 *s = '\n'; /* replace NUL with newline */
6619 ++s;
6620 }
6621 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 }
6624 y_ptr->y_type = type;
6625 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (type == MBLOCK)
6627 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6628 else
6629 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630}
6631#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6632
6633 void
6634clear_oparg(oap)
6635 oparg_T *oap;
6636{
6637 vim_memset(oap, 0, sizeof(oparg_T));
6638}
6639
Bram Moolenaar7c626922005-02-07 22:01:03 +00006640static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006643 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 *
6645 * "Words" are counted by looking for boundaries between non-space and
6646 * space characters. (it seems to produce results that match 'wc'.)
6647 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006648 * Return value is byte count; word count for the line is added to "*wc".
6649 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 *
6651 * The function will only examine the first "limit" characters in the
6652 * line, stopping if it encounters an end-of-line (NUL byte). In that
6653 * case, eol_size will be added to the character count to account for
6654 * the size of the EOL character.
6655 */
6656 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006657line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 char_u *line;
6659 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006660 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 long limit;
6662 int eol_size;
6663{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006664 long i;
6665 long words = 0;
6666 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 int is_word = 0;
6668
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006669 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
6671 if (is_word)
6672 {
6673 if (vim_isspace(line[i]))
6674 {
6675 words++;
6676 is_word = 0;
6677 }
6678 }
6679 else if (!vim_isspace(line[i]))
6680 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006681 ++chars;
6682#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006684#else
6685 ++i;
6686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688
6689 if (is_word)
6690 words++;
6691 *wc += words;
6692
6693 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006694 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006697 chars += eol_size;
6698 }
6699 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 return i;
6701}
6702
6703/*
6704 * Give some info about the position of the cursor (for "g CTRL-G").
6705 * In Visual mode, give some info about the selected region. (In this case,
6706 * the *_count_cursor variables store running totals for the selection.)
6707 */
6708 void
6709cursor_pos_info()
6710{
6711 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006712 char_u buf1[50];
6713 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006715 long byte_count = 0;
6716 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 long char_count = 0;
6718 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 long word_count = 0;
6720 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006721 int eol_size;
6722 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 long line_count_selected = 0;
6724 pos_T min_pos, max_pos;
6725 oparg_T oparg;
6726 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
6728 /*
6729 * Compute the length of the file in characters.
6730 */
6731 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6732 {
6733 MSG(_(no_lines_msg));
6734 }
6735 else
6736 {
6737 if (get_fileformat(curbuf) == EOL_DOS)
6738 eol_size = 2;
6739 else
6740 eol_size = 1;
6741
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (VIsual_active)
6743 {
6744 if (lt(VIsual, curwin->w_cursor))
6745 {
6746 min_pos = VIsual;
6747 max_pos = curwin->w_cursor;
6748 }
6749 else
6750 {
6751 min_pos = curwin->w_cursor;
6752 max_pos = VIsual;
6753 }
6754 if (*p_sel == 'e' && max_pos.col > 0)
6755 --max_pos.col;
6756
6757 if (VIsual_mode == Ctrl_V)
6758 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006759#ifdef FEAT_LINEBREAK
6760 char_u * saved_sbr = p_sbr;
6761
6762 /* Make 'sbr' empty for a moment to get the correct size. */
6763 p_sbr = empty_option;
6764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 oparg.is_VIsual = 1;
6766 oparg.block_mode = TRUE;
6767 oparg.op_type = OP_NOP;
6768 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006769 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006770#ifdef FEAT_LINEBREAK
6771 p_sbr = saved_sbr;
6772#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006773 if (curwin->w_curswant == MAXCOL)
6774 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 /* Swap the start, end vcol if needed */
6776 if (oparg.end_vcol < oparg.start_vcol)
6777 {
6778 oparg.end_vcol += oparg.start_vcol;
6779 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6780 oparg.end_vcol -= oparg.start_vcol;
6781 }
6782 }
6783 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6787 {
6788 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006789 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 ui_breakcheck();
6792 if (got_int)
6793 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006794 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 }
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* Do extra processing for VIsual mode. */
6798 if (VIsual_active
6799 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6800 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006801 char_u *s = NULL;
6802 long len = 0L;
6803
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 switch (VIsual_mode)
6805 {
6806 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006807#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006811#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006813#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006814 s = bd.textstart;
6815 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 break;
6817 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006818 s = ml_get(lnum);
6819 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 break;
6821 case 'v':
6822 {
6823 colnr_T start_col = (lnum == min_pos.lnum)
6824 ? min_pos.col : 0;
6825 colnr_T end_col = (lnum == max_pos.lnum)
6826 ? max_pos.col - start_col + 1 : MAXCOL;
6827
Bram Moolenaardef9e822004-12-31 20:58:58 +00006828 s = ml_get(lnum) + start_col;
6829 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
6831 break;
6832 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006833 if (s != NULL)
6834 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006835 byte_count_cursor += line_count_info(s, &word_count_cursor,
6836 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006837 if (lnum == curbuf->b_ml.ml_line_count
6838 && !curbuf->b_p_eol
6839 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006840 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006841 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 }
6844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
6846 /* In non-visual mode, check for the line the cursor is on */
6847 if (lnum == curwin->w_cursor.lnum)
6848 {
6849 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006850 char_count_cursor += char_count;
6851 byte_count_cursor = byte_count +
6852 line_count_info(ml_get(lnum),
6853 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 (long)(curwin->w_cursor.col + 1), eol_size);
6855 }
6856 }
6857 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006858 byte_count += line_count_info(ml_get(lnum), &word_count,
6859 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
6861
6862 /* Correction for when last line doesn't have an EOL. */
6863 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006864 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (VIsual_active)
6867 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006868 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {
6870 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006871 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006872 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6874 }
6875 else
6876 buf1[0] = NUL;
6877
Bram Moolenaar7c626922005-02-07 22:01:03 +00006878 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006879 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006880 vim_snprintf((char *)IObuff, IOSIZE,
6881 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 buf1, line_count_selected,
6883 (long)curbuf->b_ml.ml_line_count,
6884 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006885 byte_count_cursor, byte_count);
6886 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006887 vim_snprintf((char *)IObuff, IOSIZE,
6888 _("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 +00006889 buf1, line_count_selected,
6890 (long)curbuf->b_ml.ml_line_count,
6891 word_count_cursor, word_count,
6892 char_count_cursor, char_count,
6893 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
6895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 {
6897 p = ml_get_curline();
6898 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006899 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006901 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
6902 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
Bram Moolenaar7c626922005-02-07 22:01:03 +00006904 if (char_count_cursor == byte_count_cursor
6905 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006906 vim_snprintf((char *)IObuff, IOSIZE,
6907 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 (char *)buf1, (char *)buf2,
6909 (long)curwin->w_cursor.lnum,
6910 (long)curbuf->b_ml.ml_line_count,
6911 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006912 byte_count_cursor, byte_count);
6913 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006914 vim_snprintf((char *)IObuff, IOSIZE,
6915 _("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 +00006916 (char *)buf1, (char *)buf2,
6917 (long)curwin->w_cursor.lnum,
6918 (long)curbuf->b_ml.ml_line_count,
6919 word_count_cursor, word_count,
6920 char_count_cursor, char_count,
6921 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 }
6923
6924#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006925 byte_count = bomb_size();
6926 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006928 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929#endif
6930 /* Don't shorten this message, the user asked for it. */
6931 p = p_shm;
6932 p_shm = (char_u *)"";
6933 msg(IObuff);
6934 p_shm = p;
6935 }
6936}