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