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