blob: 8f0031401e1f45dd4bc62bf485d35779ed4afbf1 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
53 * Each yank register is an array of pointers to lines.
54 */
55static struct yankreg
56{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
60#ifdef FEAT_VISUAL
61 colnr_T y_width; /* only set if y_type == MBLOCK */
62#endif
63} y_regs[NUM_REGISTERS];
64
65static struct yankreg *y_current; /* ptr to current yankreg */
66static int y_append; /* TRUE when appending */
67static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
68
69/*
70 * structure used by block_prep, op_delete and op_yank for blockwise operators
71 * also op_change, op_shift, op_insert, op_replace - AKelly
72 */
73struct block_def
74{
75 int startspaces; /* 'extra' cols of first char */
76 int endspaces; /* 'extra' cols of first char */
77 int textlen; /* chars in block */
78 char_u *textstart; /* pointer to 1st char in block */
79 colnr_T textcol; /* cols of chars (at least part.) in block */
80 colnr_T start_vcol; /* start col of 1st char wholly inside block */
81 colnr_T end_vcol; /* start col of 1st char wholly after block */
82#ifdef FEAT_VISUALEXTRA
83 int is_short; /* TRUE if line is too short to fit in block */
84 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
85 int is_oneChar; /* TRUE if block within one character */
86 int pre_whitesp; /* screen cols of ws before block */
87 int pre_whitesp_c; /* chars of ws before block */
88 colnr_T end_char_vcols; /* number of vcols of post-block char */
89#endif
90 colnr_T start_char_vcols; /* number of vcols of pre-block char */
91};
92
93#ifdef FEAT_VISUALEXTRA
94static void shift_block __ARGS((oparg_T *oap, int amount));
95static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
96#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000097static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000098static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000099static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
100 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#ifdef FEAT_MBYTE
103static void mb_adjust_opend __ARGS((oparg_T *oap));
104#endif
105static void free_yank __ARGS((long));
106static void free_yank_all __ARGS((void));
107static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
108#ifdef FEAT_CLIPBOARD
109static void copy_yank_reg __ARGS((struct yankreg *reg));
110# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
111static void may_set_selection __ARGS((void));
112# endif
113#endif
114static void dis_msg __ARGS((char_u *p, int skip_esc));
115#ifdef FEAT_VISUAL
116static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
117#endif
118#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
119static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
120#endif
121static int ends_in_white __ARGS((linenr_T lnum));
122#ifdef FEAT_COMMENTS
123static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
124static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
125#else
126static int fmt_check_par __ARGS((linenr_T));
127#endif
128
129/*
130 * The names of operators.
131 * IMPORTANT: Index must correspond with defines in vim.h!!!
132 * The third field indicates whether the operator always works on lines.
133 */
134static char opchars[][3] =
135{
136 {NUL, NUL, FALSE}, /* OP_NOP */
137 {'d', NUL, FALSE}, /* OP_DELETE */
138 {'y', NUL, FALSE}, /* OP_YANK */
139 {'c', NUL, FALSE}, /* OP_CHANGE */
140 {'<', NUL, TRUE}, /* OP_LSHIFT */
141 {'>', NUL, TRUE}, /* OP_RSHIFT */
142 {'!', NUL, TRUE}, /* OP_FILTER */
143 {'g', '~', FALSE}, /* OP_TILDE */
144 {'=', NUL, TRUE}, /* OP_INDENT */
145 {'g', 'q', TRUE}, /* OP_FORMAT */
146 {':', NUL, TRUE}, /* OP_COLON */
147 {'g', 'U', FALSE}, /* OP_UPPER */
148 {'g', 'u', FALSE}, /* OP_LOWER */
149 {'J', NUL, TRUE}, /* DO_JOIN */
150 {'g', 'J', TRUE}, /* DO_JOIN_NS */
151 {'g', '?', FALSE}, /* OP_ROT13 */
152 {'r', NUL, FALSE}, /* OP_REPLACE */
153 {'I', NUL, FALSE}, /* OP_INSERT */
154 {'A', NUL, FALSE}, /* OP_APPEND */
155 {'z', 'f', TRUE}, /* OP_FOLD */
156 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
157 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
158 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
159 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
160 {'z', 'd', TRUE}, /* OP_FOLDDEL */
161 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
162 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000163 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164};
165
166/*
167 * Translate a command name into an operator type.
168 * Must only be called with a valid operator name!
169 */
170 int
171get_op_type(char1, char2)
172 int char1;
173 int char2;
174{
175 int i;
176
177 if (char1 == 'r') /* ignore second character */
178 return OP_REPLACE;
179 if (char1 == '~') /* when tilde is an operator */
180 return OP_TILDE;
181 for (i = 0; ; ++i)
182 if (opchars[i][0] == char1 && opchars[i][1] == char2)
183 break;
184 return i;
185}
186
187#if defined(FEAT_VISUAL) || defined(PROTO)
188/*
189 * Return TRUE if operator "op" always works on whole lines.
190 */
191 int
192op_on_lines(op)
193 int op;
194{
195 return opchars[op][2];
196}
197#endif
198
199/*
200 * Get first operator command character.
201 * Returns 'g' or 'z' if there is another command character.
202 */
203 int
204get_op_char(optype)
205 int optype;
206{
207 return opchars[optype][0];
208}
209
210/*
211 * Get second operator command character.
212 */
213 int
214get_extra_op_char(optype)
215 int optype;
216{
217 return opchars[optype][1];
218}
219
220/*
221 * op_shift - handle a shift operation
222 */
223 void
224op_shift(oap, curs_top, amount)
225 oparg_T *oap;
226 int curs_top;
227 int amount;
228{
229 long i;
230 int first_char;
231 char_u *s;
232#ifdef FEAT_VISUAL
233 int block_col = 0;
234#endif
235
236 if (u_save((linenr_T)(oap->start.lnum - 1),
237 (linenr_T)(oap->end.lnum + 1)) == FAIL)
238 return;
239
240#ifdef FEAT_VISUAL
241 if (oap->block_mode)
242 block_col = curwin->w_cursor.col;
243#endif
244
245 for (i = oap->line_count; --i >= 0; )
246 {
247 first_char = *ml_get_curline();
248 if (first_char == NUL) /* empty line */
249 curwin->w_cursor.col = 0;
250#ifdef FEAT_VISUALEXTRA
251 else if (oap->block_mode)
252 shift_block(oap, amount);
253#endif
254 else
255 /* Move the line right if it doesn't start with '#', 'smartindent'
256 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
257#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
258 if (first_char != '#' || !preprocs_left())
259#endif
260 {
261 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount);
262 }
263 ++curwin->w_cursor.lnum;
264 }
265
266 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
267
268#ifdef FEAT_VISUAL
269 if (oap->block_mode)
270 {
271 curwin->w_cursor.lnum = oap->start.lnum;
272 curwin->w_cursor.col = block_col;
273 }
274 else
275#endif
276 if (curs_top) /* put cursor on first line, for ">>" */
277 {
278 curwin->w_cursor.lnum = oap->start.lnum;
279 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
280 }
281 else
282 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
283
284 if (oap->line_count > p_report)
285 {
286 if (oap->op_type == OP_RSHIFT)
287 s = (char_u *)">";
288 else
289 s = (char_u *)"<";
290 if (oap->line_count == 1)
291 {
292 if (amount == 1)
293 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
294 else
295 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
296 }
297 else
298 {
299 if (amount == 1)
300 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
301 oap->line_count, s);
302 else
303 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
304 oap->line_count, s, amount);
305 }
306 msg(IObuff);
307 }
308
309 /*
310 * Set "'[" and "']" marks.
311 */
312 curbuf->b_op_start = oap->start;
313 curbuf->b_op_end.lnum = oap->end.lnum;
314 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
315 if (curbuf->b_op_end.col > 0)
316 --curbuf->b_op_end.col;
317}
318
319/*
320 * shift the current line one shiftwidth left (if left != 0) or right
321 * leaves cursor on first blank in the line
322 */
323 void
324shift_line(left, round, amount)
325 int left;
326 int round;
327 int amount;
328{
329 int count;
330 int i, j;
331 int p_sw = (int)curbuf->b_p_sw;
332
333 count = get_indent(); /* get current indent */
334
335 if (round) /* round off indent */
336 {
337 i = count / p_sw; /* number of p_sw rounded down */
338 j = count % p_sw; /* extra spaces */
339 if (j && left) /* first remove extra spaces */
340 --amount;
341 if (left)
342 {
343 i -= amount;
344 if (i < 0)
345 i = 0;
346 }
347 else
348 i += amount;
349 count = i * p_sw;
350 }
351 else /* original vi indent */
352 {
353 if (left)
354 {
355 count -= p_sw * amount;
356 if (count < 0)
357 count = 0;
358 }
359 else
360 count += p_sw * amount;
361 }
362
363 /* Set new indent */
364#ifdef FEAT_VREPLACE
365 if (State & VREPLACE_FLAG)
366 change_indent(INDENT_SET, count, FALSE, NUL);
367 else
368#endif
369 (void)set_indent(count, SIN_CHANGED);
370}
371
372#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
373/*
374 * Shift one line of the current block one shiftwidth right or left.
375 * Leaves cursor on first character in block.
376 */
377 static void
378shift_block(oap, amount)
379 oparg_T *oap;
380 int amount;
381{
382 int left = (oap->op_type == OP_LSHIFT);
383 int oldstate = State;
384 int total, split;
385 char_u *newp, *oldp, *midp, *ptr;
386 int oldcol = curwin->w_cursor.col;
387 int p_sw = (int)curbuf->b_p_sw;
388 int p_ts = (int)curbuf->b_p_ts;
389 struct block_def bd;
390 int internal = 0;
391 int incr;
392 colnr_T vcol, col = 0, ws_vcol;
393 int i = 0, j = 0;
394 int len;
395
396#ifdef FEAT_RIGHTLEFT
397 int old_p_ri = p_ri;
398
399 p_ri = 0; /* don't want revins in ident */
400#endif
401
402 State = INSERT; /* don't want REPLACE for State */
403 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
404 if (bd.is_short)
405 return;
406
407 /* total is number of screen columns to be inserted/removed */
408 total = amount * p_sw;
409 oldp = ml_get_curline();
410
411 if (!left)
412 {
413 /*
414 * 1. Get start vcol
415 * 2. Total ws vcols
416 * 3. Divvy into TABs & spp
417 * 4. Construct new string
418 */
419 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
420 ws_vcol = bd.start_vcol - bd.pre_whitesp;
421 if (bd.startspaces)
422 {
423#ifdef FEAT_MBYTE
424 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000425 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#endif
427 ++bd.textstart;
428 }
429 for ( ; vim_iswhite(*bd.textstart); )
430 {
431 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
432 total += incr;
433 bd.start_vcol += incr;
434 }
435 /* OK, now total=all the VWS reqd, and textstart points at the 1st
436 * non-ws char in the block. */
437 if (!curbuf->b_p_et)
438 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
439 if (i)
440 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
441 else
442 j = total;
443 /* if we're splitting a TAB, allow for it */
444 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
445 len = (int)STRLEN(bd.textstart) + 1;
446 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
447 if (newp == NULL)
448 return;
449 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
450 mch_memmove(newp, oldp, (size_t)bd.textcol);
451 copy_chars(newp + bd.textcol, (size_t)i, TAB);
452 copy_spaces(newp + bd.textcol + i, (size_t)j);
453 /* the end */
454 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
455 }
456 else /* left */
457 {
458 vcol = oap->start_vcol;
459 /* walk vcol past ws to be removed */
460 for (midp = oldp + bd.textcol;
461 vcol < (oap->start_vcol + total) && vim_iswhite(*midp); )
462 {
463 incr = lbr_chartabsize_adv(&midp, (colnr_T)vcol);
464 vcol += incr;
465 }
466 /* internal is the block-internal ws replacing a split TAB */
467 if (vcol > (oap->start_vcol + total))
468 {
469 /* we have to split the TAB *(midp-1) */
470 internal = vcol - (oap->start_vcol + total);
471 }
472 /* if 'expandtab' is not set, use TABs */
473
474 split = bd.startspaces + internal;
475 if (split > 0)
476 {
477 if (!curbuf->b_p_et)
478 {
479 for (ptr = oldp, col = 0; ptr < oldp+bd.textcol; )
480 col += lbr_chartabsize_adv(&ptr, (colnr_T)col);
481
482 /* col+1 now equals the start col of the first char of the
483 * block (may be < oap.start_vcol if we're splitting a TAB) */
484 i = ((col % p_ts) + split) / p_ts; /* number of tabs */
485 }
486 if (i)
487 j = ((col % p_ts) + split) % p_ts; /* number of spp */
488 else
489 j = split;
490 }
491
492 newp = alloc_check(bd.textcol + i + j + (unsigned)STRLEN(midp) + 1);
493 if (newp == NULL)
494 return;
495 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + STRLEN(midp) + 1));
496
497 /* copy first part we want to keep */
498 mch_memmove(newp, oldp, (size_t)bd.textcol);
499 /* Now copy any TABS and spp to ensure correct alignment! */
500 while (vim_iswhite(*midp))
501 {
502 if (*midp == TAB)
503 i++;
504 else /*space */
505 j++;
506 midp++;
507 }
508 /* We might have an extra TAB worth of spp now! */
509 if (j / p_ts && !curbuf->b_p_et)
510 {
511 i++;
512 j -= p_ts;
513 }
514 copy_chars(newp + bd.textcol, (size_t)i, TAB);
515 copy_spaces(newp + bd.textcol + i, (size_t)j);
516
517 /* the end */
518 mch_memmove(newp + STRLEN(newp), midp, (size_t)STRLEN(midp) + 1);
519 }
520 /* replace the line */
521 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
522 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
523 State = oldstate;
524 curwin->w_cursor.col = oldcol;
525#ifdef FEAT_RIGHTLEFT
526 p_ri = old_p_ri;
527#endif
528}
529#endif
530
531#ifdef FEAT_VISUALEXTRA
532/*
533 * Insert string "s" (b_insert ? before : after) block :AKelly
534 * Caller must prepare for undo.
535 */
536 static void
537block_insert(oap, s, b_insert, bdp)
538 oparg_T *oap;
539 char_u *s;
540 int b_insert;
541 struct block_def *bdp;
542{
543 int p_ts;
544 int count = 0; /* extra spaces to replace a cut TAB */
545 int spaces = 0; /* non-zero if cutting a TAB */
546 colnr_T offset; /* pointer along new line */
547 unsigned s_len; /* STRLEN(s) */
548 char_u *newp, *oldp; /* new, old lines */
549 linenr_T lnum; /* loop var */
550 int oldstate = State;
551
552 State = INSERT; /* don't want REPLACE for State */
553 s_len = (unsigned)STRLEN(s);
554
555 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
556 {
557 block_prep(oap, bdp, lnum, TRUE);
558 if (bdp->is_short && b_insert)
559 continue; /* OP_INSERT, line ends before block start */
560
561 oldp = ml_get(lnum);
562
563 if (b_insert)
564 {
565 p_ts = bdp->start_char_vcols;
566 spaces = bdp->startspaces;
567 if (spaces != 0)
568 count = p_ts - 1; /* we're cutting a TAB */
569 offset = bdp->textcol;
570 }
571 else /* append */
572 {
573 p_ts = bdp->end_char_vcols;
574 if (!bdp->is_short) /* spaces = padding after block */
575 {
576 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
577 if (spaces != 0)
578 count = p_ts - 1; /* we're cutting a TAB */
579 offset = bdp->textcol + bdp->textlen - (spaces != 0);
580 }
581 else /* spaces = padding to block edge */
582 {
583 /* if $ used, just append to EOL (ie spaces==0) */
584 if (!bdp->is_MAX)
585 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
586 count = spaces;
587 offset = bdp->textcol + bdp->textlen;
588 }
589 }
590
591 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
592 if (newp == NULL)
593 continue;
594
595 /* copy up to shifted part */
596 mch_memmove(newp, oldp, (size_t)(offset));
597 oldp += offset;
598
599 /* insert pre-padding */
600 copy_spaces(newp + offset, (size_t)spaces);
601
602 /* copy the new text */
603 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
604 offset += s_len;
605
606 if (spaces && !bdp->is_short)
607 {
608 /* insert post-padding */
609 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
610 /* We're splitting a TAB, don't copy it. */
611 oldp++;
612 /* We allowed for that TAB, remember this now */
613 count++;
614 }
615
616 if (spaces > 0)
617 offset += count;
618 mch_memmove(newp + offset, oldp, (size_t)(STRLEN(oldp) + 1));
619
620 ml_replace(lnum, newp, FALSE);
621
622 if (lnum == oap->end.lnum)
623 {
624 /* Set "']" mark to the end of the block instead of the end of
625 * the insert in the first line. */
626 curbuf->b_op_end.lnum = oap->end.lnum;
627 curbuf->b_op_end.col = offset;
628 }
629 } /* for all lnum */
630
631 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
632
633 State = oldstate;
634}
635#endif
636
637#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
638/*
639 * op_reindent - handle reindenting a block of lines.
640 */
641 void
642op_reindent(oap, how)
643 oparg_T *oap;
644 int (*how) __ARGS((void));
645{
646 long i;
647 char_u *l;
648 int count;
649 linenr_T first_changed = 0;
650 linenr_T last_changed = 0;
651 linenr_T start_lnum = curwin->w_cursor.lnum;
652
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000653 /* Don't even try when 'modifiable' is off. */
654 if (!curbuf->b_p_ma)
655 {
656 EMSG(_(e_modifiable));
657 return;
658 }
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 for (i = oap->line_count; --i >= 0 && !got_int; )
661 {
662 /* it's a slow thing to do, so give feedback so there's no worry that
663 * the computer's just hung. */
664
665 if (i > 1
666 && (i % 50 == 0 || i == oap->line_count - 1)
667 && oap->line_count > p_report)
668 smsg((char_u *)_("%ld lines to indent... "), i);
669
670 /*
671 * Be vi-compatible: For lisp indenting the first line is not
672 * indented, unless there is only one line.
673 */
674#ifdef FEAT_LISP
675 if (i != oap->line_count - 1 || oap->line_count == 1
676 || how != get_lisp_indent)
677#endif
678 {
679 l = skipwhite(ml_get_curline());
680 if (*l == NUL) /* empty or blank line */
681 count = 0;
682 else
683 count = how(); /* get the indent for this line */
684
685 if (set_indent(count, SIN_UNDO))
686 {
687 /* did change the indent, call changed_lines() later */
688 if (first_changed == 0)
689 first_changed = curwin->w_cursor.lnum;
690 last_changed = curwin->w_cursor.lnum;
691 }
692 }
693 ++curwin->w_cursor.lnum;
694 }
695
696 /* put cursor on first non-blank of indented line */
697 curwin->w_cursor.lnum = start_lnum;
698 beginline(BL_SOL | BL_FIX);
699
700 /* Mark changed lines so that they will be redrawn. When Visual
701 * highlighting was present, need to continue until the last line. When
702 * there is no change still need to remove the Visual highlighting. */
703 if (last_changed != 0)
704 changed_lines(first_changed, 0,
705#ifdef FEAT_VISUAL
706 oap->is_VIsual ? start_lnum + oap->line_count :
707#endif
708 last_changed + 1, 0L);
709#ifdef FEAT_VISUAL
710 else if (oap->is_VIsual)
711 redraw_curbuf_later(INVERTED);
712#endif
713
714 if (oap->line_count > p_report)
715 {
716 i = oap->line_count - (i + 1);
717 if (i == 1)
718 MSG(_("1 line indented "));
719 else
720 smsg((char_u *)_("%ld lines indented "), i);
721 }
722 /* set '[ and '] marks */
723 curbuf->b_op_start = oap->start;
724 curbuf->b_op_end = oap->end;
725}
726#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
727
728#if defined(FEAT_EVAL) || defined(PROTO)
729/*
730 * Keep the last expression line here, for repeating.
731 */
732static char_u *expr_line = NULL;
733
734/*
735 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
736 * Returns '=' when OK, NUL otherwise.
737 */
738 int
739get_expr_register()
740{
741 char_u *new_line;
742
743 new_line = getcmdline('=', 0L, 0);
744 if (new_line == NULL)
745 return NUL;
746 if (*new_line == NUL) /* use previous line */
747 vim_free(new_line);
748 else
749 set_expr_line(new_line);
750 return '=';
751}
752
753/*
754 * Set the expression for the '=' register.
755 * Argument must be an allocated string.
756 */
757 void
758set_expr_line(new_line)
759 char_u *new_line;
760{
761 vim_free(expr_line);
762 expr_line = new_line;
763}
764
765/*
766 * Get the result of the '=' register expression.
767 * Returns a pointer to allocated memory, or NULL for failure.
768 */
769 char_u *
770get_expr_line()
771{
772 char_u *expr_copy;
773 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000774 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
776 if (expr_line == NULL)
777 return NULL;
778
779 /* Make a copy of the expression, because evaluating it may cause it to be
780 * changed. */
781 expr_copy = vim_strsave(expr_line);
782 if (expr_copy == NULL)
783 return NULL;
784
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000785 /* When we are invoked recursively limit the evaluation to 10 levels.
786 * Then return the string as-is. */
787 if (nested >= 10)
788 return expr_copy;
789
790 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000791 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000792 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 vim_free(expr_copy);
794 return rv;
795}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000796
797/*
798 * Get the '=' register expression itself, without evaluating it.
799 */
800 char_u *
801get_expr_line_src()
802{
803 if (expr_line == NULL)
804 return NULL;
805 return vim_strsave(expr_line);
806}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#endif /* FEAT_EVAL */
808
809/*
810 * Check if 'regname' is a valid name of a yank register.
811 * Note: There is no check for 0 (default register), caller should do this
812 */
813 int
814valid_yank_reg(regname, writing)
815 int regname;
816 int writing; /* if TRUE check for writable registers */
817{
818 if ( (regname > 0 && ASCII_ISALNUM(regname))
819 || (!writing && vim_strchr((char_u *)
820#ifdef FEAT_EVAL
821 "/.%#:="
822#else
823 "/.%#:"
824#endif
825 , regname) != NULL)
826 || regname == '"'
827 || regname == '-'
828 || regname == '_'
829#ifdef FEAT_CLIPBOARD
830 || regname == '*'
831 || regname == '+'
832#endif
833#ifdef FEAT_DND
834 || (!writing && regname == '~')
835#endif
836 )
837 return TRUE;
838 return FALSE;
839}
840
841/*
842 * Set y_current and y_append, according to the value of "regname".
843 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000844 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *
846 * If regname is 0 and writing, use register 0
847 * If regname is 0 and reading, use previous register
848 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000849 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850get_yank_register(regname, writing)
851 int regname;
852 int writing;
853{
854 int i;
855
856 y_append = FALSE;
857 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
858 {
859 y_current = y_previous;
860 return;
861 }
862 i = regname;
863 if (VIM_ISDIGIT(i))
864 i -= '0';
865 else if (ASCII_ISLOWER(i))
866 i = CharOrdLow(i) + 10;
867 else if (ASCII_ISUPPER(i))
868 {
869 i = CharOrdUp(i) + 10;
870 y_append = TRUE;
871 }
872 else if (regname == '-')
873 i = DELETION_REGISTER;
874#ifdef FEAT_CLIPBOARD
875 /* When selection is not available, use register 0 instead of '*' */
876 else if (clip_star.available && regname == '*')
877 i = STAR_REGISTER;
878 /* When clipboard is not available, use register 0 instead of '+' */
879 else if (clip_plus.available && regname == '+')
880 i = PLUS_REGISTER;
881#endif
882#ifdef FEAT_DND
883 else if (!writing && regname == '~')
884 i = TILDE_REGISTER;
885#endif
886 else /* not 0-9, a-z, A-Z or '-': use register 0 */
887 i = 0;
888 y_current = &(y_regs[i]);
889 if (writing) /* remember the register we write into for do_put() */
890 y_previous = y_current;
891}
892
Bram Moolenaar8299df92004-07-10 09:47:34 +0000893#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894/*
895 * When "regname" is a clipboard register, obtain the selection. If it's not
896 * available return zero, otherwise return "regname".
897 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000898 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899may_get_selection(regname)
900 int regname;
901{
902 if (regname == '*')
903 {
904 if (!clip_star.available)
905 regname = 0;
906 else
907 clip_get_selection(&clip_star);
908 }
909 else if (regname == '+')
910 {
911 if (!clip_plus.available)
912 regname = 0;
913 else
914 clip_get_selection(&clip_plus);
915 }
916 return regname;
917}
918#endif
919
920#if defined(FEAT_VISUAL) || defined(PROTO)
921/*
922 * Obtain the contents of a "normal" register. The register is made empty.
923 * The returned pointer has allocated memory, use put_register() later.
924 */
925 void *
926get_register(name, copy)
927 int name;
928 int copy; /* make a copy, if FALSE make register empty. */
929{
930 static struct yankreg *reg;
931 int i;
932
933#ifdef FEAT_CLIPBOARD
934 /* When Visual area changed, may have to update selection. Obtain the
935 * selection too. */
936 if (name == '*' && clip_star.available && clip_isautosel())
937 {
938 clip_update_selection();
939 may_get_selection(name);
940 }
941#endif
942
943 get_yank_register(name, 0);
944 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
945 if (reg != NULL)
946 {
947 *reg = *y_current;
948 if (copy)
949 {
950 /* If we run out of memory some or all of the lines are empty. */
951 if (reg->y_size == 0)
952 reg->y_array = NULL;
953 else
954 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
955 * reg->y_size));
956 if (reg->y_array != NULL)
957 {
958 for (i = 0; i < reg->y_size; ++i)
959 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
960 }
961 }
962 else
963 y_current->y_array = NULL;
964 }
965 return (void *)reg;
966}
967
968/*
969 * Put "reg" into register "name". Free any previous contents.
970 */
971 void
972put_register(name, reg)
973 int name;
974 void *reg;
975{
976 get_yank_register(name, 0);
977 free_yank_all();
978 *y_current = *(struct yankreg *)reg;
979
980# ifdef FEAT_CLIPBOARD
981 /* Send text written to clipboard register to the clipboard. */
982 may_set_selection();
983# endif
984}
985#endif
986
987#if defined(FEAT_MOUSE) || defined(PROTO)
988/*
989 * return TRUE if the current yank register has type MLINE
990 */
991 int
992yank_register_mline(regname)
993 int regname;
994{
995 if (regname != 0 && !valid_yank_reg(regname, FALSE))
996 return FALSE;
997 if (regname == '_') /* black hole is always empty */
998 return FALSE;
999 get_yank_register(regname, FALSE);
1000 return (y_current->y_type == MLINE);
1001}
1002#endif
1003
1004/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001005 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001007 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 */
1009 int
1010do_record(c)
1011 int c;
1012{
Bram Moolenaard55de222007-05-06 13:38:48 +00001013 char_u *p;
1014 static int regname;
1015 struct yankreg *old_y_previous, *old_y_current;
1016 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 if (Recording == FALSE) /* start recording */
1019 {
1020 /* registers 0-9, a-z and " are allowed */
1021 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1022 retval = FAIL;
1023 else
1024 {
1025 Recording = TRUE;
1026 showmode();
1027 regname = c;
1028 retval = OK;
1029 }
1030 }
1031 else /* stop recording */
1032 {
1033 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001034 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1035 * needs to be removed again to put it in a register. exec_reg then
1036 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 */
1038 Recording = FALSE;
1039 MSG("");
1040 p = get_recorded();
1041 if (p == NULL)
1042 retval = FAIL;
1043 else
1044 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001045 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1046 vim_unescape_csi(p);
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 /*
1049 * We don't want to change the default register here, so save and
1050 * restore the current register name.
1051 */
1052 old_y_previous = y_previous;
1053 old_y_current = y_current;
1054
1055 retval = stuff_yank(regname, p);
1056
1057 y_previous = old_y_previous;
1058 y_current = old_y_current;
1059 }
1060 }
1061 return retval;
1062}
1063
1064/*
1065 * Stuff string "p" into yank register "regname" as a single line (append if
1066 * uppercase). "p" must have been alloced.
1067 *
1068 * return FAIL for failure, OK otherwise
1069 */
1070 static int
1071stuff_yank(regname, p)
1072 int regname;
1073 char_u *p;
1074{
1075 char_u *lp;
1076 char_u **pp;
1077
1078 /* check for read-only register */
1079 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1080 {
1081 vim_free(p);
1082 return FAIL;
1083 }
1084 if (regname == '_') /* black hole: don't do anything */
1085 {
1086 vim_free(p);
1087 return OK;
1088 }
1089 get_yank_register(regname, TRUE);
1090 if (y_append && y_current->y_array != NULL)
1091 {
1092 pp = &(y_current->y_array[y_current->y_size - 1]);
1093 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1094 if (lp == NULL)
1095 {
1096 vim_free(p);
1097 return FAIL;
1098 }
1099 STRCPY(lp, *pp);
1100 STRCAT(lp, p);
1101 vim_free(p);
1102 vim_free(*pp);
1103 *pp = lp;
1104 }
1105 else
1106 {
1107 free_yank_all();
1108 if ((y_current->y_array =
1109 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1110 {
1111 vim_free(p);
1112 return FAIL;
1113 }
1114 y_current->y_array[0] = p;
1115 y_current->y_size = 1;
1116 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1117 }
1118 return OK;
1119}
1120
1121/*
1122 * execute a yank register: copy it into the stuff buffer
1123 *
1124 * return FAIL for failure, OK otherwise
1125 */
1126 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001127do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 int regname;
1129 int colon; /* insert ':' before each line */
1130 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001131 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 static int lastc = NUL;
1134 long i;
1135 char_u *p;
1136 int retval = OK;
1137 int remap;
1138
1139 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001140 {
1141 if (lastc == NUL)
1142 {
1143 EMSG(_("E748: No previously used register"));
1144 return FAIL;
1145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 regname = lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /* check for valid regname */
1149 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001150 {
1151 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 lastc = regname;
1155
1156#ifdef FEAT_CLIPBOARD
1157 regname = may_get_selection(regname);
1158#endif
1159
1160 if (regname == '_') /* black hole: don't stuff anything */
1161 return OK;
1162
1163#ifdef FEAT_CMDHIST
1164 if (regname == ':') /* use last command line */
1165 {
1166 if (last_cmdline == NULL)
1167 {
1168 EMSG(_(e_nolastcmd));
1169 return FAIL;
1170 }
1171 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1172 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001173 /* Escape all control characters with a CTRL-V */
1174 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001175 (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 +00001176 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001177 {
1178 /* When in Visual mode "'<,'>" will be prepended to the command.
1179 * Remove it when it's already there. */
1180 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001181 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001182 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001183 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001184 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187#endif
1188#ifdef FEAT_EVAL
1189 else if (regname == '=')
1190 {
1191 p = get_expr_line();
1192 if (p == NULL)
1193 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001194 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 vim_free(p);
1196 }
1197#endif
1198 else if (regname == '.') /* use last inserted text */
1199 {
1200 p = get_last_insert_save();
1201 if (p == NULL)
1202 {
1203 EMSG(_(e_noinstext));
1204 return FAIL;
1205 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001206 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 vim_free(p);
1208 }
1209 else
1210 {
1211 get_yank_register(regname, FALSE);
1212 if (y_current->y_array == NULL)
1213 return FAIL;
1214
1215 /* Disallow remaping for ":@r". */
1216 remap = colon ? REMAP_NONE : REMAP_YES;
1217
1218 /*
1219 * Insert lines into typeahead buffer, from last one to first one.
1220 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001221 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 for (i = y_current->y_size; --i >= 0; )
1223 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001224 char_u *escaped;
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 /* insert NL between lines and after last line if type is MLINE */
1227 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1228 || addcr)
1229 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001230 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 return FAIL;
1232 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001233 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1234 if (escaped == NULL)
1235 return FAIL;
1236 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1237 vim_free(escaped);
1238 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001240 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 == FAIL)
1242 return FAIL;
1243 }
1244 Exec_reg = TRUE; /* disable the 'q' command */
1245 }
1246 return retval;
1247}
1248
1249/*
1250 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1251 * used only after other typeahead has been processed.
1252 */
1253 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001254put_reedit_in_typebuf(silent)
1255 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256{
1257 char_u buf[3];
1258
1259 if (restart_edit != NUL)
1260 {
1261 if (restart_edit == 'V')
1262 {
1263 buf[0] = 'g';
1264 buf[1] = 'R';
1265 buf[2] = NUL;
1266 }
1267 else
1268 {
1269 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1270 buf[1] = NUL;
1271 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001272 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 restart_edit = NUL;
1274 }
1275}
1276
1277 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001278put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 char_u *s;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001280 int esc; /* Escape CSI characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001282 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int retval = OK;
1285
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001286 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (colon)
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001288 retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001290 {
1291 char_u *p;
1292
1293 if (esc)
1294 p = vim_strsave_escape_csi(s);
1295 else
1296 p = s;
1297 if (p == NULL)
1298 retval = FAIL;
1299 else
1300 retval = ins_typebuf(p, REMAP_YES, 0, TRUE, silent);
1301 if (esc)
1302 vim_free(p);
1303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (colon && retval == OK)
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001305 retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 return retval;
1307}
1308
1309/*
1310 * Insert a yank register: copy it into the Read buffer.
1311 * Used by CTRL-R command and middle mouse button in insert mode.
1312 *
1313 * return FAIL for failure, OK otherwise
1314 */
1315 int
1316insert_reg(regname, literally)
1317 int regname;
1318 int literally; /* insert literally, not as if typed */
1319{
1320 long i;
1321 int retval = OK;
1322 char_u *arg;
1323 int allocated;
1324
1325 /*
1326 * It is possible to get into an endless loop by having CTRL-R a in
1327 * register a and then, in insert mode, doing CTRL-R a.
1328 * If you hit CTRL-C, the loop will be broken here.
1329 */
1330 ui_breakcheck();
1331 if (got_int)
1332 return FAIL;
1333
1334 /* check for valid regname */
1335 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1336 return FAIL;
1337
1338#ifdef FEAT_CLIPBOARD
1339 regname = may_get_selection(regname);
1340#endif
1341
1342 if (regname == '.') /* insert last inserted text */
1343 retval = stuff_inserted(NUL, 1L, TRUE);
1344 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1345 {
1346 if (arg == NULL)
1347 return FAIL;
1348 stuffescaped(arg, literally);
1349 if (allocated)
1350 vim_free(arg);
1351 }
1352 else /* name or number register */
1353 {
1354 get_yank_register(regname, FALSE);
1355 if (y_current->y_array == NULL)
1356 retval = FAIL;
1357 else
1358 {
1359 for (i = 0; i < y_current->y_size; ++i)
1360 {
1361 stuffescaped(y_current->y_array[i], literally);
1362 /*
1363 * Insert a newline between lines and after last line if
1364 * y_type is MLINE.
1365 */
1366 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1367 stuffcharReadbuff('\n');
1368 }
1369 }
1370 }
1371
1372 return retval;
1373}
1374
1375/*
1376 * Stuff a string into the typeahead buffer, such that edit() will insert it
1377 * literally ("literally" TRUE) or interpret is as typed characters.
1378 */
1379 static void
1380stuffescaped(arg, literally)
1381 char_u *arg;
1382 int literally;
1383{
1384 int c;
1385 char_u *start;
1386
1387 while (*arg != NUL)
1388 {
1389 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1390 * stuff K_SPECIAL to get the effect of a special key when "literally"
1391 * is TRUE. */
1392 start = arg;
1393 while ((*arg >= ' '
1394#ifndef EBCDIC
1395 && *arg < DEL /* EBCDIC: chars above space are normal */
1396#endif
1397 )
1398 || (*arg == K_SPECIAL && !literally))
1399 ++arg;
1400 if (arg > start)
1401 stuffReadbuffLen(start, (long)(arg - start));
1402
1403 /* stuff a single special character */
1404 if (*arg != NUL)
1405 {
1406#ifdef FEAT_MBYTE
1407 if (has_mbyte)
1408 c = mb_ptr2char_adv(&arg);
1409 else
1410#endif
1411 c = *arg++;
1412 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1413 stuffcharReadbuff(Ctrl_V);
1414 stuffcharReadbuff(c);
1415 }
1416 }
1417}
1418
1419/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001420 * If "regname" is a special register, return TRUE and store a pointer to its
1421 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001423 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424get_spec_reg(regname, argp, allocated, errmsg)
1425 int regname;
1426 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001427 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 int errmsg; /* give error message when failing */
1429{
1430 int cnt;
1431
1432 *argp = NULL;
1433 *allocated = FALSE;
1434 switch (regname)
1435 {
1436 case '%': /* file name */
1437 if (errmsg)
1438 check_fname(); /* will give emsg if not set */
1439 *argp = curbuf->b_fname;
1440 return TRUE;
1441
1442 case '#': /* alternate file name */
1443 *argp = getaltfname(errmsg); /* may give emsg if not set */
1444 return TRUE;
1445
1446#ifdef FEAT_EVAL
1447 case '=': /* result of expression */
1448 *argp = get_expr_line();
1449 *allocated = TRUE;
1450 return TRUE;
1451#endif
1452
1453 case ':': /* last command line */
1454 if (last_cmdline == NULL && errmsg)
1455 EMSG(_(e_nolastcmd));
1456 *argp = last_cmdline;
1457 return TRUE;
1458
1459 case '/': /* last search-pattern */
1460 if (last_search_pat() == NULL && errmsg)
1461 EMSG(_(e_noprevre));
1462 *argp = last_search_pat();
1463 return TRUE;
1464
1465 case '.': /* last inserted text */
1466 *argp = get_last_insert_save();
1467 *allocated = TRUE;
1468 if (*argp == NULL && errmsg)
1469 EMSG(_(e_noinstext));
1470 return TRUE;
1471
1472#ifdef FEAT_SEARCHPATH
1473 case Ctrl_F: /* Filename under cursor */
1474 case Ctrl_P: /* Path under cursor, expand via "path" */
1475 if (!errmsg)
1476 return FALSE;
1477 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001478 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 *allocated = TRUE;
1480 return TRUE;
1481#endif
1482
1483 case Ctrl_W: /* word under cursor */
1484 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1485 if (!errmsg)
1486 return FALSE;
1487 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1488 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1489 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1490 *allocated = TRUE;
1491 return TRUE;
1492
1493 case '_': /* black hole: always empty */
1494 *argp = (char_u *)"";
1495 return TRUE;
1496 }
1497
1498 return FALSE;
1499}
1500
1501/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001502 * Paste a yank register into the command line.
1503 * Only for non-special registers.
1504 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 * insert_reg() can't be used here, because special characters from the
1506 * register contents will be interpreted as commands.
1507 *
1508 * return FAIL for failure, OK otherwise
1509 */
1510 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001511cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int regname;
1513 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001514 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
1518 get_yank_register(regname, FALSE);
1519 if (y_current->y_array == NULL)
1520 return FAIL;
1521
1522 for (i = 0; i < y_current->y_size; ++i)
1523 {
1524 cmdline_paste_str(y_current->y_array[i], literally);
1525
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001526 /* Insert ^M between lines and after last line if type is MLINE.
1527 * Don't do this when "remcr" is TRUE and the next line is empty. */
1528 if (y_current->y_type == MLINE
1529 || (i < y_current->y_size - 1
1530 && !(remcr
1531 && i == y_current->y_size - 2
1532 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 cmdline_paste_str((char_u *)"\r", literally);
1534
1535 /* Check for CTRL-C, in case someone tries to paste a few thousand
1536 * lines and gets bored. */
1537 ui_breakcheck();
1538 if (got_int)
1539 return FAIL;
1540 }
1541 return OK;
1542}
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1545/*
1546 * Adjust the register name pointed to with "rp" for the clipboard being
1547 * used always and the clipboard being available.
1548 */
1549 void
1550adjust_clip_reg(rp)
1551 int *rp;
1552{
1553 /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1554 if (*rp == 0 && clip_unnamed)
1555 *rp = '*';
1556 if (!clip_star.available && *rp == '*')
1557 *rp = 0;
1558 if (!clip_plus.available && *rp == '+')
1559 *rp = 0;
1560}
1561#endif
1562
1563/*
1564 * op_delete - handle a delete operation
1565 *
1566 * return FAIL if undo failed, OK otherwise.
1567 */
1568 int
1569op_delete(oap)
1570 oparg_T *oap;
1571{
1572 int n;
1573 linenr_T lnum;
1574 char_u *ptr;
1575#ifdef FEAT_VISUAL
1576 char_u *newp, *oldp;
1577 struct block_def bd;
1578#endif
1579 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1580 int did_yank = FALSE;
1581
1582 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1583 return OK;
1584
1585 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1586 if (oap->empty)
1587 return u_save_cursor();
1588
1589 if (!curbuf->b_p_ma)
1590 {
1591 EMSG(_(e_modifiable));
1592 return FAIL;
1593 }
1594
1595#ifdef FEAT_CLIPBOARD
1596 adjust_clip_reg(&oap->regname);
1597#endif
1598
1599#ifdef FEAT_MBYTE
1600 if (has_mbyte)
1601 mb_adjust_opend(oap);
1602#endif
1603
1604/*
1605 * Imitate the strange Vi behaviour: If the delete spans more than one line
1606 * and motion_type == MCHAR and the result is a blank line, make the delete
1607 * linewise. Don't do this for the change command or Visual mode.
1608 */
1609 if ( oap->motion_type == MCHAR
1610#ifdef FEAT_VISUAL
1611 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001612 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#endif
1614 && oap->line_count > 1
1615 && oap->op_type == OP_DELETE)
1616 {
1617 ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive;
1618 ptr = skipwhite(ptr);
1619 if (*ptr == NUL && inindent(0))
1620 oap->motion_type = MLINE;
1621 }
1622
1623/*
1624 * Check for trying to delete (e.g. "D") in an empty line.
1625 * Note: For the change operator it is ok.
1626 */
1627 if ( oap->motion_type == MCHAR
1628 && oap->line_count == 1
1629 && oap->op_type == OP_DELETE
1630 && *ml_get(oap->start.lnum) == NUL)
1631 {
1632 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001633 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 * 'cpoptions' (Vi compatible).
1635 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001636#ifdef FEAT_VIRTUALEDIT
1637 if (virtual_op)
1638 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1639 * marks as if it happened. */
1640 goto setmarks;
1641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1643 beep_flush();
1644 return OK;
1645 }
1646
1647/*
1648 * Do a yank of whatever we're about to delete.
1649 * If a yank register was specified, put the deleted text into that register.
1650 * For the black hole register '_' don't yank anything.
1651 */
1652 if (oap->regname != '_')
1653 {
1654 if (oap->regname != 0)
1655 {
1656 /* check for read-only register */
1657 if (!valid_yank_reg(oap->regname, TRUE))
1658 {
1659 beep_flush();
1660 return OK;
1661 }
1662 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1663 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1664 did_yank = TRUE;
1665 }
1666
1667 /*
1668 * Put deleted text into register 1 and shift number registers if the
1669 * delete contains a line break, or when a regname has been specified.
1670 */
1671 if (oap->regname != 0 || oap->motion_type == MLINE
1672 || oap->line_count > 1 || oap->use_reg_one)
1673 {
1674 y_current = &y_regs[9];
1675 free_yank_all(); /* free register nine */
1676 for (n = 9; n > 1; --n)
1677 y_regs[n] = y_regs[n - 1];
1678 y_previous = y_current = &y_regs[1];
1679 y_regs[1].y_array = NULL; /* set register one to empty */
1680 if (op_yank(oap, TRUE, FALSE) == OK)
1681 did_yank = TRUE;
1682 }
1683
1684 /* Yank into small delete register when no register specified and the
1685 * delete is within one line. */
1686 if (oap->regname == 0 && oap->motion_type != MLINE
1687 && oap->line_count == 1)
1688 {
1689 oap->regname = '-';
1690 get_yank_register(oap->regname, TRUE);
1691 if (op_yank(oap, TRUE, FALSE) == OK)
1692 did_yank = TRUE;
1693 oap->regname = 0;
1694 }
1695
1696 /*
1697 * If there's too much stuff to fit in the yank register, then get a
1698 * confirmation before doing the delete. This is crude, but simple.
1699 * And it avoids doing a delete of something we can't put back if we
1700 * want.
1701 */
1702 if (!did_yank)
1703 {
1704 int msg_silent_save = msg_silent;
1705
1706 msg_silent = 0; /* must display the prompt */
1707 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1708 msg_silent = msg_silent_save;
1709 if (n != 'y')
1710 {
1711 EMSG(_(e_abort));
1712 return FAIL;
1713 }
1714 }
1715 }
1716
1717#ifdef FEAT_VISUAL
1718/*
1719 * block mode delete
1720 */
1721 if (oap->block_mode)
1722 {
1723 if (u_save((linenr_T)(oap->start.lnum - 1),
1724 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1725 return FAIL;
1726
1727 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1728 {
1729 block_prep(oap, &bd, lnum, TRUE);
1730 if (bd.textlen == 0) /* nothing to delete */
1731 continue;
1732
1733 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1734 if (lnum == curwin->w_cursor.lnum)
1735 {
1736 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1737# ifdef FEAT_VIRTUALEDIT
1738 curwin->w_cursor.coladd = 0;
1739# endif
1740 }
1741
1742 /* n == number of chars deleted
1743 * If we delete a TAB, it may be replaced by several characters.
1744 * Thus the number of characters may increase!
1745 */
1746 n = bd.textlen - bd.startspaces - bd.endspaces;
1747 oldp = ml_get(lnum);
1748 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1749 if (newp == NULL)
1750 continue;
1751 /* copy up to deleted part */
1752 mch_memmove(newp, oldp, (size_t)bd.textcol);
1753 /* insert spaces */
1754 copy_spaces(newp + bd.textcol,
1755 (size_t)(bd.startspaces + bd.endspaces));
1756 /* copy the part after the deleted part */
1757 oldp += bd.textcol + bd.textlen;
1758 mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
1759 oldp, STRLEN(oldp) + 1);
1760 /* replace the line */
1761 ml_replace(lnum, newp, FALSE);
1762 }
1763
1764 check_cursor_col();
1765 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1766 oap->end.lnum + 1, 0L);
1767 oap->line_count = 0; /* no lines deleted */
1768 }
1769 else
1770#endif
1771 if (oap->motion_type == MLINE)
1772 {
1773 if (oap->op_type == OP_CHANGE)
1774 {
1775 /* Delete the lines except the first one. Temporarily move the
1776 * cursor to the next line. Save the current line number, if the
1777 * last line is deleted it may be changed.
1778 */
1779 if (oap->line_count > 1)
1780 {
1781 lnum = curwin->w_cursor.lnum;
1782 ++curwin->w_cursor.lnum;
1783 del_lines((long)(oap->line_count - 1), TRUE);
1784 curwin->w_cursor.lnum = lnum;
1785 }
1786 if (u_save_cursor() == FAIL)
1787 return FAIL;
1788 if (curbuf->b_p_ai) /* don't delete indent */
1789 {
1790 beginline(BL_WHITE); /* cursor on first non-white */
1791 did_ai = TRUE; /* delete the indent when ESC hit */
1792 ai_col = curwin->w_cursor.col;
1793 }
1794 else
1795 beginline(0); /* cursor in column 0 */
1796 truncate_line(FALSE); /* delete the rest of the line */
1797 /* leave cursor past last char in line */
1798 if (oap->line_count > 1)
1799 u_clearline(); /* "U" command not possible after "2cc" */
1800 }
1801 else
1802 {
1803 del_lines(oap->line_count, TRUE);
1804 beginline(BL_WHITE | BL_FIX);
1805 u_clearline(); /* "U" command not possible after "dd" */
1806 }
1807 }
1808 else
1809 {
1810#ifdef FEAT_VIRTUALEDIT
1811 if (virtual_op)
1812 {
1813 int endcol = 0;
1814
1815 /* For virtualedit: break the tabs that are partly included. */
1816 if (gchar_pos(&oap->start) == '\t')
1817 {
1818 if (u_save_cursor() == FAIL) /* save first line for undo */
1819 return FAIL;
1820 if (oap->line_count == 1)
1821 endcol = getviscol2(oap->end.col, oap->end.coladd);
1822 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1823 oap->start = curwin->w_cursor;
1824 if (oap->line_count == 1)
1825 {
1826 coladvance(endcol);
1827 oap->end.col = curwin->w_cursor.col;
1828 oap->end.coladd = curwin->w_cursor.coladd;
1829 curwin->w_cursor = oap->start;
1830 }
1831 }
1832
1833 /* Break a tab only when it's included in the area. */
1834 if (gchar_pos(&oap->end) == '\t'
1835 && (int)oap->end.coladd < oap->inclusive)
1836 {
1837 /* save last line for undo */
1838 if (u_save((linenr_T)(oap->end.lnum - 1),
1839 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1840 return FAIL;
1841 curwin->w_cursor = oap->end;
1842 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1843 oap->end = curwin->w_cursor;
1844 curwin->w_cursor = oap->start;
1845 }
1846 }
1847#endif
1848
1849 if (oap->line_count == 1) /* delete characters within one line */
1850 {
1851 if (u_save_cursor() == FAIL) /* save line for undo */
1852 return FAIL;
1853
1854 /* if 'cpoptions' contains '$', display '$' at end of change */
1855 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1856 && oap->op_type == OP_CHANGE
1857 && oap->end.lnum == curwin->w_cursor.lnum
1858#ifdef FEAT_VISUAL
1859 && !oap->is_VIsual
1860#endif
1861 )
1862 display_dollar(oap->end.col - !oap->inclusive);
1863
1864 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1865
1866#ifdef FEAT_VIRTUALEDIT
1867 if (virtual_op)
1868 {
1869 /* fix up things for virtualedit-delete:
1870 * break the tabs which are going to get in our way
1871 */
1872 char_u *curline = ml_get_curline();
1873 int len = (int)STRLEN(curline);
1874
1875 if (oap->end.coladd != 0
1876 && (int)oap->end.col >= len - 1
1877 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1878 n++;
1879 /* Delete at least one char (e.g, when on a control char). */
1880 if (n == 0 && oap->start.coladd != oap->end.coladd)
1881 n = 1;
1882
1883 /* When deleted a char in the line, reset coladd. */
1884 if (gchar_cursor() != NUL)
1885 curwin->w_cursor.coladd = 0;
1886 }
1887#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +00001888 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001889#ifdef FEAT_VISUAL
1890 && !oap->is_VIsual
1891#endif
1892 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
1894 else /* delete characters between lines */
1895 {
1896 pos_T curpos;
1897
1898 /* save deleted and changed lines for undo */
1899 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1900 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1901 return FAIL;
1902
1903 truncate_line(TRUE); /* delete from cursor to end of line */
1904
1905 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1906 ++curwin->w_cursor.lnum;
1907 del_lines((long)(oap->line_count - 2), FALSE);
1908
1909 /* delete from start of line until op_end */
1910 curwin->w_cursor.col = 0;
1911 (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
Bram Moolenaarca003e12006-03-17 23:19:38 +00001912 !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001913#ifdef FEAT_VISUAL
1914 && !oap->is_VIsual
1915#endif
1916 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1918
1919 (void)do_join(FALSE);
1920 }
1921 }
1922
1923 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1924
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001925#ifdef FEAT_VIRTUALEDIT
1926setmarks:
1927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#ifdef FEAT_VISUAL
1929 if (oap->block_mode)
1930 {
1931 curbuf->b_op_end.lnum = oap->end.lnum;
1932 curbuf->b_op_end.col = oap->start.col;
1933 }
1934 else
1935#endif
1936 curbuf->b_op_end = oap->start;
1937 curbuf->b_op_start = oap->start;
1938
1939 return OK;
1940}
1941
1942#ifdef FEAT_MBYTE
1943/*
1944 * Adjust end of operating area for ending on a multi-byte character.
1945 * Used for deletion.
1946 */
1947 static void
1948mb_adjust_opend(oap)
1949 oparg_T *oap;
1950{
1951 char_u *p;
1952
1953 if (oap->inclusive)
1954 {
1955 p = ml_get(oap->end.lnum);
1956 oap->end.col += mb_tail_off(p, p + oap->end.col);
1957 }
1958}
1959#endif
1960
1961#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1962/*
1963 * Replace a whole area with one character.
1964 */
1965 int
1966op_replace(oap, c)
1967 oparg_T *oap;
1968 int c;
1969{
1970 int n, numc;
1971#ifdef FEAT_MBYTE
1972 int num_chars;
1973#endif
1974 char_u *newp, *oldp;
1975 size_t oldlen;
1976 struct block_def bd;
1977
1978 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1979 return OK; /* nothing to do */
1980
1981#ifdef FEAT_MBYTE
1982 if (has_mbyte)
1983 mb_adjust_opend(oap);
1984#endif
1985
1986 if (u_save((linenr_T)(oap->start.lnum - 1),
1987 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1988 return FAIL;
1989
1990 /*
1991 * block mode replace
1992 */
1993 if (oap->block_mode)
1994 {
1995 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1996 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1997 {
1998 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1999 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2000 continue; /* nothing to replace */
2001
2002 /* n == number of extra chars required
2003 * If we split a TAB, it may be replaced by several characters.
2004 * Thus the number of characters may increase!
2005 */
2006#ifdef FEAT_VIRTUALEDIT
2007 /* If the range starts in virtual space, count the initial
2008 * coladd offset as part of "startspaces" */
2009 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2010 {
2011 pos_T vpos;
2012
2013 getvpos(&vpos, oap->start_vcol);
2014 bd.startspaces += vpos.coladd;
2015 n = bd.startspaces;
2016 }
2017 else
2018#endif
2019 /* allow for pre spaces */
2020 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2021
2022 /* allow for post spp */
2023 n += (bd.endspaces
2024#ifdef FEAT_VIRTUALEDIT
2025 && !bd.is_oneChar
2026#endif
2027 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2028 /* Figure out how many characters to replace. */
2029 numc = oap->end_vcol - oap->start_vcol + 1;
2030 if (bd.is_short && (!virtual_op || bd.is_MAX))
2031 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2032
2033#ifdef FEAT_MBYTE
2034 /* A double-wide character can be replaced only up to half the
2035 * times. */
2036 if ((*mb_char2cells)(c) > 1)
2037 {
2038 if ((numc & 1) && !bd.is_short)
2039 {
2040 ++bd.endspaces;
2041 ++n;
2042 }
2043 numc = numc / 2;
2044 }
2045
2046 /* Compute bytes needed, move character count to num_chars. */
2047 num_chars = numc;
2048 numc *= (*mb_char2len)(c);
2049#endif
2050 /* oldlen includes textlen, so don't double count */
2051 n += numc - bd.textlen;
2052
2053 oldp = ml_get_curline();
2054 oldlen = STRLEN(oldp);
2055 newp = alloc_check((unsigned)oldlen + 1 + n);
2056 if (newp == NULL)
2057 continue;
2058 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2059 /* copy up to deleted part */
2060 mch_memmove(newp, oldp, (size_t)bd.textcol);
2061 oldp += bd.textcol + bd.textlen;
2062 /* insert pre-spaces */
2063 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2064 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2065#ifdef FEAT_MBYTE
2066 if (has_mbyte)
2067 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002068 n = (int)STRLEN(newp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 while (--num_chars >= 0)
2070 n += (*mb_char2bytes)(c, newp + n);
2071 }
2072 else
2073#endif
2074 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2075 if (!bd.is_short)
2076 {
2077 /* insert post-spaces */
2078 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2079 /* copy the part after the changed part */
2080 mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1);
2081 }
2082 /* replace the line */
2083 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2084 }
2085 }
2086 else
2087 {
2088 /*
2089 * MCHAR and MLINE motion replace.
2090 */
2091 if (oap->motion_type == MLINE)
2092 {
2093 oap->start.col = 0;
2094 curwin->w_cursor.col = 0;
2095 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2096 if (oap->end.col)
2097 --oap->end.col;
2098 }
2099 else if (!oap->inclusive)
2100 dec(&(oap->end));
2101
2102 while (ltoreq(curwin->w_cursor, oap->end))
2103 {
2104 n = gchar_cursor();
2105 if (n != NUL)
2106 {
2107#ifdef FEAT_MBYTE
2108 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2109 {
2110 /* This is slow, but it handles replacing a single-byte
2111 * with a multi-byte and the other way around. */
2112 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2113 n = State;
2114 State = REPLACE;
2115 ins_char(c);
2116 State = n;
2117 /* Backup to the replaced character. */
2118 dec_cursor();
2119 }
2120 else
2121#endif
2122 {
2123#ifdef FEAT_VIRTUALEDIT
2124 if (n == TAB)
2125 {
2126 int end_vcol = 0;
2127
2128 if (curwin->w_cursor.lnum == oap->end.lnum)
2129 {
2130 /* oap->end has to be recalculated when
2131 * the tab breaks */
2132 end_vcol = getviscol2(oap->end.col,
2133 oap->end.coladd);
2134 }
2135 coladvance_force(getviscol());
2136 if (curwin->w_cursor.lnum == oap->end.lnum)
2137 getvpos(&oap->end, end_vcol);
2138 }
2139#endif
2140 pchar(curwin->w_cursor, c);
2141 }
2142 }
2143#ifdef FEAT_VIRTUALEDIT
2144 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2145 {
2146 int virtcols = oap->end.coladd;
2147
2148 if (curwin->w_cursor.lnum == oap->start.lnum
2149 && oap->start.col == oap->end.col && oap->start.coladd)
2150 virtcols -= oap->start.coladd;
2151
2152 /* oap->end has been trimmed so it's effectively inclusive;
2153 * as a result an extra +1 must be counted so we don't
2154 * trample the NUL byte. */
2155 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2156 curwin->w_cursor.col -= (virtcols + 1);
2157 for (; virtcols >= 0; virtcols--)
2158 {
2159 pchar(curwin->w_cursor, c);
2160 if (inc(&curwin->w_cursor) == -1)
2161 break;
2162 }
2163 }
2164#endif
2165
2166 /* Advance to next character, stop at the end of the file. */
2167 if (inc_cursor() == -1)
2168 break;
2169 }
2170 }
2171
2172 curwin->w_cursor = oap->start;
2173 check_cursor();
2174 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2175
2176 /* Set "'[" and "']" marks. */
2177 curbuf->b_op_start = oap->start;
2178 curbuf->b_op_end = oap->end;
2179
2180 return OK;
2181}
2182#endif
2183
2184/*
2185 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2186 */
2187 void
2188op_tilde(oap)
2189 oparg_T *oap;
2190{
2191 pos_T pos;
2192#ifdef FEAT_VISUAL
2193 struct block_def bd;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002194 int todo;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 int did_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198 if (u_save((linenr_T)(oap->start.lnum - 1),
2199 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2200 return;
2201
2202 pos = oap->start;
2203#ifdef FEAT_VISUAL
2204 if (oap->block_mode) /* Visual block mode */
2205 {
2206 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2207 {
2208 block_prep(oap, &bd, pos.lnum, FALSE);
2209 pos.col = bd.textcol;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002210 for (todo = bd.textlen; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002212# ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002213 if (has_mbyte)
2214 todo -= (*mb_ptr2len)(ml_get_pos(&pos)) - 1;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 did_change |= swapchar(oap->op_type, &pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (inc(&pos) == -1) /* at end of file */
2218 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 }
2220# ifdef FEAT_NETBEANS_INTG
2221 if (usingNetbeans && did_change)
2222 {
2223 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2224
Bram Moolenaar009b2592004-10-24 19:18:58 +00002225 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2226 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002228 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230# endif
2231 }
2232 if (did_change)
2233 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2234 }
2235 else /* not block mode */
2236#endif
2237 {
2238 if (oap->motion_type == MLINE)
2239 {
2240 oap->start.col = 0;
2241 pos.col = 0;
2242 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2243 if (oap->end.col)
2244 --oap->end.col;
2245 }
2246 else if (!oap->inclusive)
2247 dec(&(oap->end));
2248
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002249 while (ltoreq(pos, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
2251 did_change |= swapchar(oap->op_type, &pos);
2252 if (inc(&pos) == -1) /* at end of file */
2253 break;
2254 }
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (did_change)
2257 {
2258 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2259 0L);
2260#ifdef FEAT_NETBEANS_INTG
2261 if (usingNetbeans && did_change)
2262 {
2263 char_u *ptr;
2264 int count;
2265
2266 pos = oap->start;
2267 while (pos.lnum < oap->end.lnum)
2268 {
2269 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002270 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002271 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002273 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 pos.col = 0;
2275 pos.lnum++;
2276 }
2277 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2278 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002279 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002281 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283#endif
2284 }
2285 }
2286
2287#ifdef FEAT_VISUAL
2288 if (!did_change && oap->is_VIsual)
2289 /* No change: need to remove the Visual selection */
2290 redraw_curbuf_later(INVERTED);
2291#endif
2292
2293 /*
2294 * Set '[ and '] marks.
2295 */
2296 curbuf->b_op_start = oap->start;
2297 curbuf->b_op_end = oap->end;
2298
2299 if (oap->line_count > p_report)
2300 {
2301 if (oap->line_count == 1)
2302 MSG(_("1 line changed"));
2303 else
2304 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2305 }
2306}
2307
2308/*
2309 * If op_type == OP_UPPER: make uppercase,
2310 * if op_type == OP_LOWER: make lowercase,
2311 * if op_type == OP_ROT13: do rot13 encoding,
2312 * else swap case of character at 'pos'
2313 * returns TRUE when something actually changed.
2314 */
2315 int
2316swapchar(op_type, pos)
2317 int op_type;
2318 pos_T *pos;
2319{
2320 int c;
2321 int nc;
2322
2323 c = gchar_pos(pos);
2324
2325 /* Only do rot13 encoding for ASCII characters. */
2326 if (c >= 0x80 && op_type == OP_ROT13)
2327 return FALSE;
2328
2329#ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002330 if (op_type == OP_UPPER && enc_latin1like && c == 0xdf)
2331 {
2332 pos_T sp = curwin->w_cursor;
2333
2334 /* Special handling of German sharp s: change to "SS". */
2335 curwin->w_cursor = *pos;
2336 del_char(FALSE);
2337 ins_char('S');
2338 ins_char('S');
2339 curwin->w_cursor = sp;
2340 inc(pos);
2341 }
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2344 return FALSE;
2345#endif
2346 nc = c;
2347 if (MB_ISLOWER(c))
2348 {
2349 if (op_type == OP_ROT13)
2350 nc = ROT13(c, 'a');
2351 else if (op_type != OP_LOWER)
2352 nc = MB_TOUPPER(c);
2353 }
2354 else if (MB_ISUPPER(c))
2355 {
2356 if (op_type == OP_ROT13)
2357 nc = ROT13(c, 'A');
2358 else if (op_type != OP_UPPER)
2359 nc = MB_TOLOWER(c);
2360 }
2361 if (nc != c)
2362 {
2363#ifdef FEAT_MBYTE
2364 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2365 {
2366 pos_T sp = curwin->w_cursor;
2367
2368 curwin->w_cursor = *pos;
2369 del_char(FALSE);
2370 ins_char(nc);
2371 curwin->w_cursor = sp;
2372 }
2373 else
2374#endif
2375 pchar(*pos, nc);
2376 return TRUE;
2377 }
2378 return FALSE;
2379}
2380
2381#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2382/*
2383 * op_insert - Insert and append operators for Visual mode.
2384 */
2385 void
2386op_insert(oap, count1)
2387 oparg_T *oap;
2388 long count1;
2389{
2390 long ins_len, pre_textlen = 0;
2391 char_u *firstline, *ins_text;
2392 struct block_def bd;
2393 int i;
2394
2395 /* edit() changes this - record it for OP_APPEND */
2396 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2397
2398 /* vis block is still marked. Get rid of it now. */
2399 curwin->w_cursor.lnum = oap->start.lnum;
2400 update_screen(INVERTED);
2401
2402 if (oap->block_mode)
2403 {
2404#ifdef FEAT_VIRTUALEDIT
2405 /* When 'virtualedit' is used, need to insert the extra spaces before
2406 * doing block_prep(). When only "block" is used, virtual edit is
2407 * already disabled, but still need it when calling
2408 * coladvance_force(). */
2409 if (curwin->w_cursor.coladd > 0)
2410 {
2411 int old_ve_flags = ve_flags;
2412
2413 ve_flags = VE_ALL;
2414 if (u_save_cursor() == FAIL)
2415 return;
2416 coladvance_force(oap->op_type == OP_APPEND
2417 ? oap->end_vcol + 1 : getviscol());
2418 if (oap->op_type == OP_APPEND)
2419 --curwin->w_cursor.col;
2420 ve_flags = old_ve_flags;
2421 }
2422#endif
2423 /* Get the info about the block before entering the text */
2424 block_prep(oap, &bd, oap->start.lnum, TRUE);
2425 firstline = ml_get(oap->start.lnum) + bd.textcol;
2426 if (oap->op_type == OP_APPEND)
2427 firstline += bd.textlen;
2428 pre_textlen = (long)STRLEN(firstline);
2429 }
2430
2431 if (oap->op_type == OP_APPEND)
2432 {
2433 if (oap->block_mode
2434#ifdef FEAT_VIRTUALEDIT
2435 && curwin->w_cursor.coladd == 0
2436#endif
2437 )
2438 {
2439 /* Move the cursor to the character right of the block. */
2440 curwin->w_set_curswant = TRUE;
2441 while (*ml_get_cursor() != NUL
2442 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2443 ++curwin->w_cursor.col;
2444 if (bd.is_short && !bd.is_MAX)
2445 {
2446 /* First line was too short, make it longer and adjust the
2447 * values in "bd". */
2448 if (u_save_cursor() == FAIL)
2449 return;
2450 for (i = 0; i < bd.endspaces; ++i)
2451 ins_char(' ');
2452 bd.textlen += bd.endspaces;
2453 }
2454 }
2455 else
2456 {
2457 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002458 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 /* Works just like an 'i'nsert on the next character. */
2461 if (!lineempty(curwin->w_cursor.lnum)
2462 && oap->start_vcol != oap->end_vcol)
2463 inc_cursor();
2464 }
2465 }
2466
2467 edit(NUL, FALSE, (linenr_T)count1);
2468
2469 /* if user has moved off this line, we don't know what to do, so do
2470 * nothing */
2471 if (curwin->w_cursor.lnum != oap->start.lnum)
2472 return;
2473
2474 if (oap->block_mode)
2475 {
2476 struct block_def bd2;
2477
2478 /*
2479 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002480 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 * Don't do this when "$" used, end-of-line will have changed.
2482 */
2483 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2484 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2485 {
2486 if (oap->op_type == OP_APPEND)
2487 {
2488 pre_textlen += bd2.textlen - bd.textlen;
2489 if (bd2.endspaces)
2490 --bd2.textlen;
2491 }
2492 bd.textcol = bd2.textcol;
2493 bd.textlen = bd2.textlen;
2494 }
2495
2496 /*
2497 * Subsequent calls to ml_get() flush the firstline data - take a
2498 * copy of the required string.
2499 */
2500 firstline = ml_get(oap->start.lnum) + bd.textcol;
2501 if (oap->op_type == OP_APPEND)
2502 firstline += bd.textlen;
2503 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2504 {
2505 ins_text = vim_strnsave(firstline, (int)ins_len);
2506 if (ins_text != NULL)
2507 {
2508 /* block handled here */
2509 if (u_save(oap->start.lnum,
2510 (linenr_T)(oap->end.lnum + 1)) == OK)
2511 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2512 &bd);
2513
2514 curwin->w_cursor.col = oap->start.col;
2515 check_cursor();
2516 vim_free(ins_text);
2517 }
2518 }
2519 }
2520}
2521#endif
2522
2523/*
2524 * op_change - handle a change operation
2525 *
2526 * return TRUE if edit() returns because of a CTRL-O command
2527 */
2528 int
2529op_change(oap)
2530 oparg_T *oap;
2531{
2532 colnr_T l;
2533 int retval;
2534#ifdef FEAT_VISUALEXTRA
2535 long offset;
2536 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002537 long ins_len;
2538 long pre_textlen = 0;
2539 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 char_u *firstline;
2541 char_u *ins_text, *newp, *oldp;
2542 struct block_def bd;
2543#endif
2544
2545 l = oap->start.col;
2546 if (oap->motion_type == MLINE)
2547 {
2548 l = 0;
2549#ifdef FEAT_SMARTINDENT
2550 if (!p_paste && curbuf->b_p_si
2551# ifdef FEAT_CINDENT
2552 && !curbuf->b_p_cin
2553# endif
2554 )
2555 can_si = TRUE; /* It's like opening a new line, do si */
2556#endif
2557 }
2558
2559 /* First delete the text in the region. In an empty buffer only need to
2560 * save for undo */
2561 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2562 {
2563 if (u_save_cursor() == FAIL)
2564 return FALSE;
2565 }
2566 else if (op_delete(oap) == FAIL)
2567 return FALSE;
2568
2569 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2570 && !virtual_op)
2571 inc_cursor();
2572
2573#ifdef FEAT_VISUALEXTRA
2574 /* check for still on same line (<CR> in inserted text meaningless) */
2575 /* skip blank lines too */
2576 if (oap->block_mode)
2577 {
2578# ifdef FEAT_VIRTUALEDIT
2579 /* Add spaces before getting the current line length. */
2580 if (virtual_op && (curwin->w_cursor.coladd > 0
2581 || gchar_cursor() == NUL))
2582 coladvance_force(getviscol());
2583# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002584 firstline = ml_get(oap->start.lnum);
2585 pre_textlen = (long)STRLEN(firstline);
2586 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 bd.textcol = curwin->w_cursor.col;
2588 }
2589#endif
2590
2591#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2592 if (oap->motion_type == MLINE)
2593 fix_indent();
2594#endif
2595
2596 retval = edit(NUL, FALSE, (linenr_T)1);
2597
2598#ifdef FEAT_VISUALEXTRA
2599 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002600 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 * block.
2602 */
2603 if (oap->block_mode && oap->start.lnum != oap->end.lnum)
2604 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002605 /* Auto-indenting may have changed the indent. If the cursor was past
2606 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002608 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002610 long new_indent = (long)(skipwhite(firstline) - firstline);
2611
2612 pre_textlen += new_indent - pre_indent;
2613 bd.textcol += new_indent - pre_indent;
2614 }
2615
2616 ins_len = (long)STRLEN(firstline) - pre_textlen;
2617 if (ins_len > 0)
2618 {
2619 /* Subsequent calls to ml_get() flush the firstline data - take a
2620 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2622 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002623 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2625 linenr++)
2626 {
2627 block_prep(oap, &bd, linenr, TRUE);
2628 if (!bd.is_short || virtual_op)
2629 {
2630# ifdef FEAT_VIRTUALEDIT
2631 pos_T vpos;
2632
2633 /* If the block starts in virtual space, count the
2634 * initial coladd offset as part of "startspaces" */
2635 if (bd.is_short)
2636 {
2637 linenr_T lnum = curwin->w_cursor.lnum;
2638
2639 curwin->w_cursor.lnum = linenr;
2640 (void)getvpos(&vpos, oap->start_vcol);
2641 curwin->w_cursor.lnum = lnum;
2642 }
2643 else
2644 vpos.coladd = 0;
2645# endif
2646 oldp = ml_get(linenr);
2647 newp = alloc_check((unsigned)(STRLEN(oldp)
2648# ifdef FEAT_VIRTUALEDIT
2649 + vpos.coladd
2650# endif
2651 + ins_len + 1));
2652 if (newp == NULL)
2653 continue;
2654 /* copy up to block start */
2655 mch_memmove(newp, oldp, (size_t)bd.textcol);
2656 offset = bd.textcol;
2657# ifdef FEAT_VIRTUALEDIT
2658 copy_spaces(newp + offset, (size_t)vpos.coladd);
2659 offset += vpos.coladd;
2660# endif
2661 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2662 offset += ins_len;
2663 oldp += bd.textcol;
2664 mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2665 ml_replace(linenr, newp, FALSE);
2666 }
2667 }
2668 check_cursor();
2669
2670 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2671 }
2672 vim_free(ins_text);
2673 }
2674 }
2675#endif
2676
2677 return retval;
2678}
2679
2680/*
2681 * set all the yank registers to empty (called from main())
2682 */
2683 void
2684init_yank()
2685{
2686 int i;
2687
2688 for (i = 0; i < NUM_REGISTERS; ++i)
2689 y_regs[i].y_array = NULL;
2690}
2691
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002692#if defined(EXITFREE) || defined(PROTO)
2693 void
2694clear_registers()
2695{
2696 int i;
2697
2698 for (i = 0; i < NUM_REGISTERS; ++i)
2699 {
2700 y_current = &y_regs[i];
2701 if (y_current->y_array != NULL)
2702 free_yank_all();
2703 }
2704}
2705#endif
2706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707/*
2708 * Free "n" lines from the current yank register.
2709 * Called for normal freeing and in case of error.
2710 */
2711 static void
2712free_yank(n)
2713 long n;
2714{
2715 if (y_current->y_array != NULL)
2716 {
2717 long i;
2718
2719 for (i = n; --i >= 0; )
2720 {
2721#ifdef AMIGA /* only for very slow machines */
2722 if ((i & 1023) == 1023) /* this may take a while */
2723 {
2724 /*
2725 * This message should never cause a hit-return message.
2726 * Overwrite this message with any next message.
2727 */
2728 ++no_wait_return;
2729 smsg((char_u *)_("freeing %ld lines"), i + 1);
2730 --no_wait_return;
2731 msg_didout = FALSE;
2732 msg_col = 0;
2733 }
2734#endif
2735 vim_free(y_current->y_array[i]);
2736 }
2737 vim_free(y_current->y_array);
2738 y_current->y_array = NULL;
2739#ifdef AMIGA
2740 if (n >= 1000)
2741 MSG("");
2742#endif
2743 }
2744}
2745
2746 static void
2747free_yank_all()
2748{
2749 free_yank(y_current->y_size);
2750}
2751
2752/*
2753 * Yank the text between "oap->start" and "oap->end" into a yank register.
2754 * If we are to append (uppercase register), we first yank into a new yank
2755 * register and then concatenate the old and the new one (so we keep the old
2756 * one in case of out-of-memory).
2757 *
2758 * return FAIL for failure, OK otherwise
2759 */
2760 int
2761op_yank(oap, deleting, mess)
2762 oparg_T *oap;
2763 int deleting;
2764 int mess;
2765{
2766 long y_idx; /* index in y_array[] */
2767 struct yankreg *curr; /* copy of y_current */
2768 struct yankreg newreg; /* new yank register when appending */
2769 char_u **new_ptr;
2770 linenr_T lnum; /* current line number */
2771 long j;
2772 int yanktype = oap->motion_type;
2773 long yanklines = oap->line_count;
2774 linenr_T yankendlnum = oap->end.lnum;
2775 char_u *p;
2776 char_u *pnew;
2777 struct block_def bd;
2778
2779 /* check for read-only register */
2780 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2781 {
2782 beep_flush();
2783 return FAIL;
2784 }
2785 if (oap->regname == '_') /* black hole: nothing to do */
2786 return OK;
2787
2788#ifdef FEAT_CLIPBOARD
2789 if (!clip_star.available && oap->regname == '*')
2790 oap->regname = 0;
2791 else if (!clip_plus.available && oap->regname == '+')
2792 oap->regname = 0;
2793#endif
2794
2795 if (!deleting) /* op_delete() already set y_current */
2796 get_yank_register(oap->regname, TRUE);
2797
2798 curr = y_current;
2799 /* append to existing contents */
2800 if (y_append && y_current->y_array != NULL)
2801 y_current = &newreg;
2802 else
2803 free_yank_all(); /* free previously yanked lines */
2804
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002805 /*
2806 * If the cursor was in column 1 before and after the movement, and the
2807 * operator is not inclusive, the yank is always linewise.
2808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 if ( oap->motion_type == MCHAR
2810 && oap->start.col == 0
2811 && !oap->inclusive
2812#ifdef FEAT_VISUAL
2813 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002814 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
2816 && oap->end.col == 0
2817 && yanklines > 1)
2818 {
2819 yanktype = MLINE;
2820 --yankendlnum;
2821 --yanklines;
2822 }
2823
2824 y_current->y_size = yanklines;
2825 y_current->y_type = yanktype; /* set the yank register type */
2826#ifdef FEAT_VISUAL
2827 y_current->y_width = 0;
2828#endif
2829 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2830 yanklines), TRUE);
2831
2832 if (y_current->y_array == NULL)
2833 {
2834 y_current = curr;
2835 return FAIL;
2836 }
2837
2838 y_idx = 0;
2839 lnum = oap->start.lnum;
2840
2841#ifdef FEAT_VISUAL
2842 if (oap->block_mode)
2843 {
2844 /* Visual block mode */
2845 y_current->y_type = MBLOCK; /* set the yank register type */
2846 y_current->y_width = oap->end_vcol - oap->start_vcol;
2847
2848 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2849 y_current->y_width--;
2850 }
2851#endif
2852
2853 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2854 {
2855 switch (y_current->y_type)
2856 {
2857#ifdef FEAT_VISUAL
2858 case MBLOCK:
2859 block_prep(oap, &bd, lnum, FALSE);
2860 if (yank_copy_line(&bd, y_idx) == FAIL)
2861 goto fail;
2862 break;
2863#endif
2864
2865 case MLINE:
2866 if ((y_current->y_array[y_idx] =
2867 vim_strsave(ml_get(lnum))) == NULL)
2868 goto fail;
2869 break;
2870
2871 case MCHAR:
2872 {
2873 colnr_T startcol = 0, endcol = MAXCOL;
2874#ifdef FEAT_VIRTUALEDIT
2875 int is_oneChar = FALSE;
2876 colnr_T cs, ce;
2877#endif
2878 p = ml_get(lnum);
2879 bd.startspaces = 0;
2880 bd.endspaces = 0;
2881
2882 if (lnum == oap->start.lnum)
2883 {
2884 startcol = oap->start.col;
2885#ifdef FEAT_VIRTUALEDIT
2886 if (virtual_op)
2887 {
2888 getvcol(curwin, &oap->start, &cs, NULL, &ce);
2889 if (ce != cs && oap->start.coladd > 0)
2890 {
2891 /* Part of a tab selected -- but don't
2892 * double-count it. */
2893 bd.startspaces = (ce - cs + 1)
2894 - oap->start.coladd;
2895 startcol++;
2896 }
2897 }
2898#endif
2899 }
2900
2901 if (lnum == oap->end.lnum)
2902 {
2903 endcol = oap->end.col;
2904#ifdef FEAT_VIRTUALEDIT
2905 if (virtual_op)
2906 {
2907 getvcol(curwin, &oap->end, &cs, NULL, &ce);
2908 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2909# ifdef FEAT_MBYTE
2910 /* Don't add space for double-wide
2911 * char; endcol will be on last byte
2912 * of multi-byte char. */
2913 && (*mb_head_off)(p, p + endcol) == 0
2914# endif
2915 ))
2916 {
2917 if (oap->start.lnum == oap->end.lnum
2918 && oap->start.col == oap->end.col)
2919 {
2920 /* Special case: inside a single char */
2921 is_oneChar = TRUE;
2922 bd.startspaces = oap->end.coladd
2923 - oap->start.coladd + oap->inclusive;
2924 endcol = startcol;
2925 }
2926 else
2927 {
2928 bd.endspaces = oap->end.coladd
2929 + oap->inclusive;
2930 endcol -= oap->inclusive;
2931 }
2932 }
2933 }
2934#endif
2935 }
2936 if (startcol > endcol
2937#ifdef FEAT_VIRTUALEDIT
2938 || is_oneChar
2939#endif
2940 )
2941 bd.textlen = 0;
2942 else
2943 {
2944 if (endcol == MAXCOL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002945 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 bd.textlen = endcol - startcol + oap->inclusive;
2947 }
2948 bd.textstart = p + startcol;
2949 if (yank_copy_line(&bd, y_idx) == FAIL)
2950 goto fail;
2951 break;
2952 }
2953 /* NOTREACHED */
2954 }
2955 }
2956
2957 if (curr != y_current) /* append the new block to the old block */
2958 {
2959 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2960 (curr->y_size + y_current->y_size)), TRUE);
2961 if (new_ptr == NULL)
2962 goto fail;
2963 for (j = 0; j < curr->y_size; ++j)
2964 new_ptr[j] = curr->y_array[j];
2965 vim_free(curr->y_array);
2966 curr->y_array = new_ptr;
2967
2968 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
2969 curr->y_type = MLINE;
2970
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002971 /* Concatenate the last line of the old block with the first line of
2972 * the new block, unless being Vi compatible. */
2973 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
2975 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2976 + STRLEN(y_current->y_array[0]) + 1), TRUE);
2977 if (pnew == NULL)
2978 {
2979 y_idx = y_current->y_size - 1;
2980 goto fail;
2981 }
2982 STRCPY(pnew, curr->y_array[--j]);
2983 STRCAT(pnew, y_current->y_array[0]);
2984 vim_free(curr->y_array[j]);
2985 vim_free(y_current->y_array[0]);
2986 curr->y_array[j++] = pnew;
2987 y_idx = 1;
2988 }
2989 else
2990 y_idx = 0;
2991 while (y_idx < y_current->y_size)
2992 curr->y_array[j++] = y_current->y_array[y_idx++];
2993 curr->y_size = j;
2994 vim_free(y_current->y_array);
2995 y_current = curr;
2996 }
2997 if (mess) /* Display message about yank? */
2998 {
2999 if (yanktype == MCHAR
3000#ifdef FEAT_VISUAL
3001 && !oap->block_mode
3002#endif
3003 && yanklines == 1)
3004 yanklines = 0;
3005 /* Some versions of Vi use ">=" here, some don't... */
3006 if (yanklines > p_report)
3007 {
3008 /* redisplay now, so message is not deleted */
3009 update_topline_redraw();
3010 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003011 {
3012#ifdef FEAT_VISUAL
3013 if (oap->block_mode)
3014 MSG(_("block of 1 line yanked"));
3015 else
3016#endif
3017 MSG(_("1 line yanked"));
3018 }
3019#ifdef FEAT_VISUAL
3020 else if (oap->block_mode)
3021 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else
3024 smsg((char_u *)_("%ld lines yanked"), yanklines);
3025 }
3026 }
3027
3028 /*
3029 * Set "'[" and "']" marks.
3030 */
3031 curbuf->b_op_start = oap->start;
3032 curbuf->b_op_end = oap->end;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003033 if (yanktype == MLINE
3034#ifdef FEAT_VISUAL
3035 && !oap->block_mode
3036#endif
3037 )
3038 {
3039 curbuf->b_op_start.col = 0;
3040 curbuf->b_op_end.col = MAXCOL;
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043#ifdef FEAT_CLIPBOARD
3044 /*
3045 * If we were yanking to the '*' register, send result to clipboard.
3046 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3047 * to the '*' register.
3048 */
3049 if (clip_star.available
3050 && (curr == &(y_regs[STAR_REGISTER])
3051 || (!deleting && oap->regname == 0 && clip_unnamed)))
3052 {
3053 if (curr != &(y_regs[STAR_REGISTER]))
3054 /* Copy the text from register 0 to the clipboard register. */
3055 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3056
3057 clip_own_selection(&clip_star);
3058 clip_gen_set_selection(&clip_star);
3059 }
3060
3061# ifdef FEAT_X11
3062 /*
3063 * If we were yanking to the '+' register, send result to selection.
3064 * Also copy to the '*' register, in case auto-select is off.
3065 */
3066 else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
3067 {
3068 /* No need to copy to * register upon 'unnamed' now - see below */
3069 clip_own_selection(&clip_plus);
3070 clip_gen_set_selection(&clip_plus);
3071 if (!clip_isautosel())
3072 {
3073 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3074 clip_own_selection(&clip_star);
3075 clip_gen_set_selection(&clip_star);
3076 }
3077 }
3078# endif
3079#endif
3080
3081 return OK;
3082
3083fail: /* free the allocated lines */
3084 free_yank(y_idx + 1);
3085 y_current = curr;
3086 return FAIL;
3087}
3088
3089 static int
3090yank_copy_line(bd, y_idx)
3091 struct block_def *bd;
3092 long y_idx;
3093{
3094 char_u *pnew;
3095
3096 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3097 == NULL)
3098 return FAIL;
3099 y_current->y_array[y_idx] = pnew;
3100 copy_spaces(pnew, (size_t)bd->startspaces);
3101 pnew += bd->startspaces;
3102 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3103 pnew += bd->textlen;
3104 copy_spaces(pnew, (size_t)bd->endspaces);
3105 pnew += bd->endspaces;
3106 *pnew = NUL;
3107 return OK;
3108}
3109
3110#ifdef FEAT_CLIPBOARD
3111/*
3112 * Make a copy of the y_current register to register "reg".
3113 */
3114 static void
3115copy_yank_reg(reg)
3116 struct yankreg *reg;
3117{
3118 struct yankreg *curr = y_current;
3119 long j;
3120
3121 y_current = reg;
3122 free_yank_all();
3123 *y_current = *curr;
3124 y_current->y_array = (char_u **)lalloc_clear(
3125 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3126 if (y_current->y_array == NULL)
3127 y_current->y_size = 0;
3128 else
3129 for (j = 0; j < y_current->y_size; ++j)
3130 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3131 {
3132 free_yank(j);
3133 y_current->y_size = 0;
3134 break;
3135 }
3136 y_current = curr;
3137}
3138#endif
3139
3140/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003141 * Put contents of register "regname" into the text.
3142 * Caller must check "regname" to be valid!
3143 * "flags": PUT_FIXINDENT make indent look nice
3144 * PUT_CURSEND leave cursor after end of new text
3145 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
3147 void
3148do_put(regname, dir, count, flags)
3149 int regname;
3150 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3151 long count;
3152 int flags;
3153{
3154 char_u *ptr;
3155 char_u *newp, *oldp;
3156 int yanklen;
3157 int totlen = 0; /* init for gcc */
3158 linenr_T lnum;
3159 colnr_T col;
3160 long i; /* index in y_array[] */
3161 int y_type;
3162 long y_size;
3163#ifdef FEAT_VISUAL
3164 int oldlen;
3165 long y_width = 0;
3166 colnr_T vcol;
3167 int delcount;
3168 int incr = 0;
3169 long j;
3170 struct block_def bd;
3171#endif
3172 char_u **y_array = NULL;
3173 long nr_lines = 0;
3174 pos_T new_cursor;
3175 int indent;
3176 int orig_indent = 0; /* init for gcc */
3177 int indent_diff = 0; /* init for gcc */
3178 int first_indent = TRUE;
3179 int lendiff = 0;
3180 pos_T old_pos;
3181 char_u *insert_string = NULL;
3182 int allocated = FALSE;
3183 long cnt;
3184
3185#ifdef FEAT_CLIPBOARD
3186 /* Adjust register name for "unnamed" in 'clipboard'. */
3187 adjust_clip_reg(&regname);
3188 (void)may_get_selection(regname);
3189#endif
3190
3191 if (flags & PUT_FIXINDENT)
3192 orig_indent = get_indent();
3193
3194 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3195 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3196
3197 /*
3198 * Using inserted text works differently, because the register includes
3199 * special characters (newlines, etc.).
3200 */
3201 if (regname == '.')
3202 {
3203 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3204 (count == -1 ? 'O' : 'i')), count, FALSE);
3205 /* Putting the text is done later, so can't really move the cursor to
3206 * the next character. Use "l" to simulate it. */
3207 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3208 stuffcharReadbuff('l');
3209 return;
3210 }
3211
3212 /*
3213 * For special registers '%' (file name), '#' (alternate file name) and
3214 * ':' (last command line), etc. we have to create a fake yank register.
3215 */
3216 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3217 {
3218 if (insert_string == NULL)
3219 return;
3220 }
3221
3222 if (insert_string != NULL)
3223 {
3224 y_type = MCHAR;
3225#ifdef FEAT_EVAL
3226 if (regname == '=')
3227 {
3228 /* For the = register we need to split the string at NL
3229 * characters. */
3230 /* Loop twice: count the number of lines and save them. */
3231 for (;;)
3232 {
3233 y_size = 0;
3234 ptr = insert_string;
3235 while (ptr != NULL)
3236 {
3237 if (y_array != NULL)
3238 y_array[y_size] = ptr;
3239 ++y_size;
3240 ptr = vim_strchr(ptr, '\n');
3241 if (ptr != NULL)
3242 {
3243 if (y_array != NULL)
3244 *ptr = NUL;
3245 ++ptr;
3246 /* A trailing '\n' makes the string linewise */
3247 if (*ptr == NUL)
3248 {
3249 y_type = MLINE;
3250 break;
3251 }
3252 }
3253 }
3254 if (y_array != NULL)
3255 break;
3256 y_array = (char_u **)alloc((unsigned)
3257 (y_size * sizeof(char_u *)));
3258 if (y_array == NULL)
3259 goto end;
3260 }
3261 }
3262 else
3263#endif
3264 {
3265 y_size = 1; /* use fake one-line yank register */
3266 y_array = &insert_string;
3267 }
3268 }
3269 else
3270 {
3271 get_yank_register(regname, FALSE);
3272
3273 y_type = y_current->y_type;
3274#ifdef FEAT_VISUAL
3275 y_width = y_current->y_width;
3276#endif
3277 y_size = y_current->y_size;
3278 y_array = y_current->y_array;
3279 }
3280
3281#ifdef FEAT_VISUAL
3282 if (y_type == MLINE)
3283 {
3284 if (flags & PUT_LINE_SPLIT)
3285 {
3286 /* "p" or "P" in Visual mode: split the lines to put the text in
3287 * between. */
3288 if (u_save_cursor() == FAIL)
3289 goto end;
3290 ptr = vim_strsave(ml_get_cursor());
3291 if (ptr == NULL)
3292 goto end;
3293 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3294 vim_free(ptr);
3295
3296 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3297 if (ptr == NULL)
3298 goto end;
3299 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3300 ++nr_lines;
3301 dir = FORWARD;
3302 }
3303 if (flags & PUT_LINE_FORWARD)
3304 {
3305 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003306 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 dir = FORWARD;
3308 }
3309 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3310 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3311 }
3312#endif
3313
3314 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3315 y_type = MLINE;
3316
3317 if (y_size == 0 || y_array == NULL)
3318 {
3319 EMSG2(_("E353: Nothing in register %s"),
3320 regname == 0 ? (char_u *)"\"" : transchar(regname));
3321 goto end;
3322 }
3323
3324#ifdef FEAT_VISUAL
3325 if (y_type == MBLOCK)
3326 {
3327 lnum = curwin->w_cursor.lnum + y_size + 1;
3328 if (lnum > curbuf->b_ml.ml_line_count)
3329 lnum = curbuf->b_ml.ml_line_count + 1;
3330 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3331 goto end;
3332 }
3333 else
3334#endif
3335 if (y_type == MLINE)
3336 {
3337 lnum = curwin->w_cursor.lnum;
3338#ifdef FEAT_FOLDING
3339 /* Correct line number for closed fold. Don't move the cursor yet,
3340 * u_save() uses it. */
3341 if (dir == BACKWARD)
3342 (void)hasFolding(lnum, &lnum, NULL);
3343 else
3344 (void)hasFolding(lnum, NULL, &lnum);
3345#endif
3346 if (dir == FORWARD)
3347 ++lnum;
3348 if (u_save(lnum - 1, lnum) == FAIL)
3349 goto end;
3350#ifdef FEAT_FOLDING
3351 if (dir == FORWARD)
3352 curwin->w_cursor.lnum = lnum - 1;
3353 else
3354 curwin->w_cursor.lnum = lnum;
3355 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3356#endif
3357 }
3358 else if (u_save_cursor() == FAIL)
3359 goto end;
3360
3361 yanklen = (int)STRLEN(y_array[0]);
3362
3363#ifdef FEAT_VIRTUALEDIT
3364 if (ve_flags == VE_ALL && y_type == MCHAR)
3365 {
3366 if (gchar_cursor() == TAB)
3367 {
3368 /* Don't need to insert spaces when "p" on the last position of a
3369 * tab or "P" on the first position. */
3370 if (dir == FORWARD
3371 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3372 : curwin->w_cursor.coladd > 0)
3373 coladvance_force(getviscol());
3374 else
3375 curwin->w_cursor.coladd = 0;
3376 }
3377 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3378 coladvance_force(getviscol() + (dir == FORWARD));
3379 }
3380#endif
3381
3382 lnum = curwin->w_cursor.lnum;
3383 col = curwin->w_cursor.col;
3384
3385#ifdef FEAT_VISUAL
3386 /*
3387 * Block mode
3388 */
3389 if (y_type == MBLOCK)
3390 {
3391 char c = gchar_cursor();
3392 colnr_T endcol2 = 0;
3393
3394 if (dir == FORWARD && c != NUL)
3395 {
3396#ifdef FEAT_VIRTUALEDIT
3397 if (ve_flags == VE_ALL)
3398 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3399 else
3400#endif
3401 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3402
3403#ifdef FEAT_MBYTE
3404 if (has_mbyte)
3405 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003406 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 else
3408#endif
3409#ifdef FEAT_VIRTUALEDIT
3410 if (c != TAB || ve_flags != VE_ALL)
3411#endif
3412 ++curwin->w_cursor.col;
3413 ++col;
3414 }
3415 else
3416 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3417
3418#ifdef FEAT_VIRTUALEDIT
3419 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003420 if (ve_flags == VE_ALL
3421 && (curwin->w_cursor.coladd > 0
3422 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
3424 if (dir == FORWARD && c == NUL)
3425 ++col;
3426 if (dir != FORWARD && c != NUL)
3427 ++curwin->w_cursor.col;
3428 if (c == TAB)
3429 {
3430 if (dir == BACKWARD && curwin->w_cursor.col)
3431 curwin->w_cursor.col--;
3432 if (dir == FORWARD && col - 1 == endcol2)
3433 curwin->w_cursor.col++;
3434 }
3435 }
3436 curwin->w_cursor.coladd = 0;
3437#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003438 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 for (i = 0; i < y_size; ++i)
3440 {
3441 int spaces;
3442 char shortline;
3443
3444 bd.startspaces = 0;
3445 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 vcol = 0;
3447 delcount = 0;
3448
3449 /* add a new line */
3450 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3451 {
3452 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3453 (colnr_T)1, FALSE) == FAIL)
3454 break;
3455 ++nr_lines;
3456 }
3457 /* get the old line and advance to the position to insert at */
3458 oldp = ml_get_curline();
3459 oldlen = (int)STRLEN(oldp);
3460 for (ptr = oldp; vcol < col && *ptr; )
3461 {
3462 /* Count a tab for what it's worth (if list mode not on) */
3463 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3464 vcol += incr;
3465 }
3466 bd.textcol = (colnr_T)(ptr - oldp);
3467
3468 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3469
3470 if (vcol < col) /* line too short, padd with spaces */
3471 bd.startspaces = col - vcol;
3472 else if (vcol > col)
3473 {
3474 bd.endspaces = vcol - col;
3475 bd.startspaces = incr - bd.endspaces;
3476 --bd.textcol;
3477 delcount = 1;
3478#ifdef FEAT_MBYTE
3479 if (has_mbyte)
3480 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3481#endif
3482 if (oldp[bd.textcol] != TAB)
3483 {
3484 /* Only a Tab can be split into spaces. Other
3485 * characters will have to be moved to after the
3486 * block, causing misalignment. */
3487 delcount = 0;
3488 bd.endspaces = 0;
3489 }
3490 }
3491
3492 yanklen = (int)STRLEN(y_array[i]);
3493
3494 /* calculate number of spaces required to fill right side of block*/
3495 spaces = y_width + 1;
3496 for (j = 0; j < yanklen; j++)
3497 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3498 if (spaces < 0)
3499 spaces = 0;
3500
3501 /* insert the new text */
3502 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3503 newp = alloc_check((unsigned)totlen + oldlen + 1);
3504 if (newp == NULL)
3505 break;
3506 /* copy part up to cursor to new line */
3507 ptr = newp;
3508 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3509 ptr += bd.textcol;
3510 /* may insert some spaces before the new text */
3511 copy_spaces(ptr, (size_t)bd.startspaces);
3512 ptr += bd.startspaces;
3513 /* insert the new text */
3514 for (j = 0; j < count; ++j)
3515 {
3516 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3517 ptr += yanklen;
3518
3519 /* insert block's trailing spaces only if there's text behind */
3520 if ((j < count - 1 || !shortline) && spaces)
3521 {
3522 copy_spaces(ptr, (size_t)spaces);
3523 ptr += spaces;
3524 }
3525 }
3526 /* may insert some spaces after the new text */
3527 copy_spaces(ptr, (size_t)bd.endspaces);
3528 ptr += bd.endspaces;
3529 /* move the text after the cursor to the end of the line. */
3530 mch_memmove(ptr, oldp + bd.textcol + delcount,
3531 (size_t)(oldlen - bd.textcol - delcount + 1));
3532 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3533
3534 ++curwin->w_cursor.lnum;
3535 if (i == 0)
3536 curwin->w_cursor.col += bd.startspaces;
3537 }
3538
3539 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3540
3541 /* Set '[ mark. */
3542 curbuf->b_op_start = curwin->w_cursor;
3543 curbuf->b_op_start.lnum = lnum;
3544
3545 /* adjust '] mark */
3546 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3547 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003548# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (flags & PUT_CURSEND)
3552 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003553 colnr_T len;
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 curwin->w_cursor = curbuf->b_op_end;
3556 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003557
3558 /* in Insert mode we might be after the NUL, correct for that */
3559 len = (colnr_T)STRLEN(ml_get_curline());
3560 if (curwin->w_cursor.col > len)
3561 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563 else
3564 curwin->w_cursor.lnum = lnum;
3565 }
3566 else
3567#endif
3568 {
3569 /*
3570 * Character or Line mode
3571 */
3572 if (y_type == MCHAR)
3573 {
3574 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3575 * char */
3576 if (dir == FORWARD && gchar_cursor() != NUL)
3577 {
3578#ifdef FEAT_MBYTE
3579 if (has_mbyte)
3580 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003581 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /* put it on the next of the multi-byte character. */
3584 col += bytelen;
3585 if (yanklen)
3586 {
3587 curwin->w_cursor.col += bytelen;
3588 curbuf->b_op_end.col += bytelen;
3589 }
3590 }
3591 else
3592#endif
3593 {
3594 ++col;
3595 if (yanklen)
3596 {
3597 ++curwin->w_cursor.col;
3598 ++curbuf->b_op_end.col;
3599 }
3600 }
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 curbuf->b_op_start = curwin->w_cursor;
3603 }
3604 /*
3605 * Line mode: BACKWARD is the same as FORWARD on the previous line
3606 */
3607 else if (dir == BACKWARD)
3608 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003609 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
3611 /*
3612 * simple case: insert into current line
3613 */
3614 if (y_type == MCHAR && y_size == 1)
3615 {
3616 totlen = count * yanklen;
3617 if (totlen)
3618 {
3619 oldp = ml_get(lnum);
3620 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3621 if (newp == NULL)
3622 goto end; /* alloc() will give error message */
3623 mch_memmove(newp, oldp, (size_t)col);
3624 ptr = newp + col;
3625 for (i = 0; i < count; ++i)
3626 {
3627 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3628 ptr += yanklen;
3629 }
3630 mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3631 ml_replace(lnum, newp, FALSE);
3632 /* Put cursor on last putted char. */
3633 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3634 }
3635 curbuf->b_op_end = curwin->w_cursor;
3636 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3637 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3638 ++curwin->w_cursor.col;
3639 changed_bytes(lnum, col);
3640 }
3641 else
3642 {
3643 /*
3644 * Insert at least one line. When y_type is MCHAR, break the first
3645 * line in two.
3646 */
3647 for (cnt = 1; cnt <= count; ++cnt)
3648 {
3649 i = 0;
3650 if (y_type == MCHAR)
3651 {
3652 /*
3653 * Split the current line in two at the insert position.
3654 * First insert y_array[size - 1] in front of second line.
3655 * Then append y_array[0] to first line.
3656 */
3657 lnum = new_cursor.lnum;
3658 ptr = ml_get(lnum) + col;
3659 totlen = (int)STRLEN(y_array[y_size - 1]);
3660 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3661 if (newp == NULL)
3662 goto error;
3663 STRCPY(newp, y_array[y_size - 1]);
3664 STRCAT(newp, ptr);
3665 /* insert second line */
3666 ml_append(lnum, newp, (colnr_T)0, FALSE);
3667 vim_free(newp);
3668
3669 oldp = ml_get(lnum);
3670 newp = alloc_check((unsigned)(col + yanklen + 1));
3671 if (newp == NULL)
3672 goto error;
3673 /* copy first part of line */
3674 mch_memmove(newp, oldp, (size_t)col);
3675 /* append to first line */
3676 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3677 ml_replace(lnum, newp, FALSE);
3678
3679 curwin->w_cursor.lnum = lnum;
3680 i = 1;
3681 }
3682
3683 for (; i < y_size; ++i)
3684 {
3685 if ((y_type != MCHAR || i < y_size - 1)
3686 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3687 == FAIL)
3688 goto error;
3689 lnum++;
3690 ++nr_lines;
3691 if (flags & PUT_FIXINDENT)
3692 {
3693 old_pos = curwin->w_cursor;
3694 curwin->w_cursor.lnum = lnum;
3695 ptr = ml_get(lnum);
3696 if (cnt == count && i == y_size - 1)
3697 lendiff = (int)STRLEN(ptr);
3698#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3699 if (*ptr == '#' && preprocs_left())
3700 indent = 0; /* Leave # lines at start */
3701 else
3702#endif
3703 if (*ptr == NUL)
3704 indent = 0; /* Ignore empty lines */
3705 else if (first_indent)
3706 {
3707 indent_diff = orig_indent - get_indent();
3708 indent = orig_indent;
3709 first_indent = FALSE;
3710 }
3711 else if ((indent = get_indent() + indent_diff) < 0)
3712 indent = 0;
3713 (void)set_indent(indent, 0);
3714 curwin->w_cursor = old_pos;
3715 /* remember how many chars were removed */
3716 if (cnt == count && i == y_size - 1)
3717 lendiff -= (int)STRLEN(ml_get(lnum));
3718 }
3719 }
3720 }
3721
3722error:
3723 /* Adjust marks. */
3724 if (y_type == MLINE)
3725 {
3726 curbuf->b_op_start.col = 0;
3727 if (dir == FORWARD)
3728 curbuf->b_op_start.lnum++;
3729 }
3730 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3731 (linenr_T)MAXLNUM, nr_lines, 0L);
3732
3733 /* note changed text for displaying and folding */
3734 if (y_type == MCHAR)
3735 changed_lines(curwin->w_cursor.lnum, col,
3736 curwin->w_cursor.lnum + 1, nr_lines);
3737 else
3738 changed_lines(curbuf->b_op_start.lnum, 0,
3739 curbuf->b_op_start.lnum, nr_lines);
3740
3741 /* put '] mark at last inserted character */
3742 curbuf->b_op_end.lnum = lnum;
3743 /* correct length for change in indent */
3744 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3745 if (col > 1)
3746 curbuf->b_op_end.col = col - 1;
3747 else
3748 curbuf->b_op_end.col = 0;
3749
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003750 if (flags & PUT_CURSLINE)
3751 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003752 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003753 curwin->w_cursor.lnum = lnum;
3754 beginline(BL_WHITE | BL_FIX);
3755 }
3756 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758 /* put cursor after inserted text */
3759 if (y_type == MLINE)
3760 {
3761 if (lnum >= curbuf->b_ml.ml_line_count)
3762 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3763 else
3764 curwin->w_cursor.lnum = lnum + 1;
3765 curwin->w_cursor.col = 0;
3766 }
3767 else
3768 {
3769 curwin->w_cursor.lnum = lnum;
3770 curwin->w_cursor.col = col;
3771 }
3772 }
3773 else if (y_type == MLINE)
3774 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003775 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 curwin->w_cursor.col = 0;
3777 if (dir == FORWARD)
3778 ++curwin->w_cursor.lnum;
3779 beginline(BL_WHITE | BL_FIX);
3780 }
3781 else /* put cursor on first inserted character */
3782 curwin->w_cursor = new_cursor;
3783 }
3784 }
3785
3786 msgmore(nr_lines);
3787 curwin->w_set_curswant = TRUE;
3788
3789end:
3790 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003792 if (regname == '=')
3793 vim_free(y_array);
3794
Bram Moolenaar677ee682005-01-27 14:41:15 +00003795 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003796 adjust_cursor_eol();
3797}
3798
3799/*
3800 * When the cursor is on the NUL past the end of the line and it should not be
3801 * there move it left.
3802 */
3803 void
3804adjust_cursor_eol()
3805{
3806 if (curwin->w_cursor.col > 0
3807 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003808#ifdef FEAT_VIRTUALEDIT
3809 && (ve_flags & VE_ONEMORE) == 0
3810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 && !(restart_edit || (State & INSERT)))
3812 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003813 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003814 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#ifdef FEAT_VIRTUALEDIT
3817 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003818 {
3819 colnr_T scol, ecol;
3820
3821 /* Coladd is set to the width of the last character. */
3822 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3823 curwin->w_cursor.coladd = ecol - scol + 1;
3824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825#endif
3826 }
3827}
3828
3829#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3830/*
3831 * Return TRUE if lines starting with '#' should be left aligned.
3832 */
3833 int
3834preprocs_left()
3835{
3836 return
3837# ifdef FEAT_SMARTINDENT
3838# ifdef FEAT_CINDENT
3839 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3840# else
3841 curbuf->b_p_si
3842# endif
3843# endif
3844# ifdef FEAT_CINDENT
3845 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3846# endif
3847 ;
3848}
3849#endif
3850
3851/* Return the character name of the register with the given number */
3852 int
3853get_register_name(num)
3854 int num;
3855{
3856 if (num == -1)
3857 return '"';
3858 else if (num < 10)
3859 return num + '0';
3860 else if (num == DELETION_REGISTER)
3861 return '-';
3862#ifdef FEAT_CLIPBOARD
3863 else if (num == STAR_REGISTER)
3864 return '*';
3865 else if (num == PLUS_REGISTER)
3866 return '+';
3867#endif
3868 else
3869 {
3870#ifdef EBCDIC
3871 int i;
3872
3873 /* EBCDIC is really braindead ... */
3874 i = 'a' + (num - 10);
3875 if (i > 'i')
3876 i += 7;
3877 if (i > 'r')
3878 i += 8;
3879 return i;
3880#else
3881 return num + 'a' - 10;
3882#endif
3883 }
3884}
3885
3886/*
3887 * ":dis" and ":registers": Display the contents of the yank registers.
3888 */
3889 void
3890ex_display(eap)
3891 exarg_T *eap;
3892{
3893 int i, n;
3894 long j;
3895 char_u *p;
3896 struct yankreg *yb;
3897 int name;
3898 int attr;
3899 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003900#ifdef FEAT_MBYTE
3901 int clen;
3902#else
3903# define clen 1
3904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 if (arg != NULL && *arg == NUL)
3907 arg = NULL;
3908 attr = hl_attr(HLF_8);
3909
3910 /* Highlight title */
3911 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3912 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3913 {
3914 name = get_register_name(i);
3915 if (arg != NULL && vim_strchr(arg, name) == NULL)
3916 continue; /* did not ask for this register */
3917
3918#ifdef FEAT_CLIPBOARD
3919 /* Adjust register name for "unnamed" in 'clipboard'.
3920 * When it's a clipboard register, fill it with the current contents
3921 * of the clipboard. */
3922 adjust_clip_reg(&name);
3923 (void)may_get_selection(name);
3924#endif
3925
3926 if (i == -1)
3927 {
3928 if (y_previous != NULL)
3929 yb = y_previous;
3930 else
3931 yb = &(y_regs[0]);
3932 }
3933 else
3934 yb = &(y_regs[i]);
3935 if (yb->y_array != NULL)
3936 {
3937 msg_putchar('\n');
3938 msg_putchar('"');
3939 msg_putchar(name);
3940 MSG_PUTS(" ");
3941
3942 n = (int)Columns - 6;
3943 for (j = 0; j < yb->y_size && n > 1; ++j)
3944 {
3945 if (j)
3946 {
3947 MSG_PUTS_ATTR("^J", attr);
3948 n -= 2;
3949 }
3950 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003953 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003954#endif
3955 msg_outtrans_len(p, clen);
3956#ifdef FEAT_MBYTE
3957 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#endif
3959 }
3960 }
3961 if (n > 1 && yb->y_type == MLINE)
3962 MSG_PUTS_ATTR("^J", attr);
3963 out_flush(); /* show one line at a time */
3964 }
3965 ui_breakcheck();
3966 }
3967
3968 /*
3969 * display last inserted text
3970 */
3971 if ((p = get_last_insert()) != NULL
3972 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3973 {
3974 MSG_PUTS("\n\". ");
3975 dis_msg(p, TRUE);
3976 }
3977
3978 /*
3979 * display last command line
3980 */
3981 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3982 && !got_int)
3983 {
3984 MSG_PUTS("\n\": ");
3985 dis_msg(last_cmdline, FALSE);
3986 }
3987
3988 /*
3989 * display current file name
3990 */
3991 if (curbuf->b_fname != NULL
3992 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3993 {
3994 MSG_PUTS("\n\"% ");
3995 dis_msg(curbuf->b_fname, FALSE);
3996 }
3997
3998 /*
3999 * display alternate file name
4000 */
4001 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4002 {
4003 char_u *fname;
4004 linenr_T dummy;
4005
4006 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4007 {
4008 MSG_PUTS("\n\"# ");
4009 dis_msg(fname, FALSE);
4010 }
4011 }
4012
4013 /*
4014 * display last search pattern
4015 */
4016 if (last_search_pat() != NULL
4017 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4018 {
4019 MSG_PUTS("\n\"/ ");
4020 dis_msg(last_search_pat(), FALSE);
4021 }
4022
4023#ifdef FEAT_EVAL
4024 /*
4025 * display last used expression
4026 */
4027 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4028 && !got_int)
4029 {
4030 MSG_PUTS("\n\"= ");
4031 dis_msg(expr_line, FALSE);
4032 }
4033#endif
4034}
4035
4036/*
4037 * display a string for do_dis()
4038 * truncate at end of screen line
4039 */
4040 static void
4041dis_msg(p, skip_esc)
4042 char_u *p;
4043 int skip_esc; /* if TRUE, ignore trailing ESC */
4044{
4045 int n;
4046#ifdef FEAT_MBYTE
4047 int l;
4048#endif
4049
4050 n = (int)Columns - 6;
4051 while (*p != NUL
4052 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4053 && (n -= ptr2cells(p)) >= 0)
4054 {
4055#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004056 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
4058 msg_outtrans_len(p, l);
4059 p += l;
4060 }
4061 else
4062#endif
4063 msg_outtrans_len(p++, 1);
4064 }
4065 ui_breakcheck();
4066}
4067
4068/*
4069 * join 'count' lines (minimal 2), including u_save()
4070 */
4071 void
4072do_do_join(count, insert_space)
4073 long count;
4074 int insert_space;
4075{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004076 colnr_T col = MAXCOL;
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4079 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4080 return;
4081
4082 while (--count > 0)
4083 {
4084 line_breakcheck();
4085 if (got_int || do_join(insert_space) == FAIL)
4086 {
4087 beep_flush();
4088 break;
4089 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004090 if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4091 col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004094 /* Vi compatible: use the column of the first join */
4095 if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4096 curwin->w_cursor.col = col;
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098#if 0
4099 /*
4100 * Need to update the screen if the line where the cursor is became too
4101 * long to fit on the screen.
4102 */
4103 update_topline_redraw();
4104#endif
4105}
4106
4107/*
4108 * Join two lines at the cursor position.
4109 * "redraw" is TRUE when the screen should be updated.
4110 * Caller must have setup for undo.
4111 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004112 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 */
4114 int
4115do_join(insert_space)
4116 int insert_space;
4117{
4118 char_u *curr;
4119 char_u *next, *next_start;
4120 char_u *newp;
4121 int endcurr1, endcurr2;
4122 int currsize; /* size of the current line */
4123 int nextsize; /* size of the next line */
4124 int spaces; /* number of spaces to insert */
4125 linenr_T t;
4126
4127 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4128 return FAIL; /* can't join on last line */
4129
4130 curr = ml_get_curline();
4131 currsize = (int)STRLEN(curr);
4132 endcurr1 = endcurr2 = NUL;
4133 if (insert_space && currsize > 0)
4134 {
4135#ifdef FEAT_MBYTE
4136 if (has_mbyte)
4137 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004138 next = curr + currsize;
4139 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 endcurr1 = (*mb_ptr2char)(next);
4141 if (next > curr)
4142 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004143 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 endcurr2 = (*mb_ptr2char)(next);
4145 }
4146 }
4147 else
4148#endif
4149 {
4150 endcurr1 = *(curr + currsize - 1);
4151 if (currsize > 1)
4152 endcurr2 = *(curr + currsize - 2);
4153 }
4154 }
4155
4156 next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4157 spaces = 0;
4158 if (insert_space)
4159 {
4160 next = skipwhite(next);
4161 if (*next != ')' && currsize != 0 && endcurr1 != TAB
4162#ifdef FEAT_MBYTE
4163 && (!has_format_option(FO_MBYTE_JOIN)
4164 || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4165 && (!has_format_option(FO_MBYTE_JOIN2)
4166 || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4167#endif
4168 )
4169 {
4170 /* don't add a space if the line is ending in a space */
4171 if (endcurr1 == ' ')
4172 endcurr1 = endcurr2;
4173 else
4174 ++spaces;
4175 /* extra space when 'joinspaces' set and line ends in '.' */
4176 if ( p_js
4177 && (endcurr1 == '.'
4178 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4179 && (endcurr1 == '?' || endcurr1 == '!'))))
4180 ++spaces;
4181 }
4182 }
4183 nextsize = (int)STRLEN(next);
4184
4185 newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4186 if (newp == NULL)
4187 return FAIL;
4188
4189 /*
4190 * Insert the next line first, because we already have that pointer.
4191 * Curr has to be obtained again, because getting next will have
4192 * invalidated it.
4193 */
4194 mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4195
4196 curr = ml_get_curline();
4197 mch_memmove(newp, curr, (size_t)currsize);
4198
4199 copy_spaces(newp + currsize, (size_t)spaces);
4200
4201 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4202
4203 /* Only report the change in the first line here, del_lines() will report
4204 * the deleted line. */
4205 changed_lines(curwin->w_cursor.lnum, currsize,
4206 curwin->w_cursor.lnum + 1, 0L);
4207
4208 /*
4209 * Delete the following line. To do this we move the cursor there
4210 * briefly, and then move it back. After del_lines() the cursor may
4211 * have moved up (last line deleted), so the current lnum is kept in t.
4212 *
4213 * Move marks from the deleted line to the joined line, adjusting the
4214 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4215 * should not really be a problem.
4216 */
4217 t = curwin->w_cursor.lnum;
4218 mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4219 (long)(currsize + spaces - (next - next_start)));
4220 ++curwin->w_cursor.lnum;
4221 del_lines(1L, FALSE);
4222 curwin->w_cursor.lnum = t;
4223
4224 /*
4225 * go to first character of the joined line
4226 */
4227 curwin->w_cursor.col = currsize;
4228 check_cursor_col();
4229#ifdef FEAT_VIRTUALEDIT
4230 curwin->w_cursor.coladd = 0;
4231#endif
4232 curwin->w_set_curswant = TRUE;
4233
4234 return OK;
4235}
4236
4237#ifdef FEAT_COMMENTS
4238/*
4239 * Return TRUE if the two comment leaders given are the same. "lnum" is
4240 * the first line. White-space is ignored. Note that the whole of
4241 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4242 */
4243 static int
4244same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4245 linenr_T lnum;
4246 int leader1_len;
4247 char_u *leader1_flags;
4248 int leader2_len;
4249 char_u *leader2_flags;
4250{
4251 int idx1 = 0, idx2 = 0;
4252 char_u *p;
4253 char_u *line1;
4254 char_u *line2;
4255
4256 if (leader1_len == 0)
4257 return (leader2_len == 0);
4258
4259 /*
4260 * If first leader has 'f' flag, the lines can be joined only if the
4261 * second line does not have a leader.
4262 * If first leader has 'e' flag, the lines can never be joined.
4263 * If fist leader has 's' flag, the lines can only be joined if there is
4264 * some text after it and the second line has the 'm' flag.
4265 */
4266 if (leader1_flags != NULL)
4267 {
4268 for (p = leader1_flags; *p && *p != ':'; ++p)
4269 {
4270 if (*p == COM_FIRST)
4271 return (leader2_len == 0);
4272 if (*p == COM_END)
4273 return FALSE;
4274 if (*p == COM_START)
4275 {
4276 if (*(ml_get(lnum) + leader1_len) == NUL)
4277 return FALSE;
4278 if (leader2_flags == NULL || leader2_len == 0)
4279 return FALSE;
4280 for (p = leader2_flags; *p && *p != ':'; ++p)
4281 if (*p == COM_MIDDLE)
4282 return TRUE;
4283 return FALSE;
4284 }
4285 }
4286 }
4287
4288 /*
4289 * Get current line and next line, compare the leaders.
4290 * The first line has to be saved, only one line can be locked at a time.
4291 */
4292 line1 = vim_strsave(ml_get(lnum));
4293 if (line1 != NULL)
4294 {
4295 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4296 ;
4297 line2 = ml_get(lnum + 1);
4298 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4299 {
4300 if (!vim_iswhite(line2[idx2]))
4301 {
4302 if (line1[idx1++] != line2[idx2])
4303 break;
4304 }
4305 else
4306 while (vim_iswhite(line1[idx1]))
4307 ++idx1;
4308 }
4309 vim_free(line1);
4310 }
4311 return (idx2 == leader2_len && idx1 == leader1_len);
4312}
4313#endif
4314
4315/*
4316 * implementation of the format operator 'gq'
4317 */
4318 void
4319op_format(oap, keep_cursor)
4320 oparg_T *oap;
4321 int keep_cursor; /* keep cursor on same text char */
4322{
4323 long old_line_count = curbuf->b_ml.ml_line_count;
4324
4325 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4326 * can put it back there. */
4327 curwin->w_cursor = oap->cursor_start;
4328
4329 if (u_save((linenr_T)(oap->start.lnum - 1),
4330 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4331 return;
4332 curwin->w_cursor = oap->start;
4333
4334#ifdef FEAT_VISUAL
4335 if (oap->is_VIsual)
4336 /* When there is no change: need to remove the Visual selection */
4337 redraw_curbuf_later(INVERTED);
4338#endif
4339
4340 /* Set '[ mark at the start of the formatted area */
4341 curbuf->b_op_start = oap->start;
4342
4343 /* For "gw" remember the cursor position and put it back below (adjusted
4344 * for joined and split lines). */
4345 if (keep_cursor)
4346 saved_cursor = oap->cursor_start;
4347
4348 format_lines(oap->line_count);
4349
4350 /*
4351 * Leave the cursor at the first non-blank of the last formatted line.
4352 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4353 * line, so "." will do the next lines.
4354 */
4355 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4356 ++curwin->w_cursor.lnum;
4357 beginline(BL_WHITE | BL_FIX);
4358 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4359 msgmore(old_line_count);
4360
4361 /* put '] mark on the end of the formatted area */
4362 curbuf->b_op_end = curwin->w_cursor;
4363
4364 if (keep_cursor)
4365 {
4366 curwin->w_cursor = saved_cursor;
4367 saved_cursor.lnum = 0;
4368 }
4369
4370#ifdef FEAT_VISUAL
4371 if (oap->is_VIsual)
4372 {
4373 win_T *wp;
4374
4375 FOR_ALL_WINDOWS(wp)
4376 {
4377 if (wp->w_old_cursor_lnum != 0)
4378 {
4379 /* When lines have been inserted or deleted, adjust the end of
4380 * the Visual area to be redrawn. */
4381 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4382 wp->w_old_cursor_lnum += old_line_count;
4383 else
4384 wp->w_old_visual_lnum += old_line_count;
4385 }
4386 }
4387 }
4388#endif
4389}
4390
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004391#if defined(FEAT_EVAL) || defined(PROTO)
4392/*
4393 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4394 */
4395 void
4396op_formatexpr(oap)
4397 oparg_T *oap;
4398{
4399# ifdef FEAT_VISUAL
4400 if (oap->is_VIsual)
4401 /* When there is no change: need to remove the Visual selection */
4402 redraw_curbuf_later(INVERTED);
4403# endif
4404
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004405 (void)fex_format(oap->start.lnum, oap->line_count, NUL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004406}
4407
4408 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004409fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004410 linenr_T lnum;
4411 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004412 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004413{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004414 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4415 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004416 int r;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004417#ifdef FEAT_MBYTE
4418 char_u buf[MB_MAXBYTES];
4419#else
4420 char_u buf[2];
4421#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004422
4423 /*
4424 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004425 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004426 */
4427 set_vim_var_nr(VV_LNUM, lnum);
4428 set_vim_var_nr(VV_COUNT, count);
4429
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004430#ifdef FEAT_MBYTE
4431 if (has_mbyte)
4432 buf[(*mb_char2bytes)(c, buf)] = NUL;
4433 else
4434#endif
4435 {
4436 buf[0] = c;
4437 buf[1] = NUL;
4438 }
4439 set_vim_var_string(VV_CHAR, buf, -1);
4440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004441 /*
4442 * Evaluate the function.
4443 */
4444 if (use_sandbox)
4445 ++sandbox;
4446 r = eval_to_number(curbuf->b_p_fex);
4447 if (use_sandbox)
4448 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004449
4450 set_vim_var_string(VV_CHAR, NULL, -1);
4451
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004452 return r;
4453}
4454#endif
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456/*
4457 * Format "line_count" lines, starting at the cursor position.
4458 * When "line_count" is negative, format until the end of the paragraph.
4459 * Lines after the cursor line are saved for undo, caller must have saved the
4460 * first line.
4461 */
4462 void
4463format_lines(line_count)
4464 linenr_T line_count;
4465{
4466 int max_len;
4467 int is_not_par; /* current line not part of parag. */
4468 int next_is_not_par; /* next line not part of paragraph */
4469 int is_end_par; /* at end of paragraph */
4470 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4471 int next_is_start_par = FALSE;
4472#ifdef FEAT_COMMENTS
4473 int leader_len = 0; /* leader len of current line */
4474 int next_leader_len; /* leader len of next line */
4475 char_u *leader_flags = NULL; /* flags for leader of current line */
4476 char_u *next_leader_flags; /* flags for leader of next line */
4477 int do_comments; /* format comments */
4478#endif
4479 int advance = TRUE;
4480 int second_indent = -1;
4481 int do_second_indent;
4482 int do_number_indent;
4483 int do_trail_white;
4484 int first_par_line = TRUE;
4485 int smd_save;
4486 long count;
4487 int need_set_indent = TRUE; /* set indent of next paragraph */
4488 int force_format = FALSE;
4489 int old_State = State;
4490
4491 /* length of a line to force formatting: 3 * 'tw' */
4492 max_len = comp_textwidth(TRUE) * 3;
4493
4494 /* check for 'q', '2' and '1' in 'formatoptions' */
4495#ifdef FEAT_COMMENTS
4496 do_comments = has_format_option(FO_Q_COMS);
4497#endif
4498 do_second_indent = has_format_option(FO_Q_SECOND);
4499 do_number_indent = has_format_option(FO_Q_NUMBER);
4500 do_trail_white = has_format_option(FO_WHITE_PAR);
4501
4502 /*
4503 * Get info about the previous and current line.
4504 */
4505 if (curwin->w_cursor.lnum > 1)
4506 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4507#ifdef FEAT_COMMENTS
4508 , &leader_len, &leader_flags, do_comments
4509#endif
4510 );
4511 else
4512 is_not_par = TRUE;
4513 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4514#ifdef FEAT_COMMENTS
4515 , &next_leader_len, &next_leader_flags, do_comments
4516#endif
4517 );
4518 is_end_par = (is_not_par || next_is_not_par);
4519 if (!is_end_par && do_trail_white)
4520 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4521
4522 curwin->w_cursor.lnum--;
4523 for (count = line_count; count != 0 && !got_int; --count)
4524 {
4525 /*
4526 * Advance to next paragraph.
4527 */
4528 if (advance)
4529 {
4530 curwin->w_cursor.lnum++;
4531 prev_is_end_par = is_end_par;
4532 is_not_par = next_is_not_par;
4533#ifdef FEAT_COMMENTS
4534 leader_len = next_leader_len;
4535 leader_flags = next_leader_flags;
4536#endif
4537 }
4538
4539 /*
4540 * The last line to be formatted.
4541 */
4542 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4543 {
4544 next_is_not_par = TRUE;
4545#ifdef FEAT_COMMENTS
4546 next_leader_len = 0;
4547 next_leader_flags = NULL;
4548#endif
4549 }
4550 else
4551 {
4552 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4553#ifdef FEAT_COMMENTS
4554 , &next_leader_len, &next_leader_flags, do_comments
4555#endif
4556 );
4557 if (do_number_indent)
4558 next_is_start_par =
4559 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4560 }
4561 advance = TRUE;
4562 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4563 if (!is_end_par && do_trail_white)
4564 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4565
4566 /*
4567 * Skip lines that are not in a paragraph.
4568 */
4569 if (is_not_par)
4570 {
4571 if (line_count < 0)
4572 break;
4573 }
4574 else
4575 {
4576 /*
4577 * For the first line of a paragraph, check indent of second line.
4578 * Don't do this for comments and empty lines.
4579 */
4580 if (first_par_line
4581 && (do_second_indent || do_number_indent)
4582 && prev_is_end_par
4583 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4584#ifdef FEAT_COMMENTS
4585 && leader_len == 0
4586 && next_leader_len == 0
4587#endif
4588 )
4589 {
4590 if (do_second_indent
4591 && !lineempty(curwin->w_cursor.lnum + 1))
4592 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4593 else if (do_number_indent)
4594 second_indent = get_number_indent(curwin->w_cursor.lnum);
4595 }
4596
4597 /*
4598 * When the comment leader changes, it's the end of the paragraph.
4599 */
4600 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4601#ifdef FEAT_COMMENTS
4602 || !same_leader(curwin->w_cursor.lnum,
4603 leader_len, leader_flags,
4604 next_leader_len, next_leader_flags)
4605#endif
4606 )
4607 is_end_par = TRUE;
4608
4609 /*
4610 * If we have got to the end of a paragraph, or the line is
4611 * getting long, format it.
4612 */
4613 if (is_end_par || force_format)
4614 {
4615 if (need_set_indent)
4616 /* replace indent in first line with minimal number of
4617 * tabs and spaces, according to current options */
4618 (void)set_indent(get_indent(), SIN_CHANGED);
4619
4620 /* put cursor on last non-space */
4621 State = NORMAL; /* don't go past end-of-line */
4622 coladvance((colnr_T)MAXCOL);
4623 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4624 dec_cursor();
4625
4626 /* do the formatting, without 'showmode' */
4627 State = INSERT; /* for open_line() */
4628 smd_save = p_smd;
4629 p_smd = FALSE;
4630 insertchar(NUL, INSCHAR_FORMAT
4631#ifdef FEAT_COMMENTS
4632 + (do_comments ? INSCHAR_DO_COM : 0)
4633#endif
4634 , second_indent);
4635 State = old_State;
4636 p_smd = smd_save;
4637 second_indent = -1;
4638 /* at end of par.: need to set indent of next par. */
4639 need_set_indent = is_end_par;
4640 if (is_end_par)
4641 {
4642 /* When called with a negative line count, break at the
4643 * end of the paragraph. */
4644 if (line_count < 0)
4645 break;
4646 first_par_line = TRUE;
4647 }
4648 force_format = FALSE;
4649 }
4650
4651 /*
4652 * When still in same paragraph, join the lines together. But
4653 * first delete the comment leader from the second line.
4654 */
4655 if (!is_end_par)
4656 {
4657 advance = FALSE;
4658 curwin->w_cursor.lnum++;
4659 curwin->w_cursor.col = 0;
4660 if (line_count < 0 && u_save_cursor() == FAIL)
4661 break;
4662#ifdef FEAT_COMMENTS
Bram Moolenaare3226be2005-12-18 22:10:00 +00004663 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 if (next_leader_len > 0)
4665 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4666 (long)-next_leader_len);
4667#endif
4668 curwin->w_cursor.lnum--;
4669 if (do_join(TRUE) == FAIL)
4670 {
4671 beep_flush();
4672 break;
4673 }
4674 first_par_line = FALSE;
4675 /* If the line is getting long, format it next time */
4676 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4677 force_format = TRUE;
4678 else
4679 force_format = FALSE;
4680 }
4681 }
4682 line_breakcheck();
4683 }
4684}
4685
4686/*
4687 * Return TRUE if line "lnum" ends in a white character.
4688 */
4689 static int
4690ends_in_white(lnum)
4691 linenr_T lnum;
4692{
4693 char_u *s = ml_get(lnum);
4694 size_t l;
4695
4696 if (*s == NUL)
4697 return FALSE;
4698 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4699 * invocation may call function multiple times". */
4700 l = STRLEN(s) - 1;
4701 return vim_iswhite(s[l]);
4702}
4703
4704/*
4705 * Blank lines, and lines containing only the comment leader, are left
4706 * untouched by the formatting. The function returns TRUE in this
4707 * case. It also returns TRUE when a line starts with the end of a comment
4708 * ('e' in comment flags), so that this line is skipped, and not joined to the
4709 * previous line. A new paragraph starts after a blank line, or when the
4710 * comment leader changes -- webb.
4711 */
4712#ifdef FEAT_COMMENTS
4713 static int
4714fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4715 linenr_T lnum;
4716 int *leader_len;
4717 char_u **leader_flags;
4718 int do_comments;
4719{
4720 char_u *flags = NULL; /* init for GCC */
4721 char_u *ptr;
4722
4723 ptr = ml_get(lnum);
4724 if (do_comments)
4725 *leader_len = get_leader_len(ptr, leader_flags, FALSE);
4726 else
4727 *leader_len = 0;
4728
4729 if (*leader_len > 0)
4730 {
4731 /*
4732 * Search for 'e' flag in comment leader flags.
4733 */
4734 flags = *leader_flags;
4735 while (*flags && *flags != ':' && *flags != COM_END)
4736 ++flags;
4737 }
4738
4739 return (*skipwhite(ptr + *leader_len) == NUL
4740 || (*leader_len > 0 && *flags == COM_END)
4741 || startPS(lnum, NUL, FALSE));
4742}
4743#else
4744 static int
4745fmt_check_par(lnum)
4746 linenr_T lnum;
4747{
4748 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4749}
4750#endif
4751
4752/*
4753 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4754 * previous line is in the same paragraph. Used for auto-formatting.
4755 */
4756 int
4757paragraph_start(lnum)
4758 linenr_T lnum;
4759{
4760 char_u *p;
4761#ifdef FEAT_COMMENTS
4762 int leader_len = 0; /* leader len of current line */
4763 char_u *leader_flags = NULL; /* flags for leader of current line */
4764 int next_leader_len; /* leader len of next line */
4765 char_u *next_leader_flags; /* flags for leader of next line */
4766 int do_comments; /* format comments */
4767#endif
4768
4769 if (lnum <= 1)
4770 return TRUE; /* start of the file */
4771
4772 p = ml_get(lnum - 1);
4773 if (*p == NUL)
4774 return TRUE; /* after empty line */
4775
4776#ifdef FEAT_COMMENTS
4777 do_comments = has_format_option(FO_Q_COMS);
4778#endif
4779 if (fmt_check_par(lnum - 1
4780#ifdef FEAT_COMMENTS
4781 , &leader_len, &leader_flags, do_comments
4782#endif
4783 ))
4784 return TRUE; /* after non-paragraph line */
4785
4786 if (fmt_check_par(lnum
4787#ifdef FEAT_COMMENTS
4788 , &next_leader_len, &next_leader_flags, do_comments
4789#endif
4790 ))
4791 return TRUE; /* "lnum" is not a paragraph line */
4792
4793 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4794 return TRUE; /* missing trailing space in previous line. */
4795
4796 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4797 return TRUE; /* numbered item starts in "lnum". */
4798
4799#ifdef FEAT_COMMENTS
4800 if (!same_leader(lnum - 1, leader_len, leader_flags,
4801 next_leader_len, next_leader_flags))
4802 return TRUE; /* change of comment leader. */
4803#endif
4804
4805 return FALSE;
4806}
4807
4808#ifdef FEAT_VISUAL
4809/*
4810 * prepare a few things for block mode yank/delete/tilde
4811 *
4812 * for delete:
4813 * - textlen includes the first/last char to be (partly) deleted
4814 * - start/endspaces is the number of columns that are taken by the
4815 * first/last deleted char minus the number of columns that have to be
4816 * deleted. for yank and tilde:
4817 * - textlen includes the first/last char to be wholly yanked
4818 * - start/endspaces is the number of columns of the first/last yanked char
4819 * that are to be yanked.
4820 */
4821 static void
4822block_prep(oap, bdp, lnum, is_del)
4823 oparg_T *oap;
4824 struct block_def *bdp;
4825 linenr_T lnum;
4826 int is_del;
4827{
4828 int incr = 0;
4829 char_u *pend;
4830 char_u *pstart;
4831 char_u *line;
4832 char_u *prev_pstart;
4833 char_u *prev_pend;
4834
4835 bdp->startspaces = 0;
4836 bdp->endspaces = 0;
4837 bdp->textlen = 0;
4838 bdp->start_vcol = 0;
4839 bdp->end_vcol = 0;
4840#ifdef FEAT_VISUALEXTRA
4841 bdp->is_short = FALSE;
4842 bdp->is_oneChar = FALSE;
4843 bdp->pre_whitesp = 0;
4844 bdp->pre_whitesp_c = 0;
4845 bdp->end_char_vcols = 0;
4846#endif
4847 bdp->start_char_vcols = 0;
4848
4849 line = ml_get(lnum);
4850 pstart = line;
4851 prev_pstart = line;
4852 while (bdp->start_vcol < oap->start_vcol && *pstart)
4853 {
4854 /* Count a tab for what it's worth (if list mode not on) */
4855 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4856 bdp->start_vcol += incr;
4857#ifdef FEAT_VISUALEXTRA
4858 if (vim_iswhite(*pstart))
4859 {
4860 bdp->pre_whitesp += incr;
4861 bdp->pre_whitesp_c++;
4862 }
4863 else
4864 {
4865 bdp->pre_whitesp = 0;
4866 bdp->pre_whitesp_c = 0;
4867 }
4868#endif
4869 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004870 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 bdp->start_char_vcols = incr;
4873 if (bdp->start_vcol < oap->start_vcol) /* line too short */
4874 {
4875 bdp->end_vcol = bdp->start_vcol;
4876#ifdef FEAT_VISUALEXTRA
4877 bdp->is_short = TRUE;
4878#endif
4879 if (!is_del || oap->op_type == OP_APPEND)
4880 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4881 }
4882 else
4883 {
4884 /* notice: this converts partly selected Multibyte characters to
4885 * spaces, too. */
4886 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4887 if (is_del && bdp->startspaces)
4888 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4889 pend = pstart;
4890 bdp->end_vcol = bdp->start_vcol;
4891 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
4892 {
4893#ifdef FEAT_VISUALEXTRA
4894 bdp->is_oneChar = TRUE;
4895#endif
4896 if (oap->op_type == OP_INSERT)
4897 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4898 else if (oap->op_type == OP_APPEND)
4899 {
4900 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4901 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4902 }
4903 else
4904 {
4905 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4906 if (is_del && oap->op_type != OP_LSHIFT)
4907 {
4908 /* just putting the sum of those two into
4909 * bdp->startspaces doesn't work for Visual replace,
4910 * so we have to split the tab in two */
4911 bdp->startspaces = bdp->start_char_vcols
4912 - (bdp->start_vcol - oap->start_vcol);
4913 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4914 }
4915 }
4916 }
4917 else
4918 {
4919 prev_pend = pend;
4920 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4921 {
4922 /* Count a tab for what it's worth (if list mode not on) */
4923 prev_pend = pend;
4924 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4925 bdp->end_vcol += incr;
4926 }
4927 if (bdp->end_vcol <= oap->end_vcol
4928 && (!is_del
4929 || oap->op_type == OP_APPEND
4930 || oap->op_type == OP_REPLACE)) /* line too short */
4931 {
4932#ifdef FEAT_VISUALEXTRA
4933 bdp->is_short = TRUE;
4934#endif
4935 /* Alternative: include spaces to fill up the block.
4936 * Disadvantage: can lead to trailing spaces when the line is
4937 * short where the text is put */
4938 /* if (!is_del || oap->op_type == OP_APPEND) */
4939 if (oap->op_type == OP_APPEND || virtual_op)
4940 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004941 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 else
4943 bdp->endspaces = 0; /* replace doesn't add characters */
4944 }
4945 else if (bdp->end_vcol > oap->end_vcol)
4946 {
4947 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4948 if (!is_del && bdp->endspaces)
4949 {
4950 bdp->endspaces = incr - bdp->endspaces;
4951 if (pend != pstart)
4952 pend = prev_pend;
4953 }
4954 }
4955 }
4956#ifdef FEAT_VISUALEXTRA
4957 bdp->end_char_vcols = incr;
4958#endif
4959 if (is_del && bdp->startspaces)
4960 pstart = prev_pstart;
4961 bdp->textlen = (int)(pend - pstart);
4962 }
4963 bdp->textcol = (colnr_T) (pstart - line);
4964 bdp->textstart = pstart;
4965}
4966#endif /* FEAT_VISUAL */
4967
4968#ifdef FEAT_RIGHTLEFT
4969static void reverse_line __ARGS((char_u *s));
4970
4971 static void
4972reverse_line(s)
4973 char_u *s;
4974{
4975 int i, j;
4976 char_u c;
4977
4978 if ((i = (int)STRLEN(s) - 1) <= 0)
4979 return;
4980
4981 curwin->w_cursor.col = i - curwin->w_cursor.col;
4982 for (j = 0; j < i; j++, i--)
4983 {
4984 c = s[i]; s[i] = s[j]; s[j] = c;
4985 }
4986}
4987
4988# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4989#else
4990# define RLADDSUBFIX(ptr)
4991#endif
4992
4993/*
4994 * add or subtract 'Prenum1' from a number in a line
4995 * 'command' is CTRL-A for add, CTRL-X for subtract
4996 *
4997 * return FAIL for failure, OK otherwise
4998 */
4999 int
5000do_addsub(command, Prenum1)
5001 int command;
5002 linenr_T Prenum1;
5003{
5004 int col;
5005 char_u *buf1;
5006 char_u buf2[NUMBUFLEN];
5007 int hex; /* 'X' or 'x': hex; '0': octal */
5008 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005009 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 long_u oldn;
5011 char_u *ptr;
5012 int c;
5013 int length = 0; /* character length of the number */
5014 int todel;
5015 int dohex;
5016 int dooct;
5017 int doalp;
5018 int firstdigit;
5019 int negative;
5020 int subtract;
5021
5022 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5023 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5024 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5025
5026 ptr = ml_get_curline();
5027 RLADDSUBFIX(ptr);
5028
5029 /*
5030 * First check if we are on a hexadecimal number, after the "0x".
5031 */
5032 col = curwin->w_cursor.col;
5033 if (dohex)
5034 while (col > 0 && vim_isxdigit(ptr[col]))
5035 --col;
5036 if ( dohex
5037 && col > 0
5038 && (ptr[col] == 'X'
5039 || ptr[col] == 'x')
5040 && ptr[col - 1] == '0'
5041 && vim_isxdigit(ptr[col + 1]))
5042 {
5043 /*
5044 * Found hexadecimal number, move to its start.
5045 */
5046 --col;
5047 }
5048 else
5049 {
5050 /*
5051 * Search forward and then backward to find the start of number.
5052 */
5053 col = curwin->w_cursor.col;
5054
5055 while (ptr[col] != NUL
5056 && !vim_isdigit(ptr[col])
5057 && !(doalp && ASCII_ISALPHA(ptr[col])))
5058 ++col;
5059
5060 while (col > 0
5061 && vim_isdigit(ptr[col - 1])
5062 && !(doalp && ASCII_ISALPHA(ptr[col])))
5063 --col;
5064 }
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 /*
5067 * If a number was found, and saving for undo works, replace the number.
5068 */
5069 firstdigit = ptr[col];
5070 RLADDSUBFIX(ptr);
5071 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5072 || u_save_cursor() != OK)
5073 {
5074 beep_flush();
5075 return FAIL;
5076 }
5077
5078 /* get ptr again, because u_save() may have changed it */
5079 ptr = ml_get_curline();
5080 RLADDSUBFIX(ptr);
5081
5082 if (doalp && ASCII_ISALPHA(firstdigit))
5083 {
5084 /* decrement or increment alphabetic character */
5085 if (command == Ctrl_X)
5086 {
5087 if (CharOrd(firstdigit) < Prenum1)
5088 {
5089 if (isupper(firstdigit))
5090 firstdigit = 'A';
5091 else
5092 firstdigit = 'a';
5093 }
5094 else
5095#ifdef EBCDIC
5096 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5097#else
5098 firstdigit -= Prenum1;
5099#endif
5100 }
5101 else
5102 {
5103 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5104 {
5105 if (isupper(firstdigit))
5106 firstdigit = 'Z';
5107 else
5108 firstdigit = 'z';
5109 }
5110 else
5111#ifdef EBCDIC
5112 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5113#else
5114 firstdigit += Prenum1;
5115#endif
5116 }
5117 curwin->w_cursor.col = col;
5118 (void)del_char(FALSE);
5119 ins_char(firstdigit);
5120 }
5121 else
5122 {
5123 negative = FALSE;
5124 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5125 {
5126 --col;
5127 negative = TRUE;
5128 }
5129
5130 /* get the number value (unsigned) */
5131 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5132
5133 /* ignore leading '-' for hex and octal numbers */
5134 if (hex && negative)
5135 {
5136 ++col;
5137 --length;
5138 negative = FALSE;
5139 }
5140
5141 /* add or subtract */
5142 subtract = FALSE;
5143 if (command == Ctrl_X)
5144 subtract ^= TRUE;
5145 if (negative)
5146 subtract ^= TRUE;
5147
5148 oldn = n;
5149 if (subtract)
5150 n -= (unsigned long)Prenum1;
5151 else
5152 n += (unsigned long)Prenum1;
5153
5154 /* handle wraparound for decimal numbers */
5155 if (!hex)
5156 {
5157 if (subtract)
5158 {
5159 if (n > oldn)
5160 {
5161 n = 1 + (n ^ (unsigned long)-1);
5162 negative ^= TRUE;
5163 }
5164 }
5165 else /* add */
5166 {
5167 if (n < oldn)
5168 {
5169 n = (n ^ (unsigned long)-1);
5170 negative ^= TRUE;
5171 }
5172 }
5173 if (n == 0)
5174 negative = FALSE;
5175 }
5176
5177 /*
5178 * Delete the old number.
5179 */
5180 curwin->w_cursor.col = col;
5181 todel = length;
5182 c = gchar_cursor();
5183 /*
5184 * Don't include the '-' in the length, only the length of the part
5185 * after it is kept the same.
5186 */
5187 if (c == '-')
5188 --length;
5189 while (todel-- > 0)
5190 {
5191 if (c < 0x100 && isalpha(c))
5192 {
5193 if (isupper(c))
5194 hexupper = TRUE;
5195 else
5196 hexupper = FALSE;
5197 }
5198 /* del_char() will mark line needing displaying */
5199 (void)del_char(FALSE);
5200 c = gchar_cursor();
5201 }
5202
5203 /*
5204 * Prepare the leading characters in buf1[].
5205 * When there are many leading zeros it could be very long. Allocate
5206 * a bit too much.
5207 */
5208 buf1 = alloc((unsigned)length + NUMBUFLEN);
5209 if (buf1 == NULL)
5210 return FAIL;
5211 ptr = buf1;
5212 if (negative)
5213 {
5214 *ptr++ = '-';
5215 }
5216 if (hex)
5217 {
5218 *ptr++ = '0';
5219 --length;
5220 }
5221 if (hex == 'x' || hex == 'X')
5222 {
5223 *ptr++ = hex;
5224 --length;
5225 }
5226
5227 /*
5228 * Put the number characters in buf2[].
5229 */
5230 if (hex == 0)
5231 sprintf((char *)buf2, "%lu", n);
5232 else if (hex == '0')
5233 sprintf((char *)buf2, "%lo", n);
5234 else if (hex && hexupper)
5235 sprintf((char *)buf2, "%lX", n);
5236 else
5237 sprintf((char *)buf2, "%lx", n);
5238 length -= (int)STRLEN(buf2);
5239
5240 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005241 * Adjust number of zeros to the new number of digits, so the
5242 * total length of the number remains the same.
5243 * Don't do this when
5244 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005246 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 while (length-- > 0)
5248 *ptr++ = '0';
5249 *ptr = NUL;
5250 STRCAT(buf1, buf2);
5251 ins_str(buf1); /* insert the new number */
5252 vim_free(buf1);
5253 }
5254 --curwin->w_cursor.col;
5255 curwin->w_set_curswant = TRUE;
5256#ifdef FEAT_RIGHTLEFT
5257 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5258 RLADDSUBFIX(ptr);
5259#endif
5260 return OK;
5261}
5262
5263#ifdef FEAT_VIMINFO
5264 int
5265read_viminfo_register(virp, force)
5266 vir_T *virp;
5267 int force;
5268{
5269 int eof;
5270 int do_it = TRUE;
5271 int size;
5272 int limit;
5273 int i;
5274 int set_prev = FALSE;
5275 char_u *str;
5276 char_u **array = NULL;
5277
5278 /* We only get here (hopefully) if line[0] == '"' */
5279 str = virp->vir_line + 1;
5280 if (*str == '"')
5281 {
5282 set_prev = TRUE;
5283 str++;
5284 }
5285 if (!ASCII_ISALNUM(*str) && *str != '-')
5286 {
5287 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5288 return TRUE; /* too many errors, pretend end-of-file */
5289 do_it = FALSE;
5290 }
5291 get_yank_register(*str++, FALSE);
5292 if (!force && y_current->y_array != NULL)
5293 do_it = FALSE;
5294 size = 0;
5295 limit = 100; /* Optimized for registers containing <= 100 lines */
5296 if (do_it)
5297 {
5298 if (set_prev)
5299 y_previous = y_current;
5300 vim_free(y_current->y_array);
5301 array = y_current->y_array =
5302 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5303 str = skipwhite(str);
5304 if (STRNCMP(str, "CHAR", 4) == 0)
5305 y_current->y_type = MCHAR;
5306#ifdef FEAT_VISUAL
5307 else if (STRNCMP(str, "BLOCK", 5) == 0)
5308 y_current->y_type = MBLOCK;
5309#endif
5310 else
5311 y_current->y_type = MLINE;
5312 /* get the block width; if it's missing we get a zero, which is OK */
5313 str = skipwhite(skiptowhite(str));
5314#ifdef FEAT_VISUAL
5315 y_current->y_width = getdigits(&str);
5316#else
5317 (void)getdigits(&str);
5318#endif
5319 }
5320
5321 while (!(eof = viminfo_readline(virp))
5322 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5323 {
5324 if (do_it)
5325 {
5326 if (size >= limit)
5327 {
5328 y_current->y_array = (char_u **)
5329 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5330 for (i = 0; i < limit; i++)
5331 y_current->y_array[i] = array[i];
5332 vim_free(array);
5333 limit *= 2;
5334 array = y_current->y_array;
5335 }
5336 str = viminfo_readstring(virp, 1, TRUE);
5337 if (str != NULL)
5338 array[size++] = str;
5339 else
5340 do_it = FALSE;
5341 }
5342 }
5343 if (do_it)
5344 {
5345 if (size == 0)
5346 {
5347 vim_free(array);
5348 y_current->y_array = NULL;
5349 }
5350 else if (size < limit)
5351 {
5352 y_current->y_array =
5353 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5354 for (i = 0; i < size; i++)
5355 y_current->y_array[i] = array[i];
5356 vim_free(array);
5357 }
5358 y_current->y_size = size;
5359 }
5360 return eof;
5361}
5362
5363 void
5364write_viminfo_registers(fp)
5365 FILE *fp;
5366{
5367 int i, j;
5368 char_u *type;
5369 char_u c;
5370 int num_lines;
5371 int max_num_lines;
5372 int max_kbyte;
5373 long len;
5374
5375 fprintf(fp, _("\n# Registers:\n"));
5376
5377 /* Get '<' value, use old '"' value if '<' is not found. */
5378 max_num_lines = get_viminfo_parameter('<');
5379 if (max_num_lines < 0)
5380 max_num_lines = get_viminfo_parameter('"');
5381 if (max_num_lines == 0)
5382 return;
5383 max_kbyte = get_viminfo_parameter('s');
5384 if (max_kbyte == 0)
5385 return;
5386 for (i = 0; i < NUM_REGISTERS; i++)
5387 {
5388 if (y_regs[i].y_array == NULL)
5389 continue;
5390#ifdef FEAT_CLIPBOARD
5391 /* Skip '*'/'+' register, we don't want them back next time */
5392 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5393 continue;
5394#endif
5395#ifdef FEAT_DND
5396 /* Neither do we want the '~' register */
5397 if (i == TILDE_REGISTER)
5398 continue;
5399#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005400 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005402 if (num_lines == 0
5403 || (num_lines == 1 && y_regs[i].y_type == MCHAR
5404 && STRLEN(y_regs[i].y_array[0]) == 0))
5405 continue;
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (max_kbyte > 0)
5408 {
5409 /* Skip register if there is more text than the maximum size. */
5410 len = 0;
5411 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005412 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (len > (long)max_kbyte * 1024L)
5414 continue;
5415 }
5416
5417 switch (y_regs[i].y_type)
5418 {
5419 case MLINE:
5420 type = (char_u *)"LINE";
5421 break;
5422 case MCHAR:
5423 type = (char_u *)"CHAR";
5424 break;
5425#ifdef FEAT_VISUAL
5426 case MBLOCK:
5427 type = (char_u *)"BLOCK";
5428 break;
5429#endif
5430 default:
5431 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005432 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 emsg(IObuff);
5434 type = (char_u *)"LINE";
5435 break;
5436 }
5437 if (y_previous == &y_regs[i])
5438 fprintf(fp, "\"");
5439 c = get_register_name(i);
5440 fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5441#ifdef FEAT_VISUAL
5442 (int)y_regs[i].y_width
5443#else
5444 0
5445#endif
5446 );
5447
5448 /* If max_num_lines < 0, then we save ALL the lines in the register */
5449 if (max_num_lines > 0 && num_lines > max_num_lines)
5450 num_lines = max_num_lines;
5451 for (j = 0; j < num_lines; j++)
5452 {
5453 putc('\t', fp);
5454 viminfo_writestring(fp, y_regs[i].y_array[j]);
5455 }
5456 }
5457}
5458#endif /* FEAT_VIMINFO */
5459
5460#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5461/*
5462 * SELECTION / PRIMARY ('*')
5463 *
5464 * Text selection stuff that uses the GUI selection register '*'. When using a
5465 * GUI this may be text from another window, otherwise it is the last text we
5466 * had highlighted with VIsual mode. With mouse support, clicking the middle
5467 * button performs the paste, otherwise you will need to do <"*p>. "
5468 * If not under X, it is synonymous with the clipboard register '+'.
5469 *
5470 * X CLIPBOARD ('+')
5471 *
5472 * Text selection stuff that uses the GUI clipboard register '+'.
5473 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5474 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5475 * otherwise you will need to do <"+p>. "
5476 * If not under X, it is synonymous with the selection register '*'.
5477 */
5478
5479/*
5480 * Routine to export any final X selection we had to the environment
5481 * so that the text is still available after vim has exited. X selections
5482 * only exist while the owning application exists, so we write to the
5483 * permanent (while X runs) store CUT_BUFFER0.
5484 * Dump the CLIPBOARD selection if we own it (it's logically the more
5485 * 'permanent' of the two), otherwise the PRIMARY one.
5486 * For now, use a hard-coded sanity limit of 1Mb of data.
5487 */
5488#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5489 void
5490x11_export_final_selection()
5491{
5492 Display *dpy;
5493 char_u *str = NULL;
5494 long_u len = 0;
5495 int motion_type = -1;
5496
5497# ifdef FEAT_GUI
5498 if (gui.in_use)
5499 dpy = X_DISPLAY;
5500 else
5501# endif
5502# ifdef FEAT_XCLIPBOARD
5503 dpy = xterm_dpy;
5504# else
5505 return;
5506# endif
5507
5508 /* Get selection to export */
5509 if (clip_plus.owned)
5510 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5511 else if (clip_star.owned)
5512 motion_type = clip_convert_selection(&str, &len, &clip_star);
5513
5514 /* Check it's OK */
5515 if (dpy != NULL && str != NULL && motion_type >= 0
5516 && len < 1024*1024 && len > 0)
5517 {
5518 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5519 XFlush(dpy);
5520 }
5521
5522 vim_free(str);
5523}
5524#endif
5525
5526 void
5527clip_free_selection(cbd)
5528 VimClipboard *cbd;
5529{
5530 struct yankreg *y_ptr = y_current;
5531
5532 if (cbd == &clip_plus)
5533 y_current = &y_regs[PLUS_REGISTER];
5534 else
5535 y_current = &y_regs[STAR_REGISTER];
5536 free_yank_all();
5537 y_current->y_size = 0;
5538 y_current = y_ptr;
5539}
5540
5541/*
5542 * Get the selected text and put it in the gui selection register '*' or '+'.
5543 */
5544 void
5545clip_get_selection(cbd)
5546 VimClipboard *cbd;
5547{
5548 struct yankreg *old_y_previous, *old_y_current;
5549 pos_T old_cursor;
5550#ifdef FEAT_VISUAL
5551 pos_T old_visual;
5552 int old_visual_mode;
5553#endif
5554 colnr_T old_curswant;
5555 int old_set_curswant;
5556 pos_T old_op_start, old_op_end;
5557 oparg_T oa;
5558 cmdarg_T ca;
5559
5560 if (cbd->owned)
5561 {
5562 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5563 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5564 return;
5565
5566 /* Get the text between clip_star.start & clip_star.end */
5567 old_y_previous = y_previous;
5568 old_y_current = y_current;
5569 old_cursor = curwin->w_cursor;
5570 old_curswant = curwin->w_curswant;
5571 old_set_curswant = curwin->w_set_curswant;
5572 old_op_start = curbuf->b_op_start;
5573 old_op_end = curbuf->b_op_end;
5574#ifdef FEAT_VISUAL
5575 old_visual = VIsual;
5576 old_visual_mode = VIsual_mode;
5577#endif
5578 clear_oparg(&oa);
5579 oa.regname = (cbd == &clip_plus ? '+' : '*');
5580 oa.op_type = OP_YANK;
5581 vim_memset(&ca, 0, sizeof(ca));
5582 ca.oap = &oa;
5583 ca.cmdchar = 'y';
5584 ca.count1 = 1;
5585 ca.retval = CA_NO_ADJ_OP_END;
5586 do_pending_operator(&ca, 0, TRUE);
5587 y_previous = old_y_previous;
5588 y_current = old_y_current;
5589 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005590 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 curwin->w_curswant = old_curswant;
5592 curwin->w_set_curswant = old_set_curswant;
5593 curbuf->b_op_start = old_op_start;
5594 curbuf->b_op_end = old_op_end;
5595#ifdef FEAT_VISUAL
5596 VIsual = old_visual;
5597 VIsual_mode = old_visual_mode;
5598#endif
5599 }
5600 else
5601 {
5602 clip_free_selection(cbd);
5603
5604 /* Try to get selected text from another window */
5605 clip_gen_request_selection(cbd);
5606 }
5607}
5608
5609/* Convert from the GUI selection string into the '*'/'+' register */
5610 void
5611clip_yank_selection(type, str, len, cbd)
5612 int type;
5613 char_u *str;
5614 long len;
5615 VimClipboard *cbd;
5616{
5617 struct yankreg *y_ptr;
5618
5619 if (cbd == &clip_plus)
5620 y_ptr = &y_regs[PLUS_REGISTER];
5621 else
5622 y_ptr = &y_regs[STAR_REGISTER];
5623
5624 clip_free_selection(cbd);
5625
5626 str_to_reg(y_ptr, type, str, len, 0L);
5627}
5628
5629/*
5630 * Convert the '*'/'+' register into a GUI selection string returned in *str
5631 * with length *len.
5632 * Returns the motion type, or -1 for failure.
5633 */
5634 int
5635clip_convert_selection(str, len, cbd)
5636 char_u **str;
5637 long_u *len;
5638 VimClipboard *cbd;
5639{
5640 char_u *p;
5641 int lnum;
5642 int i, j;
5643 int_u eolsize;
5644 struct yankreg *y_ptr;
5645
5646 if (cbd == &clip_plus)
5647 y_ptr = &y_regs[PLUS_REGISTER];
5648 else
5649 y_ptr = &y_regs[STAR_REGISTER];
5650
5651#ifdef USE_CRNL
5652 eolsize = 2;
5653#else
5654 eolsize = 1;
5655#endif
5656
5657 *str = NULL;
5658 *len = 0;
5659 if (y_ptr->y_array == NULL)
5660 return -1;
5661
5662 for (i = 0; i < y_ptr->y_size; i++)
5663 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5664
5665 /*
5666 * Don't want newline character at end of last line if we're in MCHAR mode.
5667 */
5668 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5669 *len -= eolsize;
5670
5671 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5672 if (p == NULL)
5673 return -1;
5674 lnum = 0;
5675 for (i = 0, j = 0; i < (int)*len; i++, j++)
5676 {
5677 if (y_ptr->y_array[lnum][j] == '\n')
5678 p[i] = NUL;
5679 else if (y_ptr->y_array[lnum][j] == NUL)
5680 {
5681#ifdef USE_CRNL
5682 p[i++] = '\r';
5683#endif
5684#ifdef USE_CR
5685 p[i] = '\r';
5686#else
5687 p[i] = '\n';
5688#endif
5689 lnum++;
5690 j = -1;
5691 }
5692 else
5693 p[i] = y_ptr->y_array[lnum][j];
5694 }
5695 return y_ptr->y_type;
5696}
5697
5698
5699# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5700/*
5701 * If we have written to a clipboard register, send the text to the clipboard.
5702 */
5703 static void
5704may_set_selection()
5705{
5706 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5707 {
5708 clip_own_selection(&clip_star);
5709 clip_gen_set_selection(&clip_star);
5710 }
5711 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5712 {
5713 clip_own_selection(&clip_plus);
5714 clip_gen_set_selection(&clip_plus);
5715 }
5716}
5717# endif
5718
5719#endif /* FEAT_CLIPBOARD || PROTO */
5720
5721
5722#if defined(FEAT_DND) || defined(PROTO)
5723/*
5724 * Replace the contents of the '~' register with str.
5725 */
5726 void
5727dnd_yank_drag_data(str, len)
5728 char_u *str;
5729 long len;
5730{
5731 struct yankreg *curr;
5732
5733 curr = y_current;
5734 y_current = &y_regs[TILDE_REGISTER];
5735 free_yank_all();
5736 str_to_reg(y_current, MCHAR, str, len, 0L);
5737 y_current = curr;
5738}
5739#endif
5740
5741
5742#if defined(FEAT_EVAL) || defined(PROTO)
5743/*
5744 * Return the type of a register.
5745 * Used for getregtype()
5746 * Returns MAUTO for error.
5747 */
5748 char_u
5749get_reg_type(regname, reglen)
5750 int regname;
5751 long *reglen;
5752{
5753 switch (regname)
5754 {
5755 case '%': /* file name */
5756 case '#': /* alternate file name */
5757 case '=': /* expression */
5758 case ':': /* last command line */
5759 case '/': /* last search-pattern */
5760 case '.': /* last inserted text */
5761#ifdef FEAT_SEARCHPATH
5762 case Ctrl_F: /* Filename under cursor */
5763 case Ctrl_P: /* Path under cursor, expand via "path" */
5764#endif
5765 case Ctrl_W: /* word under cursor */
5766 case Ctrl_A: /* WORD (mnemonic All) under cursor */
5767 case '_': /* black hole: always empty */
5768 return MCHAR;
5769 }
5770
5771#ifdef FEAT_CLIPBOARD
5772 regname = may_get_selection(regname);
5773#endif
5774
5775 /* Should we check for a valid name? */
5776 get_yank_register(regname, FALSE);
5777
5778 if (y_current->y_array != NULL)
5779 {
5780#ifdef FEAT_VISUAL
5781 if (reglen != NULL && y_current->y_type == MBLOCK)
5782 *reglen = y_current->y_width;
5783#endif
5784 return y_current->y_type;
5785 }
5786 return MAUTO;
5787}
5788
5789/*
5790 * Return the contents of a register as a single allocated string.
5791 * Used for "@r" in expressions and for getreg().
5792 * Returns NULL for error.
5793 */
5794 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00005795get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00005797 int allowexpr; /* allow "=" register */
5798 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 long i;
5801 char_u *retval;
5802 int allocated;
5803 long len;
5804
5805 /* Don't allow using an expression register inside an expression */
5806 if (regname == '=')
5807 {
5808 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00005809 {
5810 if (expr_src)
5811 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00005813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 return NULL;
5815 }
5816
5817 if (regname == '@') /* "@@" is used for unnamed register */
5818 regname = '"';
5819
5820 /* check for valid regname */
5821 if (regname != NUL && !valid_yank_reg(regname, FALSE))
5822 return NULL;
5823
5824#ifdef FEAT_CLIPBOARD
5825 regname = may_get_selection(regname);
5826#endif
5827
5828 if (get_spec_reg(regname, &retval, &allocated, FALSE))
5829 {
5830 if (retval == NULL)
5831 return NULL;
5832 if (!allocated)
5833 retval = vim_strsave(retval);
5834 return retval;
5835 }
5836
5837 get_yank_register(regname, FALSE);
5838 if (y_current->y_array == NULL)
5839 return NULL;
5840
5841 /*
5842 * Compute length of resulting string.
5843 */
5844 len = 0;
5845 for (i = 0; i < y_current->y_size; ++i)
5846 {
5847 len += (long)STRLEN(y_current->y_array[i]);
5848 /*
5849 * Insert a newline between lines and after last line if
5850 * y_type is MLINE.
5851 */
5852 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5853 ++len;
5854 }
5855
5856 retval = lalloc(len + 1, TRUE);
5857
5858 /*
5859 * Copy the lines of the yank register into the string.
5860 */
5861 if (retval != NULL)
5862 {
5863 len = 0;
5864 for (i = 0; i < y_current->y_size; ++i)
5865 {
5866 STRCPY(retval + len, y_current->y_array[i]);
5867 len += (long)STRLEN(retval + len);
5868
5869 /*
5870 * Insert a NL between lines and after the last line if y_type is
5871 * MLINE.
5872 */
5873 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5874 retval[len++] = '\n';
5875 }
5876 retval[len] = NUL;
5877 }
5878
5879 return retval;
5880}
5881
5882/*
5883 * Store string "str" in register "name".
5884 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5885 * If "must_append" is TRUE, always append to the register. Otherwise append
5886 * if "name" is an uppercase letter.
5887 * Note: "maxlen" and "must_append" don't work for the "/" register.
5888 * Careful: 'str' is modified, you may have to use a copy!
5889 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5890 */
5891 void
5892write_reg_contents(name, str, maxlen, must_append)
5893 int name;
5894 char_u *str;
5895 int maxlen;
5896 int must_append;
5897{
5898 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5899}
5900
5901 void
5902write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5903 int name;
5904 char_u *str;
5905 int maxlen;
5906 int must_append;
5907 int yank_type;
5908 long block_len;
5909{
5910 struct yankreg *old_y_previous, *old_y_current;
5911 long len;
5912
Bram Moolenaare7566042005-06-17 22:00:15 +00005913 if (maxlen >= 0)
5914 len = maxlen;
5915 else
5916 len = (long)STRLEN(str);
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 /* Special case: '/' search pattern */
5919 if (name == '/')
5920 {
5921 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5922 return;
5923 }
5924
Bram Moolenaare7566042005-06-17 22:00:15 +00005925#ifdef FEAT_EVAL
5926 if (name == '=')
5927 {
5928 char_u *p, *s;
5929
5930 p = vim_strnsave(str, (int)len);
5931 if (p == NULL)
5932 return;
5933 if (must_append)
5934 {
5935 s = concat_str(get_expr_line_src(), p);
5936 vim_free(p);
5937 p = s;
5938
5939 }
5940 set_expr_line(p);
5941 return;
5942 }
5943#endif
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
5946 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005947 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 return;
5949 }
5950
5951 if (name == '_') /* black hole: nothing to do */
5952 return;
5953
5954 /* Don't want to change the current (unnamed) register */
5955 old_y_previous = y_previous;
5956 old_y_current = y_current;
5957
5958 get_yank_register(name, TRUE);
5959 if (!y_append && !must_append)
5960 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961#ifndef FEAT_VISUAL
5962 /* Just in case - make sure we don't use MBLOCK */
5963 if (yank_type == MBLOCK)
5964 yank_type = MAUTO;
5965#endif
5966 if (yank_type == MAUTO)
5967 yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5968 ? MLINE : MCHAR);
5969 str_to_reg(y_current, yank_type, str, len, block_len);
5970
5971# ifdef FEAT_CLIPBOARD
5972 /* Send text of clipboard register to the clipboard. */
5973 may_set_selection();
5974# endif
5975
5976 /* ':let @" = "val"' should change the meaning of the "" register */
5977 if (name != '"')
5978 y_previous = old_y_previous;
5979 y_current = old_y_current;
5980}
5981#endif /* FEAT_EVAL */
5982
5983#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5984/*
5985 * Put a string into a register. When the register is not empty, the string
5986 * is appended.
5987 */
5988 static void
5989str_to_reg(y_ptr, type, str, len, blocklen)
5990 struct yankreg *y_ptr; /* pointer to yank register */
5991 int type; /* MCHAR, MLINE or MBLOCK */
5992 char_u *str; /* string to put in register */
5993 long len; /* length of string */
5994 long blocklen; /* width of Visual block */
5995{
5996 int lnum;
5997 long start;
5998 long i;
5999 int extra;
6000 int newlines; /* number of lines added */
6001 int extraline = 0; /* extra line at the end */
6002 int append = FALSE; /* append to last line in register */
6003 char_u *s;
6004 char_u **pp;
6005#ifdef FEAT_VISUAL
6006 long maxlen;
6007#endif
6008
6009 if (y_ptr->y_array == NULL) /* NULL means emtpy register */
6010 y_ptr->y_size = 0;
6011
6012 /*
6013 * Count the number of lines within the string
6014 */
6015 newlines = 0;
6016 for (i = 0; i < len; i++)
6017 if (str[i] == '\n')
6018 ++newlines;
6019 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6020 {
6021 extraline = 1;
6022 ++newlines; /* count extra newline at the end */
6023 }
6024 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6025 {
6026 append = TRUE;
6027 --newlines; /* uncount newline when appending first line */
6028 }
6029
6030 /*
6031 * Allocate an array to hold the pointers to the new register lines.
6032 * If the register was not empty, move the existing lines to the new array.
6033 */
6034 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6035 * sizeof(char_u *), TRUE);
6036 if (pp == NULL) /* out of memory */
6037 return;
6038 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6039 pp[lnum] = y_ptr->y_array[lnum];
6040 vim_free(y_ptr->y_array);
6041 y_ptr->y_array = pp;
6042#ifdef FEAT_VISUAL
6043 maxlen = 0;
6044#endif
6045
6046 /*
6047 * Find the end of each line and save it into the array.
6048 */
6049 for (start = 0; start < len + extraline; start += i + 1)
6050 {
6051 for (i = start; i < len; ++i) /* find the end of the line */
6052 if (str[i] == '\n')
6053 break;
6054 i -= start; /* i is now length of line */
6055#ifdef FEAT_VISUAL
6056 if (i > maxlen)
6057 maxlen = i;
6058#endif
6059 if (append)
6060 {
6061 --lnum;
6062 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6063 }
6064 else
6065 extra = 0;
6066 s = alloc((unsigned)(i + extra + 1));
6067 if (s == NULL)
6068 break;
6069 if (extra)
6070 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6071 if (append)
6072 vim_free(y_ptr->y_array[lnum]);
6073 if (i)
6074 mch_memmove(s + extra, str + start, (size_t)i);
6075 extra += i;
6076 s[extra] = NUL;
6077 y_ptr->y_array[lnum++] = s;
6078 while (--extra >= 0)
6079 {
6080 if (*s == NUL)
6081 *s = '\n'; /* replace NUL with newline */
6082 ++s;
6083 }
6084 append = FALSE; /* only first line is appended */
6085 }
6086 y_ptr->y_type = type;
6087 y_ptr->y_size = lnum;
6088# ifdef FEAT_VISUAL
6089 if (type == MBLOCK)
6090 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6091 else
6092 y_ptr->y_width = 0;
6093# endif
6094}
6095#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6096
6097 void
6098clear_oparg(oap)
6099 oparg_T *oap;
6100{
6101 vim_memset(oap, 0, sizeof(oparg_T));
6102}
6103
Bram Moolenaar7c626922005-02-07 22:01:03 +00006104static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006107 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 *
6109 * "Words" are counted by looking for boundaries between non-space and
6110 * space characters. (it seems to produce results that match 'wc'.)
6111 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006112 * Return value is byte count; word count for the line is added to "*wc".
6113 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 *
6115 * The function will only examine the first "limit" characters in the
6116 * line, stopping if it encounters an end-of-line (NUL byte). In that
6117 * case, eol_size will be added to the character count to account for
6118 * the size of the EOL character.
6119 */
6120 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006121line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 char_u *line;
6123 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006124 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 long limit;
6126 int eol_size;
6127{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006128 long i;
6129 long words = 0;
6130 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int is_word = 0;
6132
Bram Moolenaar7c626922005-02-07 22:01:03 +00006133 for (i = 0; line[i] && i < limit; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
6135 if (is_word)
6136 {
6137 if (vim_isspace(line[i]))
6138 {
6139 words++;
6140 is_word = 0;
6141 }
6142 }
6143 else if (!vim_isspace(line[i]))
6144 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006145 ++chars;
6146#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006147 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006148#else
6149 ++i;
6150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 }
6152
6153 if (is_word)
6154 words++;
6155 *wc += words;
6156
6157 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006158 if (line[i] == NUL && i < limit)
6159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006161 chars += eol_size;
6162 }
6163 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 return i;
6165}
6166
6167/*
6168 * Give some info about the position of the cursor (for "g CTRL-G").
6169 * In Visual mode, give some info about the selected region. (In this case,
6170 * the *_count_cursor variables store running totals for the selection.)
6171 */
6172 void
6173cursor_pos_info()
6174{
6175 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006176 char_u buf1[50];
6177 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006179 long byte_count = 0;
6180 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 long char_count = 0;
6182 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 long word_count = 0;
6184 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006185 int eol_size;
6186 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187#ifdef FEAT_VISUAL
6188 long line_count_selected = 0;
6189 pos_T min_pos, max_pos;
6190 oparg_T oparg;
6191 struct block_def bd;
6192#endif
6193
6194 /*
6195 * Compute the length of the file in characters.
6196 */
6197 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6198 {
6199 MSG(_(no_lines_msg));
6200 }
6201 else
6202 {
6203 if (get_fileformat(curbuf) == EOL_DOS)
6204 eol_size = 2;
6205 else
6206 eol_size = 1;
6207
6208#ifdef FEAT_VISUAL
6209 if (VIsual_active)
6210 {
6211 if (lt(VIsual, curwin->w_cursor))
6212 {
6213 min_pos = VIsual;
6214 max_pos = curwin->w_cursor;
6215 }
6216 else
6217 {
6218 min_pos = curwin->w_cursor;
6219 max_pos = VIsual;
6220 }
6221 if (*p_sel == 'e' && max_pos.col > 0)
6222 --max_pos.col;
6223
6224 if (VIsual_mode == Ctrl_V)
6225 {
6226 oparg.is_VIsual = 1;
6227 oparg.block_mode = TRUE;
6228 oparg.op_type = OP_NOP;
6229 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006230 &oparg.start_vcol, &oparg.end_vcol);
6231 if (curwin->w_curswant == MAXCOL)
6232 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 /* Swap the start, end vcol if needed */
6234 if (oparg.end_vcol < oparg.start_vcol)
6235 {
6236 oparg.end_vcol += oparg.start_vcol;
6237 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6238 oparg.end_vcol -= oparg.start_vcol;
6239 }
6240 }
6241 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6242 }
6243#endif
6244
6245 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6246 {
6247 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006248 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
6250 ui_breakcheck();
6251 if (got_int)
6252 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006253 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255
6256#ifdef FEAT_VISUAL
6257 /* Do extra processing for VIsual mode. */
6258 if (VIsual_active
6259 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6260 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006261 char_u *s = NULL;
6262 long len = 0L;
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 switch (VIsual_mode)
6265 {
6266 case Ctrl_V:
6267# ifdef FEAT_VIRTUALEDIT
6268 virtual_op = virtual_active();
6269# endif
6270 block_prep(&oparg, &bd, lnum, 0);
6271# ifdef FEAT_VIRTUALEDIT
6272 virtual_op = MAYBE;
6273# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006274 s = bd.textstart;
6275 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 break;
6277 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006278 s = ml_get(lnum);
6279 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 break;
6281 case 'v':
6282 {
6283 colnr_T start_col = (lnum == min_pos.lnum)
6284 ? min_pos.col : 0;
6285 colnr_T end_col = (lnum == max_pos.lnum)
6286 ? max_pos.col - start_col + 1 : MAXCOL;
6287
Bram Moolenaardef9e822004-12-31 20:58:58 +00006288 s = ml_get(lnum) + start_col;
6289 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 }
6291 break;
6292 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006293 if (s != NULL)
6294 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006295 byte_count_cursor += line_count_info(s, &word_count_cursor,
6296 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006297 if (lnum == curbuf->b_ml.ml_line_count
6298 && !curbuf->b_p_eol
6299 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006300 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006301 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 }
6304 else
6305#endif
6306 {
6307 /* In non-visual mode, check for the line the cursor is on */
6308 if (lnum == curwin->w_cursor.lnum)
6309 {
6310 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006311 char_count_cursor += char_count;
6312 byte_count_cursor = byte_count +
6313 line_count_info(ml_get(lnum),
6314 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 (long)(curwin->w_cursor.col + 1), eol_size);
6316 }
6317 }
6318 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006319 byte_count += line_count_info(ml_get(lnum), &word_count,
6320 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 }
6322
6323 /* Correction for when last line doesn't have an EOL. */
6324 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006325 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327#ifdef FEAT_VISUAL
6328 if (VIsual_active)
6329 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006330 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
6332 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006333 &max_pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 sprintf((char *)buf1, _("%ld Cols; "),
6335 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6336 }
6337 else
6338 buf1[0] = NUL;
6339
Bram Moolenaar7c626922005-02-07 22:01:03 +00006340 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006341 && char_count == byte_count)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006342 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 buf1, line_count_selected,
6344 (long)curbuf->b_ml.ml_line_count,
6345 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006346 byte_count_cursor, byte_count);
6347 else
6348 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6349 buf1, line_count_selected,
6350 (long)curbuf->b_ml.ml_line_count,
6351 word_count_cursor, word_count,
6352 char_count_cursor, char_count,
6353 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 }
6355 else
6356#endif
6357 {
6358 p = ml_get_curline();
6359 validate_virtcol();
6360 col_print(buf1, (int)curwin->w_cursor.col + 1,
6361 (int)curwin->w_virtcol + 1);
6362 col_print(buf2, (int)STRLEN(p), linetabsize(p));
6363
Bram Moolenaar7c626922005-02-07 22:01:03 +00006364 if (char_count_cursor == byte_count_cursor
6365 && char_count == byte_count)
6366 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 (char *)buf1, (char *)buf2,
6368 (long)curwin->w_cursor.lnum,
6369 (long)curbuf->b_ml.ml_line_count,
6370 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006371 byte_count_cursor, byte_count);
6372 else
6373 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6374 (char *)buf1, (char *)buf2,
6375 (long)curwin->w_cursor.lnum,
6376 (long)curbuf->b_ml.ml_line_count,
6377 word_count_cursor, word_count,
6378 char_count_cursor, char_count,
6379 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 }
6381
6382#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006383 byte_count = bomb_size();
6384 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006386 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387#endif
6388 /* Don't shorten this message, the user asked for it. */
6389 p = p_shm;
6390 p_shm = (char_u *)"";
6391 msg(IObuff);
6392 p_shm = p;
6393 }
6394}