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