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