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.
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
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
31 # define STAR_REGISTER 37
33 # define PLUS_REGISTER 38
35 # define PLUS_REGISTER STAR_REGISTER /* there is only one */
39 # define TILDE_REGISTER (PLUS_REGISTER + 1)
44 # define NUM_REGISTERS (TILDE_REGISTER + 1)
46 # define NUM_REGISTERS (PLUS_REGISTER + 1)
49 # define NUM_REGISTERS 37
53 * Each yank register is an array of pointers to lines.
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 */
61 colnr_T y_width
; /* only set if y_type == MBLOCK */
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
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 */
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
));
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
,
101 static void stuffescaped
__ARGS((char_u
*arg
, int literally
));
103 static void mb_adjust_opend
__ARGS((oparg_T
*oap
));
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));
114 static void dis_msg
__ARGS((char_u
*p
, int skip_esc
));
116 static void block_prep
__ARGS((oparg_T
*oap
, struct block_def
*, linenr_T
, int));
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
));
121 static int ends_in_white
__ARGS((linenr_T lnum
));
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
));
126 static int fmt_check_par
__ARGS((linenr_T
));
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
)
177 if (char1
== 'r') /* ignore second character */
179 if (char1
== '~') /* when tilde is an operator */
182 if (opchars
[i
][0] == char1
&& opchars
[i
][1] == char2
)
187 #if defined(FEAT_VISUAL) || defined(PROTO)
189 * Return TRUE if operator "op" always works on whole lines.
195 return opchars
[op
][2];
200 * Get first operator command character.
201 * Returns 'g' or 'z' if there is another command character.
207 return opchars
[optype
][0];
211 * Get second operator command character.
214 get_extra_op_char(optype
)
217 return opchars
[optype
][1];
221 * op_shift - handle a shift operation
224 op_shift(oap
, curs_top
, amount
)
236 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
237 (linenr_T
)(oap
->end
.lnum
+ 1)) == FAIL
)
242 block_col
= curwin
->w_cursor
.col
;
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
);
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())
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);
271 curwin
->w_cursor
.lnum
= oap
->start
.lnum
;
272 curwin
->w_cursor
.col
= block_col
;
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 */
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
)
290 if (oap
->line_count
== 1)
293 sprintf((char *)IObuff
, _("1 line %sed 1 time"), s
);
295 sprintf((char *)IObuff
, _("1 line %sed %d times"), s
, amount
);
300 sprintf((char *)IObuff
, _("%ld lines %sed 1 time"),
303 sprintf((char *)IObuff
, _("%ld lines %sed %d times"),
304 oap
->line_count
, s
, amount
);
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
324 shift_line(left
, round
, amount
)
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 */
351 else /* original vi indent */
355 count
-= p_sw
* amount
;
360 count
+= p_sw
* amount
;
365 if (State
& VREPLACE_FLAG
)
366 change_indent(INDENT_SET
, count
, FALSE
, NUL
);
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.
378 shift_block(oap
, amount
)
382 int left
= (oap
->op_type
== OP_LSHIFT
);
383 int oldstate
= State
;
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
;
392 colnr_T vcol
, col
= 0, ws_vcol
;
396 #ifdef FEAT_RIGHTLEFT
399 p_ri
= 0; /* don't want revins in ident */
402 State
= INSERT
; /* don't want REPLACE for State */
403 block_prep(oap
, &bd
, curwin
->w_cursor
.lnum
, TRUE
);
407 /* total is number of screen columns to be inserted/removed */
408 total
= amount
* p_sw
;
409 oldp
= ml_get_curline();
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
;
425 bd
.textstart
+= (*mb_ptr2len
)(bd
.textstart
);
429 for ( ; vim_iswhite(*bd
.textstart
); )
431 incr
= lbr_chartabsize_adv(&bd
.textstart
, (colnr_T
)(bd
.start_vcol
));
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. */
438 i
= ((ws_vcol
% p_ts
) + total
) / p_ts
; /* number of tabs */
440 j
= ((ws_vcol
% p_ts
) + total
) % p_ts
; /* number of spp */
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
));
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
);
454 mch_memmove(newp
+ bd
.textcol
+ i
+ j
, bd
.textstart
, (size_t)len
);
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
);
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
;
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 */
487 j
= ((col
% p_ts
) + split
) % p_ts
; /* number of spp */
492 newp
= alloc_check(bd
.textcol
+ i
+ j
+ (unsigned)STRLEN(midp
) + 1);
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
))
508 /* We might have an extra TAB worth of spp now! */
509 if (j
/ p_ts
&& !curbuf
->b_p_et
)
514 copy_chars(newp
+ bd
.textcol
, (size_t)i
, TAB
);
515 copy_spaces(newp
+ bd
.textcol
+ i
, (size_t)j
);
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
);
524 curwin
->w_cursor
.col
= oldcol
;
525 #ifdef FEAT_RIGHTLEFT
531 #ifdef FEAT_VISUALEXTRA
533 * Insert string "s" (b_insert ? before : after) block :AKelly
534 * Caller must prepare for undo.
537 block_insert(oap
, s
, b_insert
, bdp
)
541 struct block_def
*bdp
;
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 */
565 p_ts
= bdp
->start_char_vcols
;
566 spaces
= bdp
->startspaces
;
568 count
= p_ts
- 1; /* we're cutting a TAB */
569 offset
= bdp
->textcol
;
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);
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) */
585 spaces
= (oap
->end_vcol
- bdp
->end_vcol
) + 1;
587 offset
= bdp
->textcol
+ bdp
->textlen
;
591 newp
= alloc_check((unsigned)(STRLEN(oldp
)) + s_len
+ count
+ 1);
595 /* copy up to shifted part */
596 mch_memmove(newp
, oldp
, (size_t)(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
);
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. */
612 /* We allowed for that TAB, remember this now */
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
;
631 changed_lines(oap
->start
.lnum
+ 1, 0, oap
->end
.lnum
+ 1, 0L);
637 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
639 * op_reindent - handle reindenting a block of lines.
642 op_reindent(oap
, how
)
644 int (*how
) __ARGS((void));
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. */
656 EMSG(_(e_modifiable
));
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. */
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.
675 if (i
!= oap
->line_count
- 1 || oap
->line_count
== 1
676 || how
!= get_lisp_indent
)
679 l
= skipwhite(ml_get_curline());
680 if (*l
== NUL
) /* empty or blank line */
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,
706 oap
->is_VIsual
? start_lnum
+ oap
->line_count
:
708 last_changed
+ 1, 0L);
710 else if (oap
->is_VIsual
)
711 redraw_curbuf_later(INVERTED
);
714 if (oap
->line_count
> p_report
)
716 i
= oap
->line_count
- (i
+ 1);
718 MSG(_("1 line indented "));
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.
743 new_line
= getcmdline('=', 0L, 0);
744 if (new_line
== NULL
)
746 if (*new_line
== NUL
) /* use previous line */
749 set_expr_line(new_line
);
754 * Set the expression for the '=' register.
755 * Argument must be an allocated string.
758 set_expr_line(new_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.
774 static int nested
= 0;
776 if (expr_line
== NULL
)
779 /* Make a copy of the expression, because evaluating it may cause it to be
781 expr_copy
= vim_strsave(expr_line
);
782 if (expr_copy
== NULL
)
785 /* When we are invoked recursively limit the evaluation to 10 levels.
786 * Then return the string as-is. */
791 rv
= eval_to_string(expr_copy
, NULL
, TRUE
);
798 * Get the '=' register expression itself, without evaluating it.
803 if (expr_line
== 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
)
816 int writing
; /* if TRUE check for writable registers */
818 if ( (regname
> 0 && ASCII_ISALNUM(regname
))
819 || (!writing
&& vim_strchr((char_u
*)
829 #ifdef FEAT_CLIPBOARD
834 || (!writing
&& regname
== '~')
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
850 get_yank_register(regname
, writing
)
857 if ((regname
== 0 || regname
== '"') && !writing
&& y_previous
!= NULL
)
859 y_current
= y_previous
;
865 else if (ASCII_ISLOWER(i
))
866 i
= CharOrdLow(i
) + 10;
867 else if (ASCII_ISUPPER(i
))
869 i
= CharOrdUp(i
) + 10;
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
== '*')
878 /* When clipboard is not available, use register 0 instead of '+' */
879 else if (clip_plus
.available
&& regname
== '+')
883 else if (!writing
&& regname
== '~')
886 else /* not 0-9, a-z, A-Z or '-': use register 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
)
904 if (!clip_star
.available
)
907 clip_get_selection(&clip_star
);
909 else if (regname
== '+')
911 if (!clip_plus
.available
)
914 clip_get_selection(&clip_plus
);
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.
926 get_register(name
, copy
)
928 int copy
; /* make a copy, if FALSE make register empty. */
930 static struct yankreg
*reg
;
933 #ifdef FEAT_CLIPBOARD
934 /* When Visual area changed, may have to update selection. Obtain the
936 if (name
== '*' && clip_star
.available
&& clip_isautosel())
938 clip_update_selection();
939 may_get_selection(name
);
943 get_yank_register(name
, 0);
944 reg
= (struct yankreg
*)alloc((unsigned)sizeof(struct yankreg
));
950 /* If we run out of memory some or all of the lines are empty. */
951 if (reg
->y_size
== 0)
954 reg
->y_array
= (char_u
**)alloc((unsigned)(sizeof(char_u
*)
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
]);
963 y_current
->y_array
= NULL
;
969 * Put "reg" into register "name". Free any previous contents.
972 put_register(name
, reg
)
976 get_yank_register(name
, 0);
978 *y_current
= *(struct yankreg
*)reg
;
980 # ifdef FEAT_CLIPBOARD
981 /* Send text written to clipboard register to the clipboard. */
987 #if defined(FEAT_MOUSE) || defined(PROTO)
989 * return TRUE if the current yank register has type MLINE
992 yank_register_mline(regname
)
995 if (regname
!= 0 && !valid_yank_reg(regname
, FALSE
))
997 if (regname
== '_') /* black hole is always empty */
999 get_yank_register(regname
, FALSE
);
1000 return (y_current
->y_type
== MLINE
);
1005 * start or stop recording into a yank register
1007 * return FAIL for failure, OK otherwise
1015 struct yankreg
*old_y_previous
, *old_y_current
;
1018 if (Recording
== FALSE
) /* start recording */
1020 /* registers 0-9, a-z and " are allowed */
1021 if (c
< 0 || (!ASCII_ISALNUM(c
) && c
!= '"'))
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
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
;
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
1071 stuff_yank(regname
, p
)
1078 /* check for read-only register */
1079 if (regname
!= 0 && !valid_yank_reg(regname
, TRUE
))
1084 if (regname
== '_') /* black hole: don't do anything */
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
);
1108 if ((y_current
->y_array
=
1109 (char_u
**)alloc((unsigned)sizeof(char_u
*))) == NULL
)
1114 y_current
->y_array
[0] = p
;
1115 y_current
->y_size
= 1;
1116 y_current
->y_type
= MCHAR
; /* used to be MLINE, why? */
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
)
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
;
1139 if (regname
== '@') /* repeat previous one */
1143 EMSG(_("E748: No previously used register"));
1148 /* check for valid regname */
1149 if (regname
== '%' || regname
== '#' || !valid_yank_reg(regname
, FALSE
))
1151 emsg_invreg(regname
);
1156 #ifdef FEAT_CLIPBOARD
1157 regname
= may_get_selection(regname
);
1160 if (regname
== '_') /* black hole: don't stuff anything */
1164 if (regname
== ':') /* use last command line */
1166 if (last_cmdline
== NULL
)
1168 EMSG(_(e_nolastcmd
));
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
);
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
);
1183 retval
= put_in_typebuf(p
, TRUE
, TRUE
, silent
);
1189 else if (regname
== '=')
1191 p
= get_expr_line();
1194 retval
= put_in_typebuf(p
, TRUE
, colon
, silent
);
1198 else if (regname
== '.') /* use last inserted text */
1200 p
= get_last_insert_save();
1203 EMSG(_(e_noinstext
));
1206 retval
= put_in_typebuf(p
, FALSE
, colon
, silent
);
1211 get_yank_register(regname
, FALSE
);
1212 if (y_current
->y_array
== NULL
)
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; )
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
1230 if (ins_typebuf((char_u
*)"\n", remap
, 0, TRUE
, silent
) == FAIL
)
1233 escaped
= vim_strsave_escape_csi(y_current
->y_array
[i
]);
1234 if (escaped
== NULL
)
1236 retval
= ins_typebuf(escaped
, remap
, 0, TRUE
, silent
);
1240 if (colon
&& ins_typebuf((char_u
*)":", remap
, 0, TRUE
, silent
)
1244 Exec_reg
= TRUE
; /* disable the 'q' command */
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.
1254 put_reedit_in_typebuf(silent
)
1259 if (restart_edit
!= NUL
)
1261 if (restart_edit
== 'V')
1269 buf
[0] = restart_edit
== 'I' ? 'i' : restart_edit
;
1272 if (ins_typebuf(buf
, REMAP_NONE
, 0, TRUE
, silent
) == OK
)
1278 put_in_typebuf(s
, esc
, colon
, silent
)
1280 int esc
; /* Escape CSI characters */
1281 int colon
; /* add ':' before the line */
1286 put_reedit_in_typebuf(silent
);
1288 retval
= ins_typebuf((char_u
*)"\n", REMAP_YES
, 0, TRUE
, silent
);
1294 p
= vim_strsave_escape_csi(s
);
1300 retval
= ins_typebuf(p
, REMAP_YES
, 0, TRUE
, silent
);
1304 if (colon
&& retval
== OK
)
1305 retval
= ins_typebuf((char_u
*)":", REMAP_YES
, 0, TRUE
, silent
);
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
)
1318 int literally
; /* insert literally, not as if typed */
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.
1334 /* check for valid regname */
1335 if (regname
!= NUL
&& !valid_yank_reg(regname
, FALSE
))
1338 #ifdef FEAT_CLIPBOARD
1339 regname
= may_get_selection(regname
);
1342 if (regname
== '.') /* insert last inserted text */
1343 retval
= stuff_inserted(NUL
, 1L, TRUE
);
1344 else if (get_spec_reg(regname
, &arg
, &allocated
, TRUE
))
1348 stuffescaped(arg
, literally
);
1352 else /* name or number register */
1354 get_yank_register(regname
, FALSE
);
1355 if (y_current
->y_array
== NULL
)
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
1366 if (y_current
->y_type
== MLINE
|| i
< y_current
->y_size
- 1)
1367 stuffcharReadbuff('\n');
1376 * Stuff a string into the typeahead buffer, such that edit() will insert it
1377 * literally ("literally" TRUE) or interpret is as typed characters.
1380 stuffescaped(arg
, literally
)
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"
1395 && *arg
< DEL
/* EBCDIC: chars above space are normal */
1398 || (*arg
== K_SPECIAL
&& !literally
))
1401 stuffReadbuffLen(start
, (long)(arg
- start
));
1403 /* stuff a single special character */
1408 c
= mb_ptr2char_adv(&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
)
1427 int errmsg
; /* give error message when failing */
1435 case '%': /* file name */
1437 check_fname(); /* will give emsg if not set */
1438 *argp
= curbuf
->b_fname
;
1441 case '#': /* alternate file name */
1442 *argp
= getaltfname(errmsg
); /* may give emsg if not set */
1446 case '=': /* result of expression */
1447 *argp
= get_expr_line();
1452 case ':': /* last command line */
1453 if (last_cmdline
== NULL
&& errmsg
)
1454 EMSG(_(e_nolastcmd
));
1455 *argp
= last_cmdline
;
1458 case '/': /* last search-pattern */
1459 if (last_search_pat() == NULL
&& errmsg
)
1460 EMSG(_(e_noprevre
));
1461 *argp
= last_search_pat();
1464 case '.': /* last inserted text */
1465 *argp
= get_last_insert_save();
1467 if (*argp
== NULL
&& errmsg
)
1468 EMSG(_(e_noinstext
));
1471 #ifdef FEAT_SEARCHPATH
1472 case Ctrl_F
: /* Filename under cursor */
1473 case Ctrl_P
: /* Path under cursor, expand via "path" */
1476 *argp
= file_name_at_cursor(FNAME_MESS
| FNAME_HYP
1477 | (regname
== Ctrl_P
? FNAME_EXP
: 0), 1L, NULL
);
1482 case Ctrl_W
: /* word under cursor */
1483 case Ctrl_A
: /* WORD (mnemonic All) under cursor */
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
;
1492 case '_': /* black hole: always empty */
1493 *argp
= (char_u
*)"";
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
)
1512 int literally
; /* Insert text literally instead of "as typed" */
1513 int remcr
; /* don't add trailing CR */
1517 get_yank_register(regname
, FALSE
);
1518 if (y_current
->y_array
== NULL
)
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
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. */
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.
1552 /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1553 if (*rp
== 0 && clip_unnamed
)
1555 if (!clip_star
.available
&& *rp
== '*')
1557 if (!clip_plus
.available
&& *rp
== '+')
1563 * op_delete - handle a delete operation
1565 * return FAIL if undo failed, OK otherwise.
1575 char_u
*newp
, *oldp
;
1576 struct block_def bd
;
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 */
1584 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1586 return u_save_cursor();
1588 if (!curbuf
->b_p_ma
)
1590 EMSG(_(e_modifiable
));
1594 #ifdef FEAT_CLIPBOARD
1595 adjust_clip_reg(&oap
->regname
);
1600 mb_adjust_opend(oap
);
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
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
1637 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1638 * marks as if it happened. */
1641 if (vim_strchr(p_cpo
, CPO_EMPTYREGION
) != NULL
)
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
))
1661 get_yank_register(oap
->regname
, TRUE
); /* yank into specif'd reg. */
1662 if (op_yank(oap
, TRUE
, FALSE
) == OK
) /* yank without message */
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
)
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)
1689 get_yank_register(oap
->regname
, TRUE
);
1690 if (op_yank(oap
, TRUE
, FALSE
) == OK
)
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
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
;
1720 if (oap
->block_mode
)
1722 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
1723 (linenr_T
)(oap
->end
.lnum
+ 1)) == 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 */
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;
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
);
1750 /* copy up to deleted part */
1751 mch_memmove(newp
, oldp
, (size_t)bd
.textcol
);
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
);
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 */
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
)
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
;
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" */
1802 del_lines(oap
->line_count
, TRUE
);
1803 beginline(BL_WHITE
| BL_FIX
);
1804 u_clearline(); /* "U" command not possible after "dd" */
1809 #ifdef FEAT_VIRTUALEDIT
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 */
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)
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
)
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
;
1848 if (oap
->line_count
== 1) /* delete characters within one line */
1850 if (u_save_cursor() == FAIL
) /* save line for undo */
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
1861 display_dollar(oap
->end
.col
- !oap
->inclusive
);
1863 n
= oap
->end
.col
- oap
->start
.col
+ 1 - !oap
->inclusive
;
1865 #ifdef FEAT_VIRTUALEDIT
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))
1878 /* Delete at least one char (e.g, when on a control char). */
1879 if (n
== 0 && oap
->start
.coladd
!= oap
->end
.coladd
)
1882 /* When deleted a char in the line, reset coladd. */
1883 if (gchar_cursor() != NUL
)
1884 curwin
->w_cursor
.coladd
= 0;
1887 (void)del_bytes((long)n
, !virtual_op
, oap
->op_type
== OP_DELETE
1893 else /* delete characters between lines */
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
)
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
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
1928 if (oap
->block_mode
)
1930 curbuf
->b_op_end
.lnum
= oap
->end
.lnum
;
1931 curbuf
->b_op_end
.col
= oap
->start
.col
;
1935 curbuf
->b_op_end
= oap
->start
;
1936 curbuf
->b_op_start
= oap
->start
;
1943 * Adjust end of operating area for ending on a multi-byte character.
1944 * Used for deletion.
1947 mb_adjust_opend(oap
)
1954 p
= ml_get(oap
->end
.lnum
);
1955 oap
->end
.col
+= mb_tail_off(p
, p
+ oap
->end
.col
);
1960 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1962 * Replace a whole area with one character.
1973 char_u
*newp
, *oldp
;
1975 struct block_def bd
;
1977 if ((curbuf
->b_ml
.ml_flags
& ML_EMPTY
) || oap
->empty
)
1978 return OK
; /* nothing to do */
1982 mb_adjust_opend(oap
);
1985 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
1986 (linenr_T
)(oap
->end
.lnum
+ 1)) == 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
)
2012 getvpos(&vpos
, oap
->start_vcol
);
2013 bd
.startspaces
+= vpos
.coladd
;
2018 /* allow for pre spaces */
2019 n
= (bd
.startspaces
? bd
.start_char_vcols
- 1 : 0);
2021 /* allow for post spp */
2023 #ifdef FEAT_VIRTUALEDIT
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;
2033 /* A double-wide character can be replaced only up to half the
2035 if ((*mb_char2cells
)(c
) > 1)
2037 if ((numc
& 1) && !bd
.is_short
)
2045 /* Compute bytes needed, move character count to num_chars. */
2047 numc
*= (*mb_char2len
)(c
);
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
);
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 */
2067 n
= (int)STRLEN(newp
);
2068 while (--num_chars
>= 0)
2069 n
+= (*mb_char2bytes
)(c
, newp
+ n
);
2073 copy_chars(newp
+ STRLEN(newp
), (size_t)numc
, c
);
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
);
2088 * MCHAR and MLINE motion replace.
2090 if (oap
->motion_type
== MLINE
)
2093 curwin
->w_cursor
.col
= 0;
2094 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
2098 else if (!oap
->inclusive
)
2101 while (ltoreq(curwin
->w_cursor
, oap
->end
))
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
);
2116 /* Backup to the replaced character. */
2122 #ifdef FEAT_VIRTUALEDIT
2127 if (curwin
->w_cursor
.lnum
== oap
->end
.lnum
)
2129 /* oap->end has to be recalculated when
2131 end_vcol
= getviscol2(oap
->end
.col
,
2134 coladvance_force(getviscol());
2135 if (curwin
->w_cursor
.lnum
== oap
->end
.lnum
)
2136 getvpos(&oap
->end
, end_vcol
);
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)
2165 /* Advance to next character, stop at the end of the file. */
2166 if (inc_cursor() == -1)
2171 curwin
->w_cursor
= oap
->start
;
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
;
2184 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2192 struct block_def bd
;
2197 if (u_save((linenr_T
)(oap
->start
.lnum
- 1),
2198 (linenr_T
)(oap
->end
.lnum
+ 1)) == FAIL
)
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
)
2213 todo
-= (*mb_ptr2len
)(ml_get_pos(&pos
)) - 1;
2215 did_change
|= swapchar(oap
->op_type
, &pos
);
2216 if (inc(&pos
) == -1) /* at end of file */
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
,
2226 netbeans_inserted(curbuf
, pos
.lnum
, bd
.textcol
,
2227 &ptr
[bd
.textcol
], bd
.textlen
);
2232 changed_lines(oap
->start
.lnum
, 0, oap
->end
.lnum
+ 1, 0L);
2234 else /* not block mode */
2237 if (oap
->motion_type
== MLINE
)
2241 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
2245 else if (!oap
->inclusive
)
2248 while (ltoreq(pos
, oap
->end
))
2250 did_change
|= swapchar(oap
->op_type
, &pos
);
2251 if (inc(&pos
) == -1) /* at end of file */
2257 changed_lines(oap
->start
.lnum
, oap
->start
.col
, oap
->end
.lnum
+ 1,
2259 #ifdef FEAT_NETBEANS_INTG
2260 if (usingNetbeans
&& did_change
)
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
);
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
);
2287 if (!did_change
&& oap
->is_VIsual
)
2288 /* No change: need to remove the Visual selection */
2289 redraw_curbuf_later(INVERTED
);
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"));
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
)
2324 /* Only do rot13 encoding for ASCII characters. */
2325 if (c
>= 0x80 && op_type
== OP_ROT13
)
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
;
2338 curwin
->w_cursor
= sp
;
2342 if (enc_dbcs
!= 0 && c
>= 0x100) /* No lower/uppercase letter */
2348 if (op_type
== OP_ROT13
)
2350 else if (op_type
!= OP_LOWER
)
2353 else if (MB_ISUPPER(c
))
2355 if (op_type
== OP_ROT13
)
2357 else if (op_type
!= OP_UPPER
)
2363 if (enc_utf8
&& (c
>= 0x80 || nc
>= 0x80))
2365 pos_T sp
= curwin
->w_cursor
;
2367 curwin
->w_cursor
= *pos
;
2370 curwin
->w_cursor
= sp
;
2380 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2382 * op_insert - Insert and append operators for Visual mode.
2385 op_insert(oap
, count1
)
2389 long ins_len
, pre_textlen
= 0;
2390 char_u
*firstline
, *ins_text
;
2391 struct block_def bd
;
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
;
2413 if (u_save_cursor() == FAIL
)
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
;
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
)
2433 #ifdef FEAT_VIRTUALEDIT
2434 && curwin
->w_cursor
.coladd
== 0
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
)
2449 for (i
= 0; i
< bd
.endspaces
; ++i
)
2451 bd
.textlen
+= bd
.endspaces
;
2456 curwin
->w_cursor
= oap
->end
;
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
)
2466 edit(NUL
, FALSE
, (linenr_T
)count1
);
2468 /* if user has moved off this line, we don't know what to do, so do
2470 if (curwin
->w_cursor
.lnum
!= oap
->start
.lnum
)
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
;
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
),
2513 curwin
->w_cursor
.col
= oap
->start
.col
;
2523 * op_change - handle a change operation
2525 * return TRUE if edit() returns because of a CTRL-O command
2533 #ifdef FEAT_VISUALEXTRA
2536 long ins_len
, pre_textlen
= 0;
2538 char_u
*ins_text
, *newp
, *oldp
;
2539 struct block_def bd
;
2543 if (oap
->motion_type
== MLINE
)
2546 #ifdef FEAT_SMARTINDENT
2547 if (!p_paste
&& curbuf
->b_p_si
2548 # ifdef FEAT_CINDENT
2552 can_si
= TRUE
; /* It's like opening a new line, do si */
2556 /* First delete the text in the region. In an empty buffer only need to
2558 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
2560 if (u_save_cursor() == FAIL
)
2563 else if (op_delete(oap
) == FAIL
)
2566 if ((l
> curwin
->w_cursor
.col
) && !lineempty(curwin
->w_cursor
.lnum
)
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());
2581 pre_textlen
= (long)STRLEN(ml_get(oap
->start
.lnum
));
2582 bd
.textcol
= curwin
->w_cursor
.col
;
2586 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2587 if (oap
->motion_type
== MLINE
)
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
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
;
2613 block_prep(oap
, &bd
, linenr
, TRUE
);
2614 if (!bd
.is_short
|| virtual_op
)
2616 # ifdef FEAT_VIRTUALEDIT
2619 /* If the block starts in virtual space, count the
2620 * initial coladd offset as part of "startspaces" */
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
;
2632 oldp
= ml_get(linenr
);
2633 newp
= alloc_check((unsigned)(STRLEN(oldp
)
2634 # ifdef FEAT_VIRTUALEDIT
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
;
2647 mch_memmove(newp
+ offset
, ins_text
, (size_t)ins_len
);
2650 mch_memmove(newp
+ offset
, oldp
, STRLEN(oldp
) + 1);
2651 ml_replace(linenr
, newp
, FALSE
);
2656 changed_lines(oap
->start
.lnum
+ 1, 0, oap
->end
.lnum
+ 1, 0L);
2667 * set all the yank registers to empty (called from main())
2674 for (i
= 0; i
< NUM_REGISTERS
; ++i
)
2675 y_regs
[i
].y_array
= NULL
;
2678 #if defined(EXITFREE) || defined(PROTO)
2684 for (i
= 0; i
< NUM_REGISTERS
; ++i
)
2686 y_current
= &y_regs
[i
];
2687 if (y_current
->y_array
!= NULL
)
2694 * Free "n" lines from the current yank register.
2695 * Called for normal freeing and in case of error.
2701 if (y_current
->y_array
!= NULL
)
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.
2715 smsg((char_u
*)_("freeing %ld lines"), i
+ 1);
2721 vim_free(y_current
->y_array
[i
]);
2723 vim_free(y_current
->y_array
);
2724 y_current
->y_array
= NULL
;
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
)
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 */
2756 linenr_T lnum
; /* current line number */
2758 int yanktype
= oap
->motion_type
;
2759 long yanklines
= oap
->line_count
;
2760 linenr_T yankendlnum
= oap
->end
.lnum
;
2763 struct block_def bd
;
2765 /* check for read-only register */
2766 if (oap
->regname
!= 0 && !valid_yank_reg(oap
->regname
, TRUE
))
2771 if (oap
->regname
== '_') /* black hole: nothing to do */
2774 #ifdef FEAT_CLIPBOARD
2775 if (!clip_star
.available
&& oap
->regname
== '*')
2777 else if (!clip_plus
.available
&& oap
->regname
== '+')
2781 if (!deleting
) /* op_delete() already set y_current */
2782 get_yank_register(oap
->regname
, TRUE
);
2785 /* append to existing contents */
2786 if (y_append
&& y_current
->y_array
!= NULL
)
2787 y_current
= &newreg
;
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
2799 && (!oap
->is_VIsual
|| *p_sel
== 'o')
2802 && oap
->end
.col
== 0
2810 y_current
->y_size
= yanklines
;
2811 y_current
->y_type
= yanktype
; /* set the yank register type */
2813 y_current
->y_width
= 0;
2815 y_current
->y_array
= (char_u
**)lalloc_clear((long_u
)(sizeof(char_u
*) *
2818 if (y_current
->y_array
== NULL
)
2825 lnum
= oap
->start
.lnum
;
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
--;
2839 for ( ; lnum
<= yankendlnum
; lnum
++, y_idx
++)
2841 switch (y_current
->y_type
)
2845 block_prep(oap
, &bd
, lnum
, FALSE
);
2846 if (yank_copy_line(&bd
, y_idx
) == FAIL
)
2852 if ((y_current
->y_array
[y_idx
] =
2853 vim_strsave(ml_get(lnum
))) == NULL
)
2859 colnr_T startcol
= 0, endcol
= MAXCOL
;
2860 #ifdef FEAT_VIRTUALEDIT
2861 int is_oneChar
= FALSE
;
2868 if (lnum
== oap
->start
.lnum
)
2870 startcol
= oap
->start
.col
;
2871 #ifdef FEAT_VIRTUALEDIT
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
;
2887 if (lnum
== oap
->end
.lnum
)
2889 endcol
= oap
->end
.col
;
2890 #ifdef FEAT_VIRTUALEDIT
2893 getvcol(curwin
, &oap
->end
, &cs
, NULL
, &ce
);
2894 if (p
[endcol
] == NUL
|| (cs
+ oap
->end
.coladd
< ce
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
2903 if (oap
->start
.lnum
== oap
->end
.lnum
2904 && oap
->start
.col
== oap
->end
.col
)
2906 /* Special case: inside a single char */
2908 bd
.startspaces
= oap
->end
.coladd
2909 - oap
->start
.coladd
+ oap
->inclusive
;
2914 bd
.endspaces
= oap
->end
.coladd
2916 endcol
-= oap
->inclusive
;
2922 if (startcol
> endcol
2923 #ifdef FEAT_VIRTUALEDIT
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
)
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
)
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
);
2965 y_idx
= y_current
->y_size
- 1;
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
;
2977 while (y_idx
< y_current
->y_size
)
2978 curr
->y_array
[j
++] = y_current
->y_array
[y_idx
++];
2980 vim_free(y_current
->y_array
);
2983 if (mess
) /* Display message about yank? */
2985 if (yanktype
== MCHAR
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();
2999 if (oap
->block_mode
)
3000 MSG(_("block of 1 line yanked"));
3003 MSG(_("1 line yanked"));
3006 else if (oap
->block_mode
)
3007 smsg((char_u
*)_("block of %ld lines yanked"), yanklines
);
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
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
);
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
);
3069 fail
: /* free the allocated lines */
3070 free_yank(y_idx
+ 1);
3076 yank_copy_line(bd
, y_idx
)
3077 struct block_def
*bd
;
3082 if ((pnew
= alloc(bd
->startspaces
+ bd
->endspaces
+ bd
->textlen
+ 1))
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
;
3096 #ifdef FEAT_CLIPBOARD
3098 * Make a copy of the y_current register to register "reg".
3102 struct yankreg
*reg
;
3104 struct yankreg
*curr
= y_current
;
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;
3115 for (j
= 0; j
< y_current
->y_size
; ++j
)
3116 if ((y_current
->y_array
[j
] = vim_strsave(curr
->y_array
[j
])) == NULL
)
3119 y_current
->y_size
= 0;
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")
3134 do_put(regname
, dir
, count
, flags
)
3136 int dir
; /* BACKWARD for 'P', FORWARD for 'p' */
3141 char_u
*newp
, *oldp
;
3143 int totlen
= 0; /* init for gcc */
3146 long i
; /* index in y_array[] */
3156 struct block_def bd
;
3158 char_u
**y_array
= NULL
;
3162 int orig_indent
= 0; /* init for gcc */
3163 int indent_diff
= 0; /* init for gcc */
3164 int first_indent
= TRUE
;
3167 char_u
*insert_string
= NULL
;
3168 int allocated
= FALSE
;
3171 #ifdef FEAT_CLIPBOARD
3172 /* Adjust register name for "unnamed" in 'clipboard'. */
3173 adjust_clip_reg(®name
);
3174 (void)may_get_selection(regname
);
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.).
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');
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
)
3208 if (insert_string
!= NULL
)
3214 /* For the = register we need to split the string at NL
3216 /* Loop twice: count the number of lines and save them. */
3220 ptr
= insert_string
;
3223 if (y_array
!= NULL
)
3224 y_array
[y_size
] = ptr
;
3226 ptr
= vim_strchr(ptr
, '\n');
3229 if (y_array
!= NULL
)
3232 /* A trailing '\n' makes the string linewise */
3240 if (y_array
!= NULL
)
3242 y_array
= (char_u
**)alloc((unsigned)
3243 (y_size
* sizeof(char_u
*)));
3244 if (y_array
== NULL
)
3251 y_size
= 1; /* use fake one-line yank register */
3252 y_array
= &insert_string
;
3257 get_yank_register(regname
, FALSE
);
3259 y_type
= y_current
->y_type
;
3261 y_width
= y_current
->y_width
;
3263 y_size
= y_current
->y_size
;
3264 y_array
= y_current
->y_array
;
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
3274 if (u_save_cursor() == FAIL
)
3276 ptr
= vim_strsave(ml_get_cursor());
3279 ml_append(curwin
->w_cursor
.lnum
, ptr
, (colnr_T
)0, FALSE
);
3282 ptr
= vim_strnsave(ml_get_curline(), curwin
->w_cursor
.col
);
3285 ml_replace(curwin
->w_cursor
.lnum
, ptr
, FALSE
);
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
;
3295 curbuf
->b_op_start
= curwin
->w_cursor
; /* default for '[ mark */
3296 curbuf
->b_op_end
= curwin
->w_cursor
; /* default for '] mark */
3300 if (flags
& PUT_LINE
) /* :put command or "p" in Visual line mode. */
3303 if (y_size
== 0 || y_array
== NULL
)
3305 EMSG2(_("E353: Nothing in register %s"),
3306 regname
== 0 ? (char_u
*)"\"" : transchar(regname
));
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
)
3321 if (y_type
== MLINE
)
3323 lnum
= curwin
->w_cursor
.lnum
;
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
);
3330 (void)hasFolding(lnum
, NULL
, &lnum
);
3334 if (u_save(lnum
- 1, lnum
) == FAIL
)
3338 curwin
->w_cursor
.lnum
= lnum
- 1;
3340 curwin
->w_cursor
.lnum
= lnum
;
3341 curbuf
->b_op_start
= curwin
->w_cursor
; /* for mark_adjust() */
3344 else if (u_save_cursor() == FAIL
)
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. */
3357 ? (int)curwin
->w_cursor
.coladd
< curbuf
->b_p_ts
- 1
3358 : curwin
->w_cursor
.coladd
> 0)
3359 coladvance_force(getviscol());
3361 curwin
->w_cursor
.coladd
= 0;
3363 else if (curwin
->w_cursor
.coladd
> 0 || gchar_cursor() == NUL
)
3364 coladvance_force(getviscol() + (dir
== FORWARD
));
3368 lnum
= curwin
->w_cursor
.lnum
;
3369 col
= curwin
->w_cursor
.col
;
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
);
3387 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &col
);
3391 /* move to start of next multi-byte character */
3392 curwin
->w_cursor
.col
+= (*mb_ptr2len
)(ml_get_cursor());
3395 #ifdef FEAT_VIRTUALEDIT
3396 if (c
!= TAB
|| ve_flags
!= VE_ALL
)
3398 ++curwin
->w_cursor
.col
;
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
)
3410 if (dir
!= FORWARD
&& c
!= NUL
)
3411 ++curwin
->w_cursor
.col
;
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;
3423 for (i
= 0; i
< y_size
; ++i
)
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
)
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
);
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
;
3464 bd
.textcol
-= (*mb_head_off
)(oldp
, oldp
+ bd
.textcol
);
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. */
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);
3485 /* insert the new text */
3486 totlen
= count
* (yanklen
+ spaces
) + bd
.startspaces
+ bd
.endspaces
;
3487 newp
= alloc_check((unsigned)totlen
+ oldlen
+ 1);
3490 /* copy part up to cursor to new line */
3492 mch_memmove(ptr
, oldp
, (size_t)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
);
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
);
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
;
3520 curwin
->w_cursor
.col
+= bd
.startspaces
;
3523 changed_lines(lnum
, 0, curwin
->w_cursor
.lnum
, nr_lines
);
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;
3535 if (flags
& PUT_CURSEND
)
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
;
3548 curwin
->w_cursor
.lnum
= lnum
;
3554 * Character or Line mode
3556 if (y_type
== MCHAR
)
3558 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3560 if (dir
== FORWARD
&& gchar_cursor() != NUL
)
3565 int bytelen
= (*mb_ptr2len
)(ml_get_cursor());
3567 /* put it on the next of the multi-byte character. */
3571 curwin
->w_cursor
.col
+= bytelen
;
3572 curbuf
->b_op_end
.col
+= bytelen
;
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
)
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
;
3603 oldp
= ml_get(lnum
);
3604 newp
= alloc_check((unsigned)(STRLEN(oldp
) + totlen
+ 1));
3606 goto end
; /* alloc() will give error message */
3607 mch_memmove(newp
, oldp
, (size_t)col
);
3609 for (i
= 0; i
< count
; ++i
)
3611 mch_memmove(ptr
, y_array
[0], (size_t)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
);
3628 * Insert at least one line. When y_type is MCHAR, break the first
3631 for (cnt
= 1; cnt
<= count
; ++cnt
)
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));
3647 STRCPY(newp
, y_array
[y_size
- 1]);
3649 /* insert second line */
3650 ml_append(lnum
, newp
, (colnr_T
)0, FALSE
);
3653 oldp
= ml_get(lnum
);
3654 newp
= alloc_check((unsigned)(col
+ yanklen
+ 1));
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
;
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
)
3675 if (flags
& PUT_FIXINDENT
)
3677 old_pos
= curwin
->w_cursor
;
3678 curwin
->w_cursor
.lnum
= 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 */
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)
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
));
3708 if (y_type
== MLINE
)
3710 curbuf
->b_op_start
.col
= 0;
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
);
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
;
3730 curbuf
->b_op_end
.col
= col
- 1;
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
;
3748 curwin
->w_cursor
.lnum
= lnum
+ 1;
3749 curwin
->w_cursor
.col
= 0;
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;
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
;
3771 curwin
->w_set_curswant
= TRUE
;
3775 vim_free(insert_string
);
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.
3790 if (curwin
->w_cursor
.col
> 0
3791 && gchar_cursor() == NUL
3792 #ifdef FEAT_VIRTUALEDIT
3793 && (ve_flags
& VE_ONEMORE
) == 0
3795 && !(restart_edit
|| (State
& INSERT
)))
3797 /* Put the cursor on the last character in the line. */
3800 #ifdef FEAT_VIRTUALEDIT
3801 if (ve_flags
== VE_ALL
)
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;
3813 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3815 * Return TRUE if lines starting with '#' should be left aligned.
3821 # ifdef FEAT_SMARTINDENT
3822 # ifdef FEAT_CINDENT
3823 (curbuf
->b_p_si
&& !curbuf
->b_p_cin
) ||
3828 # ifdef FEAT_CINDENT
3829 (curbuf
->b_p_cin
&& in_cinkeys('#', ' ', TRUE
))
3835 /* Return the character name of the register with the given number */
3837 get_register_name(num
)
3844 else if (num
== DELETION_REGISTER
)
3846 #ifdef FEAT_CLIPBOARD
3847 else if (num
== STAR_REGISTER
)
3849 else if (num
== PLUS_REGISTER
)
3857 /* EBCDIC is really braindead ... */
3858 i
= 'a' + (num
- 10);
3865 return num
+ 'a' - 10;
3871 * ":dis" and ":registers": Display the contents of the yank registers.
3883 char_u
*arg
= eap
->arg
;
3890 if (arg
!= NULL
&& *arg
== NUL
)
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
);
3912 if (y_previous
!= NULL
)
3919 if (yb
->y_array
!= NULL
)
3926 n
= (int)Columns
- 6;
3927 for (j
= 0; j
< yb
->y_size
&& n
> 1; ++j
)
3931 MSG_PUTS_ATTR("^J", attr
);
3934 for (p
= yb
->y_array
[j
]; *p
&& (n
-= ptr2cells(p
)) >= 0; ++p
)
3937 clen
= (*mb_ptr2len
)(p
);
3939 msg_outtrans_len(p
, clen
);
3945 if (n
> 1 && yb
->y_type
== MLINE
)
3946 MSG_PUTS_ATTR("^J", attr
);
3947 out_flush(); /* show one line at a time */
3953 * display last inserted text
3955 if ((p
= get_last_insert()) != NULL
3956 && (arg
== NULL
|| vim_strchr(arg
, '.') != NULL
) && !got_int
)
3963 * display last command line
3965 if (last_cmdline
!= NULL
&& (arg
== NULL
|| vim_strchr(arg
, ':') != NULL
)
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
)
3979 dis_msg(curbuf
->b_fname
, FALSE
);
3983 * display alternate file name
3985 if ((arg
== NULL
|| vim_strchr(arg
, '%') != NULL
) && !got_int
)
3990 if (buflist_name_nr(0, &fname
, &dummy
) != FAIL
)
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
)
4004 dis_msg(last_search_pat(), FALSE
);
4009 * display last used expression
4011 if (expr_line
!= NULL
&& (arg
== NULL
|| vim_strchr(arg
, '=') != NULL
)
4015 dis_msg(expr_line
, FALSE
);
4021 * display a string for do_dis()
4022 * truncate at end of screen line
4025 dis_msg(p
, skip_esc
)
4027 int skip_esc
; /* if TRUE, ignore trailing ESC */
4034 n
= (int)Columns
- 6;
4036 && !(*p
== ESC
&& skip_esc
&& *(p
+ 1) == NUL
)
4037 && (n
-= ptr2cells(p
)) >= 0)
4040 if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
4042 msg_outtrans_len(p
, l
);
4047 msg_outtrans_len(p
++, 1);
4053 * join 'count' lines (minimal 2), including u_save()
4056 do_do_join(count
, 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
)
4069 if (got_int
|| do_join(insert_space
) == FAIL
)
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
;
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();
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
)
4103 char_u
*next
, *next_start
;
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 */
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)
4122 next
= curr
+ currsize
;
4123 mb_ptr_back(curr
, next
);
4124 endcurr1
= (*mb_ptr2char
)(next
);
4127 mb_ptr_back(curr
, next
);
4128 endcurr2
= (*mb_ptr2char
)(next
);
4134 endcurr1
= *(curr
+ currsize
- 1);
4136 endcurr2
= *(curr
+ currsize
- 2);
4140 next
= next_start
= ml_get((linenr_T
)(curwin
->w_cursor
.lnum
+ 1));
4144 next
= skipwhite(next
);
4145 if (*next
!= ')' && currsize
!= 0 && endcurr1
!= TAB
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)
4154 /* don't add a space if the line is ending in a space */
4155 if (endcurr1
== ' ')
4156 endcurr1
= endcurr2
;
4159 /* extra space when 'joinspaces' set and line ends in '.' */
4162 || (vim_strchr(p_cpo
, CPO_JOINSP
) == NULL
4163 && (endcurr1
== '?' || endcurr1
== '!'))))
4167 nextsize
= (int)STRLEN(next
);
4169 newp
= alloc_check((unsigned)(currsize
+ nextsize
+ spaces
+ 1));
4174 * Insert the next line first, because we already have that pointer.
4175 * Curr has to be obtained again, because getting next will have
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
;
4213 #ifdef FEAT_VIRTUALEDIT
4214 curwin
->w_cursor
.coladd
= 0;
4216 curwin
->w_set_curswant
= TRUE
;
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
4228 same_leader(lnum
, leader1_len
, leader1_flags
, leader2_len
, leader2_flags
)
4231 char_u
*leader1_flags
;
4233 char_u
*leader2_flags
;
4235 int idx1
= 0, idx2
= 0;
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);
4258 if (*p
== COM_START
)
4260 if (*(ml_get(lnum
) + leader1_len
) == NUL
)
4262 if (leader2_flags
== NULL
|| leader2_len
== 0)
4264 for (p
= leader2_flags
; *p
&& *p
!= ':'; ++p
)
4265 if (*p
== COM_MIDDLE
)
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
));
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
])
4290 while (vim_iswhite(line1
[idx1
]))
4295 return (idx2
== leader2_len
&& idx1
== leader1_len
);
4300 * implementation of the format operator 'gq'
4303 op_format(oap
, keep_cursor
)
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
)
4316 curwin
->w_cursor
= oap
->start
;
4320 /* When there is no change: need to remove the Visual selection */
4321 redraw_curbuf_later(INVERTED
);
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). */
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
;
4350 curwin
->w_cursor
= saved_cursor
;
4351 saved_cursor
.lnum
= 0;
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
;
4368 wp
->w_old_visual_lnum
+= old_line_count
;
4375 #if defined(FEAT_EVAL) || defined(PROTO)
4377 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4385 /* When there is no change: need to remove the Visual selection */
4386 redraw_curbuf_later(INVERTED
);
4389 (void)fex_format(oap
->start
.lnum
, oap
->line_count
, NUL
);
4393 fex_format(lnum
, count
, c
)
4396 int c
; /* character to be inserted */
4398 int use_sandbox
= was_set_insecurely((char_u
*)"formatexpr",
4402 char_u buf
[MB_MAXBYTES
];
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
);
4416 buf
[(*mb_char2bytes
)(c
, buf
)] = NUL
;
4423 set_vim_var_string(VV_CHAR
, buf
, -1);
4426 * Evaluate the function.
4430 r
= eval_to_number(curbuf
->b_p_fex
);
4434 set_vim_var_string(VV_CHAR
, NULL
, -1);
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
4447 format_lines(line_count
)
4448 linenr_T line_count
;
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 */
4464 int second_indent
= -1;
4465 int do_second_indent
;
4466 int do_number_indent
;
4468 int first_par_line
= TRUE
;
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
);
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
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
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.
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
;
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
;
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
4541 if (do_number_indent
)
4543 (get_number_indent(curwin
->w_cursor
.lnum
+ 1) > 0);
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.
4561 * For the first line of a paragraph, check indent of second line.
4562 * Don't do this for comments and empty lines.
4565 && (do_second_indent
|| do_number_indent
)
4567 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
4568 #ifdef FEAT_COMMENTS
4570 && next_leader_len
== 0
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
)
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()))
4610 /* do the formatting, without 'showmode' */
4611 State
= INSERT
; /* for open_line() */
4614 insertchar(NUL
, INSCHAR_FORMAT
4615 #ifdef FEAT_COMMENTS
4616 + (do_comments
? INSCHAR_DO_COM
: 0)
4622 /* at end of par.: need to set indent of next par. */
4623 need_set_indent
= is_end_par
;
4626 /* When called with a negative line count, break at the
4627 * end of the paragraph. */
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.
4642 curwin
->w_cursor
.lnum
++;
4643 curwin
->w_cursor
.col
= 0;
4644 if (line_count
< 0 && u_save_cursor() == FAIL
)
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
);
4652 curwin
->w_cursor
.lnum
--;
4653 if (do_join(TRUE
) == FAIL
)
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
;
4663 force_format
= FALSE
;
4671 * Return TRUE if line "lnum" ends in a white character.
4677 char_u
*s
= ml_get(lnum
);
4682 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4683 * invocation may call function multiple times". */
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
4698 fmt_check_par(lnum
, leader_len
, leader_flags
, do_comments
)
4701 char_u
**leader_flags
;
4704 char_u
*flags
= NULL
; /* init for GCC */
4709 *leader_len
= get_leader_len(ptr
, leader_flags
, FALSE
);
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
)
4723 return (*skipwhite(ptr
+ *leader_len
) == NUL
4724 || (*leader_len
> 0 && *flags
== COM_END
)
4725 || startPS(lnum
, NUL
, FALSE
));
4732 return (*skipwhite(ml_get(lnum
)) == NUL
|| startPS(lnum
, NUL
, FALSE
));
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
)
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 */
4754 return TRUE
; /* start of the file */
4756 p
= ml_get(lnum
- 1);
4758 return TRUE
; /* after empty line */
4760 #ifdef FEAT_COMMENTS
4761 do_comments
= has_format_option(FO_Q_COMS
);
4763 if (fmt_check_par(lnum
- 1
4764 #ifdef FEAT_COMMENTS
4765 , &leader_len
, &leader_flags
, do_comments
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
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. */
4794 * prepare a few things for block mode yank/delete/tilde
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.
4806 block_prep(oap
, bdp
, lnum
, is_del
)
4808 struct block_def
*bdp
;
4816 char_u
*prev_pstart
;
4819 bdp
->startspaces
= 0;
4822 bdp
->start_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;
4831 bdp
->start_char_vcols
= 0;
4833 line
= ml_get(lnum
);
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
++;
4849 bdp
->pre_whitesp
= 0;
4850 bdp
->pre_whitesp_c
= 0;
4853 prev_pstart
= 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
;
4863 if (!is_del
|| oap
->op_type
== OP_APPEND
)
4864 bdp
->endspaces
= oap
->end_vcol
- oap
->start_vcol
+ 1;
4868 /* notice: this converts partly selected Multibyte characters to
4870 bdp
->startspaces
= bdp
->start_vcol
- oap
->start_vcol
;
4871 if (is_del
&& bdp
->startspaces
)
4872 bdp
->startspaces
= bdp
->start_char_vcols
- bdp
->startspaces
;
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
;
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
;
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;
4904 while (bdp
->end_vcol
<= oap
->end_vcol
&& *pend
!= NUL
)
4906 /* Count a tab for what it's worth (if list mode not on) */
4908 incr
= lbr_chartabsize_adv(&pend
, (colnr_T
)bdp
->end_vcol
);
4909 bdp
->end_vcol
+= incr
;
4911 if (bdp
->end_vcol
<= oap
->end_vcol
4913 || oap
->op_type
== OP_APPEND
4914 || oap
->op_type
== OP_REPLACE
)) /* line too short */
4916 #ifdef FEAT_VISUALEXTRA
4917 bdp
->is_short
= TRUE
;
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
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
;
4940 #ifdef FEAT_VISUALEXTRA
4941 bdp
->end_char_vcols
= incr
;
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
));
4962 if ((i
= (int)STRLEN(s
) - 1) <= 0)
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);
4974 # define RLADDSUBFIX(ptr)
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
)
4990 char_u buf2
[NUMBUFLEN
];
4991 int hex
; /* 'X' or 'x': hex; '0': octal */
4992 static int hexupper
= FALSE
; /* 0xABC */
4997 int length
= 0; /* character length of the number */
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();
5014 * First check if we are on a hexadecimal number, after the "0x".
5016 col
= curwin
->w_cursor
.col
;
5018 while (col
> 0 && vim_isxdigit(ptr
[col
]))
5024 && ptr
[col
- 1] == '0'
5025 && vim_isxdigit(ptr
[col
+ 1]))
5028 * Found hexadecimal number, move to its start.
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
])))
5045 && vim_isdigit(ptr
[col
- 1])
5046 && !(doalp
&& ASCII_ISALPHA(ptr
[col
])))
5051 * If a number was found, and saving for undo works, replace the number.
5053 firstdigit
= ptr
[col
];
5055 if ((!VIM_ISDIGIT(firstdigit
) && !(doalp
&& ASCII_ISALPHA(firstdigit
)))
5056 || u_save_cursor() != OK
)
5062 /* get ptr again, because u_save() may have changed it */
5063 ptr
= ml_get_curline();
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
))
5080 firstdigit
= EBCDIC_CHAR_ADD(firstdigit
, -Prenum1
);
5082 firstdigit
-= Prenum1
;
5087 if (26 - CharOrd(firstdigit
) - 1 < Prenum1
)
5089 if (isupper(firstdigit
))
5096 firstdigit
= EBCDIC_CHAR_ADD(firstdigit
, Prenum1
);
5098 firstdigit
+= Prenum1
;
5101 curwin
->w_cursor
.col
= col
;
5102 (void)del_char(FALSE
);
5103 ins_char(firstdigit
);
5108 if (col
> 0 && ptr
[col
- 1] == '-') /* negative number */
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
)
5125 /* add or subtract */
5127 if (command
== Ctrl_X
)
5134 n
-= (unsigned long)Prenum1
;
5136 n
+= (unsigned long)Prenum1
;
5138 /* handle wraparound for decimal numbers */
5145 n
= 1 + (n
^ (unsigned long)-1);
5153 n
= (n
^ (unsigned long)-1);
5162 * Delete the old number.
5164 curwin
->w_cursor
.col
= col
;
5168 * Don't include the '-' in the length, only the length of the part
5169 * after it is kept the same.
5175 if (c
< 0x100 && isalpha(c
))
5182 /* del_char() will mark line needing displaying */
5183 (void)del_char(FALSE
);
5188 * Prepare the leading characters in buf1[].
5189 * When there are many leading zeros it could be very long. Allocate
5192 buf1
= alloc((unsigned)length
+ NUMBUFLEN
);
5205 if (hex
== 'x' || hex
== 'X')
5212 * Put the number characters in buf2[].
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
);
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)
5235 ins_str(buf1
); /* insert the new number */
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
);
5249 read_viminfo_register(virp
, force
)
5258 int set_prev
= FALSE
;
5260 char_u
**array
= NULL
;
5262 /* We only get here (hopefully) if line[0] == '"' */
5263 str
= virp
->vir_line
+ 1;
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 */
5275 get_yank_register(*str
++, FALSE
);
5276 if (!force
&& y_current
->y_array
!= NULL
)
5279 limit
= 100; /* Optimized for registers containing <= 100 lines */
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
;
5291 else if (STRNCMP(str
, "BLOCK", 5) == 0)
5292 y_current
->y_type
= MBLOCK
;
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
));
5299 y_current
->y_width
= getdigits(&str
);
5301 (void)getdigits(&str
);
5305 while (!(eof
= viminfo_readline(virp
))
5306 && (virp
->vir_line
[0] == TAB
|| virp
->vir_line
[0] == '<'))
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
];
5318 array
= y_current
->y_array
;
5320 str
= viminfo_readstring(virp
, 1, TRUE
);
5322 array
[size
++] = str
;
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
];
5342 y_current
->y_size
= size
;
5348 write_viminfo_registers(fp
)
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)
5367 max_kbyte
= get_viminfo_parameter('s');
5370 for (i
= 0; i
< NUM_REGISTERS
; i
++)
5372 if (y_regs
[i
].y_array
== NULL
)
5374 #ifdef FEAT_CLIPBOARD
5375 /* Skip '*'/'+' register, we don't want them back next time */
5376 if (i
== STAR_REGISTER
|| i
== PLUS_REGISTER
)
5380 /* Neither do we want the '~' register */
5381 if (i
== TILDE_REGISTER
)
5384 /* Skip empty registers. */
5385 num_lines
= y_regs
[i
].y_size
;
5387 || (num_lines
== 1 && y_regs
[i
].y_type
== MCHAR
5388 && STRLEN(y_regs
[i
].y_array
[0]) == 0))
5393 /* Skip register if there is more text than the maximum size. */
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)
5401 switch (y_regs
[i
].y_type
)
5404 type
= (char_u
*)"LINE";
5407 type
= (char_u
*)"CHAR";
5411 type
= (char_u
*)"BLOCK";
5415 sprintf((char *)IObuff
, _("E574: Unknown register type %d"),
5418 type
= (char_u
*)"LINE";
5421 if (y_previous
== &y_regs
[i
])
5423 c
= get_register_name(i
);
5424 fprintf(fp
, "\"%c\t%s\t%d\n", c
, type
,
5426 (int)y_regs
[i
].y_width
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
++)
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 '+'.
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)
5474 x11_export_final_selection()
5479 int motion_type
= -1;
5486 # ifdef FEAT_XCLIPBOARD
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
);
5499 if (dpy
!= NULL
&& str
!= NULL
&& motion_type
>= 0
5500 && len
< 1024*1024 && len
> 0)
5502 XStoreBuffer(dpy
, (char *)str
, (int)len
, 0);
5511 clip_free_selection(cbd
)
5514 struct yankreg
*y_ptr
= y_current
;
5516 if (cbd
== &clip_plus
)
5517 y_current
= &y_regs
[PLUS_REGISTER
];
5519 y_current
= &y_regs
[STAR_REGISTER
];
5521 y_current
->y_size
= 0;
5526 * Get the selected text and put it in the gui selection register '*' or '+'.
5529 clip_get_selection(cbd
)
5532 struct yankreg
*old_y_previous
, *old_y_current
;
5536 int old_visual_mode
;
5538 colnr_T old_curswant
;
5539 int old_set_curswant
;
5540 pos_T old_op_start
, old_op_end
;
5546 if ((cbd
== &clip_plus
&& y_regs
[PLUS_REGISTER
].y_array
!= NULL
)
5547 || (cbd
== &clip_star
&& y_regs
[STAR_REGISTER
].y_array
!= NULL
))
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
;
5559 old_visual
= VIsual
;
5560 old_visual_mode
= VIsual_mode
;
5563 oa
.regname
= (cbd
== &clip_plus
? '+' : '*');
5564 oa
.op_type
= OP_YANK
;
5565 vim_memset(&ca
, 0, sizeof(ca
));
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
;
5580 VIsual
= old_visual
;
5581 VIsual_mode
= old_visual_mode
;
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 */
5595 clip_yank_selection(type
, str
, len
, cbd
)
5601 struct yankreg
*y_ptr
;
5603 if (cbd
== &clip_plus
)
5604 y_ptr
= &y_regs
[PLUS_REGISTER
];
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
5616 * Returns the motion type, or -1 for failure.
5619 clip_convert_selection(str
, len
, cbd
)
5628 struct yankreg
*y_ptr
;
5630 if (cbd
== &clip_plus
)
5631 y_ptr
= &y_regs
[PLUS_REGISTER
];
5633 y_ptr
= &y_regs
[STAR_REGISTER
];
5643 if (y_ptr
->y_array
== NULL
)
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
)
5655 p
= *str
= lalloc(*len
+ 1, TRUE
); /* add one to avoid zero */
5659 for (i
= 0, j
= 0; i
< (int)*len
; i
++, j
++)
5661 if (y_ptr
->y_array
[lnum
][j
] == '\n')
5663 else if (y_ptr
->y_array
[lnum
][j
] == NUL
)
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.
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
);
5703 #endif /* FEAT_CLIPBOARD || PROTO */
5706 #if defined(FEAT_DND) || defined(PROTO)
5708 * Replace the contents of the '~' register with str.
5711 dnd_yank_drag_data(str
, len
)
5715 struct yankreg
*curr
;
5718 y_current
= &y_regs
[TILDE_REGISTER
];
5720 str_to_reg(y_current
, MCHAR
, str
, len
, 0L);
5726 #if defined(FEAT_EVAL) || defined(PROTO)
5728 * Return the type of a register.
5729 * Used for getregtype()
5730 * Returns MAUTO for error.
5733 get_reg_type(regname
, reglen
)
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" */
5749 case Ctrl_W
: /* word under cursor */
5750 case Ctrl_A
: /* WORD (mnemonic All) under cursor */
5751 case '_': /* black hole: always empty */
5755 #ifdef FEAT_CLIPBOARD
5756 regname
= may_get_selection(regname
);
5759 /* Should we check for a valid name? */
5760 get_yank_register(regname
, FALSE
);
5762 if (y_current
->y_array
!= NULL
)
5765 if (reglen
!= NULL
&& y_current
->y_type
== MBLOCK
)
5766 *reglen
= y_current
->y_width
;
5768 return y_current
->y_type
;
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.
5779 get_reg_contents(regname
, allowexpr
, expr_src
)
5781 int allowexpr
; /* allow "=" register */
5782 int expr_src
; /* get expression for "=" register */
5789 /* Don't allow using an expression register inside an expression */
5795 return get_expr_line_src();
5796 return get_expr_line();
5801 if (regname
== '@') /* "@@" is used for unnamed register */
5804 /* check for valid regname */
5805 if (regname
!= NUL
&& !valid_yank_reg(regname
, FALSE
))
5808 #ifdef FEAT_CLIPBOARD
5809 regname
= may_get_selection(regname
);
5812 if (get_spec_reg(regname
, &retval
, &allocated
, FALSE
))
5817 retval
= vim_strsave(retval
);
5821 get_yank_register(regname
, FALSE
);
5822 if (y_current
->y_array
== NULL
)
5826 * Compute length of resulting string.
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
5836 if (y_current
->y_type
== MLINE
|| i
< y_current
->y_size
- 1)
5840 retval
= lalloc(len
+ 1, TRUE
);
5843 * Copy the lines of the yank register into the string.
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
5857 if (y_current
->y_type
== MLINE
|| i
< y_current
->y_size
- 1)
5858 retval
[len
++] = '\n';
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.
5876 write_reg_contents(name
, str
, maxlen
, must_append
)
5882 write_reg_contents_ex(name
, str
, maxlen
, must_append
, MAUTO
, 0L);
5886 write_reg_contents_ex(name
, str
, maxlen
, must_append
, yank_type
, block_len
)
5894 struct yankreg
*old_y_previous
, *old_y_current
;
5900 len
= (long)STRLEN(str
);
5902 /* Special case: '/' search pattern */
5905 set_last_search_pat(str
, RE_SEARCH
, TRUE
, TRUE
);
5914 p
= vim_strnsave(str
, (int)len
);
5919 s
= concat_str(get_expr_line_src(), p
);
5929 if (!valid_yank_reg(name
, TRUE
)) /* check for valid reg name */
5935 if (name
== '_') /* black hole: nothing to do */
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
)
5946 /* Just in case - make sure we don't use MBLOCK */
5947 if (yank_type
== MBLOCK
)
5950 if (yank_type
== MAUTO
)
5951 yank_type
= ((len
> 0 && (str
[len
- 1] == '\n' || str
[len
- 1] == '\r'))
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();
5960 /* ':let @" = "val"' should change the meaning of the "" register */
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
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 */
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 */
5993 if (y_ptr
->y_array
== NULL
) /* NULL means emtpy register */
5997 * Count the number of lines within the string
6000 for (i
= 0; i
< len
; i
++)
6003 if (type
== MCHAR
|| len
== 0 || str
[len
- 1] != '\n')
6006 ++newlines
; /* count extra newline at the end */
6008 if (y_ptr
->y_size
> 0 && y_ptr
->y_type
== MCHAR
)
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 */
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
;
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 */
6038 i
-= start
; /* i is now length of line */
6046 extra
= (int)STRLEN(y_ptr
->y_array
[lnum
]);
6050 s
= alloc((unsigned)(i
+ extra
+ 1));
6054 mch_memmove(s
, y_ptr
->y_array
[lnum
], (size_t)extra
);
6056 vim_free(y_ptr
->y_array
[lnum
]);
6058 mch_memmove(s
+ extra
, str
+ start
, (size_t)i
);
6061 y_ptr
->y_array
[lnum
++] = s
;
6062 while (--extra
>= 0)
6065 *s
= '\n'; /* replace NUL with newline */
6068 append
= FALSE
; /* only first line is appended */
6070 y_ptr
->y_type
= type
;
6071 y_ptr
->y_size
= lnum
;
6074 y_ptr
->y_width
= (blocklen
< 0 ? maxlen
- 1 : blocklen
);
6079 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
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.
6105 line_count_info(line
, wc
, cc
, limit
, eol_size
)
6117 for (i
= 0; line
[i
] && i
< limit
; )
6121 if (vim_isspace(line
[i
]))
6127 else if (!vim_isspace(line
[i
]))
6131 i
+= (*mb_ptr2len
)(line
+ i
);
6141 /* Add eol_size if the end of line was reached before hitting limit. */
6142 if (line
[i
] == NUL
&& i
< limit
)
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.)
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;
6170 long last_check
= 100000L;
6172 long line_count_selected
= 0;
6173 pos_T min_pos
, max_pos
;
6175 struct block_def bd
;
6179 * Compute the length of the file in characters.
6181 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
6183 MSG(_(no_lines_msg
));
6187 if (get_fileformat(curbuf
) == EOL_DOS
)
6195 if (lt(VIsual
, curwin
->w_cursor
))
6198 max_pos
= curwin
->w_cursor
;
6202 min_pos
= curwin
->w_cursor
;
6205 if (*p_sel
== 'e' && max_pos
.col
> 0)
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;
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
)
6237 last_check
= byte_count
+ 100000L;
6241 /* Do extra processing for VIsual mode. */
6243 && lnum
>= min_pos
.lnum
&& lnum
<= max_pos
.lnum
)
6248 switch (VIsual_mode
)
6251 # ifdef FEAT_VIRTUALEDIT
6252 virtual_op
= virtual_active();
6254 block_prep(&oparg
, &bd
, lnum
, 0);
6255 # ifdef FEAT_VIRTUALEDIT
6259 len
= (long)bd
.textlen
;
6267 colnr_T start_col
= (lnum
== min_pos
.lnum
)
6269 colnr_T end_col
= (lnum
== max_pos
.lnum
)
6270 ? max_pos
.col
- start_col
+ 1 : MAXCOL
;
6272 s
= ml_get(lnum
) + start_col
;
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
6284 && (long)STRLEN(s
) < len
)
6285 byte_count_cursor
-= eol_size
;
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
;
6314 if (VIsual_mode
== Ctrl_V
&& curwin
->w_curswant
< MAXCOL
)
6316 getvcols(curwin
, &min_pos
, &max_pos
, &min_pos
.col
,
6318 sprintf((char *)buf1
, _("%ld Cols; "),
6319 (long)(oparg
.end_vcol
- oparg
.start_vcol
+ 1));
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
);
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
);
6342 p
= ml_get_curline();
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
);
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
);
6367 byte_count
= bomb_size();
6369 sprintf((char *)IObuff
+ STRLEN(IObuff
), _("(+%ld for BOM)"),
6372 /* Don't shorten this message, the user asked for it. */
6374 p_shm
= (char_u
*)"";