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