Patch 7.0.207
[MacVim/jjgod.git] / src / ops.c
blob3fe69fc75e752243ef8b8f415ae50c71889eeb35
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
15 #include "vim.h"
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
27 * Symbolic names for some registers.
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
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
53 * Each yank register is an array of pointers to lines.
55 static struct yankreg
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];
65 static struct yankreg *y_current; /* ptr to current yankreg */
66 static int y_append; /* TRUE when appending */
67 static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
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
73 struct block_def
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 */
93 #ifdef FEAT_VISUALEXTRA
94 static void shift_block __ARGS((oparg_T *oap, int amount));
95 static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
96 #endif
97 static int stuff_yank __ARGS((int, char_u *));
98 static void put_reedit_in_typebuf __ARGS((int silent));
99 static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
100 int silent));
101 static void stuffescaped __ARGS((char_u *arg, int literally));
102 #ifdef FEAT_MBYTE
103 static void mb_adjust_opend __ARGS((oparg_T *oap));
104 #endif
105 static void free_yank __ARGS((long));
106 static void free_yank_all __ARGS((void));
107 static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
108 #ifdef FEAT_CLIPBOARD
109 static void copy_yank_reg __ARGS((struct yankreg *reg));
110 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
111 static void may_set_selection __ARGS((void));
112 # endif
113 #endif
114 static void dis_msg __ARGS((char_u *p, int skip_esc));
115 #ifdef FEAT_VISUAL
116 static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
117 #endif
118 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
119 static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
120 #endif
121 static int ends_in_white __ARGS((linenr_T lnum));
122 #ifdef FEAT_COMMENTS
123 static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
124 static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
125 #else
126 static int fmt_check_par __ARGS((linenr_T));
127 #endif
130 * The names of operators.
131 * IMPORTANT: Index must correspond with defines in vim.h!!!
132 * The third field indicates whether the operator always works on lines.
134 static char opchars[][3] =
136 {NUL, NUL, FALSE}, /* OP_NOP */
137 {'d', NUL, FALSE}, /* OP_DELETE */
138 {'y', NUL, FALSE}, /* OP_YANK */
139 {'c', NUL, FALSE}, /* OP_CHANGE */
140 {'<', NUL, TRUE}, /* OP_LSHIFT */
141 {'>', NUL, TRUE}, /* OP_RSHIFT */
142 {'!', NUL, TRUE}, /* OP_FILTER */
143 {'g', '~', FALSE}, /* OP_TILDE */
144 {'=', NUL, TRUE}, /* OP_INDENT */
145 {'g', 'q', TRUE}, /* OP_FORMAT */
146 {':', NUL, TRUE}, /* OP_COLON */
147 {'g', 'U', FALSE}, /* OP_UPPER */
148 {'g', 'u', FALSE}, /* OP_LOWER */
149 {'J', NUL, TRUE}, /* DO_JOIN */
150 {'g', 'J', TRUE}, /* DO_JOIN_NS */
151 {'g', '?', FALSE}, /* OP_ROT13 */
152 {'r', NUL, FALSE}, /* OP_REPLACE */
153 {'I', NUL, FALSE}, /* OP_INSERT */
154 {'A', NUL, FALSE}, /* OP_APPEND */
155 {'z', 'f', TRUE}, /* OP_FOLD */
156 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
157 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
158 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
159 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
160 {'z', 'd', TRUE}, /* OP_FOLDDEL */
161 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
162 {'g', 'w', TRUE}, /* OP_FORMAT2 */
163 {'g', '@', FALSE}, /* OP_FUNCTION */
167 * Translate a command name into an operator type.
168 * Must only be called with a valid operator name!
171 get_op_type(char1, char2)
172 int char1;
173 int char2;
175 int i;
177 if (char1 == 'r') /* ignore second character */
178 return OP_REPLACE;
179 if (char1 == '~') /* when tilde is an operator */
180 return OP_TILDE;
181 for (i = 0; ; ++i)
182 if (opchars[i][0] == char1 && opchars[i][1] == char2)
183 break;
184 return i;
187 #if defined(FEAT_VISUAL) || defined(PROTO)
189 * Return TRUE if operator "op" always works on whole lines.
192 op_on_lines(op)
193 int op;
195 return opchars[op][2];
197 #endif
200 * Get first operator command character.
201 * Returns 'g' or 'z' if there is another command character.
204 get_op_char(optype)
205 int optype;
207 return opchars[optype][0];
211 * Get second operator command character.
214 get_extra_op_char(optype)
215 int optype;
217 return opchars[optype][1];
221 * op_shift - handle a shift operation
223 void
224 op_shift(oap, curs_top, amount)
225 oparg_T *oap;
226 int curs_top;
227 int amount;
229 long i;
230 int first_char;
231 char_u *s;
232 #ifdef FEAT_VISUAL
233 int block_col = 0;
234 #endif
236 if (u_save((linenr_T)(oap->start.lnum - 1),
237 (linenr_T)(oap->end.lnum + 1)) == FAIL)
238 return;
240 #ifdef FEAT_VISUAL
241 if (oap->block_mode)
242 block_col = curwin->w_cursor.col;
243 #endif
245 for (i = oap->line_count; --i >= 0; )
247 first_char = *ml_get_curline();
248 if (first_char == NUL) /* empty line */
249 curwin->w_cursor.col = 0;
250 #ifdef FEAT_VISUALEXTRA
251 else if (oap->block_mode)
252 shift_block(oap, amount);
253 #endif
254 else
255 /* Move the line right if it doesn't start with '#', 'smartindent'
256 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
257 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
258 if (first_char != '#' || !preprocs_left())
259 #endif
261 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount);
263 ++curwin->w_cursor.lnum;
266 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
268 #ifdef FEAT_VISUAL
269 if (oap->block_mode)
271 curwin->w_cursor.lnum = oap->start.lnum;
272 curwin->w_cursor.col = block_col;
274 else
275 #endif
276 if (curs_top) /* put cursor on first line, for ">>" */
278 curwin->w_cursor.lnum = oap->start.lnum;
279 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
281 else
282 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
284 if (oap->line_count > p_report)
286 if (oap->op_type == OP_RSHIFT)
287 s = (char_u *)">";
288 else
289 s = (char_u *)"<";
290 if (oap->line_count == 1)
292 if (amount == 1)
293 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
294 else
295 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
297 else
299 if (amount == 1)
300 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
301 oap->line_count, s);
302 else
303 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
304 oap->line_count, s, amount);
306 msg(IObuff);
310 * Set "'[" and "']" marks.
312 curbuf->b_op_start = oap->start;
313 curbuf->b_op_end.lnum = oap->end.lnum;
314 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
315 if (curbuf->b_op_end.col > 0)
316 --curbuf->b_op_end.col;
320 * shift the current line one shiftwidth left (if left != 0) or right
321 * leaves cursor on first blank in the line
323 void
324 shift_line(left, round, amount)
325 int left;
326 int round;
327 int amount;
329 int count;
330 int i, j;
331 int p_sw = (int)curbuf->b_p_sw;
333 count = get_indent(); /* get current indent */
335 if (round) /* round off indent */
337 i = count / p_sw; /* number of p_sw rounded down */
338 j = count % p_sw; /* extra spaces */
339 if (j && left) /* first remove extra spaces */
340 --amount;
341 if (left)
343 i -= amount;
344 if (i < 0)
345 i = 0;
347 else
348 i += amount;
349 count = i * p_sw;
351 else /* original vi indent */
353 if (left)
355 count -= p_sw * amount;
356 if (count < 0)
357 count = 0;
359 else
360 count += p_sw * amount;
363 /* Set new indent */
364 #ifdef FEAT_VREPLACE
365 if (State & VREPLACE_FLAG)
366 change_indent(INDENT_SET, count, FALSE, NUL);
367 else
368 #endif
369 (void)set_indent(count, SIN_CHANGED);
372 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
374 * Shift one line of the current block one shiftwidth right or left.
375 * Leaves cursor on first character in block.
377 static void
378 shift_block(oap, amount)
379 oparg_T *oap;
380 int amount;
382 int left = (oap->op_type == OP_LSHIFT);
383 int oldstate = State;
384 int total, split;
385 char_u *newp, *oldp, *midp, *ptr;
386 int oldcol = curwin->w_cursor.col;
387 int p_sw = (int)curbuf->b_p_sw;
388 int p_ts = (int)curbuf->b_p_ts;
389 struct block_def bd;
390 int internal = 0;
391 int incr;
392 colnr_T vcol, col = 0, ws_vcol;
393 int i = 0, j = 0;
394 int len;
396 #ifdef FEAT_RIGHTLEFT
397 int old_p_ri = p_ri;
399 p_ri = 0; /* don't want revins in ident */
400 #endif
402 State = INSERT; /* don't want REPLACE for State */
403 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
404 if (bd.is_short)
405 return;
407 /* total is number of screen columns to be inserted/removed */
408 total = amount * p_sw;
409 oldp = ml_get_curline();
411 if (!left)
414 * 1. Get start vcol
415 * 2. Total ws vcols
416 * 3. Divvy into TABs & spp
417 * 4. Construct new string
419 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
420 ws_vcol = bd.start_vcol - bd.pre_whitesp;
421 if (bd.startspaces)
423 #ifdef FEAT_MBYTE
424 if (has_mbyte)
425 bd.textstart += (*mb_ptr2len)(bd.textstart);
426 #endif
427 ++bd.textstart;
429 for ( ; vim_iswhite(*bd.textstart); )
431 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
432 total += incr;
433 bd.start_vcol += incr;
435 /* OK, now total=all the VWS reqd, and textstart points at the 1st
436 * non-ws char in the block. */
437 if (!curbuf->b_p_et)
438 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
439 if (i)
440 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
441 else
442 j = total;
443 /* if we're splitting a TAB, allow for it */
444 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
445 len = (int)STRLEN(bd.textstart) + 1;
446 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
447 if (newp == NULL)
448 return;
449 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
450 mch_memmove(newp, oldp, (size_t)bd.textcol);
451 copy_chars(newp + bd.textcol, (size_t)i, TAB);
452 copy_spaces(newp + bd.textcol + i, (size_t)j);
453 /* the end */
454 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
456 else /* left */
458 vcol = oap->start_vcol;
459 /* walk vcol past ws to be removed */
460 for (midp = oldp + bd.textcol;
461 vcol < (oap->start_vcol + total) && vim_iswhite(*midp); )
463 incr = lbr_chartabsize_adv(&midp, (colnr_T)vcol);
464 vcol += incr;
466 /* internal is the block-internal ws replacing a split TAB */
467 if (vcol > (oap->start_vcol + total))
469 /* we have to split the TAB *(midp-1) */
470 internal = vcol - (oap->start_vcol + total);
472 /* if 'expandtab' is not set, use TABs */
474 split = bd.startspaces + internal;
475 if (split > 0)
477 if (!curbuf->b_p_et)
479 for (ptr = oldp, col = 0; ptr < oldp+bd.textcol; )
480 col += lbr_chartabsize_adv(&ptr, (colnr_T)col);
482 /* col+1 now equals the start col of the first char of the
483 * block (may be < oap.start_vcol if we're splitting a TAB) */
484 i = ((col % p_ts) + split) / p_ts; /* number of tabs */
486 if (i)
487 j = ((col % p_ts) + split) % p_ts; /* number of spp */
488 else
489 j = split;
492 newp = alloc_check(bd.textcol + i + j + (unsigned)STRLEN(midp) + 1);
493 if (newp == NULL)
494 return;
495 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + STRLEN(midp) + 1));
497 /* copy first part we want to keep */
498 mch_memmove(newp, oldp, (size_t)bd.textcol);
499 /* Now copy any TABS and spp to ensure correct alignment! */
500 while (vim_iswhite(*midp))
502 if (*midp == TAB)
503 i++;
504 else /*space */
505 j++;
506 midp++;
508 /* We might have an extra TAB worth of spp now! */
509 if (j / p_ts && !curbuf->b_p_et)
511 i++;
512 j -= p_ts;
514 copy_chars(newp + bd.textcol, (size_t)i, TAB);
515 copy_spaces(newp + bd.textcol + i, (size_t)j);
517 /* the end */
518 mch_memmove(newp + STRLEN(newp), midp, (size_t)STRLEN(midp) + 1);
520 /* replace the line */
521 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
522 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
523 State = oldstate;
524 curwin->w_cursor.col = oldcol;
525 #ifdef FEAT_RIGHTLEFT
526 p_ri = old_p_ri;
527 #endif
529 #endif
531 #ifdef FEAT_VISUALEXTRA
533 * Insert string "s" (b_insert ? before : after) block :AKelly
534 * Caller must prepare for undo.
536 static void
537 block_insert(oap, s, b_insert, bdp)
538 oparg_T *oap;
539 char_u *s;
540 int b_insert;
541 struct block_def *bdp;
543 int p_ts;
544 int count = 0; /* extra spaces to replace a cut TAB */
545 int spaces = 0; /* non-zero if cutting a TAB */
546 colnr_T offset; /* pointer along new line */
547 unsigned s_len; /* STRLEN(s) */
548 char_u *newp, *oldp; /* new, old lines */
549 linenr_T lnum; /* loop var */
550 int oldstate = State;
552 State = INSERT; /* don't want REPLACE for State */
553 s_len = (unsigned)STRLEN(s);
555 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
557 block_prep(oap, bdp, lnum, TRUE);
558 if (bdp->is_short && b_insert)
559 continue; /* OP_INSERT, line ends before block start */
561 oldp = ml_get(lnum);
563 if (b_insert)
565 p_ts = bdp->start_char_vcols;
566 spaces = bdp->startspaces;
567 if (spaces != 0)
568 count = p_ts - 1; /* we're cutting a TAB */
569 offset = bdp->textcol;
571 else /* append */
573 p_ts = bdp->end_char_vcols;
574 if (!bdp->is_short) /* spaces = padding after block */
576 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
577 if (spaces != 0)
578 count = p_ts - 1; /* we're cutting a TAB */
579 offset = bdp->textcol + bdp->textlen - (spaces != 0);
581 else /* spaces = padding to block edge */
583 /* if $ used, just append to EOL (ie spaces==0) */
584 if (!bdp->is_MAX)
585 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
586 count = spaces;
587 offset = bdp->textcol + bdp->textlen;
591 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
592 if (newp == NULL)
593 continue;
595 /* copy up to shifted part */
596 mch_memmove(newp, oldp, (size_t)(offset));
597 oldp += offset;
599 /* insert pre-padding */
600 copy_spaces(newp + offset, (size_t)spaces);
602 /* copy the new text */
603 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
604 offset += s_len;
606 if (spaces && !bdp->is_short)
608 /* insert post-padding */
609 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
610 /* We're splitting a TAB, don't copy it. */
611 oldp++;
612 /* We allowed for that TAB, remember this now */
613 count++;
616 if (spaces > 0)
617 offset += count;
618 mch_memmove(newp + offset, oldp, (size_t)(STRLEN(oldp) + 1));
620 ml_replace(lnum, newp, FALSE);
622 if (lnum == oap->end.lnum)
624 /* Set "']" mark to the end of the block instead of the end of
625 * the insert in the first line. */
626 curbuf->b_op_end.lnum = oap->end.lnum;
627 curbuf->b_op_end.col = offset;
629 } /* for all lnum */
631 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
633 State = oldstate;
635 #endif
637 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
639 * op_reindent - handle reindenting a block of lines.
641 void
642 op_reindent(oap, how)
643 oparg_T *oap;
644 int (*how) __ARGS((void));
646 long i;
647 char_u *l;
648 int count;
649 linenr_T first_changed = 0;
650 linenr_T last_changed = 0;
651 linenr_T start_lnum = curwin->w_cursor.lnum;
653 /* Don't even try when 'modifiable' is off. */
654 if (!curbuf->b_p_ma)
656 EMSG(_(e_modifiable));
657 return;
660 for (i = oap->line_count; --i >= 0 && !got_int; )
662 /* it's a slow thing to do, so give feedback so there's no worry that
663 * the computer's just hung. */
665 if (i > 1
666 && (i % 50 == 0 || i == oap->line_count - 1)
667 && oap->line_count > p_report)
668 smsg((char_u *)_("%ld lines to indent... "), i);
671 * Be vi-compatible: For lisp indenting the first line is not
672 * indented, unless there is only one line.
674 #ifdef FEAT_LISP
675 if (i != oap->line_count - 1 || oap->line_count == 1
676 || how != get_lisp_indent)
677 #endif
679 l = skipwhite(ml_get_curline());
680 if (*l == NUL) /* empty or blank line */
681 count = 0;
682 else
683 count = how(); /* get the indent for this line */
685 if (set_indent(count, SIN_UNDO))
687 /* did change the indent, call changed_lines() later */
688 if (first_changed == 0)
689 first_changed = curwin->w_cursor.lnum;
690 last_changed = curwin->w_cursor.lnum;
693 ++curwin->w_cursor.lnum;
696 /* put cursor on first non-blank of indented line */
697 curwin->w_cursor.lnum = start_lnum;
698 beginline(BL_SOL | BL_FIX);
700 /* Mark changed lines so that they will be redrawn. When Visual
701 * highlighting was present, need to continue until the last line. When
702 * there is no change still need to remove the Visual highlighting. */
703 if (last_changed != 0)
704 changed_lines(first_changed, 0,
705 #ifdef FEAT_VISUAL
706 oap->is_VIsual ? start_lnum + oap->line_count :
707 #endif
708 last_changed + 1, 0L);
709 #ifdef FEAT_VISUAL
710 else if (oap->is_VIsual)
711 redraw_curbuf_later(INVERTED);
712 #endif
714 if (oap->line_count > p_report)
716 i = oap->line_count - (i + 1);
717 if (i == 1)
718 MSG(_("1 line indented "));
719 else
720 smsg((char_u *)_("%ld lines indented "), i);
722 /* set '[ and '] marks */
723 curbuf->b_op_start = oap->start;
724 curbuf->b_op_end = oap->end;
726 #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
728 #if defined(FEAT_EVAL) || defined(PROTO)
730 * Keep the last expression line here, for repeating.
732 static char_u *expr_line = NULL;
735 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
736 * Returns '=' when OK, NUL otherwise.
739 get_expr_register()
741 char_u *new_line;
743 new_line = getcmdline('=', 0L, 0);
744 if (new_line == NULL)
745 return NUL;
746 if (*new_line == NUL) /* use previous line */
747 vim_free(new_line);
748 else
749 set_expr_line(new_line);
750 return '=';
754 * Set the expression for the '=' register.
755 * Argument must be an allocated string.
757 void
758 set_expr_line(new_line)
759 char_u *new_line;
761 vim_free(expr_line);
762 expr_line = new_line;
766 * Get the result of the '=' register expression.
767 * Returns a pointer to allocated memory, or NULL for failure.
769 char_u *
770 get_expr_line()
772 char_u *expr_copy;
773 char_u *rv;
774 static int nested = 0;
776 if (expr_line == NULL)
777 return NULL;
779 /* Make a copy of the expression, because evaluating it may cause it to be
780 * changed. */
781 expr_copy = vim_strsave(expr_line);
782 if (expr_copy == NULL)
783 return NULL;
785 /* When we are invoked recursively limit the evaluation to 10 levels.
786 * Then return the string as-is. */
787 if (nested >= 10)
788 return expr_copy;
790 ++nested;
791 rv = eval_to_string(expr_copy, NULL, TRUE);
792 --nested;
793 vim_free(expr_copy);
794 return rv;
798 * Get the '=' register expression itself, without evaluating it.
800 char_u *
801 get_expr_line_src()
803 if (expr_line == NULL)
804 return NULL;
805 return vim_strsave(expr_line);
807 #endif /* FEAT_EVAL */
810 * Check if 'regname' is a valid name of a yank register.
811 * Note: There is no check for 0 (default register), caller should do this
814 valid_yank_reg(regname, writing)
815 int regname;
816 int writing; /* if TRUE check for writable registers */
818 if ( (regname > 0 && ASCII_ISALNUM(regname))
819 || (!writing && vim_strchr((char_u *)
820 #ifdef FEAT_EVAL
821 "/.%#:="
822 #else
823 "/.%#:"
824 #endif
825 , regname) != NULL)
826 || regname == '"'
827 || regname == '-'
828 || regname == '_'
829 #ifdef FEAT_CLIPBOARD
830 || regname == '*'
831 || regname == '+'
832 #endif
833 #ifdef FEAT_DND
834 || (!writing && regname == '~')
835 #endif
837 return TRUE;
838 return FALSE;
842 * Set y_current and y_append, according to the value of "regname".
843 * Cannot handle the '_' register.
844 * Must only be called with a valid register name!
846 * If regname is 0 and writing, use register 0
847 * If regname is 0 and reading, use previous register
849 void
850 get_yank_register(regname, writing)
851 int regname;
852 int writing;
854 int i;
856 y_append = FALSE;
857 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
859 y_current = y_previous;
860 return;
862 i = regname;
863 if (VIM_ISDIGIT(i))
864 i -= '0';
865 else if (ASCII_ISLOWER(i))
866 i = CharOrdLow(i) + 10;
867 else if (ASCII_ISUPPER(i))
869 i = CharOrdUp(i) + 10;
870 y_append = TRUE;
872 else if (regname == '-')
873 i = DELETION_REGISTER;
874 #ifdef FEAT_CLIPBOARD
875 /* When selection is not available, use register 0 instead of '*' */
876 else if (clip_star.available && regname == '*')
877 i = STAR_REGISTER;
878 /* When clipboard is not available, use register 0 instead of '+' */
879 else if (clip_plus.available && regname == '+')
880 i = PLUS_REGISTER;
881 #endif
882 #ifdef FEAT_DND
883 else if (!writing && regname == '~')
884 i = TILDE_REGISTER;
885 #endif
886 else /* not 0-9, a-z, A-Z or '-': use register 0 */
887 i = 0;
888 y_current = &(y_regs[i]);
889 if (writing) /* remember the register we write into for do_put() */
890 y_previous = y_current;
893 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
895 * When "regname" is a clipboard register, obtain the selection. If it's not
896 * available return zero, otherwise return "regname".
899 may_get_selection(regname)
900 int regname;
902 if (regname == '*')
904 if (!clip_star.available)
905 regname = 0;
906 else
907 clip_get_selection(&clip_star);
909 else if (regname == '+')
911 if (!clip_plus.available)
912 regname = 0;
913 else
914 clip_get_selection(&clip_plus);
916 return regname;
918 #endif
920 #if defined(FEAT_VISUAL) || defined(PROTO)
922 * Obtain the contents of a "normal" register. The register is made empty.
923 * The returned pointer has allocated memory, use put_register() later.
925 void *
926 get_register(name, copy)
927 int name;
928 int copy; /* make a copy, if FALSE make register empty. */
930 static struct yankreg *reg;
931 int i;
933 #ifdef FEAT_CLIPBOARD
934 /* When Visual area changed, may have to update selection. Obtain the
935 * selection too. */
936 if (name == '*' && clip_star.available && clip_isautosel())
938 clip_update_selection();
939 may_get_selection(name);
941 #endif
943 get_yank_register(name, 0);
944 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
945 if (reg != NULL)
947 *reg = *y_current;
948 if (copy)
950 /* If we run out of memory some or all of the lines are empty. */
951 if (reg->y_size == 0)
952 reg->y_array = NULL;
953 else
954 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
955 * reg->y_size));
956 if (reg->y_array != NULL)
958 for (i = 0; i < reg->y_size; ++i)
959 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
962 else
963 y_current->y_array = NULL;
965 return (void *)reg;
969 * Put "reg" into register "name". Free any previous contents.
971 void
972 put_register(name, reg)
973 int name;
974 void *reg;
976 get_yank_register(name, 0);
977 free_yank_all();
978 *y_current = *(struct yankreg *)reg;
980 # ifdef FEAT_CLIPBOARD
981 /* Send text written to clipboard register to the clipboard. */
982 may_set_selection();
983 # endif
985 #endif
987 #if defined(FEAT_MOUSE) || defined(PROTO)
989 * return TRUE if the current yank register has type MLINE
992 yank_register_mline(regname)
993 int regname;
995 if (regname != 0 && !valid_yank_reg(regname, FALSE))
996 return FALSE;
997 if (regname == '_') /* black hole is always empty */
998 return FALSE;
999 get_yank_register(regname, FALSE);
1000 return (y_current->y_type == MLINE);
1002 #endif
1005 * start or stop recording into a yank register
1007 * return FAIL for failure, OK otherwise
1010 do_record(c)
1011 int c;
1013 char_u *p;
1014 static int regname;
1015 struct yankreg *old_y_previous, *old_y_current;
1016 int retval;
1018 if (Recording == FALSE) /* start recording */
1020 /* registers 0-9, a-z and " are allowed */
1021 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1022 retval = FAIL;
1023 else
1025 Recording = TRUE;
1026 showmode();
1027 regname = c;
1028 retval = OK;
1031 else /* stop recording */
1034 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, so
1035 * that the register can be put into the typeahead buffer without
1036 * translation.
1038 Recording = FALSE;
1039 MSG("");
1040 p = get_recorded();
1041 if (p == NULL)
1042 retval = FAIL;
1043 else
1045 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1046 vim_unescape_csi(p);
1049 * We don't want to change the default register here, so save and
1050 * restore the current register name.
1052 old_y_previous = y_previous;
1053 old_y_current = y_current;
1055 retval = stuff_yank(regname, p);
1057 y_previous = old_y_previous;
1058 y_current = old_y_current;
1061 return retval;
1065 * Stuff string "p" into yank register "regname" as a single line (append if
1066 * uppercase). "p" must have been alloced.
1068 * return FAIL for failure, OK otherwise
1070 static int
1071 stuff_yank(regname, p)
1072 int regname;
1073 char_u *p;
1075 char_u *lp;
1076 char_u **pp;
1078 /* check for read-only register */
1079 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1081 vim_free(p);
1082 return FAIL;
1084 if (regname == '_') /* black hole: don't do anything */
1086 vim_free(p);
1087 return OK;
1089 get_yank_register(regname, TRUE);
1090 if (y_append && y_current->y_array != NULL)
1092 pp = &(y_current->y_array[y_current->y_size - 1]);
1093 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1094 if (lp == NULL)
1096 vim_free(p);
1097 return FAIL;
1099 STRCPY(lp, *pp);
1100 STRCAT(lp, p);
1101 vim_free(p);
1102 vim_free(*pp);
1103 *pp = lp;
1105 else
1107 free_yank_all();
1108 if ((y_current->y_array =
1109 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1111 vim_free(p);
1112 return FAIL;
1114 y_current->y_array[0] = p;
1115 y_current->y_size = 1;
1116 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1118 return OK;
1122 * execute a yank register: copy it into the stuff buffer
1124 * return FAIL for failure, OK otherwise
1127 do_execreg(regname, colon, addcr, silent)
1128 int regname;
1129 int colon; /* insert ':' before each line */
1130 int addcr; /* always add '\n' to end of line */
1131 int silent; /* set "silent" flag in typeahead buffer */
1133 static int lastc = NUL;
1134 long i;
1135 char_u *p;
1136 int retval = OK;
1137 int remap;
1139 if (regname == '@') /* repeat previous one */
1141 if (lastc == NUL)
1143 EMSG(_("E748: No previously used register"));
1144 return FAIL;
1146 regname = lastc;
1148 /* check for valid regname */
1149 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
1151 emsg_invreg(regname);
1152 return FAIL;
1154 lastc = regname;
1156 #ifdef FEAT_CLIPBOARD
1157 regname = may_get_selection(regname);
1158 #endif
1160 if (regname == '_') /* black hole: don't stuff anything */
1161 return OK;
1163 #ifdef FEAT_CMDHIST
1164 if (regname == ':') /* use last command line */
1166 if (last_cmdline == NULL)
1168 EMSG(_(e_nolastcmd));
1169 return FAIL;
1171 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1172 new_last_cmdline = NULL;
1173 /* Escape all control characters with a CTRL-V */
1174 p = vim_strsave_escaped_ext(last_cmdline,
1175 (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);
1176 if (p != NULL)
1178 /* When in Visual mode "'<,'>" will be prepended to the command.
1179 * Remove it when it's already there. */
1180 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
1181 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
1182 else
1183 retval = put_in_typebuf(p, TRUE, TRUE, silent);
1185 vim_free(p);
1187 #endif
1188 #ifdef FEAT_EVAL
1189 else if (regname == '=')
1191 p = get_expr_line();
1192 if (p == NULL)
1193 return FAIL;
1194 retval = put_in_typebuf(p, TRUE, colon, silent);
1195 vim_free(p);
1197 #endif
1198 else if (regname == '.') /* use last inserted text */
1200 p = get_last_insert_save();
1201 if (p == NULL)
1203 EMSG(_(e_noinstext));
1204 return FAIL;
1206 retval = put_in_typebuf(p, FALSE, colon, silent);
1207 vim_free(p);
1209 else
1211 get_yank_register(regname, FALSE);
1212 if (y_current->y_array == NULL)
1213 return FAIL;
1215 /* Disallow remaping for ":@r". */
1216 remap = colon ? REMAP_NONE : REMAP_YES;
1219 * Insert lines into typeahead buffer, from last one to first one.
1221 put_reedit_in_typebuf(silent);
1222 for (i = y_current->y_size; --i >= 0; )
1224 char_u *escaped;
1226 /* insert NL between lines and after last line if type is MLINE */
1227 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1228 || addcr)
1230 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
1231 return FAIL;
1233 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1234 if (escaped == NULL)
1235 return FAIL;
1236 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1237 vim_free(escaped);
1238 if (retval == FAIL)
1239 return FAIL;
1240 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
1241 == FAIL)
1242 return FAIL;
1244 Exec_reg = TRUE; /* disable the 'q' command */
1246 return retval;
1250 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1251 * used only after other typeahead has been processed.
1253 static void
1254 put_reedit_in_typebuf(silent)
1255 int silent;
1257 char_u buf[3];
1259 if (restart_edit != NUL)
1261 if (restart_edit == 'V')
1263 buf[0] = 'g';
1264 buf[1] = 'R';
1265 buf[2] = NUL;
1267 else
1269 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1270 buf[1] = NUL;
1272 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
1273 restart_edit = NUL;
1277 static int
1278 put_in_typebuf(s, esc, colon, silent)
1279 char_u *s;
1280 int esc; /* Escape CSI characters */
1281 int colon; /* add ':' before the line */
1282 int silent;
1284 int retval = OK;
1286 put_reedit_in_typebuf(silent);
1287 if (colon)
1288 retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, silent);
1289 if (retval == OK)
1291 char_u *p;
1293 if (esc)
1294 p = vim_strsave_escape_csi(s);
1295 else
1296 p = s;
1297 if (p == NULL)
1298 retval = FAIL;
1299 else
1300 retval = ins_typebuf(p, REMAP_YES, 0, TRUE, silent);
1301 if (esc)
1302 vim_free(p);
1304 if (colon && retval == OK)
1305 retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, silent);
1306 return retval;
1310 * Insert a yank register: copy it into the Read buffer.
1311 * Used by CTRL-R command and middle mouse button in insert mode.
1313 * return FAIL for failure, OK otherwise
1316 insert_reg(regname, literally)
1317 int regname;
1318 int literally; /* insert literally, not as if typed */
1320 long i;
1321 int retval = OK;
1322 char_u *arg;
1323 int allocated;
1326 * It is possible to get into an endless loop by having CTRL-R a in
1327 * register a and then, in insert mode, doing CTRL-R a.
1328 * If you hit CTRL-C, the loop will be broken here.
1330 ui_breakcheck();
1331 if (got_int)
1332 return FAIL;
1334 /* check for valid regname */
1335 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1336 return FAIL;
1338 #ifdef FEAT_CLIPBOARD
1339 regname = may_get_selection(regname);
1340 #endif
1342 if (regname == '.') /* insert last inserted text */
1343 retval = stuff_inserted(NUL, 1L, TRUE);
1344 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1346 if (arg == NULL)
1347 return FAIL;
1348 stuffescaped(arg, literally);
1349 if (allocated)
1350 vim_free(arg);
1352 else /* name or number register */
1354 get_yank_register(regname, FALSE);
1355 if (y_current->y_array == NULL)
1356 retval = FAIL;
1357 else
1359 for (i = 0; i < y_current->y_size; ++i)
1361 stuffescaped(y_current->y_array[i], literally);
1363 * Insert a newline between lines and after last line if
1364 * y_type is MLINE.
1366 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1367 stuffcharReadbuff('\n');
1372 return retval;
1376 * Stuff a string into the typeahead buffer, such that edit() will insert it
1377 * literally ("literally" TRUE) or interpret is as typed characters.
1379 static void
1380 stuffescaped(arg, literally)
1381 char_u *arg;
1382 int literally;
1384 int c;
1385 char_u *start;
1387 while (*arg != NUL)
1389 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1390 * stuff K_SPECIAL to get the effect of a special key when "literally"
1391 * is TRUE. */
1392 start = arg;
1393 while ((*arg >= ' '
1394 #ifndef EBCDIC
1395 && *arg < DEL /* EBCDIC: chars above space are normal */
1396 #endif
1398 || (*arg == K_SPECIAL && !literally))
1399 ++arg;
1400 if (arg > start)
1401 stuffReadbuffLen(start, (long)(arg - start));
1403 /* stuff a single special character */
1404 if (*arg != NUL)
1406 #ifdef FEAT_MBYTE
1407 if (has_mbyte)
1408 c = mb_ptr2char_adv(&arg);
1409 else
1410 #endif
1411 c = *arg++;
1412 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1413 stuffcharReadbuff(Ctrl_V);
1414 stuffcharReadbuff(c);
1420 * If "regname" is a special register, return a pointer to its value.
1423 get_spec_reg(regname, argp, allocated, errmsg)
1424 int regname;
1425 char_u **argp;
1426 int *allocated;
1427 int errmsg; /* give error message when failing */
1429 int cnt;
1431 *argp = NULL;
1432 *allocated = FALSE;
1433 switch (regname)
1435 case '%': /* file name */
1436 if (errmsg)
1437 check_fname(); /* will give emsg if not set */
1438 *argp = curbuf->b_fname;
1439 return TRUE;
1441 case '#': /* alternate file name */
1442 *argp = getaltfname(errmsg); /* may give emsg if not set */
1443 return TRUE;
1445 #ifdef FEAT_EVAL
1446 case '=': /* result of expression */
1447 *argp = get_expr_line();
1448 *allocated = TRUE;
1449 return TRUE;
1450 #endif
1452 case ':': /* last command line */
1453 if (last_cmdline == NULL && errmsg)
1454 EMSG(_(e_nolastcmd));
1455 *argp = last_cmdline;
1456 return TRUE;
1458 case '/': /* last search-pattern */
1459 if (last_search_pat() == NULL && errmsg)
1460 EMSG(_(e_noprevre));
1461 *argp = last_search_pat();
1462 return TRUE;
1464 case '.': /* last inserted text */
1465 *argp = get_last_insert_save();
1466 *allocated = TRUE;
1467 if (*argp == NULL && errmsg)
1468 EMSG(_(e_noinstext));
1469 return TRUE;
1471 #ifdef FEAT_SEARCHPATH
1472 case Ctrl_F: /* Filename under cursor */
1473 case Ctrl_P: /* Path under cursor, expand via "path" */
1474 if (!errmsg)
1475 return FALSE;
1476 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
1477 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
1478 *allocated = TRUE;
1479 return TRUE;
1480 #endif
1482 case Ctrl_W: /* word under cursor */
1483 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1484 if (!errmsg)
1485 return FALSE;
1486 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1487 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1488 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1489 *allocated = TRUE;
1490 return TRUE;
1492 case '_': /* black hole: always empty */
1493 *argp = (char_u *)"";
1494 return TRUE;
1497 return FALSE;
1501 * Paste a yank register into the command line.
1502 * Only for non-special registers.
1503 * Used by CTRL-R command in command-line mode
1504 * insert_reg() can't be used here, because special characters from the
1505 * register contents will be interpreted as commands.
1507 * return FAIL for failure, OK otherwise
1510 cmdline_paste_reg(regname, literally, remcr)
1511 int regname;
1512 int literally; /* Insert text literally instead of "as typed" */
1513 int remcr; /* don't add trailing CR */
1515 long i;
1517 get_yank_register(regname, FALSE);
1518 if (y_current->y_array == NULL)
1519 return FAIL;
1521 for (i = 0; i < y_current->y_size; ++i)
1523 cmdline_paste_str(y_current->y_array[i], literally);
1525 /* Insert ^M between lines and after last line if type is MLINE.
1526 * Don't do this when "remcr" is TRUE and the next line is empty. */
1527 if (y_current->y_type == MLINE
1528 || (i < y_current->y_size - 1
1529 && !(remcr
1530 && i == y_current->y_size - 2
1531 && *y_current->y_array[i + 1] == NUL)))
1532 cmdline_paste_str((char_u *)"\r", literally);
1534 /* Check for CTRL-C, in case someone tries to paste a few thousand
1535 * lines and gets bored. */
1536 ui_breakcheck();
1537 if (got_int)
1538 return FAIL;
1540 return OK;
1543 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
1545 * Adjust the register name pointed to with "rp" for the clipboard being
1546 * used always and the clipboard being available.
1548 void
1549 adjust_clip_reg(rp)
1550 int *rp;
1552 /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1553 if (*rp == 0 && clip_unnamed)
1554 *rp = '*';
1555 if (!clip_star.available && *rp == '*')
1556 *rp = 0;
1557 if (!clip_plus.available && *rp == '+')
1558 *rp = 0;
1560 #endif
1563 * op_delete - handle a delete operation
1565 * return FAIL if undo failed, OK otherwise.
1568 op_delete(oap)
1569 oparg_T *oap;
1571 int n;
1572 linenr_T lnum;
1573 char_u *ptr;
1574 #ifdef FEAT_VISUAL
1575 char_u *newp, *oldp;
1576 struct block_def bd;
1577 #endif
1578 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1579 int did_yank = FALSE;
1581 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1582 return OK;
1584 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1585 if (oap->empty)
1586 return u_save_cursor();
1588 if (!curbuf->b_p_ma)
1590 EMSG(_(e_modifiable));
1591 return FAIL;
1594 #ifdef FEAT_CLIPBOARD
1595 adjust_clip_reg(&oap->regname);
1596 #endif
1598 #ifdef FEAT_MBYTE
1599 if (has_mbyte)
1600 mb_adjust_opend(oap);
1601 #endif
1604 * Imitate the strange Vi behaviour: If the delete spans more than one line
1605 * and motion_type == MCHAR and the result is a blank line, make the delete
1606 * linewise. Don't do this for the change command or Visual mode.
1608 if ( oap->motion_type == MCHAR
1609 #ifdef FEAT_VISUAL
1610 && !oap->is_VIsual
1611 && !oap->block_mode
1612 #endif
1613 && oap->line_count > 1
1614 && oap->op_type == OP_DELETE)
1616 ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive;
1617 ptr = skipwhite(ptr);
1618 if (*ptr == NUL && inindent(0))
1619 oap->motion_type = MLINE;
1623 * Check for trying to delete (e.g. "D") in an empty line.
1624 * Note: For the change operator it is ok.
1626 if ( oap->motion_type == MCHAR
1627 && oap->line_count == 1
1628 && oap->op_type == OP_DELETE
1629 && *ml_get(oap->start.lnum) == NUL)
1632 * It's an error to operate on an empty region, when 'E' included in
1633 * 'cpoptions' (Vi compatible).
1635 #ifdef FEAT_VIRTUALEDIT
1636 if (virtual_op)
1637 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1638 * marks as if it happened. */
1639 goto setmarks;
1640 #endif
1641 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1642 beep_flush();
1643 return OK;
1647 * Do a yank of whatever we're about to delete.
1648 * If a yank register was specified, put the deleted text into that register.
1649 * For the black hole register '_' don't yank anything.
1651 if (oap->regname != '_')
1653 if (oap->regname != 0)
1655 /* check for read-only register */
1656 if (!valid_yank_reg(oap->regname, TRUE))
1658 beep_flush();
1659 return OK;
1661 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1662 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1663 did_yank = TRUE;
1667 * Put deleted text into register 1 and shift number registers if the
1668 * delete contains a line break, or when a regname has been specified.
1670 if (oap->regname != 0 || oap->motion_type == MLINE
1671 || oap->line_count > 1 || oap->use_reg_one)
1673 y_current = &y_regs[9];
1674 free_yank_all(); /* free register nine */
1675 for (n = 9; n > 1; --n)
1676 y_regs[n] = y_regs[n - 1];
1677 y_previous = y_current = &y_regs[1];
1678 y_regs[1].y_array = NULL; /* set register one to empty */
1679 if (op_yank(oap, TRUE, FALSE) == OK)
1680 did_yank = TRUE;
1683 /* Yank into small delete register when no register specified and the
1684 * delete is within one line. */
1685 if (oap->regname == 0 && oap->motion_type != MLINE
1686 && oap->line_count == 1)
1688 oap->regname = '-';
1689 get_yank_register(oap->regname, TRUE);
1690 if (op_yank(oap, TRUE, FALSE) == OK)
1691 did_yank = TRUE;
1692 oap->regname = 0;
1696 * If there's too much stuff to fit in the yank register, then get a
1697 * confirmation before doing the delete. This is crude, but simple.
1698 * And it avoids doing a delete of something we can't put back if we
1699 * want.
1701 if (!did_yank)
1703 int msg_silent_save = msg_silent;
1705 msg_silent = 0; /* must display the prompt */
1706 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1707 msg_silent = msg_silent_save;
1708 if (n != 'y')
1710 EMSG(_(e_abort));
1711 return FAIL;
1716 #ifdef FEAT_VISUAL
1718 * block mode delete
1720 if (oap->block_mode)
1722 if (u_save((linenr_T)(oap->start.lnum - 1),
1723 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1724 return FAIL;
1726 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1728 block_prep(oap, &bd, lnum, TRUE);
1729 if (bd.textlen == 0) /* nothing to delete */
1730 continue;
1732 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1733 if (lnum == curwin->w_cursor.lnum)
1735 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1736 # ifdef FEAT_VIRTUALEDIT
1737 curwin->w_cursor.coladd = 0;
1738 # endif
1741 /* n == number of chars deleted
1742 * If we delete a TAB, it may be replaced by several characters.
1743 * Thus the number of characters may increase!
1745 n = bd.textlen - bd.startspaces - bd.endspaces;
1746 oldp = ml_get(lnum);
1747 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1748 if (newp == NULL)
1749 continue;
1750 /* copy up to deleted part */
1751 mch_memmove(newp, oldp, (size_t)bd.textcol);
1752 /* insert spaces */
1753 copy_spaces(newp + bd.textcol,
1754 (size_t)(bd.startspaces + bd.endspaces));
1755 /* copy the part after the deleted part */
1756 oldp += bd.textcol + bd.textlen;
1757 mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
1758 oldp, STRLEN(oldp) + 1);
1759 /* replace the line */
1760 ml_replace(lnum, newp, FALSE);
1763 check_cursor_col();
1764 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1765 oap->end.lnum + 1, 0L);
1766 oap->line_count = 0; /* no lines deleted */
1768 else
1769 #endif
1770 if (oap->motion_type == MLINE)
1772 if (oap->op_type == OP_CHANGE)
1774 /* Delete the lines except the first one. Temporarily move the
1775 * cursor to the next line. Save the current line number, if the
1776 * last line is deleted it may be changed.
1778 if (oap->line_count > 1)
1780 lnum = curwin->w_cursor.lnum;
1781 ++curwin->w_cursor.lnum;
1782 del_lines((long)(oap->line_count - 1), TRUE);
1783 curwin->w_cursor.lnum = lnum;
1785 if (u_save_cursor() == FAIL)
1786 return FAIL;
1787 if (curbuf->b_p_ai) /* don't delete indent */
1789 beginline(BL_WHITE); /* cursor on first non-white */
1790 did_ai = TRUE; /* delete the indent when ESC hit */
1791 ai_col = curwin->w_cursor.col;
1793 else
1794 beginline(0); /* cursor in column 0 */
1795 truncate_line(FALSE); /* delete the rest of the line */
1796 /* leave cursor past last char in line */
1797 if (oap->line_count > 1)
1798 u_clearline(); /* "U" command not possible after "2cc" */
1800 else
1802 del_lines(oap->line_count, TRUE);
1803 beginline(BL_WHITE | BL_FIX);
1804 u_clearline(); /* "U" command not possible after "dd" */
1807 else
1809 #ifdef FEAT_VIRTUALEDIT
1810 if (virtual_op)
1812 int endcol = 0;
1814 /* For virtualedit: break the tabs that are partly included. */
1815 if (gchar_pos(&oap->start) == '\t')
1817 if (u_save_cursor() == FAIL) /* save first line for undo */
1818 return FAIL;
1819 if (oap->line_count == 1)
1820 endcol = getviscol2(oap->end.col, oap->end.coladd);
1821 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1822 oap->start = curwin->w_cursor;
1823 if (oap->line_count == 1)
1825 coladvance(endcol);
1826 oap->end.col = curwin->w_cursor.col;
1827 oap->end.coladd = curwin->w_cursor.coladd;
1828 curwin->w_cursor = oap->start;
1832 /* Break a tab only when it's included in the area. */
1833 if (gchar_pos(&oap->end) == '\t'
1834 && (int)oap->end.coladd < oap->inclusive)
1836 /* save last line for undo */
1837 if (u_save((linenr_T)(oap->end.lnum - 1),
1838 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1839 return FAIL;
1840 curwin->w_cursor = oap->end;
1841 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1842 oap->end = curwin->w_cursor;
1843 curwin->w_cursor = oap->start;
1846 #endif
1848 if (oap->line_count == 1) /* delete characters within one line */
1850 if (u_save_cursor() == FAIL) /* save line for undo */
1851 return FAIL;
1853 /* if 'cpoptions' contains '$', display '$' at end of change */
1854 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1855 && oap->op_type == OP_CHANGE
1856 && oap->end.lnum == curwin->w_cursor.lnum
1857 #ifdef FEAT_VISUAL
1858 && !oap->is_VIsual
1859 #endif
1861 display_dollar(oap->end.col - !oap->inclusive);
1863 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1865 #ifdef FEAT_VIRTUALEDIT
1866 if (virtual_op)
1868 /* fix up things for virtualedit-delete:
1869 * break the tabs which are going to get in our way
1871 char_u *curline = ml_get_curline();
1872 int len = (int)STRLEN(curline);
1874 if (oap->end.coladd != 0
1875 && (int)oap->end.col >= len - 1
1876 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1877 n++;
1878 /* Delete at least one char (e.g, when on a control char). */
1879 if (n == 0 && oap->start.coladd != oap->end.coladd)
1880 n = 1;
1882 /* When deleted a char in the line, reset coladd. */
1883 if (gchar_cursor() != NUL)
1884 curwin->w_cursor.coladd = 0;
1886 #endif
1887 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
1888 #ifdef FEAT_VISUAL
1889 && !oap->is_VIsual
1890 #endif
1893 else /* delete characters between lines */
1895 pos_T curpos;
1897 /* save deleted and changed lines for undo */
1898 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1899 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1900 return FAIL;
1902 truncate_line(TRUE); /* delete from cursor to end of line */
1904 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1905 ++curwin->w_cursor.lnum;
1906 del_lines((long)(oap->line_count - 2), FALSE);
1908 /* delete from start of line until op_end */
1909 curwin->w_cursor.col = 0;
1910 (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
1911 !virtual_op, oap->op_type == OP_DELETE
1912 #ifdef FEAT_VISUAL
1913 && !oap->is_VIsual
1914 #endif
1916 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1918 (void)do_join(FALSE);
1922 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1924 #ifdef FEAT_VIRTUALEDIT
1925 setmarks:
1926 #endif
1927 #ifdef FEAT_VISUAL
1928 if (oap->block_mode)
1930 curbuf->b_op_end.lnum = oap->end.lnum;
1931 curbuf->b_op_end.col = oap->start.col;
1933 else
1934 #endif
1935 curbuf->b_op_end = oap->start;
1936 curbuf->b_op_start = oap->start;
1938 return OK;
1941 #ifdef FEAT_MBYTE
1943 * Adjust end of operating area for ending on a multi-byte character.
1944 * Used for deletion.
1946 static void
1947 mb_adjust_opend(oap)
1948 oparg_T *oap;
1950 char_u *p;
1952 if (oap->inclusive)
1954 p = ml_get(oap->end.lnum);
1955 oap->end.col += mb_tail_off(p, p + oap->end.col);
1958 #endif
1960 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1962 * Replace a whole area with one character.
1965 op_replace(oap, c)
1966 oparg_T *oap;
1967 int c;
1969 int n, numc;
1970 #ifdef FEAT_MBYTE
1971 int num_chars;
1972 #endif
1973 char_u *newp, *oldp;
1974 size_t oldlen;
1975 struct block_def bd;
1977 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1978 return OK; /* nothing to do */
1980 #ifdef FEAT_MBYTE
1981 if (has_mbyte)
1982 mb_adjust_opend(oap);
1983 #endif
1985 if (u_save((linenr_T)(oap->start.lnum - 1),
1986 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1987 return FAIL;
1990 * block mode replace
1992 if (oap->block_mode)
1994 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1995 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1997 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1998 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1999 continue; /* nothing to replace */
2001 /* n == number of extra chars required
2002 * If we split a TAB, it may be replaced by several characters.
2003 * Thus the number of characters may increase!
2005 #ifdef FEAT_VIRTUALEDIT
2006 /* If the range starts in virtual space, count the initial
2007 * coladd offset as part of "startspaces" */
2008 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2010 pos_T vpos;
2012 getvpos(&vpos, oap->start_vcol);
2013 bd.startspaces += vpos.coladd;
2014 n = bd.startspaces;
2016 else
2017 #endif
2018 /* allow for pre spaces */
2019 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2021 /* allow for post spp */
2022 n += (bd.endspaces
2023 #ifdef FEAT_VIRTUALEDIT
2024 && !bd.is_oneChar
2025 #endif
2026 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2027 /* Figure out how many characters to replace. */
2028 numc = oap->end_vcol - oap->start_vcol + 1;
2029 if (bd.is_short && (!virtual_op || bd.is_MAX))
2030 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2032 #ifdef FEAT_MBYTE
2033 /* A double-wide character can be replaced only up to half the
2034 * times. */
2035 if ((*mb_char2cells)(c) > 1)
2037 if ((numc & 1) && !bd.is_short)
2039 ++bd.endspaces;
2040 ++n;
2042 numc = numc / 2;
2045 /* Compute bytes needed, move character count to num_chars. */
2046 num_chars = numc;
2047 numc *= (*mb_char2len)(c);
2048 #endif
2049 /* oldlen includes textlen, so don't double count */
2050 n += numc - bd.textlen;
2052 oldp = ml_get_curline();
2053 oldlen = STRLEN(oldp);
2054 newp = alloc_check((unsigned)oldlen + 1 + n);
2055 if (newp == NULL)
2056 continue;
2057 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2058 /* copy up to deleted part */
2059 mch_memmove(newp, oldp, (size_t)bd.textcol);
2060 oldp += bd.textcol + bd.textlen;
2061 /* insert pre-spaces */
2062 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2063 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2064 #ifdef FEAT_MBYTE
2065 if (has_mbyte)
2067 n = (int)STRLEN(newp);
2068 while (--num_chars >= 0)
2069 n += (*mb_char2bytes)(c, newp + n);
2071 else
2072 #endif
2073 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2074 if (!bd.is_short)
2076 /* insert post-spaces */
2077 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2078 /* copy the part after the changed part */
2079 mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1);
2081 /* replace the line */
2082 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2085 else
2088 * MCHAR and MLINE motion replace.
2090 if (oap->motion_type == MLINE)
2092 oap->start.col = 0;
2093 curwin->w_cursor.col = 0;
2094 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2095 if (oap->end.col)
2096 --oap->end.col;
2098 else if (!oap->inclusive)
2099 dec(&(oap->end));
2101 while (ltoreq(curwin->w_cursor, oap->end))
2103 n = gchar_cursor();
2104 if (n != NUL)
2106 #ifdef FEAT_MBYTE
2107 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2109 /* This is slow, but it handles replacing a single-byte
2110 * with a multi-byte and the other way around. */
2111 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2112 n = State;
2113 State = REPLACE;
2114 ins_char(c);
2115 State = n;
2116 /* Backup to the replaced character. */
2117 dec_cursor();
2119 else
2120 #endif
2122 #ifdef FEAT_VIRTUALEDIT
2123 if (n == TAB)
2125 int end_vcol = 0;
2127 if (curwin->w_cursor.lnum == oap->end.lnum)
2129 /* oap->end has to be recalculated when
2130 * the tab breaks */
2131 end_vcol = getviscol2(oap->end.col,
2132 oap->end.coladd);
2134 coladvance_force(getviscol());
2135 if (curwin->w_cursor.lnum == oap->end.lnum)
2136 getvpos(&oap->end, end_vcol);
2138 #endif
2139 pchar(curwin->w_cursor, c);
2142 #ifdef FEAT_VIRTUALEDIT
2143 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2145 int virtcols = oap->end.coladd;
2147 if (curwin->w_cursor.lnum == oap->start.lnum
2148 && oap->start.col == oap->end.col && oap->start.coladd)
2149 virtcols -= oap->start.coladd;
2151 /* oap->end has been trimmed so it's effectively inclusive;
2152 * as a result an extra +1 must be counted so we don't
2153 * trample the NUL byte. */
2154 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2155 curwin->w_cursor.col -= (virtcols + 1);
2156 for (; virtcols >= 0; virtcols--)
2158 pchar(curwin->w_cursor, c);
2159 if (inc(&curwin->w_cursor) == -1)
2160 break;
2163 #endif
2165 /* Advance to next character, stop at the end of the file. */
2166 if (inc_cursor() == -1)
2167 break;
2171 curwin->w_cursor = oap->start;
2172 check_cursor();
2173 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2175 /* Set "'[" and "']" marks. */
2176 curbuf->b_op_start = oap->start;
2177 curbuf->b_op_end = oap->end;
2179 return OK;
2181 #endif
2184 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2186 void
2187 op_tilde(oap)
2188 oparg_T *oap;
2190 pos_T pos;
2191 #ifdef FEAT_VISUAL
2192 struct block_def bd;
2193 int todo;
2194 #endif
2195 int did_change = 0;
2197 if (u_save((linenr_T)(oap->start.lnum - 1),
2198 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2199 return;
2201 pos = oap->start;
2202 #ifdef FEAT_VISUAL
2203 if (oap->block_mode) /* Visual block mode */
2205 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2207 block_prep(oap, &bd, pos.lnum, FALSE);
2208 pos.col = bd.textcol;
2209 for (todo = bd.textlen; todo > 0; --todo)
2211 # ifdef FEAT_MBYTE
2212 if (has_mbyte)
2213 todo -= (*mb_ptr2len)(ml_get_pos(&pos)) - 1;
2214 # endif
2215 did_change |= swapchar(oap->op_type, &pos);
2216 if (inc(&pos) == -1) /* at end of file */
2217 break;
2219 # ifdef FEAT_NETBEANS_INTG
2220 if (usingNetbeans && did_change)
2222 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2224 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2225 (long)bd.textlen);
2226 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
2227 &ptr[bd.textcol], bd.textlen);
2229 # endif
2231 if (did_change)
2232 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2234 else /* not block mode */
2235 #endif
2237 if (oap->motion_type == MLINE)
2239 oap->start.col = 0;
2240 pos.col = 0;
2241 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2242 if (oap->end.col)
2243 --oap->end.col;
2245 else if (!oap->inclusive)
2246 dec(&(oap->end));
2248 while (ltoreq(pos, oap->end))
2250 did_change |= swapchar(oap->op_type, &pos);
2251 if (inc(&pos) == -1) /* at end of file */
2252 break;
2255 if (did_change)
2257 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2258 0L);
2259 #ifdef FEAT_NETBEANS_INTG
2260 if (usingNetbeans && did_change)
2262 char_u *ptr;
2263 int count;
2265 pos = oap->start;
2266 while (pos.lnum < oap->end.lnum)
2268 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2269 count = (int)STRLEN(ptr) - pos.col;
2270 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2271 netbeans_inserted(curbuf, pos.lnum, pos.col,
2272 &ptr[pos.col], count);
2273 pos.col = 0;
2274 pos.lnum++;
2276 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2277 count = oap->end.col - pos.col + 1;
2278 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2279 netbeans_inserted(curbuf, pos.lnum, pos.col,
2280 &ptr[pos.col], count);
2282 #endif
2286 #ifdef FEAT_VISUAL
2287 if (!did_change && oap->is_VIsual)
2288 /* No change: need to remove the Visual selection */
2289 redraw_curbuf_later(INVERTED);
2290 #endif
2293 * Set '[ and '] marks.
2295 curbuf->b_op_start = oap->start;
2296 curbuf->b_op_end = oap->end;
2298 if (oap->line_count > p_report)
2300 if (oap->line_count == 1)
2301 MSG(_("1 line changed"));
2302 else
2303 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2308 * If op_type == OP_UPPER: make uppercase,
2309 * if op_type == OP_LOWER: make lowercase,
2310 * if op_type == OP_ROT13: do rot13 encoding,
2311 * else swap case of character at 'pos'
2312 * returns TRUE when something actually changed.
2315 swapchar(op_type, pos)
2316 int op_type;
2317 pos_T *pos;
2319 int c;
2320 int nc;
2322 c = gchar_pos(pos);
2324 /* Only do rot13 encoding for ASCII characters. */
2325 if (c >= 0x80 && op_type == OP_ROT13)
2326 return FALSE;
2328 #ifdef FEAT_MBYTE
2329 if (op_type == OP_UPPER && enc_latin1like && c == 0xdf)
2331 pos_T sp = curwin->w_cursor;
2333 /* Special handling of German sharp s: change to "SS". */
2334 curwin->w_cursor = *pos;
2335 del_char(FALSE);
2336 ins_char('S');
2337 ins_char('S');
2338 curwin->w_cursor = sp;
2339 inc(pos);
2342 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2343 return FALSE;
2344 #endif
2345 nc = c;
2346 if (MB_ISLOWER(c))
2348 if (op_type == OP_ROT13)
2349 nc = ROT13(c, 'a');
2350 else if (op_type != OP_LOWER)
2351 nc = MB_TOUPPER(c);
2353 else if (MB_ISUPPER(c))
2355 if (op_type == OP_ROT13)
2356 nc = ROT13(c, 'A');
2357 else if (op_type != OP_UPPER)
2358 nc = MB_TOLOWER(c);
2360 if (nc != c)
2362 #ifdef FEAT_MBYTE
2363 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2365 pos_T sp = curwin->w_cursor;
2367 curwin->w_cursor = *pos;
2368 del_char(FALSE);
2369 ins_char(nc);
2370 curwin->w_cursor = sp;
2372 else
2373 #endif
2374 pchar(*pos, nc);
2375 return TRUE;
2377 return FALSE;
2380 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2382 * op_insert - Insert and append operators for Visual mode.
2384 void
2385 op_insert(oap, count1)
2386 oparg_T *oap;
2387 long count1;
2389 long ins_len, pre_textlen = 0;
2390 char_u *firstline, *ins_text;
2391 struct block_def bd;
2392 int i;
2394 /* edit() changes this - record it for OP_APPEND */
2395 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2397 /* vis block is still marked. Get rid of it now. */
2398 curwin->w_cursor.lnum = oap->start.lnum;
2399 update_screen(INVERTED);
2401 if (oap->block_mode)
2403 #ifdef FEAT_VIRTUALEDIT
2404 /* When 'virtualedit' is used, need to insert the extra spaces before
2405 * doing block_prep(). When only "block" is used, virtual edit is
2406 * already disabled, but still need it when calling
2407 * coladvance_force(). */
2408 if (curwin->w_cursor.coladd > 0)
2410 int old_ve_flags = ve_flags;
2412 ve_flags = VE_ALL;
2413 if (u_save_cursor() == FAIL)
2414 return;
2415 coladvance_force(oap->op_type == OP_APPEND
2416 ? oap->end_vcol + 1 : getviscol());
2417 if (oap->op_type == OP_APPEND)
2418 --curwin->w_cursor.col;
2419 ve_flags = old_ve_flags;
2421 #endif
2422 /* Get the info about the block before entering the text */
2423 block_prep(oap, &bd, oap->start.lnum, TRUE);
2424 firstline = ml_get(oap->start.lnum) + bd.textcol;
2425 if (oap->op_type == OP_APPEND)
2426 firstline += bd.textlen;
2427 pre_textlen = (long)STRLEN(firstline);
2430 if (oap->op_type == OP_APPEND)
2432 if (oap->block_mode
2433 #ifdef FEAT_VIRTUALEDIT
2434 && curwin->w_cursor.coladd == 0
2435 #endif
2438 /* Move the cursor to the character right of the block. */
2439 curwin->w_set_curswant = TRUE;
2440 while (*ml_get_cursor() != NUL
2441 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2442 ++curwin->w_cursor.col;
2443 if (bd.is_short && !bd.is_MAX)
2445 /* First line was too short, make it longer and adjust the
2446 * values in "bd". */
2447 if (u_save_cursor() == FAIL)
2448 return;
2449 for (i = 0; i < bd.endspaces; ++i)
2450 ins_char(' ');
2451 bd.textlen += bd.endspaces;
2454 else
2456 curwin->w_cursor = oap->end;
2457 check_cursor_col();
2459 /* Works just like an 'i'nsert on the next character. */
2460 if (!lineempty(curwin->w_cursor.lnum)
2461 && oap->start_vcol != oap->end_vcol)
2462 inc_cursor();
2466 edit(NUL, FALSE, (linenr_T)count1);
2468 /* if user has moved off this line, we don't know what to do, so do
2469 * nothing */
2470 if (curwin->w_cursor.lnum != oap->start.lnum)
2471 return;
2473 if (oap->block_mode)
2475 struct block_def bd2;
2478 * Spaces and tabs in the indent may have changed to other spaces and
2479 * tabs. Get the starting column again and correct the lenght.
2480 * Don't do this when "$" used, end-of-line will have changed.
2482 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2483 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2485 if (oap->op_type == OP_APPEND)
2487 pre_textlen += bd2.textlen - bd.textlen;
2488 if (bd2.endspaces)
2489 --bd2.textlen;
2491 bd.textcol = bd2.textcol;
2492 bd.textlen = bd2.textlen;
2496 * Subsequent calls to ml_get() flush the firstline data - take a
2497 * copy of the required string.
2499 firstline = ml_get(oap->start.lnum) + bd.textcol;
2500 if (oap->op_type == OP_APPEND)
2501 firstline += bd.textlen;
2502 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2504 ins_text = vim_strnsave(firstline, (int)ins_len);
2505 if (ins_text != NULL)
2507 /* block handled here */
2508 if (u_save(oap->start.lnum,
2509 (linenr_T)(oap->end.lnum + 1)) == OK)
2510 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2511 &bd);
2513 curwin->w_cursor.col = oap->start.col;
2514 check_cursor();
2515 vim_free(ins_text);
2520 #endif
2523 * op_change - handle a change operation
2525 * return TRUE if edit() returns because of a CTRL-O command
2528 op_change(oap)
2529 oparg_T *oap;
2531 colnr_T l;
2532 int retval;
2533 #ifdef FEAT_VISUALEXTRA
2534 long offset;
2535 linenr_T linenr;
2536 long ins_len, pre_textlen = 0;
2537 char_u *firstline;
2538 char_u *ins_text, *newp, *oldp;
2539 struct block_def bd;
2540 #endif
2542 l = oap->start.col;
2543 if (oap->motion_type == MLINE)
2545 l = 0;
2546 #ifdef FEAT_SMARTINDENT
2547 if (!p_paste && curbuf->b_p_si
2548 # ifdef FEAT_CINDENT
2549 && !curbuf->b_p_cin
2550 # endif
2552 can_si = TRUE; /* It's like opening a new line, do si */
2553 #endif
2556 /* First delete the text in the region. In an empty buffer only need to
2557 * save for undo */
2558 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2560 if (u_save_cursor() == FAIL)
2561 return FALSE;
2563 else if (op_delete(oap) == FAIL)
2564 return FALSE;
2566 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2567 && !virtual_op)
2568 inc_cursor();
2570 #ifdef FEAT_VISUALEXTRA
2571 /* check for still on same line (<CR> in inserted text meaningless) */
2572 /* skip blank lines too */
2573 if (oap->block_mode)
2575 # ifdef FEAT_VIRTUALEDIT
2576 /* Add spaces before getting the current line length. */
2577 if (virtual_op && (curwin->w_cursor.coladd > 0
2578 || gchar_cursor() == NUL))
2579 coladvance_force(getviscol());
2580 # endif
2581 pre_textlen = (long)STRLEN(ml_get(oap->start.lnum));
2582 bd.textcol = curwin->w_cursor.col;
2584 #endif
2586 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2587 if (oap->motion_type == MLINE)
2588 fix_indent();
2589 #endif
2591 retval = edit(NUL, FALSE, (linenr_T)1);
2593 #ifdef FEAT_VISUALEXTRA
2595 * In Visual block mode, handle copying the new text to all lines of the
2596 * block.
2598 if (oap->block_mode && oap->start.lnum != oap->end.lnum)
2600 firstline = ml_get(oap->start.lnum);
2602 * Subsequent calls to ml_get() flush the firstline data - take a
2603 * copy of the required bit.
2605 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2607 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2609 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
2610 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2611 linenr++)
2613 block_prep(oap, &bd, linenr, TRUE);
2614 if (!bd.is_short || virtual_op)
2616 # ifdef FEAT_VIRTUALEDIT
2617 pos_T vpos;
2619 /* If the block starts in virtual space, count the
2620 * initial coladd offset as part of "startspaces" */
2621 if (bd.is_short)
2623 linenr_T lnum = curwin->w_cursor.lnum;
2625 curwin->w_cursor.lnum = linenr;
2626 (void)getvpos(&vpos, oap->start_vcol);
2627 curwin->w_cursor.lnum = lnum;
2629 else
2630 vpos.coladd = 0;
2631 # endif
2632 oldp = ml_get(linenr);
2633 newp = alloc_check((unsigned)(STRLEN(oldp)
2634 # ifdef FEAT_VIRTUALEDIT
2635 + vpos.coladd
2636 # endif
2637 + ins_len + 1));
2638 if (newp == NULL)
2639 continue;
2640 /* copy up to block start */
2641 mch_memmove(newp, oldp, (size_t)bd.textcol);
2642 offset = bd.textcol;
2643 # ifdef FEAT_VIRTUALEDIT
2644 copy_spaces(newp + offset, (size_t)vpos.coladd);
2645 offset += vpos.coladd;
2646 # endif
2647 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2648 offset += ins_len;
2649 oldp += bd.textcol;
2650 mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2651 ml_replace(linenr, newp, FALSE);
2654 check_cursor();
2656 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2658 vim_free(ins_text);
2661 #endif
2663 return retval;
2667 * set all the yank registers to empty (called from main())
2669 void
2670 init_yank()
2672 int i;
2674 for (i = 0; i < NUM_REGISTERS; ++i)
2675 y_regs[i].y_array = NULL;
2678 #if defined(EXITFREE) || defined(PROTO)
2679 void
2680 clear_registers()
2682 int i;
2684 for (i = 0; i < NUM_REGISTERS; ++i)
2686 y_current = &y_regs[i];
2687 if (y_current->y_array != NULL)
2688 free_yank_all();
2691 #endif
2694 * Free "n" lines from the current yank register.
2695 * Called for normal freeing and in case of error.
2697 static void
2698 free_yank(n)
2699 long n;
2701 if (y_current->y_array != NULL)
2703 long i;
2705 for (i = n; --i >= 0; )
2707 #ifdef AMIGA /* only for very slow machines */
2708 if ((i & 1023) == 1023) /* this may take a while */
2711 * This message should never cause a hit-return message.
2712 * Overwrite this message with any next message.
2714 ++no_wait_return;
2715 smsg((char_u *)_("freeing %ld lines"), i + 1);
2716 --no_wait_return;
2717 msg_didout = FALSE;
2718 msg_col = 0;
2720 #endif
2721 vim_free(y_current->y_array[i]);
2723 vim_free(y_current->y_array);
2724 y_current->y_array = NULL;
2725 #ifdef AMIGA
2726 if (n >= 1000)
2727 MSG("");
2728 #endif
2732 static void
2733 free_yank_all()
2735 free_yank(y_current->y_size);
2739 * Yank the text between "oap->start" and "oap->end" into a yank register.
2740 * If we are to append (uppercase register), we first yank into a new yank
2741 * register and then concatenate the old and the new one (so we keep the old
2742 * one in case of out-of-memory).
2744 * return FAIL for failure, OK otherwise
2747 op_yank(oap, deleting, mess)
2748 oparg_T *oap;
2749 int deleting;
2750 int mess;
2752 long y_idx; /* index in y_array[] */
2753 struct yankreg *curr; /* copy of y_current */
2754 struct yankreg newreg; /* new yank register when appending */
2755 char_u **new_ptr;
2756 linenr_T lnum; /* current line number */
2757 long j;
2758 int yanktype = oap->motion_type;
2759 long yanklines = oap->line_count;
2760 linenr_T yankendlnum = oap->end.lnum;
2761 char_u *p;
2762 char_u *pnew;
2763 struct block_def bd;
2765 /* check for read-only register */
2766 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2768 beep_flush();
2769 return FAIL;
2771 if (oap->regname == '_') /* black hole: nothing to do */
2772 return OK;
2774 #ifdef FEAT_CLIPBOARD
2775 if (!clip_star.available && oap->regname == '*')
2776 oap->regname = 0;
2777 else if (!clip_plus.available && oap->regname == '+')
2778 oap->regname = 0;
2779 #endif
2781 if (!deleting) /* op_delete() already set y_current */
2782 get_yank_register(oap->regname, TRUE);
2784 curr = y_current;
2785 /* append to existing contents */
2786 if (y_append && y_current->y_array != NULL)
2787 y_current = &newreg;
2788 else
2789 free_yank_all(); /* free previously yanked lines */
2792 * If the cursor was in column 1 before and after the movement, and the
2793 * operator is not inclusive, the yank is always linewise.
2795 if ( oap->motion_type == MCHAR
2796 && oap->start.col == 0
2797 && !oap->inclusive
2798 #ifdef FEAT_VISUAL
2799 && (!oap->is_VIsual || *p_sel == 'o')
2800 && !oap->block_mode
2801 #endif
2802 && oap->end.col == 0
2803 && yanklines > 1)
2805 yanktype = MLINE;
2806 --yankendlnum;
2807 --yanklines;
2810 y_current->y_size = yanklines;
2811 y_current->y_type = yanktype; /* set the yank register type */
2812 #ifdef FEAT_VISUAL
2813 y_current->y_width = 0;
2814 #endif
2815 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2816 yanklines), TRUE);
2818 if (y_current->y_array == NULL)
2820 y_current = curr;
2821 return FAIL;
2824 y_idx = 0;
2825 lnum = oap->start.lnum;
2827 #ifdef FEAT_VISUAL
2828 if (oap->block_mode)
2830 /* Visual block mode */
2831 y_current->y_type = MBLOCK; /* set the yank register type */
2832 y_current->y_width = oap->end_vcol - oap->start_vcol;
2834 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2835 y_current->y_width--;
2837 #endif
2839 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2841 switch (y_current->y_type)
2843 #ifdef FEAT_VISUAL
2844 case MBLOCK:
2845 block_prep(oap, &bd, lnum, FALSE);
2846 if (yank_copy_line(&bd, y_idx) == FAIL)
2847 goto fail;
2848 break;
2849 #endif
2851 case MLINE:
2852 if ((y_current->y_array[y_idx] =
2853 vim_strsave(ml_get(lnum))) == NULL)
2854 goto fail;
2855 break;
2857 case MCHAR:
2859 colnr_T startcol = 0, endcol = MAXCOL;
2860 #ifdef FEAT_VIRTUALEDIT
2861 int is_oneChar = FALSE;
2862 colnr_T cs, ce;
2863 #endif
2864 p = ml_get(lnum);
2865 bd.startspaces = 0;
2866 bd.endspaces = 0;
2868 if (lnum == oap->start.lnum)
2870 startcol = oap->start.col;
2871 #ifdef FEAT_VIRTUALEDIT
2872 if (virtual_op)
2874 getvcol(curwin, &oap->start, &cs, NULL, &ce);
2875 if (ce != cs && oap->start.coladd > 0)
2877 /* Part of a tab selected -- but don't
2878 * double-count it. */
2879 bd.startspaces = (ce - cs + 1)
2880 - oap->start.coladd;
2881 startcol++;
2884 #endif
2887 if (lnum == oap->end.lnum)
2889 endcol = oap->end.col;
2890 #ifdef FEAT_VIRTUALEDIT
2891 if (virtual_op)
2893 getvcol(curwin, &oap->end, &cs, NULL, &ce);
2894 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2895 # ifdef FEAT_MBYTE
2896 /* Don't add space for double-wide
2897 * char; endcol will be on last byte
2898 * of multi-byte char. */
2899 && (*mb_head_off)(p, p + endcol) == 0
2900 # endif
2903 if (oap->start.lnum == oap->end.lnum
2904 && oap->start.col == oap->end.col)
2906 /* Special case: inside a single char */
2907 is_oneChar = TRUE;
2908 bd.startspaces = oap->end.coladd
2909 - oap->start.coladd + oap->inclusive;
2910 endcol = startcol;
2912 else
2914 bd.endspaces = oap->end.coladd
2915 + oap->inclusive;
2916 endcol -= oap->inclusive;
2920 #endif
2922 if (startcol > endcol
2923 #ifdef FEAT_VIRTUALEDIT
2924 || is_oneChar
2925 #endif
2927 bd.textlen = 0;
2928 else
2930 if (endcol == MAXCOL)
2931 endcol = (colnr_T)STRLEN(p);
2932 bd.textlen = endcol - startcol + oap->inclusive;
2934 bd.textstart = p + startcol;
2935 if (yank_copy_line(&bd, y_idx) == FAIL)
2936 goto fail;
2937 break;
2939 /* NOTREACHED */
2943 if (curr != y_current) /* append the new block to the old block */
2945 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2946 (curr->y_size + y_current->y_size)), TRUE);
2947 if (new_ptr == NULL)
2948 goto fail;
2949 for (j = 0; j < curr->y_size; ++j)
2950 new_ptr[j] = curr->y_array[j];
2951 vim_free(curr->y_array);
2952 curr->y_array = new_ptr;
2954 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
2955 curr->y_type = MLINE;
2957 /* Concatenate the last line of the old block with the first line of
2958 * the new block, unless being Vi compatible. */
2959 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
2961 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2962 + STRLEN(y_current->y_array[0]) + 1), TRUE);
2963 if (pnew == NULL)
2965 y_idx = y_current->y_size - 1;
2966 goto fail;
2968 STRCPY(pnew, curr->y_array[--j]);
2969 STRCAT(pnew, y_current->y_array[0]);
2970 vim_free(curr->y_array[j]);
2971 vim_free(y_current->y_array[0]);
2972 curr->y_array[j++] = pnew;
2973 y_idx = 1;
2975 else
2976 y_idx = 0;
2977 while (y_idx < y_current->y_size)
2978 curr->y_array[j++] = y_current->y_array[y_idx++];
2979 curr->y_size = j;
2980 vim_free(y_current->y_array);
2981 y_current = curr;
2983 if (mess) /* Display message about yank? */
2985 if (yanktype == MCHAR
2986 #ifdef FEAT_VISUAL
2987 && !oap->block_mode
2988 #endif
2989 && yanklines == 1)
2990 yanklines = 0;
2991 /* Some versions of Vi use ">=" here, some don't... */
2992 if (yanklines > p_report)
2994 /* redisplay now, so message is not deleted */
2995 update_topline_redraw();
2996 if (yanklines == 1)
2998 #ifdef FEAT_VISUAL
2999 if (oap->block_mode)
3000 MSG(_("block of 1 line yanked"));
3001 else
3002 #endif
3003 MSG(_("1 line yanked"));
3005 #ifdef FEAT_VISUAL
3006 else if (oap->block_mode)
3007 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3008 #endif
3009 else
3010 smsg((char_u *)_("%ld lines yanked"), yanklines);
3015 * Set "'[" and "']" marks.
3017 curbuf->b_op_start = oap->start;
3018 curbuf->b_op_end = oap->end;
3019 if (yanktype == MLINE
3020 #ifdef FEAT_VISUAL
3021 && !oap->block_mode
3022 #endif
3025 curbuf->b_op_start.col = 0;
3026 curbuf->b_op_end.col = MAXCOL;
3029 #ifdef FEAT_CLIPBOARD
3031 * If we were yanking to the '*' register, send result to clipboard.
3032 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3033 * to the '*' register.
3035 if (clip_star.available
3036 && (curr == &(y_regs[STAR_REGISTER])
3037 || (!deleting && oap->regname == 0 && clip_unnamed)))
3039 if (curr != &(y_regs[STAR_REGISTER]))
3040 /* Copy the text from register 0 to the clipboard register. */
3041 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3043 clip_own_selection(&clip_star);
3044 clip_gen_set_selection(&clip_star);
3047 # ifdef FEAT_X11
3049 * If we were yanking to the '+' register, send result to selection.
3050 * Also copy to the '*' register, in case auto-select is off.
3052 else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
3054 /* No need to copy to * register upon 'unnamed' now - see below */
3055 clip_own_selection(&clip_plus);
3056 clip_gen_set_selection(&clip_plus);
3057 if (!clip_isautosel())
3059 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3060 clip_own_selection(&clip_star);
3061 clip_gen_set_selection(&clip_star);
3064 # endif
3065 #endif
3067 return OK;
3069 fail: /* free the allocated lines */
3070 free_yank(y_idx + 1);
3071 y_current = curr;
3072 return FAIL;
3075 static int
3076 yank_copy_line(bd, y_idx)
3077 struct block_def *bd;
3078 long y_idx;
3080 char_u *pnew;
3082 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3083 == NULL)
3084 return FAIL;
3085 y_current->y_array[y_idx] = pnew;
3086 copy_spaces(pnew, (size_t)bd->startspaces);
3087 pnew += bd->startspaces;
3088 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3089 pnew += bd->textlen;
3090 copy_spaces(pnew, (size_t)bd->endspaces);
3091 pnew += bd->endspaces;
3092 *pnew = NUL;
3093 return OK;
3096 #ifdef FEAT_CLIPBOARD
3098 * Make a copy of the y_current register to register "reg".
3100 static void
3101 copy_yank_reg(reg)
3102 struct yankreg *reg;
3104 struct yankreg *curr = y_current;
3105 long j;
3107 y_current = reg;
3108 free_yank_all();
3109 *y_current = *curr;
3110 y_current->y_array = (char_u **)lalloc_clear(
3111 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3112 if (y_current->y_array == NULL)
3113 y_current->y_size = 0;
3114 else
3115 for (j = 0; j < y_current->y_size; ++j)
3116 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3118 free_yank(j);
3119 y_current->y_size = 0;
3120 break;
3122 y_current = curr;
3124 #endif
3127 * Put contents of register "regname" into the text.
3128 * Caller must check "regname" to be valid!
3129 * "flags": PUT_FIXINDENT make indent look nice
3130 * PUT_CURSEND leave cursor after end of new text
3131 * PUT_LINE force linewise put (":put")
3133 void
3134 do_put(regname, dir, count, flags)
3135 int regname;
3136 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3137 long count;
3138 int flags;
3140 char_u *ptr;
3141 char_u *newp, *oldp;
3142 int yanklen;
3143 int totlen = 0; /* init for gcc */
3144 linenr_T lnum;
3145 colnr_T col;
3146 long i; /* index in y_array[] */
3147 int y_type;
3148 long y_size;
3149 #ifdef FEAT_VISUAL
3150 int oldlen;
3151 long y_width = 0;
3152 colnr_T vcol;
3153 int delcount;
3154 int incr = 0;
3155 long j;
3156 struct block_def bd;
3157 #endif
3158 char_u **y_array = NULL;
3159 long nr_lines = 0;
3160 pos_T new_cursor;
3161 int indent;
3162 int orig_indent = 0; /* init for gcc */
3163 int indent_diff = 0; /* init for gcc */
3164 int first_indent = TRUE;
3165 int lendiff = 0;
3166 pos_T old_pos;
3167 char_u *insert_string = NULL;
3168 int allocated = FALSE;
3169 long cnt;
3171 #ifdef FEAT_CLIPBOARD
3172 /* Adjust register name for "unnamed" in 'clipboard'. */
3173 adjust_clip_reg(&regname);
3174 (void)may_get_selection(regname);
3175 #endif
3177 if (flags & PUT_FIXINDENT)
3178 orig_indent = get_indent();
3180 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3181 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3184 * Using inserted text works differently, because the register includes
3185 * special characters (newlines, etc.).
3187 if (regname == '.')
3189 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3190 (count == -1 ? 'O' : 'i')), count, FALSE);
3191 /* Putting the text is done later, so can't really move the cursor to
3192 * the next character. Use "l" to simulate it. */
3193 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3194 stuffcharReadbuff('l');
3195 return;
3199 * For special registers '%' (file name), '#' (alternate file name) and
3200 * ':' (last command line), etc. we have to create a fake yank register.
3202 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3204 if (insert_string == NULL)
3205 return;
3208 if (insert_string != NULL)
3210 y_type = MCHAR;
3211 #ifdef FEAT_EVAL
3212 if (regname == '=')
3214 /* For the = register we need to split the string at NL
3215 * characters. */
3216 /* Loop twice: count the number of lines and save them. */
3217 for (;;)
3219 y_size = 0;
3220 ptr = insert_string;
3221 while (ptr != NULL)
3223 if (y_array != NULL)
3224 y_array[y_size] = ptr;
3225 ++y_size;
3226 ptr = vim_strchr(ptr, '\n');
3227 if (ptr != NULL)
3229 if (y_array != NULL)
3230 *ptr = NUL;
3231 ++ptr;
3232 /* A trailing '\n' makes the string linewise */
3233 if (*ptr == NUL)
3235 y_type = MLINE;
3236 break;
3240 if (y_array != NULL)
3241 break;
3242 y_array = (char_u **)alloc((unsigned)
3243 (y_size * sizeof(char_u *)));
3244 if (y_array == NULL)
3245 goto end;
3248 else
3249 #endif
3251 y_size = 1; /* use fake one-line yank register */
3252 y_array = &insert_string;
3255 else
3257 get_yank_register(regname, FALSE);
3259 y_type = y_current->y_type;
3260 #ifdef FEAT_VISUAL
3261 y_width = y_current->y_width;
3262 #endif
3263 y_size = y_current->y_size;
3264 y_array = y_current->y_array;
3267 #ifdef FEAT_VISUAL
3268 if (y_type == MLINE)
3270 if (flags & PUT_LINE_SPLIT)
3272 /* "p" or "P" in Visual mode: split the lines to put the text in
3273 * between. */
3274 if (u_save_cursor() == FAIL)
3275 goto end;
3276 ptr = vim_strsave(ml_get_cursor());
3277 if (ptr == NULL)
3278 goto end;
3279 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3280 vim_free(ptr);
3282 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3283 if (ptr == NULL)
3284 goto end;
3285 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3286 ++nr_lines;
3287 dir = FORWARD;
3289 if (flags & PUT_LINE_FORWARD)
3291 /* Must be "p" for a Visual block, put lines below the block. */
3292 curwin->w_cursor = curbuf->b_visual.vi_end;
3293 dir = FORWARD;
3295 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3296 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3298 #endif
3300 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3301 y_type = MLINE;
3303 if (y_size == 0 || y_array == NULL)
3305 EMSG2(_("E353: Nothing in register %s"),
3306 regname == 0 ? (char_u *)"\"" : transchar(regname));
3307 goto end;
3310 #ifdef FEAT_VISUAL
3311 if (y_type == MBLOCK)
3313 lnum = curwin->w_cursor.lnum + y_size + 1;
3314 if (lnum > curbuf->b_ml.ml_line_count)
3315 lnum = curbuf->b_ml.ml_line_count + 1;
3316 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3317 goto end;
3319 else
3320 #endif
3321 if (y_type == MLINE)
3323 lnum = curwin->w_cursor.lnum;
3324 #ifdef FEAT_FOLDING
3325 /* Correct line number for closed fold. Don't move the cursor yet,
3326 * u_save() uses it. */
3327 if (dir == BACKWARD)
3328 (void)hasFolding(lnum, &lnum, NULL);
3329 else
3330 (void)hasFolding(lnum, NULL, &lnum);
3331 #endif
3332 if (dir == FORWARD)
3333 ++lnum;
3334 if (u_save(lnum - 1, lnum) == FAIL)
3335 goto end;
3336 #ifdef FEAT_FOLDING
3337 if (dir == FORWARD)
3338 curwin->w_cursor.lnum = lnum - 1;
3339 else
3340 curwin->w_cursor.lnum = lnum;
3341 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3342 #endif
3344 else if (u_save_cursor() == FAIL)
3345 goto end;
3347 yanklen = (int)STRLEN(y_array[0]);
3349 #ifdef FEAT_VIRTUALEDIT
3350 if (ve_flags == VE_ALL && y_type == MCHAR)
3352 if (gchar_cursor() == TAB)
3354 /* Don't need to insert spaces when "p" on the last position of a
3355 * tab or "P" on the first position. */
3356 if (dir == FORWARD
3357 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3358 : curwin->w_cursor.coladd > 0)
3359 coladvance_force(getviscol());
3360 else
3361 curwin->w_cursor.coladd = 0;
3363 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3364 coladvance_force(getviscol() + (dir == FORWARD));
3366 #endif
3368 lnum = curwin->w_cursor.lnum;
3369 col = curwin->w_cursor.col;
3371 #ifdef FEAT_VISUAL
3373 * Block mode
3375 if (y_type == MBLOCK)
3377 char c = gchar_cursor();
3378 colnr_T endcol2 = 0;
3380 if (dir == FORWARD && c != NUL)
3382 #ifdef FEAT_VIRTUALEDIT
3383 if (ve_flags == VE_ALL)
3384 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3385 else
3386 #endif
3387 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3389 #ifdef FEAT_MBYTE
3390 if (has_mbyte)
3391 /* move to start of next multi-byte character */
3392 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
3393 else
3394 #endif
3395 #ifdef FEAT_VIRTUALEDIT
3396 if (c != TAB || ve_flags != VE_ALL)
3397 #endif
3398 ++curwin->w_cursor.col;
3399 ++col;
3401 else
3402 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3404 #ifdef FEAT_VIRTUALEDIT
3405 col += curwin->w_cursor.coladd;
3406 if (ve_flags == VE_ALL && curwin->w_cursor.coladd > 0)
3408 if (dir == FORWARD && c == NUL)
3409 ++col;
3410 if (dir != FORWARD && c != NUL)
3411 ++curwin->w_cursor.col;
3412 if (c == TAB)
3414 if (dir == BACKWARD && curwin->w_cursor.col)
3415 curwin->w_cursor.col--;
3416 if (dir == FORWARD && col - 1 == endcol2)
3417 curwin->w_cursor.col++;
3420 curwin->w_cursor.coladd = 0;
3421 #endif
3422 bd.textcol = 0;
3423 for (i = 0; i < y_size; ++i)
3425 int spaces;
3426 char shortline;
3428 bd.startspaces = 0;
3429 bd.endspaces = 0;
3430 vcol = 0;
3431 delcount = 0;
3433 /* add a new line */
3434 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3436 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3437 (colnr_T)1, FALSE) == FAIL)
3438 break;
3439 ++nr_lines;
3441 /* get the old line and advance to the position to insert at */
3442 oldp = ml_get_curline();
3443 oldlen = (int)STRLEN(oldp);
3444 for (ptr = oldp; vcol < col && *ptr; )
3446 /* Count a tab for what it's worth (if list mode not on) */
3447 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3448 vcol += incr;
3450 bd.textcol = (colnr_T)(ptr - oldp);
3452 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3454 if (vcol < col) /* line too short, padd with spaces */
3455 bd.startspaces = col - vcol;
3456 else if (vcol > col)
3458 bd.endspaces = vcol - col;
3459 bd.startspaces = incr - bd.endspaces;
3460 --bd.textcol;
3461 delcount = 1;
3462 #ifdef FEAT_MBYTE
3463 if (has_mbyte)
3464 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3465 #endif
3466 if (oldp[bd.textcol] != TAB)
3468 /* Only a Tab can be split into spaces. Other
3469 * characters will have to be moved to after the
3470 * block, causing misalignment. */
3471 delcount = 0;
3472 bd.endspaces = 0;
3476 yanklen = (int)STRLEN(y_array[i]);
3478 /* calculate number of spaces required to fill right side of block*/
3479 spaces = y_width + 1;
3480 for (j = 0; j < yanklen; j++)
3481 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3482 if (spaces < 0)
3483 spaces = 0;
3485 /* insert the new text */
3486 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3487 newp = alloc_check((unsigned)totlen + oldlen + 1);
3488 if (newp == NULL)
3489 break;
3490 /* copy part up to cursor to new line */
3491 ptr = newp;
3492 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3493 ptr += bd.textcol;
3494 /* may insert some spaces before the new text */
3495 copy_spaces(ptr, (size_t)bd.startspaces);
3496 ptr += bd.startspaces;
3497 /* insert the new text */
3498 for (j = 0; j < count; ++j)
3500 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3501 ptr += yanklen;
3503 /* insert block's trailing spaces only if there's text behind */
3504 if ((j < count - 1 || !shortline) && spaces)
3506 copy_spaces(ptr, (size_t)spaces);
3507 ptr += spaces;
3510 /* may insert some spaces after the new text */
3511 copy_spaces(ptr, (size_t)bd.endspaces);
3512 ptr += bd.endspaces;
3513 /* move the text after the cursor to the end of the line. */
3514 mch_memmove(ptr, oldp + bd.textcol + delcount,
3515 (size_t)(oldlen - bd.textcol - delcount + 1));
3516 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3518 ++curwin->w_cursor.lnum;
3519 if (i == 0)
3520 curwin->w_cursor.col += bd.startspaces;
3523 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3525 /* Set '[ mark. */
3526 curbuf->b_op_start = curwin->w_cursor;
3527 curbuf->b_op_start.lnum = lnum;
3529 /* adjust '] mark */
3530 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3531 curbuf->b_op_end.col = bd.textcol + totlen - 1;
3532 # ifdef FEAT_VIRTUALEDIT
3533 curbuf->b_op_end.coladd = 0;
3534 # endif
3535 if (flags & PUT_CURSEND)
3537 colnr_T len;
3539 curwin->w_cursor = curbuf->b_op_end;
3540 curwin->w_cursor.col++;
3542 /* in Insert mode we might be after the NUL, correct for that */
3543 len = (colnr_T)STRLEN(ml_get_curline());
3544 if (curwin->w_cursor.col > len)
3545 curwin->w_cursor.col = len;
3547 else
3548 curwin->w_cursor.lnum = lnum;
3550 else
3551 #endif
3554 * Character or Line mode
3556 if (y_type == MCHAR)
3558 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3559 * char */
3560 if (dir == FORWARD && gchar_cursor() != NUL)
3562 #ifdef FEAT_MBYTE
3563 if (has_mbyte)
3565 int bytelen = (*mb_ptr2len)(ml_get_cursor());
3567 /* put it on the next of the multi-byte character. */
3568 col += bytelen;
3569 if (yanklen)
3571 curwin->w_cursor.col += bytelen;
3572 curbuf->b_op_end.col += bytelen;
3575 else
3576 #endif
3578 ++col;
3579 if (yanklen)
3581 ++curwin->w_cursor.col;
3582 ++curbuf->b_op_end.col;
3586 curbuf->b_op_start = curwin->w_cursor;
3589 * Line mode: BACKWARD is the same as FORWARD on the previous line
3591 else if (dir == BACKWARD)
3592 --lnum;
3593 new_cursor = curwin->w_cursor;
3596 * simple case: insert into current line
3598 if (y_type == MCHAR && y_size == 1)
3600 totlen = count * yanklen;
3601 if (totlen)
3603 oldp = ml_get(lnum);
3604 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3605 if (newp == NULL)
3606 goto end; /* alloc() will give error message */
3607 mch_memmove(newp, oldp, (size_t)col);
3608 ptr = newp + col;
3609 for (i = 0; i < count; ++i)
3611 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3612 ptr += yanklen;
3614 mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3615 ml_replace(lnum, newp, FALSE);
3616 /* Put cursor on last putted char. */
3617 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3619 curbuf->b_op_end = curwin->w_cursor;
3620 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3621 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3622 ++curwin->w_cursor.col;
3623 changed_bytes(lnum, col);
3625 else
3628 * Insert at least one line. When y_type is MCHAR, break the first
3629 * line in two.
3631 for (cnt = 1; cnt <= count; ++cnt)
3633 i = 0;
3634 if (y_type == MCHAR)
3637 * Split the current line in two at the insert position.
3638 * First insert y_array[size - 1] in front of second line.
3639 * Then append y_array[0] to first line.
3641 lnum = new_cursor.lnum;
3642 ptr = ml_get(lnum) + col;
3643 totlen = (int)STRLEN(y_array[y_size - 1]);
3644 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3645 if (newp == NULL)
3646 goto error;
3647 STRCPY(newp, y_array[y_size - 1]);
3648 STRCAT(newp, ptr);
3649 /* insert second line */
3650 ml_append(lnum, newp, (colnr_T)0, FALSE);
3651 vim_free(newp);
3653 oldp = ml_get(lnum);
3654 newp = alloc_check((unsigned)(col + yanklen + 1));
3655 if (newp == NULL)
3656 goto error;
3657 /* copy first part of line */
3658 mch_memmove(newp, oldp, (size_t)col);
3659 /* append to first line */
3660 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3661 ml_replace(lnum, newp, FALSE);
3663 curwin->w_cursor.lnum = lnum;
3664 i = 1;
3667 for (; i < y_size; ++i)
3669 if ((y_type != MCHAR || i < y_size - 1)
3670 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3671 == FAIL)
3672 goto error;
3673 lnum++;
3674 ++nr_lines;
3675 if (flags & PUT_FIXINDENT)
3677 old_pos = curwin->w_cursor;
3678 curwin->w_cursor.lnum = lnum;
3679 ptr = ml_get(lnum);
3680 if (cnt == count && i == y_size - 1)
3681 lendiff = (int)STRLEN(ptr);
3682 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3683 if (*ptr == '#' && preprocs_left())
3684 indent = 0; /* Leave # lines at start */
3685 else
3686 #endif
3687 if (*ptr == NUL)
3688 indent = 0; /* Ignore empty lines */
3689 else if (first_indent)
3691 indent_diff = orig_indent - get_indent();
3692 indent = orig_indent;
3693 first_indent = FALSE;
3695 else if ((indent = get_indent() + indent_diff) < 0)
3696 indent = 0;
3697 (void)set_indent(indent, 0);
3698 curwin->w_cursor = old_pos;
3699 /* remember how many chars were removed */
3700 if (cnt == count && i == y_size - 1)
3701 lendiff -= (int)STRLEN(ml_get(lnum));
3706 error:
3707 /* Adjust marks. */
3708 if (y_type == MLINE)
3710 curbuf->b_op_start.col = 0;
3711 if (dir == FORWARD)
3712 curbuf->b_op_start.lnum++;
3714 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3715 (linenr_T)MAXLNUM, nr_lines, 0L);
3717 /* note changed text for displaying and folding */
3718 if (y_type == MCHAR)
3719 changed_lines(curwin->w_cursor.lnum, col,
3720 curwin->w_cursor.lnum + 1, nr_lines);
3721 else
3722 changed_lines(curbuf->b_op_start.lnum, 0,
3723 curbuf->b_op_start.lnum, nr_lines);
3725 /* put '] mark at last inserted character */
3726 curbuf->b_op_end.lnum = lnum;
3727 /* correct length for change in indent */
3728 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3729 if (col > 1)
3730 curbuf->b_op_end.col = col - 1;
3731 else
3732 curbuf->b_op_end.col = 0;
3734 if (flags & PUT_CURSLINE)
3736 /* ":put": put cursor on last inserted line */
3737 curwin->w_cursor.lnum = lnum;
3738 beginline(BL_WHITE | BL_FIX);
3740 else if (flags & PUT_CURSEND)
3742 /* put cursor after inserted text */
3743 if (y_type == MLINE)
3745 if (lnum >= curbuf->b_ml.ml_line_count)
3746 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3747 else
3748 curwin->w_cursor.lnum = lnum + 1;
3749 curwin->w_cursor.col = 0;
3751 else
3753 curwin->w_cursor.lnum = lnum;
3754 curwin->w_cursor.col = col;
3757 else if (y_type == MLINE)
3759 /* put cursor on first non-blank in first inserted line */
3760 curwin->w_cursor.col = 0;
3761 if (dir == FORWARD)
3762 ++curwin->w_cursor.lnum;
3763 beginline(BL_WHITE | BL_FIX);
3765 else /* put cursor on first inserted character */
3766 curwin->w_cursor = new_cursor;
3770 msgmore(nr_lines);
3771 curwin->w_set_curswant = TRUE;
3773 end:
3774 if (allocated)
3775 vim_free(insert_string);
3776 if (regname == '=')
3777 vim_free(y_array);
3779 /* If the cursor is past the end of the line put it at the end. */
3780 adjust_cursor_eol();
3784 * When the cursor is on the NUL past the end of the line and it should not be
3785 * there move it left.
3787 void
3788 adjust_cursor_eol()
3790 if (curwin->w_cursor.col > 0
3791 && gchar_cursor() == NUL
3792 #ifdef FEAT_VIRTUALEDIT
3793 && (ve_flags & VE_ONEMORE) == 0
3794 #endif
3795 && !(restart_edit || (State & INSERT)))
3797 /* Put the cursor on the last character in the line. */
3798 dec_cursor();
3800 #ifdef FEAT_VIRTUALEDIT
3801 if (ve_flags == VE_ALL)
3803 colnr_T scol, ecol;
3805 /* Coladd is set to the width of the last character. */
3806 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3807 curwin->w_cursor.coladd = ecol - scol + 1;
3809 #endif
3813 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3815 * Return TRUE if lines starting with '#' should be left aligned.
3818 preprocs_left()
3820 return
3821 # ifdef FEAT_SMARTINDENT
3822 # ifdef FEAT_CINDENT
3823 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3824 # else
3825 curbuf->b_p_si
3826 # endif
3827 # endif
3828 # ifdef FEAT_CINDENT
3829 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3830 # endif
3833 #endif
3835 /* Return the character name of the register with the given number */
3837 get_register_name(num)
3838 int num;
3840 if (num == -1)
3841 return '"';
3842 else if (num < 10)
3843 return num + '0';
3844 else if (num == DELETION_REGISTER)
3845 return '-';
3846 #ifdef FEAT_CLIPBOARD
3847 else if (num == STAR_REGISTER)
3848 return '*';
3849 else if (num == PLUS_REGISTER)
3850 return '+';
3851 #endif
3852 else
3854 #ifdef EBCDIC
3855 int i;
3857 /* EBCDIC is really braindead ... */
3858 i = 'a' + (num - 10);
3859 if (i > 'i')
3860 i += 7;
3861 if (i > 'r')
3862 i += 8;
3863 return i;
3864 #else
3865 return num + 'a' - 10;
3866 #endif
3871 * ":dis" and ":registers": Display the contents of the yank registers.
3873 void
3874 ex_display(eap)
3875 exarg_T *eap;
3877 int i, n;
3878 long j;
3879 char_u *p;
3880 struct yankreg *yb;
3881 int name;
3882 int attr;
3883 char_u *arg = eap->arg;
3884 #ifdef FEAT_MBYTE
3885 int clen;
3886 #else
3887 # define clen 1
3888 #endif
3890 if (arg != NULL && *arg == NUL)
3891 arg = NULL;
3892 attr = hl_attr(HLF_8);
3894 /* Highlight title */
3895 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3896 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3898 name = get_register_name(i);
3899 if (arg != NULL && vim_strchr(arg, name) == NULL)
3900 continue; /* did not ask for this register */
3902 #ifdef FEAT_CLIPBOARD
3903 /* Adjust register name for "unnamed" in 'clipboard'.
3904 * When it's a clipboard register, fill it with the current contents
3905 * of the clipboard. */
3906 adjust_clip_reg(&name);
3907 (void)may_get_selection(name);
3908 #endif
3910 if (i == -1)
3912 if (y_previous != NULL)
3913 yb = y_previous;
3914 else
3915 yb = &(y_regs[0]);
3917 else
3918 yb = &(y_regs[i]);
3919 if (yb->y_array != NULL)
3921 msg_putchar('\n');
3922 msg_putchar('"');
3923 msg_putchar(name);
3924 MSG_PUTS(" ");
3926 n = (int)Columns - 6;
3927 for (j = 0; j < yb->y_size && n > 1; ++j)
3929 if (j)
3931 MSG_PUTS_ATTR("^J", attr);
3932 n -= 2;
3934 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3936 #ifdef FEAT_MBYTE
3937 clen = (*mb_ptr2len)(p);
3938 #endif
3939 msg_outtrans_len(p, clen);
3940 #ifdef FEAT_MBYTE
3941 p += clen - 1;
3942 #endif
3945 if (n > 1 && yb->y_type == MLINE)
3946 MSG_PUTS_ATTR("^J", attr);
3947 out_flush(); /* show one line at a time */
3949 ui_breakcheck();
3953 * display last inserted text
3955 if ((p = get_last_insert()) != NULL
3956 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3958 MSG_PUTS("\n\". ");
3959 dis_msg(p, TRUE);
3963 * display last command line
3965 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3966 && !got_int)
3968 MSG_PUTS("\n\": ");
3969 dis_msg(last_cmdline, FALSE);
3973 * display current file name
3975 if (curbuf->b_fname != NULL
3976 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3978 MSG_PUTS("\n\"% ");
3979 dis_msg(curbuf->b_fname, FALSE);
3983 * display alternate file name
3985 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3987 char_u *fname;
3988 linenr_T dummy;
3990 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
3992 MSG_PUTS("\n\"# ");
3993 dis_msg(fname, FALSE);
3998 * display last search pattern
4000 if (last_search_pat() != NULL
4001 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4003 MSG_PUTS("\n\"/ ");
4004 dis_msg(last_search_pat(), FALSE);
4007 #ifdef FEAT_EVAL
4009 * display last used expression
4011 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4012 && !got_int)
4014 MSG_PUTS("\n\"= ");
4015 dis_msg(expr_line, FALSE);
4017 #endif
4021 * display a string for do_dis()
4022 * truncate at end of screen line
4024 static void
4025 dis_msg(p, skip_esc)
4026 char_u *p;
4027 int skip_esc; /* if TRUE, ignore trailing ESC */
4029 int n;
4030 #ifdef FEAT_MBYTE
4031 int l;
4032 #endif
4034 n = (int)Columns - 6;
4035 while (*p != NUL
4036 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4037 && (n -= ptr2cells(p)) >= 0)
4039 #ifdef FEAT_MBYTE
4040 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
4042 msg_outtrans_len(p, l);
4043 p += l;
4045 else
4046 #endif
4047 msg_outtrans_len(p++, 1);
4049 ui_breakcheck();
4053 * join 'count' lines (minimal 2), including u_save()
4055 void
4056 do_do_join(count, insert_space)
4057 long count;
4058 int insert_space;
4060 colnr_T col = MAXCOL;
4062 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4063 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4064 return;
4066 while (--count > 0)
4068 line_breakcheck();
4069 if (got_int || do_join(insert_space) == FAIL)
4071 beep_flush();
4072 break;
4074 if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4075 col = curwin->w_cursor.col;
4078 /* Vi compatible: use the column of the first join */
4079 if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4080 curwin->w_cursor.col = col;
4082 #if 0
4084 * Need to update the screen if the line where the cursor is became too
4085 * long to fit on the screen.
4087 update_topline_redraw();
4088 #endif
4092 * Join two lines at the cursor position.
4093 * "redraw" is TRUE when the screen should be updated.
4094 * Caller must have setup for undo.
4096 * return FAIL for failure, OK ohterwise
4099 do_join(insert_space)
4100 int insert_space;
4102 char_u *curr;
4103 char_u *next, *next_start;
4104 char_u *newp;
4105 int endcurr1, endcurr2;
4106 int currsize; /* size of the current line */
4107 int nextsize; /* size of the next line */
4108 int spaces; /* number of spaces to insert */
4109 linenr_T t;
4111 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4112 return FAIL; /* can't join on last line */
4114 curr = ml_get_curline();
4115 currsize = (int)STRLEN(curr);
4116 endcurr1 = endcurr2 = NUL;
4117 if (insert_space && currsize > 0)
4119 #ifdef FEAT_MBYTE
4120 if (has_mbyte)
4122 next = curr + currsize;
4123 mb_ptr_back(curr, next);
4124 endcurr1 = (*mb_ptr2char)(next);
4125 if (next > curr)
4127 mb_ptr_back(curr, next);
4128 endcurr2 = (*mb_ptr2char)(next);
4131 else
4132 #endif
4134 endcurr1 = *(curr + currsize - 1);
4135 if (currsize > 1)
4136 endcurr2 = *(curr + currsize - 2);
4140 next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4141 spaces = 0;
4142 if (insert_space)
4144 next = skipwhite(next);
4145 if (*next != ')' && currsize != 0 && endcurr1 != TAB
4146 #ifdef FEAT_MBYTE
4147 && (!has_format_option(FO_MBYTE_JOIN)
4148 || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4149 && (!has_format_option(FO_MBYTE_JOIN2)
4150 || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4151 #endif
4154 /* don't add a space if the line is ending in a space */
4155 if (endcurr1 == ' ')
4156 endcurr1 = endcurr2;
4157 else
4158 ++spaces;
4159 /* extra space when 'joinspaces' set and line ends in '.' */
4160 if ( p_js
4161 && (endcurr1 == '.'
4162 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4163 && (endcurr1 == '?' || endcurr1 == '!'))))
4164 ++spaces;
4167 nextsize = (int)STRLEN(next);
4169 newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4170 if (newp == NULL)
4171 return FAIL;
4174 * Insert the next line first, because we already have that pointer.
4175 * Curr has to be obtained again, because getting next will have
4176 * invalidated it.
4178 mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4180 curr = ml_get_curline();
4181 mch_memmove(newp, curr, (size_t)currsize);
4183 copy_spaces(newp + currsize, (size_t)spaces);
4185 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4187 /* Only report the change in the first line here, del_lines() will report
4188 * the deleted line. */
4189 changed_lines(curwin->w_cursor.lnum, currsize,
4190 curwin->w_cursor.lnum + 1, 0L);
4193 * Delete the following line. To do this we move the cursor there
4194 * briefly, and then move it back. After del_lines() the cursor may
4195 * have moved up (last line deleted), so the current lnum is kept in t.
4197 * Move marks from the deleted line to the joined line, adjusting the
4198 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4199 * should not really be a problem.
4201 t = curwin->w_cursor.lnum;
4202 mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4203 (long)(currsize + spaces - (next - next_start)));
4204 ++curwin->w_cursor.lnum;
4205 del_lines(1L, FALSE);
4206 curwin->w_cursor.lnum = t;
4209 * go to first character of the joined line
4211 curwin->w_cursor.col = currsize;
4212 check_cursor_col();
4213 #ifdef FEAT_VIRTUALEDIT
4214 curwin->w_cursor.coladd = 0;
4215 #endif
4216 curwin->w_set_curswant = TRUE;
4218 return OK;
4221 #ifdef FEAT_COMMENTS
4223 * Return TRUE if the two comment leaders given are the same. "lnum" is
4224 * the first line. White-space is ignored. Note that the whole of
4225 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4227 static int
4228 same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4229 linenr_T lnum;
4230 int leader1_len;
4231 char_u *leader1_flags;
4232 int leader2_len;
4233 char_u *leader2_flags;
4235 int idx1 = 0, idx2 = 0;
4236 char_u *p;
4237 char_u *line1;
4238 char_u *line2;
4240 if (leader1_len == 0)
4241 return (leader2_len == 0);
4244 * If first leader has 'f' flag, the lines can be joined only if the
4245 * second line does not have a leader.
4246 * If first leader has 'e' flag, the lines can never be joined.
4247 * If fist leader has 's' flag, the lines can only be joined if there is
4248 * some text after it and the second line has the 'm' flag.
4250 if (leader1_flags != NULL)
4252 for (p = leader1_flags; *p && *p != ':'; ++p)
4254 if (*p == COM_FIRST)
4255 return (leader2_len == 0);
4256 if (*p == COM_END)
4257 return FALSE;
4258 if (*p == COM_START)
4260 if (*(ml_get(lnum) + leader1_len) == NUL)
4261 return FALSE;
4262 if (leader2_flags == NULL || leader2_len == 0)
4263 return FALSE;
4264 for (p = leader2_flags; *p && *p != ':'; ++p)
4265 if (*p == COM_MIDDLE)
4266 return TRUE;
4267 return FALSE;
4273 * Get current line and next line, compare the leaders.
4274 * The first line has to be saved, only one line can be locked at a time.
4276 line1 = vim_strsave(ml_get(lnum));
4277 if (line1 != NULL)
4279 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4281 line2 = ml_get(lnum + 1);
4282 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4284 if (!vim_iswhite(line2[idx2]))
4286 if (line1[idx1++] != line2[idx2])
4287 break;
4289 else
4290 while (vim_iswhite(line1[idx1]))
4291 ++idx1;
4293 vim_free(line1);
4295 return (idx2 == leader2_len && idx1 == leader1_len);
4297 #endif
4300 * implementation of the format operator 'gq'
4302 void
4303 op_format(oap, keep_cursor)
4304 oparg_T *oap;
4305 int keep_cursor; /* keep cursor on same text char */
4307 long old_line_count = curbuf->b_ml.ml_line_count;
4309 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4310 * can put it back there. */
4311 curwin->w_cursor = oap->cursor_start;
4313 if (u_save((linenr_T)(oap->start.lnum - 1),
4314 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4315 return;
4316 curwin->w_cursor = oap->start;
4318 #ifdef FEAT_VISUAL
4319 if (oap->is_VIsual)
4320 /* When there is no change: need to remove the Visual selection */
4321 redraw_curbuf_later(INVERTED);
4322 #endif
4324 /* Set '[ mark at the start of the formatted area */
4325 curbuf->b_op_start = oap->start;
4327 /* For "gw" remember the cursor position and put it back below (adjusted
4328 * for joined and split lines). */
4329 if (keep_cursor)
4330 saved_cursor = oap->cursor_start;
4332 format_lines(oap->line_count);
4335 * Leave the cursor at the first non-blank of the last formatted line.
4336 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4337 * line, so "." will do the next lines.
4339 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4340 ++curwin->w_cursor.lnum;
4341 beginline(BL_WHITE | BL_FIX);
4342 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4343 msgmore(old_line_count);
4345 /* put '] mark on the end of the formatted area */
4346 curbuf->b_op_end = curwin->w_cursor;
4348 if (keep_cursor)
4350 curwin->w_cursor = saved_cursor;
4351 saved_cursor.lnum = 0;
4354 #ifdef FEAT_VISUAL
4355 if (oap->is_VIsual)
4357 win_T *wp;
4359 FOR_ALL_WINDOWS(wp)
4361 if (wp->w_old_cursor_lnum != 0)
4363 /* When lines have been inserted or deleted, adjust the end of
4364 * the Visual area to be redrawn. */
4365 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4366 wp->w_old_cursor_lnum += old_line_count;
4367 else
4368 wp->w_old_visual_lnum += old_line_count;
4372 #endif
4375 #if defined(FEAT_EVAL) || defined(PROTO)
4377 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4379 void
4380 op_formatexpr(oap)
4381 oparg_T *oap;
4383 # ifdef FEAT_VISUAL
4384 if (oap->is_VIsual)
4385 /* When there is no change: need to remove the Visual selection */
4386 redraw_curbuf_later(INVERTED);
4387 # endif
4389 (void)fex_format(oap->start.lnum, oap->line_count, NUL);
4393 fex_format(lnum, count, c)
4394 linenr_T lnum;
4395 long count;
4396 int c; /* character to be inserted */
4398 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4399 OPT_LOCAL);
4400 int r;
4401 #ifdef FEAT_MBYTE
4402 char_u buf[MB_MAXBYTES];
4403 #else
4404 char_u buf[2];
4405 #endif
4408 * Set v:lnum to the first line number and v:count to the number of lines.
4409 * Set v:char to the character to be inserted (can be NUL).
4411 set_vim_var_nr(VV_LNUM, lnum);
4412 set_vim_var_nr(VV_COUNT, count);
4414 #ifdef FEAT_MBYTE
4415 if (has_mbyte)
4416 buf[(*mb_char2bytes)(c, buf)] = NUL;
4417 else
4418 #endif
4420 buf[0] = c;
4421 buf[1] = NUL;
4423 set_vim_var_string(VV_CHAR, buf, -1);
4426 * Evaluate the function.
4428 if (use_sandbox)
4429 ++sandbox;
4430 r = eval_to_number(curbuf->b_p_fex);
4431 if (use_sandbox)
4432 --sandbox;
4434 set_vim_var_string(VV_CHAR, NULL, -1);
4436 return r;
4438 #endif
4441 * Format "line_count" lines, starting at the cursor position.
4442 * When "line_count" is negative, format until the end of the paragraph.
4443 * Lines after the cursor line are saved for undo, caller must have saved the
4444 * first line.
4446 void
4447 format_lines(line_count)
4448 linenr_T line_count;
4450 int max_len;
4451 int is_not_par; /* current line not part of parag. */
4452 int next_is_not_par; /* next line not part of paragraph */
4453 int is_end_par; /* at end of paragraph */
4454 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4455 int next_is_start_par = FALSE;
4456 #ifdef FEAT_COMMENTS
4457 int leader_len = 0; /* leader len of current line */
4458 int next_leader_len; /* leader len of next line */
4459 char_u *leader_flags = NULL; /* flags for leader of current line */
4460 char_u *next_leader_flags; /* flags for leader of next line */
4461 int do_comments; /* format comments */
4462 #endif
4463 int advance = TRUE;
4464 int second_indent = -1;
4465 int do_second_indent;
4466 int do_number_indent;
4467 int do_trail_white;
4468 int first_par_line = TRUE;
4469 int smd_save;
4470 long count;
4471 int need_set_indent = TRUE; /* set indent of next paragraph */
4472 int force_format = FALSE;
4473 int old_State = State;
4475 /* length of a line to force formatting: 3 * 'tw' */
4476 max_len = comp_textwidth(TRUE) * 3;
4478 /* check for 'q', '2' and '1' in 'formatoptions' */
4479 #ifdef FEAT_COMMENTS
4480 do_comments = has_format_option(FO_Q_COMS);
4481 #endif
4482 do_second_indent = has_format_option(FO_Q_SECOND);
4483 do_number_indent = has_format_option(FO_Q_NUMBER);
4484 do_trail_white = has_format_option(FO_WHITE_PAR);
4487 * Get info about the previous and current line.
4489 if (curwin->w_cursor.lnum > 1)
4490 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4491 #ifdef FEAT_COMMENTS
4492 , &leader_len, &leader_flags, do_comments
4493 #endif
4495 else
4496 is_not_par = TRUE;
4497 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4498 #ifdef FEAT_COMMENTS
4499 , &next_leader_len, &next_leader_flags, do_comments
4500 #endif
4502 is_end_par = (is_not_par || next_is_not_par);
4503 if (!is_end_par && do_trail_white)
4504 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4506 curwin->w_cursor.lnum--;
4507 for (count = line_count; count != 0 && !got_int; --count)
4510 * Advance to next paragraph.
4512 if (advance)
4514 curwin->w_cursor.lnum++;
4515 prev_is_end_par = is_end_par;
4516 is_not_par = next_is_not_par;
4517 #ifdef FEAT_COMMENTS
4518 leader_len = next_leader_len;
4519 leader_flags = next_leader_flags;
4520 #endif
4524 * The last line to be formatted.
4526 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4528 next_is_not_par = TRUE;
4529 #ifdef FEAT_COMMENTS
4530 next_leader_len = 0;
4531 next_leader_flags = NULL;
4532 #endif
4534 else
4536 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4537 #ifdef FEAT_COMMENTS
4538 , &next_leader_len, &next_leader_flags, do_comments
4539 #endif
4541 if (do_number_indent)
4542 next_is_start_par =
4543 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4545 advance = TRUE;
4546 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4547 if (!is_end_par && do_trail_white)
4548 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4551 * Skip lines that are not in a paragraph.
4553 if (is_not_par)
4555 if (line_count < 0)
4556 break;
4558 else
4561 * For the first line of a paragraph, check indent of second line.
4562 * Don't do this for comments and empty lines.
4564 if (first_par_line
4565 && (do_second_indent || do_number_indent)
4566 && prev_is_end_par
4567 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4568 #ifdef FEAT_COMMENTS
4569 && leader_len == 0
4570 && next_leader_len == 0
4571 #endif
4574 if (do_second_indent
4575 && !lineempty(curwin->w_cursor.lnum + 1))
4576 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4577 else if (do_number_indent)
4578 second_indent = get_number_indent(curwin->w_cursor.lnum);
4582 * When the comment leader changes, it's the end of the paragraph.
4584 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4585 #ifdef FEAT_COMMENTS
4586 || !same_leader(curwin->w_cursor.lnum,
4587 leader_len, leader_flags,
4588 next_leader_len, next_leader_flags)
4589 #endif
4591 is_end_par = TRUE;
4594 * If we have got to the end of a paragraph, or the line is
4595 * getting long, format it.
4597 if (is_end_par || force_format)
4599 if (need_set_indent)
4600 /* replace indent in first line with minimal number of
4601 * tabs and spaces, according to current options */
4602 (void)set_indent(get_indent(), SIN_CHANGED);
4604 /* put cursor on last non-space */
4605 State = NORMAL; /* don't go past end-of-line */
4606 coladvance((colnr_T)MAXCOL);
4607 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4608 dec_cursor();
4610 /* do the formatting, without 'showmode' */
4611 State = INSERT; /* for open_line() */
4612 smd_save = p_smd;
4613 p_smd = FALSE;
4614 insertchar(NUL, INSCHAR_FORMAT
4615 #ifdef FEAT_COMMENTS
4616 + (do_comments ? INSCHAR_DO_COM : 0)
4617 #endif
4618 , second_indent);
4619 State = old_State;
4620 p_smd = smd_save;
4621 second_indent = -1;
4622 /* at end of par.: need to set indent of next par. */
4623 need_set_indent = is_end_par;
4624 if (is_end_par)
4626 /* When called with a negative line count, break at the
4627 * end of the paragraph. */
4628 if (line_count < 0)
4629 break;
4630 first_par_line = TRUE;
4632 force_format = FALSE;
4636 * When still in same paragraph, join the lines together. But
4637 * first delete the comment leader from the second line.
4639 if (!is_end_par)
4641 advance = FALSE;
4642 curwin->w_cursor.lnum++;
4643 curwin->w_cursor.col = 0;
4644 if (line_count < 0 && u_save_cursor() == FAIL)
4645 break;
4646 #ifdef FEAT_COMMENTS
4647 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
4648 if (next_leader_len > 0)
4649 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4650 (long)-next_leader_len);
4651 #endif
4652 curwin->w_cursor.lnum--;
4653 if (do_join(TRUE) == FAIL)
4655 beep_flush();
4656 break;
4658 first_par_line = FALSE;
4659 /* If the line is getting long, format it next time */
4660 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4661 force_format = TRUE;
4662 else
4663 force_format = FALSE;
4666 line_breakcheck();
4671 * Return TRUE if line "lnum" ends in a white character.
4673 static int
4674 ends_in_white(lnum)
4675 linenr_T lnum;
4677 char_u *s = ml_get(lnum);
4678 size_t l;
4680 if (*s == NUL)
4681 return FALSE;
4682 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4683 * invocation may call function multiple times". */
4684 l = STRLEN(s) - 1;
4685 return vim_iswhite(s[l]);
4689 * Blank lines, and lines containing only the comment leader, are left
4690 * untouched by the formatting. The function returns TRUE in this
4691 * case. It also returns TRUE when a line starts with the end of a comment
4692 * ('e' in comment flags), so that this line is skipped, and not joined to the
4693 * previous line. A new paragraph starts after a blank line, or when the
4694 * comment leader changes -- webb.
4696 #ifdef FEAT_COMMENTS
4697 static int
4698 fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4699 linenr_T lnum;
4700 int *leader_len;
4701 char_u **leader_flags;
4702 int do_comments;
4704 char_u *flags = NULL; /* init for GCC */
4705 char_u *ptr;
4707 ptr = ml_get(lnum);
4708 if (do_comments)
4709 *leader_len = get_leader_len(ptr, leader_flags, FALSE);
4710 else
4711 *leader_len = 0;
4713 if (*leader_len > 0)
4716 * Search for 'e' flag in comment leader flags.
4718 flags = *leader_flags;
4719 while (*flags && *flags != ':' && *flags != COM_END)
4720 ++flags;
4723 return (*skipwhite(ptr + *leader_len) == NUL
4724 || (*leader_len > 0 && *flags == COM_END)
4725 || startPS(lnum, NUL, FALSE));
4727 #else
4728 static int
4729 fmt_check_par(lnum)
4730 linenr_T lnum;
4732 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4734 #endif
4737 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4738 * previous line is in the same paragraph. Used for auto-formatting.
4741 paragraph_start(lnum)
4742 linenr_T lnum;
4744 char_u *p;
4745 #ifdef FEAT_COMMENTS
4746 int leader_len = 0; /* leader len of current line */
4747 char_u *leader_flags = NULL; /* flags for leader of current line */
4748 int next_leader_len; /* leader len of next line */
4749 char_u *next_leader_flags; /* flags for leader of next line */
4750 int do_comments; /* format comments */
4751 #endif
4753 if (lnum <= 1)
4754 return TRUE; /* start of the file */
4756 p = ml_get(lnum - 1);
4757 if (*p == NUL)
4758 return TRUE; /* after empty line */
4760 #ifdef FEAT_COMMENTS
4761 do_comments = has_format_option(FO_Q_COMS);
4762 #endif
4763 if (fmt_check_par(lnum - 1
4764 #ifdef FEAT_COMMENTS
4765 , &leader_len, &leader_flags, do_comments
4766 #endif
4768 return TRUE; /* after non-paragraph line */
4770 if (fmt_check_par(lnum
4771 #ifdef FEAT_COMMENTS
4772 , &next_leader_len, &next_leader_flags, do_comments
4773 #endif
4775 return TRUE; /* "lnum" is not a paragraph line */
4777 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4778 return TRUE; /* missing trailing space in previous line. */
4780 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4781 return TRUE; /* numbered item starts in "lnum". */
4783 #ifdef FEAT_COMMENTS
4784 if (!same_leader(lnum - 1, leader_len, leader_flags,
4785 next_leader_len, next_leader_flags))
4786 return TRUE; /* change of comment leader. */
4787 #endif
4789 return FALSE;
4792 #ifdef FEAT_VISUAL
4794 * prepare a few things for block mode yank/delete/tilde
4796 * for delete:
4797 * - textlen includes the first/last char to be (partly) deleted
4798 * - start/endspaces is the number of columns that are taken by the
4799 * first/last deleted char minus the number of columns that have to be
4800 * deleted. for yank and tilde:
4801 * - textlen includes the first/last char to be wholly yanked
4802 * - start/endspaces is the number of columns of the first/last yanked char
4803 * that are to be yanked.
4805 static void
4806 block_prep(oap, bdp, lnum, is_del)
4807 oparg_T *oap;
4808 struct block_def *bdp;
4809 linenr_T lnum;
4810 int is_del;
4812 int incr = 0;
4813 char_u *pend;
4814 char_u *pstart;
4815 char_u *line;
4816 char_u *prev_pstart;
4817 char_u *prev_pend;
4819 bdp->startspaces = 0;
4820 bdp->endspaces = 0;
4821 bdp->textlen = 0;
4822 bdp->start_vcol = 0;
4823 bdp->end_vcol = 0;
4824 #ifdef FEAT_VISUALEXTRA
4825 bdp->is_short = FALSE;
4826 bdp->is_oneChar = FALSE;
4827 bdp->pre_whitesp = 0;
4828 bdp->pre_whitesp_c = 0;
4829 bdp->end_char_vcols = 0;
4830 #endif
4831 bdp->start_char_vcols = 0;
4833 line = ml_get(lnum);
4834 pstart = line;
4835 prev_pstart = line;
4836 while (bdp->start_vcol < oap->start_vcol && *pstart)
4838 /* Count a tab for what it's worth (if list mode not on) */
4839 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4840 bdp->start_vcol += incr;
4841 #ifdef FEAT_VISUALEXTRA
4842 if (vim_iswhite(*pstart))
4844 bdp->pre_whitesp += incr;
4845 bdp->pre_whitesp_c++;
4847 else
4849 bdp->pre_whitesp = 0;
4850 bdp->pre_whitesp_c = 0;
4852 #endif
4853 prev_pstart = pstart;
4854 mb_ptr_adv(pstart);
4856 bdp->start_char_vcols = incr;
4857 if (bdp->start_vcol < oap->start_vcol) /* line too short */
4859 bdp->end_vcol = bdp->start_vcol;
4860 #ifdef FEAT_VISUALEXTRA
4861 bdp->is_short = TRUE;
4862 #endif
4863 if (!is_del || oap->op_type == OP_APPEND)
4864 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4866 else
4868 /* notice: this converts partly selected Multibyte characters to
4869 * spaces, too. */
4870 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4871 if (is_del && bdp->startspaces)
4872 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4873 pend = pstart;
4874 bdp->end_vcol = bdp->start_vcol;
4875 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
4877 #ifdef FEAT_VISUALEXTRA
4878 bdp->is_oneChar = TRUE;
4879 #endif
4880 if (oap->op_type == OP_INSERT)
4881 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4882 else if (oap->op_type == OP_APPEND)
4884 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4885 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4887 else
4889 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4890 if (is_del && oap->op_type != OP_LSHIFT)
4892 /* just putting the sum of those two into
4893 * bdp->startspaces doesn't work for Visual replace,
4894 * so we have to split the tab in two */
4895 bdp->startspaces = bdp->start_char_vcols
4896 - (bdp->start_vcol - oap->start_vcol);
4897 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4901 else
4903 prev_pend = pend;
4904 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4906 /* Count a tab for what it's worth (if list mode not on) */
4907 prev_pend = pend;
4908 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4909 bdp->end_vcol += incr;
4911 if (bdp->end_vcol <= oap->end_vcol
4912 && (!is_del
4913 || oap->op_type == OP_APPEND
4914 || oap->op_type == OP_REPLACE)) /* line too short */
4916 #ifdef FEAT_VISUALEXTRA
4917 bdp->is_short = TRUE;
4918 #endif
4919 /* Alternative: include spaces to fill up the block.
4920 * Disadvantage: can lead to trailing spaces when the line is
4921 * short where the text is put */
4922 /* if (!is_del || oap->op_type == OP_APPEND) */
4923 if (oap->op_type == OP_APPEND || virtual_op)
4924 bdp->endspaces = oap->end_vcol - bdp->end_vcol
4925 + oap->inclusive;
4926 else
4927 bdp->endspaces = 0; /* replace doesn't add characters */
4929 else if (bdp->end_vcol > oap->end_vcol)
4931 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4932 if (!is_del && bdp->endspaces)
4934 bdp->endspaces = incr - bdp->endspaces;
4935 if (pend != pstart)
4936 pend = prev_pend;
4940 #ifdef FEAT_VISUALEXTRA
4941 bdp->end_char_vcols = incr;
4942 #endif
4943 if (is_del && bdp->startspaces)
4944 pstart = prev_pstart;
4945 bdp->textlen = (int)(pend - pstart);
4947 bdp->textcol = (colnr_T) (pstart - line);
4948 bdp->textstart = pstart;
4950 #endif /* FEAT_VISUAL */
4952 #ifdef FEAT_RIGHTLEFT
4953 static void reverse_line __ARGS((char_u *s));
4955 static void
4956 reverse_line(s)
4957 char_u *s;
4959 int i, j;
4960 char_u c;
4962 if ((i = (int)STRLEN(s) - 1) <= 0)
4963 return;
4965 curwin->w_cursor.col = i - curwin->w_cursor.col;
4966 for (j = 0; j < i; j++, i--)
4968 c = s[i]; s[i] = s[j]; s[j] = c;
4972 # define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4973 #else
4974 # define RLADDSUBFIX(ptr)
4975 #endif
4978 * add or subtract 'Prenum1' from a number in a line
4979 * 'command' is CTRL-A for add, CTRL-X for subtract
4981 * return FAIL for failure, OK otherwise
4984 do_addsub(command, Prenum1)
4985 int command;
4986 linenr_T Prenum1;
4988 int col;
4989 char_u *buf1;
4990 char_u buf2[NUMBUFLEN];
4991 int hex; /* 'X' or 'x': hex; '0': octal */
4992 static int hexupper = FALSE; /* 0xABC */
4993 unsigned long n;
4994 long_u oldn;
4995 char_u *ptr;
4996 int c;
4997 int length = 0; /* character length of the number */
4998 int todel;
4999 int dohex;
5000 int dooct;
5001 int doalp;
5002 int firstdigit;
5003 int negative;
5004 int subtract;
5006 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5007 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5008 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5010 ptr = ml_get_curline();
5011 RLADDSUBFIX(ptr);
5014 * First check if we are on a hexadecimal number, after the "0x".
5016 col = curwin->w_cursor.col;
5017 if (dohex)
5018 while (col > 0 && vim_isxdigit(ptr[col]))
5019 --col;
5020 if ( dohex
5021 && col > 0
5022 && (ptr[col] == 'X'
5023 || ptr[col] == 'x')
5024 && ptr[col - 1] == '0'
5025 && vim_isxdigit(ptr[col + 1]))
5028 * Found hexadecimal number, move to its start.
5030 --col;
5032 else
5035 * Search forward and then backward to find the start of number.
5037 col = curwin->w_cursor.col;
5039 while (ptr[col] != NUL
5040 && !vim_isdigit(ptr[col])
5041 && !(doalp && ASCII_ISALPHA(ptr[col])))
5042 ++col;
5044 while (col > 0
5045 && vim_isdigit(ptr[col - 1])
5046 && !(doalp && ASCII_ISALPHA(ptr[col])))
5047 --col;
5051 * If a number was found, and saving for undo works, replace the number.
5053 firstdigit = ptr[col];
5054 RLADDSUBFIX(ptr);
5055 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5056 || u_save_cursor() != OK)
5058 beep_flush();
5059 return FAIL;
5062 /* get ptr again, because u_save() may have changed it */
5063 ptr = ml_get_curline();
5064 RLADDSUBFIX(ptr);
5066 if (doalp && ASCII_ISALPHA(firstdigit))
5068 /* decrement or increment alphabetic character */
5069 if (command == Ctrl_X)
5071 if (CharOrd(firstdigit) < Prenum1)
5073 if (isupper(firstdigit))
5074 firstdigit = 'A';
5075 else
5076 firstdigit = 'a';
5078 else
5079 #ifdef EBCDIC
5080 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5081 #else
5082 firstdigit -= Prenum1;
5083 #endif
5085 else
5087 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5089 if (isupper(firstdigit))
5090 firstdigit = 'Z';
5091 else
5092 firstdigit = 'z';
5094 else
5095 #ifdef EBCDIC
5096 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5097 #else
5098 firstdigit += Prenum1;
5099 #endif
5101 curwin->w_cursor.col = col;
5102 (void)del_char(FALSE);
5103 ins_char(firstdigit);
5105 else
5107 negative = FALSE;
5108 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5110 --col;
5111 negative = TRUE;
5114 /* get the number value (unsigned) */
5115 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5117 /* ignore leading '-' for hex and octal numbers */
5118 if (hex && negative)
5120 ++col;
5121 --length;
5122 negative = FALSE;
5125 /* add or subtract */
5126 subtract = FALSE;
5127 if (command == Ctrl_X)
5128 subtract ^= TRUE;
5129 if (negative)
5130 subtract ^= TRUE;
5132 oldn = n;
5133 if (subtract)
5134 n -= (unsigned long)Prenum1;
5135 else
5136 n += (unsigned long)Prenum1;
5138 /* handle wraparound for decimal numbers */
5139 if (!hex)
5141 if (subtract)
5143 if (n > oldn)
5145 n = 1 + (n ^ (unsigned long)-1);
5146 negative ^= TRUE;
5149 else /* add */
5151 if (n < oldn)
5153 n = (n ^ (unsigned long)-1);
5154 negative ^= TRUE;
5157 if (n == 0)
5158 negative = FALSE;
5162 * Delete the old number.
5164 curwin->w_cursor.col = col;
5165 todel = length;
5166 c = gchar_cursor();
5168 * Don't include the '-' in the length, only the length of the part
5169 * after it is kept the same.
5171 if (c == '-')
5172 --length;
5173 while (todel-- > 0)
5175 if (c < 0x100 && isalpha(c))
5177 if (isupper(c))
5178 hexupper = TRUE;
5179 else
5180 hexupper = FALSE;
5182 /* del_char() will mark line needing displaying */
5183 (void)del_char(FALSE);
5184 c = gchar_cursor();
5188 * Prepare the leading characters in buf1[].
5189 * When there are many leading zeros it could be very long. Allocate
5190 * a bit too much.
5192 buf1 = alloc((unsigned)length + NUMBUFLEN);
5193 if (buf1 == NULL)
5194 return FAIL;
5195 ptr = buf1;
5196 if (negative)
5198 *ptr++ = '-';
5200 if (hex)
5202 *ptr++ = '0';
5203 --length;
5205 if (hex == 'x' || hex == 'X')
5207 *ptr++ = hex;
5208 --length;
5212 * Put the number characters in buf2[].
5214 if (hex == 0)
5215 sprintf((char *)buf2, "%lu", n);
5216 else if (hex == '0')
5217 sprintf((char *)buf2, "%lo", n);
5218 else if (hex && hexupper)
5219 sprintf((char *)buf2, "%lX", n);
5220 else
5221 sprintf((char *)buf2, "%lx", n);
5222 length -= (int)STRLEN(buf2);
5225 * Adjust number of zeros to the new number of digits, so the
5226 * total length of the number remains the same.
5227 * Don't do this when
5228 * the result may look like an octal number.
5230 if (firstdigit == '0' && !(dooct && hex == 0))
5231 while (length-- > 0)
5232 *ptr++ = '0';
5233 *ptr = NUL;
5234 STRCAT(buf1, buf2);
5235 ins_str(buf1); /* insert the new number */
5236 vim_free(buf1);
5238 --curwin->w_cursor.col;
5239 curwin->w_set_curswant = TRUE;
5240 #ifdef FEAT_RIGHTLEFT
5241 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5242 RLADDSUBFIX(ptr);
5243 #endif
5244 return OK;
5247 #ifdef FEAT_VIMINFO
5249 read_viminfo_register(virp, force)
5250 vir_T *virp;
5251 int force;
5253 int eof;
5254 int do_it = TRUE;
5255 int size;
5256 int limit;
5257 int i;
5258 int set_prev = FALSE;
5259 char_u *str;
5260 char_u **array = NULL;
5262 /* We only get here (hopefully) if line[0] == '"' */
5263 str = virp->vir_line + 1;
5264 if (*str == '"')
5266 set_prev = TRUE;
5267 str++;
5269 if (!ASCII_ISALNUM(*str) && *str != '-')
5271 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5272 return TRUE; /* too many errors, pretend end-of-file */
5273 do_it = FALSE;
5275 get_yank_register(*str++, FALSE);
5276 if (!force && y_current->y_array != NULL)
5277 do_it = FALSE;
5278 size = 0;
5279 limit = 100; /* Optimized for registers containing <= 100 lines */
5280 if (do_it)
5282 if (set_prev)
5283 y_previous = y_current;
5284 vim_free(y_current->y_array);
5285 array = y_current->y_array =
5286 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5287 str = skipwhite(str);
5288 if (STRNCMP(str, "CHAR", 4) == 0)
5289 y_current->y_type = MCHAR;
5290 #ifdef FEAT_VISUAL
5291 else if (STRNCMP(str, "BLOCK", 5) == 0)
5292 y_current->y_type = MBLOCK;
5293 #endif
5294 else
5295 y_current->y_type = MLINE;
5296 /* get the block width; if it's missing we get a zero, which is OK */
5297 str = skipwhite(skiptowhite(str));
5298 #ifdef FEAT_VISUAL
5299 y_current->y_width = getdigits(&str);
5300 #else
5301 (void)getdigits(&str);
5302 #endif
5305 while (!(eof = viminfo_readline(virp))
5306 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5308 if (do_it)
5310 if (size >= limit)
5312 y_current->y_array = (char_u **)
5313 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5314 for (i = 0; i < limit; i++)
5315 y_current->y_array[i] = array[i];
5316 vim_free(array);
5317 limit *= 2;
5318 array = y_current->y_array;
5320 str = viminfo_readstring(virp, 1, TRUE);
5321 if (str != NULL)
5322 array[size++] = str;
5323 else
5324 do_it = FALSE;
5327 if (do_it)
5329 if (size == 0)
5331 vim_free(array);
5332 y_current->y_array = NULL;
5334 else if (size < limit)
5336 y_current->y_array =
5337 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5338 for (i = 0; i < size; i++)
5339 y_current->y_array[i] = array[i];
5340 vim_free(array);
5342 y_current->y_size = size;
5344 return eof;
5347 void
5348 write_viminfo_registers(fp)
5349 FILE *fp;
5351 int i, j;
5352 char_u *type;
5353 char_u c;
5354 int num_lines;
5355 int max_num_lines;
5356 int max_kbyte;
5357 long len;
5359 fprintf(fp, _("\n# Registers:\n"));
5361 /* Get '<' value, use old '"' value if '<' is not found. */
5362 max_num_lines = get_viminfo_parameter('<');
5363 if (max_num_lines < 0)
5364 max_num_lines = get_viminfo_parameter('"');
5365 if (max_num_lines == 0)
5366 return;
5367 max_kbyte = get_viminfo_parameter('s');
5368 if (max_kbyte == 0)
5369 return;
5370 for (i = 0; i < NUM_REGISTERS; i++)
5372 if (y_regs[i].y_array == NULL)
5373 continue;
5374 #ifdef FEAT_CLIPBOARD
5375 /* Skip '*'/'+' register, we don't want them back next time */
5376 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5377 continue;
5378 #endif
5379 #ifdef FEAT_DND
5380 /* Neither do we want the '~' register */
5381 if (i == TILDE_REGISTER)
5382 continue;
5383 #endif
5384 /* Skip empty registers. */
5385 num_lines = y_regs[i].y_size;
5386 if (num_lines == 0
5387 || (num_lines == 1 && y_regs[i].y_type == MCHAR
5388 && STRLEN(y_regs[i].y_array[0]) == 0))
5389 continue;
5391 if (max_kbyte > 0)
5393 /* Skip register if there is more text than the maximum size. */
5394 len = 0;
5395 for (j = 0; j < num_lines; j++)
5396 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
5397 if (len > (long)max_kbyte * 1024L)
5398 continue;
5401 switch (y_regs[i].y_type)
5403 case MLINE:
5404 type = (char_u *)"LINE";
5405 break;
5406 case MCHAR:
5407 type = (char_u *)"CHAR";
5408 break;
5409 #ifdef FEAT_VISUAL
5410 case MBLOCK:
5411 type = (char_u *)"BLOCK";
5412 break;
5413 #endif
5414 default:
5415 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
5416 y_regs[i].y_type);
5417 emsg(IObuff);
5418 type = (char_u *)"LINE";
5419 break;
5421 if (y_previous == &y_regs[i])
5422 fprintf(fp, "\"");
5423 c = get_register_name(i);
5424 fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5425 #ifdef FEAT_VISUAL
5426 (int)y_regs[i].y_width
5427 #else
5429 #endif
5432 /* If max_num_lines < 0, then we save ALL the lines in the register */
5433 if (max_num_lines > 0 && num_lines > max_num_lines)
5434 num_lines = max_num_lines;
5435 for (j = 0; j < num_lines; j++)
5437 putc('\t', fp);
5438 viminfo_writestring(fp, y_regs[i].y_array[j]);
5442 #endif /* FEAT_VIMINFO */
5444 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
5446 * SELECTION / PRIMARY ('*')
5448 * Text selection stuff that uses the GUI selection register '*'. When using a
5449 * GUI this may be text from another window, otherwise it is the last text we
5450 * had highlighted with VIsual mode. With mouse support, clicking the middle
5451 * button performs the paste, otherwise you will need to do <"*p>. "
5452 * If not under X, it is synonymous with the clipboard register '+'.
5454 * X CLIPBOARD ('+')
5456 * Text selection stuff that uses the GUI clipboard register '+'.
5457 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5458 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5459 * otherwise you will need to do <"+p>. "
5460 * If not under X, it is synonymous with the selection register '*'.
5464 * Routine to export any final X selection we had to the environment
5465 * so that the text is still available after vim has exited. X selections
5466 * only exist while the owning application exists, so we write to the
5467 * permanent (while X runs) store CUT_BUFFER0.
5468 * Dump the CLIPBOARD selection if we own it (it's logically the more
5469 * 'permanent' of the two), otherwise the PRIMARY one.
5470 * For now, use a hard-coded sanity limit of 1Mb of data.
5472 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5473 void
5474 x11_export_final_selection()
5476 Display *dpy;
5477 char_u *str = NULL;
5478 long_u len = 0;
5479 int motion_type = -1;
5481 # ifdef FEAT_GUI
5482 if (gui.in_use)
5483 dpy = X_DISPLAY;
5484 else
5485 # endif
5486 # ifdef FEAT_XCLIPBOARD
5487 dpy = xterm_dpy;
5488 # else
5489 return;
5490 # endif
5492 /* Get selection to export */
5493 if (clip_plus.owned)
5494 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5495 else if (clip_star.owned)
5496 motion_type = clip_convert_selection(&str, &len, &clip_star);
5498 /* Check it's OK */
5499 if (dpy != NULL && str != NULL && motion_type >= 0
5500 && len < 1024*1024 && len > 0)
5502 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5503 XFlush(dpy);
5506 vim_free(str);
5508 #endif
5510 void
5511 clip_free_selection(cbd)
5512 VimClipboard *cbd;
5514 struct yankreg *y_ptr = y_current;
5516 if (cbd == &clip_plus)
5517 y_current = &y_regs[PLUS_REGISTER];
5518 else
5519 y_current = &y_regs[STAR_REGISTER];
5520 free_yank_all();
5521 y_current->y_size = 0;
5522 y_current = y_ptr;
5526 * Get the selected text and put it in the gui selection register '*' or '+'.
5528 void
5529 clip_get_selection(cbd)
5530 VimClipboard *cbd;
5532 struct yankreg *old_y_previous, *old_y_current;
5533 pos_T old_cursor;
5534 #ifdef FEAT_VISUAL
5535 pos_T old_visual;
5536 int old_visual_mode;
5537 #endif
5538 colnr_T old_curswant;
5539 int old_set_curswant;
5540 pos_T old_op_start, old_op_end;
5541 oparg_T oa;
5542 cmdarg_T ca;
5544 if (cbd->owned)
5546 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5547 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5548 return;
5550 /* Get the text between clip_star.start & clip_star.end */
5551 old_y_previous = y_previous;
5552 old_y_current = y_current;
5553 old_cursor = curwin->w_cursor;
5554 old_curswant = curwin->w_curswant;
5555 old_set_curswant = curwin->w_set_curswant;
5556 old_op_start = curbuf->b_op_start;
5557 old_op_end = curbuf->b_op_end;
5558 #ifdef FEAT_VISUAL
5559 old_visual = VIsual;
5560 old_visual_mode = VIsual_mode;
5561 #endif
5562 clear_oparg(&oa);
5563 oa.regname = (cbd == &clip_plus ? '+' : '*');
5564 oa.op_type = OP_YANK;
5565 vim_memset(&ca, 0, sizeof(ca));
5566 ca.oap = &oa;
5567 ca.cmdchar = 'y';
5568 ca.count1 = 1;
5569 ca.retval = CA_NO_ADJ_OP_END;
5570 do_pending_operator(&ca, 0, TRUE);
5571 y_previous = old_y_previous;
5572 y_current = old_y_current;
5573 curwin->w_cursor = old_cursor;
5574 changed_cline_bef_curs(); /* need to update w_virtcol et al */
5575 curwin->w_curswant = old_curswant;
5576 curwin->w_set_curswant = old_set_curswant;
5577 curbuf->b_op_start = old_op_start;
5578 curbuf->b_op_end = old_op_end;
5579 #ifdef FEAT_VISUAL
5580 VIsual = old_visual;
5581 VIsual_mode = old_visual_mode;
5582 #endif
5584 else
5586 clip_free_selection(cbd);
5588 /* Try to get selected text from another window */
5589 clip_gen_request_selection(cbd);
5593 /* Convert from the GUI selection string into the '*'/'+' register */
5594 void
5595 clip_yank_selection(type, str, len, cbd)
5596 int type;
5597 char_u *str;
5598 long len;
5599 VimClipboard *cbd;
5601 struct yankreg *y_ptr;
5603 if (cbd == &clip_plus)
5604 y_ptr = &y_regs[PLUS_REGISTER];
5605 else
5606 y_ptr = &y_regs[STAR_REGISTER];
5608 clip_free_selection(cbd);
5610 str_to_reg(y_ptr, type, str, len, 0L);
5614 * Convert the '*'/'+' register into a GUI selection string returned in *str
5615 * with length *len.
5616 * Returns the motion type, or -1 for failure.
5619 clip_convert_selection(str, len, cbd)
5620 char_u **str;
5621 long_u *len;
5622 VimClipboard *cbd;
5624 char_u *p;
5625 int lnum;
5626 int i, j;
5627 int_u eolsize;
5628 struct yankreg *y_ptr;
5630 if (cbd == &clip_plus)
5631 y_ptr = &y_regs[PLUS_REGISTER];
5632 else
5633 y_ptr = &y_regs[STAR_REGISTER];
5635 #ifdef USE_CRNL
5636 eolsize = 2;
5637 #else
5638 eolsize = 1;
5639 #endif
5641 *str = NULL;
5642 *len = 0;
5643 if (y_ptr->y_array == NULL)
5644 return -1;
5646 for (i = 0; i < y_ptr->y_size; i++)
5647 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5650 * Don't want newline character at end of last line if we're in MCHAR mode.
5652 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5653 *len -= eolsize;
5655 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5656 if (p == NULL)
5657 return -1;
5658 lnum = 0;
5659 for (i = 0, j = 0; i < (int)*len; i++, j++)
5661 if (y_ptr->y_array[lnum][j] == '\n')
5662 p[i] = NUL;
5663 else if (y_ptr->y_array[lnum][j] == NUL)
5665 #ifdef USE_CRNL
5666 p[i++] = '\r';
5667 #endif
5668 #ifdef USE_CR
5669 p[i] = '\r';
5670 #else
5671 p[i] = '\n';
5672 #endif
5673 lnum++;
5674 j = -1;
5676 else
5677 p[i] = y_ptr->y_array[lnum][j];
5679 return y_ptr->y_type;
5683 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5685 * If we have written to a clipboard register, send the text to the clipboard.
5687 static void
5688 may_set_selection()
5690 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5692 clip_own_selection(&clip_star);
5693 clip_gen_set_selection(&clip_star);
5695 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5697 clip_own_selection(&clip_plus);
5698 clip_gen_set_selection(&clip_plus);
5701 # endif
5703 #endif /* FEAT_CLIPBOARD || PROTO */
5706 #if defined(FEAT_DND) || defined(PROTO)
5708 * Replace the contents of the '~' register with str.
5710 void
5711 dnd_yank_drag_data(str, len)
5712 char_u *str;
5713 long len;
5715 struct yankreg *curr;
5717 curr = y_current;
5718 y_current = &y_regs[TILDE_REGISTER];
5719 free_yank_all();
5720 str_to_reg(y_current, MCHAR, str, len, 0L);
5721 y_current = curr;
5723 #endif
5726 #if defined(FEAT_EVAL) || defined(PROTO)
5728 * Return the type of a register.
5729 * Used for getregtype()
5730 * Returns MAUTO for error.
5732 char_u
5733 get_reg_type(regname, reglen)
5734 int regname;
5735 long *reglen;
5737 switch (regname)
5739 case '%': /* file name */
5740 case '#': /* alternate file name */
5741 case '=': /* expression */
5742 case ':': /* last command line */
5743 case '/': /* last search-pattern */
5744 case '.': /* last inserted text */
5745 #ifdef FEAT_SEARCHPATH
5746 case Ctrl_F: /* Filename under cursor */
5747 case Ctrl_P: /* Path under cursor, expand via "path" */
5748 #endif
5749 case Ctrl_W: /* word under cursor */
5750 case Ctrl_A: /* WORD (mnemonic All) under cursor */
5751 case '_': /* black hole: always empty */
5752 return MCHAR;
5755 #ifdef FEAT_CLIPBOARD
5756 regname = may_get_selection(regname);
5757 #endif
5759 /* Should we check for a valid name? */
5760 get_yank_register(regname, FALSE);
5762 if (y_current->y_array != NULL)
5764 #ifdef FEAT_VISUAL
5765 if (reglen != NULL && y_current->y_type == MBLOCK)
5766 *reglen = y_current->y_width;
5767 #endif
5768 return y_current->y_type;
5770 return MAUTO;
5774 * Return the contents of a register as a single allocated string.
5775 * Used for "@r" in expressions and for getreg().
5776 * Returns NULL for error.
5778 char_u *
5779 get_reg_contents(regname, allowexpr, expr_src)
5780 int regname;
5781 int allowexpr; /* allow "=" register */
5782 int expr_src; /* get expression for "=" register */
5784 long i;
5785 char_u *retval;
5786 int allocated;
5787 long len;
5789 /* Don't allow using an expression register inside an expression */
5790 if (regname == '=')
5792 if (allowexpr)
5794 if (expr_src)
5795 return get_expr_line_src();
5796 return get_expr_line();
5798 return NULL;
5801 if (regname == '@') /* "@@" is used for unnamed register */
5802 regname = '"';
5804 /* check for valid regname */
5805 if (regname != NUL && !valid_yank_reg(regname, FALSE))
5806 return NULL;
5808 #ifdef FEAT_CLIPBOARD
5809 regname = may_get_selection(regname);
5810 #endif
5812 if (get_spec_reg(regname, &retval, &allocated, FALSE))
5814 if (retval == NULL)
5815 return NULL;
5816 if (!allocated)
5817 retval = vim_strsave(retval);
5818 return retval;
5821 get_yank_register(regname, FALSE);
5822 if (y_current->y_array == NULL)
5823 return NULL;
5826 * Compute length of resulting string.
5828 len = 0;
5829 for (i = 0; i < y_current->y_size; ++i)
5831 len += (long)STRLEN(y_current->y_array[i]);
5833 * Insert a newline between lines and after last line if
5834 * y_type is MLINE.
5836 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5837 ++len;
5840 retval = lalloc(len + 1, TRUE);
5843 * Copy the lines of the yank register into the string.
5845 if (retval != NULL)
5847 len = 0;
5848 for (i = 0; i < y_current->y_size; ++i)
5850 STRCPY(retval + len, y_current->y_array[i]);
5851 len += (long)STRLEN(retval + len);
5854 * Insert a NL between lines and after the last line if y_type is
5855 * MLINE.
5857 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5858 retval[len++] = '\n';
5860 retval[len] = NUL;
5863 return retval;
5867 * Store string "str" in register "name".
5868 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5869 * If "must_append" is TRUE, always append to the register. Otherwise append
5870 * if "name" is an uppercase letter.
5871 * Note: "maxlen" and "must_append" don't work for the "/" register.
5872 * Careful: 'str' is modified, you may have to use a copy!
5873 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5875 void
5876 write_reg_contents(name, str, maxlen, must_append)
5877 int name;
5878 char_u *str;
5879 int maxlen;
5880 int must_append;
5882 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5885 void
5886 write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5887 int name;
5888 char_u *str;
5889 int maxlen;
5890 int must_append;
5891 int yank_type;
5892 long block_len;
5894 struct yankreg *old_y_previous, *old_y_current;
5895 long len;
5897 if (maxlen >= 0)
5898 len = maxlen;
5899 else
5900 len = (long)STRLEN(str);
5902 /* Special case: '/' search pattern */
5903 if (name == '/')
5905 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5906 return;
5909 #ifdef FEAT_EVAL
5910 if (name == '=')
5912 char_u *p, *s;
5914 p = vim_strnsave(str, (int)len);
5915 if (p == NULL)
5916 return;
5917 if (must_append)
5919 s = concat_str(get_expr_line_src(), p);
5920 vim_free(p);
5921 p = s;
5924 set_expr_line(p);
5925 return;
5927 #endif
5929 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
5931 emsg_invreg(name);
5932 return;
5935 if (name == '_') /* black hole: nothing to do */
5936 return;
5938 /* Don't want to change the current (unnamed) register */
5939 old_y_previous = y_previous;
5940 old_y_current = y_current;
5942 get_yank_register(name, TRUE);
5943 if (!y_append && !must_append)
5944 free_yank_all();
5945 #ifndef FEAT_VISUAL
5946 /* Just in case - make sure we don't use MBLOCK */
5947 if (yank_type == MBLOCK)
5948 yank_type = MAUTO;
5949 #endif
5950 if (yank_type == MAUTO)
5951 yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5952 ? MLINE : MCHAR);
5953 str_to_reg(y_current, yank_type, str, len, block_len);
5955 # ifdef FEAT_CLIPBOARD
5956 /* Send text of clipboard register to the clipboard. */
5957 may_set_selection();
5958 # endif
5960 /* ':let @" = "val"' should change the meaning of the "" register */
5961 if (name != '"')
5962 y_previous = old_y_previous;
5963 y_current = old_y_current;
5965 #endif /* FEAT_EVAL */
5967 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5969 * Put a string into a register. When the register is not empty, the string
5970 * is appended.
5972 static void
5973 str_to_reg(y_ptr, type, str, len, blocklen)
5974 struct yankreg *y_ptr; /* pointer to yank register */
5975 int type; /* MCHAR, MLINE or MBLOCK */
5976 char_u *str; /* string to put in register */
5977 long len; /* length of string */
5978 long blocklen; /* width of Visual block */
5980 int lnum;
5981 long start;
5982 long i;
5983 int extra;
5984 int newlines; /* number of lines added */
5985 int extraline = 0; /* extra line at the end */
5986 int append = FALSE; /* append to last line in register */
5987 char_u *s;
5988 char_u **pp;
5989 #ifdef FEAT_VISUAL
5990 long maxlen;
5991 #endif
5993 if (y_ptr->y_array == NULL) /* NULL means emtpy register */
5994 y_ptr->y_size = 0;
5997 * Count the number of lines within the string
5999 newlines = 0;
6000 for (i = 0; i < len; i++)
6001 if (str[i] == '\n')
6002 ++newlines;
6003 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6005 extraline = 1;
6006 ++newlines; /* count extra newline at the end */
6008 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6010 append = TRUE;
6011 --newlines; /* uncount newline when appending first line */
6015 * Allocate an array to hold the pointers to the new register lines.
6016 * If the register was not empty, move the existing lines to the new array.
6018 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6019 * sizeof(char_u *), TRUE);
6020 if (pp == NULL) /* out of memory */
6021 return;
6022 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6023 pp[lnum] = y_ptr->y_array[lnum];
6024 vim_free(y_ptr->y_array);
6025 y_ptr->y_array = pp;
6026 #ifdef FEAT_VISUAL
6027 maxlen = 0;
6028 #endif
6031 * Find the end of each line and save it into the array.
6033 for (start = 0; start < len + extraline; start += i + 1)
6035 for (i = start; i < len; ++i) /* find the end of the line */
6036 if (str[i] == '\n')
6037 break;
6038 i -= start; /* i is now length of line */
6039 #ifdef FEAT_VISUAL
6040 if (i > maxlen)
6041 maxlen = i;
6042 #endif
6043 if (append)
6045 --lnum;
6046 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6048 else
6049 extra = 0;
6050 s = alloc((unsigned)(i + extra + 1));
6051 if (s == NULL)
6052 break;
6053 if (extra)
6054 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6055 if (append)
6056 vim_free(y_ptr->y_array[lnum]);
6057 if (i)
6058 mch_memmove(s + extra, str + start, (size_t)i);
6059 extra += i;
6060 s[extra] = NUL;
6061 y_ptr->y_array[lnum++] = s;
6062 while (--extra >= 0)
6064 if (*s == NUL)
6065 *s = '\n'; /* replace NUL with newline */
6066 ++s;
6068 append = FALSE; /* only first line is appended */
6070 y_ptr->y_type = type;
6071 y_ptr->y_size = lnum;
6072 # ifdef FEAT_VISUAL
6073 if (type == MBLOCK)
6074 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6075 else
6076 y_ptr->y_width = 0;
6077 # endif
6079 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6081 void
6082 clear_oparg(oap)
6083 oparg_T *oap;
6085 vim_memset(oap, 0, sizeof(oparg_T));
6088 static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
6091 * Count the number of bytes, characters and "words" in a line.
6093 * "Words" are counted by looking for boundaries between non-space and
6094 * space characters. (it seems to produce results that match 'wc'.)
6096 * Return value is byte count; word count for the line is added to "*wc".
6097 * Char count is added to "*cc".
6099 * The function will only examine the first "limit" characters in the
6100 * line, stopping if it encounters an end-of-line (NUL byte). In that
6101 * case, eol_size will be added to the character count to account for
6102 * the size of the EOL character.
6104 static long
6105 line_count_info(line, wc, cc, limit, eol_size)
6106 char_u *line;
6107 long *wc;
6108 long *cc;
6109 long limit;
6110 int eol_size;
6112 long i;
6113 long words = 0;
6114 long chars = 0;
6115 int is_word = 0;
6117 for (i = 0; line[i] && i < limit; )
6119 if (is_word)
6121 if (vim_isspace(line[i]))
6123 words++;
6124 is_word = 0;
6127 else if (!vim_isspace(line[i]))
6128 is_word = 1;
6129 ++chars;
6130 #ifdef FEAT_MBYTE
6131 i += (*mb_ptr2len)(line + i);
6132 #else
6133 ++i;
6134 #endif
6137 if (is_word)
6138 words++;
6139 *wc += words;
6141 /* Add eol_size if the end of line was reached before hitting limit. */
6142 if (line[i] == NUL && i < limit)
6144 i += eol_size;
6145 chars += eol_size;
6147 *cc += chars;
6148 return i;
6152 * Give some info about the position of the cursor (for "g CTRL-G").
6153 * In Visual mode, give some info about the selected region. (In this case,
6154 * the *_count_cursor variables store running totals for the selection.)
6156 void
6157 cursor_pos_info()
6159 char_u *p;
6160 char_u buf1[50];
6161 char_u buf2[40];
6162 linenr_T lnum;
6163 long byte_count = 0;
6164 long byte_count_cursor = 0;
6165 long char_count = 0;
6166 long char_count_cursor = 0;
6167 long word_count = 0;
6168 long word_count_cursor = 0;
6169 int eol_size;
6170 long last_check = 100000L;
6171 #ifdef FEAT_VISUAL
6172 long line_count_selected = 0;
6173 pos_T min_pos, max_pos;
6174 oparg_T oparg;
6175 struct block_def bd;
6176 #endif
6179 * Compute the length of the file in characters.
6181 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6183 MSG(_(no_lines_msg));
6185 else
6187 if (get_fileformat(curbuf) == EOL_DOS)
6188 eol_size = 2;
6189 else
6190 eol_size = 1;
6192 #ifdef FEAT_VISUAL
6193 if (VIsual_active)
6195 if (lt(VIsual, curwin->w_cursor))
6197 min_pos = VIsual;
6198 max_pos = curwin->w_cursor;
6200 else
6202 min_pos = curwin->w_cursor;
6203 max_pos = VIsual;
6205 if (*p_sel == 'e' && max_pos.col > 0)
6206 --max_pos.col;
6208 if (VIsual_mode == Ctrl_V)
6210 oparg.is_VIsual = 1;
6211 oparg.block_mode = TRUE;
6212 oparg.op_type = OP_NOP;
6213 getvcols(curwin, &min_pos, &max_pos,
6214 &oparg.start_vcol, &oparg.end_vcol);
6215 if (curwin->w_curswant == MAXCOL)
6216 oparg.end_vcol = MAXCOL;
6217 /* Swap the start, end vcol if needed */
6218 if (oparg.end_vcol < oparg.start_vcol)
6220 oparg.end_vcol += oparg.start_vcol;
6221 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6222 oparg.end_vcol -= oparg.start_vcol;
6225 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6227 #endif
6229 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6231 /* Check for a CTRL-C every 100000 characters. */
6232 if (byte_count > last_check)
6234 ui_breakcheck();
6235 if (got_int)
6236 return;
6237 last_check = byte_count + 100000L;
6240 #ifdef FEAT_VISUAL
6241 /* Do extra processing for VIsual mode. */
6242 if (VIsual_active
6243 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6245 char_u *s = NULL;
6246 long len = 0L;
6248 switch (VIsual_mode)
6250 case Ctrl_V:
6251 # ifdef FEAT_VIRTUALEDIT
6252 virtual_op = virtual_active();
6253 # endif
6254 block_prep(&oparg, &bd, lnum, 0);
6255 # ifdef FEAT_VIRTUALEDIT
6256 virtual_op = MAYBE;
6257 # endif
6258 s = bd.textstart;
6259 len = (long)bd.textlen;
6260 break;
6261 case 'V':
6262 s = ml_get(lnum);
6263 len = MAXCOL;
6264 break;
6265 case 'v':
6267 colnr_T start_col = (lnum == min_pos.lnum)
6268 ? min_pos.col : 0;
6269 colnr_T end_col = (lnum == max_pos.lnum)
6270 ? max_pos.col - start_col + 1 : MAXCOL;
6272 s = ml_get(lnum) + start_col;
6273 len = end_col;
6275 break;
6277 if (s != NULL)
6279 byte_count_cursor += line_count_info(s, &word_count_cursor,
6280 &char_count_cursor, len, eol_size);
6281 if (lnum == curbuf->b_ml.ml_line_count
6282 && !curbuf->b_p_eol
6283 && curbuf->b_p_bin
6284 && (long)STRLEN(s) < len)
6285 byte_count_cursor -= eol_size;
6288 else
6289 #endif
6291 /* In non-visual mode, check for the line the cursor is on */
6292 if (lnum == curwin->w_cursor.lnum)
6294 word_count_cursor += word_count;
6295 char_count_cursor += char_count;
6296 byte_count_cursor = byte_count +
6297 line_count_info(ml_get(lnum),
6298 &word_count_cursor, &char_count_cursor,
6299 (long)(curwin->w_cursor.col + 1), eol_size);
6302 /* Add to the running totals */
6303 byte_count += line_count_info(ml_get(lnum), &word_count,
6304 &char_count, (long)MAXCOL, eol_size);
6307 /* Correction for when last line doesn't have an EOL. */
6308 if (!curbuf->b_p_eol && curbuf->b_p_bin)
6309 byte_count -= eol_size;
6311 #ifdef FEAT_VISUAL
6312 if (VIsual_active)
6314 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
6316 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
6317 &max_pos.col);
6318 sprintf((char *)buf1, _("%ld Cols; "),
6319 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6321 else
6322 buf1[0] = NUL;
6324 if (char_count_cursor == byte_count_cursor
6325 && char_count == byte_count)
6326 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
6327 buf1, line_count_selected,
6328 (long)curbuf->b_ml.ml_line_count,
6329 word_count_cursor, word_count,
6330 byte_count_cursor, byte_count);
6331 else
6332 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6333 buf1, line_count_selected,
6334 (long)curbuf->b_ml.ml_line_count,
6335 word_count_cursor, word_count,
6336 char_count_cursor, char_count,
6337 byte_count_cursor, byte_count);
6339 else
6340 #endif
6342 p = ml_get_curline();
6343 validate_virtcol();
6344 col_print(buf1, (int)curwin->w_cursor.col + 1,
6345 (int)curwin->w_virtcol + 1);
6346 col_print(buf2, (int)STRLEN(p), linetabsize(p));
6348 if (char_count_cursor == byte_count_cursor
6349 && char_count == byte_count)
6350 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
6351 (char *)buf1, (char *)buf2,
6352 (long)curwin->w_cursor.lnum,
6353 (long)curbuf->b_ml.ml_line_count,
6354 word_count_cursor, word_count,
6355 byte_count_cursor, byte_count);
6356 else
6357 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6358 (char *)buf1, (char *)buf2,
6359 (long)curwin->w_cursor.lnum,
6360 (long)curbuf->b_ml.ml_line_count,
6361 word_count_cursor, word_count,
6362 char_count_cursor, char_count,
6363 byte_count_cursor, byte_count);
6366 #ifdef FEAT_MBYTE
6367 byte_count = bomb_size();
6368 if (byte_count > 0)
6369 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
6370 byte_count);
6371 #endif
6372 /* Don't shorten this message, the user asked for it. */
6373 p = p_shm;
6374 p_shm = (char_u *)"";
6375 msg(IObuff);
6376 p_shm = p;